Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [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 | /** Generate typed init functions for multiple hash table styles... */ |
| 17 | #include <vppinfra/bihash_16_8.h> |
| 18 | #include <vppinfra/bihash_template.h> |
| 19 | |
| 20 | #include <vppinfra/bihash_template.c> |
| 21 | |
| 22 | #undef __included_bihash_template_h__ |
| 23 | |
| 24 | #include <vppinfra/bihash_48_8.h> |
| 25 | #include <vppinfra/bihash_template.h> |
| 26 | |
| 27 | #include <vppinfra/bihash_template.c> |
| 28 | #include <vnet/session/session_lookup.h> |
| 29 | #include <vnet/session/session.h> |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 30 | #include <vnet/session/application.h> |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 31 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 32 | /** |
| 33 | * External vector of per transport virtual functions table |
| 34 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 35 | extern transport_proto_vft_t *tp_vfts; |
| 36 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Network namespace index (i.e., fib index) to session lookup table. We |
| 39 | * should have one per network protocol type but for now we only support IP4/6 |
| 40 | */ |
| 41 | static u32 *fib_index_to_table_index[2]; |
| 42 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 43 | /* *INDENT-OFF* */ |
| 44 | /* 16 octets */ |
| 45 | typedef CLIB_PACKED (struct { |
| 46 | union |
| 47 | { |
| 48 | struct |
| 49 | { |
| 50 | ip4_address_t src; |
| 51 | ip4_address_t dst; |
| 52 | u16 src_port; |
| 53 | u16 dst_port; |
| 54 | /* align by making this 4 octets even though its a 1-bit field |
| 55 | * NOTE: avoid key overlap with other transports that use 5 tuples for |
| 56 | * session identification. |
| 57 | */ |
| 58 | u32 proto; |
| 59 | }; |
| 60 | u64 as_u64[2]; |
| 61 | }; |
| 62 | }) v4_connection_key_t; |
| 63 | |
| 64 | typedef CLIB_PACKED (struct { |
| 65 | union |
| 66 | { |
| 67 | struct |
| 68 | { |
| 69 | /* 48 octets */ |
| 70 | ip6_address_t src; |
| 71 | ip6_address_t dst; |
| 72 | u16 src_port; |
| 73 | u16 dst_port; |
| 74 | u32 proto; |
| 75 | u64 unused; |
| 76 | }; |
| 77 | u64 as_u64[6]; |
| 78 | }; |
| 79 | }) v6_connection_key_t; |
| 80 | /* *INDENT-ON* */ |
| 81 | |
| 82 | typedef clib_bihash_kv_16_8_t session_kv4_t; |
| 83 | typedef clib_bihash_kv_48_8_t session_kv6_t; |
| 84 | |
| 85 | always_inline void |
| 86 | make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt, |
| 87 | u16 lcl_port, u16 rmt_port, u8 proto) |
| 88 | { |
| 89 | v4_connection_key_t *key = (v4_connection_key_t *) kv->key; |
| 90 | |
| 91 | key->src.as_u32 = lcl->as_u32; |
| 92 | key->dst.as_u32 = rmt->as_u32; |
| 93 | key->src_port = lcl_port; |
| 94 | key->dst_port = rmt_port; |
| 95 | key->proto = proto; |
| 96 | |
| 97 | kv->value = ~0ULL; |
| 98 | } |
| 99 | |
| 100 | always_inline void |
| 101 | make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port, |
| 102 | u8 proto) |
| 103 | { |
| 104 | v4_connection_key_t *key = (v4_connection_key_t *) kv->key; |
| 105 | |
| 106 | key->src.as_u32 = lcl->as_u32; |
| 107 | key->dst.as_u32 = 0; |
| 108 | key->src_port = lcl_port; |
| 109 | key->dst_port = 0; |
| 110 | key->proto = proto; |
| 111 | |
| 112 | kv->value = ~0ULL; |
| 113 | } |
| 114 | |
| 115 | always_inline void |
| 116 | make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t) |
| 117 | { |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 118 | make_v4_ss_kv (kv, &t->lcl_ip.ip4, &t->rmt_ip.ip4, t->lcl_port, t->rmt_port, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 119 | session_type_from_proto_and_ip (t->proto, 1)); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | always_inline void |
| 123 | make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt, |
| 124 | u16 lcl_port, u16 rmt_port, u8 proto) |
| 125 | { |
| 126 | v6_connection_key_t *key = (v6_connection_key_t *) kv->key; |
| 127 | |
| 128 | key->src.as_u64[0] = lcl->as_u64[0]; |
| 129 | key->src.as_u64[1] = lcl->as_u64[1]; |
| 130 | key->dst.as_u64[0] = rmt->as_u64[0]; |
| 131 | key->dst.as_u64[1] = rmt->as_u64[1]; |
| 132 | key->src_port = lcl_port; |
| 133 | key->dst_port = rmt_port; |
| 134 | key->proto = proto; |
| 135 | key->unused = 0; |
| 136 | |
| 137 | kv->value = ~0ULL; |
| 138 | } |
| 139 | |
| 140 | always_inline void |
| 141 | make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port, |
| 142 | u8 proto) |
| 143 | { |
| 144 | v6_connection_key_t *key = (v6_connection_key_t *) kv->key; |
| 145 | |
| 146 | key->src.as_u64[0] = lcl->as_u64[0]; |
| 147 | key->src.as_u64[1] = lcl->as_u64[1]; |
| 148 | key->dst.as_u64[0] = 0; |
| 149 | key->dst.as_u64[1] = 0; |
| 150 | key->src_port = lcl_port; |
| 151 | key->dst_port = 0; |
| 152 | key->proto = proto; |
| 153 | key->unused = 0; |
| 154 | |
| 155 | kv->value = ~0ULL; |
| 156 | } |
| 157 | |
| 158 | always_inline void |
| 159 | make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * t) |
| 160 | { |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 161 | make_v6_ss_kv (kv, &t->lcl_ip.ip6, &t->rmt_ip.ip6, t->lcl_port, t->rmt_port, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 162 | session_type_from_proto_and_ip (t->proto, 0)); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 165 | |
| 166 | static session_table_t * |
| 167 | session_table_get_or_alloc_for_connection (transport_connection_t * tc) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 168 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 169 | session_table_t *st; |
| 170 | u32 table_index, fib_proto = transport_connection_fib_proto (tc); |
| 171 | if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index) |
| 172 | { |
| 173 | st = session_table_alloc (); |
| 174 | table_index = session_table_index (st); |
| 175 | vec_validate (fib_index_to_table_index[fib_proto], tc->fib_index); |
| 176 | fib_index_to_table_index[fib_proto][tc->fib_index] = table_index; |
| 177 | return st; |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | table_index = fib_index_to_table_index[fib_proto][tc->fib_index]; |
| 182 | return session_table_get (table_index); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static session_table_t * |
| 187 | session_table_get_for_connection (transport_connection_t * tc) |
| 188 | { |
| 189 | u32 fib_proto = transport_connection_fib_proto (tc); |
| 190 | if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index) |
| 191 | return 0; |
| 192 | return |
| 193 | session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]); |
| 194 | } |
| 195 | |
| 196 | static session_table_t * |
| 197 | session_table_get_for_fib_index (u32 fib_proto, u32 fib_index) |
| 198 | { |
| 199 | if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index) |
| 200 | return 0; |
| 201 | return session_table_get (fib_index_to_table_index[fib_proto][fib_index]); |
| 202 | } |
| 203 | |
| 204 | u32 |
| 205 | session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index) |
| 206 | { |
| 207 | if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index) |
| 208 | return SESSION_TABLE_INVALID_INDEX; |
| 209 | return fib_index_to_table_index[fib_proto][fib_index]; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Add transport connection to a session table |
| 214 | * |
| 215 | * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type) |
| 216 | * is added to requested session table. |
| 217 | * |
| 218 | * @param tc transport connection to be added |
| 219 | * @param value value to be stored |
| 220 | * |
| 221 | * @return non-zero if failure |
| 222 | */ |
| 223 | int |
| 224 | session_lookup_add_connection (transport_connection_t * tc, u64 value) |
| 225 | { |
| 226 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 227 | session_kv4_t kv4; |
| 228 | session_kv6_t kv6; |
| 229 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 230 | st = session_table_get_or_alloc_for_connection (tc); |
| 231 | if (!st) |
| 232 | return -1; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 233 | if (tc->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 234 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 235 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 236 | kv4.value = value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 237 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, |
| 238 | 1 /* is_add */ ); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 239 | } |
| 240 | else |
| 241 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 242 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 243 | kv6.value = value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 244 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, |
| 245 | 1 /* is_add */ ); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 249 | int |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 250 | session_lookup_add_session_endpoint (u32 table_index, |
| 251 | session_endpoint_t * sep, u64 value) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 252 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 253 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 254 | session_kv4_t kv4; |
| 255 | session_kv6_t kv6; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 256 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 257 | st = session_table_get (table_index); |
| 258 | if (!st) |
| 259 | return -1; |
| 260 | if (sep->is_ip4) |
| 261 | { |
| 262 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 263 | sep->transport_proto); |
| 264 | kv4.value = value; |
| 265 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 270 | sep->transport_proto); |
| 271 | kv6.value = value; |
| 272 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | int |
| 277 | session_lookup_del_session_endpoint (u32 table_index, |
| 278 | session_endpoint_t * sep) |
| 279 | { |
| 280 | session_table_t *st; |
| 281 | session_kv4_t kv4; |
| 282 | session_kv6_t kv6; |
| 283 | |
| 284 | st = session_table_get (table_index); |
| 285 | if (!st) |
| 286 | return -1; |
| 287 | if (sep->is_ip4) |
| 288 | { |
| 289 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 290 | sep->transport_proto); |
| 291 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 296 | sep->transport_proto); |
| 297 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Delete transport connection from session table |
| 303 | * |
| 304 | * @param table_index session table index |
| 305 | * @param tc transport connection to be removed |
| 306 | * |
| 307 | * @return non-zero if failure |
| 308 | */ |
| 309 | int |
| 310 | session_lookup_del_connection (transport_connection_t * tc) |
| 311 | { |
| 312 | session_table_t *st; |
| 313 | session_kv4_t kv4; |
| 314 | session_kv6_t kv6; |
| 315 | |
| 316 | st = session_table_get_for_connection (tc); |
| 317 | if (!st) |
| 318 | return -1; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 319 | if (tc->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 320 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 321 | make_v4_ss_kv_from_tc (&kv4, tc); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 322 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 323 | 0 /* is_add */ ); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 324 | } |
| 325 | else |
| 326 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 327 | make_v6_ss_kv_from_tc (&kv6, tc); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 328 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 329 | 0 /* is_add */ ); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 330 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | int |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 334 | session_lookup_del_session (stream_session_t * s) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 335 | { |
| 336 | transport_connection_t *ts; |
| 337 | ts = tp_vfts[s->session_type].get_connection (s->connection_index, |
| 338 | s->thread_index); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 339 | return session_lookup_del_connection (ts); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 342 | static stream_session_t * |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 343 | session_lookup_app_listen_session (u32 app_index, u8 fib_proto, |
| 344 | u8 transport_proto) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 345 | { |
| 346 | application_t *app; |
| 347 | app = application_get (app_index); |
| 348 | if (!app) |
| 349 | return 0; |
| 350 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 351 | return application_first_listener (app, fib_proto, transport_proto); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | stream_session_t * |
| 355 | session_lookup_rules_table4 (session_rules_table_t * srt, u8 proto, |
| 356 | ip4_address_t * lcl, u16 lcl_port, |
| 357 | ip4_address_t * rmt, u16 rmt_port) |
| 358 | { |
| 359 | u32 action_index; |
| 360 | action_index = session_rules_table_lookup4 (srt, proto, lcl, rmt, lcl_port, |
| 361 | rmt_port); |
| 362 | /* Nothing sophisticated for now, action index is app index */ |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 363 | return session_lookup_app_listen_session (action_index, FIB_PROTOCOL_IP4, |
| 364 | proto); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | stream_session_t * |
| 368 | session_lookup_rules_table6 (session_rules_table_t * srt, u8 proto, |
| 369 | ip6_address_t * lcl, u16 lcl_port, |
| 370 | ip6_address_t * rmt, u16 rmt_port) |
| 371 | { |
| 372 | u32 action_index; |
| 373 | action_index = session_rules_table_lookup6 (srt, proto, lcl, rmt, lcl_port, |
| 374 | rmt_port); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 375 | return session_lookup_app_listen_session (action_index, FIB_PROTOCOL_IP6, |
| 376 | proto); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 379 | u64 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 380 | session_lookup_session_endpoint (u32 table_index, session_endpoint_t * sep) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 381 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 382 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 383 | session_kv4_t kv4; |
| 384 | session_kv6_t kv6; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 385 | ip4_address_t lcl4; |
| 386 | ip6_address_t lcl6; |
| 387 | u32 si; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 388 | int rv; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 389 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 390 | st = session_table_get (table_index); |
| 391 | if (!st) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 392 | return SESSION_INVALID_HANDLE; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 393 | if (sep->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 394 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 395 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 396 | sep->transport_proto); |
| 397 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 398 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 399 | return kv4.value; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 400 | |
| 401 | memset (&lcl4, 0, sizeof (lcl4)); |
| 402 | si = |
| 403 | session_rules_table_lookup4 (&st->session_rules, sep->transport_proto, |
| 404 | &lcl4, &sep->ip.ip4, 0, sep->port); |
| 405 | if (si != SESSION_RULES_TABLE_INVALID_INDEX) |
| 406 | return si; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 407 | } |
| 408 | else |
| 409 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 410 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 411 | sep->transport_proto); |
| 412 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 413 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 414 | return kv6.value; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 415 | |
| 416 | memset (&lcl6, 0, sizeof (lcl6)); |
| 417 | si = |
| 418 | session_rules_table_lookup6 (&st->session_rules, sep->transport_proto, |
| 419 | &lcl6, &sep->ip.ip6, 0, sep->port); |
| 420 | if (si != SESSION_RULES_TABLE_INVALID_INDEX) |
| 421 | return si; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 422 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 423 | return SESSION_INVALID_HANDLE; |
| 424 | } |
| 425 | |
| 426 | stream_session_t * |
| 427 | session_lookup_global_session_endpoint (session_endpoint_t * sep) |
| 428 | { |
| 429 | session_table_t *st; |
| 430 | session_kv4_t kv4; |
| 431 | session_kv6_t kv6; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 432 | ip4_address_t lcl4; |
| 433 | ip6_address_t lcl6; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 434 | u8 fib_proto; |
| 435 | u32 table_index; |
| 436 | int rv; |
| 437 | |
| 438 | fib_proto = session_endpoint_fib_proto (sep); |
| 439 | table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index); |
| 440 | st = session_table_get (table_index); |
| 441 | if (!st) |
| 442 | return 0; |
| 443 | if (sep->is_ip4) |
| 444 | { |
| 445 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 446 | sep->transport_proto); |
| 447 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 448 | if (rv == 0) |
| 449 | return session_get_from_handle (kv4.value); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 450 | memset (&lcl4, 0, sizeof (lcl4)); |
| 451 | return session_lookup_rules_table4 (&st->session_rules, |
| 452 | sep->transport_proto, &lcl4, 0, |
| 453 | &sep->ip.ip4, sep->port); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 454 | } |
| 455 | else |
| 456 | { |
| 457 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 458 | sep->transport_proto); |
| 459 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 460 | if (rv == 0) |
| 461 | return session_get_from_handle (kv6.value); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 462 | memset (&lcl6, 0, sizeof (lcl6)); |
| 463 | return session_lookup_rules_table6 (&st->session_rules, |
| 464 | sep->transport_proto, &lcl6, 0, |
| 465 | &sep->ip.ip6, sep->port); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 466 | } |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 469 | u32 |
| 470 | session_lookup_local_session_endpoint (u32 table_index, |
| 471 | session_endpoint_t * sep) |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 472 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 473 | session_table_t *st; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 474 | session_kv4_t kv4; |
| 475 | session_kv6_t kv6; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 476 | ip4_address_t lcl4; |
| 477 | ip6_address_t lcl6; |
| 478 | u32 si; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 479 | int rv; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 480 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 481 | st = session_table_get (table_index); |
| 482 | if (!st) |
| 483 | return SESSION_INVALID_INDEX; |
| 484 | if (sep->is_ip4) |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 485 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 486 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 487 | sep->transport_proto); |
| 488 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 489 | if (rv == 0) |
| 490 | return (u32) kv4.value; |
| 491 | |
| 492 | /* |
| 493 | * Zero out the ip. Logic is that connect to local ips, say |
| 494 | * 127.0.0.1:port, can match 0.0.0.0:port |
| 495 | */ |
| 496 | kv4.key[0] = 0; |
| 497 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 498 | if (rv == 0) |
| 499 | return (u32) kv4.value; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 500 | |
| 501 | memset (&lcl4, 0, sizeof (lcl4)); |
| 502 | si = |
| 503 | session_rules_table_lookup4 (&st->session_rules, sep->transport_proto, |
| 504 | &lcl4, &sep->ip.ip4, 0, sep->port); |
| 505 | if (si != SESSION_RULES_TABLE_INVALID_INDEX) |
| 506 | return si; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 507 | } |
| 508 | else |
| 509 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 510 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 511 | sep->transport_proto); |
| 512 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 513 | if (rv == 0) |
| 514 | return (u32) kv6.value; |
| 515 | |
| 516 | /* |
| 517 | * Zero out the ip. Same logic as above. |
| 518 | */ |
| 519 | kv6.key[0] = kv6.key[1] = 0; |
| 520 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 521 | if (rv == 0) |
| 522 | return (u32) kv6.value; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 523 | |
| 524 | memset (&lcl6, 0, sizeof (lcl6)); |
| 525 | si = |
| 526 | session_rules_table_lookup6 (&st->session_rules, sep->transport_proto, |
| 527 | &lcl6, &sep->ip.ip6, 0, sep->port); |
| 528 | if (si != SESSION_RULES_TABLE_INVALID_INDEX) |
| 529 | return si; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 530 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 531 | return SESSION_INVALID_INDEX; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 534 | static stream_session_t * |
| 535 | session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl, |
| 536 | u16 lcl_port, u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 537 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 538 | session_kv4_t kv4; |
| 539 | int rv; |
| 540 | |
| 541 | make_v4_listener_kv (&kv4, lcl, lcl_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 542 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 543 | if (rv == 0) |
| 544 | return session_manager_get_listener (proto, (u32) kv4.value); |
| 545 | |
| 546 | /* Zero out the lcl ip */ |
| 547 | kv4.key[0] = 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 548 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 549 | if (rv == 0) |
| 550 | return session_manager_get_listener (proto, (u32) kv4.value); |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 555 | stream_session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 556 | session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port, |
| 557 | u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 558 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 559 | session_table_t *st; |
| 560 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 561 | if (!st) |
| 562 | return 0; |
| 563 | return session_lookup_listener4_i (st, lcl, lcl_port, proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 566 | static stream_session_t * |
| 567 | session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl, |
| 568 | u16 lcl_port, u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 569 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 570 | session_kv6_t kv6; |
| 571 | int rv; |
| 572 | |
| 573 | make_v6_listener_kv (&kv6, lcl, lcl_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 574 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 575 | if (rv == 0) |
| 576 | return session_manager_get_listener (proto, (u32) kv6.value); |
| 577 | |
| 578 | /* Zero out the lcl ip */ |
| 579 | kv6.key[0] = kv6.key[1] = 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 580 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 581 | if (rv == 0) |
| 582 | return session_manager_get_listener (proto, (u32) kv6.value); |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 587 | stream_session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 588 | session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port, |
| 589 | u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 590 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 591 | session_table_t *st; |
| 592 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 593 | if (!st) |
| 594 | return 0; |
| 595 | return session_lookup_listener6_i (st, lcl, lcl_port, proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | stream_session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 599 | session_lookup_listener (u32 table_index, session_endpoint_t * sep) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 600 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 601 | session_table_t *st; |
| 602 | st = session_table_get (table_index); |
| 603 | if (!st) |
| 604 | return 0; |
| 605 | if (sep->is_ip4) |
| 606 | return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port, |
| 607 | sep->transport_proto); |
| 608 | else |
| 609 | return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port, |
| 610 | sep->transport_proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 614 | int |
| 615 | session_lookup_add_half_open (transport_connection_t * tc, u64 value) |
| 616 | { |
| 617 | session_table_t *st; |
| 618 | session_kv4_t kv4; |
| 619 | session_kv6_t kv6; |
| 620 | |
| 621 | st = session_table_get_or_alloc_for_connection (tc); |
| 622 | if (!st) |
| 623 | return 0; |
| 624 | if (tc->is_ip4) |
| 625 | { |
| 626 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 627 | kv4.value = value; |
| 628 | return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4, |
| 629 | 1 /* is_add */ ); |
| 630 | } |
| 631 | else |
| 632 | { |
| 633 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 634 | kv6.value = value; |
| 635 | return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6, |
| 636 | 1 /* is_add */ ); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | int |
| 641 | session_lookup_del_half_open (transport_connection_t * tc) |
| 642 | { |
| 643 | session_table_t *st; |
| 644 | session_kv4_t kv4; |
| 645 | session_kv6_t kv6; |
| 646 | |
| 647 | st = session_table_get_for_connection (tc); |
| 648 | if (!st) |
| 649 | return -1; |
| 650 | if (tc->is_ip4) |
| 651 | { |
| 652 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 653 | return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4, |
| 654 | 0 /* is_add */ ); |
| 655 | } |
| 656 | else |
| 657 | { |
| 658 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 659 | return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6, |
| 660 | 0 /* is_add */ ); |
| 661 | } |
| 662 | } |
| 663 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 664 | u64 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 665 | session_lookup_half_open_handle (transport_connection_t * tc) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 666 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 667 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 668 | session_kv4_t kv4; |
| 669 | session_kv6_t kv6; |
| 670 | int rv; |
| 671 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 672 | st = session_table_get_for_fib_index (transport_connection_fib_proto (tc), |
| 673 | tc->fib_index); |
| 674 | if (!st) |
| 675 | return HALF_OPEN_LOOKUP_INVALID_VALUE; |
| 676 | if (tc->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 677 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 678 | make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 679 | tc->rmt_port, tc->proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 680 | rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 681 | if (rv == 0) |
| 682 | return kv4.value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 683 | } |
| 684 | else |
| 685 | { |
| 686 | make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 687 | tc->rmt_port, tc->proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 688 | rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 689 | if (rv == 0) |
| 690 | return kv6.value; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 691 | } |
| 692 | return HALF_OPEN_LOOKUP_INVALID_VALUE; |
| 693 | } |
| 694 | |
| 695 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 696 | session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 697 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 698 | u32 sst; |
| 699 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 700 | if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 701 | { |
| 702 | sst = session_type_from_proto_and_ip (proto, is_ip4); |
| 703 | return tp_vfts[sst].get_half_open (handle & 0xFFFFFFFF); |
| 704 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 708 | transport_connection_t * |
| 709 | session_lookup_rules_table_connection4 (session_rules_table_t * srt, u8 proto, |
| 710 | ip4_address_t * lcl, u16 lcl_port, |
| 711 | ip4_address_t * rmt, u16 rmt_port) |
| 712 | { |
| 713 | stream_session_t *s; |
| 714 | s = session_lookup_rules_table4 (srt, proto, lcl, lcl_port, rmt, rmt_port); |
| 715 | if (s) |
| 716 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | transport_connection_t * |
| 721 | session_lookup_rules_table_connection6 (session_rules_table_t * srt, u8 proto, |
| 722 | ip6_address_t * lcl, u16 lcl_port, |
| 723 | ip6_address_t * rmt, u16 rmt_port) |
| 724 | { |
| 725 | stream_session_t *s; |
| 726 | s = session_lookup_rules_table6 (srt, proto, lcl, lcl_port, rmt, rmt_port); |
| 727 | if (s) |
| 728 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 729 | return 0; |
| 730 | } |
| 731 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 732 | /** |
| 733 | * Lookup connection with ip4 and transport layer information |
| 734 | * |
| 735 | * This is used on the fast path so it needs to be fast. Thereby, |
| 736 | * duplication of code and 'hacks' allowed. |
| 737 | * |
| 738 | * The lookup is incremental and returns whenever something is matched. The |
| 739 | * steps are: |
| 740 | * - Try to find an established session |
| 741 | * - Try to find a fully-formed or local source wildcarded (listener bound to |
| 742 | * all interfaces) listener session |
| 743 | * - Try to find a half-open connection |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 744 | * - Try session rules table |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 745 | * - return 0 |
| 746 | * |
| 747 | * @param fib_index index of fib wherein the connection was received |
| 748 | * @param lcl local ip4 address |
| 749 | * @param rmt remote ip4 address |
| 750 | * @param lcl_port local port |
| 751 | * @param rmt_port remote port |
| 752 | * @param proto transport protocol (e.g., tcp, udp) |
| 753 | * @param thread_index thread index for request |
| 754 | * |
| 755 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 756 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 757 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 758 | session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl, |
| 759 | ip4_address_t * rmt, u16 lcl_port, |
| 760 | u16 rmt_port, u8 proto, u32 thread_index) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 761 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 762 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 763 | session_kv4_t kv4; |
| 764 | stream_session_t *s; |
| 765 | int rv; |
| 766 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 767 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 768 | if (PREDICT_FALSE (!st)) |
| 769 | return 0; |
| 770 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 771 | /* Lookup session amongst established ones */ |
| 772 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 773 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 774 | if (rv == 0) |
| 775 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 776 | ASSERT ((u32) (kv4.value >> 32) == thread_index); |
| 777 | s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 778 | return tp_vfts[s->session_type].get_connection (s->connection_index, |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 779 | thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* If nothing is found, check if any listener is available */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 783 | s = session_lookup_listener4_i (st, lcl, lcl_port, proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 784 | if (s) |
| 785 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 786 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 787 | /* Try half-open connections */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 788 | rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 789 | if (rv == 0) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 790 | { |
| 791 | u32 sst = session_type_from_proto_and_ip (proto, 1); |
| 792 | return tp_vfts[sst].get_half_open (kv4.value & 0xFFFFFFFF); |
| 793 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 794 | |
| 795 | /* Check the session rules table */ |
| 796 | return session_lookup_rules_table_connection4 (&st->session_rules, proto, |
| 797 | lcl, lcl_port, rmt, |
| 798 | rmt_port); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 801 | /** |
| 802 | * Lookup connection with ip4 and transport layer information |
| 803 | * |
| 804 | * Not optimized. This is used on the fast path so it needs to be fast. |
| 805 | * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical |
| 806 | * to that of @ref session_lookup_connection_wt4 |
| 807 | * |
| 808 | * @param fib_index index of the fib wherein the connection was received |
| 809 | * @param lcl local ip4 address |
| 810 | * @param rmt remote ip4 address |
| 811 | * @param lcl_port local port |
| 812 | * @param rmt_port remote port |
| 813 | * @param proto transport protocol (e.g., tcp, udp) |
| 814 | * |
| 815 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 816 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 817 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 818 | session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl, |
| 819 | ip4_address_t * rmt, u16 lcl_port, u16 rmt_port, |
| 820 | u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 821 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 822 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 823 | session_kv4_t kv4; |
| 824 | stream_session_t *s; |
| 825 | int rv; |
| 826 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 827 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 828 | if (PREDICT_FALSE (!st)) |
| 829 | return 0; |
| 830 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 831 | /* Lookup session amongst established ones */ |
| 832 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 833 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 834 | if (rv == 0) |
| 835 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 836 | s = session_get_from_handle (kv4.value); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 837 | return tp_vfts[s->session_type].get_connection (s->connection_index, |
| 838 | s->thread_index); |
| 839 | } |
| 840 | |
| 841 | /* If nothing is found, check if any listener is available */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 842 | s = session_lookup_listener4_i (st, lcl, lcl_port, proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 843 | if (s) |
| 844 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 845 | |
| 846 | /* Finally, try half-open connections */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 847 | rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 848 | if (rv == 0) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 849 | { |
| 850 | u32 sst = session_type_from_proto_and_ip (proto, 1); |
| 851 | return tp_vfts[sst].get_half_open (kv4.value & 0xFFFFFFFF); |
| 852 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 853 | /* Check the session rules table */ |
| 854 | return session_lookup_rules_table_connection4 (&st->session_rules, proto, |
| 855 | lcl, lcl_port, rmt, |
| 856 | rmt_port); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 859 | /** |
| 860 | * Lookup session with ip4 and transport layer information |
| 861 | * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 862 | * Important note: this may look into another thread's pool table and |
| 863 | * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as |
| 864 | * if needed as soon as possible. |
| 865 | * |
| 866 | * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but |
| 867 | * this returns a session as opposed to a transport connection and it does not |
| 868 | * try to lookup half-open sessions. |
| 869 | * |
| 870 | * Typically used by dgram connections |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 871 | */ |
| 872 | stream_session_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 873 | session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt, |
| 874 | u16 lcl_port, u16 rmt_port, u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 875 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 876 | session_table_t *st; |
| 877 | session_kv4_t kv4; |
| 878 | stream_session_t *s; |
| 879 | int rv; |
| 880 | |
| 881 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 882 | if (PREDICT_FALSE (!st)) |
| 883 | return 0; |
| 884 | |
| 885 | /* Lookup session amongst established ones */ |
| 886 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
| 887 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 888 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 889 | return session_get_from_handle_safe (kv4.value); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 890 | |
| 891 | /* If nothing is found, check if any listener is available */ |
| 892 | if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto))) |
| 893 | return s; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 894 | return session_lookup_rules_table4 (&st->session_rules, proto, lcl, |
| 895 | lcl_port, rmt, rmt_port); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /** |
| 899 | * Lookup connection with ip6 and transport layer information |
| 900 | * |
| 901 | * This is used on the fast path so it needs to be fast. Thereby, |
| 902 | * duplication of code and 'hacks' allowed. |
| 903 | * |
| 904 | * The lookup is incremental and returns whenever something is matched. The |
| 905 | * steps are: |
| 906 | * - Try to find an established session |
| 907 | * - Try to find a fully-formed or local source wildcarded (listener bound to |
| 908 | * all interfaces) listener session |
| 909 | * - Try to find a half-open connection |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 910 | * - Try session rules table |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 911 | * - return 0 |
| 912 | * |
| 913 | * @param fib_index index of the fib wherein the connection was received |
| 914 | * @param lcl local ip6 address |
| 915 | * @param rmt remote ip6 address |
| 916 | * @param lcl_port local port |
| 917 | * @param rmt_port remote port |
| 918 | * @param proto transport protocol (e.g., tcp, udp) |
| 919 | * @param thread_index thread index for request |
| 920 | * |
| 921 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 922 | */ |
| 923 | transport_connection_t * |
| 924 | session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl, |
| 925 | ip6_address_t * rmt, u16 lcl_port, |
| 926 | u16 rmt_port, u8 proto, u32 thread_index) |
| 927 | { |
| 928 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 929 | stream_session_t *s; |
| 930 | session_kv6_t kv6; |
| 931 | int rv; |
| 932 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 933 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 934 | if (PREDICT_FALSE (!st)) |
| 935 | return 0; |
| 936 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 937 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 938 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 939 | if (rv == 0) |
| 940 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 941 | ASSERT ((u32) (kv6.value >> 32) == thread_index); |
| 942 | s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 943 | return tp_vfts[s->session_type].get_connection (s->connection_index, |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 944 | thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /* If nothing is found, check if any listener is available */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 948 | s = session_lookup_listener6_i (st, lcl, lcl_port, proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 949 | if (s) |
| 950 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 951 | |
| 952 | /* Finally, try half-open connections */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 953 | rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 954 | if (rv == 0) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 955 | { |
| 956 | u32 sst = session_type_from_proto_and_ip (proto, 1); |
| 957 | return tp_vfts[sst].get_half_open (kv6.value & 0xFFFFFFFF); |
| 958 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 959 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 960 | return session_lookup_rules_table_connection6 (&st->session_rules, proto, |
| 961 | lcl, lcl_port, rmt, |
| 962 | rmt_port); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 965 | /** |
| 966 | * Lookup connection with ip6 and transport layer information |
| 967 | * |
| 968 | * Not optimized. This is used on the fast path so it needs to be fast. |
| 969 | * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical |
| 970 | * to that of @ref session_lookup_connection_wt4 |
| 971 | * |
| 972 | * @param fib_index index of the fib wherein the connection was received |
| 973 | * @param lcl local ip6 address |
| 974 | * @param rmt remote ip6 address |
| 975 | * @param lcl_port local port |
| 976 | * @param rmt_port remote port |
| 977 | * @param proto transport protocol (e.g., tcp, udp) |
| 978 | * |
| 979 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 980 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 981 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 982 | session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl, |
| 983 | ip6_address_t * rmt, u16 lcl_port, u16 rmt_port, |
| 984 | u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 985 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 986 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 987 | stream_session_t *s; |
| 988 | session_kv6_t kv6; |
| 989 | int rv; |
| 990 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 991 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 992 | if (PREDICT_FALSE (!st)) |
| 993 | return 0; |
| 994 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 995 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 996 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 997 | if (rv == 0) |
| 998 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 999 | s = session_get_from_handle (kv6.value); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1000 | return tp_vfts[s->session_type].get_connection (s->connection_index, |
| 1001 | s->thread_index); |
| 1002 | } |
| 1003 | |
| 1004 | /* If nothing is found, check if any listener is available */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1005 | s = session_lookup_listener6 (fib_index, lcl, lcl_port, proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1006 | if (s) |
| 1007 | return tp_vfts[s->session_type].get_listener (s->connection_index); |
| 1008 | |
| 1009 | /* Finally, try half-open connections */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1010 | rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1011 | if (rv == 0) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1012 | { |
| 1013 | u32 sst = session_type_from_proto_and_ip (proto, 1); |
| 1014 | return tp_vfts[sst].get_half_open (kv6.value & 0xFFFFFFFF); |
| 1015 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1016 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1017 | return session_lookup_rules_table_connection6 (&st->session_rules, proto, |
| 1018 | lcl, lcl_port, rmt, |
| 1019 | rmt_port); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1022 | /** |
| 1023 | * Lookup session with ip6 and transport layer information |
| 1024 | * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1025 | * Important note: this may look into another thread's pool table and |
| 1026 | * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as |
| 1027 | * if needed as soon as possible. |
| 1028 | * |
| 1029 | * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but |
| 1030 | * this returns a session as opposed to a transport connection and it does not |
| 1031 | * try to lookup half-open sessions. |
| 1032 | * |
| 1033 | * Typically used by dgram connections |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1034 | */ |
| 1035 | stream_session_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1036 | session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt, |
| 1037 | u16 lcl_port, u16 rmt_port, u8 proto) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1038 | { |
| 1039 | session_table_t *st; |
| 1040 | session_kv6_t kv6; |
| 1041 | stream_session_t *s; |
| 1042 | int rv; |
| 1043 | |
| 1044 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 1045 | if (PREDICT_FALSE (!st)) |
| 1046 | return 0; |
| 1047 | |
| 1048 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
| 1049 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 1050 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1051 | return session_get_from_handle_safe (kv6.value); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1052 | |
| 1053 | /* If nothing is found, check if any listener is available */ |
| 1054 | if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto))) |
| 1055 | return s; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1056 | return session_lookup_rules_table6 (&st->session_rules, proto, lcl, |
| 1057 | lcl_port, rmt, rmt_port); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | u64 |
| 1061 | session_lookup_local_listener_make_handle (session_endpoint_t * sep) |
| 1062 | { |
| 1063 | return ((u64) SESSION_LOCAL_TABLE_PREFIX << 32 |
| 1064 | | (u32) sep->port << 16 | (u32) sep->transport_proto << 8 |
| 1065 | | (u32) sep->is_ip4); |
| 1066 | } |
| 1067 | |
| 1068 | u8 |
| 1069 | session_lookup_local_is_handle (u64 handle) |
| 1070 | { |
| 1071 | if (handle >> 32 == SESSION_LOCAL_TABLE_PREFIX) |
| 1072 | return 1; |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | int |
| 1077 | session_lookup_local_listener_parse_handle (u64 handle, |
| 1078 | session_endpoint_t * sep) |
| 1079 | { |
| 1080 | u32 local_table_handle; |
| 1081 | if (handle >> 32 != SESSION_LOCAL_TABLE_PREFIX) |
| 1082 | return -1; |
| 1083 | local_table_handle = handle & 0xFFFFFFFFULL; |
| 1084 | sep->is_ip4 = local_table_handle & 0xff; |
| 1085 | local_table_handle >>= 8; |
| 1086 | sep->transport_proto = local_table_handle & 0xff; |
| 1087 | sep->port = local_table_handle >> 8; |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1091 | clib_error_t * |
| 1092 | vnet_session_rule_add_del (session_rule_add_del_args_t * args) |
| 1093 | { |
| 1094 | app_namespace_t *app_ns = app_namespace_get (args->appns_index); |
| 1095 | session_table_t *st; |
| 1096 | u32 fib_index; |
| 1097 | u8 fib_proto; |
| 1098 | clib_error_t *error; |
| 1099 | |
| 1100 | if (!app_ns) |
| 1101 | return clib_error_return_code (0, VNET_API_ERROR_APP_INVALID_NS, 0, |
| 1102 | "invalid app ns"); |
| 1103 | if (args->scope > 3) |
| 1104 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, |
| 1105 | "invalid scope"); |
| 1106 | if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0) |
| 1107 | { |
| 1108 | fib_proto = args->table_args.rmt.fp_proto; |
| 1109 | fib_index = app_namespace_get_fib_index (app_ns, fib_proto); |
| 1110 | st = session_table_get_for_fib_index (fib_proto, fib_index); |
| 1111 | if ((error = session_rules_table_add_del (&st->session_rules, |
| 1112 | &args->table_args))) |
| 1113 | return error; |
| 1114 | } |
| 1115 | if (args->scope & SESSION_RULE_SCOPE_LOCAL) |
| 1116 | { |
| 1117 | st = app_namespace_get_local_table (app_ns); |
| 1118 | error = |
| 1119 | session_rules_table_add_del (&st->session_rules, &args->table_args); |
| 1120 | } |
| 1121 | return error; |
| 1122 | } |
| 1123 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1124 | u8 * |
| 1125 | format_ip4_session_lookup_kvp (u8 * s, va_list * args) |
| 1126 | { |
| 1127 | clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *); |
| 1128 | u32 is_local = va_arg (*args, u32); |
| 1129 | u8 *app_name, *str = 0; |
| 1130 | stream_session_t *session; |
| 1131 | v4_connection_key_t *key = (v4_connection_key_t *) kvp->key; |
| 1132 | |
| 1133 | char *proto = key->proto == TRANSPORT_PROTO_TCP ? "T" : "U"; |
| 1134 | if (!is_local) |
| 1135 | { |
| 1136 | session = session_get_from_handle (kvp->value); |
| 1137 | app_name = application_name_from_index (session->app_index); |
| 1138 | str = format (0, "[%s] %U:%d->%U:%d", proto, format_ip4_address, |
| 1139 | &key->src, clib_net_to_host_u16 (key->src_port), |
| 1140 | format_ip4_address, &key->dst, |
| 1141 | clib_net_to_host_u16 (key->dst_port)); |
| 1142 | s = format (s, "%-40v%-30v", str, app_name); |
| 1143 | } |
| 1144 | else |
| 1145 | { |
| 1146 | app_name = application_name_from_index (kvp->value); |
| 1147 | str = format (0, "[%s] %U:%d", proto, format_ip4_address, |
| 1148 | &key->src, clib_net_to_host_u16 (key->src_port)); |
| 1149 | s = format (s, "%-30v%-30v", str, app_name); |
| 1150 | } |
| 1151 | vec_free (app_name); |
| 1152 | return s; |
| 1153 | } |
| 1154 | |
| 1155 | typedef struct _ip4_session_table_show_ctx_t |
| 1156 | { |
| 1157 | vlib_main_t *vm; |
| 1158 | u8 is_local; |
| 1159 | } ip4_session_table_show_ctx_t; |
| 1160 | |
| 1161 | static int |
| 1162 | ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg) |
| 1163 | { |
| 1164 | ip4_session_table_show_ctx_t *ctx = arg; |
| 1165 | vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp, |
| 1166 | ctx->is_local); |
| 1167 | return 1; |
| 1168 | } |
| 1169 | |
| 1170 | void |
| 1171 | session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table, |
| 1172 | u8 type, u8 is_local) |
| 1173 | { |
| 1174 | ip4_session_table_show_ctx_t ctx = { |
| 1175 | .vm = vm, |
| 1176 | .is_local = is_local, |
| 1177 | }; |
| 1178 | if (!is_local) |
| 1179 | vlib_cli_output (vm, "%-40s%-30s", "Session", "Application"); |
| 1180 | else |
| 1181 | vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application"); |
| 1182 | switch (type) |
| 1183 | { |
| 1184 | /* main table v4 */ |
| 1185 | case 0: |
| 1186 | ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show, |
| 1187 | &ctx); |
| 1188 | break; |
| 1189 | default: |
| 1190 | clib_warning ("not supported"); |
| 1191 | } |
| 1192 | } |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1193 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1194 | static clib_error_t * |
| 1195 | session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1196 | vlib_cli_command_t * cmd) |
| 1197 | { |
| 1198 | u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen, rmt_plen; |
| 1199 | u32 appns_index, scope = 0; |
| 1200 | ip46_address_t lcl_ip, rmt_ip; |
| 1201 | u8 is_ip4 = 1, conn_set = 0; |
| 1202 | u8 fib_proto, is_add = 1, *ns_id = 0; |
| 1203 | app_namespace_t *app_ns; |
| 1204 | |
| 1205 | memset (&lcl_ip, 0, sizeof (lcl_ip)); |
| 1206 | memset (&rmt_ip, 0, sizeof (rmt_ip)); |
| 1207 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1208 | { |
| 1209 | if (unformat (input, "del")) |
| 1210 | is_add = 0; |
| 1211 | else if (unformat (input, "add")) |
| 1212 | ; |
| 1213 | else if (unformat (input, "appns %_%v%_", &ns_id)) |
| 1214 | ; |
| 1215 | else if (unformat (input, "scope global")) |
| 1216 | scope = SESSION_RULE_SCOPE_GLOBAL; |
| 1217 | else if (unformat (input, "scope local")) |
| 1218 | scope = SESSION_RULE_SCOPE_LOCAL; |
| 1219 | else if (unformat (input, "scope all")) |
| 1220 | scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL; |
| 1221 | else if (unformat (input, "proto %U", unformat_transport_proto, &proto)) |
| 1222 | ; |
| 1223 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address, |
| 1224 | &lcl_ip.ip4, &lcl_plen, &lcl_port, |
| 1225 | unformat_ip4_address, &rmt_ip.ip4, &rmt_plen, |
| 1226 | &rmt_port)) |
| 1227 | { |
| 1228 | is_ip4 = 1; |
| 1229 | conn_set = 1; |
| 1230 | } |
| 1231 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address, |
| 1232 | &lcl_ip.ip6, &lcl_plen, &lcl_port, |
| 1233 | unformat_ip6_address, &rmt_ip.ip6, &rmt_plen, |
| 1234 | &rmt_port)) |
| 1235 | { |
| 1236 | is_ip4 = 0; |
| 1237 | conn_set = 1; |
| 1238 | } |
| 1239 | else if (unformat (input, "action %d", &action)) |
| 1240 | ; |
| 1241 | else |
| 1242 | return clib_error_return (0, "unknown input `%U'", |
| 1243 | format_unformat_error, input); |
| 1244 | } |
| 1245 | |
| 1246 | if (proto == ~0 || !conn_set || action == ~0) |
| 1247 | return clib_error_return (0, "proto, connection and action must be set"); |
| 1248 | |
| 1249 | if (ns_id) |
| 1250 | { |
| 1251 | app_ns = app_namespace_get_from_id (ns_id); |
| 1252 | if (!app_ns) |
| 1253 | return clib_error_return (0, "namespace %v does not exist", ns_id); |
| 1254 | } |
| 1255 | else |
| 1256 | { |
| 1257 | app_ns = app_namespace_get_default (); |
| 1258 | } |
| 1259 | appns_index = app_namespace_index (app_ns); |
| 1260 | |
| 1261 | fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 1262 | session_rule_add_del_args_t args = { |
| 1263 | .table_args.lcl.fp_addr = lcl_ip, |
| 1264 | .table_args.lcl.fp_len = lcl_plen, |
| 1265 | .table_args.lcl.fp_proto = fib_proto, |
| 1266 | .table_args.rmt.fp_addr = rmt_ip, |
| 1267 | .table_args.rmt.fp_len = rmt_plen, |
| 1268 | .table_args.rmt.fp_proto = fib_proto, |
| 1269 | .table_args.lcl_port = lcl_port, |
| 1270 | .table_args.rmt_port = rmt_port, |
| 1271 | .table_args.action_index = action, |
| 1272 | .table_args.is_add = is_add, |
| 1273 | .appns_index = appns_index, |
| 1274 | .scope = scope, |
| 1275 | }; |
| 1276 | return vnet_session_rule_add_del (&args); |
| 1277 | } |
| 1278 | |
| 1279 | /* *INDENT-OFF* */ |
| 1280 | VLIB_CLI_COMMAND (session_rule_command, static) = |
| 1281 | { |
| 1282 | .path = "session rule", |
| 1283 | .short_help = "session rule [add|del] appns <ns_id> proto <proto> " |
| 1284 | "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>", |
| 1285 | .function = session_rule_command_fn, |
| 1286 | }; |
| 1287 | /* *INDENT-ON* */ |
| 1288 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1289 | void |
| 1290 | session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto, |
| 1291 | u8 transport_proto) |
| 1292 | { |
| 1293 | vlib_main_t *vm = vlib_get_main (); |
| 1294 | session_table_t *st; |
| 1295 | st = session_table_get_for_fib_index (fib_index, fib_proto); |
| 1296 | session_rules_table_cli_dump (vm, &st->session_rules, fib_proto, |
| 1297 | transport_proto); |
| 1298 | } |
| 1299 | |
| 1300 | void |
| 1301 | session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto, |
| 1302 | u8 transport_proto) |
| 1303 | { |
| 1304 | vlib_main_t *vm = vlib_get_main (); |
| 1305 | session_table_t *st; |
| 1306 | st = session_table_get (table_index); |
| 1307 | session_rules_table_cli_dump (vm, &st->session_rules, fib_proto, |
| 1308 | transport_proto); |
| 1309 | } |
| 1310 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1311 | static clib_error_t * |
| 1312 | show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1313 | vlib_cli_command_t * cmd) |
| 1314 | { |
| 1315 | u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen; |
| 1316 | u32 fib_index, scope = 0; |
| 1317 | ip46_address_t lcl_ip, rmt_ip; |
| 1318 | u8 is_ip4 = 1, show_one = 0; |
| 1319 | app_namespace_t *app_ns; |
| 1320 | session_table_t *st; |
| 1321 | u8 *ns_id = 0, fib_proto; |
| 1322 | |
| 1323 | memset (&lcl_ip, 0, sizeof (lcl_ip)); |
| 1324 | memset (&rmt_ip, 0, sizeof (rmt_ip)); |
| 1325 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1326 | { |
| 1327 | if (unformat (input, "%U", unformat_transport_proto, &transport_proto)) |
| 1328 | ; |
| 1329 | else if (unformat (input, "appns %_%v%_", &ns_id)) |
| 1330 | ; |
| 1331 | else if (unformat (input, "scope global")) |
| 1332 | scope = 1; |
| 1333 | else if (unformat (input, "scope local")) |
| 1334 | scope = 2; |
| 1335 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address, |
| 1336 | &lcl_ip.ip4, &lcl_plen, &lcl_port, |
| 1337 | unformat_ip4_address, &rmt_ip.ip4, &rmt_plen, |
| 1338 | &rmt_port)) |
| 1339 | { |
| 1340 | is_ip4 = 1; |
| 1341 | show_one = 1; |
| 1342 | } |
| 1343 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address, |
| 1344 | &lcl_ip.ip6, &lcl_plen, &lcl_port, |
| 1345 | unformat_ip6_address, &rmt_ip.ip6, &rmt_plen, |
| 1346 | &rmt_port)) |
| 1347 | { |
| 1348 | is_ip4 = 0; |
| 1349 | show_one = 1; |
| 1350 | } |
| 1351 | else |
| 1352 | return clib_error_return (0, "unknown input `%U'", |
| 1353 | format_unformat_error, input); |
| 1354 | } |
| 1355 | |
| 1356 | if (transport_proto == ~0) |
| 1357 | { |
| 1358 | vlib_cli_output (vm, "transport proto must be set"); |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
| 1362 | if (ns_id) |
| 1363 | { |
| 1364 | app_ns = app_namespace_get_from_id (ns_id); |
| 1365 | if (!app_ns) |
| 1366 | { |
| 1367 | vlib_cli_output (vm, "appns %v doesn't exist", ns_id); |
| 1368 | return 0; |
| 1369 | } |
| 1370 | } |
| 1371 | else |
| 1372 | { |
| 1373 | app_ns = app_namespace_get_default (); |
| 1374 | } |
| 1375 | |
| 1376 | if (scope == 1 || scope == 0) |
| 1377 | { |
| 1378 | fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 1379 | fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 1380 | st = session_table_get_for_fib_index (fib_proto, fib_index); |
| 1381 | } |
| 1382 | else |
| 1383 | { |
| 1384 | st = app_namespace_get_local_table (app_ns); |
| 1385 | } |
| 1386 | |
| 1387 | if (show_one) |
| 1388 | { |
| 1389 | session_rules_table_show_rule (vm, &st->session_rules, transport_proto, |
| 1390 | &lcl_ip, lcl_port, &rmt_ip, rmt_port, |
| 1391 | is_ip4); |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | session_rules_table_cli_dump (vm, &st->session_rules, FIB_PROTOCOL_IP4, |
| 1396 | transport_proto); |
| 1397 | session_rules_table_cli_dump (vm, &st->session_rules, FIB_PROTOCOL_IP6, |
| 1398 | transport_proto); |
| 1399 | |
| 1400 | vec_free (ns_id); |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | /* *INDENT-OFF* */ |
| 1405 | VLIB_CLI_COMMAND (show_session_rules_command, static) = |
| 1406 | { |
| 1407 | .path = "show session rules", |
| 1408 | .short_help = "show session rules [appns <id> proto <proto> <lcl-ip/plen>" |
| 1409 | " <lcl-port> <rmt-ip/plen> <rmt-port>]", |
| 1410 | .function = show_session_rules_command_fn, |
| 1411 | }; |
| 1412 | /* *INDENT-ON* */ |
| 1413 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1414 | void |
| 1415 | session_lookup_init (void) |
| 1416 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1417 | /* |
| 1418 | * Allocate default table and map it to fib_index 0 |
| 1419 | */ |
| 1420 | session_table_t *st = session_table_alloc (); |
| 1421 | vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0); |
| 1422 | fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st); |
| 1423 | session_table_init (st); |
| 1424 | st = session_table_alloc (); |
| 1425 | vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0); |
| 1426 | fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st); |
| 1427 | session_table_init (st); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | /* |
| 1431 | * fd.io coding-style-patch-verification: ON |
| 1432 | * |
| 1433 | * Local Variables: |
| 1434 | * eval: (c-set-style "gnu") |
| 1435 | * End: |
| 1436 | */ |