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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @brief IPv6 Full Reassembly.
20 * This file contains the source code for IPv6 full reassembly.
23 #include <vppinfra/vec.h>
24 #include <vnet/vnet.h>
25 #include <vnet/ip/ip.h>
26 #include <vppinfra/bihash_48_8.h>
27 #include <vnet/ip/reass/ip6_full_reass.h>
28 #include <vnet/ip/ip6_inlines.h>
30 #define MSEC_PER_SEC 1000
31 #define IP6_FULL_REASS_TIMEOUT_DEFAULT_MS 200
32 /* As there are only 1024 reass context per thread, either the DDOS attacks
33 * or fractions of real timeouts, would consume these contexts quickly and
34 * running out context space and unable to perform reassembly */
35 #define IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 50 // 50 ms default
36 #define IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT 1024
37 #define IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
38 #define IP6_FULL_REASS_HT_LOAD_FACTOR (0.75)
43 IP6_FULL_REASS_RC_INTERNAL_ERROR,
44 IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS,
45 IP6_FULL_REASS_RC_NO_BUF,
46 IP6_FULL_REASS_RC_HANDOFF,
47 IP6_FULL_REASS_RC_INVALID_FRAG_LEN,
48 IP6_FULL_REASS_RC_OVERLAP,
49 } ip6_full_reass_rc_t;
66 } ip6_full_reass_key_t;
73 u32 memory_owner_thread_index;
76 } ip6_full_reass_val_t;
82 ip6_full_reass_key_t k;
83 ip6_full_reass_val_t v;
85 clib_bihash_kv_48_8_t kv;
86 } ip6_full_reass_kv_t;
90 ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
92 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
93 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
97 ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
99 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
100 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
101 (vnb->ip.reass.fragment_first +
102 ip6_full_reass_buffer_get_data_offset (b)) + 1;
108 ip6_full_reass_key_t key;
109 // time when last packet was received
111 // internal id of this reassembly
113 // buffer index of first buffer in this reassembly context
115 // last octet of packet, ~0 until fragment without more_fragments arrives
116 u32 last_packet_octet;
117 // length of data collected so far
119 // trace operation counter
120 u32 trace_op_counter;
121 // next index - used by custom apps (~0 if not set)
123 // error next index - used by custom apps (~0 if not set)
124 u32 error_next_index;
125 // minimum fragment length for this reassembly - used to estimate MTU
126 u16 min_fragment_length;
127 // number of fragments for this reassembly
129 // thread owning memory for this context (whose pool contains this ctx)
130 u32 memory_owner_thread_index;
131 // thread which received fragment with offset 0 and which sends out the
132 // completed reassembly
133 u32 sendout_thread_index;
138 ip6_full_reass_t *pool;
141 // for pacing the main thread timeouts
143 clib_spinlock_t lock;
144 } ip6_full_reass_per_thread_t;
151 u32 expire_walk_interval_ms;
152 // maximum number of fragments in one reassembly
154 // maximum number of reassemblies
158 clib_bihash_48_8_t hash;
161 ip6_full_reass_per_thread_t *per_thread_data;
164 vlib_main_t *vlib_main;
166 u32 ip6_icmp_error_idx;
167 u32 ip6_full_reass_expire_node_idx;
169 /** Worker handoff */
172 u32 fq_feature_index;
175 // reference count for enabling/disabling feature - per interface
176 u32 *feature_use_refcount_per_intf;
178 // whether local fragmented packets are reassembled or not
179 int is_local_reass_enabled;
180 } ip6_full_reass_main_t;
182 extern ip6_full_reass_main_t ip6_full_reass_main;
184 #ifndef CLIB_MARCH_VARIANT
185 ip6_full_reass_main_t ip6_full_reass_main;
186 #endif /* CLIB_MARCH_VARIANT */
190 IP6_FULL_REASSEMBLY_NEXT_INPUT,
191 IP6_FULL_REASSEMBLY_NEXT_DROP,
192 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
193 IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
194 IP6_FULL_REASSEMBLY_N_NEXT,
195 } ip6_full_reass_next_t;
202 } ip6_full_reass_node_type_t;
209 ICMP_ERROR_RT_EXCEEDED,
210 ICMP_ERROR_FL_TOO_BIG,
211 ICMP_ERROR_FL_NOT_MULT_8,
215 } ip6_full_reass_trace_operation_e;
225 } ip6_full_reass_range_trace_t;
229 ip6_full_reass_trace_operation_e action;
231 ip6_full_reass_range_trace_t trace_range;
238 bool is_after_handoff;
239 ip6_header_t ip6_header;
240 ip6_frag_hdr_t ip6_frag_header;
241 } ip6_full_reass_trace_t;
244 ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
245 ip6_full_reass_range_trace_t * trace)
247 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
248 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
249 trace->range_first = vnb->ip.reass.range_first;
250 trace->range_last = vnb->ip.reass.range_last;
251 trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
252 trace->data_len = ip6_full_reass_buffer_get_data_len (b);
253 trace->range_bi = bi;
257 format_ip6_full_reass_range_trace (u8 * s, va_list * args)
259 ip6_full_reass_range_trace_t *trace =
260 va_arg (*args, ip6_full_reass_range_trace_t *);
262 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
263 trace->range_last, trace->data_offset, trace->data_len,
269 format_ip6_full_reass_trace (u8 * s, va_list * args)
271 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
272 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
273 ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
275 if (~0 != t->reass_id)
277 if (t->is_after_handoff)
280 format (s, "%U\n", format_ip6_header, &t->ip6_header,
281 sizeof (t->ip6_header));
283 format (s, " %U\n", format_ip6_frag_hdr, &t->ip6_frag_header,
284 sizeof (t->ip6_frag_header));
288 format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
289 t->reass_id, t->op_id);
290 indent = format_get_indent (s);
291 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
292 t->trace_range.first_bi, t->total_data_len,
293 t->fragment_first, t->fragment_last);
298 s = format (s, "\n%Unew %U", format_white_space, indent,
299 format_ip6_full_reass_range_trace, &t->trace_range);
302 s = format (s, "\n%Udiscard %U", format_white_space, indent,
303 format_ip6_full_reass_range_trace, &t->trace_range);
306 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
307 format_ip6_full_reass_range_trace, &t->trace_range);
309 case ICMP_ERROR_FL_TOO_BIG:
310 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
311 format_white_space, indent,
312 format_ip6_full_reass_range_trace, &t->trace_range);
314 case ICMP_ERROR_FL_NOT_MULT_8:
315 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
316 format_white_space, indent,
317 format_ip6_full_reass_range_trace, &t->trace_range);
319 case ICMP_ERROR_RT_EXCEEDED:
320 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
321 format_white_space, indent);
324 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
328 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
332 s = format (s, "passthrough - not a fragment");
339 ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
340 ip6_full_reass_t * reass, u32 bi,
341 ip6_frag_hdr_t * ip6_frag_header,
342 ip6_full_reass_trace_operation_e action,
345 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
346 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
347 bool is_after_handoff = false;
348 if (pool_is_free_index
349 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
351 // this buffer's trace is gone
352 b->flags &= ~VLIB_BUFFER_IS_TRACED;
355 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
357 is_after_handoff = true;
359 ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
360 t->is_after_handoff = is_after_handoff;
361 if (t->is_after_handoff)
363 clib_memcpy (&t->ip6_header, vlib_buffer_get_current (b),
364 clib_min (sizeof (t->ip6_header), b->current_length));
367 clib_memcpy (&t->ip6_frag_header, ip6_frag_header,
368 sizeof (t->ip6_frag_header));
372 clib_memset (&t->ip6_frag_header, 0, sizeof (t->ip6_frag_header));
377 t->reass_id = reass->id;
378 t->op_id = reass->trace_op_counter;
379 t->trace_range.first_bi = reass->first_bi;
380 t->total_data_len = reass->data_len;
381 ++reass->trace_op_counter;
388 t->thread_id = vm->thread_index;
389 t->thread_id_to = thread_id_to;
390 ip6_full_reass_trace_details (vm, bi, &t->trace_range);
391 t->fragment_first = vnb->ip.reass.fragment_first;
392 t->fragment_last = vnb->ip.reass.fragment_last;
395 s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
396 printf ("%.*s\n", vec_len (s), s);
398 vec_reset_length (s);
403 ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
404 ip6_full_reass_t * reass)
406 pool_put (rt->pool, reass);
411 ip6_full_reass_free (ip6_full_reass_main_t * rm,
412 ip6_full_reass_per_thread_t * rt,
413 ip6_full_reass_t * reass)
415 clib_bihash_kv_48_8_t kv;
416 kv.key[0] = reass->key.as_u64[0];
417 kv.key[1] = reass->key.as_u64[1];
418 kv.key[2] = reass->key.as_u64[2];
419 kv.key[3] = reass->key.as_u64[3];
420 kv.key[4] = reass->key.as_u64[4];
421 kv.key[5] = reass->key.as_u64[5];
422 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
423 ip6_full_reass_free_ctx (rt, reass);
426 /* n_left_to_next, and to_next are taken as input params, as this function
427 * could be called from a graphnode, where its managing local copy of these
428 * variables, and ignoring those and still trying to enqueue the buffers
429 * with local variables would cause either buffer leak or corruption */
431 ip6_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
432 ip6_full_reass_t *reass, u32 *n_left_to_next,
435 u32 range_bi = reass->first_bi;
436 vlib_buffer_t *range_b;
437 vnet_buffer_opaque_t *range_vnb;
440 while (~0 != range_bi)
442 range_b = vlib_get_buffer (vm, range_bi);
443 range_vnb = vnet_buffer (range_b);
447 vec_add1 (to_free, range_bi);
449 range_bi = range_vnb->ip.reass.next_range_bi;
452 /* send to next_error_index */
453 if (~0 != reass->error_next_index &&
454 reass->error_next_index < node->n_next_nodes)
458 next_index = reass->error_next_index;
461 while (vec_len (to_free) > 0)
463 vlib_get_next_frame (vm, node, next_index, *to_next,
466 while (vec_len (to_free) > 0 && (*n_left_to_next) > 0)
468 bi = vec_pop (to_free);
472 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
473 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
475 ip6_full_reass_add_trace (vm, node, reass, bi, NULL,
480 (*n_left_to_next) -= 1;
483 vlib_put_next_frame (vm, node, next_index, (*n_left_to_next));
488 vlib_buffer_free (vm, to_free, vec_len (to_free));
494 sanitize_reass_buffers_add_missing (vlib_main_t *vm, ip6_full_reass_t *reass,
497 u32 range_bi = reass->first_bi;
498 vlib_buffer_t *range_b;
499 vnet_buffer_opaque_t *range_vnb;
501 while (~0 != range_bi)
503 range_b = vlib_get_buffer (vm, range_bi);
504 range_vnb = vnet_buffer (range_b);
510 if (range_b->flags & VLIB_BUFFER_NEXT_PRESENT)
513 vlib_buffer_t *_b = vlib_get_buffer (vm, _bi);
514 while (_b->flags & VLIB_BUFFER_NEXT_PRESENT)
516 if (_b->next_buffer != range_vnb->ip.reass.next_range_bi)
518 _bi = _b->next_buffer;
519 _b = vlib_get_buffer (vm, _bi);
523 _b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
528 range_bi = range_vnb->ip.reass.next_range_bi;
533 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
534 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
535 if (~0 != reass->first_bi)
537 fvnb->ip.reass.next_range_bi = reass->first_bi;
538 reass->first_bi = *bi0;
542 reass->first_bi = *bi0;
543 fvnb->ip.reass.next_range_bi = ~0;
550 ip6_full_reass_on_timeout (vlib_main_t *vm, vlib_node_runtime_t *node,
551 ip6_full_reass_t *reass, u32 *icmp_bi,
552 u32 *n_left_to_next, u32 **to_next)
554 if (~0 == reass->first_bi)
558 if (~0 == reass->next_index) // custom apps don't want icmp
560 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
561 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
563 *icmp_bi = reass->first_bi;
564 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
566 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
567 ICMP_ERROR_RT_EXCEEDED, ~0);
569 // fragment with offset zero received - send icmp message back
570 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
572 // separate first buffer from chain and steer it towards icmp node
573 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
574 reass->first_bi = b->next_buffer;
578 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
580 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
581 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
585 ip6_full_reass_drop_all (vm, node, reass, n_left_to_next, to_next);
588 always_inline ip6_full_reass_t *
589 ip6_full_reass_find_or_create (vlib_main_t *vm, vlib_node_runtime_t *node,
590 ip6_full_reass_main_t *rm,
591 ip6_full_reass_per_thread_t *rt,
592 ip6_full_reass_kv_t *kv, u32 *icmp_bi,
593 u8 *do_handoff, int skip_bihash,
594 u32 *n_left_to_next, u32 **to_next)
596 ip6_full_reass_t *reass;
602 now = vlib_time_now (vm);
604 if (!skip_bihash && !clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
606 if (vm->thread_index != kv->v.memory_owner_thread_index)
613 pool_elt_at_index (rm->per_thread_data
614 [kv->v.memory_owner_thread_index].pool,
617 if (now > reass->last_heard + rm->timeout)
619 ip6_full_reass_on_timeout (vm, node, reass, icmp_bi, n_left_to_next,
621 ip6_full_reass_free (rm, rt, reass);
628 reass->last_heard = now;
632 if (rt->reass_n >= rm->max_reass_n)
639 pool_get (rt->pool, reass);
640 clib_memset (reass, 0, sizeof (*reass));
641 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
643 reass->first_bi = ~0;
644 reass->last_packet_octet = ~0;
646 reass->next_index = ~0;
647 reass->error_next_index = ~0;
648 reass->memory_owner_thread_index = vm->thread_index;
652 kv->v.reass_index = (reass - rt->pool);
653 kv->v.memory_owner_thread_index = vm->thread_index;
654 reass->last_heard = now;
658 reass->key.as_u64[0] = kv->kv.key[0];
659 reass->key.as_u64[1] = kv->kv.key[1];
660 reass->key.as_u64[2] = kv->kv.key[2];
661 reass->key.as_u64[3] = kv->kv.key[3];
662 reass->key.as_u64[4] = kv->kv.key[4];
663 reass->key.as_u64[5] = kv->kv.key[5];
665 int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
668 ip6_full_reass_free (rm, rt, reass);
670 // if other worker created a context already work with the other copy
677 reass->key.as_u64[0] = ~0;
678 reass->key.as_u64[1] = ~0;
679 reass->key.as_u64[2] = ~0;
680 reass->key.as_u64[3] = ~0;
681 reass->key.as_u64[4] = ~0;
682 reass->key.as_u64[5] = ~0;
688 always_inline ip6_full_reass_rc_t
689 ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
690 ip6_full_reass_main_t * rm,
691 ip6_full_reass_per_thread_t * rt,
692 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
693 u32 * error0, bool is_custom_app)
695 *bi0 = reass->first_bi;
696 *error0 = IP6_ERROR_NONE;
697 ip6_frag_hdr_t *frag_hdr;
698 vlib_buffer_t *last_b = NULL;
699 u32 sub_chain_bi = reass->first_bi;
700 u32 total_length = 0;
703 u32 *vec_drop_compress = NULL;
704 ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
707 u32 tmp_bi = sub_chain_bi;
708 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
709 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
710 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
711 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
713 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
714 goto free_buffers_and_return;
717 u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
718 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
719 sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
721 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
722 if (tmp_bi == reass->first_bi)
724 /* first buffer - keep ip6 header */
725 if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
727 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
728 goto free_buffers_and_return;
731 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
732 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
734 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
736 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
737 goto free_buffers_and_return;
741 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
747 if (trim_front > tmp->current_length)
749 /* drop whole buffer */
750 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
752 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
753 goto free_buffers_and_return;
755 trim_front -= tmp->current_length;
756 vec_add1 (vec_drop_compress, tmp_bi);
757 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
758 tmp_bi = tmp->next_buffer;
759 tmp = vlib_get_buffer (vm, tmp_bi);
764 vlib_buffer_advance (tmp, trim_front);
772 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
773 last_b->next_buffer = tmp_bi;
776 if (keep_data <= tmp->current_length)
778 tmp->current_length = keep_data;
783 keep_data -= tmp->current_length;
784 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
786 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
787 goto free_buffers_and_return;
790 total_length += tmp->current_length;
794 if (reass->first_bi == tmp_bi)
796 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
797 goto free_buffers_and_return;
799 vec_add1 (vec_drop_compress, tmp_bi);
802 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
804 tmp_bi = tmp->next_buffer;
805 tmp = vlib_get_buffer (vm, tmp->next_buffer);
813 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
816 while (~0 != sub_chain_bi);
820 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
821 goto free_buffers_and_return;
823 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
824 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
825 if (total_length < first_b->current_length)
827 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
828 goto free_buffers_and_return;
830 total_length -= first_b->current_length;
831 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
832 first_b->total_length_not_including_first_buffer = total_length;
833 // drop fragment header
834 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
835 ip6_header_t *ip = vlib_buffer_get_current (first_b);
836 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
837 ip6_ext_hdr_chain_t hdr_chain;
838 ip6_ext_header_t *prev_hdr = 0;
839 int res = ip6_ext_header_walk (first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
842 (hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION))
844 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
845 goto free_buffers_and_return;
847 frag_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res].offset);
850 prev_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res - 1].offset);
851 prev_hdr->next_hdr = frag_hdr->next_hdr;
855 ip->protocol = frag_hdr->next_hdr;
857 if (hdr_chain.eh[res].offset != ip6_frag_hdr_offset)
859 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
860 goto free_buffers_and_return;
862 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
863 first_b->current_length - ip6_frag_hdr_offset -
864 sizeof (ip6_frag_hdr_t));
865 first_b->current_length -= sizeof (*frag_hdr);
867 clib_host_to_net_u16 (total_length + first_b->current_length -
869 if (!vlib_buffer_chain_linearize (vm, first_b))
871 rv = IP6_FULL_REASS_RC_NO_BUF;
872 goto free_buffers_and_return;
874 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
875 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
877 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
880 // following code does a hexdump of packet fragments to stdout ...
883 u32 bi = reass->first_bi;
887 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
888 s = format (s, "%u: %U\n", bi, format_hexdump,
889 vlib_buffer_get_current (b), b->current_length);
890 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
899 printf ("%.*s\n", vec_len (s), s);
908 *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
912 *next0 = reass->next_index;
914 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
915 ip6_full_reass_free (rm, rt, reass);
917 free_buffers_and_return:
918 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
919 vec_free (vec_drop_compress);
924 ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
925 ip6_full_reass_t * reass,
926 u32 prev_range_bi, u32 new_next_bi)
929 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
930 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
931 if (~0 != prev_range_bi)
933 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
934 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
935 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
936 prev_vnb->ip.reass.next_range_bi = new_next_bi;
940 if (~0 != reass->first_bi)
942 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
944 reass->first_bi = new_next_bi;
946 reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
949 always_inline ip6_full_reass_rc_t
950 ip6_full_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
951 ip6_full_reass_main_t *rm,
952 ip6_full_reass_per_thread_t *rt,
953 ip6_full_reass_t *reass, u32 *bi0, u32 *next0,
954 u32 *error0, ip6_frag_hdr_t *frag_hdr,
955 bool is_custom_app, u32 *handoff_thread_idx,
959 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
960 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
963 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
964 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
967 fvnb->ip.reass.ip6_frag_hdr_offset =
968 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
969 ip6_header_t *fip = vlib_buffer_get_current (fb);
970 if (fb->current_length < sizeof (*fip) ||
971 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
972 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
974 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
977 u32 fragment_first = fvnb->ip.reass.fragment_first =
978 ip6_frag_hdr_offset_bytes (frag_hdr);
979 u32 fragment_length =
980 vlib_buffer_length_in_chain (vm, fb) -
981 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
982 if (0 == fragment_length)
984 return IP6_FULL_REASS_RC_INVALID_FRAG_LEN;
986 u32 fragment_last = fvnb->ip.reass.fragment_last =
987 fragment_first + fragment_length - 1;
988 int more_fragments = ip6_frag_hdr_more (frag_hdr);
989 u32 candidate_range_bi = reass->first_bi;
990 u32 prev_range_bi = ~0;
991 fvnb->ip.reass.range_first = fragment_first;
992 fvnb->ip.reass.range_last = fragment_last;
993 fvnb->ip.reass.next_range_bi = ~0;
996 reass->last_packet_octet = fragment_last;
998 if (~0 == reass->first_bi)
1000 // starting a new reassembly
1001 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
1002 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
1004 reass->fragments_n = 1;
1005 goto check_if_done_maybe;
1007 reass->min_fragment_length =
1008 clib_min (clib_net_to_host_u16 (fip->payload_length),
1009 fvnb->ip.reass.estimated_mtu);
1010 while (~0 != candidate_range_bi)
1012 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
1013 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
1014 if (fragment_first > candidate_vnb->ip.reass.range_last)
1016 // this fragments starts after candidate range
1017 prev_range_bi = candidate_range_bi;
1018 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
1019 if (candidate_vnb->ip.reass.range_last < fragment_last &&
1020 ~0 == candidate_range_bi)
1022 // special case - this fragment falls beyond all known ranges
1023 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
1030 if (fragment_last < candidate_vnb->ip.reass.range_first)
1032 // this fragment ends before candidate range without any overlap
1033 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
1037 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
1038 fragment_last == candidate_vnb->ip.reass.range_last)
1040 // duplicate fragment - ignore
1044 // overlapping fragment - not allowed by RFC 8200
1045 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1047 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr,
1050 return IP6_FULL_REASS_RC_OVERLAP;
1054 ++reass->fragments_n;
1055 check_if_done_maybe:
1058 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1060 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr, RANGE_NEW,
1064 else if (skip_bihash)
1066 // if this reassembly is not in bihash, then the packet must have been
1068 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1070 if (~0 != reass->last_packet_octet &&
1071 reass->data_len == reass->last_packet_octet + 1)
1073 *handoff_thread_idx = reass->sendout_thread_index;
1075 reass->memory_owner_thread_index != reass->sendout_thread_index;
1076 ip6_full_reass_rc_t rc =
1077 ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
1079 if (IP6_FULL_REASS_RC_OK == rc && handoff)
1081 return IP6_FULL_REASS_RC_HANDOFF;
1089 // if this reassembly is not in bihash, it should've been an atomic
1090 // fragment and thus finalized
1091 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1096 if (reass->fragments_n > rm->max_reass_len)
1098 return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
1103 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1104 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
1107 return IP6_FULL_REASS_RC_OK;
1111 ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
1113 ip6_ext_hdr_chain_t *hc)
1115 int nh = hc->eh[hc->length - 1].protocol;
1116 /* Checking to see if it's a terminating header */
1117 if (ip6_ext_hdr (nh))
1119 icmp6_error_set_vnet_buffer (
1120 b, ICMP6_parameter_problem,
1121 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
1122 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
1129 ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
1131 ip6_frag_hdr_t * frag_hdr)
1133 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1134 ip6_header_t *ip = vlib_buffer_get_current (b);
1135 int more_fragments = ip6_frag_hdr_more (frag_hdr);
1136 u32 fragment_length =
1137 vlib_buffer_length_in_chain (vm, b) -
1138 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1139 if (more_fragments && 0 != fragment_length % 8)
1141 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1142 ICMP6_parameter_problem_erroneous_header_field,
1143 (u8 *) & ip->payload_length - (u8 *) ip);
1150 ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
1152 ip6_frag_hdr_t * frag_hdr)
1154 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1155 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
1156 u32 fragment_length =
1157 vlib_buffer_length_in_chain (vm, b) -
1158 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1159 if (fragment_first + fragment_length > 65535)
1161 ip6_header_t *ip0 = vlib_buffer_get_current (b);
1162 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1163 ICMP6_parameter_problem_erroneous_header_field,
1164 (u8 *) & frag_hdr->fragment_offset_and_more
1172 ip6_full_reassembly_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1173 vlib_frame_t *frame, bool is_feature,
1174 bool is_custom_app, bool is_local)
1176 u32 *from = vlib_frame_vector_args (frame);
1177 u32 n_left_from, n_left_to_next, *to_next, next_index;
1178 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1179 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
1180 clib_spinlock_lock (&rt->lock);
1182 n_left_from = frame->n_vectors;
1183 next_index = node->cached_next_index;
1184 while (n_left_from > 0)
1186 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1188 while (n_left_from > 0 && n_left_to_next > 0)
1192 u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1193 u32 error0 = IP6_ERROR_NONE;
1197 b0 = vlib_get_buffer (vm, bi0);
1199 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
1200 ip6_frag_hdr_t *frag_hdr;
1201 ip6_ext_hdr_chain_t hdr_chain;
1202 int res = ip6_ext_header_walk (
1203 b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
1205 hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION)
1207 // this is a mangled packet - no fragmentation
1208 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1209 ip6_full_reass_add_trace (vm, node, NULL, bi0, NULL, PASSTHROUGH,
1213 if (is_local && !rm->is_local_reass_enabled)
1215 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1219 ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
1220 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1221 hdr_chain.eh[res].offset;
1223 if (0 == ip6_frag_hdr_offset (frag_hdr))
1225 // first fragment - verify upper-layer is present
1226 if (!ip6_full_reass_verify_upper_layer_present (node, b0,
1229 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1233 if (!ip6_full_reass_verify_fragment_multiple_8 (vm, b0, frag_hdr) ||
1234 !ip6_full_reass_verify_packet_size_lt_64k (vm, b0, frag_hdr))
1236 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1240 int skip_bihash = 0;
1241 ip6_full_reass_kv_t kv;
1244 if (0 == ip6_frag_hdr_offset (frag_hdr) &&
1245 !ip6_frag_hdr_more (frag_hdr))
1247 // this is atomic fragment and needs to be processed separately
1252 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1253 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1254 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1255 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1257 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1258 vnet_buffer (b0)->sw_if_index[VLIB_RX]))
1260 (u64) frag_hdr->identification;
1261 kv.k.as_u64[5] = ip0->protocol;
1264 ip6_full_reass_t *reass = ip6_full_reass_find_or_create (
1265 vm, node, rm, rt, &kv, &icmp_bi, &do_handoff, skip_bihash,
1266 &n_left_to_next, &to_next);
1270 const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1271 if (0 == fragment_first)
1273 reass->sendout_thread_index = vm->thread_index;
1276 if (PREDICT_FALSE (do_handoff))
1278 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1279 vnet_buffer (b0)->ip.reass.owner_thread_index =
1280 kv.v.memory_owner_thread_index;
1284 u32 handoff_thread_idx;
1286 switch (ip6_full_reass_update (
1287 vm, node, rm, rt, reass, &bi0, &next0, &error0, frag_hdr,
1288 is_custom_app, &handoff_thread_idx, skip_bihash))
1290 case IP6_FULL_REASS_RC_OK:
1291 /* nothing to do here */
1293 case IP6_FULL_REASS_RC_HANDOFF:
1294 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1295 b0 = vlib_get_buffer (vm, bi0);
1296 vnet_buffer (b0)->ip.reass.owner_thread_index =
1299 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
1300 counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
1302 case IP6_FULL_REASS_RC_NO_BUF:
1303 counter = IP6_ERROR_REASS_NO_BUF;
1305 case IP6_FULL_REASS_RC_INVALID_FRAG_LEN:
1306 counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
1308 case IP6_FULL_REASS_RC_OVERLAP:
1309 counter = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
1311 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
1312 counter = IP6_ERROR_REASS_INTERNAL_ERROR;
1313 /* Sanitization is needed in internal error cases only, as
1314 * the incoming packet is already dropped in other cases,
1315 * also adding bi0 back to the reassembly list, fixes the
1316 * leaking of buffers during internal errors.
1318 * Also it doesnt make sense to send these buffers custom
1319 * app, these fragments are with internal errors */
1320 sanitize_reass_buffers_add_missing (vm, reass, &bi0);
1321 reass->error_next_index = ~0;
1326 vlib_node_increment_counter (vm, node->node_index, counter,
1328 ip6_full_reass_drop_all (vm, node, reass, &n_left_to_next,
1330 ip6_full_reass_free (rm, rt, reass);
1339 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1343 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1344 next0 = fvnb->ip.reass.error_next_index;
1346 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1354 n_left_to_next -= 1;
1356 /* bi0 might have been updated by reass_finalize, reload */
1357 b0 = vlib_get_buffer (vm, bi0);
1358 if (IP6_ERROR_NONE != error0)
1360 b0->error = node->errors[error0];
1363 if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
1365 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1367 ip6_full_reass_add_trace (
1368 vm, node, NULL, bi0, frag_hdr, HANDOFF,
1369 vnet_buffer (b0)->ip.reass.owner_thread_index);
1372 else if (is_feature && IP6_ERROR_NONE == error0)
1374 vnet_feature_next (&next0, b0);
1376 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1377 n_left_to_next, bi0, next0);
1382 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1383 to_next[0] = icmp_bi;
1385 n_left_to_next -= 1;
1386 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1387 n_left_to_next, icmp_bi,
1395 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1398 clib_spinlock_unlock (&rt->lock);
1399 return frame->n_vectors;
1402 static char *ip6_full_reassembly_error_strings[] = {
1403 #define _(sym, string) string,
1408 VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1409 vlib_node_runtime_t * node,
1410 vlib_frame_t * frame)
1412 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1413 false /* is_custom_app */,
1414 false /* is_local */);
1417 VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1418 .name = "ip6-full-reassembly",
1419 .vector_size = sizeof (u32),
1420 .format_trace = format_ip6_full_reass_trace,
1421 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1422 .error_strings = ip6_full_reassembly_error_strings,
1423 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1426 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1427 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1428 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1429 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
1433 VLIB_NODE_FN (ip6_local_full_reass_node)
1434 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1436 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1437 false /* is_custom_app */,
1438 true /* is_local */);
1441 VLIB_REGISTER_NODE (ip6_local_full_reass_node) = {
1442 .name = "ip6-local-full-reassembly",
1443 .vector_size = sizeof (u32),
1444 .format_trace = format_ip6_full_reass_trace,
1445 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1446 .error_strings = ip6_full_reassembly_error_strings,
1447 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1450 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1451 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1452 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1453 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-local-full-reassembly-handoff",
1457 VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1458 vlib_node_runtime_t * node,
1459 vlib_frame_t * frame)
1461 return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */,
1462 false /* is_custom_app */,
1463 false /* is_local */);
1466 VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1467 .name = "ip6-full-reassembly-feature",
1468 .vector_size = sizeof (u32),
1469 .format_trace = format_ip6_full_reass_trace,
1470 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1471 .error_strings = ip6_full_reassembly_error_strings,
1472 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1475 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1476 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1477 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1478 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
1482 VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
1483 .arc_name = "ip6-unicast",
1484 .node_name = "ip6-full-reassembly-feature",
1485 .runs_before = VNET_FEATURES ("ip6-lookup",
1486 "ipsec6-input-feature"),
1490 VLIB_NODE_FN (ip6_full_reass_node_custom)
1491 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1493 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1494 true /* is_custom_app */,
1495 false /* is_local */);
1498 VLIB_REGISTER_NODE (ip6_full_reass_node_custom) = {
1499 .name = "ip6-full-reassembly-custom",
1500 .vector_size = sizeof (u32),
1501 .format_trace = format_ip6_full_reass_trace,
1502 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1503 .error_strings = ip6_full_reassembly_error_strings,
1504 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1507 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1508 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1509 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1510 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-custom-hoff",
1514 #ifndef CLIB_MARCH_VARIANT
1516 ip6_full_reass_get_nbuckets ()
1518 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1522 /* need more mem with more workers */
1523 nbuckets = (u32) (rm->max_reass_n * (vlib_num_workers () + 1) /
1524 IP6_FULL_REASS_HT_LOAD_FACTOR);
1526 for (i = 0; i < 31; i++)
1527 if ((1 << i) >= nbuckets)
1533 #endif /* CLIB_MARCH_VARIANT */
1537 IP6_EVENT_CONFIG_CHANGED = 1,
1538 } ip6_full_reass_event_t;
1540 #ifndef CLIB_MARCH_VARIANT
1544 clib_bihash_48_8_t *new_hash;
1545 } ip6_rehash_cb_ctx;
1548 ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1550 ip6_rehash_cb_ctx *ctx = _ctx;
1551 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1555 return (BIHASH_WALK_CONTINUE);
1559 ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1560 u32 max_reassembly_length,
1561 u32 expire_walk_interval_ms)
1563 ip6_full_reass_main.timeout_ms = timeout_ms;
1564 ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1565 ip6_full_reass_main.max_reass_n = max_reassemblies;
1566 ip6_full_reass_main.max_reass_len = max_reassembly_length;
1567 ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1571 ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1572 u32 max_reassembly_length, u32 expire_walk_interval_ms)
1574 u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1575 ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1576 max_reassembly_length, expire_walk_interval_ms);
1577 vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1578 ip6_full_reass_main.ip6_full_reass_expire_node_idx,
1579 IP6_EVENT_CONFIG_CHANGED, 0);
1580 u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1581 if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
1583 clib_bihash_48_8_t new_hash;
1584 clib_memset (&new_hash, 0, sizeof (new_hash));
1585 ip6_rehash_cb_ctx ctx;
1587 ctx.new_hash = &new_hash;
1588 clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
1589 new_nbuckets * 1024);
1590 clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
1591 ip6_rehash_cb, &ctx);
1594 clib_bihash_free_48_8 (&new_hash);
1599 clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1600 clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1601 sizeof (ip6_full_reass_main.hash));
1602 clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
1609 ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1610 u32 * max_reassembly_length,
1611 u32 * expire_walk_interval_ms)
1613 *timeout_ms = ip6_full_reass_main.timeout_ms;
1614 *max_reassemblies = ip6_full_reass_main.max_reass_n;
1615 *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1616 *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
1620 static clib_error_t *
1621 ip6_full_reass_init_function (vlib_main_t * vm)
1623 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1624 clib_error_t *error = 0;
1630 vec_validate (rm->per_thread_data, vlib_num_workers ());
1631 ip6_full_reass_per_thread_t *rt;
1632 vec_foreach (rt, rm->per_thread_data)
1634 clib_spinlock_init (&rt->lock);
1635 pool_alloc (rt->pool, rm->max_reass_n);
1638 node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
1640 rm->ip6_full_reass_expire_node_idx = node->index;
1642 ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1643 IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1644 IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1645 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1647 nbuckets = ip6_full_reass_get_nbuckets ();
1648 clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1651 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1653 rm->ip6_icmp_error_idx = node->index;
1655 if ((error = vlib_call_init_function (vm, ip_main_init)))
1657 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1658 ip6_local_full_reass_node.index);
1659 rm->is_local_reass_enabled = 1;
1661 rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
1662 rm->fq_local_index =
1663 vlib_frame_queue_main_init (ip6_local_full_reass_node.index, 0);
1664 rm->fq_feature_index =
1665 vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
1666 rm->fq_custom_index =
1667 vlib_frame_queue_main_init (ip6_full_reass_node_custom.index, 0);
1669 rm->feature_use_refcount_per_intf = NULL;
1673 VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
1674 #endif /* CLIB_MARCH_VARIANT */
1677 ip6_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
1678 CLIB_UNUSED (vlib_frame_t *f))
1680 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1681 uword event_type, *event_data = 0;
1685 vlib_process_wait_for_event_or_clock (vm,
1686 (f64) rm->expire_walk_interval_ms
1687 / (f64) MSEC_PER_SEC);
1688 event_type = vlib_process_get_events (vm, &event_data);
1693 /* no events => timeout */
1695 case IP6_EVENT_CONFIG_CHANGED:
1696 /* nothing to do here */
1699 clib_warning ("BUG: event type 0x%wx", event_type);
1702 f64 now = vlib_time_now (vm);
1704 ip6_full_reass_t *reass;
1705 int *pool_indexes_to_free = NULL;
1707 uword thread_index = 0;
1709 const uword nthreads = vlib_num_workers () + 1;
1710 u32 *vec_icmp_bi = NULL;
1711 u32 n_left_to_next, *to_next;
1713 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1715 ip6_full_reass_per_thread_t *rt =
1716 &rm->per_thread_data[thread_index];
1717 clib_spinlock_lock (&rt->lock);
1719 vec_reset_length (pool_indexes_to_free);
1720 /* Pace the number of timeouts handled per thread,to avoid barrier
1721 * sync issues in real world scenarios */
1723 u32 beg = rt->last_id;
1724 /* to ensure we walk at least once per sec per context */
1725 u32 end = beg + (IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT *
1726 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS /
1729 if (end > vec_len (rt->pool))
1731 end = vec_len (rt->pool);
1739 pool_foreach_stepping_index (index, beg, end, rt->pool)
1741 reass = pool_elt_at_index (rt->pool, index);
1742 if (now > reass->last_heard + rm->timeout)
1744 vec_add1 (pool_indexes_to_free, index);
1749 vec_foreach (i, pool_indexes_to_free)
1751 ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1753 ip6_full_reass_on_timeout (vm, node, reass, &icmp_bi,
1754 &n_left_to_next, &to_next);
1756 vec_add1 (vec_icmp_bi, icmp_bi);
1758 ip6_full_reass_free (rm, rt, reass);
1761 clib_spinlock_unlock (&rt->lock);
1764 while (vec_len (vec_icmp_bi) > 0)
1767 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1768 u32 *to_next = vlib_frame_vector_args (f);
1769 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
1770 int trace_frame = 0;
1771 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1773 u32 bi = vec_pop (vec_icmp_bi);
1774 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1775 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1777 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1781 n_left_to_next -= 1;
1783 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
1784 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1787 vec_free (pool_indexes_to_free);
1788 vec_free (vec_icmp_bi);
1791 vec_set_len (event_data, 0);
1798 VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
1799 .function = ip6_full_reass_walk_expired,
1800 .format_trace = format_ip6_full_reass_trace,
1801 .type = VLIB_NODE_TYPE_PROCESS,
1802 .name = "ip6-full-reassembly-expire-walk",
1804 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1805 .error_strings = ip6_full_reassembly_error_strings,
1810 format_ip6_full_reass_key (u8 * s, va_list * args)
1812 ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
1813 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1814 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1815 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1820 format_ip6_full_reass (u8 * s, va_list * args)
1822 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1823 ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
1825 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
1826 "last_packet_octet: %u, trace_op_counter: %u\n",
1827 reass->id, format_ip6_full_reass_key, &reass->key,
1828 reass->first_bi, reass->data_len, reass->last_packet_octet,
1829 reass->trace_op_counter);
1830 u32 bi = reass->first_bi;
1834 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1835 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1836 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1837 "fragment[%u, %u]\n",
1838 counter, vnb->ip.reass.range_first,
1839 vnb->ip.reass.range_last, bi,
1840 ip6_full_reass_buffer_get_data_offset (b),
1841 ip6_full_reass_buffer_get_data_len (b),
1842 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1843 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1845 bi = b->next_buffer;
1855 static clib_error_t *
1856 show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1857 CLIB_UNUSED (vlib_cli_command_t * lmd))
1859 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1861 vlib_cli_output (vm, "---------------------");
1862 vlib_cli_output (vm, "IP6 reassembly status");
1863 vlib_cli_output (vm, "---------------------");
1864 bool details = false;
1865 if (unformat (input, "details"))
1870 u32 sum_reass_n = 0;
1871 u64 sum_buffers_n = 0;
1872 ip6_full_reass_t *reass;
1874 const uword nthreads = vlib_num_workers () + 1;
1875 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1877 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1878 clib_spinlock_lock (&rt->lock);
1881 pool_foreach (reass, rt->pool) {
1882 vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
1885 sum_reass_n += rt->reass_n;
1886 clib_spinlock_unlock (&rt->lock);
1888 vlib_cli_output (vm, "---------------------");
1889 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1890 (long unsigned) sum_reass_n);
1891 vlib_cli_output (vm,
1892 "Maximum configured concurrent full IP6 reassemblies per worker-thread: %lu\n",
1893 (long unsigned) rm->max_reass_n);
1894 vlib_cli_output (vm,
1895 "Maximum configured amount of fragments "
1896 "per full IP6 reassembly: %lu\n",
1897 (long unsigned) rm->max_reass_len);
1898 vlib_cli_output (vm,
1899 "Maximum configured full IP6 reassembly timeout: %lums\n",
1900 (long unsigned) rm->timeout_ms);
1901 vlib_cli_output (vm,
1902 "Maximum configured full IP6 reassembly expire walk interval: %lums\n",
1903 (long unsigned) rm->expire_walk_interval_ms);
1904 vlib_cli_output (vm, "Buffers in use: %lu\n",
1905 (long unsigned) sum_buffers_n);
1909 VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1910 .path = "show ip6-full-reassembly",
1911 .short_help = "show ip6-full-reassembly [details]",
1912 .function = show_ip6_full_reass,
1915 #ifndef CLIB_MARCH_VARIANT
1917 ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1919 return vnet_feature_enable_disable ("ip6-unicast",
1920 "ip6-full-reassembly-feature",
1921 sw_if_index, enable_disable, 0, 0);
1923 #endif /* CLIB_MARCH_VARIANT */
1925 #define foreach_ip6_full_reassembly_handoff_error \
1926 _(CONGESTION_DROP, "congestion drop")
1931 #define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1932 foreach_ip6_full_reassembly_handoff_error
1934 IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1935 } ip6_full_reassembly_handoff_error_t;
1937 static char *ip6_full_reassembly_handoff_error_strings[] = {
1938 #define _(sym,string) string,
1939 foreach_ip6_full_reassembly_handoff_error
1945 u32 next_worker_index;
1946 } ip6_full_reassembly_handoff_trace_t;
1949 format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
1951 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1952 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1953 ip6_full_reassembly_handoff_trace_t *t =
1954 va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
1957 format (s, "ip6-full-reassembly-handoff: next-worker %d",
1958 t->next_worker_index);
1964 ip6_full_reassembly_handoff_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1965 vlib_frame_t *frame,
1966 ip6_full_reass_node_type_t type,
1969 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1971 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1972 u32 n_enq, n_left_from, *from;
1973 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1976 from = vlib_frame_vector_args (frame);
1977 n_left_from = frame->n_vectors;
1978 vlib_get_buffers (vm, from, bufs, n_left_from);
1981 ti = thread_indices;
1988 fq_index = rm->fq_local_index;
1992 fq_index = rm->fq_index;
1996 fq_index = rm->fq_feature_index;
1999 fq_index = rm->fq_custom_index;
2002 clib_warning ("Unexpected `type' (%d)!", type);
2005 while (n_left_from > 0)
2007 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
2010 ((node->flags & VLIB_NODE_FLAG_TRACE)
2011 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
2013 ip6_full_reassembly_handoff_trace_t *t =
2014 vlib_add_trace (vm, node, b[0], sizeof (*t));
2015 t->next_worker_index = ti[0];
2022 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
2023 thread_indices, frame->n_vectors, 1);
2025 if (n_enq < frame->n_vectors)
2026 vlib_node_increment_counter (vm, node->node_index,
2027 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
2028 frame->n_vectors - n_enq);
2029 return frame->n_vectors;
2032 VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
2033 vlib_node_runtime_t * node,
2034 vlib_frame_t * frame)
2036 return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
2037 false /* is_local */);
2040 VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
2041 .name = "ip6-full-reassembly-handoff",
2042 .vector_size = sizeof (u32),
2043 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2044 .error_strings = ip6_full_reassembly_handoff_error_strings,
2045 .format_trace = format_ip6_full_reassembly_handoff_trace,
2054 VLIB_NODE_FN (ip6_local_full_reassembly_handoff_node)
2055 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
2057 return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
2058 true /* is_feature */);
2061 VLIB_REGISTER_NODE (ip6_local_full_reassembly_handoff_node) = {
2062 .name = "ip6-local-full-reassembly-handoff",
2063 .vector_size = sizeof (u32),
2064 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2065 .error_strings = ip6_full_reassembly_handoff_error_strings,
2066 .format_trace = format_ip6_full_reassembly_handoff_trace,
2075 VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
2076 vlib_node_runtime_t * node, vlib_frame_t * frame)
2078 return ip6_full_reassembly_handoff_inline (vm, node, frame, FEATURE,
2079 false /* is_local */);
2082 VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
2083 .name = "ip6-full-reass-feature-hoff",
2084 .vector_size = sizeof (u32),
2085 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2086 .error_strings = ip6_full_reassembly_handoff_error_strings,
2087 .format_trace = format_ip6_full_reassembly_handoff_trace,
2096 VLIB_NODE_FN (ip6_full_reassembly_custom_handoff_node)
2097 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
2099 return ip6_full_reassembly_handoff_inline (vm, node, frame, CUSTOM,
2100 false /* is_local */);
2103 VLIB_REGISTER_NODE (ip6_full_reassembly_custom_handoff_node) = {
2104 .name = "ip6-full-reass-custom-hoff",
2105 .vector_size = sizeof (u32),
2106 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2107 .error_strings = ip6_full_reassembly_handoff_error_strings,
2108 .format_trace = format_ip6_full_reassembly_handoff_trace,
2117 #ifndef CLIB_MARCH_VARIANT
2119 ip6_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
2121 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
2122 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
2125 if (!rm->feature_use_refcount_per_intf[sw_if_index])
2127 ++rm->feature_use_refcount_per_intf[sw_if_index];
2128 return vnet_feature_enable_disable ("ip6-unicast",
2129 "ip6-full-reassembly-feature",
2130 sw_if_index, 1, 0, 0);
2132 ++rm->feature_use_refcount_per_intf[sw_if_index];
2136 --rm->feature_use_refcount_per_intf[sw_if_index];
2137 if (!rm->feature_use_refcount_per_intf[sw_if_index])
2138 return vnet_feature_enable_disable ("ip6-unicast",
2139 "ip6-full-reassembly-feature",
2140 sw_if_index, 0, 0, 0);
2146 ip6_local_full_reass_enable_disable (int enable)
2150 if (!ip6_full_reass_main.is_local_reass_enabled)
2152 ip6_full_reass_main.is_local_reass_enabled = 1;
2153 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
2154 ip6_local_full_reass_node.index);
2159 if (ip6_full_reass_main.is_local_reass_enabled)
2161 ip6_full_reass_main.is_local_reass_enabled = 0;
2162 ip6_unregister_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION);
2168 ip6_local_full_reass_enabled ()
2170 return ip6_full_reass_main.is_local_reass_enabled;
2176 * fd.io coding-style-patch-verification: ON
2179 * eval: (c-set-style "gnu")