blob: 28941311f50ac1fef6e9bfff2c001b11c8d6e41c [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>
29
30#define MSEC_PER_SEC 1000
31#define IP6_SV_REASS_TIMEOUT_DEFAULT_MS 100
32#define IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
33#define IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT 1024
34#define IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
35#define IP6_SV_REASS_HT_LOAD_FACTOR (0.75)
36
37typedef enum
38{
39 IP6_SV_REASS_RC_OK,
40 IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS,
41 IP6_SV_REASS_RC_INTERNAL_ERROR,
42 IP6_SV_REASS_RC_UNSUPP_IP_PROTO,
43} ip6_sv_reass_rc_t;
44
45typedef struct
46{
47 union
48 {
49 struct
50 {
51 ip6_address_t src;
52 ip6_address_t dst;
53 u32 xx_id;
54 u32 frag_id;
55 u8 unused[7];
56 u8 proto;
57 };
58 u64 as_u64[6];
59 };
60} ip6_sv_reass_key_t;
61
62typedef union
63{
64 struct
65 {
66 u32 reass_index;
67 u32 thread_index;
68 };
69 u64 as_u64;
70} ip6_sv_reass_val_t;
71
72typedef union
73{
74 struct
75 {
76 ip6_sv_reass_key_t k;
77 ip6_sv_reass_val_t v;
78 };
79 clib_bihash_kv_48_8_t kv;
80} ip6_sv_reass_kv_t;
81
82typedef struct
83{
84 // hash table key
85 ip6_sv_reass_key_t key;
86 // time when last packet was received
87 f64 last_heard;
88 // internal id of this reassembly
89 u64 id;
90 // trace operation counter
91 u32 trace_op_counter;
92 // buffer indexes of buffers in this reassembly in chronological order -
93 // including overlaps and duplicate fragments
94 u32 *cached_buffers;
95 // set to true when this reassembly is completed
96 bool is_complete;
97 // ip protocol
98 u8 ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +000099 u8 icmp_type_or_tcp_flags;
100 u32 tcp_ack_number;
101 u32 tcp_seq_number;
Klement Sekerade34c352019-06-25 11:19:22 +0000102 // l4 src port
103 u16 l4_src_port;
104 // l4 dst port
105 u16 l4_dst_port;
106 // lru indexes
107 u32 lru_prev;
108 u32 lru_next;
109} ip6_sv_reass_t;
110
111typedef struct
112{
113 ip6_sv_reass_t *pool;
114 u32 reass_n;
115 u32 id_counter;
116 clib_spinlock_t lock;
117 // lru indexes
118 u32 lru_first;
119 u32 lru_last;
120} ip6_sv_reass_per_thread_t;
121
122typedef struct
123{
124 // IPv6 config
125 u32 timeout_ms;
126 f64 timeout;
127 u32 expire_walk_interval_ms;
128 // maximum number of fragments in one reassembly
129 u32 max_reass_len;
130 // maximum number of reassemblies
131 u32 max_reass_n;
132
133 // IPv6 runtime
134 clib_bihash_48_8_t hash;
135
136 // per-thread data
137 ip6_sv_reass_per_thread_t *per_thread_data;
138
139 // convenience
140 vlib_main_t *vlib_main;
141 vnet_main_t *vnet_main;
142
143 // node index of ip6-drop node
144 u32 ip6_drop_idx;
145 u32 ip6_icmp_error_idx;
146 u32 ip6_sv_reass_expire_node_idx;
147
148 /** Worker handoff */
149 u32 fq_index;
150 u32 fq_feature_index;
151
Klement Sekera63c73532019-09-30 14:35:36 +0000152 // reference count for enabling/disabling feature - per interface
153 u32 *feature_use_refcount_per_intf;
Klement Sekerade34c352019-06-25 11:19:22 +0000154} ip6_sv_reass_main_t;
155
156extern ip6_sv_reass_main_t ip6_sv_reass_main;
157
158#ifndef CLIB_MARCH_VARIANT
159ip6_sv_reass_main_t ip6_sv_reass_main;
160#endif /* CLIB_MARCH_VARIANT */
161
162typedef enum
163{
164 IP6_SV_REASSEMBLY_NEXT_INPUT,
165 IP6_SV_REASSEMBLY_NEXT_DROP,
166 IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR,
167 IP6_SV_REASSEMBLY_NEXT_HANDOFF,
168 IP6_SV_REASSEMBLY_N_NEXT,
169} ip6_sv_reass_next_t;
170
171typedef enum
172{
Klement Sekerade34c352019-06-25 11:19:22 +0000173 REASS_FRAGMENT_CACHE,
174 REASS_FINISH,
175 REASS_FRAGMENT_FORWARD,
Klement Sekeraf126e742019-10-10 09:46:06 +0000176 REASS_PASSTHROUGH,
Klement Sekerade34c352019-06-25 11:19:22 +0000177} ip6_sv_reass_trace_operation_e;
178
179typedef struct
180{
181 ip6_sv_reass_trace_operation_e action;
182 u32 reass_id;
183 u32 op_id;
184 u8 ip_proto;
185 u16 l4_src_port;
186 u16 l4_dst_port;
187} ip6_sv_reass_trace_t;
188
189static u8 *
190format_ip6_sv_reass_trace (u8 * s, va_list * args)
191{
192 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
193 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
194 ip6_sv_reass_trace_t *t = va_arg (*args, ip6_sv_reass_trace_t *);
Klement Sekeraf126e742019-10-10 09:46:06 +0000195 if (REASS_PASSTHROUGH != t->action)
196 {
197 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
198 }
Klement Sekerade34c352019-06-25 11:19:22 +0000199 switch (t->action)
200 {
Klement Sekerade34c352019-06-25 11:19:22 +0000201 case REASS_FRAGMENT_CACHE:
202 s = format (s, "[cached]");
203 break;
204 case REASS_FINISH:
205 s =
206 format (s, "[finish, ip proto=%u, src_port=%u, dst_port=%u]",
207 t->ip_proto, clib_net_to_host_u16 (t->l4_src_port),
208 clib_net_to_host_u16 (t->l4_dst_port));
209 break;
210 case REASS_FRAGMENT_FORWARD:
211 s =
212 format (s, "[forward, ip proto=%u, src_port=%u, dst_port=%u]",
213 t->ip_proto, clib_net_to_host_u16 (t->l4_src_port),
214 clib_net_to_host_u16 (t->l4_dst_port));
215 break;
Klement Sekeraf126e742019-10-10 09:46:06 +0000216 case REASS_PASSTHROUGH:
217 s = format (s, "[not-fragmented]");
218 break;
Klement Sekerade34c352019-06-25 11:19:22 +0000219 }
220 return s;
221}
222
223static void
224ip6_sv_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekerade34c352019-06-25 11:19:22 +0000225 ip6_sv_reass_t * reass, u32 bi,
226 ip6_sv_reass_trace_operation_e action,
227 u32 ip_proto, u16 l4_src_port, u16 l4_dst_port)
228{
229 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Klement Sekera53be16d2020-12-15 21:47:36 +0100230 if (pool_is_free_index
231 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
232 {
233 // this buffer's trace is gone
234 b->flags &= ~VLIB_BUFFER_IS_TRACED;
235 return;
236 }
Klement Sekerade34c352019-06-25 11:19:22 +0000237 ip6_sv_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekeraf126e742019-10-10 09:46:06 +0000238 if (reass)
239 {
240 t->reass_id = reass->id;
241 t->op_id = reass->trace_op_counter;
242 ++reass->trace_op_counter;
243 }
Klement Sekerade34c352019-06-25 11:19:22 +0000244 t->action = action;
Klement Sekerade34c352019-06-25 11:19:22 +0000245 t->ip_proto = ip_proto;
246 t->l4_src_port = l4_src_port;
247 t->l4_dst_port = l4_dst_port;
Klement Sekerade34c352019-06-25 11:19:22 +0000248#if 0
249 static u8 *s = NULL;
250 s = format (s, "%U", format_ip6_sv_reass_trace, NULL, NULL, t);
251 printf ("%.*s\n", vec_len (s), s);
252 fflush (stdout);
253 vec_reset_length (s);
254#endif
255}
256
257always_inline void
258ip6_sv_reass_free (vlib_main_t * vm, ip6_sv_reass_main_t * rm,
259 ip6_sv_reass_per_thread_t * rt, ip6_sv_reass_t * reass)
260{
261 clib_bihash_kv_48_8_t kv;
262 kv.key[0] = reass->key.as_u64[0];
263 kv.key[1] = reass->key.as_u64[1];
264 kv.key[2] = reass->key.as_u64[2];
265 kv.key[3] = reass->key.as_u64[3];
266 kv.key[4] = reass->key.as_u64[4];
267 kv.key[5] = reass->key.as_u64[5];
268 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
269 vlib_buffer_free (vm, reass->cached_buffers,
270 vec_len (reass->cached_buffers));
271 vec_free (reass->cached_buffers);
272 reass->cached_buffers = NULL;
273 if (~0 != reass->lru_prev)
274 {
275 ip6_sv_reass_t *lru_prev =
276 pool_elt_at_index (rt->pool, reass->lru_prev);
277 lru_prev->lru_next = reass->lru_next;
278 }
279 if (~0 != reass->lru_next)
280 {
281 ip6_sv_reass_t *lru_next =
282 pool_elt_at_index (rt->pool, reass->lru_next);
283 lru_next->lru_prev = reass->lru_prev;
284 }
285 if (rt->lru_first == reass - rt->pool)
286 {
287 rt->lru_first = reass->lru_next;
288 }
289 if (rt->lru_last == reass - rt->pool)
290 {
291 rt->lru_last = reass->lru_prev;
292 }
293 pool_put (rt->pool, reass);
294 --rt->reass_n;
295}
296
297always_inline void
298ip6_sv_reass_init (ip6_sv_reass_t * reass)
299{
300 reass->cached_buffers = NULL;
301 reass->is_complete = false;
302}
303
304always_inline ip6_sv_reass_t *
Klement Sekera42cec0e2021-08-02 16:14:15 +0200305ip6_sv_reass_find_or_create (vlib_main_t *vm, ip6_sv_reass_main_t *rm,
306 ip6_sv_reass_per_thread_t *rt,
307 ip6_sv_reass_kv_t *kv, u8 *do_handoff)
Klement Sekerade34c352019-06-25 11:19:22 +0000308{
309 ip6_sv_reass_t *reass = NULL;
Tom Seidenberg5a7f2f12020-04-28 17:58:12 -0400310 f64 now = vlib_time_now (vm);
Klement Sekerade34c352019-06-25 11:19:22 +0000311
Klement Sekerac99c0252019-12-18 12:17:06 +0000312 if (!clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekerade34c352019-06-25 11:19:22 +0000313 {
314 if (vm->thread_index != kv->v.thread_index)
315 {
316 *do_handoff = 1;
317 return NULL;
318 }
319 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
320
321 if (now > reass->last_heard + rm->timeout)
322 {
323 ip6_sv_reass_free (vm, rm, rt, reass);
324 reass = NULL;
325 }
326 }
327
328 if (reass)
329 {
330 reass->last_heard = now;
331 return reass;
332 }
333
334 if (rt->reass_n >= rm->max_reass_n)
335 {
zhengdelunce533632020-05-22 15:42:28 +0800336 reass = pool_elt_at_index (rt->pool, rt->lru_first);
Klement Sekerade34c352019-06-25 11:19:22 +0000337 ip6_sv_reass_free (vm, rm, rt, reass);
338 }
339
340 pool_get (rt->pool, reass);
341 clib_memset (reass, 0, sizeof (*reass));
342 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
343 ++rt->id_counter;
344 ip6_sv_reass_init (reass);
345 ++rt->reass_n;
346
347 reass->lru_prev = reass->lru_next = ~0;
348
349 if (~0 != rt->lru_last)
350 {
351 ip6_sv_reass_t *lru_last = pool_elt_at_index (rt->pool, rt->lru_last);
352 reass->lru_prev = rt->lru_last;
353 lru_last->lru_next = rt->lru_last = reass - rt->pool;
354 }
355
356 if (~0 == rt->lru_first)
357 {
358 rt->lru_first = rt->lru_last = reass - rt->pool;
359 }
360
Klement Sekerac99c0252019-12-18 12:17:06 +0000361 reass->key.as_u64[0] = kv->kv.key[0];
362 reass->key.as_u64[1] = kv->kv.key[1];
363 reass->key.as_u64[2] = kv->kv.key[2];
364 reass->key.as_u64[3] = kv->kv.key[3];
365 reass->key.as_u64[4] = kv->kv.key[4];
366 reass->key.as_u64[5] = kv->kv.key[5];
Klement Sekerade34c352019-06-25 11:19:22 +0000367 kv->v.reass_index = (reass - rt->pool);
368 kv->v.thread_index = vm->thread_index;
369 reass->last_heard = now;
370
Klement Sekerac99c0252019-12-18 12:17:06 +0000371 if (clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 1))
Klement Sekerade34c352019-06-25 11:19:22 +0000372 {
373 ip6_sv_reass_free (vm, rm, rt, reass);
374 reass = NULL;
375 }
376
377 return reass;
378}
379
380always_inline ip6_sv_reass_rc_t
Klement Sekera42cec0e2021-08-02 16:14:15 +0200381ip6_sv_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
382 ip6_sv_reass_main_t *rm, ip6_sv_reass_t *reass, u32 bi0,
383 ip6_frag_hdr_t *frag_hdr)
Klement Sekerade34c352019-06-25 11:19:22 +0000384{
385 vlib_buffer_t *fb = vlib_get_buffer (vm, bi0);
386 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
387 fvnb->ip.reass.ip6_frag_hdr_offset =
388 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
389 ip6_header_t *fip = vlib_buffer_get_current (fb);
390 if (fb->current_length < sizeof (*fip) ||
391 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
392 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
393 {
394 return IP6_SV_REASS_RC_INTERNAL_ERROR;
395 }
396
397 u32 fragment_first = fvnb->ip.reass.fragment_first =
398 ip6_frag_hdr_offset_bytes (frag_hdr);
399 u32 fragment_length =
400 vlib_buffer_length_in_chain (vm, fb) -
401 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
402 u32 fragment_last = fvnb->ip.reass.fragment_last =
403 fragment_first + fragment_length - 1;
404 fvnb->ip.reass.range_first = fragment_first;
405 fvnb->ip.reass.range_last = fragment_last;
406 fvnb->ip.reass.next_range_bi = ~0;
407 if (0 == fragment_first)
408 {
Klement Sekeraf126e742019-10-10 09:46:06 +0000409 if (!ip6_get_port
410 (vm, fb, fip, fb->current_length, &reass->ip_proto,
411 &reass->l4_src_port, &reass->l4_dst_port,
412 &reass->icmp_type_or_tcp_flags, &reass->tcp_ack_number,
413 &reass->tcp_seq_number))
Klement Sekerade34c352019-06-25 11:19:22 +0000414 return IP6_SV_REASS_RC_UNSUPP_IP_PROTO;
Klement Sekeraf126e742019-10-10 09:46:06 +0000415
Klement Sekerade34c352019-06-25 11:19:22 +0000416 reass->is_complete = true;
417 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
418 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
419 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200420 ip6_sv_reass_add_trace (vm, node, reass, bi0, REASS_FINISH,
Klement Sekerade34c352019-06-25 11:19:22 +0000421 reass->ip_proto, reass->l4_src_port,
422 reass->l4_dst_port);
423 }
424 }
425 vec_add1 (reass->cached_buffers, bi0);
426 if (!reass->is_complete)
427 {
428 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
429 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200430 ip6_sv_reass_add_trace (vm, node, reass, bi0, REASS_FRAGMENT_CACHE,
431 reass->ip_proto, reass->l4_src_port,
432 reass->l4_dst_port);
Klement Sekerade34c352019-06-25 11:19:22 +0000433 }
434 if (vec_len (reass->cached_buffers) > rm->max_reass_len)
435 {
436 return IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS;
437 }
438 }
439 return IP6_SV_REASS_RC_OK;
440}
441
442always_inline bool
443ip6_sv_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
444 vlib_buffer_t * b,
445 ip6_frag_hdr_t * frag_hdr)
446{
447 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
448 while (ip6_ext_hdr (tmp->next_hdr))
449 {
450 tmp = ip6_ext_next_header (tmp);
451 }
452 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
453 {
454 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
455 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
456 0);
457 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
458
459 return false;
460 }
461 return true;
462}
463
464always_inline bool
465ip6_sv_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
Klement Sekerade34c352019-06-25 11:19:22 +0000466 vlib_buffer_t * b,
467 ip6_frag_hdr_t * frag_hdr)
468{
469 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
470 ip6_header_t *ip = vlib_buffer_get_current (b);
471 int more_fragments = ip6_frag_hdr_more (frag_hdr);
472 u32 fragment_length =
473 vlib_buffer_length_in_chain (vm, b) -
474 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
475 if (more_fragments && 0 != fragment_length % 8)
476 {
477 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
478 ICMP6_parameter_problem_erroneous_header_field,
479 (u8 *) & ip->payload_length - (u8 *) ip);
480 return false;
481 }
482 return true;
483}
484
485always_inline bool
486ip6_sv_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
Klement Sekerade34c352019-06-25 11:19:22 +0000487 vlib_buffer_t * b,
488 ip6_frag_hdr_t * frag_hdr)
489{
490 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
491 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
492 u32 fragment_length =
493 vlib_buffer_length_in_chain (vm, b) -
494 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
495 if (fragment_first + fragment_length > 65535)
496 {
497 ip6_header_t *ip0 = vlib_buffer_get_current (b);
498 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
499 ICMP6_parameter_problem_erroneous_header_field,
500 (u8 *) & frag_hdr->fragment_offset_and_more
501 - (u8 *) ip0);
502 return false;
503 }
504 return true;
505}
506
507always_inline uword
508ip6_sv_reassembly_inline (vlib_main_t * vm,
509 vlib_node_runtime_t * node,
510 vlib_frame_t * frame, bool is_feature)
511{
512 u32 *from = vlib_frame_vector_args (frame);
513 u32 n_left_from, n_left_to_next, *to_next, next_index;
514 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
515 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
516 clib_spinlock_lock (&rt->lock);
517
518 n_left_from = frame->n_vectors;
519 next_index = node->cached_next_index;
520
521 while (n_left_from > 0)
522 {
523 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
524
525 while (n_left_from > 0 && n_left_to_next > 0)
526 {
527 u32 bi0;
528 vlib_buffer_t *b0;
529 u32 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
530 u32 error0 = IP6_ERROR_NONE;
Klement Sekerade34c352019-06-25 11:19:22 +0000531
532 bi0 = from[0];
533 b0 = vlib_get_buffer (vm, bi0);
534
535 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
536 ip6_frag_hdr_t *frag_hdr = NULL;
537 ip6_ext_header_t *prev_hdr;
538 if (ip6_ext_hdr (ip0->protocol))
539 {
540 frag_hdr =
541 ip6_ext_header_find (vm, b0, ip0,
542 IP_PROTOCOL_IPV6_FRAGMENTATION,
543 &prev_hdr);
544 }
545 if (!frag_hdr)
546 {
547 // this is a regular packet - no fragmentation
Klement Sekeraf126e742019-10-10 09:46:06 +0000548 if (!ip6_get_port
549 (vm, b0, ip0, b0->current_length,
550 &(vnet_buffer (b0)->ip.reass.ip_proto),
551 &(vnet_buffer (b0)->ip.reass.l4_src_port),
552 &(vnet_buffer (b0)->ip.reass.l4_dst_port),
553 &(vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags),
554 &(vnet_buffer (b0)->ip.reass.tcp_ack_number),
555 &(vnet_buffer (b0)->ip.reass.tcp_seq_number)))
556 {
557 error0 = IP6_ERROR_REASS_UNSUPP_IP_PROTO;
Klement Sekera1766ddc2020-03-30 16:59:38 +0200558 b0->error = node->errors[error0];
Klement Sekeraf126e742019-10-10 09:46:06 +0000559 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
560 goto packet_enqueue;
561 }
Klement Sekeraf126e742019-10-10 09:46:06 +0000562 vnet_buffer (b0)->ip.reass.is_non_first_fragment = 0;
Klement Sekerade34c352019-06-25 11:19:22 +0000563 next0 = IP6_SV_REASSEMBLY_NEXT_INPUT;
Klement Sekeraf126e742019-10-10 09:46:06 +0000564 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
565 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200566 ip6_sv_reass_add_trace (
567 vm, node, NULL, bi0, REASS_PASSTHROUGH,
568 vnet_buffer (b0)->ip.reass.ip_proto,
569 vnet_buffer (b0)->ip.reass.l4_src_port,
570 vnet_buffer (b0)->ip.reass.l4_dst_port);
Klement Sekeraf126e742019-10-10 09:46:06 +0000571 }
Klement Sekerade34c352019-06-25 11:19:22 +0000572 goto packet_enqueue;
573 }
Klement Sekera38f7ccb2019-10-28 11:26:28 +0000574 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
575 (u8 *) frag_hdr - (u8 *) ip0;
Klement Sekerade34c352019-06-25 11:19:22 +0000576 if (0 == ip6_frag_hdr_offset (frag_hdr))
577 {
578 // first fragment - verify upper-layer is present
579 if (!ip6_sv_reass_verify_upper_layer_present
580 (node, b0, frag_hdr))
581 {
582 next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
583 goto packet_enqueue;
584 }
585 }
Klement Sekera42cec0e2021-08-02 16:14:15 +0200586 if (!ip6_sv_reass_verify_fragment_multiple_8 (vm, b0, frag_hdr) ||
587 !ip6_sv_reass_verify_packet_size_lt_64k (vm, b0, frag_hdr))
Klement Sekerade34c352019-06-25 11:19:22 +0000588 {
589 next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
590 goto packet_enqueue;
591 }
Klement Sekerade34c352019-06-25 11:19:22 +0000592
593 ip6_sv_reass_kv_t kv;
594 u8 do_handoff = 0;
595
596 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
597 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
598 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
599 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
600 kv.k.as_u64[4] =
601 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
602 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
603 (u64) frag_hdr->identification;
604 kv.k.as_u64[5] = ip0->protocol;
605
606 ip6_sv_reass_t *reass =
Klement Sekera42cec0e2021-08-02 16:14:15 +0200607 ip6_sv_reass_find_or_create (vm, rm, rt, &kv, &do_handoff);
Klement Sekerade34c352019-06-25 11:19:22 +0000608
609 if (PREDICT_FALSE (do_handoff))
610 {
611 next0 = IP6_SV_REASSEMBLY_NEXT_HANDOFF;
612 vnet_buffer (b0)->ip.reass.owner_thread_index =
613 kv.v.thread_index;
Klement Sekera364b20a2019-10-07 09:48:06 +0000614 goto packet_enqueue;
Klement Sekerade34c352019-06-25 11:19:22 +0000615 }
616
617 if (!reass)
618 {
619 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
620 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
Klement Sekera1766ddc2020-03-30 16:59:38 +0200621 b0->error = node->errors[error0];
Klement Sekerade34c352019-06-25 11:19:22 +0000622 goto packet_enqueue;
623 }
624
625 if (reass->is_complete)
626 {
Klement Sekeraf126e742019-10-10 09:46:06 +0000627 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
628 ! !ip6_frag_hdr_offset (frag_hdr);
Klement Sekerade34c352019-06-25 11:19:22 +0000629 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +0000630 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
631 reass->icmp_type_or_tcp_flags;
632 vnet_buffer (b0)->ip.reass.tcp_ack_number =
633 reass->tcp_ack_number;
634 vnet_buffer (b0)->ip.reass.tcp_seq_number =
635 reass->tcp_seq_number;
Klement Sekerade34c352019-06-25 11:19:22 +0000636 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
637 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
638 next0 = IP6_SV_REASSEMBLY_NEXT_INPUT;
Klement Sekerade34c352019-06-25 11:19:22 +0000639 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
640 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200641 ip6_sv_reass_add_trace (
642 vm, node, reass, bi0, REASS_FRAGMENT_FORWARD,
643 reass->ip_proto, reass->l4_src_port, reass->l4_dst_port);
Klement Sekerade34c352019-06-25 11:19:22 +0000644 }
645 goto packet_enqueue;
646 }
647
Klement Sekera42cec0e2021-08-02 16:14:15 +0200648 switch (ip6_sv_reass_update (vm, node, rm, reass, bi0, frag_hdr))
Klement Sekerade34c352019-06-25 11:19:22 +0000649 {
650 case IP6_SV_REASS_RC_OK:
651 /* nothing to do here */
652 break;
653 case IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS:
654 vlib_node_increment_counter (vm, node->node_index,
655 IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
656 1);
657 ip6_sv_reass_free (vm, rm, rt, reass);
658 goto next_packet;
659 break;
660 case IP6_SV_REASS_RC_UNSUPP_IP_PROTO:
661 vlib_node_increment_counter (vm, node->node_index,
662 IP6_ERROR_REASS_UNSUPP_IP_PROTO,
663 1);
664 ip6_sv_reass_free (vm, rm, rt, reass);
665 goto next_packet;
666 break;
667 case IP6_SV_REASS_RC_INTERNAL_ERROR:
668 vlib_node_increment_counter (vm, node->node_index,
669 IP6_ERROR_REASS_INTERNAL_ERROR, 1);
670 ip6_sv_reass_free (vm, rm, rt, reass);
671 goto next_packet;
672 break;
673 }
674
Klement Sekerade34c352019-06-25 11:19:22 +0000675 if (reass->is_complete)
676 {
677 u32 idx;
678 vec_foreach_index (idx, reass->cached_buffers)
679 {
680 u32 bi0 = vec_elt (reass->cached_buffers, idx);
681 if (0 == n_left_to_next)
682 {
683 vlib_put_next_frame (vm, node, next_index,
684 n_left_to_next);
685 vlib_get_next_frame (vm, node, next_index, to_next,
686 n_left_to_next);
687 }
688 to_next[0] = bi0;
689 to_next += 1;
690 n_left_to_next -= 1;
691 b0 = vlib_get_buffer (vm, bi0);
692 if (is_feature)
693 {
694 vnet_feature_next (&next0, b0);
695 }
Klement Sekeraf126e742019-10-10 09:46:06 +0000696 frag_hdr =
697 vlib_buffer_get_current (b0) +
698 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset;
Klement Sekeraf126e742019-10-10 09:46:06 +0000699 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
700 ! !ip6_frag_hdr_offset (frag_hdr);
Klement Sekerade34c352019-06-25 11:19:22 +0000701 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +0000702 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
703 reass->icmp_type_or_tcp_flags;
704 vnet_buffer (b0)->ip.reass.tcp_ack_number =
705 reass->tcp_ack_number;
706 vnet_buffer (b0)->ip.reass.tcp_seq_number =
707 reass->tcp_seq_number;
Klement Sekerade34c352019-06-25 11:19:22 +0000708 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
709 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
710 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
711 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200712 ip6_sv_reass_add_trace (
713 vm, node, reass, bi0, REASS_FRAGMENT_FORWARD,
714 reass->ip_proto, reass->l4_src_port, reass->l4_dst_port);
Klement Sekerade34c352019-06-25 11:19:22 +0000715 }
716 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
717 to_next, n_left_to_next, bi0,
718 next0);
719 }
720 _vec_len (reass->cached_buffers) = 0; // buffers are owned by frame now
721 }
722 goto next_packet;
723
724 packet_enqueue:
725 to_next[0] = bi0;
726 to_next += 1;
727 n_left_to_next -= 1;
728 if (is_feature && IP6_ERROR_NONE == error0)
729 {
730 b0 = vlib_get_buffer (vm, bi0);
731 vnet_feature_next (&next0, b0);
732 }
733 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
734 n_left_to_next, bi0, next0);
735
Klement Sekerade34c352019-06-25 11:19:22 +0000736 next_packet:
737 from += 1;
738 n_left_from -= 1;
739 }
740
741 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
742 }
743
744 clib_spinlock_unlock (&rt->lock);
745 return frame->n_vectors;
746}
747
748static char *ip6_sv_reassembly_error_strings[] = {
749#define _(sym, string) string,
750 foreach_ip6_error
751#undef _
752};
753
754VLIB_NODE_FN (ip6_sv_reass_node) (vlib_main_t * vm,
755 vlib_node_runtime_t * node,
756 vlib_frame_t * frame)
757{
758 return ip6_sv_reassembly_inline (vm, node, frame, false /* is_feature */ );
759}
760
761/* *INDENT-OFF* */
762VLIB_REGISTER_NODE (ip6_sv_reass_node) = {
763 .name = "ip6-sv-reassembly",
764 .vector_size = sizeof (u32),
765 .format_trace = format_ip6_sv_reass_trace,
766 .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
767 .error_strings = ip6_sv_reassembly_error_strings,
768 .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
769 .next_nodes =
770 {
771 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
772 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
773 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
774 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reassembly-handoff",
775 },
776};
777/* *INDENT-ON* */
778
779VLIB_NODE_FN (ip6_sv_reass_node_feature) (vlib_main_t * vm,
780 vlib_node_runtime_t * node,
781 vlib_frame_t * frame)
782{
783 return ip6_sv_reassembly_inline (vm, node, frame, true /* is_feature */ );
784}
785
786/* *INDENT-OFF* */
787VLIB_REGISTER_NODE (ip6_sv_reass_node_feature) = {
788 .name = "ip6-sv-reassembly-feature",
789 .vector_size = sizeof (u32),
790 .format_trace = format_ip6_sv_reass_trace,
791 .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
792 .error_strings = ip6_sv_reassembly_error_strings,
793 .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
794 .next_nodes =
795 {
796 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
797 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
798 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
799 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reass-feature-hoff",
800 },
801};
802/* *INDENT-ON* */
803
804/* *INDENT-OFF* */
805VNET_FEATURE_INIT (ip6_sv_reassembly_feature) = {
806 .arc_name = "ip6-unicast",
807 .node_name = "ip6-sv-reassembly-feature",
808 .runs_before = VNET_FEATURES ("ip6-lookup"),
809 .runs_after = 0,
810};
811/* *INDENT-ON* */
812
813#ifndef CLIB_MARCH_VARIANT
814static u32
815ip6_sv_reass_get_nbuckets ()
816{
817 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
818 u32 nbuckets;
819 u8 i;
820
821 nbuckets = (u32) (rm->max_reass_n / IP6_SV_REASS_HT_LOAD_FACTOR);
822
823 for (i = 0; i < 31; i++)
824 if ((1 << i) >= nbuckets)
825 break;
826 nbuckets = 1 << i;
827
828 return nbuckets;
829}
830#endif /* CLIB_MARCH_VARIANT */
831
832typedef enum
833{
834 IP6_EVENT_CONFIG_CHANGED = 1,
835} ip6_sv_reass_event_t;
836
837#ifndef CLIB_MARCH_VARIANT
838typedef struct
839{
840 int failure;
841 clib_bihash_48_8_t *new_hash;
842} ip6_rehash_cb_ctx;
843
Neale Rannsf50bac12019-12-06 05:53:17 +0000844static int
Klement Sekerade34c352019-06-25 11:19:22 +0000845ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
846{
847 ip6_rehash_cb_ctx *ctx = _ctx;
848 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
849 {
850 ctx->failure = 1;
851 }
Neale Rannsf50bac12019-12-06 05:53:17 +0000852 return (BIHASH_WALK_CONTINUE);
Klement Sekerade34c352019-06-25 11:19:22 +0000853}
854
855static void
856ip6_sv_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
857 u32 max_reassembly_length,
858 u32 expire_walk_interval_ms)
859{
860 ip6_sv_reass_main.timeout_ms = timeout_ms;
861 ip6_sv_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
862 ip6_sv_reass_main.max_reass_n = max_reassemblies;
863 ip6_sv_reass_main.max_reass_len = max_reassembly_length;
864 ip6_sv_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
865}
866
867vnet_api_error_t
868ip6_sv_reass_set (u32 timeout_ms, u32 max_reassemblies,
869 u32 max_reassembly_length, u32 expire_walk_interval_ms)
870{
871 u32 old_nbuckets = ip6_sv_reass_get_nbuckets ();
872 ip6_sv_reass_set_params (timeout_ms, max_reassemblies,
873 max_reassembly_length, expire_walk_interval_ms);
874 vlib_process_signal_event (ip6_sv_reass_main.vlib_main,
875 ip6_sv_reass_main.ip6_sv_reass_expire_node_idx,
876 IP6_EVENT_CONFIG_CHANGED, 0);
877 u32 new_nbuckets = ip6_sv_reass_get_nbuckets ();
878 if (ip6_sv_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
879 {
880 clib_bihash_48_8_t new_hash;
881 clib_memset (&new_hash, 0, sizeof (new_hash));
882 ip6_rehash_cb_ctx ctx;
883 ctx.failure = 0;
884 ctx.new_hash = &new_hash;
885 clib_bihash_init_48_8 (&new_hash, "ip6-sv-reass", new_nbuckets,
886 new_nbuckets * 1024);
887 clib_bihash_foreach_key_value_pair_48_8 (&ip6_sv_reass_main.hash,
888 ip6_rehash_cb, &ctx);
889 if (ctx.failure)
890 {
891 clib_bihash_free_48_8 (&new_hash);
892 return -1;
893 }
894 else
895 {
896 clib_bihash_free_48_8 (&ip6_sv_reass_main.hash);
897 clib_memcpy_fast (&ip6_sv_reass_main.hash, &new_hash,
898 sizeof (ip6_sv_reass_main.hash));
899 clib_bihash_copied (&ip6_sv_reass_main.hash, &new_hash);
900 }
901 }
902 return 0;
903}
904
905vnet_api_error_t
906ip6_sv_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
907 u32 * max_reassembly_length, u32 * expire_walk_interval_ms)
908{
909 *timeout_ms = ip6_sv_reass_main.timeout_ms;
910 *max_reassemblies = ip6_sv_reass_main.max_reass_n;
911 *max_reassembly_length = ip6_sv_reass_main.max_reass_len;
912 *expire_walk_interval_ms = ip6_sv_reass_main.expire_walk_interval_ms;
913 return 0;
914}
915
916static clib_error_t *
917ip6_sv_reass_init_function (vlib_main_t * vm)
918{
919 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
920 clib_error_t *error = 0;
921 u32 nbuckets;
922 vlib_node_t *node;
923
924 rm->vlib_main = vm;
925 rm->vnet_main = vnet_get_main ();
926
927 vec_validate (rm->per_thread_data, vlib_num_workers ());
928 ip6_sv_reass_per_thread_t *rt;
929 vec_foreach (rt, rm->per_thread_data)
930 {
931 clib_spinlock_init (&rt->lock);
932 pool_alloc (rt->pool, rm->max_reass_n);
933 rt->lru_first = rt->lru_last = ~0;
934 }
935
936 node = vlib_get_node_by_name (vm, (u8 *) "ip6-sv-reassembly-expire-walk");
937 ASSERT (node);
938 rm->ip6_sv_reass_expire_node_idx = node->index;
939
940 ip6_sv_reass_set_params (IP6_SV_REASS_TIMEOUT_DEFAULT_MS,
941 IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT,
942 IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
943 IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
944
945 nbuckets = ip6_sv_reass_get_nbuckets ();
946 clib_bihash_init_48_8 (&rm->hash, "ip6-sv-reass", nbuckets,
947 nbuckets * 1024);
948
949 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
950 ASSERT (node);
951 rm->ip6_drop_idx = node->index;
952 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
953 ASSERT (node);
954 rm->ip6_icmp_error_idx = node->index;
955
956 if ((error = vlib_call_init_function (vm, ip_main_init)))
957 return error;
Klement Sekerade34c352019-06-25 11:19:22 +0000958
959 rm->fq_index = vlib_frame_queue_main_init (ip6_sv_reass_node.index, 0);
960 rm->fq_feature_index =
961 vlib_frame_queue_main_init (ip6_sv_reass_node_feature.index, 0);
962
Klement Sekera63c73532019-09-30 14:35:36 +0000963 rm->feature_use_refcount_per_intf = NULL;
964
Klement Sekerade34c352019-06-25 11:19:22 +0000965 return error;
966}
967
968VLIB_INIT_FUNCTION (ip6_sv_reass_init_function);
969#endif /* CLIB_MARCH_VARIANT */
970
971static uword
Klement Sekera42cec0e2021-08-02 16:14:15 +0200972ip6_sv_reass_walk_expired (vlib_main_t *vm,
973 CLIB_UNUSED (vlib_node_runtime_t *node),
974 CLIB_UNUSED (vlib_frame_t *f))
Klement Sekerade34c352019-06-25 11:19:22 +0000975{
976 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
977 uword event_type, *event_data = 0;
978
979 while (true)
980 {
981 vlib_process_wait_for_event_or_clock (vm,
982 (f64) rm->expire_walk_interval_ms
983 / (f64) MSEC_PER_SEC);
984 event_type = vlib_process_get_events (vm, &event_data);
985
986 switch (event_type)
987 {
Klement Sekera42cec0e2021-08-02 16:14:15 +0200988 case ~0:
989 /* no events => timeout */
990 /* fallthrough */
Klement Sekerade34c352019-06-25 11:19:22 +0000991 case IP6_EVENT_CONFIG_CHANGED:
Klement Sekera42cec0e2021-08-02 16:14:15 +0200992 /* nothing to do here */
Klement Sekerade34c352019-06-25 11:19:22 +0000993 break;
994 default:
995 clib_warning ("BUG: event type 0x%wx", event_type);
996 break;
997 }
998 f64 now = vlib_time_now (vm);
999
1000 ip6_sv_reass_t *reass;
1001 int *pool_indexes_to_free = NULL;
1002
1003 uword thread_index = 0;
1004 int index;
1005 const uword nthreads = vlib_num_workers () + 1;
Klement Sekerade34c352019-06-25 11:19:22 +00001006 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1007 {
1008 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1009 clib_spinlock_lock (&rt->lock);
1010
1011 vec_reset_length (pool_indexes_to_free);
1012 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001013 pool_foreach_index (index, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001014 reass = pool_elt_at_index (rt->pool, index);
1015 if (now > reass->last_heard + rm->timeout)
1016 {
1017 vec_add1 (pool_indexes_to_free, index);
1018 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001019 }
Klement Sekerade34c352019-06-25 11:19:22 +00001020 /* *INDENT-ON* */
1021 int *i;
1022 /* *INDENT-OFF* */
1023 vec_foreach (i, pool_indexes_to_free)
1024 {
1025 ip6_sv_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1026 ip6_sv_reass_free (vm, rm, rt, reass);
1027 }
1028 /* *INDENT-ON* */
1029
1030 clib_spinlock_unlock (&rt->lock);
1031 }
1032
Klement Sekerade34c352019-06-25 11:19:22 +00001033 vec_free (pool_indexes_to_free);
Klement Sekerade34c352019-06-25 11:19:22 +00001034 if (event_data)
1035 {
1036 _vec_len (event_data) = 0;
1037 }
1038 }
1039
1040 return 0;
1041}
1042
1043/* *INDENT-OFF* */
1044VLIB_REGISTER_NODE (ip6_sv_reass_expire_node) = {
1045 .function = ip6_sv_reass_walk_expired,
1046 .format_trace = format_ip6_sv_reass_trace,
1047 .type = VLIB_NODE_TYPE_PROCESS,
1048 .name = "ip6-sv-reassembly-expire-walk",
1049
1050 .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
1051 .error_strings = ip6_sv_reassembly_error_strings,
1052
1053};
1054/* *INDENT-ON* */
1055
1056static u8 *
1057format_ip6_sv_reass_key (u8 * s, va_list * args)
1058{
1059 ip6_sv_reass_key_t *key = va_arg (*args, ip6_sv_reass_key_t *);
1060 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1061 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1062 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1063 return s;
1064}
1065
1066static u8 *
1067format_ip6_sv_reass (u8 * s, va_list * args)
1068{
1069 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1070 ip6_sv_reass_t *reass = va_arg (*args, ip6_sv_reass_t *);
1071
1072 s = format (s, "ID: %lu, key: %U, trace_op_counter: %u\n",
1073 reass->id, format_ip6_sv_reass_key, &reass->key,
1074 reass->trace_op_counter);
1075 vlib_buffer_t *b;
1076 u32 *bip;
1077 u32 counter = 0;
1078 vec_foreach (bip, reass->cached_buffers)
1079 {
1080 u32 bi = *bip;
1081 do
1082 {
1083 b = vlib_get_buffer (vm, bi);
1084 s = format (s, " #%03u: bi: %u\n", counter, bi);
1085 ++counter;
1086 bi = b->next_buffer;
1087 }
1088 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
1089 }
1090 return s;
1091}
1092
1093static clib_error_t *
1094show_ip6_sv_reass (vlib_main_t * vm, unformat_input_t * input,
1095 CLIB_UNUSED (vlib_cli_command_t * lmd))
1096{
1097 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1098
1099 vlib_cli_output (vm, "---------------------");
1100 vlib_cli_output (vm, "IP6 reassembly status");
1101 vlib_cli_output (vm, "---------------------");
1102 bool details = false;
1103 if (unformat (input, "details"))
1104 {
1105 details = true;
1106 }
1107
1108 u32 sum_reass_n = 0;
1109 u64 sum_buffers_n = 0;
1110 ip6_sv_reass_t *reass;
1111 uword thread_index;
1112 const uword nthreads = vlib_num_workers () + 1;
1113 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1114 {
1115 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1116 clib_spinlock_lock (&rt->lock);
1117 if (details)
1118 {
1119 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001120 pool_foreach (reass, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001121 vlib_cli_output (vm, "%U", format_ip6_sv_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001122 }
Klement Sekerade34c352019-06-25 11:19:22 +00001123 /* *INDENT-ON* */
1124 }
1125 sum_reass_n += rt->reass_n;
1126 clib_spinlock_unlock (&rt->lock);
1127 }
1128 vlib_cli_output (vm, "---------------------");
1129 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1130 (long unsigned) sum_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001131 vlib_cli_output (vm,
1132 "Maximum configured concurrent shallow virtual IP6 reassemblies per worker-thread: %lu\n",
1133 (long unsigned) rm->max_reass_n);
1134 vlib_cli_output (vm,
Anton Nikolaev74a4a702021-02-17 14:45:40 +05001135 "Maximum configured amount of fragments per shallow "
1136 "virtual IP6 reassembly: %lu\n",
1137 (long unsigned) rm->max_reass_len);
1138 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001139 "Maximum configured shallow virtual IP6 reassembly timeout: %lums\n",
1140 (long unsigned) rm->timeout_ms);
1141 vlib_cli_output (vm,
1142 "Maximum configured shallow virtual IP6 reassembly expire walk interval: %lums\n",
1143 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekerade34c352019-06-25 11:19:22 +00001144 vlib_cli_output (vm, "Buffers in use: %lu\n",
1145 (long unsigned) sum_buffers_n);
1146 return 0;
1147}
1148
1149/* *INDENT-OFF* */
1150VLIB_CLI_COMMAND (show_ip6_sv_reassembly_cmd, static) = {
1151 .path = "show ip6-sv-reassembly",
1152 .short_help = "show ip6-sv-reassembly [details]",
1153 .function = show_ip6_sv_reass,
1154};
1155/* *INDENT-ON* */
1156
1157#ifndef CLIB_MARCH_VARIANT
1158vnet_api_error_t
1159ip6_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1160{
Klement Sekeraf126e742019-10-10 09:46:06 +00001161 return ip6_sv_reass_enable_disable_with_refcnt (sw_if_index,
1162 enable_disable);
Klement Sekerade34c352019-06-25 11:19:22 +00001163}
1164#endif /* CLIB_MARCH_VARIANT */
1165
1166#define foreach_ip6_sv_reassembly_handoff_error \
1167_(CONGESTION_DROP, "congestion drop")
1168
1169
1170typedef enum
1171{
1172#define _(sym,str) IP6_SV_REASSEMBLY_HANDOFF_ERROR_##sym,
1173 foreach_ip6_sv_reassembly_handoff_error
1174#undef _
1175 IP6_SV_REASSEMBLY_HANDOFF_N_ERROR,
1176} ip6_sv_reassembly_handoff_error_t;
1177
1178static char *ip6_sv_reassembly_handoff_error_strings[] = {
1179#define _(sym,string) string,
1180 foreach_ip6_sv_reassembly_handoff_error
1181#undef _
1182};
1183
1184typedef struct
1185{
1186 u32 next_worker_index;
1187} ip6_sv_reassembly_handoff_trace_t;
1188
1189static u8 *
1190format_ip6_sv_reassembly_handoff_trace (u8 * s, va_list * args)
1191{
1192 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1193 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1194 ip6_sv_reassembly_handoff_trace_t *t =
1195 va_arg (*args, ip6_sv_reassembly_handoff_trace_t *);
1196
1197 s =
1198 format (s, "ip6-sv-reassembly-handoff: next-worker %d",
1199 t->next_worker_index);
1200
1201 return s;
1202}
1203
1204always_inline uword
1205ip6_sv_reassembly_handoff_inline (vlib_main_t * vm,
1206 vlib_node_runtime_t * node,
1207 vlib_frame_t * frame, bool is_feature)
1208{
1209 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1210
1211 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1212 u32 n_enq, n_left_from, *from;
1213 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1214 u32 fq_index;
1215
1216 from = vlib_frame_vector_args (frame);
1217 n_left_from = frame->n_vectors;
1218 vlib_get_buffers (vm, from, bufs, n_left_from);
1219
1220 b = bufs;
1221 ti = thread_indices;
1222
1223 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1224
1225 while (n_left_from > 0)
1226 {
1227 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1228
1229 if (PREDICT_FALSE
1230 ((node->flags & VLIB_NODE_FLAG_TRACE)
1231 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1232 {
1233 ip6_sv_reassembly_handoff_trace_t *t =
1234 vlib_add_trace (vm, node, b[0], sizeof (*t));
1235 t->next_worker_index = ti[0];
1236 }
1237
1238 n_left_from -= 1;
1239 ti += 1;
1240 b += 1;
1241 }
Damjan Marion9e7a0b42021-05-14 14:50:01 +02001242 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
1243 thread_indices, frame->n_vectors, 1);
Klement Sekerade34c352019-06-25 11:19:22 +00001244
1245 if (n_enq < frame->n_vectors)
1246 vlib_node_increment_counter (vm, node->node_index,
1247 IP6_SV_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1248 frame->n_vectors - n_enq);
1249 return frame->n_vectors;
1250}
1251
1252VLIB_NODE_FN (ip6_sv_reassembly_handoff_node) (vlib_main_t * vm,
1253 vlib_node_runtime_t * node,
1254 vlib_frame_t * frame)
1255{
1256 return ip6_sv_reassembly_handoff_inline (vm, node, frame,
1257 false /* is_feature */ );
1258}
1259
1260/* *INDENT-OFF* */
1261VLIB_REGISTER_NODE (ip6_sv_reassembly_handoff_node) = {
1262 .name = "ip6-sv-reassembly-handoff",
1263 .vector_size = sizeof (u32),
1264 .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1265 .error_strings = ip6_sv_reassembly_handoff_error_strings,
1266 .format_trace = format_ip6_sv_reassembly_handoff_trace,
1267
1268 .n_next_nodes = 1,
1269
1270 .next_nodes = {
1271 [0] = "error-drop",
1272 },
1273};
1274
1275
1276VLIB_NODE_FN (ip6_sv_reassembly_feature_handoff_node) (vlib_main_t * vm,
1277 vlib_node_runtime_t * node, vlib_frame_t * frame)
1278{
1279 return ip6_sv_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1280}
1281
1282
1283/* *INDENT-OFF* */
1284VLIB_REGISTER_NODE (ip6_sv_reassembly_feature_handoff_node) = {
1285 .name = "ip6-sv-reass-feature-hoff",
1286 .vector_size = sizeof (u32),
1287 .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1288 .error_strings = ip6_sv_reassembly_handoff_error_strings,
1289 .format_trace = format_ip6_sv_reassembly_handoff_trace,
1290
1291 .n_next_nodes = 1,
1292
1293 .next_nodes = {
1294 [0] = "error-drop",
1295 },
1296};
1297/* *INDENT-ON* */
1298
1299#ifndef CLIB_MARCH_VARIANT
1300int
1301ip6_sv_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1302{
1303 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
Klement Sekera63c73532019-09-30 14:35:36 +00001304 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
Klement Sekerade34c352019-06-25 11:19:22 +00001305 if (is_enable)
1306 {
Klement Sekera63c73532019-09-30 14:35:36 +00001307 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001308 {
Klement Sekera63c73532019-09-30 14:35:36 +00001309 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001310 return vnet_feature_enable_disable ("ip6-unicast",
1311 "ip6-sv-reassembly-feature",
1312 sw_if_index, 1, 0, 0);
1313 }
Klement Sekera63c73532019-09-30 14:35:36 +00001314 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001315 }
1316 else
1317 {
Klement Sekera63c73532019-09-30 14:35:36 +00001318 --rm->feature_use_refcount_per_intf[sw_if_index];
1319 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001320 return vnet_feature_enable_disable ("ip6-unicast",
1321 "ip6-sv-reassembly-feature",
1322 sw_if_index, 0, 0, 0);
1323 }
Klement Sekera407f5932019-12-11 13:06:27 +00001324 return 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001325}
1326#endif
1327
1328/*
1329 * fd.io coding-style-patch-verification: ON
1330 *
1331 * Local Variables:
1332 * eval: (c-set-style "gnu")
1333 * End:
1334 */