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> |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 26 | #include <vnet/session/session_debug.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Per-type vector of transport protocol virtual function tables |
| 30 | */ |
| 31 | static transport_proto_vft_t *tp_vfts; |
| 32 | |
| 33 | session_manager_main_t session_manager_main; |
| 34 | |
| 35 | /* |
| 36 | * Session lookup key; (src-ip, dst-ip, src-port, dst-port, session-type) |
| 37 | * Value: (owner thread index << 32 | session_index); |
| 38 | */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 39 | void |
| 40 | stream_session_table_add_for_tc (transport_connection_t * tc, u64 value) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 41 | { |
| 42 | session_manager_main_t *smm = &session_manager_main; |
| 43 | session_kv4_t kv4; |
| 44 | session_kv6_t kv6; |
| 45 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 46 | switch (tc->proto) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 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); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 74 | stream_session_table_add_for_tc (tc, value); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 78 | stream_session_half_open_table_add (session_type_t sst, |
| 79 | transport_connection_t * tc, u64 value) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 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 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 107 | int |
| 108 | stream_session_table_del_for_tc (transport_connection_t * tc) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 109 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 110 | session_manager_main_t *smm = &session_manager_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 111 | session_kv4_t kv4; |
| 112 | session_kv6_t kv6; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 113 | switch (tc->proto) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 114 | { |
| 115 | case SESSION_TYPE_IP4_UDP: |
| 116 | case SESSION_TYPE_IP4_TCP: |
| 117 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 118 | return clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4, |
| 119 | 0 /* is_add */ ); |
| 120 | break; |
| 121 | case SESSION_TYPE_IP6_UDP: |
| 122 | case SESSION_TYPE_IP6_TCP: |
| 123 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 124 | return clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6, |
| 125 | 0 /* is_add */ ); |
| 126 | break; |
| 127 | default: |
| 128 | clib_warning ("Session type not supported"); |
| 129 | ASSERT (0); |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int |
| 136 | stream_session_table_del (session_manager_main_t * smm, stream_session_t * s) |
| 137 | { |
| 138 | transport_connection_t *ts; |
| 139 | |
| 140 | ts = tp_vfts[s->session_type].get_connection (s->connection_index, |
| 141 | s->thread_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 142 | return stream_session_table_del_for_tc (ts); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void |
| 146 | stream_session_half_open_table_del (session_manager_main_t * smm, u8 sst, |
| 147 | transport_connection_t * tc) |
| 148 | { |
| 149 | session_kv4_t kv4; |
| 150 | session_kv6_t kv6; |
| 151 | |
| 152 | switch (sst) |
| 153 | { |
| 154 | case SESSION_TYPE_IP4_UDP: |
| 155 | case SESSION_TYPE_IP4_TCP: |
| 156 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 157 | clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4, |
| 158 | 0 /* is_add */ ); |
| 159 | break; |
| 160 | case SESSION_TYPE_IP6_UDP: |
| 161 | case SESSION_TYPE_IP6_TCP: |
| 162 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 163 | clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6, |
| 164 | 0 /* is_add */ ); |
| 165 | break; |
| 166 | default: |
| 167 | clib_warning ("Session type not supported"); |
| 168 | ASSERT (0); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | stream_session_t * |
| 173 | stream_session_lookup_listener4 (ip4_address_t * lcl, u16 lcl_port, u8 proto) |
| 174 | { |
| 175 | session_manager_main_t *smm = &session_manager_main; |
| 176 | session_kv4_t kv4; |
| 177 | int rv; |
| 178 | |
| 179 | make_v4_listener_kv (&kv4, lcl, lcl_port, proto); |
| 180 | rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4); |
| 181 | if (rv == 0) |
| 182 | return pool_elt_at_index (smm->listen_sessions[proto], (u32) kv4.value); |
| 183 | |
| 184 | /* Zero out the lcl ip */ |
| 185 | kv4.key[0] = 0; |
| 186 | rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4); |
| 187 | if (rv == 0) |
| 188 | return pool_elt_at_index (smm->listen_sessions[proto], kv4.value); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | /** Looks up a session based on the 5-tuple passed as argument. |
| 194 | * |
| 195 | * First it tries to find an established session, if this fails, it tries |
| 196 | * finding a listener session if this fails, it tries a lookup with a |
| 197 | * wildcarded local source (listener bound to all interfaces) |
| 198 | */ |
| 199 | stream_session_t * |
| 200 | stream_session_lookup4 (ip4_address_t * lcl, ip4_address_t * rmt, |
| 201 | u16 lcl_port, u16 rmt_port, u8 proto, |
| 202 | u32 my_thread_index) |
| 203 | { |
| 204 | session_manager_main_t *smm = &session_manager_main; |
| 205 | session_kv4_t kv4; |
| 206 | int rv; |
| 207 | |
| 208 | /* Lookup session amongst established ones */ |
| 209 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
| 210 | rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4); |
| 211 | if (rv == 0) |
| 212 | return stream_session_get_tsi (kv4.value, my_thread_index); |
| 213 | |
| 214 | /* If nothing is found, check if any listener is available */ |
| 215 | return stream_session_lookup_listener4 (lcl, lcl_port, proto); |
| 216 | } |
| 217 | |
| 218 | stream_session_t * |
| 219 | stream_session_lookup_listener6 (ip6_address_t * lcl, u16 lcl_port, u8 proto) |
| 220 | { |
| 221 | session_manager_main_t *smm = &session_manager_main; |
| 222 | session_kv6_t kv6; |
| 223 | int rv; |
| 224 | |
| 225 | make_v6_listener_kv (&kv6, lcl, lcl_port, proto); |
| 226 | rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6); |
| 227 | if (rv == 0) |
| 228 | return pool_elt_at_index (smm->listen_sessions[proto], kv6.value); |
| 229 | |
| 230 | /* Zero out the lcl ip */ |
| 231 | kv6.key[0] = kv6.key[1] = 0; |
| 232 | rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6); |
| 233 | if (rv == 0) |
| 234 | return pool_elt_at_index (smm->listen_sessions[proto], kv6.value); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /* Looks up a session based on the 5-tuple passed as argument. |
| 240 | * First it tries to find an established session, if this fails, it tries |
| 241 | * finding a listener session if this fails, it tries a lookup with a |
| 242 | * wildcarded local source (listener bound to all interfaces) */ |
| 243 | stream_session_t * |
| 244 | stream_session_lookup6 (ip6_address_t * lcl, ip6_address_t * rmt, |
| 245 | u16 lcl_port, u16 rmt_port, u8 proto, |
| 246 | u32 my_thread_index) |
| 247 | { |
| 248 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 249 | session_kv6_t kv6; |
| 250 | int rv; |
| 251 | |
| 252 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
| 253 | rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6); |
| 254 | if (rv == 0) |
| 255 | return stream_session_get_tsi (kv6.value, my_thread_index); |
| 256 | |
| 257 | /* If nothing is found, check if any listener is available */ |
| 258 | return stream_session_lookup_listener6 (lcl, lcl_port, proto); |
| 259 | } |
| 260 | |
| 261 | stream_session_t * |
| 262 | stream_session_lookup_listener (ip46_address_t * lcl, u16 lcl_port, u8 proto) |
| 263 | { |
| 264 | switch (proto) |
| 265 | { |
| 266 | case SESSION_TYPE_IP4_UDP: |
| 267 | case SESSION_TYPE_IP4_TCP: |
| 268 | return stream_session_lookup_listener4 (&lcl->ip4, lcl_port, proto); |
| 269 | break; |
| 270 | case SESSION_TYPE_IP6_UDP: |
| 271 | case SESSION_TYPE_IP6_TCP: |
| 272 | return stream_session_lookup_listener6 (&lcl->ip6, lcl_port, proto); |
| 273 | break; |
| 274 | } |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static u64 |
| 279 | stream_session_half_open_lookup (session_manager_main_t * smm, |
| 280 | ip46_address_t * lcl, ip46_address_t * rmt, |
| 281 | u16 lcl_port, u16 rmt_port, u8 proto) |
| 282 | { |
| 283 | session_kv4_t kv4; |
| 284 | session_kv6_t kv6; |
| 285 | int rv; |
| 286 | |
| 287 | switch (proto) |
| 288 | { |
| 289 | case SESSION_TYPE_IP4_UDP: |
| 290 | case SESSION_TYPE_IP4_TCP: |
| 291 | make_v4_ss_kv (&kv4, &lcl->ip4, &rmt->ip4, lcl_port, rmt_port, proto); |
| 292 | rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4); |
| 293 | |
| 294 | if (rv == 0) |
| 295 | return kv4.value; |
| 296 | |
| 297 | return (u64) ~ 0; |
| 298 | break; |
| 299 | case SESSION_TYPE_IP6_UDP: |
| 300 | case SESSION_TYPE_IP6_TCP: |
| 301 | make_v6_ss_kv (&kv6, &lcl->ip6, &rmt->ip6, lcl_port, rmt_port, proto); |
| 302 | rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6); |
| 303 | |
| 304 | if (rv == 0) |
| 305 | return kv6.value; |
| 306 | |
| 307 | return (u64) ~ 0; |
| 308 | break; |
| 309 | } |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | transport_connection_t * |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 314 | stream_session_lookup_transport4 (ip4_address_t * lcl, ip4_address_t * rmt, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 315 | u16 lcl_port, u16 rmt_port, u8 proto, |
| 316 | u32 my_thread_index) |
| 317 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 318 | session_manager_main_t *smm = &session_manager_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 319 | session_kv4_t kv4; |
| 320 | stream_session_t *s; |
| 321 | int rv; |
| 322 | |
| 323 | /* Lookup session amongst established ones */ |
| 324 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
| 325 | rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4); |
| 326 | if (rv == 0) |
| 327 | { |
| 328 | s = stream_session_get_tsi (kv4.value, my_thread_index); |
| 329 | |
| 330 | return tp_vfts[s->session_type].get_connection (s->connection_index, |
| 331 | my_thread_index); |
| 332 | } |
| 333 | |
| 334 | /* If nothing is found, check if any listener is available */ |
| 335 | s = stream_session_lookup_listener4 (lcl, lcl_port, proto); |
| 336 | if (s) |
| 337 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 338 | |
| 339 | /* Finally, try half-open connections */ |
| 340 | rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4); |
| 341 | if (rv == 0) |
| 342 | return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | transport_connection_t * |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 348 | stream_session_lookup_transport6 (ip6_address_t * lcl, ip6_address_t * rmt, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 349 | u16 lcl_port, u16 rmt_port, u8 proto, |
| 350 | u32 my_thread_index) |
| 351 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 352 | session_manager_main_t *smm = &session_manager_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 353 | stream_session_t *s; |
| 354 | session_kv6_t kv6; |
| 355 | int rv; |
| 356 | |
| 357 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
| 358 | rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6); |
| 359 | if (rv == 0) |
| 360 | { |
| 361 | s = stream_session_get_tsi (kv6.value, my_thread_index); |
| 362 | |
| 363 | return tp_vfts[s->session_type].get_connection (s->connection_index, |
| 364 | my_thread_index); |
| 365 | } |
| 366 | |
| 367 | /* If nothing is found, check if any listener is available */ |
| 368 | s = stream_session_lookup_listener6 (lcl, lcl_port, proto); |
| 369 | if (s) |
| 370 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 371 | |
| 372 | /* Finally, try half-open connections */ |
| 373 | rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6); |
| 374 | if (rv == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 375 | return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 380 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 381 | stream_session_create_i (segment_manager_t * sm, transport_connection_t * tc, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 382 | stream_session_t ** ret_s) |
| 383 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 384 | session_manager_main_t *smm = &session_manager_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 385 | svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0; |
| 386 | u32 fifo_segment_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 387 | u32 pool_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 388 | stream_session_t *s; |
| 389 | u64 value; |
| 390 | u32 thread_index = tc->thread_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 391 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 392 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 393 | if ((rv = segment_manager_alloc_session_fifos (sm, &server_rx_fifo, |
| 394 | &server_tx_fifo, |
| 395 | &fifo_segment_index))) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 396 | return rv; |
| 397 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 398 | /* Create the session */ |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 399 | pool_get_aligned (smm->sessions[thread_index], s, CLIB_CACHE_LINE_BYTES); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 400 | memset (s, 0, sizeof (*s)); |
| 401 | |
| 402 | /* Initialize backpointers */ |
| 403 | pool_index = s - smm->sessions[thread_index]; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 404 | server_rx_fifo->master_session_index = pool_index; |
| 405 | server_rx_fifo->master_thread_index = thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 406 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 407 | server_tx_fifo->master_session_index = pool_index; |
| 408 | server_tx_fifo->master_thread_index = thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 409 | |
| 410 | s->server_rx_fifo = server_rx_fifo; |
| 411 | s->server_tx_fifo = server_tx_fifo; |
| 412 | |
| 413 | /* Initialize state machine, such as it is... */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 414 | s->session_type = tc->proto; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 415 | s->session_state = SESSION_STATE_CONNECTING; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 416 | s->svm_segment_index = fifo_segment_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 417 | s->thread_index = thread_index; |
| 418 | s->session_index = pool_index; |
| 419 | |
| 420 | /* Attach transport to session */ |
| 421 | s->connection_index = tc->c_index; |
| 422 | |
| 423 | /* Attach session to transport */ |
| 424 | tc->s_index = s->session_index; |
| 425 | |
| 426 | /* Add to the main lookup table */ |
| 427 | value = (((u64) thread_index) << 32) | (u64) s->session_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 428 | stream_session_table_add_for_tc (tc, value); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 429 | |
| 430 | *ret_s = s; |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 435 | /** Enqueue buffer chain tail */ |
| 436 | always_inline int |
| 437 | session_enqueue_chain_tail (stream_session_t * s, vlib_buffer_t * b, |
| 438 | u32 offset, u8 is_in_order) |
| 439 | { |
| 440 | vlib_buffer_t *chain_b; |
| 441 | u32 chain_bi = b->next_buffer; |
| 442 | vlib_main_t *vm = vlib_get_main (); |
| 443 | u8 *data, len; |
| 444 | u16 written = 0; |
| 445 | int rv = 0; |
| 446 | |
| 447 | do |
| 448 | { |
| 449 | chain_b = vlib_get_buffer (vm, chain_bi); |
| 450 | data = vlib_buffer_get_current (chain_b); |
| 451 | len = chain_b->current_length; |
| 452 | if (is_in_order) |
| 453 | { |
| 454 | rv = svm_fifo_enqueue_nowait (s->server_rx_fifo, len, data); |
| 455 | if (rv < len) |
| 456 | { |
| 457 | return (rv > 0) ? (written + rv) : written; |
| 458 | } |
| 459 | written += rv; |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset, len, |
| 464 | data); |
| 465 | if (rv) |
| 466 | return -1; |
| 467 | offset += len; |
| 468 | } |
| 469 | } |
| 470 | while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 471 | ? chain_b->next_buffer : 0)); |
| 472 | |
| 473 | if (is_in_order) |
| 474 | return written; |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 479 | /* |
| 480 | * Enqueue data for delivery to session peer. Does not notify peer of enqueue |
| 481 | * event but on request can queue notification events for later delivery by |
| 482 | * calling stream_server_flush_enqueue_events(). |
| 483 | * |
| 484 | * @param tc Transport connection which is to be enqueued data |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 485 | * @param b Buffer to be enqueued |
| 486 | * @param offset Offset at which to start enqueueing if out-of-order |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 487 | * @param queue_event Flag to indicate if peer is to be notified or if event |
| 488 | * is to be queued. The former is useful when more data is |
| 489 | * enqueued and only one event is to be generated. |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 490 | * @param is_in_order Flag to indicate if data is in order |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 491 | * @return Number of bytes enqueued or a negative value if enqueueing failed. |
| 492 | */ |
| 493 | int |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 494 | stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b, |
| 495 | u32 offset, u8 queue_event, u8 is_in_order) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 496 | { |
| 497 | stream_session_t *s; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 498 | int enqueued = 0, rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 499 | |
| 500 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 501 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 502 | if (is_in_order) |
| 503 | { |
| 504 | enqueued = |
| 505 | svm_fifo_enqueue_nowait (s->server_rx_fifo, b->current_length, |
| 506 | vlib_buffer_get_current (b)); |
| 507 | if (PREDICT_FALSE |
| 508 | ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued > 0)) |
| 509 | { |
| 510 | rv = session_enqueue_chain_tail (s, b, 0, 1); |
| 511 | if (rv <= 0) |
| 512 | return enqueued; |
| 513 | enqueued += rv; |
| 514 | } |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset, |
| 519 | b->current_length, |
| 520 | vlib_buffer_get_current (b)); |
| 521 | if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv)) |
| 522 | rv = session_enqueue_chain_tail (s, b, offset + b->current_length, 0); |
| 523 | if (rv) |
| 524 | return -1; |
| 525 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 526 | |
| 527 | if (queue_event) |
| 528 | { |
| 529 | /* Queue RX event on this fifo. Eventually these will need to be flushed |
| 530 | * by calling stream_server_flush_enqueue_events () */ |
| 531 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 532 | u32 thread_index = s->thread_index; |
| 533 | u32 my_enqueue_epoch = smm->current_enqueue_epoch[thread_index]; |
| 534 | |
| 535 | if (s->enqueue_epoch != my_enqueue_epoch) |
| 536 | { |
| 537 | s->enqueue_epoch = my_enqueue_epoch; |
| 538 | vec_add1 (smm->session_indices_to_enqueue_by_thread[thread_index], |
| 539 | s - smm->sessions[thread_index]); |
| 540 | } |
| 541 | } |
| 542 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 543 | if (is_in_order) |
| 544 | return enqueued; |
| 545 | |
| 546 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /** Check if we have space in rx fifo to push more bytes */ |
| 550 | u8 |
| 551 | stream_session_no_space (transport_connection_t * tc, u32 thread_index, |
| 552 | u16 data_len) |
| 553 | { |
| 554 | stream_session_t *s = stream_session_get (tc->c_index, thread_index); |
| 555 | |
| 556 | if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY)) |
| 557 | return 1; |
| 558 | |
| 559 | if (data_len > svm_fifo_max_enqueue (s->server_rx_fifo)) |
| 560 | return 1; |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | u32 |
| 566 | stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer, |
| 567 | u32 offset, u32 max_bytes) |
| 568 | { |
| 569 | stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 570 | return svm_fifo_peek (s->server_tx_fifo, offset, max_bytes, buffer); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | u32 |
| 574 | stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes) |
| 575 | { |
| 576 | stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 577 | return svm_fifo_dequeue_drop (s->server_tx_fifo, max_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Notify session peer that new data has been enqueued. |
| 582 | * |
| 583 | * @param s Stream session for which the event is to be generated. |
| 584 | * @param block Flag to indicate if call should block if event queue is full. |
| 585 | * |
| 586 | * @return 0 on succes or negative number if failed to send notification. |
| 587 | */ |
| 588 | static int |
| 589 | stream_session_enqueue_notify (stream_session_t * s, u8 block) |
| 590 | { |
| 591 | application_t *app; |
| 592 | session_fifo_event_t evt; |
| 593 | unix_shared_memory_queue_t *q; |
| 594 | static u32 serial_number; |
| 595 | |
| 596 | if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED)) |
| 597 | return 0; |
| 598 | |
| 599 | /* Get session's server */ |
| 600 | app = application_get (s->app_index); |
| 601 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 602 | /* Built-in server? Hand event to the callback... */ |
| 603 | if (app->cb_fns.builtin_server_rx_callback) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 604 | return app->cb_fns.builtin_server_rx_callback (s); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 605 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 606 | /* If no event, send one */ |
| 607 | if (svm_fifo_set_event (s->server_rx_fifo)) |
| 608 | { |
| 609 | /* Fabricate event */ |
| 610 | evt.fifo = s->server_rx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 611 | evt.event_type = FIFO_EVENT_APP_RX; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 612 | evt.event_id = serial_number++; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 613 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 614 | /* Add event to server's event queue */ |
| 615 | q = app->event_queue; |
| 616 | |
| 617 | /* Based on request block (or not) for lack of space */ |
| 618 | if (block || PREDICT_TRUE (q->cursize < q->maxsize)) |
| 619 | unix_shared_memory_queue_add (app->event_queue, (u8 *) & evt, |
| 620 | 0 /* do wait for mutex */ ); |
| 621 | else |
| 622 | { |
| 623 | clib_warning ("fifo full"); |
| 624 | return -1; |
| 625 | } |
| 626 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 627 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 628 | /* *INDENT-OFF* */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 629 | SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 630 | ed->data[0] = evt.event_id; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 631 | ed->data[1] = svm_fifo_max_dequeue (s->server_rx_fifo); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 632 | })); |
| 633 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Flushes queue of sessions that are to be notified of new data |
| 640 | * enqueued events. |
| 641 | * |
| 642 | * @param thread_index Thread index for which the flush is to be performed. |
| 643 | * @return 0 on success or a positive number indicating the number of |
| 644 | * failures due to API queue being full. |
| 645 | */ |
| 646 | int |
| 647 | session_manager_flush_enqueue_events (u32 thread_index) |
| 648 | { |
| 649 | session_manager_main_t *smm = &session_manager_main; |
| 650 | u32 *session_indices_to_enqueue; |
| 651 | int i, errors = 0; |
| 652 | |
| 653 | session_indices_to_enqueue = |
| 654 | smm->session_indices_to_enqueue_by_thread[thread_index]; |
| 655 | |
| 656 | for (i = 0; i < vec_len (session_indices_to_enqueue); i++) |
| 657 | { |
| 658 | stream_session_t *s0; |
| 659 | |
| 660 | /* Get session */ |
| 661 | s0 = stream_session_get (session_indices_to_enqueue[i], thread_index); |
| 662 | if (stream_session_enqueue_notify (s0, 0 /* don't block */ )) |
| 663 | { |
| 664 | errors++; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | vec_reset_length (session_indices_to_enqueue); |
| 669 | |
| 670 | smm->session_indices_to_enqueue_by_thread[thread_index] = |
| 671 | session_indices_to_enqueue; |
| 672 | |
| 673 | /* Increment enqueue epoch for next round */ |
| 674 | smm->current_enqueue_epoch[thread_index]++; |
| 675 | |
| 676 | return errors; |
| 677 | } |
| 678 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 679 | /** |
| 680 | * Init fifo tail and head pointers |
| 681 | * |
| 682 | * Useful if transport uses absolute offsets for tracking ooo segments. |
| 683 | */ |
| 684 | void |
| 685 | stream_session_init_fifos_pointers (transport_connection_t * tc, |
| 686 | u32 rx_pointer, u32 tx_pointer) |
| 687 | { |
| 688 | stream_session_t *s; |
| 689 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 690 | svm_fifo_init_pointers (s->server_rx_fifo, rx_pointer); |
| 691 | svm_fifo_init_pointers (s->server_tx_fifo, tx_pointer); |
| 692 | } |
| 693 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 694 | void |
| 695 | stream_session_connect_notify (transport_connection_t * tc, u8 sst, |
| 696 | u8 is_fail) |
| 697 | { |
| 698 | session_manager_main_t *smm = &session_manager_main; |
| 699 | application_t *app; |
| 700 | stream_session_t *new_s = 0; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 701 | u64 handle; |
| 702 | u32 api_context = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 703 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 704 | handle = stream_session_half_open_lookup (smm, &tc->lcl_ip, &tc->rmt_ip, |
| 705 | tc->lcl_port, tc->rmt_port, |
| 706 | tc->proto); |
| 707 | if (handle == HALF_OPEN_LOOKUP_INVALID_VALUE) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 708 | { |
| 709 | clib_warning ("This can't be good!"); |
| 710 | return; |
| 711 | } |
| 712 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 713 | /* Get the app's index from the handle we stored when opening connection */ |
| 714 | app = application_get (handle >> 32); |
| 715 | api_context = tc->s_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 716 | |
| 717 | if (!is_fail) |
| 718 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 719 | segment_manager_t *sm; |
| 720 | sm = application_get_connect_segment_manager (app); |
| 721 | |
| 722 | /* Create new session (svm segments are allocated if needed) */ |
| 723 | if (stream_session_create_i (sm, tc, &new_s)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 724 | return; |
| 725 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 726 | new_s->app_index = app->index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* Notify client */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 730 | app->cb_fns.session_connected_callback (app->index, api_context, new_s, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 731 | is_fail); |
| 732 | |
| 733 | /* Cleanup session lookup */ |
| 734 | stream_session_half_open_table_del (smm, sst, tc); |
Dave Barach | 259cdae | 2017-05-15 16:27:05 -0400 | [diff] [blame^] | 735 | |
| 736 | /* Add to established lookup table */ |
| 737 | handle = (((u64) tc->thread_index) << 32) | (u64) new_s->session_index; |
| 738 | stream_session_table_add_for_tc (tc, handle); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | void |
| 742 | stream_session_accept_notify (transport_connection_t * tc) |
| 743 | { |
| 744 | application_t *server; |
| 745 | stream_session_t *s; |
| 746 | |
| 747 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 748 | server = application_get (s->app_index); |
| 749 | server->cb_fns.session_accept_callback (s); |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * Notification from transport that connection is being closed. |
| 754 | * |
| 755 | * A disconnect is sent to application but state is not removed. Once |
| 756 | * disconnect is acknowledged by application, session disconnect is called. |
| 757 | * Ultimately this leads to close being called on transport (passive close). |
| 758 | */ |
| 759 | void |
| 760 | stream_session_disconnect_notify (transport_connection_t * tc) |
| 761 | { |
| 762 | application_t *server; |
| 763 | stream_session_t *s; |
| 764 | |
| 765 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 766 | server = application_get (s->app_index); |
| 767 | server->cb_fns.session_disconnect_callback (s); |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Cleans up session and associated app if needed. |
| 772 | */ |
| 773 | void |
| 774 | stream_session_delete (stream_session_t * s) |
| 775 | { |
| 776 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 777 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 778 | /* Delete from the main lookup table. */ |
| 779 | stream_session_table_del (smm, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 780 | |
| 781 | /* Cleanup fifo segments */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 782 | segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo, |
| 783 | s->server_tx_fifo); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 784 | |
| 785 | pool_put (smm->sessions[s->thread_index], s); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Notification from transport that connection is being deleted |
| 790 | * |
| 791 | * This should be called only on previously fully established sessions. For |
| 792 | * instance failed connects should call stream_session_connect_notify and |
| 793 | * indicate that the connect has failed. |
| 794 | */ |
| 795 | void |
| 796 | stream_session_delete_notify (transport_connection_t * tc) |
| 797 | { |
| 798 | stream_session_t *s; |
| 799 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 800 | /* App might've been removed already */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 801 | s = stream_session_get_if_valid (tc->s_index, tc->thread_index); |
| 802 | if (!s) |
| 803 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 804 | return; |
| 805 | } |
| 806 | stream_session_delete (s); |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Notify application that connection has been reset. |
| 811 | */ |
| 812 | void |
| 813 | stream_session_reset_notify (transport_connection_t * tc) |
| 814 | { |
| 815 | stream_session_t *s; |
| 816 | application_t *app; |
| 817 | s = stream_session_get (tc->s_index, tc->thread_index); |
| 818 | |
| 819 | app = application_get (s->app_index); |
| 820 | app->cb_fns.session_reset_callback (s); |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Accept a stream session. Optionally ping the server by callback. |
| 825 | */ |
| 826 | int |
| 827 | stream_session_accept (transport_connection_t * tc, u32 listener_index, |
| 828 | u8 sst, u8 notify) |
| 829 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 830 | application_t *server; |
| 831 | stream_session_t *s, *listener; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 832 | segment_manager_t *sm; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 833 | |
| 834 | int rv; |
| 835 | |
| 836 | /* Find the server */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 837 | listener = listen_session_get (sst, listener_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 838 | server = application_get (listener->app_index); |
| 839 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 840 | sm = application_get_listen_segment_manager (server, listener); |
| 841 | if ((rv = stream_session_create_i (sm, tc, &s))) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 842 | return rv; |
| 843 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 844 | s->app_index = server->index; |
| 845 | s->listener_index = listener_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 846 | |
| 847 | /* Shoulder-tap the server */ |
| 848 | if (notify) |
| 849 | { |
| 850 | server->cb_fns.session_accept_callback (s); |
| 851 | } |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 856 | /** |
| 857 | * Ask transport to open connection to remote transport endpoint. |
| 858 | * |
| 859 | * Stores handle for matching request with reply since the call can be |
| 860 | * asynchronous. For instance, for TCP the 3-way handshake must complete |
| 861 | * before reply comes. Session is only created once connection is established. |
| 862 | * |
| 863 | * @param app_index Index of the application requesting the connect |
| 864 | * @param st Session type requested. |
| 865 | * @param tep Remote transport endpoint |
| 866 | * @param res Resulting transport connection . |
| 867 | */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 868 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 869 | stream_session_open (u32 app_index, session_type_t st, |
| 870 | transport_endpoint_t * tep, |
| 871 | transport_connection_t ** res) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 872 | { |
| 873 | transport_connection_t *tc; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 874 | int rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 875 | u64 handle; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 876 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 877 | rv = tp_vfts[st].open (&tep->ip, tep->port); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 878 | if (rv < 0) |
| 879 | { |
| 880 | clib_warning ("Transport failed to open connection."); |
| 881 | return VNET_API_ERROR_SESSION_CONNECT_FAIL; |
| 882 | } |
| 883 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 884 | tc = tp_vfts[st].get_half_open ((u32) rv); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 885 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 886 | /* Save app and tc index. The latter is needed to help establish the |
| 887 | * connection while the former is needed when the connect notify comes |
| 888 | * and we have to notify the external app */ |
| 889 | handle = (((u64) app_index) << 32) | (u64) tc->c_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 890 | |
| 891 | /* Add to the half-open lookup table */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 892 | stream_session_half_open_table_add (st, tc, handle); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 893 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 894 | *res = tc; |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * Ask transport to listen on local transport endpoint. |
| 901 | * |
| 902 | * @param s Session for which listen will be called. Note that unlike |
| 903 | * established sessions, listen sessions are not associated to a |
| 904 | * thread. |
| 905 | * @param tep Local endpoint to be listened on. |
| 906 | */ |
| 907 | int |
| 908 | stream_session_listen (stream_session_t * s, transport_endpoint_t * tep) |
| 909 | { |
| 910 | transport_connection_t *tc; |
| 911 | u32 tci; |
| 912 | |
| 913 | /* Transport bind/listen */ |
| 914 | tci = tp_vfts[s->session_type].bind (s->session_index, &tep->ip, tep->port); |
| 915 | |
| 916 | if (tci == (u32) ~ 0) |
| 917 | return -1; |
| 918 | |
| 919 | /* Attach transport to session */ |
| 920 | s->connection_index = tci; |
| 921 | tc = tp_vfts[s->session_type].get_listener (tci); |
| 922 | |
| 923 | /* Weird but handle it ... */ |
| 924 | if (tc == 0) |
| 925 | return -1; |
| 926 | |
| 927 | /* Add to the main lookup table */ |
| 928 | stream_session_table_add_for_tc (tc, s->session_index); |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * Ask transport to stop listening on local transport endpoint. |
| 935 | * |
| 936 | * @param s Session to stop listening on. It must be in state LISTENING. |
| 937 | */ |
| 938 | int |
| 939 | stream_session_stop_listen (stream_session_t * s) |
| 940 | { |
| 941 | transport_connection_t *tc; |
| 942 | |
| 943 | if (s->session_state != SESSION_STATE_LISTENING) |
| 944 | { |
| 945 | clib_warning ("not a listening session"); |
| 946 | return -1; |
| 947 | } |
| 948 | |
| 949 | tc = tp_vfts[s->session_type].get_listener (s->connection_index); |
| 950 | if (!tc) |
| 951 | { |
| 952 | clib_warning ("no transport"); |
| 953 | return VNET_API_ERROR_ADDRESS_NOT_IN_USE; |
| 954 | } |
| 955 | |
| 956 | stream_session_table_del_for_tc (tc); |
| 957 | tp_vfts[s->session_type].unbind (s->connection_index); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 958 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 959 | } |
| 960 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 961 | void |
| 962 | session_send_session_evt_to_thread (u64 session_handle, |
| 963 | fifo_event_type_t evt_type, |
| 964 | u32 thread_index) |
| 965 | { |
| 966 | static u16 serial_number = 0; |
| 967 | session_fifo_event_t evt; |
| 968 | unix_shared_memory_queue_t *q; |
| 969 | |
| 970 | /* Fabricate event */ |
| 971 | evt.session_handle = session_handle; |
| 972 | evt.event_type = evt_type; |
| 973 | evt.event_id = serial_number++; |
| 974 | |
| 975 | q = session_manager_get_vpp_event_queue (thread_index); |
| 976 | |
| 977 | /* Based on request block (or not) for lack of space */ |
| 978 | if (PREDICT_TRUE (q->cursize < q->maxsize)) |
| 979 | unix_shared_memory_queue_add (q, (u8 *) & evt, |
| 980 | 0 /* do wait for mutex */ ); |
| 981 | else |
| 982 | { |
| 983 | clib_warning ("queue full"); |
| 984 | return; |
| 985 | } |
| 986 | } |
| 987 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 988 | /** |
| 989 | * Disconnect session and propagate to transport. This should eventually |
| 990 | * result in a delete notification that allows us to cleanup session state. |
| 991 | * Called for both active/passive disconnects. |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 992 | * |
| 993 | * Should be called from the session's thread. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 994 | */ |
| 995 | void |
| 996 | stream_session_disconnect (stream_session_t * s) |
| 997 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 998 | s->session_state = SESSION_STATE_CLOSED; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 999 | tp_vfts[s->session_type].close (s->connection_index, s->thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Cleanup transport and session state. |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1004 | * |
| 1005 | * Notify transport of the cleanup, wait for a delete notify to actually |
| 1006 | * remove the session state. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1007 | */ |
| 1008 | void |
| 1009 | stream_session_cleanup (stream_session_t * s) |
| 1010 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1011 | session_manager_main_t *smm = &session_manager_main; |
| 1012 | int rv; |
| 1013 | |
| 1014 | s->session_state = SESSION_STATE_CLOSED; |
| 1015 | |
| 1016 | /* Delete from the main lookup table to avoid more enqueues */ |
| 1017 | rv = stream_session_table_del (smm, s); |
| 1018 | if (rv) |
| 1019 | clib_warning ("hash delete error, rv %d", rv); |
| 1020 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1021 | tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | void |
| 1025 | session_register_transport (u8 type, const transport_proto_vft_t * vft) |
| 1026 | { |
| 1027 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 1028 | |
| 1029 | vec_validate (tp_vfts, type); |
| 1030 | tp_vfts[type] = *vft; |
| 1031 | |
| 1032 | /* If an offset function is provided, then peek instead of dequeue */ |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1033 | smm->session_tx_fns[type] = |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1034 | (vft->tx_fifo_offset) ? session_tx_fifo_peek_and_snd : |
| 1035 | session_tx_fifo_dequeue_and_snd; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | transport_proto_vft_t * |
| 1039 | session_get_transport_vft (u8 type) |
| 1040 | { |
| 1041 | if (type >= vec_len (tp_vfts)) |
| 1042 | return 0; |
| 1043 | return &tp_vfts[type]; |
| 1044 | } |
| 1045 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1046 | /** |
| 1047 | * Allocate vpp event queue (once) per worker thread |
| 1048 | */ |
| 1049 | void |
| 1050 | session_vpp_event_queue_allocate (session_manager_main_t * smm, |
| 1051 | u32 thread_index) |
| 1052 | { |
| 1053 | api_main_t *am = &api_main; |
| 1054 | void *oldheap; |
| 1055 | |
| 1056 | if (smm->vpp_event_queues[thread_index] == 0) |
| 1057 | { |
| 1058 | /* Allocate event fifo in the /vpe-api shared-memory segment */ |
| 1059 | oldheap = svm_push_data_heap (am->vlib_rp); |
| 1060 | |
| 1061 | smm->vpp_event_queues[thread_index] = |
| 1062 | unix_shared_memory_queue_init (2048 /* nels $$$$ config */ , |
| 1063 | sizeof (session_fifo_event_t), |
| 1064 | 0 /* consumer pid */ , |
| 1065 | 0 |
| 1066 | /* (do not) send signal when queue non-empty */ |
| 1067 | ); |
| 1068 | |
| 1069 | svm_pop_heap (oldheap); |
| 1070 | } |
| 1071 | } |
| 1072 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1073 | static clib_error_t * |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1074 | session_manager_main_enable (vlib_main_t * vm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1075 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1076 | session_manager_main_t *smm = &session_manager_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1077 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 1078 | u32 num_threads; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1079 | int i; |
| 1080 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1081 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 1082 | |
| 1083 | if (num_threads < 1) |
| 1084 | return clib_error_return (0, "n_thread_stacks not set"); |
| 1085 | |
| 1086 | /* $$$ config parameters */ |
| 1087 | svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ , |
| 1088 | 20 /* timeout in seconds */ ); |
| 1089 | |
| 1090 | /* configure per-thread ** vectors */ |
| 1091 | vec_validate (smm->sessions, num_threads - 1); |
| 1092 | vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1); |
| 1093 | vec_validate (smm->tx_buffers, num_threads - 1); |
| 1094 | vec_validate (smm->fifo_events, num_threads - 1); |
| 1095 | vec_validate (smm->evts_partially_read, num_threads - 1); |
| 1096 | vec_validate (smm->current_enqueue_epoch, num_threads - 1); |
| 1097 | vec_validate (smm->vpp_event_queues, num_threads - 1); |
| 1098 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1099 | #if SESSION_DBG |
| 1100 | vec_validate (smm->last_event_poll_by_thread, num_threads - 1); |
| 1101 | #endif |
| 1102 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1103 | /* Allocate vpp event queues */ |
| 1104 | for (i = 0; i < vec_len (smm->vpp_event_queues); i++) |
| 1105 | session_vpp_event_queue_allocate (smm, i); |
| 1106 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1107 | /* $$$$ preallocate hack config parameter */ |
| 1108 | for (i = 0; i < 200000; i++) |
| 1109 | { |
| 1110 | stream_session_t *ss; |
Dave Barach | 259cdae | 2017-05-15 16:27:05 -0400 | [diff] [blame^] | 1111 | pool_get_aligned (smm->sessions[0], ss, CLIB_CACHE_LINE_BYTES); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1112 | memset (ss, 0, sizeof (*ss)); |
| 1113 | } |
| 1114 | |
| 1115 | for (i = 0; i < 200000; i++) |
| 1116 | pool_put_index (smm->sessions[0], i); |
| 1117 | |
| 1118 | clib_bihash_init_16_8 (&smm->v4_session_hash, "v4 session table", |
| 1119 | 200000 /* $$$$ config parameter nbuckets */ , |
| 1120 | (64 << 20) /*$$$ config parameter table size */ ); |
| 1121 | clib_bihash_init_48_8 (&smm->v6_session_hash, "v6 session table", |
| 1122 | 200000 /* $$$$ config parameter nbuckets */ , |
| 1123 | (64 << 20) /*$$$ config parameter table size */ ); |
| 1124 | |
| 1125 | clib_bihash_init_16_8 (&smm->v4_half_open_hash, "v4 half-open table", |
| 1126 | 200000 /* $$$$ config parameter nbuckets */ , |
| 1127 | (64 << 20) /*$$$ config parameter table size */ ); |
| 1128 | clib_bihash_init_48_8 (&smm->v6_half_open_hash, "v6 half-open table", |
| 1129 | 200000 /* $$$$ config parameter nbuckets */ , |
| 1130 | (64 << 20) /*$$$ config parameter table size */ ); |
| 1131 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1132 | smm->is_enabled = 1; |
| 1133 | |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 1134 | /* Enable TCP transport */ |
| 1135 | vnet_tcp_enable_disable (vm, 1); |
| 1136 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1137 | return 0; |
| 1138 | } |
| 1139 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1140 | void |
| 1141 | session_node_enable_disable (u8 is_en) |
| 1142 | { |
| 1143 | u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED; |
| 1144 | /* *INDENT-OFF* */ |
| 1145 | foreach_vlib_main (({ |
| 1146 | vlib_node_set_state (this_vlib_main, session_queue_node.index, |
| 1147 | state); |
| 1148 | })); |
| 1149 | /* *INDENT-ON* */ |
| 1150 | } |
| 1151 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1152 | clib_error_t * |
| 1153 | vnet_session_enable_disable (vlib_main_t * vm, u8 is_en) |
| 1154 | { |
| 1155 | if (is_en) |
| 1156 | { |
| 1157 | if (session_manager_main.is_enabled) |
| 1158 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1159 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1160 | session_node_enable_disable (is_en); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1161 | |
| 1162 | return session_manager_main_enable (vm); |
| 1163 | } |
| 1164 | else |
| 1165 | { |
| 1166 | session_manager_main.is_enabled = 0; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1167 | session_node_enable_disable (is_en); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1173 | clib_error_t * |
| 1174 | session_manager_main_init (vlib_main_t * vm) |
| 1175 | { |
| 1176 | session_manager_main_t *smm = &session_manager_main; |
| 1177 | |
| 1178 | smm->vlib_main = vm; |
| 1179 | smm->vnet_main = vnet_get_main (); |
| 1180 | smm->is_enabled = 0; |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | VLIB_INIT_FUNCTION (session_manager_main_init) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1186 | /* |
| 1187 | * fd.io coding-style-patch-verification: ON |
| 1188 | * |
| 1189 | * Local Variables: |
| 1190 | * eval: (c-set-style "gnu") |
| 1191 | * End: |
| 1192 | */ |