blob: d5218a4fb1d26890cbe3f022b0a2de25054372aa [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,
225 ip6_sv_reass_main_t * rm,
226 ip6_sv_reass_t * reass, u32 bi,
227 ip6_sv_reass_trace_operation_e action,
228 u32 ip_proto, u16 l4_src_port, u16 l4_dst_port)
229{
230 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Klement Sekera53be16d2020-12-15 21:47:36 +0100231 if (pool_is_free_index
232 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
233 {
234 // this buffer's trace is gone
235 b->flags &= ~VLIB_BUFFER_IS_TRACED;
236 return;
237 }
Klement Sekerade34c352019-06-25 11:19:22 +0000238 ip6_sv_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekeraf126e742019-10-10 09:46:06 +0000239 if (reass)
240 {
241 t->reass_id = reass->id;
242 t->op_id = reass->trace_op_counter;
243 ++reass->trace_op_counter;
244 }
Klement Sekerade34c352019-06-25 11:19:22 +0000245 t->action = action;
Klement Sekerade34c352019-06-25 11:19:22 +0000246 t->ip_proto = ip_proto;
247 t->l4_src_port = l4_src_port;
248 t->l4_dst_port = l4_dst_port;
Klement Sekerade34c352019-06-25 11:19:22 +0000249#if 0
250 static u8 *s = NULL;
251 s = format (s, "%U", format_ip6_sv_reass_trace, NULL, NULL, t);
252 printf ("%.*s\n", vec_len (s), s);
253 fflush (stdout);
254 vec_reset_length (s);
255#endif
256}
257
258always_inline void
259ip6_sv_reass_free (vlib_main_t * vm, ip6_sv_reass_main_t * rm,
260 ip6_sv_reass_per_thread_t * rt, ip6_sv_reass_t * reass)
261{
262 clib_bihash_kv_48_8_t kv;
263 kv.key[0] = reass->key.as_u64[0];
264 kv.key[1] = reass->key.as_u64[1];
265 kv.key[2] = reass->key.as_u64[2];
266 kv.key[3] = reass->key.as_u64[3];
267 kv.key[4] = reass->key.as_u64[4];
268 kv.key[5] = reass->key.as_u64[5];
269 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
270 vlib_buffer_free (vm, reass->cached_buffers,
271 vec_len (reass->cached_buffers));
272 vec_free (reass->cached_buffers);
273 reass->cached_buffers = NULL;
274 if (~0 != reass->lru_prev)
275 {
276 ip6_sv_reass_t *lru_prev =
277 pool_elt_at_index (rt->pool, reass->lru_prev);
278 lru_prev->lru_next = reass->lru_next;
279 }
280 if (~0 != reass->lru_next)
281 {
282 ip6_sv_reass_t *lru_next =
283 pool_elt_at_index (rt->pool, reass->lru_next);
284 lru_next->lru_prev = reass->lru_prev;
285 }
286 if (rt->lru_first == reass - rt->pool)
287 {
288 rt->lru_first = reass->lru_next;
289 }
290 if (rt->lru_last == reass - rt->pool)
291 {
292 rt->lru_last = reass->lru_prev;
293 }
294 pool_put (rt->pool, reass);
295 --rt->reass_n;
296}
297
298always_inline void
299ip6_sv_reass_init (ip6_sv_reass_t * reass)
300{
301 reass->cached_buffers = NULL;
302 reass->is_complete = false;
303}
304
305always_inline ip6_sv_reass_t *
306ip6_sv_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
307 ip6_sv_reass_main_t * rm,
308 ip6_sv_reass_per_thread_t * rt,
309 ip6_sv_reass_kv_t * kv, u32 * icmp_bi,
310 u8 * do_handoff)
311{
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 Sekerac99c0252019-12-18 12:17:06 +0000315 if (!clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
Klement Sekerade34c352019-06-25 11:19:22 +0000316 {
317 if (vm->thread_index != kv->v.thread_index)
318 {
319 *do_handoff = 1;
320 return NULL;
321 }
322 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
323
324 if (now > reass->last_heard + rm->timeout)
325 {
326 ip6_sv_reass_free (vm, rm, rt, reass);
327 reass = NULL;
328 }
329 }
330
331 if (reass)
332 {
333 reass->last_heard = now;
334 return reass;
335 }
336
337 if (rt->reass_n >= rm->max_reass_n)
338 {
zhengdelunce533632020-05-22 15:42:28 +0800339 reass = pool_elt_at_index (rt->pool, rt->lru_first);
Klement Sekerade34c352019-06-25 11:19:22 +0000340 ip6_sv_reass_free (vm, rm, rt, reass);
341 }
342
343 pool_get (rt->pool, reass);
344 clib_memset (reass, 0, sizeof (*reass));
345 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
346 ++rt->id_counter;
347 ip6_sv_reass_init (reass);
348 ++rt->reass_n;
349
350 reass->lru_prev = reass->lru_next = ~0;
351
352 if (~0 != rt->lru_last)
353 {
354 ip6_sv_reass_t *lru_last = pool_elt_at_index (rt->pool, rt->lru_last);
355 reass->lru_prev = rt->lru_last;
356 lru_last->lru_next = rt->lru_last = reass - rt->pool;
357 }
358
359 if (~0 == rt->lru_first)
360 {
361 rt->lru_first = rt->lru_last = reass - rt->pool;
362 }
363
Klement Sekerac99c0252019-12-18 12:17:06 +0000364 reass->key.as_u64[0] = kv->kv.key[0];
365 reass->key.as_u64[1] = kv->kv.key[1];
366 reass->key.as_u64[2] = kv->kv.key[2];
367 reass->key.as_u64[3] = kv->kv.key[3];
368 reass->key.as_u64[4] = kv->kv.key[4];
369 reass->key.as_u64[5] = kv->kv.key[5];
Klement Sekerade34c352019-06-25 11:19:22 +0000370 kv->v.reass_index = (reass - rt->pool);
371 kv->v.thread_index = vm->thread_index;
372 reass->last_heard = now;
373
Klement Sekerac99c0252019-12-18 12:17:06 +0000374 if (clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 1))
Klement Sekerade34c352019-06-25 11:19:22 +0000375 {
376 ip6_sv_reass_free (vm, rm, rt, reass);
377 reass = NULL;
378 }
379
380 return reass;
381}
382
383always_inline ip6_sv_reass_rc_t
384ip6_sv_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
385 ip6_sv_reass_main_t * rm, ip6_sv_reass_per_thread_t * rt,
386 ip6_sv_reass_t * reass, u32 bi0,
387 ip6_frag_hdr_t * frag_hdr)
388{
389 vlib_buffer_t *fb = vlib_get_buffer (vm, bi0);
390 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
391 fvnb->ip.reass.ip6_frag_hdr_offset =
392 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
393 ip6_header_t *fip = vlib_buffer_get_current (fb);
394 if (fb->current_length < sizeof (*fip) ||
395 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
396 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
397 {
398 return IP6_SV_REASS_RC_INTERNAL_ERROR;
399 }
400
401 u32 fragment_first = fvnb->ip.reass.fragment_first =
402 ip6_frag_hdr_offset_bytes (frag_hdr);
403 u32 fragment_length =
404 vlib_buffer_length_in_chain (vm, fb) -
405 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
406 u32 fragment_last = fvnb->ip.reass.fragment_last =
407 fragment_first + fragment_length - 1;
408 fvnb->ip.reass.range_first = fragment_first;
409 fvnb->ip.reass.range_last = fragment_last;
410 fvnb->ip.reass.next_range_bi = ~0;
411 if (0 == fragment_first)
412 {
Klement Sekeraf126e742019-10-10 09:46:06 +0000413 if (!ip6_get_port
414 (vm, fb, fip, fb->current_length, &reass->ip_proto,
415 &reass->l4_src_port, &reass->l4_dst_port,
416 &reass->icmp_type_or_tcp_flags, &reass->tcp_ack_number,
417 &reass->tcp_seq_number))
Klement Sekerade34c352019-06-25 11:19:22 +0000418 return IP6_SV_REASS_RC_UNSUPP_IP_PROTO;
Klement Sekeraf126e742019-10-10 09:46:06 +0000419
Klement Sekerade34c352019-06-25 11:19:22 +0000420 reass->is_complete = true;
421 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
422 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
423 {
424 ip6_sv_reass_add_trace (vm, node, rm, reass, bi0, REASS_FINISH,
425 reass->ip_proto, reass->l4_src_port,
426 reass->l4_dst_port);
427 }
428 }
429 vec_add1 (reass->cached_buffers, bi0);
430 if (!reass->is_complete)
431 {
432 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
433 {
434 ip6_sv_reass_add_trace (vm, node, rm, reass, bi0,
435 REASS_FRAGMENT_CACHE, reass->ip_proto,
436 reass->l4_src_port, reass->l4_dst_port);
437 }
438 if (vec_len (reass->cached_buffers) > rm->max_reass_len)
439 {
440 return IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS;
441 }
442 }
443 return IP6_SV_REASS_RC_OK;
444}
445
446always_inline bool
447ip6_sv_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
448 vlib_buffer_t * b,
449 ip6_frag_hdr_t * frag_hdr)
450{
451 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
452 while (ip6_ext_hdr (tmp->next_hdr))
453 {
454 tmp = ip6_ext_next_header (tmp);
455 }
456 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
457 {
458 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
459 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
460 0);
461 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
462
463 return false;
464 }
465 return true;
466}
467
468always_inline bool
469ip6_sv_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
470 vlib_node_runtime_t * node,
471 vlib_buffer_t * b,
472 ip6_frag_hdr_t * frag_hdr)
473{
474 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
475 ip6_header_t *ip = vlib_buffer_get_current (b);
476 int more_fragments = ip6_frag_hdr_more (frag_hdr);
477 u32 fragment_length =
478 vlib_buffer_length_in_chain (vm, b) -
479 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
480 if (more_fragments && 0 != fragment_length % 8)
481 {
482 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
483 ICMP6_parameter_problem_erroneous_header_field,
484 (u8 *) & ip->payload_length - (u8 *) ip);
485 return false;
486 }
487 return true;
488}
489
490always_inline bool
491ip6_sv_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
492 vlib_node_runtime_t * node,
493 vlib_buffer_t * b,
494 ip6_frag_hdr_t * frag_hdr)
495{
496 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
497 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
498 u32 fragment_length =
499 vlib_buffer_length_in_chain (vm, b) -
500 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
501 if (fragment_first + fragment_length > 65535)
502 {
503 ip6_header_t *ip0 = vlib_buffer_get_current (b);
504 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
505 ICMP6_parameter_problem_erroneous_header_field,
506 (u8 *) & frag_hdr->fragment_offset_and_more
507 - (u8 *) ip0);
508 return false;
509 }
510 return true;
511}
512
513always_inline uword
514ip6_sv_reassembly_inline (vlib_main_t * vm,
515 vlib_node_runtime_t * node,
516 vlib_frame_t * frame, bool is_feature)
517{
518 u32 *from = vlib_frame_vector_args (frame);
519 u32 n_left_from, n_left_to_next, *to_next, next_index;
520 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
521 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
522 clib_spinlock_lock (&rt->lock);
523
524 n_left_from = frame->n_vectors;
525 next_index = node->cached_next_index;
526
527 while (n_left_from > 0)
528 {
529 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
530
531 while (n_left_from > 0 && n_left_to_next > 0)
532 {
533 u32 bi0;
534 vlib_buffer_t *b0;
535 u32 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
536 u32 error0 = IP6_ERROR_NONE;
537 u32 icmp_bi = ~0;
538
539 bi0 = from[0];
540 b0 = vlib_get_buffer (vm, bi0);
541
542 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
543 ip6_frag_hdr_t *frag_hdr = NULL;
544 ip6_ext_header_t *prev_hdr;
545 if (ip6_ext_hdr (ip0->protocol))
546 {
547 frag_hdr =
548 ip6_ext_header_find (vm, b0, ip0,
549 IP_PROTOCOL_IPV6_FRAGMENTATION,
550 &prev_hdr);
551 }
552 if (!frag_hdr)
553 {
554 // this is a regular packet - no fragmentation
Klement Sekeraf126e742019-10-10 09:46:06 +0000555 if (!ip6_get_port
556 (vm, b0, ip0, b0->current_length,
557 &(vnet_buffer (b0)->ip.reass.ip_proto),
558 &(vnet_buffer (b0)->ip.reass.l4_src_port),
559 &(vnet_buffer (b0)->ip.reass.l4_dst_port),
560 &(vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags),
561 &(vnet_buffer (b0)->ip.reass.tcp_ack_number),
562 &(vnet_buffer (b0)->ip.reass.tcp_seq_number)))
563 {
564 error0 = IP6_ERROR_REASS_UNSUPP_IP_PROTO;
Klement Sekera1766ddc2020-03-30 16:59:38 +0200565 b0->error = node->errors[error0];
Klement Sekeraf126e742019-10-10 09:46:06 +0000566 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
567 goto packet_enqueue;
568 }
Klement Sekeraf126e742019-10-10 09:46:06 +0000569 vnet_buffer (b0)->ip.reass.is_non_first_fragment = 0;
Klement Sekerade34c352019-06-25 11:19:22 +0000570 next0 = IP6_SV_REASSEMBLY_NEXT_INPUT;
Klement Sekeraf126e742019-10-10 09:46:06 +0000571 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
572 {
573 ip6_sv_reass_add_trace (vm, node, rm, NULL, bi0,
574 REASS_PASSTHROUGH,
575 vnet_buffer (b0)->ip.reass.ip_proto,
576 vnet_buffer (b0)->ip.
577 reass.l4_src_port,
578 vnet_buffer (b0)->ip.
579 reass.l4_dst_port);
580 }
Klement Sekerade34c352019-06-25 11:19:22 +0000581 goto packet_enqueue;
582 }
Klement Sekera38f7ccb2019-10-28 11:26:28 +0000583 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
584 (u8 *) frag_hdr - (u8 *) ip0;
Klement Sekerade34c352019-06-25 11:19:22 +0000585 if (0 == ip6_frag_hdr_offset (frag_hdr))
586 {
587 // first fragment - verify upper-layer is present
588 if (!ip6_sv_reass_verify_upper_layer_present
589 (node, b0, frag_hdr))
590 {
591 next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
592 goto packet_enqueue;
593 }
594 }
595 if (!ip6_sv_reass_verify_fragment_multiple_8
596 (vm, node, b0, frag_hdr)
597 || !ip6_sv_reass_verify_packet_size_lt_64k (vm, node, b0,
598 frag_hdr))
599 {
600 next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
601 goto packet_enqueue;
602 }
Klement Sekerade34c352019-06-25 11:19:22 +0000603
604 ip6_sv_reass_kv_t kv;
605 u8 do_handoff = 0;
606
607 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
608 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
609 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
610 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
611 kv.k.as_u64[4] =
612 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
613 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
614 (u64) frag_hdr->identification;
615 kv.k.as_u64[5] = ip0->protocol;
616
617 ip6_sv_reass_t *reass =
618 ip6_sv_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
619 &do_handoff);
620
621 if (PREDICT_FALSE (do_handoff))
622 {
623 next0 = IP6_SV_REASSEMBLY_NEXT_HANDOFF;
624 vnet_buffer (b0)->ip.reass.owner_thread_index =
625 kv.v.thread_index;
Klement Sekera364b20a2019-10-07 09:48:06 +0000626 goto packet_enqueue;
Klement Sekerade34c352019-06-25 11:19:22 +0000627 }
628
629 if (!reass)
630 {
631 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
632 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
Klement Sekera1766ddc2020-03-30 16:59:38 +0200633 b0->error = node->errors[error0];
Klement Sekerade34c352019-06-25 11:19:22 +0000634 goto packet_enqueue;
635 }
636
637 if (reass->is_complete)
638 {
Klement Sekeraf126e742019-10-10 09:46:06 +0000639 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
640 ! !ip6_frag_hdr_offset (frag_hdr);
Klement Sekerade34c352019-06-25 11:19:22 +0000641 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +0000642 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
643 reass->icmp_type_or_tcp_flags;
644 vnet_buffer (b0)->ip.reass.tcp_ack_number =
645 reass->tcp_ack_number;
646 vnet_buffer (b0)->ip.reass.tcp_seq_number =
647 reass->tcp_seq_number;
Klement Sekerade34c352019-06-25 11:19:22 +0000648 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
649 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
650 next0 = IP6_SV_REASSEMBLY_NEXT_INPUT;
Klement Sekerade34c352019-06-25 11:19:22 +0000651 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
652 {
653 ip6_sv_reass_add_trace (vm, node, rm, reass, bi0,
654 REASS_FRAGMENT_FORWARD,
655 reass->ip_proto,
656 reass->l4_src_port,
657 reass->l4_dst_port);
658 }
659 goto packet_enqueue;
660 }
661
662 switch (ip6_sv_reass_update
663 (vm, node, rm, rt, reass, bi0, frag_hdr))
664 {
665 case IP6_SV_REASS_RC_OK:
666 /* nothing to do here */
667 break;
668 case IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS:
669 vlib_node_increment_counter (vm, node->node_index,
670 IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
671 1);
672 ip6_sv_reass_free (vm, rm, rt, reass);
673 goto next_packet;
674 break;
675 case IP6_SV_REASS_RC_UNSUPP_IP_PROTO:
676 vlib_node_increment_counter (vm, node->node_index,
677 IP6_ERROR_REASS_UNSUPP_IP_PROTO,
678 1);
679 ip6_sv_reass_free (vm, rm, rt, reass);
680 goto next_packet;
681 break;
682 case IP6_SV_REASS_RC_INTERNAL_ERROR:
683 vlib_node_increment_counter (vm, node->node_index,
684 IP6_ERROR_REASS_INTERNAL_ERROR, 1);
685 ip6_sv_reass_free (vm, rm, rt, reass);
686 goto next_packet;
687 break;
688 }
689
Klement Sekerade34c352019-06-25 11:19:22 +0000690 if (reass->is_complete)
691 {
692 u32 idx;
693 vec_foreach_index (idx, reass->cached_buffers)
694 {
695 u32 bi0 = vec_elt (reass->cached_buffers, idx);
696 if (0 == n_left_to_next)
697 {
698 vlib_put_next_frame (vm, node, next_index,
699 n_left_to_next);
700 vlib_get_next_frame (vm, node, next_index, to_next,
701 n_left_to_next);
702 }
703 to_next[0] = bi0;
704 to_next += 1;
705 n_left_to_next -= 1;
706 b0 = vlib_get_buffer (vm, bi0);
707 if (is_feature)
708 {
709 vnet_feature_next (&next0, b0);
710 }
Klement Sekeraf126e742019-10-10 09:46:06 +0000711 frag_hdr =
712 vlib_buffer_get_current (b0) +
713 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset;
Klement Sekeraf126e742019-10-10 09:46:06 +0000714 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
715 ! !ip6_frag_hdr_offset (frag_hdr);
Klement Sekerade34c352019-06-25 11:19:22 +0000716 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
Klement Sekeraf126e742019-10-10 09:46:06 +0000717 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
718 reass->icmp_type_or_tcp_flags;
719 vnet_buffer (b0)->ip.reass.tcp_ack_number =
720 reass->tcp_ack_number;
721 vnet_buffer (b0)->ip.reass.tcp_seq_number =
722 reass->tcp_seq_number;
Klement Sekerade34c352019-06-25 11:19:22 +0000723 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
724 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
725 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
726 {
727 ip6_sv_reass_add_trace (vm, node, rm, reass, bi0,
728 REASS_FRAGMENT_FORWARD,
729 reass->ip_proto,
730 reass->l4_src_port,
731 reass->l4_dst_port);
732 }
733 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
734 to_next, n_left_to_next, bi0,
735 next0);
736 }
737 _vec_len (reass->cached_buffers) = 0; // buffers are owned by frame now
738 }
739 goto next_packet;
740
741 packet_enqueue:
742 to_next[0] = bi0;
743 to_next += 1;
744 n_left_to_next -= 1;
745 if (is_feature && IP6_ERROR_NONE == error0)
746 {
747 b0 = vlib_get_buffer (vm, bi0);
748 vnet_feature_next (&next0, b0);
749 }
750 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
751 n_left_to_next, bi0, next0);
752
753 if (~0 != icmp_bi)
754 {
755 next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
756 to_next[0] = icmp_bi;
757 to_next += 1;
758 n_left_to_next -= 1;
759 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
760 n_left_to_next, icmp_bi,
761 next0);
762 }
763
764 next_packet:
765 from += 1;
766 n_left_from -= 1;
767 }
768
769 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
770 }
771
772 clib_spinlock_unlock (&rt->lock);
773 return frame->n_vectors;
774}
775
776static char *ip6_sv_reassembly_error_strings[] = {
777#define _(sym, string) string,
778 foreach_ip6_error
779#undef _
780};
781
782VLIB_NODE_FN (ip6_sv_reass_node) (vlib_main_t * vm,
783 vlib_node_runtime_t * node,
784 vlib_frame_t * frame)
785{
786 return ip6_sv_reassembly_inline (vm, node, frame, false /* is_feature */ );
787}
788
789/* *INDENT-OFF* */
790VLIB_REGISTER_NODE (ip6_sv_reass_node) = {
791 .name = "ip6-sv-reassembly",
792 .vector_size = sizeof (u32),
793 .format_trace = format_ip6_sv_reass_trace,
794 .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
795 .error_strings = ip6_sv_reassembly_error_strings,
796 .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
797 .next_nodes =
798 {
799 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
800 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
801 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
802 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reassembly-handoff",
803 },
804};
805/* *INDENT-ON* */
806
807VLIB_NODE_FN (ip6_sv_reass_node_feature) (vlib_main_t * vm,
808 vlib_node_runtime_t * node,
809 vlib_frame_t * frame)
810{
811 return ip6_sv_reassembly_inline (vm, node, frame, true /* is_feature */ );
812}
813
814/* *INDENT-OFF* */
815VLIB_REGISTER_NODE (ip6_sv_reass_node_feature) = {
816 .name = "ip6-sv-reassembly-feature",
817 .vector_size = sizeof (u32),
818 .format_trace = format_ip6_sv_reass_trace,
819 .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
820 .error_strings = ip6_sv_reassembly_error_strings,
821 .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
822 .next_nodes =
823 {
824 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
825 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
826 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
827 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reass-feature-hoff",
828 },
829};
830/* *INDENT-ON* */
831
832/* *INDENT-OFF* */
833VNET_FEATURE_INIT (ip6_sv_reassembly_feature) = {
834 .arc_name = "ip6-unicast",
835 .node_name = "ip6-sv-reassembly-feature",
836 .runs_before = VNET_FEATURES ("ip6-lookup"),
837 .runs_after = 0,
838};
839/* *INDENT-ON* */
840
841#ifndef CLIB_MARCH_VARIANT
842static u32
843ip6_sv_reass_get_nbuckets ()
844{
845 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
846 u32 nbuckets;
847 u8 i;
848
849 nbuckets = (u32) (rm->max_reass_n / IP6_SV_REASS_HT_LOAD_FACTOR);
850
851 for (i = 0; i < 31; i++)
852 if ((1 << i) >= nbuckets)
853 break;
854 nbuckets = 1 << i;
855
856 return nbuckets;
857}
858#endif /* CLIB_MARCH_VARIANT */
859
860typedef enum
861{
862 IP6_EVENT_CONFIG_CHANGED = 1,
863} ip6_sv_reass_event_t;
864
865#ifndef CLIB_MARCH_VARIANT
866typedef struct
867{
868 int failure;
869 clib_bihash_48_8_t *new_hash;
870} ip6_rehash_cb_ctx;
871
Neale Rannsf50bac12019-12-06 05:53:17 +0000872static int
Klement Sekerade34c352019-06-25 11:19:22 +0000873ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
874{
875 ip6_rehash_cb_ctx *ctx = _ctx;
876 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
877 {
878 ctx->failure = 1;
879 }
Neale Rannsf50bac12019-12-06 05:53:17 +0000880 return (BIHASH_WALK_CONTINUE);
Klement Sekerade34c352019-06-25 11:19:22 +0000881}
882
883static void
884ip6_sv_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
885 u32 max_reassembly_length,
886 u32 expire_walk_interval_ms)
887{
888 ip6_sv_reass_main.timeout_ms = timeout_ms;
889 ip6_sv_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
890 ip6_sv_reass_main.max_reass_n = max_reassemblies;
891 ip6_sv_reass_main.max_reass_len = max_reassembly_length;
892 ip6_sv_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
893}
894
895vnet_api_error_t
896ip6_sv_reass_set (u32 timeout_ms, u32 max_reassemblies,
897 u32 max_reassembly_length, u32 expire_walk_interval_ms)
898{
899 u32 old_nbuckets = ip6_sv_reass_get_nbuckets ();
900 ip6_sv_reass_set_params (timeout_ms, max_reassemblies,
901 max_reassembly_length, expire_walk_interval_ms);
902 vlib_process_signal_event (ip6_sv_reass_main.vlib_main,
903 ip6_sv_reass_main.ip6_sv_reass_expire_node_idx,
904 IP6_EVENT_CONFIG_CHANGED, 0);
905 u32 new_nbuckets = ip6_sv_reass_get_nbuckets ();
906 if (ip6_sv_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
907 {
908 clib_bihash_48_8_t new_hash;
909 clib_memset (&new_hash, 0, sizeof (new_hash));
910 ip6_rehash_cb_ctx ctx;
911 ctx.failure = 0;
912 ctx.new_hash = &new_hash;
913 clib_bihash_init_48_8 (&new_hash, "ip6-sv-reass", new_nbuckets,
914 new_nbuckets * 1024);
915 clib_bihash_foreach_key_value_pair_48_8 (&ip6_sv_reass_main.hash,
916 ip6_rehash_cb, &ctx);
917 if (ctx.failure)
918 {
919 clib_bihash_free_48_8 (&new_hash);
920 return -1;
921 }
922 else
923 {
924 clib_bihash_free_48_8 (&ip6_sv_reass_main.hash);
925 clib_memcpy_fast (&ip6_sv_reass_main.hash, &new_hash,
926 sizeof (ip6_sv_reass_main.hash));
927 clib_bihash_copied (&ip6_sv_reass_main.hash, &new_hash);
928 }
929 }
930 return 0;
931}
932
933vnet_api_error_t
934ip6_sv_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
935 u32 * max_reassembly_length, u32 * expire_walk_interval_ms)
936{
937 *timeout_ms = ip6_sv_reass_main.timeout_ms;
938 *max_reassemblies = ip6_sv_reass_main.max_reass_n;
939 *max_reassembly_length = ip6_sv_reass_main.max_reass_len;
940 *expire_walk_interval_ms = ip6_sv_reass_main.expire_walk_interval_ms;
941 return 0;
942}
943
944static clib_error_t *
945ip6_sv_reass_init_function (vlib_main_t * vm)
946{
947 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
948 clib_error_t *error = 0;
949 u32 nbuckets;
950 vlib_node_t *node;
951
952 rm->vlib_main = vm;
953 rm->vnet_main = vnet_get_main ();
954
955 vec_validate (rm->per_thread_data, vlib_num_workers ());
956 ip6_sv_reass_per_thread_t *rt;
957 vec_foreach (rt, rm->per_thread_data)
958 {
959 clib_spinlock_init (&rt->lock);
960 pool_alloc (rt->pool, rm->max_reass_n);
961 rt->lru_first = rt->lru_last = ~0;
962 }
963
964 node = vlib_get_node_by_name (vm, (u8 *) "ip6-sv-reassembly-expire-walk");
965 ASSERT (node);
966 rm->ip6_sv_reass_expire_node_idx = node->index;
967
968 ip6_sv_reass_set_params (IP6_SV_REASS_TIMEOUT_DEFAULT_MS,
969 IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT,
970 IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
971 IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
972
973 nbuckets = ip6_sv_reass_get_nbuckets ();
974 clib_bihash_init_48_8 (&rm->hash, "ip6-sv-reass", nbuckets,
975 nbuckets * 1024);
976
977 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
978 ASSERT (node);
979 rm->ip6_drop_idx = node->index;
980 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
981 ASSERT (node);
982 rm->ip6_icmp_error_idx = node->index;
983
984 if ((error = vlib_call_init_function (vm, ip_main_init)))
985 return error;
Klement Sekerade34c352019-06-25 11:19:22 +0000986
987 rm->fq_index = vlib_frame_queue_main_init (ip6_sv_reass_node.index, 0);
988 rm->fq_feature_index =
989 vlib_frame_queue_main_init (ip6_sv_reass_node_feature.index, 0);
990
Klement Sekera63c73532019-09-30 14:35:36 +0000991 rm->feature_use_refcount_per_intf = NULL;
992
Klement Sekerade34c352019-06-25 11:19:22 +0000993 return error;
994}
995
996VLIB_INIT_FUNCTION (ip6_sv_reass_init_function);
997#endif /* CLIB_MARCH_VARIANT */
998
999static uword
1000ip6_sv_reass_walk_expired (vlib_main_t * vm,
1001 vlib_node_runtime_t * node, vlib_frame_t * f)
1002{
1003 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1004 uword event_type, *event_data = 0;
1005
1006 while (true)
1007 {
1008 vlib_process_wait_for_event_or_clock (vm,
1009 (f64) rm->expire_walk_interval_ms
1010 / (f64) MSEC_PER_SEC);
1011 event_type = vlib_process_get_events (vm, &event_data);
1012
1013 switch (event_type)
1014 {
1015 case ~0: /* no events => timeout */
1016 /* nothing to do here */
1017 break;
1018 case IP6_EVENT_CONFIG_CHANGED:
1019 break;
1020 default:
1021 clib_warning ("BUG: event type 0x%wx", event_type);
1022 break;
1023 }
1024 f64 now = vlib_time_now (vm);
1025
1026 ip6_sv_reass_t *reass;
1027 int *pool_indexes_to_free = NULL;
1028
1029 uword thread_index = 0;
1030 int index;
1031 const uword nthreads = vlib_num_workers () + 1;
Klement Sekerade34c352019-06-25 11:19:22 +00001032 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1033 {
1034 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1035 clib_spinlock_lock (&rt->lock);
1036
1037 vec_reset_length (pool_indexes_to_free);
1038 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001039 pool_foreach_index (index, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001040 reass = pool_elt_at_index (rt->pool, index);
1041 if (now > reass->last_heard + rm->timeout)
1042 {
1043 vec_add1 (pool_indexes_to_free, index);
1044 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001045 }
Klement Sekerade34c352019-06-25 11:19:22 +00001046 /* *INDENT-ON* */
1047 int *i;
1048 /* *INDENT-OFF* */
1049 vec_foreach (i, pool_indexes_to_free)
1050 {
1051 ip6_sv_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1052 ip6_sv_reass_free (vm, rm, rt, reass);
1053 }
1054 /* *INDENT-ON* */
1055
1056 clib_spinlock_unlock (&rt->lock);
1057 }
1058
Klement Sekerade34c352019-06-25 11:19:22 +00001059 vec_free (pool_indexes_to_free);
Klement Sekerade34c352019-06-25 11:19:22 +00001060 if (event_data)
1061 {
1062 _vec_len (event_data) = 0;
1063 }
1064 }
1065
1066 return 0;
1067}
1068
1069/* *INDENT-OFF* */
1070VLIB_REGISTER_NODE (ip6_sv_reass_expire_node) = {
1071 .function = ip6_sv_reass_walk_expired,
1072 .format_trace = format_ip6_sv_reass_trace,
1073 .type = VLIB_NODE_TYPE_PROCESS,
1074 .name = "ip6-sv-reassembly-expire-walk",
1075
1076 .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
1077 .error_strings = ip6_sv_reassembly_error_strings,
1078
1079};
1080/* *INDENT-ON* */
1081
1082static u8 *
1083format_ip6_sv_reass_key (u8 * s, va_list * args)
1084{
1085 ip6_sv_reass_key_t *key = va_arg (*args, ip6_sv_reass_key_t *);
1086 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1087 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1088 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1089 return s;
1090}
1091
1092static u8 *
1093format_ip6_sv_reass (u8 * s, va_list * args)
1094{
1095 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1096 ip6_sv_reass_t *reass = va_arg (*args, ip6_sv_reass_t *);
1097
1098 s = format (s, "ID: %lu, key: %U, trace_op_counter: %u\n",
1099 reass->id, format_ip6_sv_reass_key, &reass->key,
1100 reass->trace_op_counter);
1101 vlib_buffer_t *b;
1102 u32 *bip;
1103 u32 counter = 0;
1104 vec_foreach (bip, reass->cached_buffers)
1105 {
1106 u32 bi = *bip;
1107 do
1108 {
1109 b = vlib_get_buffer (vm, bi);
1110 s = format (s, " #%03u: bi: %u\n", counter, bi);
1111 ++counter;
1112 bi = b->next_buffer;
1113 }
1114 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
1115 }
1116 return s;
1117}
1118
1119static clib_error_t *
1120show_ip6_sv_reass (vlib_main_t * vm, unformat_input_t * input,
1121 CLIB_UNUSED (vlib_cli_command_t * lmd))
1122{
1123 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1124
1125 vlib_cli_output (vm, "---------------------");
1126 vlib_cli_output (vm, "IP6 reassembly status");
1127 vlib_cli_output (vm, "---------------------");
1128 bool details = false;
1129 if (unformat (input, "details"))
1130 {
1131 details = true;
1132 }
1133
1134 u32 sum_reass_n = 0;
1135 u64 sum_buffers_n = 0;
1136 ip6_sv_reass_t *reass;
1137 uword thread_index;
1138 const uword nthreads = vlib_num_workers () + 1;
1139 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1140 {
1141 ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1142 clib_spinlock_lock (&rt->lock);
1143 if (details)
1144 {
1145 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001146 pool_foreach (reass, rt->pool) {
Klement Sekerade34c352019-06-25 11:19:22 +00001147 vlib_cli_output (vm, "%U", format_ip6_sv_reass, vm, reass);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001148 }
Klement Sekerade34c352019-06-25 11:19:22 +00001149 /* *INDENT-ON* */
1150 }
1151 sum_reass_n += rt->reass_n;
1152 clib_spinlock_unlock (&rt->lock);
1153 }
1154 vlib_cli_output (vm, "---------------------");
1155 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1156 (long unsigned) sum_reass_n);
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001157 vlib_cli_output (vm,
1158 "Maximum configured concurrent shallow virtual IP6 reassemblies per worker-thread: %lu\n",
1159 (long unsigned) rm->max_reass_n);
1160 vlib_cli_output (vm,
Anton Nikolaev74a4a702021-02-17 14:45:40 +05001161 "Maximum configured amount of fragments per shallow "
1162 "virtual IP6 reassembly: %lu\n",
1163 (long unsigned) rm->max_reass_len);
1164 vlib_cli_output (vm,
Vladimir Ratnikova877cf92019-12-21 06:27:52 -05001165 "Maximum configured shallow virtual IP6 reassembly timeout: %lums\n",
1166 (long unsigned) rm->timeout_ms);
1167 vlib_cli_output (vm,
1168 "Maximum configured shallow virtual IP6 reassembly expire walk interval: %lums\n",
1169 (long unsigned) rm->expire_walk_interval_ms);
Klement Sekerade34c352019-06-25 11:19:22 +00001170 vlib_cli_output (vm, "Buffers in use: %lu\n",
1171 (long unsigned) sum_buffers_n);
1172 return 0;
1173}
1174
1175/* *INDENT-OFF* */
1176VLIB_CLI_COMMAND (show_ip6_sv_reassembly_cmd, static) = {
1177 .path = "show ip6-sv-reassembly",
1178 .short_help = "show ip6-sv-reassembly [details]",
1179 .function = show_ip6_sv_reass,
1180};
1181/* *INDENT-ON* */
1182
1183#ifndef CLIB_MARCH_VARIANT
1184vnet_api_error_t
1185ip6_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1186{
Klement Sekeraf126e742019-10-10 09:46:06 +00001187 return ip6_sv_reass_enable_disable_with_refcnt (sw_if_index,
1188 enable_disable);
Klement Sekerade34c352019-06-25 11:19:22 +00001189}
1190#endif /* CLIB_MARCH_VARIANT */
1191
1192#define foreach_ip6_sv_reassembly_handoff_error \
1193_(CONGESTION_DROP, "congestion drop")
1194
1195
1196typedef enum
1197{
1198#define _(sym,str) IP6_SV_REASSEMBLY_HANDOFF_ERROR_##sym,
1199 foreach_ip6_sv_reassembly_handoff_error
1200#undef _
1201 IP6_SV_REASSEMBLY_HANDOFF_N_ERROR,
1202} ip6_sv_reassembly_handoff_error_t;
1203
1204static char *ip6_sv_reassembly_handoff_error_strings[] = {
1205#define _(sym,string) string,
1206 foreach_ip6_sv_reassembly_handoff_error
1207#undef _
1208};
1209
1210typedef struct
1211{
1212 u32 next_worker_index;
1213} ip6_sv_reassembly_handoff_trace_t;
1214
1215static u8 *
1216format_ip6_sv_reassembly_handoff_trace (u8 * s, va_list * args)
1217{
1218 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1219 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1220 ip6_sv_reassembly_handoff_trace_t *t =
1221 va_arg (*args, ip6_sv_reassembly_handoff_trace_t *);
1222
1223 s =
1224 format (s, "ip6-sv-reassembly-handoff: next-worker %d",
1225 t->next_worker_index);
1226
1227 return s;
1228}
1229
1230always_inline uword
1231ip6_sv_reassembly_handoff_inline (vlib_main_t * vm,
1232 vlib_node_runtime_t * node,
1233 vlib_frame_t * frame, bool is_feature)
1234{
1235 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1236
1237 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1238 u32 n_enq, n_left_from, *from;
1239 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1240 u32 fq_index;
1241
1242 from = vlib_frame_vector_args (frame);
1243 n_left_from = frame->n_vectors;
1244 vlib_get_buffers (vm, from, bufs, n_left_from);
1245
1246 b = bufs;
1247 ti = thread_indices;
1248
1249 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1250
1251 while (n_left_from > 0)
1252 {
1253 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1254
1255 if (PREDICT_FALSE
1256 ((node->flags & VLIB_NODE_FLAG_TRACE)
1257 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1258 {
1259 ip6_sv_reassembly_handoff_trace_t *t =
1260 vlib_add_trace (vm, node, b[0], sizeof (*t));
1261 t->next_worker_index = ti[0];
1262 }
1263
1264 n_left_from -= 1;
1265 ti += 1;
1266 b += 1;
1267 }
Damjan Marion9e7a0b42021-05-14 14:50:01 +02001268 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
1269 thread_indices, frame->n_vectors, 1);
Klement Sekerade34c352019-06-25 11:19:22 +00001270
1271 if (n_enq < frame->n_vectors)
1272 vlib_node_increment_counter (vm, node->node_index,
1273 IP6_SV_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1274 frame->n_vectors - n_enq);
1275 return frame->n_vectors;
1276}
1277
1278VLIB_NODE_FN (ip6_sv_reassembly_handoff_node) (vlib_main_t * vm,
1279 vlib_node_runtime_t * node,
1280 vlib_frame_t * frame)
1281{
1282 return ip6_sv_reassembly_handoff_inline (vm, node, frame,
1283 false /* is_feature */ );
1284}
1285
1286/* *INDENT-OFF* */
1287VLIB_REGISTER_NODE (ip6_sv_reassembly_handoff_node) = {
1288 .name = "ip6-sv-reassembly-handoff",
1289 .vector_size = sizeof (u32),
1290 .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1291 .error_strings = ip6_sv_reassembly_handoff_error_strings,
1292 .format_trace = format_ip6_sv_reassembly_handoff_trace,
1293
1294 .n_next_nodes = 1,
1295
1296 .next_nodes = {
1297 [0] = "error-drop",
1298 },
1299};
1300
1301
1302VLIB_NODE_FN (ip6_sv_reassembly_feature_handoff_node) (vlib_main_t * vm,
1303 vlib_node_runtime_t * node, vlib_frame_t * frame)
1304{
1305 return ip6_sv_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1306}
1307
1308
1309/* *INDENT-OFF* */
1310VLIB_REGISTER_NODE (ip6_sv_reassembly_feature_handoff_node) = {
1311 .name = "ip6-sv-reass-feature-hoff",
1312 .vector_size = sizeof (u32),
1313 .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1314 .error_strings = ip6_sv_reassembly_handoff_error_strings,
1315 .format_trace = format_ip6_sv_reassembly_handoff_trace,
1316
1317 .n_next_nodes = 1,
1318
1319 .next_nodes = {
1320 [0] = "error-drop",
1321 },
1322};
1323/* *INDENT-ON* */
1324
1325#ifndef CLIB_MARCH_VARIANT
1326int
1327ip6_sv_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1328{
1329 ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
Klement Sekera63c73532019-09-30 14:35:36 +00001330 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
Klement Sekerade34c352019-06-25 11:19:22 +00001331 if (is_enable)
1332 {
Klement Sekera63c73532019-09-30 14:35:36 +00001333 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001334 {
Klement Sekera63c73532019-09-30 14:35:36 +00001335 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001336 return vnet_feature_enable_disable ("ip6-unicast",
1337 "ip6-sv-reassembly-feature",
1338 sw_if_index, 1, 0, 0);
1339 }
Klement Sekera63c73532019-09-30 14:35:36 +00001340 ++rm->feature_use_refcount_per_intf[sw_if_index];
Klement Sekerade34c352019-06-25 11:19:22 +00001341 }
1342 else
1343 {
Klement Sekera63c73532019-09-30 14:35:36 +00001344 --rm->feature_use_refcount_per_intf[sw_if_index];
1345 if (!rm->feature_use_refcount_per_intf[sw_if_index])
Klement Sekerade34c352019-06-25 11:19:22 +00001346 return vnet_feature_enable_disable ("ip6-unicast",
1347 "ip6-sv-reassembly-feature",
1348 sw_if_index, 0, 0, 0);
1349 }
Klement Sekera407f5932019-12-11 13:06:27 +00001350 return 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001351}
1352#endif
1353
1354/*
1355 * fd.io coding-style-patch-verification: ON
1356 *
1357 * Local Variables:
1358 * eval: (c-set-style "gnu")
1359 * End:
1360 */