Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /** |
| 16 | * @file |
| 17 | * @brief Session and session manager |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/session/session.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | #include <vnet/dpo/load_balance.h> |
| 23 | #include <vnet/fib/ip4_fib.h> |
| 24 | #include <vnet/session/application.h> |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 25 | #include <vnet/tcp/tcp.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Per-type vector of transport protocol virtual function tables |
| 29 | */ |
| 30 | static transport_proto_vft_t *tp_vfts; |
| 31 | |
| 32 | session_manager_main_t session_manager_main; |
| 33 | |
| 34 | /* |
| 35 | * Session lookup key; (src-ip, dst-ip, src-port, dst-port, session-type) |
| 36 | * Value: (owner thread index << 32 | session_index); |
| 37 | */ |
| 38 | static void |
| 39 | stream_session_table_add_for_tc (u8 sst, transport_connection_t * tc, |
| 40 | u64 value) |
| 41 | { |
| 42 | session_manager_main_t *smm = &session_manager_main; |
| 43 | session_kv4_t kv4; |
| 44 | session_kv6_t kv6; |
| 45 | |
| 46 | switch (sst) |
| 47 | { |
| 48 | case SESSION_TYPE_IP4_UDP: |
| 49 | case SESSION_TYPE_IP4_TCP: |
| 50 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 51 | kv4.value = value; |
| 52 | clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4, 1 /* is_add */ ); |
| 53 | break; |
| 54 | case SESSION_TYPE_IP6_UDP: |
| 55 | case SESSION_TYPE_IP6_TCP: |
| 56 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 57 | kv6.value = value; |
| 58 | clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6, 1 /* is_add */ ); |
| 59 | break; |
| 60 | default: |
| 61 | clib_warning ("Session type not supported"); |
| 62 | ASSERT (0); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | stream_session_table_add (session_manager_main_t * smm, stream_session_t * s, |
| 68 | u64 value) |
| 69 | { |
| 70 | transport_connection_t *tc; |
| 71 | |
| 72 | tc = tp_vfts[s->session_type].get_connection (s->connection_index, |
| 73 | s->thread_index); |
| 74 | stream_session_table_add_for_tc (s->session_type, tc, value); |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | stream_session_half_open_table_add (u8 sst, transport_connection_t * tc, |
| 79 | u64 value) |
| 80 | { |
| 81 | session_manager_main_t *smm = &session_manager_main; |
| 82 | session_kv4_t kv4; |
| 83 | session_kv6_t kv6; |
| 84 | |
| 85 | switch (sst) |
| 86 | { |
| 87 | case SESSION_TYPE_IP4_UDP: |
| 88 | case SESSION_TYPE_IP4_TCP: |
| 89 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 90 | kv4.value = value; |
| 91 | clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4, |
| 92 | 1 /* is_add */ ); |
| 93 | break; |
| 94 | case SESSION_TYPE_IP6_UDP: |
| 95 | case SESSION_TYPE_IP6_TCP: |
| 96 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 97 | kv6.value = value; |
| 98 | clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6, |
| 99 | 1 /* is_add */ ); |
| 100 | break; |
| 101 | default: |
| 102 | clib_warning ("Session type not supported"); |
| 103 | ASSERT (0); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | stream_session_table_del_for_tc (session_manager_main_t * smm, u8 sst, |
| 109 | transport_connection_t * tc) |
| 110 | { |
| 111 | session_kv4_t kv4; |
| 112 | session_kv6_t kv6; |
| 113 | |
| 114 | switch (sst) |
| 115 | { |
| 116 | case SESSION_TYPE_IP4_UDP: |
| 117 | case SESSION_TYPE_IP4_TCP: |
| 118 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 119 | return clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4, |
| 120 | 0 /* is_add */ ); |
| 121 | break; |
| 122 | case SESSION_TYPE_IP6_UDP: |
| 123 | case SESSION_TYPE_IP6_TCP: |
| 124 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 125 | return clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6, |
| 126 | 0 /* is_add */ ); |
| 127 | break; |
| 128 | default: |
| 129 | clib_warning ("Session type not supported"); |
| 130 | ASSERT (0); |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int |
| 137 | stream_session_table_del (session_manager_main_t * smm, stream_session_t * s) |
| 138 | { |
| 139 | transport_connection_t *ts; |
| 140 | |
| 141 | ts = tp_vfts[s->session_type].get_connection (s->connection_index, |
| 142 | s->thread_index); |
| 143 | return stream_session_table_del_for_tc (smm, s->session_type, ts); |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | stream_session_half_open_table_del (session_manager_main_t * smm, u8 sst, |
| 148 | transport_connection_t * tc) |
| 149 | { |
| 150 | session_kv4_t kv4; |
| 151 | session_kv6_t kv6; |
| 152 | |
| 153 | switch (sst) |
| 154 | { |
| 155 | case SESSION_TYPE_IP4_UDP: |
| 156 | case SESSION_TYPE_IP4_TCP: |
| 157 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 158 | clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4, |
| 159 | 0 /* is_add */ ); |
| 160 | break; |
| 161 | case SESSION_TYPE_IP6_UDP: |
| 162 | case SESSION_TYPE_IP6_TCP: |
| 163 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 164 | clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6, |
| 165 | 0 /* is_add */ ); |
| 166 | break; |
| 167 | default: |
| 168 | clib_warning ("Session type not supported"); |
| 169 | ASSERT (0); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | stream_session_t * |
| 174 | stream_session_lookup_listener4 (ip4_address_t * lcl, u16 lcl_port, u8 proto) |
| 175 | { |
| 176 | session_manager_main_t *smm = &session_manager_main; |
| 177 | session_kv4_t kv4; |
| 178 | int rv; |
| 179 | |
| 180 | make_v4_listener_kv (&kv4, lcl, lcl_port, proto); |
| 181 | rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4); |
| 182 | if (rv == 0) |
| 183 | return pool_elt_at_index (smm->listen_sessions[proto], (u32) kv4.value); |
| 184 | |
| 185 | /* Zero out the lcl ip */ |
| 186 | kv4.key[0] = 0; |
| 187 | rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4); |
| 188 | if (rv == 0) |
| 189 | return pool_elt_at_index (smm->listen_sessions[proto], kv4.value); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /** Looks up a session based on the 5-tuple passed as argument. |
| 195 | * |
| 196 | * First it tries to find an established session, if this fails, it tries |
| 197 | * finding a listener session if this fails, it tries a lookup with a |
| 198 | * wildcarded local source (listener bound to all interfaces) |
| 199 | */ |
| 200 | stream_session_t * |
| 201 | stream_session_lookup4 (ip4_address_t * lcl, ip4_address_t * rmt, |
| 202 | u16 lcl_port, u16 rmt_port, u8 proto, |
| 203 | u32 my_thread_index) |
| 204 | { |
| 205 | session_manager_main_t *smm = &session_manager_main; |
| 206 | session_kv4_t kv4; |
| 207 | int rv; |
| 208 | |
| 209 | /* Lookup session amongst established ones */ |
| 210 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
| 211 | rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4); |
| 212 | if (rv == 0) |
| 213 | return stream_session_get_tsi (kv4.value, my_thread_index); |
| 214 | |
| 215 | /* If nothing is found, check if any listener is available */ |
| 216 | return stream_session_lookup_listener4 (lcl, lcl_port, proto); |
| 217 | } |
| 218 | |
| 219 | stream_session_t * |
| 220 | stream_session_lookup_listener6 (ip6_address_t * lcl, u16 lcl_port, u8 proto) |
| 221 | { |
| 222 | session_manager_main_t *smm = &session_manager_main; |
| 223 | session_kv6_t kv6; |
| 224 | int rv; |
| 225 | |
| 226 | make_v6_listener_kv (&kv6, lcl, lcl_port, proto); |
| 227 | rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6); |
| 228 | if (rv == 0) |
| 229 | return pool_elt_at_index (smm->listen_sessions[proto], kv6.value); |
| 230 | |
| 231 | /* Zero out the lcl ip */ |
| 232 | kv6.key[0] = kv6.key[1] = 0; |
| 233 | rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6); |
| 234 | if (rv == 0) |
| 235 | return pool_elt_at_index (smm->listen_sessions[proto], kv6.value); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* Looks up a session based on the 5-tuple passed as argument. |
| 241 | * First it tries to find an established session, if this fails, it tries |
| 242 | * finding a listener session if this fails, it tries a lookup with a |
| 243 | * wildcarded local source (listener bound to all interfaces) */ |
| 244 | stream_session_t * |
| 245 | stream_session_lookup6 (ip6_address_t * lcl, ip6_address_t * rmt, |
| 246 | u16 lcl_port, u16 rmt_port, u8 proto, |
| 247 | u32 my_thread_index) |
| 248 | { |
| 249 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 250 | session_kv6_t kv6; |
| 251 | int rv; |
| 252 | |
| 253 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
| 254 | rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6); |
| 255 | if (rv == 0) |
| 256 | return stream_session_get_tsi (kv6.value, my_thread_index); |
| 257 | |
| 258 | /* If nothing is found, check if any listener is available */ |
| 259 | return stream_session_lookup_listener6 (lcl, lcl_port, proto); |
| 260 | } |
| 261 | |
| 262 | stream_session_t * |
| 263 | stream_session_lookup_listener (ip46_address_t * lcl, u16 lcl_port, u8 proto) |
| 264 | { |
| 265 | switch (proto) |
| 266 | { |
| 267 | case SESSION_TYPE_IP4_UDP: |
| 268 | case SESSION_TYPE_IP4_TCP: |
| 269 | return stream_session_lookup_listener4 (&lcl->ip4, lcl_port, proto); |
| 270 | break; |
| 271 | case SESSION_TYPE_IP6_UDP: |
| 272 | case SESSION_TYPE_IP6_TCP: |
| 273 | return stream_session_lookup_listener6 (&lcl->ip6, lcl_port, proto); |
| 274 | break; |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static u64 |
| 280 | stream_session_half_open_lookup (session_manager_main_t * smm, |
| 281 | ip46_address_t * lcl, ip46_address_t * rmt, |
| 282 | u16 lcl_port, u16 rmt_port, u8 proto) |
| 283 | { |
| 284 | session_kv4_t kv4; |
| 285 | session_kv6_t kv6; |
| 286 | int rv; |
| 287 | |
| 288 | switch (proto) |
| 289 | { |
| 290 | case SESSION_TYPE_IP4_UDP: |
| 291 | case SESSION_TYPE_IP4_TCP: |
| 292 | make_v4_ss_kv (&kv4, &lcl->ip4, &rmt->ip4, lcl_port, rmt_port, proto); |
| 293 | rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4); |
| 294 | |
| 295 | if (rv == 0) |
| 296 | return kv4.value; |
| 297 | |
| 298 | return (u64) ~ 0; |
| 299 | break; |
| 300 | case SESSION_TYPE_IP6_UDP: |
| 301 | case SESSION_TYPE_IP6_TCP: |
| 302 | make_v6_ss_kv (&kv6, &lcl->ip6, &rmt->ip6, lcl_port, rmt_port, proto); |
| 303 | rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6); |
| 304 | |
| 305 | if (rv == 0) |
| 306 | return kv6.value; |
| 307 | |
| 308 | return (u64) ~ 0; |
| 309 | break; |
| 310 | } |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | transport_connection_t * |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 315 | stream_session_lookup_transport4 (ip4_address_t * lcl, ip4_address_t * rmt, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 316 | u16 lcl_port, u16 rmt_port, u8 proto, |
| 317 | u32 my_thread_index) |
| 318 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 319 | session_manager_main_t *smm = &session_manager_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 320 | session_kv4_t kv4; |
| 321 | stream_session_t *s; |
| 322 | int rv; |
| 323 | |
| 324 | /* Lookup session amongst established ones */ |
| 325 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
| 326 | rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4); |
| 327 | if (rv == 0) |
| 328 | { |
| 329 | s = stream_session_get_tsi (kv4.value, my_thread_index); |
| 330 | |
| 331 | return tp_vfts[s->session_type].get_connection (s->connection_index, |
| 332 | my_thread_index); |
| 333 | } |
| 334 | |
| 335 | /* If nothing is found, check if any listener is available */ |
| 336 | s = stream_session_lookup_listener4 (lcl, lcl_port, proto); |
| 337 | if (s) |
| 338 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 339 | |
| 340 | /* Finally, try half-open connections */ |
| 341 | rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4); |
| 342 | if (rv == 0) |
| 343 | return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | transport_connection_t * |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 349 | stream_session_lookup_transport6 (ip6_address_t * lcl, ip6_address_t * rmt, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 350 | u16 lcl_port, u16 rmt_port, u8 proto, |
| 351 | u32 my_thread_index) |
| 352 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 353 | session_manager_main_t *smm = &session_manager_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 354 | stream_session_t *s; |
| 355 | session_kv6_t kv6; |
| 356 | int rv; |
| 357 | |
| 358 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
| 359 | rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6); |
| 360 | if (rv == 0) |
| 361 | { |
| 362 | s = stream_session_get_tsi (kv6.value, my_thread_index); |
| 363 | |
| 364 | return tp_vfts[s->session_type].get_connection (s->connection_index, |
| 365 | my_thread_index); |
| 366 | } |
| 367 | |
| 368 | /* If nothing is found, check if any listener is available */ |
| 369 | s = stream_session_lookup_listener6 (lcl, lcl_port, proto); |
| 370 | if (s) |
| 371 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 372 | |
| 373 | /* Finally, try half-open connections */ |
| 374 | rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6); |
| 375 | if (rv == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 376 | return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Allocate vpp event queue (once) per worker thread |
| 383 | */ |
| 384 | void |
| 385 | vpp_session_event_queue_allocate (session_manager_main_t * smm, |
| 386 | u32 thread_index) |
| 387 | { |
| 388 | api_main_t *am = &api_main; |
| 389 | void *oldheap; |
| 390 | |
| 391 | if (smm->vpp_event_queues[thread_index] == 0) |
| 392 | { |
| 393 | /* Allocate event fifo in the /vpe-api shared-memory segment */ |
| 394 | oldheap = svm_push_data_heap (am->vlib_rp); |
| 395 | |
| 396 | smm->vpp_event_queues[thread_index] = |
| 397 | unix_shared_memory_queue_init (2048 /* nels $$$$ config */ , |
| 398 | sizeof (session_fifo_event_t), |
| 399 | 0 /* consumer pid */ , |
| 400 | 0 |
| 401 | /* (do not) send signal when queue non-empty */ |
| 402 | ); |
| 403 | |
| 404 | svm_pop_heap (oldheap); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void |
| 409 | session_manager_get_segment_info (u32 index, u8 ** name, u32 * size) |
| 410 | { |
| 411 | svm_fifo_segment_private_t *s; |
| 412 | s = svm_fifo_get_segment (index); |
| 413 | *name = s->h->segment_name; |
| 414 | *size = s->ssvm.ssvm_size; |
| 415 | } |
| 416 | |
| 417 | always_inline int |
| 418 | session_manager_add_segment_i (session_manager_main_t * smm, |
| 419 | session_manager_t * sm, |
| 420 | u32 segment_size, u8 * segment_name) |
| 421 | { |
| 422 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
| 423 | int rv; |
| 424 | |
| 425 | memset (ca, 0, sizeof (*ca)); |
| 426 | |
| 427 | ca->segment_name = (char *) segment_name; |
| 428 | ca->segment_size = segment_size; |
| 429 | |
| 430 | rv = svm_fifo_segment_create (ca); |
| 431 | if (rv) |
| 432 | { |
| 433 | clib_warning ("svm_fifo_segment_create ('%s', %d) failed", |
| 434 | ca->segment_name, ca->segment_size); |
| 435 | vec_free (segment_name); |
| 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | vec_add1 (sm->segment_indices, ca->new_segment_index); |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int |
| 445 | session_manager_add_segment (session_manager_main_t * smm, |
| 446 | session_manager_t * sm) |
| 447 | { |
| 448 | u8 *segment_name; |
| 449 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
| 450 | u32 add_segment_size; |
| 451 | u32 default_segment_size = 128 << 10; |
| 452 | |
| 453 | memset (ca, 0, sizeof (*ca)); |
| 454 | segment_name = format (0, "%d-%d%c", getpid (), |
| 455 | smm->unique_segment_name_counter++, 0); |
| 456 | add_segment_size = |
| 457 | sm->add_segment_size ? sm->add_segment_size : default_segment_size; |
| 458 | |
| 459 | return session_manager_add_segment_i (smm, sm, add_segment_size, |
| 460 | segment_name); |
| 461 | } |
| 462 | |
| 463 | int |
| 464 | session_manager_add_first_segment (session_manager_main_t * smm, |
| 465 | session_manager_t * sm, u32 segment_size, |
| 466 | u8 ** segment_name) |
| 467 | { |
| 468 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
| 469 | memset (ca, 0, sizeof (*ca)); |
| 470 | *segment_name = format (0, "%d-%d%c", getpid (), |
| 471 | smm->unique_segment_name_counter++, 0); |
| 472 | return session_manager_add_segment_i (smm, sm, segment_size, *segment_name); |
| 473 | } |
| 474 | |
| 475 | void |
| 476 | session_manager_del (session_manager_main_t * smm, session_manager_t * sm) |
| 477 | { |
| 478 | u32 *deleted_sessions = 0; |
| 479 | u32 *deleted_thread_indices = 0; |
| 480 | int i, j; |
| 481 | |
| 482 | /* Across all fifo segments used by the server */ |
| 483 | for (j = 0; j < vec_len (sm->segment_indices); j++) |
| 484 | { |
| 485 | svm_fifo_segment_private_t *fifo_segment; |
| 486 | svm_fifo_t **fifos; |
| 487 | /* Vector of fifos allocated in the segment */ |
| 488 | fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]); |
| 489 | fifos = (svm_fifo_t **) fifo_segment->h->fifos; |
| 490 | |
| 491 | /* |
| 492 | * Remove any residual sessions from the session lookup table |
| 493 | * Don't bother deleting the individual fifos, we're going to |
| 494 | * throw away the fifo segment in a minute. |
| 495 | */ |
| 496 | for (i = 0; i < vec_len (fifos); i++) |
| 497 | { |
| 498 | svm_fifo_t *fifo; |
| 499 | u32 session_index, thread_index; |
| 500 | stream_session_t *session; |
| 501 | |
| 502 | fifo = fifos[i]; |
| 503 | session_index = fifo->server_session_index; |
| 504 | thread_index = fifo->server_thread_index; |
| 505 | |
| 506 | session = pool_elt_at_index (smm->sessions[thread_index], |
| 507 | session_index); |
| 508 | |
| 509 | /* Add to the deleted_sessions vector (once!) */ |
| 510 | if (!session->is_deleted) |
| 511 | { |
| 512 | session->is_deleted = 1; |
| 513 | vec_add1 (deleted_sessions, |
| 514 | session - smm->sessions[thread_index]); |
| 515 | vec_add1 (deleted_thread_indices, thread_index); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | for (i = 0; i < vec_len (deleted_sessions); i++) |
| 520 | { |
| 521 | stream_session_t *session; |
| 522 | |
| 523 | session = |
| 524 | pool_elt_at_index (smm->sessions[deleted_thread_indices[i]], |
| 525 | deleted_sessions[i]); |
| 526 | |
| 527 | /* Instead of directly removing the session call disconnect */ |
| 528 | stream_session_disconnect (session); |
| 529 | |
| 530 | /* |
| 531 | stream_session_table_del (smm, session); |
| 532 | pool_put(smm->sessions[deleted_thread_indices[i]], session); |
| 533 | */ |
| 534 | } |
| 535 | |
| 536 | vec_reset_length (deleted_sessions); |
| 537 | vec_reset_length (deleted_thread_indices); |
| 538 | |
| 539 | /* Instead of removing the segment, test when removing the session if |
| 540 | * the segment can be removed |
| 541 | */ |
| 542 | /* svm_fifo_segment_delete (fifo_segment); */ |
| 543 | } |
| 544 | |
| 545 | vec_free (deleted_sessions); |
| 546 | vec_free (deleted_thread_indices); |
| 547 | } |
| 548 | |
| 549 | int |
| 550 | session_manager_allocate_session_fifos (session_manager_main_t * smm, |
| 551 | session_manager_t * sm, |
| 552 | svm_fifo_t ** server_rx_fifo, |
| 553 | svm_fifo_t ** server_tx_fifo, |
| 554 | u32 * fifo_segment_index, |
| 555 | u8 * added_a_segment) |
| 556 | { |
| 557 | svm_fifo_segment_private_t *fifo_segment; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 558 | u32 fifo_size, default_fifo_size = 128 << 10; /* TODO config */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 559 | int i; |
| 560 | |
| 561 | *added_a_segment = 0; |
| 562 | |
| 563 | /* Allocate svm fifos */ |
| 564 | ASSERT (vec_len (sm->segment_indices)); |
| 565 | |
| 566 | again: |
| 567 | for (i = 0; i < vec_len (sm->segment_indices); i++) |
| 568 | { |
| 569 | *fifo_segment_index = sm->segment_indices[i]; |
| 570 | fifo_segment = svm_fifo_get_segment (*fifo_segment_index); |
| 571 | |
| 572 | fifo_size = sm->rx_fifo_size; |
| 573 | fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size; |
| 574 | *server_rx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size); |
| 575 | |
| 576 | fifo_size = sm->tx_fifo_size; |
| 577 | fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size; |
| 578 | *server_tx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size); |
| 579 | |
| 580 | if (*server_rx_fifo == 0) |
| 581 | { |
| 582 | /* This would be very odd, but handle it... */ |
| 583 | if (*server_tx_fifo != 0) |
| 584 | { |
| 585 | svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo); |
| 586 | *server_tx_fifo = 0; |
| 587 | } |
| 588 | continue; |
| 589 | } |
| 590 | if (*server_tx_fifo == 0) |
| 591 | { |
| 592 | if (*server_rx_fifo != 0) |
| 593 | { |
| 594 | svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo); |
| 595 | *server_rx_fifo = 0; |
| 596 | } |
| 597 | continue; |
| 598 | } |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | /* See if we're supposed to create another segment */ |
| 603 | if (*server_rx_fifo == 0) |
| 604 | { |
| 605 | if (sm->add_segment) |
| 606 | { |
| 607 | if (*added_a_segment) |
| 608 | { |
| 609 | clib_warning ("added a segment, still cant allocate a fifo"); |
| 610 | return SESSION_ERROR_NEW_SEG_NO_SPACE; |
| 611 | } |
| 612 | |
| 613 | if (session_manager_add_segment (smm, sm)) |
| 614 | return VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 615 | |
| 616 | *added_a_segment = 1; |
| 617 | goto again; |
| 618 | } |
| 619 | else |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 620 | { |
| 621 | clib_warning ("No space to allocate fifos!"); |
| 622 | return SESSION_ERROR_NO_SPACE; |
| 623 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 624 | } |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | int |
| 629 | stream_session_create_i (session_manager_main_t * smm, application_t * app, |
| 630 | transport_connection_t * tc, |
| 631 | stream_session_t ** ret_s) |
| 632 | { |
| 633 | int rv; |
| 634 | svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0; |
| 635 | u32 fifo_segment_index; |
| 636 | u32 pool_index, seg_size; |
| 637 | stream_session_t *s; |
| 638 | u64 value; |
| 639 | u32 thread_index = tc->thread_index; |
| 640 | session_manager_t *sm; |
| 641 | u8 segment_added; |
| 642 | u8 *seg_name; |
| 643 | |
| 644 | sm = session_manager_get (app->session_manager_index); |
| 645 | |
| 646 | /* Check the API queue */ |
| 647 | if (app->mode == APP_SERVER && application_api_queue_is_full (app)) |
| 648 | return SESSION_ERROR_API_QUEUE_FULL; |
| 649 | |
| 650 | if ((rv = session_manager_allocate_session_fifos (smm, sm, &server_rx_fifo, |
| 651 | &server_tx_fifo, |
| 652 | &fifo_segment_index, |
| 653 | &segment_added))) |
| 654 | return rv; |
| 655 | |
| 656 | if (segment_added && app->mode == APP_SERVER) |
| 657 | { |
| 658 | /* Send an API message to the external server, to map new segment */ |
| 659 | ASSERT (app->cb_fns.add_segment_callback); |
| 660 | |
| 661 | session_manager_get_segment_info (fifo_segment_index, &seg_name, |
| 662 | &seg_size); |
| 663 | if (app->cb_fns.add_segment_callback (app->api_client_index, seg_name, |
| 664 | seg_size)) |
| 665 | return VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 666 | } |
| 667 | |
| 668 | /* Create the session */ |
| 669 | pool_get (smm->sessions[thread_index], s); |
| 670 | memset (s, 0, sizeof (*s)); |
| 671 | |
| 672 | /* Initialize backpointers */ |
| 673 | pool_index = s - smm->sessions[thread_index]; |
| 674 | server_rx_fifo->server_session_index = pool_index; |
| 675 | server_rx_fifo->server_thread_index = thread_index; |
| 676 | |
| 677 | server_tx_fifo->server_session_index = pool_index; |
| 678 | server_tx_fifo->server_thread_index = thread_index; |
| 679 | |
| 680 | s->server_rx_fifo = server_rx_fifo; |
| 681 | s->server_tx_fifo = server_tx_fifo; |
| 682 | |
| 683 | /* Initialize state machine, such as it is... */ |
| 684 | s->session_type = app->session_type; |
| 685 | s->session_state = SESSION_STATE_CONNECTING; |
| 686 | s->app_index = application_get_index (app); |
| 687 | s->server_segment_index = fifo_segment_index; |
| 688 | s->thread_index = thread_index; |
| 689 | s->session_index = pool_index; |
| 690 | |
| 691 | /* Attach transport to session */ |
| 692 | s->connection_index = tc->c_index; |
| 693 | |
| 694 | /* Attach session to transport */ |
| 695 | tc->s_index = s->session_index; |
| 696 | |
| 697 | /* Add to the main lookup table */ |
| 698 | value = (((u64) thread_index) << 32) | (u64) s->session_index; |
| 699 | stream_session_table_add_for_tc (app->session_type, tc, value); |
| 700 | |
| 701 | *ret_s = s; |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * Enqueue data for delivery to session peer. Does not notify peer of enqueue |
| 708 | * event but on request can queue notification events for later delivery by |
| 709 | * calling stream_server_flush_enqueue_events(). |
| 710 | * |
| 711 | * @param tc Transport connection which is to be enqueued data |
| 712 | * @param data Data to be enqueued |
| 713 | * @param len Length of data to be enqueued |
| 714 | * @param queue_event Flag to indicate if peer is to be notified or if event |
| 715 | * is to be queued. The former is useful when more data is |
| 716 | * enqueued and only one event is to be generated. |
| 717 | * @return Number of bytes enqueued or a negative value if enqueueing failed. |
| 718 | */ |
| 719 | int |
| 720 | stream_session_enqueue_data (transport_connection_t * tc, u8 * data, u16 len, |
| 721 | u8 queue_event) |
| 722 | { |
| 723 | stream_session_t *s; |
| 724 | int enqueued; |
| 725 | |
| 726 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 727 | |
| 728 | /* Make sure there's enough space left. We might've filled the pipes */ |
| 729 | if (PREDICT_FALSE (len > svm_fifo_max_enqueue (s->server_rx_fifo))) |
| 730 | return -1; |
| 731 | |
| 732 | enqueued = svm_fifo_enqueue_nowait (s->server_rx_fifo, s->pid, len, data); |
| 733 | |
| 734 | if (queue_event) |
| 735 | { |
| 736 | /* Queue RX event on this fifo. Eventually these will need to be flushed |
| 737 | * by calling stream_server_flush_enqueue_events () */ |
| 738 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 739 | u32 thread_index = s->thread_index; |
| 740 | u32 my_enqueue_epoch = smm->current_enqueue_epoch[thread_index]; |
| 741 | |
| 742 | if (s->enqueue_epoch != my_enqueue_epoch) |
| 743 | { |
| 744 | s->enqueue_epoch = my_enqueue_epoch; |
| 745 | vec_add1 (smm->session_indices_to_enqueue_by_thread[thread_index], |
| 746 | s - smm->sessions[thread_index]); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | return enqueued; |
| 751 | } |
| 752 | |
| 753 | /** Check if we have space in rx fifo to push more bytes */ |
| 754 | u8 |
| 755 | stream_session_no_space (transport_connection_t * tc, u32 thread_index, |
| 756 | u16 data_len) |
| 757 | { |
| 758 | stream_session_t *s = stream_session_get (tc->c_index, thread_index); |
| 759 | |
| 760 | if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY)) |
| 761 | return 1; |
| 762 | |
| 763 | if (data_len > svm_fifo_max_enqueue (s->server_rx_fifo)) |
| 764 | return 1; |
| 765 | |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | u32 |
| 770 | stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer, |
| 771 | u32 offset, u32 max_bytes) |
| 772 | { |
| 773 | stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index); |
| 774 | return svm_fifo_peek (s->server_tx_fifo, s->pid, offset, max_bytes, buffer); |
| 775 | } |
| 776 | |
| 777 | u32 |
| 778 | stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes) |
| 779 | { |
| 780 | stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index); |
| 781 | return svm_fifo_dequeue_drop (s->server_tx_fifo, s->pid, max_bytes); |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Notify session peer that new data has been enqueued. |
| 786 | * |
| 787 | * @param s Stream session for which the event is to be generated. |
| 788 | * @param block Flag to indicate if call should block if event queue is full. |
| 789 | * |
| 790 | * @return 0 on succes or negative number if failed to send notification. |
| 791 | */ |
| 792 | static int |
| 793 | stream_session_enqueue_notify (stream_session_t * s, u8 block) |
| 794 | { |
| 795 | application_t *app; |
| 796 | session_fifo_event_t evt; |
| 797 | unix_shared_memory_queue_t *q; |
| 798 | static u32 serial_number; |
| 799 | |
| 800 | if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED)) |
| 801 | return 0; |
| 802 | |
| 803 | /* Get session's server */ |
| 804 | app = application_get (s->app_index); |
| 805 | |
| 806 | /* Fabricate event */ |
| 807 | evt.fifo = s->server_rx_fifo; |
| 808 | evt.event_type = FIFO_EVENT_SERVER_RX; |
| 809 | evt.event_id = serial_number++; |
| 810 | evt.enqueue_length = svm_fifo_max_dequeue (s->server_rx_fifo); |
| 811 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 812 | /* Built-in server? Hand event to the callback... */ |
| 813 | if (app->cb_fns.builtin_server_rx_callback) |
| 814 | return app->cb_fns.builtin_server_rx_callback (s, &evt); |
| 815 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 816 | /* Add event to server's event queue */ |
| 817 | q = app->event_queue; |
| 818 | |
| 819 | /* Based on request block (or not) for lack of space */ |
| 820 | if (block || PREDICT_TRUE (q->cursize < q->maxsize)) |
| 821 | unix_shared_memory_queue_add (app->event_queue, (u8 *) & evt, |
| 822 | 0 /* do wait for mutex */ ); |
| 823 | else |
| 824 | return -1; |
| 825 | |
| 826 | if (1) |
| 827 | { |
| 828 | ELOG_TYPE_DECLARE (e) = |
| 829 | { |
| 830 | .format = "evt-enqueue: id %d length %d",.format_args = "i4i4",}; |
| 831 | struct |
| 832 | { |
| 833 | u32 data[2]; |
| 834 | } *ed; |
| 835 | ed = ELOG_DATA (&vlib_global_main.elog_main, e); |
| 836 | ed->data[0] = evt.event_id; |
| 837 | ed->data[1] = evt.enqueue_length; |
| 838 | } |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Flushes queue of sessions that are to be notified of new data |
| 845 | * enqueued events. |
| 846 | * |
| 847 | * @param thread_index Thread index for which the flush is to be performed. |
| 848 | * @return 0 on success or a positive number indicating the number of |
| 849 | * failures due to API queue being full. |
| 850 | */ |
| 851 | int |
| 852 | session_manager_flush_enqueue_events (u32 thread_index) |
| 853 | { |
| 854 | session_manager_main_t *smm = &session_manager_main; |
| 855 | u32 *session_indices_to_enqueue; |
| 856 | int i, errors = 0; |
| 857 | |
| 858 | session_indices_to_enqueue = |
| 859 | smm->session_indices_to_enqueue_by_thread[thread_index]; |
| 860 | |
| 861 | for (i = 0; i < vec_len (session_indices_to_enqueue); i++) |
| 862 | { |
| 863 | stream_session_t *s0; |
| 864 | |
| 865 | /* Get session */ |
| 866 | s0 = stream_session_get (session_indices_to_enqueue[i], thread_index); |
| 867 | if (stream_session_enqueue_notify (s0, 0 /* don't block */ )) |
| 868 | { |
| 869 | errors++; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | vec_reset_length (session_indices_to_enqueue); |
| 874 | |
| 875 | smm->session_indices_to_enqueue_by_thread[thread_index] = |
| 876 | session_indices_to_enqueue; |
| 877 | |
| 878 | /* Increment enqueue epoch for next round */ |
| 879 | smm->current_enqueue_epoch[thread_index]++; |
| 880 | |
| 881 | return errors; |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Start listening on server's ip/port pair for requested transport. |
| 886 | * |
| 887 | * Creates a 'dummy' stream session with state LISTENING to be used in session |
| 888 | * lookups, prior to establishing connection. Requests transport to build |
| 889 | * it's own specific listening connection. |
| 890 | */ |
| 891 | int |
| 892 | stream_session_start_listen (u32 server_index, ip46_address_t * ip, u16 port) |
| 893 | { |
| 894 | session_manager_main_t *smm = &session_manager_main; |
| 895 | stream_session_t *s; |
| 896 | transport_connection_t *tc; |
| 897 | application_t *srv; |
| 898 | u32 tci; |
| 899 | |
| 900 | srv = application_get (server_index); |
| 901 | |
| 902 | pool_get (smm->listen_sessions[srv->session_type], s); |
| 903 | memset (s, 0, sizeof (*s)); |
| 904 | |
| 905 | s->session_type = srv->session_type; |
| 906 | s->session_state = SESSION_STATE_LISTENING; |
| 907 | s->session_index = s - smm->listen_sessions[srv->session_type]; |
| 908 | s->app_index = srv->index; |
| 909 | |
| 910 | /* Transport bind/listen */ |
| 911 | tci = tp_vfts[srv->session_type].bind (smm->vlib_main, s->session_index, ip, |
| 912 | port); |
| 913 | |
| 914 | /* Attach transport to session */ |
| 915 | s->connection_index = tci; |
| 916 | tc = tp_vfts[srv->session_type].get_listener (tci); |
| 917 | |
| 918 | srv->session_index = s->session_index; |
| 919 | |
| 920 | /* Add to the main lookup table */ |
| 921 | stream_session_table_add_for_tc (s->session_type, tc, s->session_index); |
| 922 | |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | void |
| 927 | stream_session_stop_listen (u32 server_index) |
| 928 | { |
| 929 | session_manager_main_t *smm = &session_manager_main; |
| 930 | stream_session_t *listener; |
| 931 | transport_connection_t *tc; |
| 932 | application_t *srv; |
| 933 | |
| 934 | srv = application_get (server_index); |
| 935 | listener = pool_elt_at_index (smm->listen_sessions[srv->session_type], |
| 936 | srv->session_index); |
| 937 | |
| 938 | tc = tp_vfts[srv->session_type].get_listener (listener->connection_index); |
| 939 | stream_session_table_del_for_tc (smm, listener->session_type, tc); |
| 940 | |
| 941 | tp_vfts[srv->session_type].unbind (smm->vlib_main, |
| 942 | listener->connection_index); |
| 943 | pool_put (smm->listen_sessions[srv->session_type], listener); |
| 944 | } |
| 945 | |
| 946 | int |
| 947 | connect_server_add_segment_cb (application_t * ss, char *segment_name, |
| 948 | u32 segment_size) |
| 949 | { |
| 950 | /* Does exactly nothing, but die */ |
| 951 | ASSERT (0); |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | void |
| 956 | connects_session_manager_init (session_manager_main_t * smm, u8 session_type) |
| 957 | { |
| 958 | session_manager_t *sm; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 959 | u32 connect_fifo_size = 256 << 10; /* Config? */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 960 | u32 default_segment_size = 1 << 20; |
| 961 | |
| 962 | pool_get (smm->session_managers, sm); |
| 963 | memset (sm, 0, sizeof (*sm)); |
| 964 | |
| 965 | sm->add_segment_size = default_segment_size; |
| 966 | sm->rx_fifo_size = connect_fifo_size; |
| 967 | sm->tx_fifo_size = connect_fifo_size; |
| 968 | sm->add_segment = 1; |
| 969 | |
| 970 | session_manager_add_segment (smm, sm); |
| 971 | smm->connect_manager_index[session_type] = sm - smm->session_managers; |
| 972 | } |
| 973 | |
| 974 | void |
| 975 | stream_session_connect_notify (transport_connection_t * tc, u8 sst, |
| 976 | u8 is_fail) |
| 977 | { |
| 978 | session_manager_main_t *smm = &session_manager_main; |
| 979 | application_t *app; |
| 980 | stream_session_t *new_s = 0; |
| 981 | u64 value; |
| 982 | |
| 983 | value = stream_session_half_open_lookup (smm, &tc->lcl_ip, &tc->rmt_ip, |
| 984 | tc->lcl_port, tc->rmt_port, |
| 985 | tc->proto); |
| 986 | if (value == HALF_OPEN_LOOKUP_INVALID_VALUE) |
| 987 | { |
| 988 | clib_warning ("This can't be good!"); |
| 989 | return; |
| 990 | } |
| 991 | |
| 992 | app = application_get (value >> 32); |
| 993 | |
| 994 | if (!is_fail) |
| 995 | { |
| 996 | /* Create new session (server segments are allocated if needed) */ |
| 997 | if (stream_session_create_i (smm, app, tc, &new_s)) |
| 998 | return; |
| 999 | |
| 1000 | app->session_index = stream_session_get_index (new_s); |
| 1001 | app->thread_index = new_s->thread_index; |
| 1002 | |
| 1003 | /* Allocate vpp event queue for this thread if needed */ |
| 1004 | vpp_session_event_queue_allocate (smm, tc->thread_index); |
| 1005 | } |
| 1006 | |
| 1007 | /* Notify client */ |
| 1008 | app->cb_fns.session_connected_callback (app->api_client_index, new_s, |
| 1009 | is_fail); |
| 1010 | |
| 1011 | /* Cleanup session lookup */ |
| 1012 | stream_session_half_open_table_del (smm, sst, tc); |
| 1013 | } |
| 1014 | |
| 1015 | void |
| 1016 | stream_session_accept_notify (transport_connection_t * tc) |
| 1017 | { |
| 1018 | application_t *server; |
| 1019 | stream_session_t *s; |
| 1020 | |
| 1021 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 1022 | server = application_get (s->app_index); |
| 1023 | server->cb_fns.session_accept_callback (s); |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * Notification from transport that connection is being closed. |
| 1028 | * |
| 1029 | * A disconnect is sent to application but state is not removed. Once |
| 1030 | * disconnect is acknowledged by application, session disconnect is called. |
| 1031 | * Ultimately this leads to close being called on transport (passive close). |
| 1032 | */ |
| 1033 | void |
| 1034 | stream_session_disconnect_notify (transport_connection_t * tc) |
| 1035 | { |
| 1036 | application_t *server; |
| 1037 | stream_session_t *s; |
| 1038 | |
| 1039 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 1040 | server = application_get (s->app_index); |
| 1041 | server->cb_fns.session_disconnect_callback (s); |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * Cleans up session and associated app if needed. |
| 1046 | */ |
| 1047 | void |
| 1048 | stream_session_delete (stream_session_t * s) |
| 1049 | { |
| 1050 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 1051 | svm_fifo_segment_private_t *fifo_segment; |
| 1052 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1053 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1054 | /* Delete from the main lookup table. */ |
| 1055 | stream_session_table_del (smm, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1056 | |
| 1057 | /* Cleanup fifo segments */ |
| 1058 | fifo_segment = svm_fifo_get_segment (s->server_segment_index); |
| 1059 | svm_fifo_segment_free_fifo (fifo_segment, s->server_rx_fifo); |
| 1060 | svm_fifo_segment_free_fifo (fifo_segment, s->server_tx_fifo); |
| 1061 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1062 | app = application_get_if_valid (s->app_index); |
| 1063 | |
| 1064 | /* No app. A possibility: after disconnect application called unbind */ |
| 1065 | if (!app) |
| 1066 | return; |
| 1067 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1068 | if (app->mode == APP_CLIENT) |
| 1069 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1070 | /* Cleanup app if client */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1071 | application_del (app); |
| 1072 | } |
| 1073 | else if (app->mode == APP_SERVER) |
| 1074 | { |
| 1075 | session_manager_t *sm; |
| 1076 | svm_fifo_segment_private_t *fifo_segment; |
| 1077 | svm_fifo_t **fifos; |
| 1078 | u32 fifo_index; |
| 1079 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1080 | /* For server, see if any segments can be removed */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1081 | sm = session_manager_get (app->session_manager_index); |
| 1082 | |
| 1083 | /* Delete fifo */ |
| 1084 | fifo_segment = svm_fifo_get_segment (s->server_segment_index); |
| 1085 | fifos = (svm_fifo_t **) fifo_segment->h->fifos; |
| 1086 | |
| 1087 | fifo_index = svm_fifo_segment_index (fifo_segment); |
| 1088 | |
| 1089 | /* Remove segment only if it holds no fifos and not the first */ |
| 1090 | if (sm->segment_indices[0] != fifo_index && vec_len (fifos) == 0) |
| 1091 | svm_fifo_segment_delete (fifo_segment); |
| 1092 | } |
| 1093 | |
| 1094 | pool_put (smm->sessions[s->thread_index], s); |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * Notification from transport that connection is being deleted |
| 1099 | * |
| 1100 | * This should be called only on previously fully established sessions. For |
| 1101 | * instance failed connects should call stream_session_connect_notify and |
| 1102 | * indicate that the connect has failed. |
| 1103 | */ |
| 1104 | void |
| 1105 | stream_session_delete_notify (transport_connection_t * tc) |
| 1106 | { |
| 1107 | stream_session_t *s; |
| 1108 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1109 | /* App might've been removed already */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1110 | s = stream_session_get_if_valid (tc->s_index, tc->thread_index); |
| 1111 | if (!s) |
| 1112 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1113 | return; |
| 1114 | } |
| 1115 | stream_session_delete (s); |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * Notify application that connection has been reset. |
| 1120 | */ |
| 1121 | void |
| 1122 | stream_session_reset_notify (transport_connection_t * tc) |
| 1123 | { |
| 1124 | stream_session_t *s; |
| 1125 | application_t *app; |
| 1126 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 1127 | |
| 1128 | app = application_get (s->app_index); |
| 1129 | app->cb_fns.session_reset_callback (s); |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * Accept a stream session. Optionally ping the server by callback. |
| 1134 | */ |
| 1135 | int |
| 1136 | stream_session_accept (transport_connection_t * tc, u32 listener_index, |
| 1137 | u8 sst, u8 notify) |
| 1138 | { |
| 1139 | session_manager_main_t *smm = &session_manager_main; |
| 1140 | application_t *server; |
| 1141 | stream_session_t *s, *listener; |
| 1142 | |
| 1143 | int rv; |
| 1144 | |
| 1145 | /* Find the server */ |
| 1146 | listener = pool_elt_at_index (smm->listen_sessions[sst], listener_index); |
| 1147 | server = application_get (listener->app_index); |
| 1148 | |
| 1149 | if ((rv = stream_session_create_i (smm, server, tc, &s))) |
| 1150 | return rv; |
| 1151 | |
| 1152 | /* Allocate vpp event queue for this thread if needed */ |
| 1153 | vpp_session_event_queue_allocate (smm, tc->thread_index); |
| 1154 | |
| 1155 | /* Shoulder-tap the server */ |
| 1156 | if (notify) |
| 1157 | { |
| 1158 | server->cb_fns.session_accept_callback (s); |
| 1159 | } |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1164 | int |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1165 | stream_session_open (u8 sst, ip46_address_t * addr, u16 port_host_byte_order, |
| 1166 | u32 app_index) |
| 1167 | { |
| 1168 | transport_connection_t *tc; |
| 1169 | u32 tci; |
| 1170 | u64 value; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1171 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1172 | |
| 1173 | /* Ask transport to open connection */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1174 | rv = tp_vfts[sst].open (addr, port_host_byte_order); |
| 1175 | if (rv < 0) |
| 1176 | { |
| 1177 | clib_warning ("Transport failed to open connection."); |
| 1178 | return VNET_API_ERROR_SESSION_CONNECT_FAIL; |
| 1179 | } |
| 1180 | |
| 1181 | tci = rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1182 | |
| 1183 | /* Get transport connection */ |
| 1184 | tc = tp_vfts[sst].get_half_open (tci); |
| 1185 | |
| 1186 | /* Store api_client_index and transport connection index */ |
| 1187 | value = (((u64) app_index) << 32) | (u64) tc->c_index; |
| 1188 | |
| 1189 | /* Add to the half-open lookup table */ |
| 1190 | stream_session_half_open_table_add (sst, tc, value); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1191 | |
| 1192 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * Disconnect session and propagate to transport. This should eventually |
| 1197 | * result in a delete notification that allows us to cleanup session state. |
| 1198 | * Called for both active/passive disconnects. |
| 1199 | */ |
| 1200 | void |
| 1201 | stream_session_disconnect (stream_session_t * s) |
| 1202 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1203 | s->session_state = SESSION_STATE_CLOSED; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1204 | tp_vfts[s->session_type].close (s->connection_index, s->thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | /** |
| 1208 | * Cleanup transport and session state. |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1209 | * |
| 1210 | * Notify transport of the cleanup, wait for a delete notify to actually |
| 1211 | * remove the session state. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1212 | */ |
| 1213 | void |
| 1214 | stream_session_cleanup (stream_session_t * s) |
| 1215 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1216 | session_manager_main_t *smm = &session_manager_main; |
| 1217 | int rv; |
| 1218 | |
| 1219 | s->session_state = SESSION_STATE_CLOSED; |
| 1220 | |
| 1221 | /* Delete from the main lookup table to avoid more enqueues */ |
| 1222 | rv = stream_session_table_del (smm, s); |
| 1223 | if (rv) |
| 1224 | clib_warning ("hash delete error, rv %d", rv); |
| 1225 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1226 | tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | void |
| 1230 | session_register_transport (u8 type, const transport_proto_vft_t * vft) |
| 1231 | { |
| 1232 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 1233 | |
| 1234 | vec_validate (tp_vfts, type); |
| 1235 | tp_vfts[type] = *vft; |
| 1236 | |
| 1237 | /* If an offset function is provided, then peek instead of dequeue */ |
| 1238 | smm->session_rx_fns[type] = |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1239 | (vft->tx_fifo_offset) ? session_tx_fifo_peek_and_snd : |
| 1240 | session_tx_fifo_dequeue_and_snd; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | transport_proto_vft_t * |
| 1244 | session_get_transport_vft (u8 type) |
| 1245 | { |
| 1246 | if (type >= vec_len (tp_vfts)) |
| 1247 | return 0; |
| 1248 | return &tp_vfts[type]; |
| 1249 | } |
| 1250 | |
| 1251 | static clib_error_t * |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1252 | session_manager_main_enable (vlib_main_t * vm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1253 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1254 | session_manager_main_t *smm = &session_manager_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1255 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 1256 | u32 num_threads; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1257 | int i; |
| 1258 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1259 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 1260 | |
| 1261 | if (num_threads < 1) |
| 1262 | return clib_error_return (0, "n_thread_stacks not set"); |
| 1263 | |
| 1264 | /* $$$ config parameters */ |
| 1265 | svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ , |
| 1266 | 20 /* timeout in seconds */ ); |
| 1267 | |
| 1268 | /* configure per-thread ** vectors */ |
| 1269 | vec_validate (smm->sessions, num_threads - 1); |
| 1270 | vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1); |
| 1271 | vec_validate (smm->tx_buffers, num_threads - 1); |
| 1272 | vec_validate (smm->fifo_events, num_threads - 1); |
| 1273 | vec_validate (smm->evts_partially_read, num_threads - 1); |
| 1274 | vec_validate (smm->current_enqueue_epoch, num_threads - 1); |
| 1275 | vec_validate (smm->vpp_event_queues, num_threads - 1); |
| 1276 | |
| 1277 | /* $$$$ preallocate hack config parameter */ |
| 1278 | for (i = 0; i < 200000; i++) |
| 1279 | { |
| 1280 | stream_session_t *ss; |
| 1281 | pool_get (smm->sessions[0], ss); |
| 1282 | memset (ss, 0, sizeof (*ss)); |
| 1283 | } |
| 1284 | |
| 1285 | for (i = 0; i < 200000; i++) |
| 1286 | pool_put_index (smm->sessions[0], i); |
| 1287 | |
| 1288 | clib_bihash_init_16_8 (&smm->v4_session_hash, "v4 session table", |
| 1289 | 200000 /* $$$$ config parameter nbuckets */ , |
| 1290 | (64 << 20) /*$$$ config parameter table size */ ); |
| 1291 | clib_bihash_init_48_8 (&smm->v6_session_hash, "v6 session table", |
| 1292 | 200000 /* $$$$ config parameter nbuckets */ , |
| 1293 | (64 << 20) /*$$$ config parameter table size */ ); |
| 1294 | |
| 1295 | clib_bihash_init_16_8 (&smm->v4_half_open_hash, "v4 half-open table", |
| 1296 | 200000 /* $$$$ config parameter nbuckets */ , |
| 1297 | (64 << 20) /*$$$ config parameter table size */ ); |
| 1298 | clib_bihash_init_48_8 (&smm->v6_half_open_hash, "v6 half-open table", |
| 1299 | 200000 /* $$$$ config parameter nbuckets */ , |
| 1300 | (64 << 20) /*$$$ config parameter table size */ ); |
| 1301 | |
| 1302 | for (i = 0; i < SESSION_N_TYPES; i++) |
| 1303 | smm->connect_manager_index[i] = INVALID_INDEX; |
| 1304 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1305 | smm->is_enabled = 1; |
| 1306 | |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 1307 | /* Enable TCP transport */ |
| 1308 | vnet_tcp_enable_disable (vm, 1); |
| 1309 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1310 | return 0; |
| 1311 | } |
| 1312 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1313 | clib_error_t * |
| 1314 | vnet_session_enable_disable (vlib_main_t * vm, u8 is_en) |
| 1315 | { |
| 1316 | if (is_en) |
| 1317 | { |
| 1318 | if (session_manager_main.is_enabled) |
| 1319 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1320 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1321 | vlib_node_set_state (vm, session_queue_node.index, |
| 1322 | VLIB_NODE_STATE_POLLING); |
| 1323 | |
| 1324 | return session_manager_main_enable (vm); |
| 1325 | } |
| 1326 | else |
| 1327 | { |
| 1328 | session_manager_main.is_enabled = 0; |
| 1329 | vlib_node_set_state (vm, session_queue_node.index, |
| 1330 | VLIB_NODE_STATE_DISABLED); |
| 1331 | } |
| 1332 | |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1336 | clib_error_t * |
| 1337 | session_manager_main_init (vlib_main_t * vm) |
| 1338 | { |
| 1339 | session_manager_main_t *smm = &session_manager_main; |
| 1340 | |
| 1341 | smm->vlib_main = vm; |
| 1342 | smm->vnet_main = vnet_get_main (); |
| 1343 | smm->is_enabled = 0; |
| 1344 | |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
| 1348 | VLIB_INIT_FUNCTION (session_manager_main_init) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1349 | /* |
| 1350 | * fd.io coding-style-patch-verification: ON |
| 1351 | * |
| 1352 | * Local Variables: |
| 1353 | * eval: (c-set-style "gnu") |
| 1354 | * End: |
| 1355 | */ |