Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 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 | f9bdc03 | 2024-02-02 13:01:07 -0800 | [diff] [blame] | 32 | static session_lookup_main_t sl_main; |
| 33 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 34 | /** |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 35 | * Network namespace index (i.e., fib index) to session lookup table. We |
| 36 | * should have one per network protocol type but for now we only support IP4/6 |
| 37 | */ |
| 38 | static u32 *fib_index_to_table_index[2]; |
| 39 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 40 | /* *INDENT-OFF* */ |
| 41 | /* 16 octets */ |
| 42 | typedef CLIB_PACKED (struct { |
| 43 | union |
| 44 | { |
| 45 | struct |
| 46 | { |
| 47 | ip4_address_t src; |
| 48 | ip4_address_t dst; |
| 49 | u16 src_port; |
| 50 | u16 dst_port; |
| 51 | /* align by making this 4 octets even though its a 1-bit field |
| 52 | * NOTE: avoid key overlap with other transports that use 5 tuples for |
| 53 | * session identification. |
| 54 | */ |
| 55 | u32 proto; |
| 56 | }; |
| 57 | u64 as_u64[2]; |
| 58 | }; |
| 59 | }) v4_connection_key_t; |
| 60 | |
| 61 | typedef CLIB_PACKED (struct { |
| 62 | union |
| 63 | { |
| 64 | struct |
| 65 | { |
| 66 | /* 48 octets */ |
| 67 | ip6_address_t src; |
| 68 | ip6_address_t dst; |
| 69 | u16 src_port; |
| 70 | u16 dst_port; |
| 71 | u32 proto; |
| 72 | u64 unused; |
| 73 | }; |
| 74 | u64 as_u64[6]; |
| 75 | }; |
| 76 | }) v6_connection_key_t; |
| 77 | /* *INDENT-ON* */ |
| 78 | |
| 79 | typedef clib_bihash_kv_16_8_t session_kv4_t; |
| 80 | typedef clib_bihash_kv_48_8_t session_kv6_t; |
| 81 | |
| 82 | always_inline void |
| 83 | make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt, |
| 84 | u16 lcl_port, u16 rmt_port, u8 proto) |
| 85 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 86 | kv->key[0] = (u64) rmt->as_u32 << 32 | (u64) lcl->as_u32; |
| 87 | kv->key[1] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 88 | kv->value = ~0ULL; |
| 89 | } |
| 90 | |
| 91 | always_inline void |
| 92 | make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port, |
| 93 | u8 proto) |
| 94 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 95 | kv->key[0] = (u64) lcl->as_u32; |
| 96 | kv->key[1] = (u64) proto << 32 | (u64) lcl_port; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 97 | kv->value = ~0ULL; |
| 98 | } |
| 99 | |
| 100 | always_inline void |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 101 | make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto) |
| 102 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 103 | kv->key[0] = (u64) lcl->as_u32; |
| 104 | kv->key[1] = (u64) proto << 32; |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 105 | kv->value = ~0ULL; |
| 106 | } |
| 107 | |
| 108 | always_inline void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 109 | make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 110 | { |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 111 | make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port, |
| 112 | tc->rmt_port, tc->proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | always_inline void |
| 116 | make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt, |
| 117 | u16 lcl_port, u16 rmt_port, u8 proto) |
| 118 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 119 | kv->key[0] = lcl->as_u64[0]; |
| 120 | kv->key[1] = lcl->as_u64[1]; |
| 121 | kv->key[2] = rmt->as_u64[0]; |
| 122 | kv->key[3] = rmt->as_u64[1]; |
| 123 | kv->key[4] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port; |
| 124 | kv->key[5] = 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 125 | kv->value = ~0ULL; |
| 126 | } |
| 127 | |
| 128 | always_inline void |
| 129 | make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port, |
| 130 | u8 proto) |
| 131 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 132 | kv->key[0] = lcl->as_u64[0]; |
| 133 | kv->key[1] = lcl->as_u64[1]; |
| 134 | kv->key[2] = 0; |
| 135 | kv->key[3] = 0; |
| 136 | kv->key[4] = (u64) proto << 32 | (u64) lcl_port; |
| 137 | kv->key[5] = 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 138 | kv->value = ~0ULL; |
| 139 | } |
| 140 | |
| 141 | always_inline void |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 142 | make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto) |
| 143 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 144 | kv->key[0] = lcl->as_u64[0]; |
| 145 | kv->key[1] = lcl->as_u64[1]; |
| 146 | kv->key[2] = 0; |
| 147 | kv->key[3] = 0; |
| 148 | kv->key[4] = (u64) proto << 32; |
| 149 | kv->key[5] = 0; |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 150 | kv->value = ~0ULL; |
| 151 | } |
| 152 | |
| 153 | always_inline void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 154 | make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 155 | { |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 156 | make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port, |
| 157 | tc->rmt_port, tc->proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Florin Coras | f9bdc03 | 2024-02-02 13:01:07 -0800 | [diff] [blame] | 160 | static inline u8 |
| 161 | session_table_alloc_needs_sync (void) |
| 162 | { |
| 163 | return !vlib_thread_is_main_w_barrier () && (vlib_num_workers () > 1); |
| 164 | } |
| 165 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 166 | static session_table_t * |
Andreas Schultz | 30a28c1 | 2020-04-23 10:41:50 +0200 | [diff] [blame] | 167 | session_table_get_or_alloc (u8 fib_proto, u32 fib_index) |
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; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 170 | u32 table_index; |
Andreas Schultz | 30a28c1 | 2020-04-23 10:41:50 +0200 | [diff] [blame] | 171 | ASSERT (fib_index != ~0); |
| 172 | if (vec_len (fib_index_to_table_index[fib_proto]) > fib_index && |
| 173 | fib_index_to_table_index[fib_proto][fib_index] != ~0) |
| 174 | { |
| 175 | table_index = fib_index_to_table_index[fib_proto][fib_index]; |
| 176 | return session_table_get (table_index); |
| 177 | } |
| 178 | else |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 179 | { |
Florin Coras | f9bdc03 | 2024-02-02 13:01:07 -0800 | [diff] [blame] | 180 | u8 needs_sync = session_table_alloc_needs_sync (); |
| 181 | session_lookup_main_t *slm = &sl_main; |
| 182 | |
| 183 | /* Stop workers, otherwise consumers might be affected. This is |
| 184 | * acceptable because new tables should seldom be allocated */ |
| 185 | if (needs_sync) |
| 186 | { |
| 187 | vlib_workers_sync (); |
| 188 | |
| 189 | /* We might have a race, only one worker allowed at once */ |
| 190 | clib_spinlock_lock (&slm->st_alloc_lock); |
| 191 | } |
| 192 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 193 | st = session_table_alloc (); |
| 194 | table_index = session_table_index (st); |
Andreas Schultz | 30a28c1 | 2020-04-23 10:41:50 +0200 | [diff] [blame] | 195 | vec_validate_init_empty (fib_index_to_table_index[fib_proto], fib_index, |
| 196 | ~0); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 197 | fib_index_to_table_index[fib_proto][fib_index] = table_index; |
| 198 | st->active_fib_proto = fib_proto; |
Florin Coras | b795bd0 | 2017-12-14 11:30:48 -0800 | [diff] [blame] | 199 | session_table_init (st, fib_proto); |
Florin Coras | f9bdc03 | 2024-02-02 13:01:07 -0800 | [diff] [blame] | 200 | |
| 201 | if (needs_sync) |
| 202 | { |
| 203 | clib_spinlock_unlock (&slm->st_alloc_lock); |
| 204 | vlib_workers_continue (); |
| 205 | } |
| 206 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 207 | return st; |
| 208 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static session_table_t * |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 212 | session_table_get_or_alloc_for_connection (transport_connection_t * tc) |
| 213 | { |
| 214 | u32 fib_proto; |
| 215 | fib_proto = transport_connection_fib_proto (tc); |
| 216 | return session_table_get_or_alloc (fib_proto, tc->fib_index); |
| 217 | } |
| 218 | |
| 219 | static session_table_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 220 | session_table_get_for_connection (transport_connection_t * tc) |
| 221 | { |
| 222 | u32 fib_proto = transport_connection_fib_proto (tc); |
| 223 | if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index) |
| 224 | return 0; |
| 225 | return |
| 226 | session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]); |
| 227 | } |
| 228 | |
| 229 | static session_table_t * |
| 230 | session_table_get_for_fib_index (u32 fib_proto, u32 fib_index) |
| 231 | { |
| 232 | if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index) |
| 233 | return 0; |
| 234 | return session_table_get (fib_index_to_table_index[fib_proto][fib_index]); |
| 235 | } |
| 236 | |
| 237 | u32 |
| 238 | session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index) |
| 239 | { |
| 240 | if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index) |
| 241 | return SESSION_TABLE_INVALID_INDEX; |
| 242 | return fib_index_to_table_index[fib_proto][fib_index]; |
| 243 | } |
| 244 | |
Florin Coras | a4d0956 | 2021-07-08 08:25:09 -0700 | [diff] [blame] | 245 | u32 |
| 246 | session_lookup_get_or_alloc_index_for_fib (u32 fib_proto, u32 fib_index) |
| 247 | { |
| 248 | session_table_t *st; |
| 249 | st = session_table_get_or_alloc (fib_proto, fib_index); |
| 250 | return session_table_index (st); |
| 251 | } |
| 252 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Add transport connection to a session table |
| 255 | * |
| 256 | * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type) |
| 257 | * is added to requested session table. |
| 258 | * |
| 259 | * @param tc transport connection to be added |
| 260 | * @param value value to be stored |
| 261 | * |
| 262 | * @return non-zero if failure |
| 263 | */ |
| 264 | int |
| 265 | session_lookup_add_connection (transport_connection_t * tc, u64 value) |
| 266 | { |
| 267 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 268 | session_kv4_t kv4; |
| 269 | session_kv6_t kv6; |
| 270 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 271 | st = session_table_get_or_alloc_for_connection (tc); |
| 272 | if (!st) |
| 273 | return -1; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 274 | if (tc->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 275 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 276 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 277 | kv4.value = value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 278 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, |
| 279 | 1 /* is_add */ ); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 280 | } |
| 281 | else |
| 282 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 283 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 284 | kv6.value = value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 285 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, |
| 286 | 1 /* is_add */ ); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 290 | int |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 291 | session_lookup_add_session_endpoint (u32 table_index, |
| 292 | session_endpoint_t * sep, u64 value) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 293 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 294 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 295 | session_kv4_t kv4; |
| 296 | session_kv6_t kv6; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 297 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 298 | st = session_table_get (table_index); |
| 299 | if (!st) |
| 300 | return -1; |
| 301 | if (sep->is_ip4) |
| 302 | { |
| 303 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 304 | sep->transport_proto); |
| 305 | kv4.value = value; |
| 306 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 311 | sep->transport_proto); |
| 312 | kv6.value = value; |
| 313 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | int |
| 318 | session_lookup_del_session_endpoint (u32 table_index, |
| 319 | session_endpoint_t * sep) |
| 320 | { |
| 321 | session_table_t *st; |
| 322 | session_kv4_t kv4; |
| 323 | session_kv6_t kv6; |
| 324 | |
| 325 | st = session_table_get (table_index); |
| 326 | if (!st) |
| 327 | return -1; |
| 328 | if (sep->is_ip4) |
| 329 | { |
| 330 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 331 | sep->transport_proto); |
| 332 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 337 | sep->transport_proto); |
| 338 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0); |
| 339 | } |
| 340 | } |
| 341 | |
Florin Coras | aab0604 | 2020-02-26 02:56:14 +0000 | [diff] [blame] | 342 | int |
| 343 | session_lookup_del_session_endpoint2 (session_endpoint_t * sep) |
| 344 | { |
| 345 | fib_protocol_t fib_proto; |
| 346 | session_table_t *st; |
| 347 | session_kv4_t kv4; |
| 348 | session_kv6_t kv6; |
| 349 | |
| 350 | fib_proto = sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 351 | st = session_table_get_for_fib_index (fib_proto, sep->fib_index); |
| 352 | if (!st) |
| 353 | return -1; |
| 354 | if (sep->is_ip4) |
| 355 | { |
| 356 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 357 | sep->transport_proto); |
| 358 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0); |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 363 | sep->transport_proto); |
| 364 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0); |
| 365 | } |
| 366 | } |
| 367 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 368 | /** |
| 369 | * Delete transport connection from session table |
| 370 | * |
| 371 | * @param table_index session table index |
| 372 | * @param tc transport connection to be removed |
| 373 | * |
| 374 | * @return non-zero if failure |
| 375 | */ |
| 376 | int |
| 377 | session_lookup_del_connection (transport_connection_t * tc) |
| 378 | { |
| 379 | session_table_t *st; |
| 380 | session_kv4_t kv4; |
| 381 | session_kv6_t kv6; |
| 382 | |
| 383 | st = session_table_get_for_connection (tc); |
| 384 | if (!st) |
| 385 | return -1; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 386 | if (tc->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 387 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 388 | make_v4_ss_kv_from_tc (&kv4, tc); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 389 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 390 | 0 /* is_add */ ); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 391 | } |
| 392 | else |
| 393 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 394 | make_v6_ss_kv_from_tc (&kv6, tc); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 395 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 396 | 0 /* is_add */ ); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 397 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 401 | session_lookup_del_session (session_t * s) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 402 | { |
| 403 | transport_connection_t *ts; |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 404 | ts = transport_get_connection (session_get_transport_proto (s), |
| 405 | s->connection_index, s->thread_index); |
Florin Coras | e1e7fb8 | 2019-09-25 07:28:34 -0700 | [diff] [blame] | 406 | if (!ts || (ts->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP)) |
Nathan Skrzypczak | 2eed1a1 | 2019-07-04 14:26:21 +0200 | [diff] [blame] | 407 | return 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 408 | return session_lookup_del_connection (ts); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 411 | static u8 |
| 412 | session_lookup_action_index_is_valid (u32 action_index) |
Florin Coras | f0c1c96 | 2017-11-02 21:31:46 -0700 | [diff] [blame] | 413 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 414 | if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW |
| 415 | || action_index == SESSION_RULES_TABLE_INVALID_INDEX) |
| 416 | return 0; |
| 417 | return 1; |
| 418 | } |
| 419 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 420 | static u64 |
| 421 | session_lookup_action_to_handle (u32 action_index) |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 422 | { |
| 423 | switch (action_index) |
| 424 | { |
| 425 | case SESSION_RULES_TABLE_ACTION_DROP: |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 426 | return SESSION_DROP_HANDLE; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 427 | case SESSION_RULES_TABLE_ACTION_ALLOW: |
| 428 | case SESSION_RULES_TABLE_INVALID_INDEX: |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 429 | return SESSION_INVALID_HANDLE; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 430 | default: |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 431 | /* application index */ |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 432 | return action_index; |
| 433 | } |
Florin Coras | f0c1c96 | 2017-11-02 21:31:46 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 436 | static session_t * |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 437 | session_lookup_app_listen_session (u32 app_index, u8 fib_proto, |
| 438 | u8 transport_proto) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 439 | { |
| 440 | application_t *app; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 441 | app = application_get_if_valid (app_index); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 442 | if (!app) |
| 443 | return 0; |
| 444 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 445 | return app_worker_first_listener (application_get_default_worker (app), |
| 446 | fib_proto, transport_proto); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 449 | static session_t * |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 450 | session_lookup_action_to_session (u32 action_index, u8 fib_proto, |
| 451 | u8 transport_proto) |
| 452 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 453 | u32 app_index; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 454 | app_index = session_lookup_action_to_handle (action_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 455 | /* Nothing sophisticated for now, action index is app index */ |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 456 | return session_lookup_app_listen_session (app_index, fib_proto, |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 457 | transport_proto); |
| 458 | } |
| 459 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 460 | /** UNUSED */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 461 | session_t * |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 462 | session_lookup_rules_table_session4 (session_table_t * st, u8 proto, |
| 463 | ip4_address_t * lcl, u16 lcl_port, |
| 464 | ip4_address_t * rmt, u16 rmt_port) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 465 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 466 | session_rules_table_t *srt = &st->session_rules[proto]; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 467 | u32 action_index, app_index; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 468 | action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 469 | rmt_port); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 470 | app_index = session_lookup_action_to_handle (action_index); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 471 | /* Nothing sophisticated for now, action index is app index */ |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 472 | return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4, |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 473 | proto); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 476 | /** UNUSED */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 477 | session_t * |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 478 | session_lookup_rules_table_session6 (session_table_t * st, u8 proto, |
| 479 | ip6_address_t * lcl, u16 lcl_port, |
| 480 | ip6_address_t * rmt, u16 rmt_port) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 481 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 482 | session_rules_table_t *srt = &st->session_rules[proto]; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 483 | u32 action_index, app_index; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 484 | action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 485 | rmt_port); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 486 | app_index = session_lookup_action_to_handle (action_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 487 | return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6, |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 488 | proto); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 491 | /** |
| 492 | * Lookup listener for session endpoint in table |
| 493 | * |
| 494 | * @param table_index table where the endpoint should be looked up |
| 495 | * @param sep session endpoint to be looked up |
| 496 | * @param use_rules flag that indicates if the session rules of the table |
| 497 | * should be used |
| 498 | * @return invalid handle if nothing is found, the handle of a valid listener |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 499 | * or an action derived handle if a rule is hit |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 500 | */ |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 501 | u64 |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 502 | session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep, |
| 503 | u8 use_rules) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 504 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 505 | session_rules_table_t *srt; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 506 | session_table_t *st; |
Florin Coras | f0c1c96 | 2017-11-02 21:31:46 -0700 | [diff] [blame] | 507 | u32 ai; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 508 | int rv; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 509 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 510 | st = session_table_get (table_index); |
| 511 | if (!st) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 512 | return SESSION_INVALID_HANDLE; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 513 | if (sep->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 514 | { |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 515 | session_kv4_t kv4; |
| 516 | ip4_address_t lcl4; |
| 517 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 518 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 519 | sep->transport_proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 520 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 521 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 522 | return kv4.value; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 523 | if (use_rules) |
| 524 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 525 | clib_memset (&lcl4, 0, sizeof (lcl4)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 526 | srt = &st->session_rules[sep->transport_proto]; |
| 527 | ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0, |
| 528 | sep->port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 529 | if (session_lookup_action_index_is_valid (ai)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 530 | return session_lookup_action_to_handle (ai); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 531 | } |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 532 | } |
| 533 | else |
| 534 | { |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 535 | session_kv6_t kv6; |
| 536 | ip6_address_t lcl6; |
| 537 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 538 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 539 | sep->transport_proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 540 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 541 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 542 | return kv6.value; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 543 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 544 | if (use_rules) |
| 545 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 546 | clib_memset (&lcl6, 0, sizeof (lcl6)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 547 | srt = &st->session_rules[sep->transport_proto]; |
| 548 | ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0, |
| 549 | sep->port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 550 | if (session_lookup_action_index_is_valid (ai)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 551 | return session_lookup_action_to_handle (ai); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 552 | } |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 553 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 554 | return SESSION_INVALID_HANDLE; |
| 555 | } |
| 556 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 557 | /** |
| 558 | * Look up endpoint in local session table |
| 559 | * |
| 560 | * The result, for now, is an application index and it may in the future |
| 561 | * be extended to a more complicated "action object". The only action we |
| 562 | * emulate now is "drop" and for that we return a special app index. |
| 563 | * |
| 564 | * Lookup logic is to check in order: |
| 565 | * - the rules in the table (connect acls) |
| 566 | * - session sub-table for a listener |
| 567 | * - session sub-table for a local listener (zeroed addr) |
| 568 | * |
| 569 | * @param table_index table where the lookup should be done |
| 570 | * @param sep session endpoint to be looked up |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 571 | * @return session handle that can be interpreted as an adjacency |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 572 | */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 573 | u64 |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 574 | session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep) |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 575 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 576 | session_rules_table_t *srt; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 577 | session_table_t *st; |
Florin Coras | f0c1c96 | 2017-11-02 21:31:46 -0700 | [diff] [blame] | 578 | u32 ai; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 579 | int rv; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 580 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 581 | st = session_table_get (table_index); |
| 582 | if (!st) |
| 583 | return SESSION_INVALID_INDEX; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 584 | ASSERT (st->is_local); |
| 585 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 586 | if (sep->is_ip4) |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 587 | { |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 588 | session_kv4_t kv4; |
| 589 | ip4_address_t lcl4; |
| 590 | |
| 591 | /* |
| 592 | * Check if endpoint has special rules associated |
| 593 | */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 594 | clib_memset (&lcl4, 0, sizeof (lcl4)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 595 | srt = &st->session_rules[sep->transport_proto]; |
| 596 | ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0, |
| 597 | sep->port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 598 | if (session_lookup_action_index_is_valid (ai)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 599 | return session_lookup_action_to_handle (ai); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 600 | |
| 601 | /* |
| 602 | * Check if session endpoint is a listener |
| 603 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 604 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 605 | sep->transport_proto); |
| 606 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 607 | if (rv == 0) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 608 | return kv4.value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 609 | |
| 610 | /* |
| 611 | * Zero out the ip. Logic is that connect to local ips, say |
| 612 | * 127.0.0.1:port, can match 0.0.0.0:port |
| 613 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 614 | if (ip4_is_local_host (&sep->ip.ip4)) |
| 615 | { |
| 616 | kv4.key[0] = 0; |
| 617 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 618 | if (rv == 0) |
| 619 | return kv4.value; |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | kv4.key[0] = 0; |
| 624 | } |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 625 | |
| 626 | /* |
| 627 | * Zero out the port and check if we have proxy |
| 628 | */ |
| 629 | kv4.key[1] = 0; |
| 630 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 631 | if (rv == 0) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 632 | return kv4.value; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 633 | } |
| 634 | else |
| 635 | { |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 636 | session_kv6_t kv6; |
| 637 | ip6_address_t lcl6; |
| 638 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 639 | clib_memset (&lcl6, 0, sizeof (lcl6)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 640 | srt = &st->session_rules[sep->transport_proto]; |
| 641 | ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0, |
| 642 | sep->port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 643 | if (session_lookup_action_index_is_valid (ai)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 644 | return session_lookup_action_to_handle (ai); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 645 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 646 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 647 | sep->transport_proto); |
| 648 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 649 | if (rv == 0) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 650 | return kv6.value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 651 | |
| 652 | /* |
| 653 | * Zero out the ip. Same logic as above. |
| 654 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 655 | |
| 656 | if (ip6_is_local_host (&sep->ip.ip6)) |
| 657 | { |
| 658 | kv6.key[0] = kv6.key[1] = 0; |
| 659 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 660 | if (rv == 0) |
| 661 | return kv6.value; |
| 662 | } |
| 663 | else |
| 664 | { |
| 665 | kv6.key[0] = kv6.key[1] = 0; |
| 666 | } |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * Zero out the port. Same logic as above. |
| 670 | */ |
| 671 | kv6.key[4] = kv6.key[5] = 0; |
| 672 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 673 | if (rv == 0) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 674 | return kv6.value; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 675 | } |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 676 | return SESSION_INVALID_HANDLE; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 679 | static inline session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 680 | session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl, |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 681 | u16 lcl_port, u8 proto, u8 use_wildcard) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 682 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 683 | session_kv4_t kv4; |
| 684 | int rv; |
| 685 | |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 686 | /* |
| 687 | * First, try a fully formed listener |
| 688 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 689 | make_v4_listener_kv (&kv4, lcl, lcl_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 690 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 691 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 692 | return listen_session_get ((u32) kv4.value); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 693 | |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 694 | /* |
| 695 | * Zero out the lcl ip and check if any 0/0 port binds have been done |
| 696 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 697 | if (use_wildcard) |
| 698 | { |
| 699 | kv4.key[0] = 0; |
| 700 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 701 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 702 | return listen_session_get ((u32) kv4.value); |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 703 | } |
| 704 | else |
| 705 | { |
| 706 | kv4.key[0] = 0; |
| 707 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 708 | |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 709 | /* |
| 710 | * Zero out port and check if we have a proxy set up for our ip |
| 711 | */ |
| 712 | make_v4_proxy_kv (&kv4, lcl, proto); |
| 713 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 714 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 715 | return listen_session_get ((u32) kv4.value); |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 716 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 720 | session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 721 | session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port, |
Yu Ping | b092b77 | 2019-12-27 04:04:33 +0800 | [diff] [blame] | 722 | u8 proto, u8 use_wildcard) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 723 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 724 | session_table_t *st; |
| 725 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 726 | if (!st) |
| 727 | return 0; |
Yu Ping | b092b77 | 2019-12-27 04:04:33 +0800 | [diff] [blame] | 728 | return session_lookup_listener4_i (st, lcl, lcl_port, proto, use_wildcard); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 731 | static session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 732 | session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl, |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 733 | u16 lcl_port, u8 proto, u8 ip_wildcard) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 734 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 735 | session_kv6_t kv6; |
| 736 | int rv; |
| 737 | |
| 738 | make_v6_listener_kv (&kv6, lcl, lcl_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 739 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 740 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 741 | return listen_session_get ((u32) kv6.value); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 742 | |
| 743 | /* Zero out the lcl ip */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 744 | if (ip_wildcard) |
| 745 | { |
| 746 | kv6.key[0] = kv6.key[1] = 0; |
| 747 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 748 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 749 | return listen_session_get ((u32) kv6.value); |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 750 | } |
| 751 | else |
| 752 | { |
| 753 | kv6.key[0] = kv6.key[1] = 0; |
| 754 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 755 | |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 756 | make_v6_proxy_kv (&kv6, lcl, proto); |
| 757 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 758 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 759 | return listen_session_get ((u32) kv6.value); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 763 | session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 764 | session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port, |
Yu Ping | b092b77 | 2019-12-27 04:04:33 +0800 | [diff] [blame] | 765 | u8 proto, u8 use_wildcard) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 766 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 767 | session_table_t *st; |
| 768 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 769 | if (!st) |
| 770 | return 0; |
Yu Ping | b092b77 | 2019-12-27 04:04:33 +0800 | [diff] [blame] | 771 | return session_lookup_listener6_i (st, lcl, lcl_port, proto, use_wildcard); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 774 | /** |
| 775 | * Lookup listener, exact or proxy (inaddr_any:0) match |
| 776 | */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 777 | session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 778 | session_lookup_listener (u32 table_index, session_endpoint_t * sep) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 779 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 780 | session_table_t *st; |
| 781 | st = session_table_get (table_index); |
| 782 | if (!st) |
| 783 | return 0; |
| 784 | if (sep->is_ip4) |
| 785 | return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port, |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 786 | sep->transport_proto, 0); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 787 | else |
| 788 | return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port, |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 789 | sep->transport_proto, 0); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | |
Florin Coras | 9f1a543 | 2019-03-10 21:03:20 -0700 | [diff] [blame] | 793 | /** |
| 794 | * Lookup listener wildcard match |
| 795 | */ |
| 796 | session_t * |
| 797 | session_lookup_listener_wildcard (u32 table_index, session_endpoint_t * sep) |
| 798 | { |
| 799 | session_table_t *st; |
| 800 | st = session_table_get (table_index); |
| 801 | if (!st) |
| 802 | return 0; |
| 803 | if (sep->is_ip4) |
| 804 | return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port, |
| 805 | sep->transport_proto, |
| 806 | 1 /* use_wildcard */ ); |
| 807 | else |
| 808 | return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port, |
| 809 | sep->transport_proto, |
| 810 | 1 /* use_wildcard */ ); |
| 811 | return 0; |
| 812 | } |
| 813 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 814 | int |
| 815 | session_lookup_add_half_open (transport_connection_t * tc, u64 value) |
| 816 | { |
| 817 | session_table_t *st; |
| 818 | session_kv4_t kv4; |
| 819 | session_kv6_t kv6; |
| 820 | |
| 821 | st = session_table_get_or_alloc_for_connection (tc); |
| 822 | if (!st) |
| 823 | return 0; |
| 824 | if (tc->is_ip4) |
| 825 | { |
| 826 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 827 | kv4.value = value; |
| 828 | return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4, |
| 829 | 1 /* is_add */ ); |
| 830 | } |
| 831 | else |
| 832 | { |
| 833 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 834 | kv6.value = value; |
| 835 | return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6, |
| 836 | 1 /* is_add */ ); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | int |
| 841 | session_lookup_del_half_open (transport_connection_t * tc) |
| 842 | { |
| 843 | session_table_t *st; |
| 844 | session_kv4_t kv4; |
| 845 | session_kv6_t kv6; |
| 846 | |
| 847 | st = session_table_get_for_connection (tc); |
| 848 | if (!st) |
| 849 | return -1; |
| 850 | if (tc->is_ip4) |
| 851 | { |
| 852 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 853 | return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4, |
| 854 | 0 /* is_add */ ); |
| 855 | } |
| 856 | else |
| 857 | { |
| 858 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 859 | return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6, |
| 860 | 0 /* is_add */ ); |
| 861 | } |
| 862 | } |
| 863 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 864 | u64 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 865 | session_lookup_half_open_handle (transport_connection_t * tc) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 866 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 867 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 868 | session_kv4_t kv4; |
| 869 | session_kv6_t kv6; |
| 870 | int rv; |
| 871 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 872 | st = session_table_get_for_fib_index (transport_connection_fib_proto (tc), |
| 873 | tc->fib_index); |
| 874 | if (!st) |
| 875 | return HALF_OPEN_LOOKUP_INVALID_VALUE; |
| 876 | if (tc->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 877 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 878 | 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] | 879 | tc->rmt_port, tc->proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 880 | 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] | 881 | if (rv == 0) |
| 882 | return kv4.value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 883 | } |
| 884 | else |
| 885 | { |
| 886 | 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] | 887 | tc->rmt_port, tc->proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 888 | 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] | 889 | if (rv == 0) |
| 890 | return kv6.value; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 891 | } |
| 892 | return HALF_OPEN_LOOKUP_INVALID_VALUE; |
| 893 | } |
| 894 | |
| 895 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 896 | session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 897 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 898 | if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 899 | { |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 900 | u32 sst = session_type_from_proto_and_ip (proto, is_ip4); |
| 901 | return transport_get_half_open (sst, handle & 0xFFFFFFFF); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 902 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 903 | return 0; |
| 904 | } |
| 905 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 906 | /** |
| 907 | * Lookup connection with ip4 and transport layer information |
| 908 | * |
| 909 | * This is used on the fast path so it needs to be fast. Thereby, |
| 910 | * duplication of code and 'hacks' allowed. |
| 911 | * |
| 912 | * The lookup is incremental and returns whenever something is matched. The |
| 913 | * steps are: |
| 914 | * - Try to find an established session |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 915 | * - Try to find a half-open connection |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 916 | * - Try session rules table |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 917 | * - Try to find a fully-formed or local source wildcarded (listener bound to |
| 918 | * all interfaces) listener session |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 919 | * - return 0 |
| 920 | * |
| 921 | * @param fib_index index of fib wherein the connection was received |
| 922 | * @param lcl local ip4 address |
| 923 | * @param rmt remote ip4 address |
| 924 | * @param lcl_port local port |
| 925 | * @param rmt_port remote port |
| 926 | * @param proto transport protocol (e.g., tcp, udp) |
| 927 | * @param thread_index thread index for request |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 928 | * @param is_filtered return flag that indicates if connection was filtered. |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 929 | * |
| 930 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 931 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 932 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 933 | session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl, |
| 934 | ip4_address_t * rmt, u16 lcl_port, |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 935 | u16 rmt_port, u8 proto, u32 thread_index, |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 936 | u8 * result) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 937 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 938 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 939 | session_kv4_t kv4; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 940 | session_t *s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 941 | u32 action_index; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 942 | int rv; |
| 943 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 944 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 945 | if (PREDICT_FALSE (!st)) |
| 946 | return 0; |
| 947 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 948 | /* |
| 949 | * Lookup session amongst established ones |
| 950 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 951 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 952 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 953 | if (rv == 0) |
| 954 | { |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 955 | if (PREDICT_FALSE ((u32) (kv4.value >> 32) != thread_index)) |
| 956 | { |
| 957 | *result = SESSION_LOOKUP_RESULT_WRONG_THREAD; |
| 958 | return 0; |
| 959 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 960 | s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 961 | return transport_get_connection (proto, s->connection_index, |
| 962 | thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 965 | /* |
| 966 | * Try half-open connections |
| 967 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 968 | 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] | 969 | if (rv == 0) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 970 | return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 971 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 972 | /* |
| 973 | * Check the session rules table |
| 974 | */ |
| 975 | action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl, |
| 976 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 977 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 978 | { |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 979 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 980 | { |
| 981 | *result = SESSION_LOOKUP_RESULT_FILTERED; |
| 982 | return 0; |
| 983 | } |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 984 | if ((s = session_lookup_action_to_session (action_index, |
| 985 | FIB_PROTOCOL_IP4, proto))) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 986 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 987 | return 0; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /* |
| 991 | * If nothing is found, check if any listener is available |
| 992 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 993 | s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 994 | if (s) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 995 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 996 | |
| 997 | return 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1000 | /** |
| 1001 | * Lookup connection with ip4 and transport layer information |
| 1002 | * |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 1003 | * Not optimized. Lookup logic is identical to that of |
| 1004 | * @ref session_lookup_connection_wt4 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1005 | * |
| 1006 | * @param fib_index index of the fib wherein the connection was received |
| 1007 | * @param lcl local ip4 address |
| 1008 | * @param rmt remote ip4 address |
| 1009 | * @param lcl_port local port |
| 1010 | * @param rmt_port remote port |
| 1011 | * @param proto transport protocol (e.g., tcp, udp) |
| 1012 | * |
| 1013 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 1014 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1015 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1016 | session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl, |
| 1017 | ip4_address_t * rmt, u16 lcl_port, u16 rmt_port, |
| 1018 | u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1019 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1020 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1021 | session_kv4_t kv4; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1022 | session_t *s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1023 | u32 action_index; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1024 | int rv; |
| 1025 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1026 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 1027 | if (PREDICT_FALSE (!st)) |
| 1028 | return 0; |
| 1029 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1030 | /* |
| 1031 | * Lookup session amongst established ones |
| 1032 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1033 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1034 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1035 | if (rv == 0) |
| 1036 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1037 | s = session_get_from_handle (kv4.value); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1038 | return transport_get_connection (proto, s->connection_index, |
| 1039 | s->thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1042 | /* |
| 1043 | * Try half-open connections |
| 1044 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1045 | 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] | 1046 | if (rv == 0) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1047 | return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1048 | |
| 1049 | /* |
| 1050 | * Check the session rules table |
| 1051 | */ |
| 1052 | action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl, |
| 1053 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1054 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1055 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1056 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1057 | return 0; |
| 1058 | if ((s = session_lookup_action_to_session (action_index, |
| 1059 | FIB_PROTOCOL_IP4, proto))) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1060 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1061 | return 0; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | * If nothing is found, check if any listener is available |
| 1066 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1067 | s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1068 | if (s) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1069 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1070 | |
| 1071 | return 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1074 | /** |
| 1075 | * Lookup session with ip4 and transport layer information |
| 1076 | * |
Florin Coras | 6bd8d3f | 2022-03-14 21:17:25 -0700 | [diff] [blame] | 1077 | * Important note: this may look into another thread's pool table |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1078 | * |
| 1079 | * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but |
| 1080 | * this returns a session as opposed to a transport connection and it does not |
| 1081 | * try to lookup half-open sessions. |
| 1082 | * |
| 1083 | * Typically used by dgram connections |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1084 | */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1085 | session_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1086 | session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt, |
| 1087 | u16 lcl_port, u16 rmt_port, u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1088 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1089 | session_table_t *st; |
| 1090 | session_kv4_t kv4; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1091 | session_t *s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1092 | u32 action_index; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1093 | int rv; |
| 1094 | |
| 1095 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 1096 | if (PREDICT_FALSE (!st)) |
| 1097 | return 0; |
| 1098 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1099 | /* |
| 1100 | * Lookup session amongst established ones |
| 1101 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1102 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
| 1103 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 1104 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1105 | return session_get_from_handle_safe (kv4.value); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1106 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1107 | /* |
| 1108 | * Check the session rules table |
| 1109 | */ |
| 1110 | action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl, |
| 1111 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1112 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1113 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1114 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1115 | return 0; |
| 1116 | return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4, |
| 1117 | proto); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | * If nothing is found, check if any listener is available |
| 1122 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1123 | if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1124 | return s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1125 | |
| 1126 | return 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * Lookup connection with ip6 and transport layer information |
| 1131 | * |
| 1132 | * This is used on the fast path so it needs to be fast. Thereby, |
| 1133 | * duplication of code and 'hacks' allowed. |
| 1134 | * |
| 1135 | * The lookup is incremental and returns whenever something is matched. The |
| 1136 | * steps are: |
| 1137 | * - Try to find an established session |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1138 | * - Try to find a half-open connection |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1139 | * - Try session rules table |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1140 | * - Try to find a fully-formed or local source wildcarded (listener bound to |
| 1141 | * all interfaces) listener session |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1142 | * - return 0 |
| 1143 | * |
| 1144 | * @param fib_index index of the fib wherein the connection was received |
| 1145 | * @param lcl local ip6 address |
| 1146 | * @param rmt remote ip6 address |
| 1147 | * @param lcl_port local port |
| 1148 | * @param rmt_port remote port |
| 1149 | * @param proto transport protocol (e.g., tcp, udp) |
| 1150 | * @param thread_index thread index for request |
| 1151 | * |
| 1152 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 1153 | */ |
| 1154 | transport_connection_t * |
| 1155 | session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl, |
| 1156 | ip6_address_t * rmt, u16 lcl_port, |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1157 | u16 rmt_port, u8 proto, u32 thread_index, |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 1158 | u8 * result) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1159 | { |
| 1160 | session_table_t *st; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1161 | session_t *s; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1162 | session_kv6_t kv6; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1163 | u32 action_index; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1164 | int rv; |
| 1165 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1166 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 1167 | if (PREDICT_FALSE (!st)) |
| 1168 | return 0; |
| 1169 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1170 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1171 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1172 | if (rv == 0) |
| 1173 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1174 | ASSERT ((u32) (kv6.value >> 32) == thread_index); |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 1175 | if (PREDICT_FALSE ((u32) (kv6.value >> 32) != thread_index)) |
| 1176 | { |
| 1177 | *result = SESSION_LOOKUP_RESULT_WRONG_THREAD; |
| 1178 | return 0; |
| 1179 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1180 | s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1181 | return transport_get_connection (proto, s->connection_index, |
| 1182 | thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1185 | /* Try half-open connections */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1186 | 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] | 1187 | if (rv == 0) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1188 | return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1189 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1190 | /* Check the session rules table */ |
| 1191 | action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl, |
| 1192 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1193 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1194 | { |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 1195 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1196 | { |
| 1197 | *result = SESSION_LOOKUP_RESULT_FILTERED; |
| 1198 | return 0; |
| 1199 | } |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1200 | if ((s = session_lookup_action_to_session (action_index, |
| 1201 | FIB_PROTOCOL_IP6, proto))) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1202 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1203 | return 0; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | /* If nothing is found, check if any listener is available */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1207 | s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1208 | if (s) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1209 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1210 | |
| 1211 | return 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1214 | /** |
| 1215 | * Lookup connection with ip6 and transport layer information |
| 1216 | * |
| 1217 | * Not optimized. This is used on the fast path so it needs to be fast. |
| 1218 | * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical |
| 1219 | * to that of @ref session_lookup_connection_wt4 |
| 1220 | * |
| 1221 | * @param fib_index index of the fib wherein the connection was received |
| 1222 | * @param lcl local ip6 address |
| 1223 | * @param rmt remote ip6 address |
| 1224 | * @param lcl_port local port |
| 1225 | * @param rmt_port remote port |
| 1226 | * @param proto transport protocol (e.g., tcp, udp) |
| 1227 | * |
| 1228 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 1229 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1230 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1231 | session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl, |
| 1232 | ip6_address_t * rmt, u16 lcl_port, u16 rmt_port, |
| 1233 | u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1234 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1235 | session_table_t *st; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1236 | session_t *s; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1237 | session_kv6_t kv6; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1238 | u32 action_index; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1239 | int rv; |
| 1240 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1241 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 1242 | if (PREDICT_FALSE (!st)) |
| 1243 | return 0; |
| 1244 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1245 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1246 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1247 | if (rv == 0) |
| 1248 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1249 | s = session_get_from_handle (kv6.value); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1250 | return transport_get_connection (proto, s->connection_index, |
| 1251 | s->thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1254 | /* Try half-open connections */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1255 | 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] | 1256 | if (rv == 0) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1257 | return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1258 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1259 | /* Check the session rules table */ |
| 1260 | action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl, |
| 1261 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1262 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1263 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1264 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1265 | return 0; |
| 1266 | if ((s = session_lookup_action_to_session (action_index, |
| 1267 | FIB_PROTOCOL_IP6, proto))) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1268 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1269 | return 0; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /* If nothing is found, check if any listener is available */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1273 | s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1274 | if (s) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1275 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1276 | |
| 1277 | return 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1280 | /** |
| 1281 | * Lookup session with ip6 and transport layer information |
| 1282 | * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1283 | * Important note: this may look into another thread's pool table and |
| 1284 | * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as |
| 1285 | * if needed as soon as possible. |
| 1286 | * |
| 1287 | * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but |
| 1288 | * this returns a session as opposed to a transport connection and it does not |
| 1289 | * try to lookup half-open sessions. |
| 1290 | * |
| 1291 | * Typically used by dgram connections |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1292 | */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1293 | session_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1294 | session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt, |
| 1295 | u16 lcl_port, u16 rmt_port, u8 proto) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1296 | { |
| 1297 | session_table_t *st; |
| 1298 | session_kv6_t kv6; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1299 | session_t *s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1300 | u32 action_index; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1301 | int rv; |
| 1302 | |
| 1303 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 1304 | if (PREDICT_FALSE (!st)) |
| 1305 | return 0; |
| 1306 | |
| 1307 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
| 1308 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 1309 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1310 | return session_get_from_handle_safe (kv6.value); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1311 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1312 | /* Check the session rules table */ |
| 1313 | action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl, |
| 1314 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1315 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1316 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1317 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1318 | return 0; |
| 1319 | return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6, |
| 1320 | proto); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1323 | /* If nothing is found, check if any listener is available */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1324 | if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1325 | return s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1326 | return 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 1329 | transport_connection_t * |
| 1330 | session_lookup_connection (u32 fib_index, ip46_address_t * lcl, |
| 1331 | ip46_address_t * rmt, u16 lcl_port, u16 rmt_port, |
| 1332 | u8 proto, u8 is_ip4) |
| 1333 | { |
| 1334 | if (is_ip4) |
| 1335 | return session_lookup_connection4 (fib_index, &lcl->ip4, &rmt->ip4, |
| 1336 | lcl_port, rmt_port, proto); |
| 1337 | else |
| 1338 | return session_lookup_connection6 (fib_index, &lcl->ip6, &rmt->ip6, |
| 1339 | lcl_port, rmt_port, proto); |
| 1340 | } |
| 1341 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1342 | session_error_t |
| 1343 | vnet_session_rule_add_del (session_rule_add_del_args_t *args) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1344 | { |
| 1345 | app_namespace_t *app_ns = app_namespace_get (args->appns_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1346 | session_rules_table_t *srt; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1347 | session_table_t *st; |
| 1348 | u32 fib_index; |
| 1349 | u8 fib_proto; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1350 | int rv = 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1351 | |
| 1352 | if (!app_ns) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1353 | return SESSION_E_INVALID_NS; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1354 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1355 | if (args->scope > 3) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1356 | return SESSION_E_INVALID; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1357 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1358 | if (args->transport_proto != TRANSPORT_PROTO_TCP |
| 1359 | && args->transport_proto != TRANSPORT_PROTO_UDP) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1360 | return SESSION_E_INVALID; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1361 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1362 | if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0) |
| 1363 | { |
| 1364 | fib_proto = args->table_args.rmt.fp_proto; |
| 1365 | fib_index = app_namespace_get_fib_index (app_ns, fib_proto); |
| 1366 | st = session_table_get_for_fib_index (fib_proto, fib_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1367 | srt = &st->session_rules[args->transport_proto]; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1368 | if ((rv = session_rules_table_add_del (srt, &args->table_args))) |
| 1369 | return rv; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1370 | } |
| 1371 | if (args->scope & SESSION_RULE_SCOPE_LOCAL) |
| 1372 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1373 | clib_memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1374 | args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto; |
| 1375 | args->table_args.lcl_port = 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1376 | st = app_namespace_get_local_table (app_ns); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1377 | srt = &st->session_rules[args->transport_proto]; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1378 | rv = session_rules_table_add_del (srt, &args->table_args); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1379 | } |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1380 | return rv; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1383 | /** |
| 1384 | * Mark (global) tables as pertaining to app ns |
| 1385 | */ |
| 1386 | void |
| 1387 | session_lookup_set_tables_appns (app_namespace_t * app_ns) |
| 1388 | { |
| 1389 | session_table_t *st; |
| 1390 | u32 fib_index; |
| 1391 | u8 fp; |
| 1392 | |
| 1393 | for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++) |
| 1394 | { |
| 1395 | fib_index = app_namespace_get_fib_index (app_ns, fp); |
Florin Coras | 95cd864 | 2019-12-30 21:53:19 -0800 | [diff] [blame] | 1396 | st = session_table_get_or_alloc (fp, fib_index); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1397 | if (st) |
| 1398 | st->appns_index = app_namespace_index (app_ns); |
| 1399 | } |
| 1400 | } |
| 1401 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1402 | u8 * |
| 1403 | format_ip4_session_lookup_kvp (u8 * s, va_list * args) |
| 1404 | { |
| 1405 | clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1406 | u32 is_local = va_arg (*args, u32); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1407 | v4_connection_key_t *key = (v4_connection_key_t *) kvp->key; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1408 | session_t *session; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1409 | app_worker_t *app_wrk; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 1410 | const u8 *app_name; |
| 1411 | u8 *str = 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1412 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1413 | if (!is_local) |
| 1414 | { |
| 1415 | session = session_get_from_handle (kvp->value); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1416 | app_wrk = app_worker_get (session->app_wrk_index); |
| 1417 | app_name = application_name_from_index (app_wrk->app_index); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1418 | str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short, |
| 1419 | key->proto, format_ip4_address, &key->src, |
| 1420 | clib_net_to_host_u16 (key->src_port), format_ip4_address, |
| 1421 | &key->dst, clib_net_to_host_u16 (key->dst_port)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1422 | s = format (s, "%-40v%-30v", str, app_name); |
| 1423 | } |
| 1424 | else |
| 1425 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1426 | session = session_get_from_handle (kvp->value); |
| 1427 | app_wrk = app_worker_get (session->app_wrk_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1428 | app_name = application_name_from_index (app_wrk->app_index); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1429 | str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto, |
| 1430 | format_ip4_address, &key->src, |
| 1431 | clib_net_to_host_u16 (key->src_port)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1432 | s = format (s, "%-30v%-30v", str, app_name); |
| 1433 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1434 | return s; |
| 1435 | } |
| 1436 | |
| 1437 | typedef struct _ip4_session_table_show_ctx_t |
| 1438 | { |
| 1439 | vlib_main_t *vm; |
| 1440 | u8 is_local; |
| 1441 | } ip4_session_table_show_ctx_t; |
| 1442 | |
| 1443 | static int |
| 1444 | ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg) |
| 1445 | { |
| 1446 | ip4_session_table_show_ctx_t *ctx = arg; |
| 1447 | vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp, |
| 1448 | ctx->is_local); |
| 1449 | return 1; |
| 1450 | } |
| 1451 | |
| 1452 | void |
| 1453 | session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table, |
| 1454 | u8 type, u8 is_local) |
| 1455 | { |
| 1456 | ip4_session_table_show_ctx_t ctx = { |
| 1457 | .vm = vm, |
| 1458 | .is_local = is_local, |
| 1459 | }; |
| 1460 | if (!is_local) |
| 1461 | vlib_cli_output (vm, "%-40s%-30s", "Session", "Application"); |
| 1462 | else |
| 1463 | vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application"); |
| 1464 | switch (type) |
| 1465 | { |
| 1466 | /* main table v4 */ |
| 1467 | case 0: |
| 1468 | ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show, |
| 1469 | &ctx); |
| 1470 | break; |
| 1471 | default: |
| 1472 | clib_warning ("not supported"); |
| 1473 | } |
| 1474 | } |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1475 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1476 | static clib_error_t * |
| 1477 | session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1478 | vlib_cli_command_t * cmd) |
| 1479 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1480 | u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1481 | clib_error_t *error = 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1482 | u32 appns_index, scope = 0; |
| 1483 | ip46_address_t lcl_ip, rmt_ip; |
| 1484 | u8 is_ip4 = 1, conn_set = 0; |
| 1485 | u8 fib_proto, is_add = 1, *ns_id = 0; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1486 | u8 *tag = 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1487 | app_namespace_t *app_ns; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1488 | int rv; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1489 | |
Guanghua Zhang | fcd5e12 | 2019-08-24 10:52:19 +0800 | [diff] [blame] | 1490 | session_cli_return_if_not_enabled (); |
| 1491 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1492 | clib_memset (&lcl_ip, 0, sizeof (lcl_ip)); |
| 1493 | clib_memset (&rmt_ip, 0, sizeof (rmt_ip)); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1494 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1495 | { |
| 1496 | if (unformat (input, "del")) |
| 1497 | is_add = 0; |
| 1498 | else if (unformat (input, "add")) |
| 1499 | ; |
| 1500 | else if (unformat (input, "appns %_%v%_", &ns_id)) |
| 1501 | ; |
| 1502 | else if (unformat (input, "scope global")) |
| 1503 | scope = SESSION_RULE_SCOPE_GLOBAL; |
| 1504 | else if (unformat (input, "scope local")) |
| 1505 | scope = SESSION_RULE_SCOPE_LOCAL; |
| 1506 | else if (unformat (input, "scope all")) |
| 1507 | scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL; |
| 1508 | else if (unformat (input, "proto %U", unformat_transport_proto, &proto)) |
| 1509 | ; |
| 1510 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address, |
| 1511 | &lcl_ip.ip4, &lcl_plen, &lcl_port, |
| 1512 | unformat_ip4_address, &rmt_ip.ip4, &rmt_plen, |
| 1513 | &rmt_port)) |
| 1514 | { |
| 1515 | is_ip4 = 1; |
| 1516 | conn_set = 1; |
| 1517 | } |
| 1518 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address, |
| 1519 | &lcl_ip.ip6, &lcl_plen, &lcl_port, |
| 1520 | unformat_ip6_address, &rmt_ip.ip6, &rmt_plen, |
| 1521 | &rmt_port)) |
| 1522 | { |
| 1523 | is_ip4 = 0; |
| 1524 | conn_set = 1; |
| 1525 | } |
| 1526 | else if (unformat (input, "action %d", &action)) |
| 1527 | ; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1528 | else if (unformat (input, "tag %_%v%_", &tag)) |
| 1529 | ; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1530 | else |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1531 | { |
| 1532 | error = clib_error_return (0, "unknown input `%U'", |
| 1533 | format_unformat_error, input); |
| 1534 | goto done; |
| 1535 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1538 | if (proto == ~0) |
| 1539 | { |
| 1540 | vlib_cli_output (vm, "proto must be set"); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1541 | goto done; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1542 | } |
| 1543 | if (is_add && !conn_set && action == ~0) |
| 1544 | { |
| 1545 | vlib_cli_output (vm, "connection and action must be set for add"); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1546 | goto done; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1547 | } |
| 1548 | if (!is_add && !tag && !conn_set) |
| 1549 | { |
| 1550 | vlib_cli_output (vm, "connection or tag must be set for delete"); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1551 | goto done; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1552 | } |
| 1553 | if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN) |
| 1554 | { |
| 1555 | vlib_cli_output (vm, "tag too long (max u64)"); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1556 | goto done; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1557 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1558 | |
| 1559 | if (ns_id) |
| 1560 | { |
| 1561 | app_ns = app_namespace_get_from_id (ns_id); |
| 1562 | if (!app_ns) |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1563 | { |
| 1564 | vlib_cli_output (vm, "namespace %v does not exist", ns_id); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1565 | goto done; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1566 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1567 | } |
| 1568 | else |
| 1569 | { |
| 1570 | app_ns = app_namespace_get_default (); |
| 1571 | } |
| 1572 | appns_index = app_namespace_index (app_ns); |
| 1573 | |
| 1574 | fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 1575 | session_rule_add_del_args_t args = { |
zhanglimao | a04ebb8 | 2019-03-13 10:36:54 +0800 | [diff] [blame] | 1576 | .transport_proto = proto, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1577 | .table_args.lcl.fp_addr = lcl_ip, |
| 1578 | .table_args.lcl.fp_len = lcl_plen, |
| 1579 | .table_args.lcl.fp_proto = fib_proto, |
| 1580 | .table_args.rmt.fp_addr = rmt_ip, |
| 1581 | .table_args.rmt.fp_len = rmt_plen, |
| 1582 | .table_args.rmt.fp_proto = fib_proto, |
| 1583 | .table_args.lcl_port = lcl_port, |
| 1584 | .table_args.rmt_port = rmt_port, |
| 1585 | .table_args.action_index = action, |
| 1586 | .table_args.is_add = is_add, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1587 | .table_args.tag = tag, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1588 | .appns_index = appns_index, |
| 1589 | .scope = scope, |
| 1590 | }; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1591 | if ((rv = vnet_session_rule_add_del (&args))) |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1592 | error = clib_error_return (0, "rule add del returned %u", rv); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1593 | |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1594 | done: |
| 1595 | vec_free (ns_id); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1596 | vec_free (tag); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 1597 | return error; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | /* *INDENT-OFF* */ |
| 1601 | VLIB_CLI_COMMAND (session_rule_command, static) = |
| 1602 | { |
| 1603 | .path = "session rule", |
| 1604 | .short_help = "session rule [add|del] appns <ns_id> proto <proto> " |
| 1605 | "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>", |
| 1606 | .function = session_rule_command_fn, |
| 1607 | }; |
| 1608 | /* *INDENT-ON* */ |
| 1609 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1610 | void |
| 1611 | session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto, |
| 1612 | u8 transport_proto) |
| 1613 | { |
| 1614 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1615 | session_rules_table_t *srt; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1616 | session_table_t *st; |
| 1617 | st = session_table_get_for_fib_index (fib_index, fib_proto); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1618 | srt = &st->session_rules[transport_proto]; |
| 1619 | session_rules_table_cli_dump (vm, srt, fib_proto); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | void |
| 1623 | session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto, |
| 1624 | u8 transport_proto) |
| 1625 | { |
| 1626 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1627 | session_rules_table_t *srt; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1628 | session_table_t *st; |
| 1629 | st = session_table_get (table_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1630 | srt = &st->session_rules[transport_proto]; |
| 1631 | session_rules_table_cli_dump (vm, srt, fib_proto); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1634 | static clib_error_t * |
| 1635 | show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1636 | vlib_cli_command_t * cmd) |
| 1637 | { |
| 1638 | u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen; |
| 1639 | u32 fib_index, scope = 0; |
| 1640 | ip46_address_t lcl_ip, rmt_ip; |
| 1641 | u8 is_ip4 = 1, show_one = 0; |
| 1642 | app_namespace_t *app_ns; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1643 | session_rules_table_t *srt; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1644 | session_table_t *st; |
| 1645 | u8 *ns_id = 0, fib_proto; |
| 1646 | |
Guanghua Zhang | fcd5e12 | 2019-08-24 10:52:19 +0800 | [diff] [blame] | 1647 | session_cli_return_if_not_enabled (); |
| 1648 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1649 | clib_memset (&lcl_ip, 0, sizeof (lcl_ip)); |
| 1650 | clib_memset (&rmt_ip, 0, sizeof (rmt_ip)); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1651 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1652 | { |
| 1653 | if (unformat (input, "%U", unformat_transport_proto, &transport_proto)) |
| 1654 | ; |
| 1655 | else if (unformat (input, "appns %_%v%_", &ns_id)) |
| 1656 | ; |
| 1657 | else if (unformat (input, "scope global")) |
| 1658 | scope = 1; |
| 1659 | else if (unformat (input, "scope local")) |
| 1660 | scope = 2; |
| 1661 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address, |
| 1662 | &lcl_ip.ip4, &lcl_plen, &lcl_port, |
| 1663 | unformat_ip4_address, &rmt_ip.ip4, &rmt_plen, |
| 1664 | &rmt_port)) |
| 1665 | { |
| 1666 | is_ip4 = 1; |
| 1667 | show_one = 1; |
| 1668 | } |
| 1669 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address, |
| 1670 | &lcl_ip.ip6, &lcl_plen, &lcl_port, |
| 1671 | unformat_ip6_address, &rmt_ip.ip6, &rmt_plen, |
| 1672 | &rmt_port)) |
| 1673 | { |
| 1674 | is_ip4 = 0; |
| 1675 | show_one = 1; |
| 1676 | } |
| 1677 | else |
| 1678 | return clib_error_return (0, "unknown input `%U'", |
| 1679 | format_unformat_error, input); |
| 1680 | } |
| 1681 | |
| 1682 | if (transport_proto == ~0) |
| 1683 | { |
| 1684 | vlib_cli_output (vm, "transport proto must be set"); |
| 1685 | return 0; |
| 1686 | } |
| 1687 | |
| 1688 | if (ns_id) |
| 1689 | { |
| 1690 | app_ns = app_namespace_get_from_id (ns_id); |
| 1691 | if (!app_ns) |
| 1692 | { |
| 1693 | vlib_cli_output (vm, "appns %v doesn't exist", ns_id); |
| 1694 | return 0; |
| 1695 | } |
| 1696 | } |
| 1697 | else |
| 1698 | { |
| 1699 | app_ns = app_namespace_get_default (); |
| 1700 | } |
| 1701 | |
| 1702 | if (scope == 1 || scope == 0) |
| 1703 | { |
| 1704 | fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 1705 | fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 1706 | st = session_table_get_for_fib_index (fib_proto, fib_index); |
| 1707 | } |
| 1708 | else |
| 1709 | { |
| 1710 | st = app_namespace_get_local_table (app_ns); |
| 1711 | } |
| 1712 | |
| 1713 | if (show_one) |
| 1714 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1715 | srt = &st->session_rules[transport_proto]; |
| 1716 | session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip, |
| 1717 | rmt_port, is_ip4); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1718 | return 0; |
| 1719 | } |
| 1720 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1721 | vlib_cli_output (vm, "%U rules table", format_transport_proto, |
| 1722 | transport_proto); |
| 1723 | srt = &st->session_rules[transport_proto]; |
| 1724 | session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4); |
| 1725 | session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1726 | |
| 1727 | vec_free (ns_id); |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | /* *INDENT-OFF* */ |
| 1732 | VLIB_CLI_COMMAND (show_session_rules_command, static) = |
| 1733 | { |
| 1734 | .path = "show session rules", |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1735 | .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> " |
| 1736 | "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]", |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1737 | .function = show_session_rules_command_fn, |
| 1738 | }; |
| 1739 | /* *INDENT-ON* */ |
| 1740 | |
Florin Coras | 894d0a6 | 2023-11-17 16:35:04 -0800 | [diff] [blame] | 1741 | u8 * |
| 1742 | format_session_lookup_tables (u8 *s, va_list *args) |
| 1743 | { |
| 1744 | u32 fib_proto = va_arg (*args, u32); |
| 1745 | u32 *fibs, num_fibs = 0, fib_index, indent; |
| 1746 | session_table_t *st; |
| 1747 | u64 total_mem = 0; |
| 1748 | |
| 1749 | fibs = fib_index_to_table_index[fib_proto]; |
| 1750 | |
| 1751 | for (fib_index = 0; fib_index < vec_len (fibs); fib_index++) |
| 1752 | { |
| 1753 | if (fibs[fib_index] == ~0) |
| 1754 | continue; |
| 1755 | |
| 1756 | num_fibs += 1; |
| 1757 | st = session_table_get (fibs[fib_index]); |
| 1758 | total_mem += session_table_memory_size (st); |
| 1759 | } |
| 1760 | |
| 1761 | indent = format_get_indent (s); |
| 1762 | s = format (s, "active fibs:\t%u\n", num_fibs); |
| 1763 | s = format (s, "%Umax fib-index:\t%u\n", format_white_space, indent, |
| 1764 | vec_len (fibs) - 1); |
| 1765 | s = format (s, "%Utable memory:\t%U\n", format_white_space, indent, |
| 1766 | format_memory_size, total_mem); |
| 1767 | s = format (s, "%Uvec memory:\t%U\n", format_white_space, indent, |
| 1768 | format_memory_size, vec_mem_size (fibs)); |
| 1769 | |
| 1770 | return s; |
| 1771 | } |
| 1772 | |
| 1773 | static clib_error_t * |
| 1774 | show_session_lookup_command_fn (vlib_main_t *vm, unformat_input_t *input, |
| 1775 | vlib_cli_command_t *cmd) |
| 1776 | { |
| 1777 | session_table_t *st; |
| 1778 | u32 fib_index = ~0; |
| 1779 | |
| 1780 | session_cli_return_if_not_enabled (); |
| 1781 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1782 | { |
| 1783 | if (unformat (input, "table %u", &fib_index)) |
| 1784 | ; |
| 1785 | else |
| 1786 | return clib_error_return (0, "unknown input `%U'", |
| 1787 | format_unformat_error, input); |
| 1788 | } |
| 1789 | |
| 1790 | if (fib_index != ~0) |
| 1791 | { |
| 1792 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 1793 | if (st) |
| 1794 | vlib_cli_output (vm, "%U", format_session_table, st); |
| 1795 | else |
| 1796 | vlib_cli_output (vm, "no ip4 table for fib-index %u", fib_index); |
| 1797 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 1798 | if (st) |
| 1799 | vlib_cli_output (vm, "%U", format_session_table, st); |
| 1800 | else |
| 1801 | vlib_cli_output (vm, "no ip6 table for fib-index %u", fib_index); |
| 1802 | goto done; |
| 1803 | } |
| 1804 | |
| 1805 | vlib_cli_output (vm, "ip4 fib lookup tables:\n %U", |
| 1806 | format_session_lookup_tables, FIB_PROTOCOL_IP4); |
| 1807 | vlib_cli_output (vm, "ip6 fib lookup tables:\n %U", |
| 1808 | format_session_lookup_tables, FIB_PROTOCOL_IP6); |
| 1809 | |
| 1810 | done: |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
| 1814 | VLIB_CLI_COMMAND (show_session_lookup_command, static) = { |
| 1815 | .path = "show session lookup", |
| 1816 | .short_help = "show session lookup [table <fib-index>]", |
| 1817 | .function = show_session_lookup_command_fn, |
| 1818 | }; |
| 1819 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1820 | void |
| 1821 | session_lookup_init (void) |
| 1822 | { |
Florin Coras | f9bdc03 | 2024-02-02 13:01:07 -0800 | [diff] [blame] | 1823 | session_lookup_main_t *slm = &sl_main; |
| 1824 | |
| 1825 | clib_spinlock_init (&slm->st_alloc_lock); |
| 1826 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1827 | /* |
| 1828 | * Allocate default table and map it to fib_index 0 |
| 1829 | */ |
| 1830 | session_table_t *st = session_table_alloc (); |
| 1831 | vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0); |
| 1832 | fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1833 | st->active_fib_proto = FIB_PROTOCOL_IP4; |
| 1834 | session_table_init (st, FIB_PROTOCOL_IP4); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1835 | st = session_table_alloc (); |
| 1836 | vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0); |
| 1837 | fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1838 | st->active_fib_proto = FIB_PROTOCOL_IP6; |
| 1839 | session_table_init (st, FIB_PROTOCOL_IP6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | /* |
| 1843 | * fd.io coding-style-patch-verification: ON |
| 1844 | * |
| 1845 | * Local Variables: |
| 1846 | * eval: (c-set-style "gnu") |
| 1847 | * End: |
| 1848 | */ |