Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * @file |
| 18 | * @brief IPv6 Shallow Virtual Reassembly. |
| 19 | * |
| 20 | * This file contains the source code for IPv6 Shallow Virtual reassembly. |
| 21 | */ |
| 22 | |
| 23 | #include <vppinfra/vec.h> |
| 24 | #include <vnet/vnet.h> |
| 25 | #include <vnet/ip/ip.h> |
| 26 | #include <vnet/ip/ip6_to_ip4.h> |
| 27 | #include <vppinfra/bihash_48_8.h> |
| 28 | #include <vnet/ip/reass/ip6_sv_reass.h> |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 29 | #include <vnet/ip/ip6_inlines.h> |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 30 | |
| 31 | #define MSEC_PER_SEC 1000 |
| 32 | #define IP6_SV_REASS_TIMEOUT_DEFAULT_MS 100 |
| 33 | #define IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default |
| 34 | #define IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT 1024 |
| 35 | #define IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3 |
| 36 | #define IP6_SV_REASS_HT_LOAD_FACTOR (0.75) |
| 37 | |
| 38 | typedef enum |
| 39 | { |
| 40 | IP6_SV_REASS_RC_OK, |
| 41 | IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS, |
| 42 | IP6_SV_REASS_RC_INTERNAL_ERROR, |
| 43 | IP6_SV_REASS_RC_UNSUPP_IP_PROTO, |
Klement Sekera | 755042d | 2021-12-01 10:14:38 +0000 | [diff] [blame] | 44 | IP6_SV_REASS_RC_INVALID_FRAG_LEN, |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 45 | } ip6_sv_reass_rc_t; |
| 46 | |
| 47 | typedef struct |
| 48 | { |
| 49 | union |
| 50 | { |
| 51 | struct |
| 52 | { |
| 53 | ip6_address_t src; |
| 54 | ip6_address_t dst; |
Klement Sekera | 5a58e7c | 2021-11-03 17:57:28 +0100 | [diff] [blame] | 55 | u32 fib_index; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 56 | u32 frag_id; |
| 57 | u8 unused[7]; |
| 58 | u8 proto; |
| 59 | }; |
| 60 | u64 as_u64[6]; |
| 61 | }; |
| 62 | } ip6_sv_reass_key_t; |
| 63 | |
| 64 | typedef union |
| 65 | { |
| 66 | struct |
| 67 | { |
| 68 | u32 reass_index; |
| 69 | u32 thread_index; |
| 70 | }; |
| 71 | u64 as_u64; |
| 72 | } ip6_sv_reass_val_t; |
| 73 | |
| 74 | typedef union |
| 75 | { |
| 76 | struct |
| 77 | { |
| 78 | ip6_sv_reass_key_t k; |
| 79 | ip6_sv_reass_val_t v; |
| 80 | }; |
| 81 | clib_bihash_kv_48_8_t kv; |
| 82 | } ip6_sv_reass_kv_t; |
| 83 | |
| 84 | typedef struct |
| 85 | { |
| 86 | // hash table key |
| 87 | ip6_sv_reass_key_t key; |
| 88 | // time when last packet was received |
| 89 | f64 last_heard; |
| 90 | // internal id of this reassembly |
| 91 | u64 id; |
| 92 | // trace operation counter |
| 93 | u32 trace_op_counter; |
| 94 | // buffer indexes of buffers in this reassembly in chronological order - |
| 95 | // including overlaps and duplicate fragments |
| 96 | u32 *cached_buffers; |
| 97 | // set to true when this reassembly is completed |
| 98 | bool is_complete; |
| 99 | // ip protocol |
| 100 | u8 ip_proto; |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 101 | u8 icmp_type_or_tcp_flags; |
| 102 | u32 tcp_ack_number; |
| 103 | u32 tcp_seq_number; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 104 | // l4 src port |
| 105 | u16 l4_src_port; |
| 106 | // l4 dst port |
| 107 | u16 l4_dst_port; |
| 108 | // lru indexes |
| 109 | u32 lru_prev; |
| 110 | u32 lru_next; |
| 111 | } ip6_sv_reass_t; |
| 112 | |
| 113 | typedef struct |
| 114 | { |
| 115 | ip6_sv_reass_t *pool; |
| 116 | u32 reass_n; |
| 117 | u32 id_counter; |
| 118 | clib_spinlock_t lock; |
| 119 | // lru indexes |
| 120 | u32 lru_first; |
| 121 | u32 lru_last; |
| 122 | } ip6_sv_reass_per_thread_t; |
| 123 | |
| 124 | typedef struct |
| 125 | { |
| 126 | // IPv6 config |
| 127 | u32 timeout_ms; |
| 128 | f64 timeout; |
| 129 | u32 expire_walk_interval_ms; |
| 130 | // maximum number of fragments in one reassembly |
| 131 | u32 max_reass_len; |
| 132 | // maximum number of reassemblies |
| 133 | u32 max_reass_n; |
| 134 | |
| 135 | // IPv6 runtime |
| 136 | clib_bihash_48_8_t hash; |
| 137 | |
| 138 | // per-thread data |
| 139 | ip6_sv_reass_per_thread_t *per_thread_data; |
| 140 | |
| 141 | // convenience |
| 142 | vlib_main_t *vlib_main; |
| 143 | vnet_main_t *vnet_main; |
| 144 | |
| 145 | // node index of ip6-drop node |
| 146 | u32 ip6_drop_idx; |
| 147 | u32 ip6_icmp_error_idx; |
| 148 | u32 ip6_sv_reass_expire_node_idx; |
| 149 | |
| 150 | /** Worker handoff */ |
| 151 | u32 fq_index; |
| 152 | u32 fq_feature_index; |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 153 | u32 fq_custom_context_index; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 154 | |
Klement Sekera | 63c7353 | 2019-09-30 14:35:36 +0000 | [diff] [blame] | 155 | // reference count for enabling/disabling feature - per interface |
| 156 | u32 *feature_use_refcount_per_intf; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 157 | } ip6_sv_reass_main_t; |
| 158 | |
| 159 | extern ip6_sv_reass_main_t ip6_sv_reass_main; |
| 160 | |
| 161 | #ifndef CLIB_MARCH_VARIANT |
| 162 | ip6_sv_reass_main_t ip6_sv_reass_main; |
| 163 | #endif /* CLIB_MARCH_VARIANT */ |
| 164 | |
| 165 | typedef enum |
| 166 | { |
| 167 | IP6_SV_REASSEMBLY_NEXT_INPUT, |
| 168 | IP6_SV_REASSEMBLY_NEXT_DROP, |
| 169 | IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR, |
| 170 | IP6_SV_REASSEMBLY_NEXT_HANDOFF, |
| 171 | IP6_SV_REASSEMBLY_N_NEXT, |
| 172 | } ip6_sv_reass_next_t; |
| 173 | |
| 174 | typedef enum |
| 175 | { |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 176 | REASS_FRAGMENT_CACHE, |
| 177 | REASS_FINISH, |
| 178 | REASS_FRAGMENT_FORWARD, |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 179 | REASS_PASSTHROUGH, |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 180 | } ip6_sv_reass_trace_operation_e; |
| 181 | |
| 182 | typedef struct |
| 183 | { |
| 184 | ip6_sv_reass_trace_operation_e action; |
| 185 | u32 reass_id; |
| 186 | u32 op_id; |
| 187 | u8 ip_proto; |
| 188 | u16 l4_src_port; |
| 189 | u16 l4_dst_port; |
| 190 | } ip6_sv_reass_trace_t; |
| 191 | |
| 192 | static u8 * |
| 193 | format_ip6_sv_reass_trace (u8 * s, va_list * args) |
| 194 | { |
| 195 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 196 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 197 | ip6_sv_reass_trace_t *t = va_arg (*args, ip6_sv_reass_trace_t *); |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 198 | if (REASS_PASSTHROUGH != t->action) |
| 199 | { |
| 200 | s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id); |
| 201 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 202 | switch (t->action) |
| 203 | { |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 204 | case REASS_FRAGMENT_CACHE: |
| 205 | s = format (s, "[cached]"); |
| 206 | break; |
| 207 | case REASS_FINISH: |
| 208 | s = |
| 209 | format (s, "[finish, ip proto=%u, src_port=%u, dst_port=%u]", |
| 210 | t->ip_proto, clib_net_to_host_u16 (t->l4_src_port), |
| 211 | clib_net_to_host_u16 (t->l4_dst_port)); |
| 212 | break; |
| 213 | case REASS_FRAGMENT_FORWARD: |
| 214 | s = |
| 215 | format (s, "[forward, ip proto=%u, src_port=%u, dst_port=%u]", |
| 216 | t->ip_proto, clib_net_to_host_u16 (t->l4_src_port), |
| 217 | clib_net_to_host_u16 (t->l4_dst_port)); |
| 218 | break; |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 219 | case REASS_PASSTHROUGH: |
Klement Sekera | 7c3275e | 2021-12-07 09:49:53 +0000 | [diff] [blame] | 220 | s = format (s, "[not fragmented or atomic fragment]"); |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 221 | break; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 222 | } |
| 223 | return s; |
| 224 | } |
| 225 | |
| 226 | static void |
| 227 | ip6_sv_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node, |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 228 | ip6_sv_reass_t * reass, u32 bi, |
| 229 | ip6_sv_reass_trace_operation_e action, |
| 230 | u32 ip_proto, u16 l4_src_port, u16 l4_dst_port) |
| 231 | { |
| 232 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Klement Sekera | 53be16d | 2020-12-15 21:47:36 +0100 | [diff] [blame] | 233 | if (pool_is_free_index |
| 234 | (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b))) |
| 235 | { |
| 236 | // this buffer's trace is gone |
| 237 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 238 | return; |
| 239 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 240 | ip6_sv_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0])); |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 241 | if (reass) |
| 242 | { |
| 243 | t->reass_id = reass->id; |
| 244 | t->op_id = reass->trace_op_counter; |
| 245 | ++reass->trace_op_counter; |
| 246 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 247 | t->action = action; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 248 | t->ip_proto = ip_proto; |
| 249 | t->l4_src_port = l4_src_port; |
| 250 | t->l4_dst_port = l4_dst_port; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 251 | #if 0 |
| 252 | static u8 *s = NULL; |
| 253 | s = format (s, "%U", format_ip6_sv_reass_trace, NULL, NULL, t); |
| 254 | printf ("%.*s\n", vec_len (s), s); |
| 255 | fflush (stdout); |
| 256 | vec_reset_length (s); |
| 257 | #endif |
| 258 | } |
| 259 | |
| 260 | always_inline void |
| 261 | ip6_sv_reass_free (vlib_main_t * vm, ip6_sv_reass_main_t * rm, |
| 262 | ip6_sv_reass_per_thread_t * rt, ip6_sv_reass_t * reass) |
| 263 | { |
| 264 | clib_bihash_kv_48_8_t kv; |
| 265 | kv.key[0] = reass->key.as_u64[0]; |
| 266 | kv.key[1] = reass->key.as_u64[1]; |
| 267 | kv.key[2] = reass->key.as_u64[2]; |
| 268 | kv.key[3] = reass->key.as_u64[3]; |
| 269 | kv.key[4] = reass->key.as_u64[4]; |
| 270 | kv.key[5] = reass->key.as_u64[5]; |
| 271 | clib_bihash_add_del_48_8 (&rm->hash, &kv, 0); |
| 272 | vlib_buffer_free (vm, reass->cached_buffers, |
| 273 | vec_len (reass->cached_buffers)); |
| 274 | vec_free (reass->cached_buffers); |
| 275 | reass->cached_buffers = NULL; |
| 276 | if (~0 != reass->lru_prev) |
| 277 | { |
| 278 | ip6_sv_reass_t *lru_prev = |
| 279 | pool_elt_at_index (rt->pool, reass->lru_prev); |
| 280 | lru_prev->lru_next = reass->lru_next; |
| 281 | } |
| 282 | if (~0 != reass->lru_next) |
| 283 | { |
| 284 | ip6_sv_reass_t *lru_next = |
| 285 | pool_elt_at_index (rt->pool, reass->lru_next); |
| 286 | lru_next->lru_prev = reass->lru_prev; |
| 287 | } |
| 288 | if (rt->lru_first == reass - rt->pool) |
| 289 | { |
| 290 | rt->lru_first = reass->lru_next; |
| 291 | } |
| 292 | if (rt->lru_last == reass - rt->pool) |
| 293 | { |
| 294 | rt->lru_last = reass->lru_prev; |
| 295 | } |
| 296 | pool_put (rt->pool, reass); |
| 297 | --rt->reass_n; |
| 298 | } |
| 299 | |
| 300 | always_inline void |
| 301 | ip6_sv_reass_init (ip6_sv_reass_t * reass) |
| 302 | { |
| 303 | reass->cached_buffers = NULL; |
| 304 | reass->is_complete = false; |
| 305 | } |
| 306 | |
| 307 | always_inline ip6_sv_reass_t * |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 308 | ip6_sv_reass_find_or_create (vlib_main_t *vm, ip6_sv_reass_main_t *rm, |
| 309 | ip6_sv_reass_per_thread_t *rt, |
| 310 | ip6_sv_reass_kv_t *kv, u8 *do_handoff) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 311 | { |
| 312 | ip6_sv_reass_t *reass = NULL; |
Tom Seidenberg | 5a7f2f1 | 2020-04-28 17:58:12 -0400 | [diff] [blame] | 313 | f64 now = vlib_time_now (vm); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 314 | |
Klement Sekera | 4ab6ad1 | 2021-12-17 12:47:51 +0000 | [diff] [blame] | 315 | again: |
| 316 | |
Klement Sekera | c99c025 | 2019-12-18 12:17:06 +0000 | [diff] [blame] | 317 | if (!clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv)) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 318 | { |
| 319 | if (vm->thread_index != kv->v.thread_index) |
| 320 | { |
| 321 | *do_handoff = 1; |
| 322 | return NULL; |
| 323 | } |
| 324 | reass = pool_elt_at_index (rt->pool, kv->v.reass_index); |
| 325 | |
| 326 | if (now > reass->last_heard + rm->timeout) |
| 327 | { |
| 328 | ip6_sv_reass_free (vm, rm, rt, reass); |
| 329 | reass = NULL; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (reass) |
| 334 | { |
| 335 | reass->last_heard = now; |
| 336 | return reass; |
| 337 | } |
| 338 | |
| 339 | if (rt->reass_n >= rm->max_reass_n) |
| 340 | { |
zhengdelun | ce53363 | 2020-05-22 15:42:28 +0800 | [diff] [blame] | 341 | reass = pool_elt_at_index (rt->pool, rt->lru_first); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 342 | ip6_sv_reass_free (vm, rm, rt, reass); |
| 343 | } |
| 344 | |
| 345 | pool_get (rt->pool, reass); |
| 346 | clib_memset (reass, 0, sizeof (*reass)); |
| 347 | reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter; |
| 348 | ++rt->id_counter; |
| 349 | ip6_sv_reass_init (reass); |
| 350 | ++rt->reass_n; |
| 351 | |
| 352 | reass->lru_prev = reass->lru_next = ~0; |
| 353 | |
| 354 | if (~0 != rt->lru_last) |
| 355 | { |
| 356 | ip6_sv_reass_t *lru_last = pool_elt_at_index (rt->pool, rt->lru_last); |
| 357 | reass->lru_prev = rt->lru_last; |
| 358 | lru_last->lru_next = rt->lru_last = reass - rt->pool; |
| 359 | } |
| 360 | |
| 361 | if (~0 == rt->lru_first) |
| 362 | { |
| 363 | rt->lru_first = rt->lru_last = reass - rt->pool; |
| 364 | } |
| 365 | |
Klement Sekera | c99c025 | 2019-12-18 12:17:06 +0000 | [diff] [blame] | 366 | reass->key.as_u64[0] = kv->kv.key[0]; |
| 367 | reass->key.as_u64[1] = kv->kv.key[1]; |
| 368 | reass->key.as_u64[2] = kv->kv.key[2]; |
| 369 | reass->key.as_u64[3] = kv->kv.key[3]; |
| 370 | reass->key.as_u64[4] = kv->kv.key[4]; |
| 371 | reass->key.as_u64[5] = kv->kv.key[5]; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 372 | kv->v.reass_index = (reass - rt->pool); |
| 373 | kv->v.thread_index = vm->thread_index; |
| 374 | reass->last_heard = now; |
| 375 | |
Klement Sekera | 4ab6ad1 | 2021-12-17 12:47:51 +0000 | [diff] [blame] | 376 | int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2); |
| 377 | if (rv) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 378 | { |
| 379 | ip6_sv_reass_free (vm, rm, rt, reass); |
| 380 | reass = NULL; |
Klement Sekera | 4ab6ad1 | 2021-12-17 12:47:51 +0000 | [diff] [blame] | 381 | // if other worker created a context already work with the other copy |
| 382 | if (-2 == rv) |
| 383 | goto again; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return reass; |
| 387 | } |
| 388 | |
| 389 | always_inline ip6_sv_reass_rc_t |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 390 | ip6_sv_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 391 | ip6_sv_reass_main_t *rm, ip6_sv_reass_t *reass, u32 bi0, |
| 392 | ip6_frag_hdr_t *frag_hdr) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 393 | { |
| 394 | vlib_buffer_t *fb = vlib_get_buffer (vm, bi0); |
| 395 | vnet_buffer_opaque_t *fvnb = vnet_buffer (fb); |
| 396 | fvnb->ip.reass.ip6_frag_hdr_offset = |
| 397 | (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb); |
| 398 | ip6_header_t *fip = vlib_buffer_get_current (fb); |
| 399 | if (fb->current_length < sizeof (*fip) || |
| 400 | fvnb->ip.reass.ip6_frag_hdr_offset == 0 || |
| 401 | fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length) |
| 402 | { |
| 403 | return IP6_SV_REASS_RC_INTERNAL_ERROR; |
| 404 | } |
| 405 | |
| 406 | u32 fragment_first = fvnb->ip.reass.fragment_first = |
| 407 | ip6_frag_hdr_offset_bytes (frag_hdr); |
| 408 | u32 fragment_length = |
| 409 | vlib_buffer_length_in_chain (vm, fb) - |
| 410 | (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
Klement Sekera | 755042d | 2021-12-01 10:14:38 +0000 | [diff] [blame] | 411 | if (0 == fragment_length) |
| 412 | { |
| 413 | return IP6_SV_REASS_RC_INVALID_FRAG_LEN; |
| 414 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 415 | u32 fragment_last = fvnb->ip.reass.fragment_last = |
| 416 | fragment_first + fragment_length - 1; |
| 417 | fvnb->ip.reass.range_first = fragment_first; |
| 418 | fvnb->ip.reass.range_last = fragment_last; |
| 419 | fvnb->ip.reass.next_range_bi = ~0; |
| 420 | if (0 == fragment_first) |
| 421 | { |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 422 | if (!ip6_get_port |
| 423 | (vm, fb, fip, fb->current_length, &reass->ip_proto, |
| 424 | &reass->l4_src_port, &reass->l4_dst_port, |
| 425 | &reass->icmp_type_or_tcp_flags, &reass->tcp_ack_number, |
| 426 | &reass->tcp_seq_number)) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 427 | return IP6_SV_REASS_RC_UNSUPP_IP_PROTO; |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 428 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 429 | reass->is_complete = true; |
| 430 | vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0); |
| 431 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 432 | { |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 433 | ip6_sv_reass_add_trace (vm, node, reass, bi0, REASS_FINISH, |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 434 | reass->ip_proto, reass->l4_src_port, |
| 435 | reass->l4_dst_port); |
| 436 | } |
| 437 | } |
| 438 | vec_add1 (reass->cached_buffers, bi0); |
| 439 | if (!reass->is_complete) |
| 440 | { |
| 441 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 442 | { |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 443 | ip6_sv_reass_add_trace (vm, node, reass, bi0, REASS_FRAGMENT_CACHE, |
| 444 | reass->ip_proto, reass->l4_src_port, |
| 445 | reass->l4_dst_port); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 446 | } |
| 447 | if (vec_len (reass->cached_buffers) > rm->max_reass_len) |
| 448 | { |
| 449 | return IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS; |
| 450 | } |
| 451 | } |
| 452 | return IP6_SV_REASS_RC_OK; |
| 453 | } |
| 454 | |
| 455 | always_inline bool |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 456 | ip6_sv_reass_verify_upper_layer_present (vlib_node_runtime_t *node, |
| 457 | vlib_buffer_t *b, |
| 458 | ip6_ext_hdr_chain_t *hc) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 459 | { |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 460 | int nh = hc->eh[hc->length - 1].protocol; |
| 461 | /* Checking to see if it's a terminating header */ |
| 462 | if (ip6_ext_hdr (nh)) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 463 | { |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 464 | icmp6_error_set_vnet_buffer ( |
| 465 | b, ICMP6_parameter_problem, |
| 466 | ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 467 | b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER]; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | always_inline bool |
| 474 | ip6_sv_reass_verify_fragment_multiple_8 (vlib_main_t * vm, |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 475 | vlib_buffer_t * b, |
| 476 | ip6_frag_hdr_t * frag_hdr) |
| 477 | { |
| 478 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 479 | ip6_header_t *ip = vlib_buffer_get_current (b); |
| 480 | int more_fragments = ip6_frag_hdr_more (frag_hdr); |
| 481 | u32 fragment_length = |
| 482 | vlib_buffer_length_in_chain (vm, b) - |
| 483 | (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
| 484 | if (more_fragments && 0 != fragment_length % 8) |
| 485 | { |
| 486 | icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem, |
| 487 | ICMP6_parameter_problem_erroneous_header_field, |
| 488 | (u8 *) & ip->payload_length - (u8 *) ip); |
| 489 | return false; |
| 490 | } |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | always_inline bool |
| 495 | ip6_sv_reass_verify_packet_size_lt_64k (vlib_main_t * vm, |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 496 | vlib_buffer_t * b, |
| 497 | ip6_frag_hdr_t * frag_hdr) |
| 498 | { |
| 499 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 500 | u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr); |
| 501 | u32 fragment_length = |
| 502 | vlib_buffer_length_in_chain (vm, b) - |
| 503 | (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
| 504 | if (fragment_first + fragment_length > 65535) |
| 505 | { |
| 506 | ip6_header_t *ip0 = vlib_buffer_get_current (b); |
| 507 | icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem, |
| 508 | ICMP6_parameter_problem_erroneous_header_field, |
| 509 | (u8 *) & frag_hdr->fragment_offset_and_more |
| 510 | - (u8 *) ip0); |
| 511 | return false; |
| 512 | } |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | always_inline uword |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 517 | ip6_sv_reassembly_inline (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 518 | vlib_frame_t *frame, bool is_feature, |
| 519 | bool custom_next, bool custom_context) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 520 | { |
| 521 | u32 *from = vlib_frame_vector_args (frame); |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 522 | u32 n_left_from, n_left_to_next, *to_next, *to_next_aux, next_index; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 523 | ip6_sv_reass_main_t *rm = &ip6_sv_reass_main; |
| 524 | ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index]; |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 525 | u32 *context; |
| 526 | if (custom_context) |
| 527 | context = vlib_frame_aux_args (frame); |
| 528 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 529 | clib_spinlock_lock (&rt->lock); |
| 530 | |
| 531 | n_left_from = frame->n_vectors; |
| 532 | next_index = node->cached_next_index; |
| 533 | |
| 534 | while (n_left_from > 0) |
| 535 | { |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 536 | if (custom_context) |
| 537 | vlib_get_next_frame_with_aux_safe (vm, node, next_index, to_next, |
| 538 | to_next_aux, n_left_to_next); |
| 539 | else |
| 540 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 541 | |
| 542 | while (n_left_from > 0 && n_left_to_next > 0) |
| 543 | { |
| 544 | u32 bi0; |
| 545 | vlib_buffer_t *b0; |
| 546 | u32 next0 = IP6_SV_REASSEMBLY_NEXT_DROP; |
| 547 | u32 error0 = IP6_ERROR_NONE; |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 548 | u8 forward_context = 0; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 549 | bi0 = from[0]; |
| 550 | b0 = vlib_get_buffer (vm, bi0); |
| 551 | |
| 552 | ip6_header_t *ip0 = vlib_buffer_get_current (b0); |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 553 | ip6_frag_hdr_t *frag_hdr; |
| 554 | ip6_ext_hdr_chain_t hdr_chain; |
Klement Sekera | 7c3275e | 2021-12-07 09:49:53 +0000 | [diff] [blame] | 555 | bool is_atomic_fragment = false; |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 556 | |
| 557 | int res = ip6_ext_header_walk ( |
| 558 | b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain); |
Klement Sekera | 7c3275e | 2021-12-07 09:49:53 +0000 | [diff] [blame] | 559 | if (res >= 0 && |
| 560 | hdr_chain.eh[res].protocol == IP_PROTOCOL_IPV6_FRAGMENTATION) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 561 | { |
Klement Sekera | 7c3275e | 2021-12-07 09:49:53 +0000 | [diff] [blame] | 562 | frag_hdr = |
| 563 | ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset); |
| 564 | is_atomic_fragment = (0 == ip6_frag_hdr_offset (frag_hdr) && |
| 565 | !ip6_frag_hdr_more (frag_hdr)); |
| 566 | } |
| 567 | |
| 568 | if (res < 0 || |
| 569 | hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION || |
| 570 | is_atomic_fragment) |
| 571 | { |
| 572 | // this is a regular unfragmented packet or an atomic fragment |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 573 | if (!ip6_get_port |
| 574 | (vm, b0, ip0, b0->current_length, |
| 575 | &(vnet_buffer (b0)->ip.reass.ip_proto), |
| 576 | &(vnet_buffer (b0)->ip.reass.l4_src_port), |
| 577 | &(vnet_buffer (b0)->ip.reass.l4_dst_port), |
| 578 | &(vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags), |
| 579 | &(vnet_buffer (b0)->ip.reass.tcp_ack_number), |
| 580 | &(vnet_buffer (b0)->ip.reass.tcp_seq_number))) |
| 581 | { |
| 582 | error0 = IP6_ERROR_REASS_UNSUPP_IP_PROTO; |
Klement Sekera | 1766ddc | 2020-03-30 16:59:38 +0200 | [diff] [blame] | 583 | b0->error = node->errors[error0]; |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 584 | next0 = IP6_SV_REASSEMBLY_NEXT_DROP; |
| 585 | goto packet_enqueue; |
| 586 | } |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 587 | vnet_buffer (b0)->ip.reass.is_non_first_fragment = 0; |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 588 | next0 = custom_next ? vnet_buffer (b0)->ip.reass.next_index : |
| 589 | IP6_SV_REASSEMBLY_NEXT_INPUT; |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 590 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 591 | { |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 592 | ip6_sv_reass_add_trace ( |
| 593 | vm, node, NULL, bi0, REASS_PASSTHROUGH, |
| 594 | vnet_buffer (b0)->ip.reass.ip_proto, |
| 595 | vnet_buffer (b0)->ip.reass.l4_src_port, |
| 596 | vnet_buffer (b0)->ip.reass.l4_dst_port); |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 597 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 598 | goto packet_enqueue; |
| 599 | } |
Klement Sekera | 7c3275e | 2021-12-07 09:49:53 +0000 | [diff] [blame] | 600 | |
Klement Sekera | 38f7ccb | 2019-10-28 11:26:28 +0000 | [diff] [blame] | 601 | vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset = |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 602 | hdr_chain.eh[res].offset; |
Klement Sekera | 7c3275e | 2021-12-07 09:49:53 +0000 | [diff] [blame] | 603 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 604 | if (0 == ip6_frag_hdr_offset (frag_hdr)) |
| 605 | { |
| 606 | // first fragment - verify upper-layer is present |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 607 | if (!ip6_sv_reass_verify_upper_layer_present (node, b0, |
| 608 | &hdr_chain)) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 609 | { |
| 610 | next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR; |
| 611 | goto packet_enqueue; |
| 612 | } |
| 613 | } |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 614 | if (!ip6_sv_reass_verify_fragment_multiple_8 (vm, b0, frag_hdr) || |
| 615 | !ip6_sv_reass_verify_packet_size_lt_64k (vm, b0, frag_hdr)) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 616 | { |
| 617 | next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR; |
| 618 | goto packet_enqueue; |
| 619 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 620 | |
| 621 | ip6_sv_reass_kv_t kv; |
| 622 | u8 do_handoff = 0; |
| 623 | |
| 624 | kv.k.as_u64[0] = ip0->src_address.as_u64[0]; |
| 625 | kv.k.as_u64[1] = ip0->src_address.as_u64[1]; |
| 626 | kv.k.as_u64[2] = ip0->dst_address.as_u64[0]; |
| 627 | kv.k.as_u64[3] = ip0->dst_address.as_u64[1]; |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 628 | if (custom_context) |
| 629 | kv.k.as_u64[4] = |
| 630 | (u64) *context << 32 | (u64) frag_hdr->identification; |
| 631 | else |
| 632 | kv.k.as_u64[4] = |
| 633 | ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index, |
| 634 | vnet_buffer (b0)->sw_if_index[VLIB_RX])) |
| 635 | << 32 | |
| 636 | (u64) frag_hdr->identification; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 637 | kv.k.as_u64[5] = ip0->protocol; |
| 638 | |
| 639 | ip6_sv_reass_t *reass = |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 640 | ip6_sv_reass_find_or_create (vm, rm, rt, &kv, &do_handoff); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 641 | |
| 642 | if (PREDICT_FALSE (do_handoff)) |
| 643 | { |
| 644 | next0 = IP6_SV_REASSEMBLY_NEXT_HANDOFF; |
| 645 | vnet_buffer (b0)->ip.reass.owner_thread_index = |
| 646 | kv.v.thread_index; |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 647 | if (custom_context) |
| 648 | forward_context = 1; |
Klement Sekera | 364b20a | 2019-10-07 09:48:06 +0000 | [diff] [blame] | 649 | goto packet_enqueue; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | if (!reass) |
| 653 | { |
| 654 | next0 = IP6_SV_REASSEMBLY_NEXT_DROP; |
| 655 | error0 = IP6_ERROR_REASS_LIMIT_REACHED; |
Klement Sekera | 1766ddc | 2020-03-30 16:59:38 +0200 | [diff] [blame] | 656 | b0->error = node->errors[error0]; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 657 | goto packet_enqueue; |
| 658 | } |
| 659 | |
| 660 | if (reass->is_complete) |
| 661 | { |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 662 | vnet_buffer (b0)->ip.reass.is_non_first_fragment = |
| 663 | ! !ip6_frag_hdr_offset (frag_hdr); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 664 | vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto; |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 665 | vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags = |
| 666 | reass->icmp_type_or_tcp_flags; |
| 667 | vnet_buffer (b0)->ip.reass.tcp_ack_number = |
| 668 | reass->tcp_ack_number; |
| 669 | vnet_buffer (b0)->ip.reass.tcp_seq_number = |
| 670 | reass->tcp_seq_number; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 671 | vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port; |
| 672 | vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port; |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 673 | next0 = custom_next ? vnet_buffer (b0)->ip.reass.next_index : |
| 674 | IP6_SV_REASSEMBLY_NEXT_INPUT; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 675 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 676 | { |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 677 | ip6_sv_reass_add_trace ( |
| 678 | vm, node, reass, bi0, REASS_FRAGMENT_FORWARD, |
| 679 | reass->ip_proto, reass->l4_src_port, reass->l4_dst_port); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 680 | } |
| 681 | goto packet_enqueue; |
| 682 | } |
| 683 | |
Klement Sekera | d4ba0d1 | 2021-11-02 15:33:55 +0100 | [diff] [blame] | 684 | u32 counter = ~0; |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 685 | switch (ip6_sv_reass_update (vm, node, rm, reass, bi0, frag_hdr)) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 686 | { |
| 687 | case IP6_SV_REASS_RC_OK: |
| 688 | /* nothing to do here */ |
| 689 | break; |
| 690 | case IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS: |
Klement Sekera | d4ba0d1 | 2021-11-02 15:33:55 +0100 | [diff] [blame] | 691 | counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 692 | break; |
| 693 | case IP6_SV_REASS_RC_UNSUPP_IP_PROTO: |
Klement Sekera | d4ba0d1 | 2021-11-02 15:33:55 +0100 | [diff] [blame] | 694 | counter = IP6_ERROR_REASS_UNSUPP_IP_PROTO; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 695 | break; |
| 696 | case IP6_SV_REASS_RC_INTERNAL_ERROR: |
Klement Sekera | d4ba0d1 | 2021-11-02 15:33:55 +0100 | [diff] [blame] | 697 | counter = IP6_ERROR_REASS_INTERNAL_ERROR; |
| 698 | break; |
Klement Sekera | 755042d | 2021-12-01 10:14:38 +0000 | [diff] [blame] | 699 | case IP6_SV_REASS_RC_INVALID_FRAG_LEN: |
| 700 | counter = IP6_ERROR_REASS_INVALID_FRAG_LEN; |
| 701 | break; |
Klement Sekera | d4ba0d1 | 2021-11-02 15:33:55 +0100 | [diff] [blame] | 702 | } |
| 703 | if (~0 != counter) |
| 704 | { |
| 705 | vlib_node_increment_counter (vm, node->node_index, counter, 1); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 706 | ip6_sv_reass_free (vm, rm, rt, reass); |
| 707 | goto next_packet; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 710 | if (reass->is_complete) |
| 711 | { |
| 712 | u32 idx; |
| 713 | vec_foreach_index (idx, reass->cached_buffers) |
| 714 | { |
| 715 | u32 bi0 = vec_elt (reass->cached_buffers, idx); |
| 716 | if (0 == n_left_to_next) |
| 717 | { |
| 718 | vlib_put_next_frame (vm, node, next_index, |
| 719 | n_left_to_next); |
| 720 | vlib_get_next_frame (vm, node, next_index, to_next, |
| 721 | n_left_to_next); |
| 722 | } |
| 723 | to_next[0] = bi0; |
| 724 | to_next += 1; |
| 725 | n_left_to_next -= 1; |
| 726 | b0 = vlib_get_buffer (vm, bi0); |
| 727 | if (is_feature) |
| 728 | { |
| 729 | vnet_feature_next (&next0, b0); |
| 730 | } |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 731 | frag_hdr = |
| 732 | vlib_buffer_get_current (b0) + |
| 733 | vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset; |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 734 | vnet_buffer (b0)->ip.reass.is_non_first_fragment = |
| 735 | ! !ip6_frag_hdr_offset (frag_hdr); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 736 | vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto; |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 737 | vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags = |
| 738 | reass->icmp_type_or_tcp_flags; |
| 739 | vnet_buffer (b0)->ip.reass.tcp_ack_number = |
| 740 | reass->tcp_ack_number; |
| 741 | vnet_buffer (b0)->ip.reass.tcp_seq_number = |
| 742 | reass->tcp_seq_number; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 743 | vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port; |
| 744 | vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port; |
| 745 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 746 | { |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 747 | ip6_sv_reass_add_trace ( |
| 748 | vm, node, reass, bi0, REASS_FRAGMENT_FORWARD, |
| 749 | reass->ip_proto, reass->l4_src_port, reass->l4_dst_port); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 750 | } |
| 751 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 752 | to_next, n_left_to_next, bi0, |
| 753 | next0); |
| 754 | } |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 755 | vec_set_len (reass->cached_buffers, |
| 756 | 0); // buffers are owned by frame now |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 757 | } |
| 758 | goto next_packet; |
| 759 | |
| 760 | packet_enqueue: |
| 761 | to_next[0] = bi0; |
| 762 | to_next += 1; |
| 763 | n_left_to_next -= 1; |
| 764 | if (is_feature && IP6_ERROR_NONE == error0) |
| 765 | { |
| 766 | b0 = vlib_get_buffer (vm, bi0); |
| 767 | vnet_feature_next (&next0, b0); |
| 768 | } |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 769 | if (custom_context && forward_context) |
| 770 | { |
| 771 | if (to_next_aux) |
| 772 | { |
| 773 | to_next_aux[0] = *context; |
| 774 | to_next_aux += 1; |
| 775 | } |
| 776 | vlib_validate_buffer_enqueue_with_aux_x1 ( |
| 777 | vm, node, next_index, to_next, to_next_aux, n_left_to_next, |
| 778 | bi0, *context, next0); |
| 779 | } |
| 780 | else |
| 781 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 782 | n_left_to_next, bi0, next0); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 783 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 784 | next_packet: |
| 785 | from += 1; |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 786 | if (custom_context) |
| 787 | context += 1; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 788 | n_left_from -= 1; |
| 789 | } |
| 790 | |
| 791 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 792 | } |
| 793 | |
| 794 | clib_spinlock_unlock (&rt->lock); |
| 795 | return frame->n_vectors; |
| 796 | } |
| 797 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 798 | VLIB_NODE_FN (ip6_sv_reass_node) (vlib_main_t * vm, |
| 799 | vlib_node_runtime_t * node, |
| 800 | vlib_frame_t * frame) |
| 801 | { |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 802 | return ip6_sv_reassembly_inline (vm, node, frame, false /* is_feature */, |
| 803 | false /* custom next */, |
| 804 | false /* custom context */); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 807 | VLIB_REGISTER_NODE (ip6_sv_reass_node) = { |
| 808 | .name = "ip6-sv-reassembly", |
| 809 | .vector_size = sizeof (u32), |
| 810 | .format_trace = format_ip6_sv_reass_trace, |
Neale Ranns | e22a704 | 2022-08-09 03:03:29 +0000 | [diff] [blame] | 811 | .n_errors = IP6_N_ERROR, |
| 812 | .error_counters = ip6_error_counters, |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 813 | .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT, |
| 814 | .next_nodes = |
| 815 | { |
| 816 | [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input", |
| 817 | [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop", |
| 818 | [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error", |
| 819 | [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reassembly-handoff", |
| 820 | }, |
| 821 | }; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 822 | |
| 823 | VLIB_NODE_FN (ip6_sv_reass_node_feature) (vlib_main_t * vm, |
| 824 | vlib_node_runtime_t * node, |
| 825 | vlib_frame_t * frame) |
| 826 | { |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 827 | return ip6_sv_reassembly_inline (vm, node, frame, true /* is_feature */, |
| 828 | false /* custom next */, |
| 829 | false /* custom context */); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 832 | VLIB_REGISTER_NODE (ip6_sv_reass_node_feature) = { |
| 833 | .name = "ip6-sv-reassembly-feature", |
| 834 | .vector_size = sizeof (u32), |
| 835 | .format_trace = format_ip6_sv_reass_trace, |
Neale Ranns | e22a704 | 2022-08-09 03:03:29 +0000 | [diff] [blame] | 836 | .n_errors = IP6_N_ERROR, |
| 837 | .error_counters = ip6_error_counters, |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 838 | .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT, |
| 839 | .next_nodes = |
| 840 | { |
| 841 | [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input", |
| 842 | [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop", |
| 843 | [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error", |
| 844 | [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reass-feature-hoff", |
| 845 | }, |
| 846 | }; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 847 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 848 | VNET_FEATURE_INIT (ip6_sv_reassembly_feature) = { |
| 849 | .arc_name = "ip6-unicast", |
| 850 | .node_name = "ip6-sv-reassembly-feature", |
| 851 | .runs_before = VNET_FEATURES ("ip6-lookup"), |
| 852 | .runs_after = 0, |
| 853 | }; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 854 | |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 855 | VLIB_NODE_FN (ip6_sv_reass_custom_context_node) |
| 856 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame) |
| 857 | { |
| 858 | return ip6_sv_reassembly_inline (vm, node, frame, false /* is_feature */, |
| 859 | true /* custom next */, |
| 860 | true /* custom context */); |
| 861 | } |
| 862 | |
| 863 | VLIB_REGISTER_NODE (ip6_sv_reass_custom_context_node) = { |
| 864 | .name = "ip6-sv-reassembly-custom-context", |
| 865 | .vector_size = sizeof (u32), |
| 866 | .aux_size = sizeof (u32), |
| 867 | .format_trace = format_ip6_sv_reass_trace, |
| 868 | .n_errors = IP6_N_ERROR, |
| 869 | .error_counters = ip6_error_counters, |
| 870 | .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT, |
| 871 | .next_nodes = |
| 872 | { |
| 873 | [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input", |
| 874 | [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop", |
| 875 | [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error", |
| 876 | [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reassembly-custom-context-handoff", |
| 877 | }, |
| 878 | }; |
| 879 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 880 | #ifndef CLIB_MARCH_VARIANT |
| 881 | static u32 |
| 882 | ip6_sv_reass_get_nbuckets () |
| 883 | { |
| 884 | ip6_sv_reass_main_t *rm = &ip6_sv_reass_main; |
| 885 | u32 nbuckets; |
| 886 | u8 i; |
| 887 | |
| 888 | nbuckets = (u32) (rm->max_reass_n / IP6_SV_REASS_HT_LOAD_FACTOR); |
| 889 | |
| 890 | for (i = 0; i < 31; i++) |
| 891 | if ((1 << i) >= nbuckets) |
| 892 | break; |
| 893 | nbuckets = 1 << i; |
| 894 | |
| 895 | return nbuckets; |
| 896 | } |
| 897 | #endif /* CLIB_MARCH_VARIANT */ |
| 898 | |
| 899 | typedef enum |
| 900 | { |
| 901 | IP6_EVENT_CONFIG_CHANGED = 1, |
| 902 | } ip6_sv_reass_event_t; |
| 903 | |
| 904 | #ifndef CLIB_MARCH_VARIANT |
| 905 | typedef struct |
| 906 | { |
| 907 | int failure; |
| 908 | clib_bihash_48_8_t *new_hash; |
| 909 | } ip6_rehash_cb_ctx; |
| 910 | |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 911 | static int |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 912 | ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx) |
| 913 | { |
| 914 | ip6_rehash_cb_ctx *ctx = _ctx; |
| 915 | if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1)) |
| 916 | { |
| 917 | ctx->failure = 1; |
| 918 | } |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 919 | return (BIHASH_WALK_CONTINUE); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | static void |
| 923 | ip6_sv_reass_set_params (u32 timeout_ms, u32 max_reassemblies, |
| 924 | u32 max_reassembly_length, |
| 925 | u32 expire_walk_interval_ms) |
| 926 | { |
| 927 | ip6_sv_reass_main.timeout_ms = timeout_ms; |
| 928 | ip6_sv_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC; |
| 929 | ip6_sv_reass_main.max_reass_n = max_reassemblies; |
| 930 | ip6_sv_reass_main.max_reass_len = max_reassembly_length; |
| 931 | ip6_sv_reass_main.expire_walk_interval_ms = expire_walk_interval_ms; |
| 932 | } |
| 933 | |
| 934 | vnet_api_error_t |
| 935 | ip6_sv_reass_set (u32 timeout_ms, u32 max_reassemblies, |
| 936 | u32 max_reassembly_length, u32 expire_walk_interval_ms) |
| 937 | { |
| 938 | u32 old_nbuckets = ip6_sv_reass_get_nbuckets (); |
| 939 | ip6_sv_reass_set_params (timeout_ms, max_reassemblies, |
| 940 | max_reassembly_length, expire_walk_interval_ms); |
| 941 | vlib_process_signal_event (ip6_sv_reass_main.vlib_main, |
| 942 | ip6_sv_reass_main.ip6_sv_reass_expire_node_idx, |
| 943 | IP6_EVENT_CONFIG_CHANGED, 0); |
| 944 | u32 new_nbuckets = ip6_sv_reass_get_nbuckets (); |
| 945 | if (ip6_sv_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets) |
| 946 | { |
| 947 | clib_bihash_48_8_t new_hash; |
| 948 | clib_memset (&new_hash, 0, sizeof (new_hash)); |
| 949 | ip6_rehash_cb_ctx ctx; |
| 950 | ctx.failure = 0; |
| 951 | ctx.new_hash = &new_hash; |
| 952 | clib_bihash_init_48_8 (&new_hash, "ip6-sv-reass", new_nbuckets, |
| 953 | new_nbuckets * 1024); |
| 954 | clib_bihash_foreach_key_value_pair_48_8 (&ip6_sv_reass_main.hash, |
| 955 | ip6_rehash_cb, &ctx); |
| 956 | if (ctx.failure) |
| 957 | { |
| 958 | clib_bihash_free_48_8 (&new_hash); |
| 959 | return -1; |
| 960 | } |
| 961 | else |
| 962 | { |
| 963 | clib_bihash_free_48_8 (&ip6_sv_reass_main.hash); |
| 964 | clib_memcpy_fast (&ip6_sv_reass_main.hash, &new_hash, |
| 965 | sizeof (ip6_sv_reass_main.hash)); |
| 966 | clib_bihash_copied (&ip6_sv_reass_main.hash, &new_hash); |
| 967 | } |
| 968 | } |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | vnet_api_error_t |
| 973 | ip6_sv_reass_get (u32 * timeout_ms, u32 * max_reassemblies, |
| 974 | u32 * max_reassembly_length, u32 * expire_walk_interval_ms) |
| 975 | { |
| 976 | *timeout_ms = ip6_sv_reass_main.timeout_ms; |
| 977 | *max_reassemblies = ip6_sv_reass_main.max_reass_n; |
| 978 | *max_reassembly_length = ip6_sv_reass_main.max_reass_len; |
| 979 | *expire_walk_interval_ms = ip6_sv_reass_main.expire_walk_interval_ms; |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | static clib_error_t * |
| 984 | ip6_sv_reass_init_function (vlib_main_t * vm) |
| 985 | { |
| 986 | ip6_sv_reass_main_t *rm = &ip6_sv_reass_main; |
| 987 | clib_error_t *error = 0; |
| 988 | u32 nbuckets; |
| 989 | vlib_node_t *node; |
| 990 | |
| 991 | rm->vlib_main = vm; |
| 992 | rm->vnet_main = vnet_get_main (); |
| 993 | |
| 994 | vec_validate (rm->per_thread_data, vlib_num_workers ()); |
| 995 | ip6_sv_reass_per_thread_t *rt; |
| 996 | vec_foreach (rt, rm->per_thread_data) |
| 997 | { |
| 998 | clib_spinlock_init (&rt->lock); |
| 999 | pool_alloc (rt->pool, rm->max_reass_n); |
| 1000 | rt->lru_first = rt->lru_last = ~0; |
| 1001 | } |
| 1002 | |
| 1003 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-sv-reassembly-expire-walk"); |
| 1004 | ASSERT (node); |
| 1005 | rm->ip6_sv_reass_expire_node_idx = node->index; |
| 1006 | |
| 1007 | ip6_sv_reass_set_params (IP6_SV_REASS_TIMEOUT_DEFAULT_MS, |
| 1008 | IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT, |
| 1009 | IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT, |
| 1010 | IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS); |
| 1011 | |
| 1012 | nbuckets = ip6_sv_reass_get_nbuckets (); |
| 1013 | clib_bihash_init_48_8 (&rm->hash, "ip6-sv-reass", nbuckets, |
| 1014 | nbuckets * 1024); |
| 1015 | |
| 1016 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop"); |
| 1017 | ASSERT (node); |
| 1018 | rm->ip6_drop_idx = node->index; |
| 1019 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error"); |
| 1020 | ASSERT (node); |
| 1021 | rm->ip6_icmp_error_idx = node->index; |
| 1022 | |
| 1023 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 1024 | return error; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1025 | |
| 1026 | rm->fq_index = vlib_frame_queue_main_init (ip6_sv_reass_node.index, 0); |
| 1027 | rm->fq_feature_index = |
| 1028 | vlib_frame_queue_main_init (ip6_sv_reass_node_feature.index, 0); |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1029 | rm->fq_custom_context_index = |
| 1030 | vlib_frame_queue_main_init (ip6_sv_reass_custom_context_node.index, 0); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1031 | |
Klement Sekera | 63c7353 | 2019-09-30 14:35:36 +0000 | [diff] [blame] | 1032 | rm->feature_use_refcount_per_intf = NULL; |
| 1033 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1034 | return error; |
| 1035 | } |
| 1036 | |
| 1037 | VLIB_INIT_FUNCTION (ip6_sv_reass_init_function); |
| 1038 | #endif /* CLIB_MARCH_VARIANT */ |
| 1039 | |
| 1040 | static uword |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 1041 | ip6_sv_reass_walk_expired (vlib_main_t *vm, |
| 1042 | CLIB_UNUSED (vlib_node_runtime_t *node), |
| 1043 | CLIB_UNUSED (vlib_frame_t *f)) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1044 | { |
| 1045 | ip6_sv_reass_main_t *rm = &ip6_sv_reass_main; |
| 1046 | uword event_type, *event_data = 0; |
| 1047 | |
| 1048 | while (true) |
| 1049 | { |
| 1050 | vlib_process_wait_for_event_or_clock (vm, |
| 1051 | (f64) rm->expire_walk_interval_ms |
| 1052 | / (f64) MSEC_PER_SEC); |
| 1053 | event_type = vlib_process_get_events (vm, &event_data); |
| 1054 | |
| 1055 | switch (event_type) |
| 1056 | { |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 1057 | case ~0: |
| 1058 | /* no events => timeout */ |
| 1059 | /* fallthrough */ |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1060 | case IP6_EVENT_CONFIG_CHANGED: |
Klement Sekera | 42cec0e | 2021-08-02 16:14:15 +0200 | [diff] [blame] | 1061 | /* nothing to do here */ |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1062 | break; |
| 1063 | default: |
| 1064 | clib_warning ("BUG: event type 0x%wx", event_type); |
| 1065 | break; |
| 1066 | } |
| 1067 | f64 now = vlib_time_now (vm); |
| 1068 | |
| 1069 | ip6_sv_reass_t *reass; |
| 1070 | int *pool_indexes_to_free = NULL; |
| 1071 | |
| 1072 | uword thread_index = 0; |
| 1073 | int index; |
| 1074 | const uword nthreads = vlib_num_workers () + 1; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1075 | for (thread_index = 0; thread_index < nthreads; ++thread_index) |
| 1076 | { |
| 1077 | ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index]; |
| 1078 | clib_spinlock_lock (&rt->lock); |
| 1079 | |
| 1080 | vec_reset_length (pool_indexes_to_free); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1081 | pool_foreach_index (index, rt->pool) { |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1082 | reass = pool_elt_at_index (rt->pool, index); |
| 1083 | if (now > reass->last_heard + rm->timeout) |
| 1084 | { |
| 1085 | vec_add1 (pool_indexes_to_free, index); |
| 1086 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1087 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1088 | int *i; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1089 | vec_foreach (i, pool_indexes_to_free) |
| 1090 | { |
| 1091 | ip6_sv_reass_t *reass = pool_elt_at_index (rt->pool, i[0]); |
| 1092 | ip6_sv_reass_free (vm, rm, rt, reass); |
| 1093 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1094 | |
| 1095 | clib_spinlock_unlock (&rt->lock); |
| 1096 | } |
| 1097 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1098 | vec_free (pool_indexes_to_free); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1099 | if (event_data) |
| 1100 | { |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1101 | vec_set_len (event_data, 0); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1108 | VLIB_REGISTER_NODE (ip6_sv_reass_expire_node) = { |
Neale Ranns | e22a704 | 2022-08-09 03:03:29 +0000 | [diff] [blame] | 1109 | .function = ip6_sv_reass_walk_expired, |
| 1110 | .format_trace = format_ip6_sv_reass_trace, |
| 1111 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1112 | .name = "ip6-sv-reassembly-expire-walk", |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1113 | |
Neale Ranns | e22a704 | 2022-08-09 03:03:29 +0000 | [diff] [blame] | 1114 | .n_errors = IP6_N_ERROR, |
| 1115 | .error_counters = ip6_error_counters, |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1116 | }; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1117 | |
| 1118 | static u8 * |
| 1119 | format_ip6_sv_reass_key (u8 * s, va_list * args) |
| 1120 | { |
| 1121 | ip6_sv_reass_key_t *key = va_arg (*args, ip6_sv_reass_key_t *); |
Klement Sekera | 5a58e7c | 2021-11-03 17:57:28 +0100 | [diff] [blame] | 1122 | s = |
| 1123 | format (s, "fib_index: %u, src: %U, dst: %U, frag_id: %u, proto: %u", |
| 1124 | key->fib_index, format_ip6_address, &key->src, format_ip6_address, |
| 1125 | &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1126 | return s; |
| 1127 | } |
| 1128 | |
| 1129 | static u8 * |
| 1130 | format_ip6_sv_reass (u8 * s, va_list * args) |
| 1131 | { |
| 1132 | vlib_main_t *vm = va_arg (*args, vlib_main_t *); |
| 1133 | ip6_sv_reass_t *reass = va_arg (*args, ip6_sv_reass_t *); |
| 1134 | |
| 1135 | s = format (s, "ID: %lu, key: %U, trace_op_counter: %u\n", |
| 1136 | reass->id, format_ip6_sv_reass_key, &reass->key, |
| 1137 | reass->trace_op_counter); |
| 1138 | vlib_buffer_t *b; |
| 1139 | u32 *bip; |
| 1140 | u32 counter = 0; |
| 1141 | vec_foreach (bip, reass->cached_buffers) |
| 1142 | { |
| 1143 | u32 bi = *bip; |
| 1144 | do |
| 1145 | { |
| 1146 | b = vlib_get_buffer (vm, bi); |
| 1147 | s = format (s, " #%03u: bi: %u\n", counter, bi); |
| 1148 | ++counter; |
| 1149 | bi = b->next_buffer; |
| 1150 | } |
| 1151 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 1152 | } |
| 1153 | return s; |
| 1154 | } |
| 1155 | |
| 1156 | static clib_error_t * |
| 1157 | show_ip6_sv_reass (vlib_main_t * vm, unformat_input_t * input, |
| 1158 | CLIB_UNUSED (vlib_cli_command_t * lmd)) |
| 1159 | { |
| 1160 | ip6_sv_reass_main_t *rm = &ip6_sv_reass_main; |
| 1161 | |
| 1162 | vlib_cli_output (vm, "---------------------"); |
| 1163 | vlib_cli_output (vm, "IP6 reassembly status"); |
| 1164 | vlib_cli_output (vm, "---------------------"); |
| 1165 | bool details = false; |
| 1166 | if (unformat (input, "details")) |
| 1167 | { |
| 1168 | details = true; |
| 1169 | } |
| 1170 | |
| 1171 | u32 sum_reass_n = 0; |
| 1172 | u64 sum_buffers_n = 0; |
| 1173 | ip6_sv_reass_t *reass; |
| 1174 | uword thread_index; |
| 1175 | const uword nthreads = vlib_num_workers () + 1; |
| 1176 | for (thread_index = 0; thread_index < nthreads; ++thread_index) |
| 1177 | { |
| 1178 | ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index]; |
| 1179 | clib_spinlock_lock (&rt->lock); |
| 1180 | if (details) |
| 1181 | { |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1182 | pool_foreach (reass, rt->pool) { |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1183 | vlib_cli_output (vm, "%U", format_ip6_sv_reass, vm, reass); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1184 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1185 | } |
| 1186 | sum_reass_n += rt->reass_n; |
| 1187 | clib_spinlock_unlock (&rt->lock); |
| 1188 | } |
| 1189 | vlib_cli_output (vm, "---------------------"); |
| 1190 | vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n", |
| 1191 | (long unsigned) sum_reass_n); |
Vladimir Ratnikov | a877cf9 | 2019-12-21 06:27:52 -0500 | [diff] [blame] | 1192 | vlib_cli_output (vm, |
| 1193 | "Maximum configured concurrent shallow virtual IP6 reassemblies per worker-thread: %lu\n", |
| 1194 | (long unsigned) rm->max_reass_n); |
| 1195 | vlib_cli_output (vm, |
Anton Nikolaev | 74a4a70 | 2021-02-17 14:45:40 +0500 | [diff] [blame] | 1196 | "Maximum configured amount of fragments per shallow " |
| 1197 | "virtual IP6 reassembly: %lu\n", |
| 1198 | (long unsigned) rm->max_reass_len); |
| 1199 | vlib_cli_output (vm, |
Vladimir Ratnikov | a877cf9 | 2019-12-21 06:27:52 -0500 | [diff] [blame] | 1200 | "Maximum configured shallow virtual IP6 reassembly timeout: %lums\n", |
| 1201 | (long unsigned) rm->timeout_ms); |
| 1202 | vlib_cli_output (vm, |
| 1203 | "Maximum configured shallow virtual IP6 reassembly expire walk interval: %lums\n", |
| 1204 | (long unsigned) rm->expire_walk_interval_ms); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1205 | vlib_cli_output (vm, "Buffers in use: %lu\n", |
| 1206 | (long unsigned) sum_buffers_n); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1210 | VLIB_CLI_COMMAND (show_ip6_sv_reassembly_cmd, static) = { |
| 1211 | .path = "show ip6-sv-reassembly", |
| 1212 | .short_help = "show ip6-sv-reassembly [details]", |
| 1213 | .function = show_ip6_sv_reass, |
| 1214 | }; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1215 | |
| 1216 | #ifndef CLIB_MARCH_VARIANT |
| 1217 | vnet_api_error_t |
| 1218 | ip6_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable) |
| 1219 | { |
Klement Sekera | f126e74 | 2019-10-10 09:46:06 +0000 | [diff] [blame] | 1220 | return ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, |
| 1221 | enable_disable); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1222 | } |
| 1223 | #endif /* CLIB_MARCH_VARIANT */ |
| 1224 | |
| 1225 | #define foreach_ip6_sv_reassembly_handoff_error \ |
| 1226 | _(CONGESTION_DROP, "congestion drop") |
| 1227 | |
| 1228 | |
| 1229 | typedef enum |
| 1230 | { |
| 1231 | #define _(sym,str) IP6_SV_REASSEMBLY_HANDOFF_ERROR_##sym, |
| 1232 | foreach_ip6_sv_reassembly_handoff_error |
| 1233 | #undef _ |
| 1234 | IP6_SV_REASSEMBLY_HANDOFF_N_ERROR, |
| 1235 | } ip6_sv_reassembly_handoff_error_t; |
| 1236 | |
| 1237 | static char *ip6_sv_reassembly_handoff_error_strings[] = { |
| 1238 | #define _(sym,string) string, |
| 1239 | foreach_ip6_sv_reassembly_handoff_error |
| 1240 | #undef _ |
| 1241 | }; |
| 1242 | |
| 1243 | typedef struct |
| 1244 | { |
| 1245 | u32 next_worker_index; |
| 1246 | } ip6_sv_reassembly_handoff_trace_t; |
| 1247 | |
| 1248 | static u8 * |
| 1249 | format_ip6_sv_reassembly_handoff_trace (u8 * s, va_list * args) |
| 1250 | { |
| 1251 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 1252 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 1253 | ip6_sv_reassembly_handoff_trace_t *t = |
| 1254 | va_arg (*args, ip6_sv_reassembly_handoff_trace_t *); |
| 1255 | |
| 1256 | s = |
| 1257 | format (s, "ip6-sv-reassembly-handoff: next-worker %d", |
| 1258 | t->next_worker_index); |
| 1259 | |
| 1260 | return s; |
| 1261 | } |
| 1262 | |
| 1263 | always_inline uword |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1264 | ip6_sv_reassembly_handoff_inline (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 1265 | vlib_frame_t *frame, bool is_feature, |
| 1266 | bool custom_context) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1267 | { |
| 1268 | ip6_sv_reass_main_t *rm = &ip6_sv_reass_main; |
| 1269 | |
| 1270 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1271 | u32 n_enq, n_left_from, *from, *context; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1272 | u16 thread_indices[VLIB_FRAME_SIZE], *ti; |
| 1273 | u32 fq_index; |
| 1274 | |
| 1275 | from = vlib_frame_vector_args (frame); |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1276 | if (custom_context) |
| 1277 | context = vlib_frame_aux_args (frame); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1278 | n_left_from = frame->n_vectors; |
| 1279 | vlib_get_buffers (vm, from, bufs, n_left_from); |
| 1280 | |
| 1281 | b = bufs; |
| 1282 | ti = thread_indices; |
| 1283 | |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1284 | fq_index = (is_feature) ? |
| 1285 | rm->fq_feature_index : |
| 1286 | (custom_context ? rm->fq_custom_context_index : rm->fq_index); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1287 | |
| 1288 | while (n_left_from > 0) |
| 1289 | { |
| 1290 | ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index; |
| 1291 | |
| 1292 | if (PREDICT_FALSE |
| 1293 | ((node->flags & VLIB_NODE_FLAG_TRACE) |
| 1294 | && (b[0]->flags & VLIB_BUFFER_IS_TRACED))) |
| 1295 | { |
| 1296 | ip6_sv_reassembly_handoff_trace_t *t = |
| 1297 | vlib_add_trace (vm, node, b[0], sizeof (*t)); |
| 1298 | t->next_worker_index = ti[0]; |
| 1299 | } |
| 1300 | |
| 1301 | n_left_from -= 1; |
| 1302 | ti += 1; |
| 1303 | b += 1; |
| 1304 | } |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1305 | if (custom_context) |
| 1306 | n_enq = vlib_buffer_enqueue_to_thread_with_aux ( |
| 1307 | vm, node, fq_index, from, context, thread_indices, frame->n_vectors, 1); |
| 1308 | else |
| 1309 | n_enq = vlib_buffer_enqueue_to_thread ( |
| 1310 | vm, node, fq_index, from, thread_indices, frame->n_vectors, 1); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1311 | |
| 1312 | if (n_enq < frame->n_vectors) |
| 1313 | vlib_node_increment_counter (vm, node->node_index, |
| 1314 | IP6_SV_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP, |
| 1315 | frame->n_vectors - n_enq); |
| 1316 | return frame->n_vectors; |
| 1317 | } |
| 1318 | |
| 1319 | VLIB_NODE_FN (ip6_sv_reassembly_handoff_node) (vlib_main_t * vm, |
| 1320 | vlib_node_runtime_t * node, |
| 1321 | vlib_frame_t * frame) |
| 1322 | { |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1323 | return ip6_sv_reassembly_handoff_inline ( |
| 1324 | vm, node, frame, false /* is_feature */, false /* custom_context */); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1327 | VLIB_REGISTER_NODE (ip6_sv_reassembly_handoff_node) = { |
| 1328 | .name = "ip6-sv-reassembly-handoff", |
| 1329 | .vector_size = sizeof (u32), |
| 1330 | .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings), |
| 1331 | .error_strings = ip6_sv_reassembly_handoff_error_strings, |
| 1332 | .format_trace = format_ip6_sv_reassembly_handoff_trace, |
| 1333 | |
| 1334 | .n_next_nodes = 1, |
| 1335 | |
| 1336 | .next_nodes = { |
| 1337 | [0] = "error-drop", |
| 1338 | }, |
| 1339 | }; |
| 1340 | |
| 1341 | |
| 1342 | VLIB_NODE_FN (ip6_sv_reassembly_feature_handoff_node) (vlib_main_t * vm, |
| 1343 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 1344 | { |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1345 | return ip6_sv_reassembly_handoff_inline ( |
| 1346 | vm, node, frame, true /* is_feature */, false /* custom_context */); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1350 | VLIB_REGISTER_NODE (ip6_sv_reassembly_feature_handoff_node) = { |
| 1351 | .name = "ip6-sv-reass-feature-hoff", |
| 1352 | .vector_size = sizeof (u32), |
| 1353 | .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings), |
| 1354 | .error_strings = ip6_sv_reassembly_handoff_error_strings, |
| 1355 | .format_trace = format_ip6_sv_reassembly_handoff_trace, |
| 1356 | |
| 1357 | .n_next_nodes = 1, |
| 1358 | |
| 1359 | .next_nodes = { |
| 1360 | [0] = "error-drop", |
| 1361 | }, |
| 1362 | }; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1363 | |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1364 | VLIB_NODE_FN (ip6_sv_reassembly_custom_context_handoff_node) |
| 1365 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame) |
| 1366 | { |
| 1367 | return ip6_sv_reassembly_handoff_inline ( |
| 1368 | vm, node, frame, false /* is_feature */, true /* custom_context */); |
| 1369 | } |
| 1370 | |
| 1371 | VLIB_REGISTER_NODE (ip6_sv_reassembly_custom_context_handoff_node) = { |
| 1372 | .name = "ip6-sv-reassembly-custom-context-handoff", |
| 1373 | .vector_size = sizeof (u32), |
| 1374 | .aux_size = sizeof (u32), |
| 1375 | .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings), |
| 1376 | .error_strings = ip6_sv_reassembly_handoff_error_strings, |
| 1377 | .format_trace = format_ip6_sv_reassembly_handoff_trace, |
| 1378 | |
| 1379 | .n_next_nodes = 1, |
| 1380 | |
| 1381 | .next_nodes = { |
| 1382 | [0] = "error-drop", |
| 1383 | }, |
| 1384 | }; |
| 1385 | |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1386 | #ifndef CLIB_MARCH_VARIANT |
| 1387 | int |
| 1388 | ip6_sv_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable) |
| 1389 | { |
| 1390 | ip6_sv_reass_main_t *rm = &ip6_sv_reass_main; |
Klement Sekera | 63c7353 | 2019-09-30 14:35:36 +0000 | [diff] [blame] | 1391 | vec_validate (rm->feature_use_refcount_per_intf, sw_if_index); |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1392 | if (is_enable) |
| 1393 | { |
Klement Sekera | 63c7353 | 2019-09-30 14:35:36 +0000 | [diff] [blame] | 1394 | if (!rm->feature_use_refcount_per_intf[sw_if_index]) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1395 | { |
Klement Sekera | 63c7353 | 2019-09-30 14:35:36 +0000 | [diff] [blame] | 1396 | ++rm->feature_use_refcount_per_intf[sw_if_index]; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1397 | return vnet_feature_enable_disable ("ip6-unicast", |
| 1398 | "ip6-sv-reassembly-feature", |
| 1399 | sw_if_index, 1, 0, 0); |
| 1400 | } |
Klement Sekera | 63c7353 | 2019-09-30 14:35:36 +0000 | [diff] [blame] | 1401 | ++rm->feature_use_refcount_per_intf[sw_if_index]; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1402 | } |
| 1403 | else |
| 1404 | { |
Klement Sekera | 63c7353 | 2019-09-30 14:35:36 +0000 | [diff] [blame] | 1405 | --rm->feature_use_refcount_per_intf[sw_if_index]; |
| 1406 | if (!rm->feature_use_refcount_per_intf[sw_if_index]) |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1407 | return vnet_feature_enable_disable ("ip6-unicast", |
| 1408 | "ip6-sv-reassembly-feature", |
| 1409 | sw_if_index, 0, 0, 0); |
| 1410 | } |
Klement Sekera | 407f593 | 2019-12-11 13:06:27 +0000 | [diff] [blame] | 1411 | return 0; |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1412 | } |
Mohammed Hawari | 2805daa | 2022-06-20 15:28:31 +0200 | [diff] [blame] | 1413 | |
| 1414 | uword |
| 1415 | ip6_sv_reass_custom_context_register_next_node (uword node_index) |
| 1416 | { |
| 1417 | return vlib_node_add_next ( |
| 1418 | vlib_get_main (), ip6_sv_reassembly_custom_context_handoff_node.index, |
| 1419 | node_index); |
| 1420 | } |
Klement Sekera | de34c35 | 2019-06-25 11:19:22 +0000 | [diff] [blame] | 1421 | #endif |
| 1422 | |
| 1423 | /* |
| 1424 | * fd.io coding-style-patch-verification: ON |
| 1425 | * |
| 1426 | * Local Variables: |
| 1427 | * eval: (c-set-style "gnu") |
| 1428 | * End: |
| 1429 | */ |