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