blob: fe2ed05555cf978ac54dae1efcd07664babd712f [file] [log] [blame]
Klement Sekerade34c352019-06-25 11:19:22 +00001/*
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 Troan03092c12021-11-23 15:55:39 +010029#include <vnet/ip/ip6_inlines.h>
Klement Sekerade34c352019-06-25 11:19:22 +000030
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
38typedef 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 Sekera755042d2021-12-01 10:14:38 +000044 IP6_SV_REASS_RC_INVALID_FRAG_LEN,
Klement Sekerade34c352019-06-25 11:19:22 +000045} ip6_sv_reass_rc_t;
46
47typedef struct
48{
49 union
50 {
51 struct
52 {
53 ip6_address_t src;
54 ip6_address_t dst;
Klement Sekera5a58e7c2021-11-03 17:57:28 +010055 u32 fib_index;
Klement Sekerade34c352019-06-25 11:19:22 +000056 u32 frag_id;
57 u8 unused[7];
58 u8 proto;
59 };
60 u64 as_u64[6];
61 };
62} ip6_sv_reass_key_t;
63
64typedef 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
74typedef 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
84typedef 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 Sekeraf126e742019-10-10 09:46:06 +0000101 u8 icmp_type_or_tcp_flags;
102 u32 tcp_ack_number;
103 u32 tcp_seq_number;
Klement Sekerade34c352019-06-25 11:19:22 +0000104 // 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
113typedef 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
124typedef 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 Hawari2805daa2022-06-20 15:28:31 +0200153 u32 fq_custom_context_index;
Klement Sekerade34c352019-06-25 11:19:22 +0000154
Klement Sekera63c73532019-09-30 14:35:36 +0000155 // reference count for enabling/disabling feature - per interface
156 u32 *feature_use_refcount_per_intf;
Klement Sekerade34c352019-06-25 11:19:22 +0000157} ip6_sv_reass_main_t;
158
159extern ip6_sv_reass_main_t ip6_sv_reass_main;
160
161#ifndef CLIB_MARCH_VARIANT
162ip6_sv_reass_main_t ip6_sv_reass_main;
163#endif /* CLIB_MARCH_VARIANT */
164
165typedef 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
174typedef enum
175{
Klement Sekerade34c352019-06-25 11:19:22 +0000176 REASS_FRAGMENT_CACHE,
177 REASS_FINISH,
178 REASS_FRAGMENT_FORWARD,
Klement Sekeraf126e742019-10-10 09:46:06 +0000179 REASS_PASSTHROUGH,
Klement Sekerade34c352019-06-25 11:19:22 +0000180} ip6_sv_reass_trace_operation_e;
181
182typedef 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
192static u8 *
193format_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 Sekeraf126e742019-10-10 09:46:06 +0000198 if (REASS_PASSTHROUGH != t->action)
199 {
200 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
201 }
Klement Sekerade34c352019-06-25 11:19:22 +0000202 switch (t->action)
203 {
Klement Sekerade34c352019-06-25 11:19:22 +0000204 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 Sekeraf126e742019-10-10 09:46:06 +0000219 case REASS_PASSTHROUGH:
Klement Sekera7c3275e2021-12-07 09:49:53 +0000220 s = format (s, "[not fragmented or atomic fragment]");
Klement Sekeraf126e742019-10-10 09:46:06 +0000221 break;
Klement Sekerade34c352019-06-25 11:19:22 +0000222 }
223 return s;
224}
225
226static void
227ip6_sv_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekerade34c352019-06-25 11:19:22 +0000228 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 Sekera53be16d2020-12-15 21:47:36 +0100233 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 Sekerade34c352019-06-25 11:19:22 +0000240 ip6_sv_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekeraf126e742019-10-10 09:46:06 +0000241 if (reass)
242 {
243 t->reass_id = reass->id;
244 t->op_id = reass->trace_op_counter;
245 ++reass->trace_op_counter;
246 }
Klement Sekerade34c352019-06-25 11:19:22 +0000247 t->action = action;
Klement Sekerade34c352019-06-25 11:19:22 +0000248 t->ip_proto = ip_proto;
249 t->l4_src_port = l4_src_port;
250 t->l4_dst_port = l4_dst_port;
Klement Sekerade34c352019-06-25 11:19:22 +0000251#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
260always_inline void
261ip6_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
300always_inline void
301ip6_sv_reass_init (ip6_sv_reass_t * reass)
302{
303 reass->cached_buffers = NULL;
304 reass->is_complete = false;
305}
306
307always_inline ip6_sv_reass_t *
Klement Sekera42cec0e2021-08-02 16:14:15 +0200308ip6_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 Sekerade34c352019-06-25 11:19:22 +0000311{
312 ip6_sv_reass_t *reass = NULL;
Tom Seidenberg5a7f2f12020-04-28 17:58:12 -0400313 f64 now = vlib_time_now (vm);
Klement Sekerade34c352019-06-25 11:19:22 +0000314
Klement Sekera4ab6ad12021-12-17 12:47:51 +0000315again:
316
Klement Sekerac99c0252019-12-18 12:17:06 +0000317 if (!clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekerade34c352019-06-25 11:19:22 +0000318 {
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 {
zhengdelunce533632020-05-22 15:42:28 +0800341 reass = pool_elt_at_index (rt->pool, rt->lru_first);
Klement Sekerade34c352019-06-25 11:19:22 +0000342 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 Sekerac99c0252019-12-18 12:17:06 +0000366 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 Sekerade34c352019-06-25 11:19:22 +0000372 kv->v.reass_index = (reass - rt->pool);
373 kv->v.thread_index = vm->thread_index;
374 reass->last_heard = now;
375
Klement Sekera4ab6ad12021-12-17 12:47:51 +0000376 int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
377 if (rv)
Klement Sekerade34c352019-06-25 11:19:22 +0000378 {
379 ip6_sv_reass_free (vm, rm, rt, reass);
380 reass = NULL;
Klement Sekera4ab6ad12021-12-17 12:47:51 +0000381 // if other worker created a context already work with the other copy
382 if (-2 == rv)
383 goto again;
Klement Sekerade34c352019-06-25 11:19:22 +0000384 }
385
386 return reass;
387}
388
389always_inline ip6_sv_reass_rc_t
Klement Sekera42cec0e2021-08-02 16:14:15 +0200390ip6_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 Sekerade34c352019-06-25 11:19:22 +0000393{
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 Sekera755042d2021-12-01 10:14:38 +0000411 if (0 == fragment_length)
412 {
413 return IP6_SV_REASS_RC_INVALID_FRAG_LEN;
414 }
Klement Sekerade34c352019-06-25 11:19:22 +0000415 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 Sekeraf126e742019-10-10 09:46:06 +0000422 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 Sekerade34c352019-06-25 11:19:22 +0000427 return IP6_SV_REASS_RC_UNSUPP_IP_PROTO;
Klement Sekeraf126e742019-10-10 09:46:06 +0000428
Klement Sekerade34c352019-06-25 11:19:22 +0000429 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 Sekera42cec0e2021-08-02 16:14:15 +0200433 ip6_sv_reass_add_trace (vm, node, reass, bi0, REASS_FINISH,
Klement Sekerade34c352019-06-25 11:19:22 +0000434 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 Sekera42cec0e2021-08-02 16:14:15 +0200443 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 Sekerade34c352019-06-25 11:19:22 +0000446 }
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
455always_inline bool
Ole Troan03092c12021-11-23 15:55:39 +0100456ip6_sv_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
457 vlib_buffer_t *b,
458 ip6_ext_hdr_chain_t *hc)
Klement Sekerade34c352019-06-25 11:19:22 +0000459{
Ole Troan03092c12021-11-23 15:55:39 +0100460 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 Sekerade34c352019-06-25 11:19:22 +0000463 {
Ole Troan03092c12021-11-23 15:55:39 +0100464 icmp6_error_set_vnet_buffer (
465 b, ICMP6_parameter_problem,
466 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
Klement Sekerade34c352019-06-25 11:19:22 +0000467 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
Klement Sekerade34c352019-06-25 11:19:22 +0000468 return false;
469 }
470 return true;
471}
472
473always_inline bool
474ip6_sv_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
Klement Sekerade34c352019-06-25 11:19:22 +0000475 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
494always_inline bool
495ip6_sv_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
Klement Sekerade34c352019-06-25 11:19:22 +0000496 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
516always_inline uword
Mohammed Hawari2805daa2022-06-20 15:28:31 +0200517ip6_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 Sekerade34c352019-06-25 11:19:22 +0000520{
521 u32 *from = vlib_frame_vector_args (frame);
Mohammed Hawari2805daa2022-06-20 15:28:31 +0200522 u32 n_left_from, n_left_to_next, *to_next, *to_next_aux, next_index;
Klement Sekerade34c352019-06-25 11:19:22 +0000523 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 Hawari2805daa2022-06-20 15:28:31 +0200525 u32 *context;
526 if (custom_context)
527 context = vlib_frame_aux_args (frame);
528
Klement Sekerade34c352019-06-25 11:19:22 +0000529 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 Hawari2805daa2022-06-20 15:28:31 +0200536 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 Sekerade34c352019-06-25 11:19:22 +0000541
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 Hawari2805daa2022-06-20 15:28:31 +0200548 u8 forward_context = 0;
Klement Sekerade34c352019-06-25 11:19:22 +0000549 bi0 = from[0];
550 b0 = vlib_get_buffer (vm, bi0);
551
552 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Ole Troan03092c12021-11-23 15:55:39 +0100553 ip6_frag_hdr_t *frag_hdr;
554 ip6_ext_hdr_chain_t hdr_chain;
Klement Sekera7c3275e2021-12-07 09:49:53 +0000555 bool is_atomic_fragment = false;
Ole Troan03092c12021-11-23 15:55:39 +0100556
557 int res = ip6_ext_header_walk (
558 b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
Klement Sekera7c3275e2021-12-07 09:49:53 +0000559 if (res >= 0 &&
560 hdr_chain.eh[res].protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
Klement Sekerade34c352019-06-25 11:19:22 +0000561 {
Klement Sekera7c3275e2021-12-07 09:49:53 +0000562 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 Sekeraf126e742019-10-10 09:46:06 +0000573 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 Sekera1766ddc2020-03-30 16:59:38 +0200583 b0->error = node->errors[error0];
Klement Sekeraf126e742019-10-10 09:46:06 +0000584 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
585 goto packet_enqueue;
586 }
Klement Sekeraf126e742019-10-10 09:46:06 +0000587 vnet_buffer (b0)->ip.reass.is_non_first_fragment = 0;
Mohammed Hawari2805daa2022-06-20 15:28:31 +0200588 next0 = custom_next ? vnet_buffer (b0)->ip.reass.next_index :
589 IP6_SV_REASSEMBLY_NEXT_INPUT;
Klement Sekeraf126e742019-10-10 09:46:06 +0000590 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
591 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200592 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 Sekeraf126e742019-10-10 09:46:06 +0000597 }
Klement Sekerade34c352019-06-25 11:19:22 +0000598 goto packet_enqueue;
599 }
Klement Sekera7c3275e2021-12-07 09:49:53 +0000600
Klement Sekera38f7ccb2019-10-28 11:26:28 +0000601 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
Ole Troan03092c12021-11-23 15:55:39 +0100602 hdr_chain.eh[res].offset;
Klement Sekera7c3275e2021-12-07 09:49:53 +0000603
Klement Sekerade34c352019-06-25 11:19:22 +0000604 if (0 == ip6_frag_hdr_offset (frag_hdr))
605 {
606 // first fragment - verify upper-layer is present
Ole Troan03092c12021-11-23 15:55:39 +0100607 if (!ip6_sv_reass_verify_upper_layer_present (node, b0,
608 &hdr_chain))
Klement Sekerade34c352019-06-25 11:19:22 +0000609 {
610 next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
611 goto packet_enqueue;
612 }
613 }
Klement Sekera42cec0e2021-08-02 16:14:15 +0200614 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 Sekerade34c352019-06-25 11:19:22 +0000616 {
617 next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
618 goto packet_enqueue;
619 }
Klement Sekerade34c352019-06-25 11:19:22 +0000620
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 Hawari2805daa2022-06-20 15:28:31 +0200628 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 Sekerade34c352019-06-25 11:19:22 +0000637 kv.k.as_u64[5] = ip0->protocol;
638
639 ip6_sv_reass_t *reass =
Klement Sekera42cec0e2021-08-02 16:14:15 +0200640 ip6_sv_reass_find_or_create (vm, rm, rt, &kv, &do_handoff);
Klement Sekerade34c352019-06-25 11:19:22 +0000641
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 Hawari2805daa2022-06-20 15:28:31 +0200647 if (custom_context)
648 forward_context = 1;
Klement Sekera364b20a2019-10-07 09:48:06 +0000649 goto packet_enqueue;
Klement Sekerade34c352019-06-25 11:19:22 +0000650 }
651
652 if (!reass)
653 {
654 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
655 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
Klement Sekera1766ddc2020-03-30 16:59:38 +0200656 b0->error = node->errors[error0];
Klement Sekerade34c352019-06-25 11:19:22 +0000657 goto packet_enqueue;
658 }
659
660 if (reass->is_complete)
661 {
Klement Sekeraf126e742019-10-10 09:46:06 +0000662 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
663 ! !ip6_frag_hdr_offset (frag_hdr);
Klement Sekerade34c352019-06-25 11:19:22 +0000664 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +0000665 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 Sekerade34c352019-06-25 11:19:22 +0000671 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 Hawari2805daa2022-06-20 15:28:31 +0200673 next0 = custom_next ? vnet_buffer (b0)->ip.reass.next_index :
674 IP6_SV_REASSEMBLY_NEXT_INPUT;
Klement Sekerade34c352019-06-25 11:19:22 +0000675 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
676 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200677 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 Sekerade34c352019-06-25 11:19:22 +0000680 }
681 goto packet_enqueue;
682 }
683
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100684 u32 counter = ~0;
Klement Sekera42cec0e2021-08-02 16:14:15 +0200685 switch (ip6_sv_reass_update (vm, node, rm, reass, bi0, frag_hdr))
Klement Sekerade34c352019-06-25 11:19:22 +0000686 {
687 case IP6_SV_REASS_RC_OK:
688 /* nothing to do here */
689 break;
690 case IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS:
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100691 counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
Klement Sekerade34c352019-06-25 11:19:22 +0000692 break;
693 case IP6_SV_REASS_RC_UNSUPP_IP_PROTO:
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100694 counter = IP6_ERROR_REASS_UNSUPP_IP_PROTO;
Klement Sekerade34c352019-06-25 11:19:22 +0000695 break;
696 case IP6_SV_REASS_RC_INTERNAL_ERROR:
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100697 counter = IP6_ERROR_REASS_INTERNAL_ERROR;
698 break;
Klement Sekera755042d2021-12-01 10:14:38 +0000699 case IP6_SV_REASS_RC_INVALID_FRAG_LEN:
700 counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
701 break;
Klement Sekerad4ba0d12021-11-02 15:33:55 +0100702 }
703 if (~0 != counter)
704 {
705 vlib_node_increment_counter (vm, node->node_index, counter, 1);
Klement Sekerade34c352019-06-25 11:19:22 +0000706 ip6_sv_reass_free (vm, rm, rt, reass);
707 goto next_packet;
Klement Sekerade34c352019-06-25 11:19:22 +0000708 }
709
Klement Sekerade34c352019-06-25 11:19:22 +0000710 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 Sekeraf126e742019-10-10 09:46:06 +0000731 frag_hdr =
732 vlib_buffer_get_current (b0) +
733 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset;
Klement Sekeraf126e742019-10-10 09:46:06 +0000734 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
735 ! !ip6_frag_hdr_offset (frag_hdr);
Klement Sekerade34c352019-06-25 11:19:22 +0000736 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +0000737 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 Sekerade34c352019-06-25 11:19:22 +0000743 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 Sekera42cec0e2021-08-02 16:14:15 +0200747 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 Sekerade34c352019-06-25 11:19:22 +0000750 }
751 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
752 to_next, n_left_to_next, bi0,
753 next0);
754 }
Damjan Marion8bea5892022-04-04 22:40:45 +0200755 vec_set_len (reass->cached_buffers,
756 0); // buffers are owned by frame now
Klement Sekerade34c352019-06-25 11:19:22 +0000757 }
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 Hawari2805daa2022-06-20 15:28:31 +0200769 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 Sekerade34c352019-06-25 11:19:22 +0000783
Klement Sekerade34c352019-06-25 11:19:22 +0000784 next_packet:
785 from += 1;
Mohammed Hawari2805daa2022-06-20 15:28:31 +0200786 if (custom_context)
787 context += 1;
Klement Sekerade34c352019-06-25 11:19:22 +0000788 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 Sekerade34c352019-06-25 11:19:22 +0000798VLIB_NODE_FN (ip6_sv_reass_node) (vlib_main_t * vm,
799 vlib_node_runtime_t * node,
800 vlib_frame_t * frame)
801{
Mohammed Hawari2805daa2022-06-20 15:28:31 +0200802 return ip6_sv_reassembly_inline (vm, node, frame, false /* is_feature */,
803 false /* custom next */,
804 false /* custom context */);
Klement Sekerade34c352019-06-25 11:19:22 +0000805}
806
Klement Sekerade34c352019-06-25 11:19:22 +0000807VLIB_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 Rannse22a7042022-08-09 03:03:29 +0000811 .n_errors = IP6_N_ERROR,
812 .error_counters = ip6_error_counters,
Klement Sekerade34c352019-06-25 11:19:22 +0000813 .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 Sekerade34c352019-06-25 11:19:22 +0000822
823VLIB_NODE_FN (ip6_sv_reass_node_feature) (vlib_main_t * vm,
824 vlib_node_runtime_t * node,
825 vlib_frame_t * frame)
826{
Mohammed Hawari2805daa2022-06-20 15:28:31 +0200827 return ip6_sv_reassembly_inline (vm, node, frame, true /* is_feature */,
828 false /* custom next */,
829 false /* custom context */);
Klement Sekerade34c352019-06-25 11:19:22 +0000830}
831
Klement Sekerade34c352019-06-25 11:19:22 +0000832VLIB_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 Rannse22a7042022-08-09 03:03:29 +0000836 .n_errors = IP6_N_ERROR,
837 .error_counters = ip6_error_counters,
Klement Sekerade34c352019-06-25 11:19:22 +0000838 .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 Sekerade34c352019-06-25 11:19:22 +0000847
Klement Sekerade34c352019-06-25 11:19:22 +0000848VNET_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 Sekerade34c352019-06-25 11:19:22 +0000854
Mohammed Hawari2805daa2022-06-20 15:28:31 +0200855VLIB_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
863VLIB_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 Sekerade34c352019-06-25 11:19:22 +0000880#ifndef CLIB_MARCH_VARIANT
881static u32
882ip6_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
899typedef enum
900{
901 IP6_EVENT_CONFIG_CHANGED = 1,
902} ip6_sv_reass_event_t;
903
904#ifndef CLIB_MARCH_VARIANT
905typedef struct
906{
907 int failure;
908 clib_bihash_48_8_t *new_hash;
909} ip6_rehash_cb_ctx;
910
Neale Rannsf50bac12019-12-06 05:53:17 +0000911static int
Klement Sekerade34c352019-06-25 11:19:22 +0000912ip6_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 Rannsf50bac12019-12-06 05:53:17 +0000919 return (BIHASH_WALK_CONTINUE);
Klement Sekerade34c352019-06-25 11:19:22 +0000920}
921
922static void
923ip6_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
934vnet_api_error_t
935ip6_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
972vnet_api_error_t
973ip6_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
983static clib_error_t *
984ip6_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 Sekerade34c352019-06-25 11:19:22 +00001025
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 Hawari2805daa2022-06-20 15:28:31 +02001029 rm->fq_custom_context_index =
1030 vlib_frame_queue_main_init (ip6_sv_reass_custom_context_node.index, 0);
Klement Sekerade34c352019-06-25 11:19:22 +00001031
Klement Sekera63c73532019-09-30 14:35:36 +00001032 rm->feature_use_refcount_per_intf = NULL;
1033
Klement Sekerade34c352019-06-25 11:19:22 +00001034 return error;
1035}
1036
1037VLIB_INIT_FUNCTION (ip6_sv_reass_init_function);
1038#endif /* CLIB_MARCH_VARIANT */
1039
1040static uword
Klement Sekera42cec0e2021-08-02 16:14:15 +02001041ip6_sv_reass_walk_expired (vlib_main_t *vm,
1042 CLIB_UNUSED (vlib_node_runtime_t *node),
1043 CLIB_UNUSED (vlib_frame_t *f))
Klement Sekerade34c352019-06-25 11:19:22 +00001044{
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 Sekera42cec0e2021-08-02 16:14:15 +02001057 case ~0:
1058 /* no events => timeout */
1059 /* fallthrough */
Klement Sekerade34c352019-06-25 11:19:22 +00001060 case IP6_EVENT_CONFIG_CHANGED:
Klement Sekera42cec0e2021-08-02 16:14:15 +02001061 /* nothing to do here */
Klement Sekerade34c352019-06-25 11:19:22 +00001062 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 Sekerade34c352019-06-25 11:19:22 +00001075 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 Marionb2c31b62020-12-13 21:47:40 +01001081 pool_foreach_index (index, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001082 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 Marionb2c31b62020-12-13 21:47:40 +01001087 }
Klement Sekerade34c352019-06-25 11:19:22 +00001088 int *i;
Klement Sekerade34c352019-06-25 11:19:22 +00001089 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 Sekerade34c352019-06-25 11:19:22 +00001094
1095 clib_spinlock_unlock (&rt->lock);
1096 }
1097
Klement Sekerade34c352019-06-25 11:19:22 +00001098 vec_free (pool_indexes_to_free);
Klement Sekerade34c352019-06-25 11:19:22 +00001099 if (event_data)
1100 {
Damjan Marion8bea5892022-04-04 22:40:45 +02001101 vec_set_len (event_data, 0);
Klement Sekerade34c352019-06-25 11:19:22 +00001102 }
1103 }
1104
1105 return 0;
1106}
1107
Klement Sekerade34c352019-06-25 11:19:22 +00001108VLIB_REGISTER_NODE (ip6_sv_reass_expire_node) = {
Neale Rannse22a7042022-08-09 03:03:29 +00001109 .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 Sekerade34c352019-06-25 11:19:22 +00001113
Neale Rannse22a7042022-08-09 03:03:29 +00001114 .n_errors = IP6_N_ERROR,
1115 .error_counters = ip6_error_counters,
Klement Sekerade34c352019-06-25 11:19:22 +00001116};
Klement Sekerade34c352019-06-25 11:19:22 +00001117
1118static u8 *
1119format_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 Sekera5a58e7c2021-11-03 17:57:28 +01001122 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 Sekerade34c352019-06-25 11:19:22 +00001126 return s;
1127}
1128
1129static u8 *
1130format_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
1156static clib_error_t *
1157show_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 Marionb2c31b62020-12-13 21:47:40 +01001182 pool_foreach (reass, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001183 vlib_cli_output (vm, "%U", format_ip6_sv_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001184 }
Klement Sekerade34c352019-06-25 11:19:22 +00001185 }
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 Ratnikova877cf92019-12-21 06:27:52 -05001192 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 Nikolaev74a4a702021-02-17 14:45:40 +05001196 "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 Ratnikova877cf92019-12-21 06:27:52 -05001200 "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 Sekerade34c352019-06-25 11:19:22 +00001205 vlib_cli_output (vm, "Buffers in use: %lu\n",
1206 (long unsigned) sum_buffers_n);
1207 return 0;
1208}
1209
Klement Sekerade34c352019-06-25 11:19:22 +00001210VLIB_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 Sekerade34c352019-06-25 11:19:22 +00001215
1216#ifndef CLIB_MARCH_VARIANT
1217vnet_api_error_t
1218ip6_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1219{
Klement Sekeraf126e742019-10-10 09:46:06 +00001220 return ip6_sv_reass_enable_disable_with_refcnt (sw_if_index,
1221 enable_disable);
Klement Sekerade34c352019-06-25 11:19:22 +00001222}
1223#endif /* CLIB_MARCH_VARIANT */
1224
1225#define foreach_ip6_sv_reassembly_handoff_error \
1226_(CONGESTION_DROP, "congestion drop")
1227
1228
1229typedef 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
1237static char *ip6_sv_reassembly_handoff_error_strings[] = {
1238#define _(sym,string) string,
1239 foreach_ip6_sv_reassembly_handoff_error
1240#undef _
1241};
1242
1243typedef struct
1244{
1245 u32 next_worker_index;
1246} ip6_sv_reassembly_handoff_trace_t;
1247
1248static u8 *
1249format_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
1263always_inline uword
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001264ip6_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 Sekerade34c352019-06-25 11:19:22 +00001267{
1268 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1269
1270 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001271 u32 n_enq, n_left_from, *from, *context;
Klement Sekerade34c352019-06-25 11:19:22 +00001272 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1273 u32 fq_index;
1274
1275 from = vlib_frame_vector_args (frame);
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001276 if (custom_context)
1277 context = vlib_frame_aux_args (frame);
Klement Sekerade34c352019-06-25 11:19:22 +00001278 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 Hawari2805daa2022-06-20 15:28:31 +02001284 fq_index = (is_feature) ?
1285 rm->fq_feature_index :
1286 (custom_context ? rm->fq_custom_context_index : rm->fq_index);
Klement Sekerade34c352019-06-25 11:19:22 +00001287
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 Hawari2805daa2022-06-20 15:28:31 +02001305 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 Sekerade34c352019-06-25 11:19:22 +00001311
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
1319VLIB_NODE_FN (ip6_sv_reassembly_handoff_node) (vlib_main_t * vm,
1320 vlib_node_runtime_t * node,
1321 vlib_frame_t * frame)
1322{
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001323 return ip6_sv_reassembly_handoff_inline (
1324 vm, node, frame, false /* is_feature */, false /* custom_context */);
Klement Sekerade34c352019-06-25 11:19:22 +00001325}
1326
Klement Sekerade34c352019-06-25 11:19:22 +00001327VLIB_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
1342VLIB_NODE_FN (ip6_sv_reassembly_feature_handoff_node) (vlib_main_t * vm,
1343 vlib_node_runtime_t * node, vlib_frame_t * frame)
1344{
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001345 return ip6_sv_reassembly_handoff_inline (
1346 vm, node, frame, true /* is_feature */, false /* custom_context */);
Klement Sekerade34c352019-06-25 11:19:22 +00001347}
1348
1349
Klement Sekerade34c352019-06-25 11:19:22 +00001350VLIB_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 Sekerade34c352019-06-25 11:19:22 +00001363
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001364VLIB_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
1371VLIB_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 Sekerade34c352019-06-25 11:19:22 +00001386#ifndef CLIB_MARCH_VARIANT
1387int
1388ip6_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 Sekera63c73532019-09-30 14:35:36 +00001391 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
Klement Sekerade34c352019-06-25 11:19:22 +00001392 if (is_enable)
1393 {
Klement Sekera63c73532019-09-30 14:35:36 +00001394 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001395 {
Klement Sekera63c73532019-09-30 14:35:36 +00001396 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001397 return vnet_feature_enable_disable ("ip6-unicast",
1398 "ip6-sv-reassembly-feature",
1399 sw_if_index, 1, 0, 0);
1400 }
Klement Sekera63c73532019-09-30 14:35:36 +00001401 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001402 }
1403 else
1404 {
Klement Sekera63c73532019-09-30 14:35:36 +00001405 --rm->feature_use_refcount_per_intf[sw_if_index];
1406 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001407 return vnet_feature_enable_disable ("ip6-unicast",
1408 "ip6-sv-reassembly-feature",
1409 sw_if_index, 0, 0, 0);
1410 }
Klement Sekera407f5932019-12-11 13:06:27 +00001411 return 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001412}
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001413
1414uword
1415ip6_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 Sekerade34c352019-06-25 11:19:22 +00001421#endif
1422
1423/*
1424 * fd.io coding-style-patch-verification: ON
1425 *
1426 * Local Variables:
1427 * eval: (c-set-style "gnu")
1428 * End:
1429 */