blob: 0a17cc13f452d5eb66a6aac8fc5fcf893302318f [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
807/* *INDENT-OFF* */
808VLIB_REGISTER_NODE (ip6_sv_reass_node) = {
809 .name = "ip6-sv-reassembly",
810 .vector_size = sizeof (u32),
811 .format_trace = format_ip6_sv_reass_trace,
Neale Rannse22a7042022-08-09 03:03:29 +0000812 .n_errors = IP6_N_ERROR,
813 .error_counters = ip6_error_counters,
Klement Sekerade34c352019-06-25 11:19:22 +0000814 .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
815 .next_nodes =
816 {
817 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
818 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
819 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
820 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reassembly-handoff",
821 },
822};
823/* *INDENT-ON* */
824
825VLIB_NODE_FN (ip6_sv_reass_node_feature) (vlib_main_t * vm,
826 vlib_node_runtime_t * node,
827 vlib_frame_t * frame)
828{
Mohammed Hawari2805daa2022-06-20 15:28:31 +0200829 return ip6_sv_reassembly_inline (vm, node, frame, true /* is_feature */,
830 false /* custom next */,
831 false /* custom context */);
Klement Sekerade34c352019-06-25 11:19:22 +0000832}
833
834/* *INDENT-OFF* */
835VLIB_REGISTER_NODE (ip6_sv_reass_node_feature) = {
836 .name = "ip6-sv-reassembly-feature",
837 .vector_size = sizeof (u32),
838 .format_trace = format_ip6_sv_reass_trace,
Neale Rannse22a7042022-08-09 03:03:29 +0000839 .n_errors = IP6_N_ERROR,
840 .error_counters = ip6_error_counters,
Klement Sekerade34c352019-06-25 11:19:22 +0000841 .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
842 .next_nodes =
843 {
844 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
845 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
846 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
847 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reass-feature-hoff",
848 },
849};
850/* *INDENT-ON* */
851
852/* *INDENT-OFF* */
853VNET_FEATURE_INIT (ip6_sv_reassembly_feature) = {
854 .arc_name = "ip6-unicast",
855 .node_name = "ip6-sv-reassembly-feature",
856 .runs_before = VNET_FEATURES ("ip6-lookup"),
857 .runs_after = 0,
858};
859/* *INDENT-ON* */
860
Mohammed Hawari2805daa2022-06-20 15:28:31 +0200861VLIB_NODE_FN (ip6_sv_reass_custom_context_node)
862(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
863{
864 return ip6_sv_reassembly_inline (vm, node, frame, false /* is_feature */,
865 true /* custom next */,
866 true /* custom context */);
867}
868
869VLIB_REGISTER_NODE (ip6_sv_reass_custom_context_node) = {
870 .name = "ip6-sv-reassembly-custom-context",
871 .vector_size = sizeof (u32),
872 .aux_size = sizeof (u32),
873 .format_trace = format_ip6_sv_reass_trace,
874 .n_errors = IP6_N_ERROR,
875 .error_counters = ip6_error_counters,
876 .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
877 .next_nodes =
878 {
879 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
880 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
881 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
882 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reassembly-custom-context-handoff",
883 },
884};
885
Klement Sekerade34c352019-06-25 11:19:22 +0000886#ifndef CLIB_MARCH_VARIANT
887static u32
888ip6_sv_reass_get_nbuckets ()
889{
890 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
891 u32 nbuckets;
892 u8 i;
893
894 nbuckets = (u32) (rm->max_reass_n / IP6_SV_REASS_HT_LOAD_FACTOR);
895
896 for (i = 0; i < 31; i++)
897 if ((1 << i) >= nbuckets)
898 break;
899 nbuckets = 1 << i;
900
901 return nbuckets;
902}
903#endif /* CLIB_MARCH_VARIANT */
904
905typedef enum
906{
907 IP6_EVENT_CONFIG_CHANGED = 1,
908} ip6_sv_reass_event_t;
909
910#ifndef CLIB_MARCH_VARIANT
911typedef struct
912{
913 int failure;
914 clib_bihash_48_8_t *new_hash;
915} ip6_rehash_cb_ctx;
916
Neale Rannsf50bac12019-12-06 05:53:17 +0000917static int
Klement Sekerade34c352019-06-25 11:19:22 +0000918ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
919{
920 ip6_rehash_cb_ctx *ctx = _ctx;
921 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
922 {
923 ctx->failure = 1;
924 }
Neale Rannsf50bac12019-12-06 05:53:17 +0000925 return (BIHASH_WALK_CONTINUE);
Klement Sekerade34c352019-06-25 11:19:22 +0000926}
927
928static void
929ip6_sv_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
930 u32 max_reassembly_length,
931 u32 expire_walk_interval_ms)
932{
933 ip6_sv_reass_main.timeout_ms = timeout_ms;
934 ip6_sv_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
935 ip6_sv_reass_main.max_reass_n = max_reassemblies;
936 ip6_sv_reass_main.max_reass_len = max_reassembly_length;
937 ip6_sv_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
938}
939
940vnet_api_error_t
941ip6_sv_reass_set (u32 timeout_ms, u32 max_reassemblies,
942 u32 max_reassembly_length, u32 expire_walk_interval_ms)
943{
944 u32 old_nbuckets = ip6_sv_reass_get_nbuckets ();
945 ip6_sv_reass_set_params (timeout_ms, max_reassemblies,
946 max_reassembly_length, expire_walk_interval_ms);
947 vlib_process_signal_event (ip6_sv_reass_main.vlib_main,
948 ip6_sv_reass_main.ip6_sv_reass_expire_node_idx,
949 IP6_EVENT_CONFIG_CHANGED, 0);
950 u32 new_nbuckets = ip6_sv_reass_get_nbuckets ();
951 if (ip6_sv_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
952 {
953 clib_bihash_48_8_t new_hash;
954 clib_memset (&new_hash, 0, sizeof (new_hash));
955 ip6_rehash_cb_ctx ctx;
956 ctx.failure = 0;
957 ctx.new_hash = &new_hash;
958 clib_bihash_init_48_8 (&new_hash, "ip6-sv-reass", new_nbuckets,
959 new_nbuckets * 1024);
960 clib_bihash_foreach_key_value_pair_48_8 (&ip6_sv_reass_main.hash,
961 ip6_rehash_cb, &ctx);
962 if (ctx.failure)
963 {
964 clib_bihash_free_48_8 (&new_hash);
965 return -1;
966 }
967 else
968 {
969 clib_bihash_free_48_8 (&ip6_sv_reass_main.hash);
970 clib_memcpy_fast (&ip6_sv_reass_main.hash, &new_hash,
971 sizeof (ip6_sv_reass_main.hash));
972 clib_bihash_copied (&ip6_sv_reass_main.hash, &new_hash);
973 }
974 }
975 return 0;
976}
977
978vnet_api_error_t
979ip6_sv_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
980 u32 * max_reassembly_length, u32 * expire_walk_interval_ms)
981{
982 *timeout_ms = ip6_sv_reass_main.timeout_ms;
983 *max_reassemblies = ip6_sv_reass_main.max_reass_n;
984 *max_reassembly_length = ip6_sv_reass_main.max_reass_len;
985 *expire_walk_interval_ms = ip6_sv_reass_main.expire_walk_interval_ms;
986 return 0;
987}
988
989static clib_error_t *
990ip6_sv_reass_init_function (vlib_main_t * vm)
991{
992 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
993 clib_error_t *error = 0;
994 u32 nbuckets;
995 vlib_node_t *node;
996
997 rm->vlib_main = vm;
998 rm->vnet_main = vnet_get_main ();
999
1000 vec_validate (rm->per_thread_data, vlib_num_workers ());
1001 ip6_sv_reass_per_thread_t *rt;
1002 vec_foreach (rt, rm->per_thread_data)
1003 {
1004 clib_spinlock_init (&rt->lock);
1005 pool_alloc (rt->pool, rm->max_reass_n);
1006 rt->lru_first = rt->lru_last = ~0;
1007 }
1008
1009 node = vlib_get_node_by_name (vm, (u8 *) "ip6-sv-reassembly-expire-walk");
1010 ASSERT (node);
1011 rm->ip6_sv_reass_expire_node_idx = node->index;
1012
1013 ip6_sv_reass_set_params (IP6_SV_REASS_TIMEOUT_DEFAULT_MS,
1014 IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT,
1015 IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1016 IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1017
1018 nbuckets = ip6_sv_reass_get_nbuckets ();
1019 clib_bihash_init_48_8 (&rm->hash, "ip6-sv-reass", nbuckets,
1020 nbuckets * 1024);
1021
1022 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
1023 ASSERT (node);
1024 rm->ip6_drop_idx = node->index;
1025 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1026 ASSERT (node);
1027 rm->ip6_icmp_error_idx = node->index;
1028
1029 if ((error = vlib_call_init_function (vm, ip_main_init)))
1030 return error;
Klement Sekerade34c352019-06-25 11:19:22 +00001031
1032 rm->fq_index = vlib_frame_queue_main_init (ip6_sv_reass_node.index, 0);
1033 rm->fq_feature_index =
1034 vlib_frame_queue_main_init (ip6_sv_reass_node_feature.index, 0);
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001035 rm->fq_custom_context_index =
1036 vlib_frame_queue_main_init (ip6_sv_reass_custom_context_node.index, 0);
Klement Sekerade34c352019-06-25 11:19:22 +00001037
Klement Sekera63c73532019-09-30 14:35:36 +00001038 rm->feature_use_refcount_per_intf = NULL;
1039
Klement Sekerade34c352019-06-25 11:19:22 +00001040 return error;
1041}
1042
1043VLIB_INIT_FUNCTION (ip6_sv_reass_init_function);
1044#endif /* CLIB_MARCH_VARIANT */
1045
1046static uword
Klement Sekera42cec0e2021-08-02 16:14:15 +02001047ip6_sv_reass_walk_expired (vlib_main_t *vm,
1048 CLIB_UNUSED (vlib_node_runtime_t *node),
1049 CLIB_UNUSED (vlib_frame_t *f))
Klement Sekerade34c352019-06-25 11:19:22 +00001050{
1051 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1052 uword event_type, *event_data = 0;
1053
1054 while (true)
1055 {
1056 vlib_process_wait_for_event_or_clock (vm,
1057 (f64) rm->expire_walk_interval_ms
1058 / (f64) MSEC_PER_SEC);
1059 event_type = vlib_process_get_events (vm, &event_data);
1060
1061 switch (event_type)
1062 {
Klement Sekera42cec0e2021-08-02 16:14:15 +02001063 case ~0:
1064 /* no events => timeout */
1065 /* fallthrough */
Klement Sekerade34c352019-06-25 11:19:22 +00001066 case IP6_EVENT_CONFIG_CHANGED:
Klement Sekera42cec0e2021-08-02 16:14:15 +02001067 /* nothing to do here */
Klement Sekerade34c352019-06-25 11:19:22 +00001068 break;
1069 default:
1070 clib_warning ("BUG: event type 0x%wx", event_type);
1071 break;
1072 }
1073 f64 now = vlib_time_now (vm);
1074
1075 ip6_sv_reass_t *reass;
1076 int *pool_indexes_to_free = NULL;
1077
1078 uword thread_index = 0;
1079 int index;
1080 const uword nthreads = vlib_num_workers () + 1;
Klement Sekerade34c352019-06-25 11:19:22 +00001081 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1082 {
1083 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1084 clib_spinlock_lock (&rt->lock);
1085
1086 vec_reset_length (pool_indexes_to_free);
1087 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001088 pool_foreach_index (index, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001089 reass = pool_elt_at_index (rt->pool, index);
1090 if (now > reass->last_heard + rm->timeout)
1091 {
1092 vec_add1 (pool_indexes_to_free, index);
1093 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001094 }
Klement Sekerade34c352019-06-25 11:19:22 +00001095 /* *INDENT-ON* */
1096 int *i;
1097 /* *INDENT-OFF* */
1098 vec_foreach (i, pool_indexes_to_free)
1099 {
1100 ip6_sv_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1101 ip6_sv_reass_free (vm, rm, rt, reass);
1102 }
1103 /* *INDENT-ON* */
1104
1105 clib_spinlock_unlock (&rt->lock);
1106 }
1107
Klement Sekerade34c352019-06-25 11:19:22 +00001108 vec_free (pool_indexes_to_free);
Klement Sekerade34c352019-06-25 11:19:22 +00001109 if (event_data)
1110 {
Damjan Marion8bea5892022-04-04 22:40:45 +02001111 vec_set_len (event_data, 0);
Klement Sekerade34c352019-06-25 11:19:22 +00001112 }
1113 }
1114
1115 return 0;
1116}
1117
1118/* *INDENT-OFF* */
1119VLIB_REGISTER_NODE (ip6_sv_reass_expire_node) = {
Neale Rannse22a7042022-08-09 03:03:29 +00001120 .function = ip6_sv_reass_walk_expired,
1121 .format_trace = format_ip6_sv_reass_trace,
1122 .type = VLIB_NODE_TYPE_PROCESS,
1123 .name = "ip6-sv-reassembly-expire-walk",
Klement Sekerade34c352019-06-25 11:19:22 +00001124
Neale Rannse22a7042022-08-09 03:03:29 +00001125 .n_errors = IP6_N_ERROR,
1126 .error_counters = ip6_error_counters,
Klement Sekerade34c352019-06-25 11:19:22 +00001127};
1128/* *INDENT-ON* */
1129
1130static u8 *
1131format_ip6_sv_reass_key (u8 * s, va_list * args)
1132{
1133 ip6_sv_reass_key_t *key = va_arg (*args, ip6_sv_reass_key_t *);
Klement Sekera5a58e7c2021-11-03 17:57:28 +01001134 s =
1135 format (s, "fib_index: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1136 key->fib_index, format_ip6_address, &key->src, format_ip6_address,
1137 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
Klement Sekerade34c352019-06-25 11:19:22 +00001138 return s;
1139}
1140
1141static u8 *
1142format_ip6_sv_reass (u8 * s, va_list * args)
1143{
1144 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1145 ip6_sv_reass_t *reass = va_arg (*args, ip6_sv_reass_t *);
1146
1147 s = format (s, "ID: %lu, key: %U, trace_op_counter: %u\n",
1148 reass->id, format_ip6_sv_reass_key, &reass->key,
1149 reass->trace_op_counter);
1150 vlib_buffer_t *b;
1151 u32 *bip;
1152 u32 counter = 0;
1153 vec_foreach (bip, reass->cached_buffers)
1154 {
1155 u32 bi = *bip;
1156 do
1157 {
1158 b = vlib_get_buffer (vm, bi);
1159 s = format (s, " #%03u: bi: %u\n", counter, bi);
1160 ++counter;
1161 bi = b->next_buffer;
1162 }
1163 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
1164 }
1165 return s;
1166}
1167
1168static clib_error_t *
1169show_ip6_sv_reass (vlib_main_t * vm, unformat_input_t * input,
1170 CLIB_UNUSED (vlib_cli_command_t * lmd))
1171{
1172 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1173
1174 vlib_cli_output (vm, "---------------------");
1175 vlib_cli_output (vm, "IP6 reassembly status");
1176 vlib_cli_output (vm, "---------------------");
1177 bool details = false;
1178 if (unformat (input, "details"))
1179 {
1180 details = true;
1181 }
1182
1183 u32 sum_reass_n = 0;
1184 u64 sum_buffers_n = 0;
1185 ip6_sv_reass_t *reass;
1186 uword thread_index;
1187 const uword nthreads = vlib_num_workers () + 1;
1188 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1189 {
1190 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1191 clib_spinlock_lock (&rt->lock);
1192 if (details)
1193 {
1194 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001195 pool_foreach (reass, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001196 vlib_cli_output (vm, "%U", format_ip6_sv_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001197 }
Klement Sekerade34c352019-06-25 11:19:22 +00001198 /* *INDENT-ON* */
1199 }
1200 sum_reass_n += rt->reass_n;
1201 clib_spinlock_unlock (&rt->lock);
1202 }
1203 vlib_cli_output (vm, "---------------------");
1204 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1205 (long unsigned) sum_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001206 vlib_cli_output (vm,
1207 "Maximum configured concurrent shallow virtual IP6 reassemblies per worker-thread: %lu\n",
1208 (long unsigned) rm->max_reass_n);
1209 vlib_cli_output (vm,
Anton Nikolaev74a4a702021-02-17 14:45:40 +05001210 "Maximum configured amount of fragments per shallow "
1211 "virtual IP6 reassembly: %lu\n",
1212 (long unsigned) rm->max_reass_len);
1213 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001214 "Maximum configured shallow virtual IP6 reassembly timeout: %lums\n",
1215 (long unsigned) rm->timeout_ms);
1216 vlib_cli_output (vm,
1217 "Maximum configured shallow virtual IP6 reassembly expire walk interval: %lums\n",
1218 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekerade34c352019-06-25 11:19:22 +00001219 vlib_cli_output (vm, "Buffers in use: %lu\n",
1220 (long unsigned) sum_buffers_n);
1221 return 0;
1222}
1223
1224/* *INDENT-OFF* */
1225VLIB_CLI_COMMAND (show_ip6_sv_reassembly_cmd, static) = {
1226 .path = "show ip6-sv-reassembly",
1227 .short_help = "show ip6-sv-reassembly [details]",
1228 .function = show_ip6_sv_reass,
1229};
1230/* *INDENT-ON* */
1231
1232#ifndef CLIB_MARCH_VARIANT
1233vnet_api_error_t
1234ip6_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1235{
Klement Sekeraf126e742019-10-10 09:46:06 +00001236 return ip6_sv_reass_enable_disable_with_refcnt (sw_if_index,
1237 enable_disable);
Klement Sekerade34c352019-06-25 11:19:22 +00001238}
1239#endif /* CLIB_MARCH_VARIANT */
1240
1241#define foreach_ip6_sv_reassembly_handoff_error \
1242_(CONGESTION_DROP, "congestion drop")
1243
1244
1245typedef enum
1246{
1247#define _(sym,str) IP6_SV_REASSEMBLY_HANDOFF_ERROR_##sym,
1248 foreach_ip6_sv_reassembly_handoff_error
1249#undef _
1250 IP6_SV_REASSEMBLY_HANDOFF_N_ERROR,
1251} ip6_sv_reassembly_handoff_error_t;
1252
1253static char *ip6_sv_reassembly_handoff_error_strings[] = {
1254#define _(sym,string) string,
1255 foreach_ip6_sv_reassembly_handoff_error
1256#undef _
1257};
1258
1259typedef struct
1260{
1261 u32 next_worker_index;
1262} ip6_sv_reassembly_handoff_trace_t;
1263
1264static u8 *
1265format_ip6_sv_reassembly_handoff_trace (u8 * s, va_list * args)
1266{
1267 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1268 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1269 ip6_sv_reassembly_handoff_trace_t *t =
1270 va_arg (*args, ip6_sv_reassembly_handoff_trace_t *);
1271
1272 s =
1273 format (s, "ip6-sv-reassembly-handoff: next-worker %d",
1274 t->next_worker_index);
1275
1276 return s;
1277}
1278
1279always_inline uword
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001280ip6_sv_reassembly_handoff_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1281 vlib_frame_t *frame, bool is_feature,
1282 bool custom_context)
Klement Sekerade34c352019-06-25 11:19:22 +00001283{
1284 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1285
1286 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001287 u32 n_enq, n_left_from, *from, *context;
Klement Sekerade34c352019-06-25 11:19:22 +00001288 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1289 u32 fq_index;
1290
1291 from = vlib_frame_vector_args (frame);
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001292 if (custom_context)
1293 context = vlib_frame_aux_args (frame);
Klement Sekerade34c352019-06-25 11:19:22 +00001294 n_left_from = frame->n_vectors;
1295 vlib_get_buffers (vm, from, bufs, n_left_from);
1296
1297 b = bufs;
1298 ti = thread_indices;
1299
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001300 fq_index = (is_feature) ?
1301 rm->fq_feature_index :
1302 (custom_context ? rm->fq_custom_context_index : rm->fq_index);
Klement Sekerade34c352019-06-25 11:19:22 +00001303
1304 while (n_left_from > 0)
1305 {
1306 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1307
1308 if (PREDICT_FALSE
1309 ((node->flags & VLIB_NODE_FLAG_TRACE)
1310 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1311 {
1312 ip6_sv_reassembly_handoff_trace_t *t =
1313 vlib_add_trace (vm, node, b[0], sizeof (*t));
1314 t->next_worker_index = ti[0];
1315 }
1316
1317 n_left_from -= 1;
1318 ti += 1;
1319 b += 1;
1320 }
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001321 if (custom_context)
1322 n_enq = vlib_buffer_enqueue_to_thread_with_aux (
1323 vm, node, fq_index, from, context, thread_indices, frame->n_vectors, 1);
1324 else
1325 n_enq = vlib_buffer_enqueue_to_thread (
1326 vm, node, fq_index, from, thread_indices, frame->n_vectors, 1);
Klement Sekerade34c352019-06-25 11:19:22 +00001327
1328 if (n_enq < frame->n_vectors)
1329 vlib_node_increment_counter (vm, node->node_index,
1330 IP6_SV_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1331 frame->n_vectors - n_enq);
1332 return frame->n_vectors;
1333}
1334
1335VLIB_NODE_FN (ip6_sv_reassembly_handoff_node) (vlib_main_t * vm,
1336 vlib_node_runtime_t * node,
1337 vlib_frame_t * frame)
1338{
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001339 return ip6_sv_reassembly_handoff_inline (
1340 vm, node, frame, false /* is_feature */, false /* custom_context */);
Klement Sekerade34c352019-06-25 11:19:22 +00001341}
1342
1343/* *INDENT-OFF* */
1344VLIB_REGISTER_NODE (ip6_sv_reassembly_handoff_node) = {
1345 .name = "ip6-sv-reassembly-handoff",
1346 .vector_size = sizeof (u32),
1347 .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1348 .error_strings = ip6_sv_reassembly_handoff_error_strings,
1349 .format_trace = format_ip6_sv_reassembly_handoff_trace,
1350
1351 .n_next_nodes = 1,
1352
1353 .next_nodes = {
1354 [0] = "error-drop",
1355 },
1356};
1357
1358
1359VLIB_NODE_FN (ip6_sv_reassembly_feature_handoff_node) (vlib_main_t * vm,
1360 vlib_node_runtime_t * node, vlib_frame_t * frame)
1361{
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001362 return ip6_sv_reassembly_handoff_inline (
1363 vm, node, frame, true /* is_feature */, false /* custom_context */);
Klement Sekerade34c352019-06-25 11:19:22 +00001364}
1365
1366
1367/* *INDENT-OFF* */
1368VLIB_REGISTER_NODE (ip6_sv_reassembly_feature_handoff_node) = {
1369 .name = "ip6-sv-reass-feature-hoff",
1370 .vector_size = sizeof (u32),
1371 .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1372 .error_strings = ip6_sv_reassembly_handoff_error_strings,
1373 .format_trace = format_ip6_sv_reassembly_handoff_trace,
1374
1375 .n_next_nodes = 1,
1376
1377 .next_nodes = {
1378 [0] = "error-drop",
1379 },
1380};
1381/* *INDENT-ON* */
1382
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001383VLIB_NODE_FN (ip6_sv_reassembly_custom_context_handoff_node)
1384(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1385{
1386 return ip6_sv_reassembly_handoff_inline (
1387 vm, node, frame, false /* is_feature */, true /* custom_context */);
1388}
1389
1390VLIB_REGISTER_NODE (ip6_sv_reassembly_custom_context_handoff_node) = {
1391 .name = "ip6-sv-reassembly-custom-context-handoff",
1392 .vector_size = sizeof (u32),
1393 .aux_size = sizeof (u32),
1394 .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1395 .error_strings = ip6_sv_reassembly_handoff_error_strings,
1396 .format_trace = format_ip6_sv_reassembly_handoff_trace,
1397
1398 .n_next_nodes = 1,
1399
1400 .next_nodes = {
1401 [0] = "error-drop",
1402 },
1403};
1404
Klement Sekerade34c352019-06-25 11:19:22 +00001405#ifndef CLIB_MARCH_VARIANT
1406int
1407ip6_sv_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1408{
1409 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
Klement Sekera63c73532019-09-30 14:35:36 +00001410 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
Klement Sekerade34c352019-06-25 11:19:22 +00001411 if (is_enable)
1412 {
Klement Sekera63c73532019-09-30 14:35:36 +00001413 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001414 {
Klement Sekera63c73532019-09-30 14:35:36 +00001415 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001416 return vnet_feature_enable_disable ("ip6-unicast",
1417 "ip6-sv-reassembly-feature",
1418 sw_if_index, 1, 0, 0);
1419 }
Klement Sekera63c73532019-09-30 14:35:36 +00001420 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001421 }
1422 else
1423 {
Klement Sekera63c73532019-09-30 14:35:36 +00001424 --rm->feature_use_refcount_per_intf[sw_if_index];
1425 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001426 return vnet_feature_enable_disable ("ip6-unicast",
1427 "ip6-sv-reassembly-feature",
1428 sw_if_index, 0, 0, 0);
1429 }
Klement Sekera407f5932019-12-11 13:06:27 +00001430 return 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001431}
Mohammed Hawari2805daa2022-06-20 15:28:31 +02001432
1433uword
1434ip6_sv_reass_custom_context_register_next_node (uword node_index)
1435{
1436 return vlib_node_add_next (
1437 vlib_get_main (), ip6_sv_reassembly_custom_context_handoff_node.index,
1438 node_index);
1439}
Klement Sekerade34c352019-06-25 11:19:22 +00001440#endif
1441
1442/*
1443 * fd.io coding-style-patch-verification: ON
1444 *
1445 * Local Variables:
1446 * eval: (c-set-style "gnu")
1447 * End:
1448 */