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