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 100
32 #define IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
33 #define IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT 1024
34 #define IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
35 #define IP6_FULL_REASS_HT_LOAD_FACTOR (0.75)
40 IP6_FULL_REASS_RC_INTERNAL_ERROR,
41 IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS,
42 IP6_FULL_REASS_RC_NO_BUF,
43 IP6_FULL_REASS_RC_HANDOFF,
44 IP6_FULL_REASS_RC_INVALID_FRAG_LEN,
45 IP6_FULL_REASS_RC_OVERLAP,
46 } ip6_full_reass_rc_t;
63 } ip6_full_reass_key_t;
70 u32 memory_owner_thread_index;
73 } ip6_full_reass_val_t;
79 ip6_full_reass_key_t k;
80 ip6_full_reass_val_t v;
82 clib_bihash_kv_48_8_t kv;
83 } ip6_full_reass_kv_t;
87 ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
89 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
90 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
94 ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
96 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
97 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
98 (vnb->ip.reass.fragment_first +
99 ip6_full_reass_buffer_get_data_offset (b)) + 1;
105 ip6_full_reass_key_t key;
106 // time when last packet was received
108 // internal id of this reassembly
110 // buffer index of first buffer in this reassembly context
112 // last octet of packet, ~0 until fragment without more_fragments arrives
113 u32 last_packet_octet;
114 // length of data collected so far
116 // trace operation counter
117 u32 trace_op_counter;
118 // next index - used by custom apps (~0 if not set)
120 // error next index - used by custom apps (~0 if not set)
121 u32 error_next_index;
122 // minimum fragment length for this reassembly - used to estimate MTU
123 u16 min_fragment_length;
124 // number of fragments for this reassembly
126 // thread owning memory for this context (whose pool contains this ctx)
127 u32 memory_owner_thread_index;
128 // thread which received fragment with offset 0 and which sends out the
129 // completed reassembly
130 u32 sendout_thread_index;
135 ip6_full_reass_t *pool;
138 clib_spinlock_t lock;
139 } ip6_full_reass_per_thread_t;
146 u32 expire_walk_interval_ms;
147 // maximum number of fragments in one reassembly
149 // maximum number of reassemblies
153 clib_bihash_48_8_t hash;
156 ip6_full_reass_per_thread_t *per_thread_data;
159 vlib_main_t *vlib_main;
161 u32 ip6_icmp_error_idx;
162 u32 ip6_full_reass_expire_node_idx;
164 /** Worker handoff */
167 u32 fq_feature_index;
169 // reference count for enabling/disabling feature - per interface
170 u32 *feature_use_refcount_per_intf;
172 // whether local fragmented packets are reassembled or not
173 int is_local_reass_enabled;
174 } ip6_full_reass_main_t;
176 extern ip6_full_reass_main_t ip6_full_reass_main;
178 #ifndef CLIB_MARCH_VARIANT
179 ip6_full_reass_main_t ip6_full_reass_main;
180 #endif /* CLIB_MARCH_VARIANT */
184 IP6_FULL_REASSEMBLY_NEXT_INPUT,
185 IP6_FULL_REASSEMBLY_NEXT_DROP,
186 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
187 IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
188 IP6_FULL_REASSEMBLY_N_NEXT,
189 } ip6_full_reass_next_t;
195 ICMP_ERROR_RT_EXCEEDED,
196 ICMP_ERROR_FL_TOO_BIG,
197 ICMP_ERROR_FL_NOT_MULT_8,
201 } ip6_full_reass_trace_operation_e;
211 } ip6_full_reass_range_trace_t;
215 ip6_full_reass_trace_operation_e action;
217 ip6_full_reass_range_trace_t trace_range;
224 bool is_after_handoff;
225 ip6_header_t ip6_header;
226 ip6_frag_hdr_t ip6_frag_header;
227 } ip6_full_reass_trace_t;
230 ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
231 ip6_full_reass_range_trace_t * trace)
233 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
234 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
235 trace->range_first = vnb->ip.reass.range_first;
236 trace->range_last = vnb->ip.reass.range_last;
237 trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
238 trace->data_len = ip6_full_reass_buffer_get_data_len (b);
239 trace->range_bi = bi;
243 format_ip6_full_reass_range_trace (u8 * s, va_list * args)
245 ip6_full_reass_range_trace_t *trace =
246 va_arg (*args, ip6_full_reass_range_trace_t *);
248 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
249 trace->range_last, trace->data_offset, trace->data_len,
255 format_ip6_full_reass_trace (u8 * s, va_list * args)
257 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
258 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
259 ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
261 if (~0 != t->reass_id)
263 if (t->is_after_handoff)
266 format (s, "%U\n", format_ip6_header, &t->ip6_header,
267 sizeof (t->ip6_header));
269 format (s, " %U\n", format_ip6_frag_hdr, &t->ip6_frag_header,
270 sizeof (t->ip6_frag_header));
274 format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
275 t->reass_id, t->op_id);
276 indent = format_get_indent (s);
277 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
278 t->trace_range.first_bi, t->total_data_len,
279 t->fragment_first, t->fragment_last);
284 s = format (s, "\n%Unew %U", format_white_space, indent,
285 format_ip6_full_reass_range_trace, &t->trace_range);
288 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
289 format_ip6_full_reass_range_trace, &t->trace_range);
291 case ICMP_ERROR_FL_TOO_BIG:
292 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
293 format_white_space, indent,
294 format_ip6_full_reass_range_trace, &t->trace_range);
296 case ICMP_ERROR_FL_NOT_MULT_8:
297 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
298 format_white_space, indent,
299 format_ip6_full_reass_range_trace, &t->trace_range);
301 case ICMP_ERROR_RT_EXCEEDED:
302 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
303 format_white_space, indent);
306 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
310 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
314 s = format (s, "passthrough - not a fragment");
321 ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
322 ip6_full_reass_t * reass, u32 bi,
323 ip6_frag_hdr_t * ip6_frag_header,
324 ip6_full_reass_trace_operation_e action,
327 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
328 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
329 bool is_after_handoff = false;
330 if (pool_is_free_index
331 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
333 // this buffer's trace is gone
334 b->flags &= ~VLIB_BUFFER_IS_TRACED;
337 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
339 is_after_handoff = true;
341 ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
342 t->is_after_handoff = is_after_handoff;
343 if (t->is_after_handoff)
345 clib_memcpy (&t->ip6_header, vlib_buffer_get_current (b),
346 clib_min (sizeof (t->ip6_header), b->current_length));
349 clib_memcpy (&t->ip6_frag_header, ip6_frag_header,
350 sizeof (t->ip6_frag_header));
354 clib_memset (&t->ip6_frag_header, 0, sizeof (t->ip6_frag_header));
359 t->reass_id = reass->id;
360 t->op_id = reass->trace_op_counter;
361 t->trace_range.first_bi = reass->first_bi;
362 t->total_data_len = reass->data_len;
363 ++reass->trace_op_counter;
370 t->thread_id = vm->thread_index;
371 t->thread_id_to = thread_id_to;
372 ip6_full_reass_trace_details (vm, bi, &t->trace_range);
373 t->fragment_first = vnb->ip.reass.fragment_first;
374 t->fragment_last = vnb->ip.reass.fragment_last;
377 s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
378 printf ("%.*s\n", vec_len (s), s);
380 vec_reset_length (s);
385 ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
386 ip6_full_reass_t * reass)
388 pool_put (rt->pool, reass);
393 ip6_full_reass_free (ip6_full_reass_main_t * rm,
394 ip6_full_reass_per_thread_t * rt,
395 ip6_full_reass_t * reass)
397 clib_bihash_kv_48_8_t kv;
398 kv.key[0] = reass->key.as_u64[0];
399 kv.key[1] = reass->key.as_u64[1];
400 kv.key[2] = reass->key.as_u64[2];
401 kv.key[3] = reass->key.as_u64[3];
402 kv.key[4] = reass->key.as_u64[4];
403 kv.key[5] = reass->key.as_u64[5];
404 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
405 ip6_full_reass_free_ctx (rt, reass);
409 ip6_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
410 ip6_full_reass_t *reass, u32 offending_bi)
412 u32 range_bi = reass->first_bi;
413 vlib_buffer_t *range_b;
414 vnet_buffer_opaque_t *range_vnb;
416 while (~0 != range_bi)
418 range_b = vlib_get_buffer (vm, range_bi);
419 range_vnb = vnet_buffer (range_b);
423 vec_add1 (to_free, bi);
424 if (bi == offending_bi)
428 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
429 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
432 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
439 range_bi = range_vnb->ip.reass.next_range_bi;
441 if (~0 != offending_bi)
443 vec_add1 (to_free, offending_bi);
445 /* send to next_error_index */
446 if (~0 != reass->error_next_index)
448 u32 n_left_to_next, *to_next, next_index;
450 next_index = reass->error_next_index;
453 while (vec_len (to_free) > 0)
455 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
457 while (vec_len (to_free) > 0 && n_left_to_next > 0)
459 bi = vec_pop (to_free);
468 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
473 vlib_buffer_free (vm, to_free, vec_len (to_free));
479 ip6_full_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
480 ip6_full_reass_t * reass, u32 * icmp_bi)
482 if (~0 == reass->first_bi)
486 if (~0 == reass->next_index) // custom apps don't want icmp
488 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
489 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
491 *icmp_bi = reass->first_bi;
492 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
494 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
495 ICMP_ERROR_RT_EXCEEDED, ~0);
497 // fragment with offset zero received - send icmp message back
498 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
500 // separate first buffer from chain and steer it towards icmp node
501 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
502 reass->first_bi = b->next_buffer;
506 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
508 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
509 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
513 ip6_full_reass_drop_all (vm, node, reass, ~0);
516 always_inline ip6_full_reass_t *
517 ip6_full_reass_find_or_create (vlib_main_t *vm, vlib_node_runtime_t *node,
518 ip6_full_reass_main_t *rm,
519 ip6_full_reass_per_thread_t *rt,
520 ip6_full_reass_kv_t *kv, u32 *icmp_bi,
521 u8 *do_handoff, int skip_bihash)
523 ip6_full_reass_t *reass;
529 now = vlib_time_now (vm);
531 if (!skip_bihash && !clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
533 if (vm->thread_index != kv->v.memory_owner_thread_index)
540 pool_elt_at_index (rm->per_thread_data
541 [kv->v.memory_owner_thread_index].pool,
544 if (now > reass->last_heard + rm->timeout)
546 ip6_full_reass_on_timeout (vm, node, reass, icmp_bi);
547 ip6_full_reass_free (rm, rt, reass);
554 reass->last_heard = now;
558 if (rt->reass_n >= rm->max_reass_n)
565 pool_get (rt->pool, reass);
566 clib_memset (reass, 0, sizeof (*reass));
567 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
569 reass->first_bi = ~0;
570 reass->last_packet_octet = ~0;
572 reass->next_index = ~0;
573 reass->error_next_index = ~0;
574 reass->memory_owner_thread_index = vm->thread_index;
578 kv->v.reass_index = (reass - rt->pool);
579 kv->v.memory_owner_thread_index = vm->thread_index;
580 reass->last_heard = now;
584 reass->key.as_u64[0] = kv->kv.key[0];
585 reass->key.as_u64[1] = kv->kv.key[1];
586 reass->key.as_u64[2] = kv->kv.key[2];
587 reass->key.as_u64[3] = kv->kv.key[3];
588 reass->key.as_u64[4] = kv->kv.key[4];
589 reass->key.as_u64[5] = kv->kv.key[5];
591 int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
594 ip6_full_reass_free (rm, rt, reass);
596 // if other worker created a context already work with the other copy
603 reass->key.as_u64[0] = ~0;
604 reass->key.as_u64[1] = ~0;
605 reass->key.as_u64[2] = ~0;
606 reass->key.as_u64[3] = ~0;
607 reass->key.as_u64[4] = ~0;
608 reass->key.as_u64[5] = ~0;
614 always_inline ip6_full_reass_rc_t
615 ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
616 ip6_full_reass_main_t * rm,
617 ip6_full_reass_per_thread_t * rt,
618 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
619 u32 * error0, bool is_custom_app)
621 *bi0 = reass->first_bi;
622 *error0 = IP6_ERROR_NONE;
623 ip6_frag_hdr_t *frag_hdr;
624 vlib_buffer_t *last_b = NULL;
625 u32 sub_chain_bi = reass->first_bi;
626 u32 total_length = 0;
629 u32 *vec_drop_compress = NULL;
630 ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
633 u32 tmp_bi = sub_chain_bi;
634 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
635 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
636 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
637 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
639 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
640 goto free_buffers_and_return;
643 u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
644 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
645 sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
647 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
648 if (tmp_bi == reass->first_bi)
650 /* first buffer - keep ip6 header */
651 if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
653 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
654 goto free_buffers_and_return;
657 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
658 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
660 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
662 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
663 goto free_buffers_and_return;
667 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
673 if (trim_front > tmp->current_length)
675 /* drop whole buffer */
676 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
678 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
679 goto free_buffers_and_return;
681 trim_front -= tmp->current_length;
682 vec_add1 (vec_drop_compress, tmp_bi);
683 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
684 tmp_bi = tmp->next_buffer;
685 tmp = vlib_get_buffer (vm, tmp_bi);
690 vlib_buffer_advance (tmp, trim_front);
698 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
699 last_b->next_buffer = tmp_bi;
702 if (keep_data <= tmp->current_length)
704 tmp->current_length = keep_data;
709 keep_data -= tmp->current_length;
710 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
712 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
713 goto free_buffers_and_return;
716 total_length += tmp->current_length;
720 if (reass->first_bi == tmp_bi)
722 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
723 goto free_buffers_and_return;
725 vec_add1 (vec_drop_compress, tmp_bi);
728 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
730 tmp_bi = tmp->next_buffer;
731 tmp = vlib_get_buffer (vm, tmp->next_buffer);
739 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
742 while (~0 != sub_chain_bi);
746 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
747 goto free_buffers_and_return;
749 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
750 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
751 if (total_length < first_b->current_length)
753 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
754 goto free_buffers_and_return;
756 total_length -= first_b->current_length;
757 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
758 first_b->total_length_not_including_first_buffer = total_length;
759 // drop fragment header
760 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
761 ip6_header_t *ip = vlib_buffer_get_current (first_b);
762 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
763 ip6_ext_hdr_chain_t hdr_chain;
764 ip6_ext_header_t *prev_hdr = 0;
765 int res = ip6_ext_header_walk (first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
768 (hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION))
770 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
771 goto free_buffers_and_return;
773 frag_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res].offset);
776 prev_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res - 1].offset);
777 prev_hdr->next_hdr = frag_hdr->next_hdr;
781 ip->protocol = frag_hdr->next_hdr;
783 if (hdr_chain.eh[res].offset != ip6_frag_hdr_offset)
785 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
786 goto free_buffers_and_return;
788 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
789 first_b->current_length - ip6_frag_hdr_offset -
790 sizeof (ip6_frag_hdr_t));
791 first_b->current_length -= sizeof (*frag_hdr);
793 clib_host_to_net_u16 (total_length + first_b->current_length -
795 if (!vlib_buffer_chain_linearize (vm, first_b))
797 rv = IP6_FULL_REASS_RC_NO_BUF;
798 goto free_buffers_and_return;
800 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
801 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
803 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
806 // following code does a hexdump of packet fragments to stdout ...
809 u32 bi = reass->first_bi;
813 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
814 s = format (s, "%u: %U\n", bi, format_hexdump,
815 vlib_buffer_get_current (b), b->current_length);
816 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
825 printf ("%.*s\n", vec_len (s), s);
834 *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
838 *next0 = reass->next_index;
840 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
841 ip6_full_reass_free (rm, rt, reass);
843 free_buffers_and_return:
844 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
845 vec_free (vec_drop_compress);
850 ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
851 ip6_full_reass_t * reass,
852 u32 prev_range_bi, u32 new_next_bi)
855 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
856 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
857 if (~0 != prev_range_bi)
859 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
860 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
861 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
862 prev_vnb->ip.reass.next_range_bi = new_next_bi;
866 if (~0 != reass->first_bi)
868 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
870 reass->first_bi = new_next_bi;
872 reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
875 always_inline ip6_full_reass_rc_t
876 ip6_full_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
877 ip6_full_reass_main_t *rm,
878 ip6_full_reass_per_thread_t *rt,
879 ip6_full_reass_t *reass, u32 *bi0, u32 *next0,
880 u32 *error0, ip6_frag_hdr_t *frag_hdr,
881 bool is_custom_app, u32 *handoff_thread_idx,
885 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
886 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
889 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
890 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
893 fvnb->ip.reass.ip6_frag_hdr_offset =
894 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
895 ip6_header_t *fip = vlib_buffer_get_current (fb);
896 if (fb->current_length < sizeof (*fip) ||
897 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
898 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
900 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
903 u32 fragment_first = fvnb->ip.reass.fragment_first =
904 ip6_frag_hdr_offset_bytes (frag_hdr);
905 u32 fragment_length =
906 vlib_buffer_length_in_chain (vm, fb) -
907 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
908 if (0 == fragment_length)
910 return IP6_FULL_REASS_RC_INVALID_FRAG_LEN;
912 u32 fragment_last = fvnb->ip.reass.fragment_last =
913 fragment_first + fragment_length - 1;
914 int more_fragments = ip6_frag_hdr_more (frag_hdr);
915 u32 candidate_range_bi = reass->first_bi;
916 u32 prev_range_bi = ~0;
917 fvnb->ip.reass.range_first = fragment_first;
918 fvnb->ip.reass.range_last = fragment_last;
919 fvnb->ip.reass.next_range_bi = ~0;
922 reass->last_packet_octet = fragment_last;
924 if (~0 == reass->first_bi)
926 // starting a new reassembly
927 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
928 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
930 reass->fragments_n = 1;
931 goto check_if_done_maybe;
933 reass->min_fragment_length =
934 clib_min (clib_net_to_host_u16 (fip->payload_length),
935 fvnb->ip.reass.estimated_mtu);
936 while (~0 != candidate_range_bi)
938 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
939 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
940 if (fragment_first > candidate_vnb->ip.reass.range_last)
942 // this fragments starts after candidate range
943 prev_range_bi = candidate_range_bi;
944 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
945 if (candidate_vnb->ip.reass.range_last < fragment_last &&
946 ~0 == candidate_range_bi)
948 // special case - this fragment falls beyond all known ranges
949 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
956 if (fragment_last < candidate_vnb->ip.reass.range_first)
958 // this fragment ends before candidate range without any overlap
959 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
963 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
964 fragment_last == candidate_vnb->ip.reass.range_last)
966 // duplicate fragment - ignore
970 // overlapping fragment - not allowed by RFC 8200
971 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
973 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr,
976 return IP6_FULL_REASS_RC_OVERLAP;
980 ++reass->fragments_n;
984 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
986 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr, RANGE_NEW,
990 else if (skip_bihash)
992 // if this reassembly is not in bihash, then the packet must have been
994 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
996 if (~0 != reass->last_packet_octet &&
997 reass->data_len == reass->last_packet_octet + 1)
999 *handoff_thread_idx = reass->sendout_thread_index;
1001 reass->memory_owner_thread_index != reass->sendout_thread_index;
1002 ip6_full_reass_rc_t rc =
1003 ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
1005 if (IP6_FULL_REASS_RC_OK == rc && handoff)
1007 return IP6_FULL_REASS_RC_HANDOFF;
1015 // if this reassembly is not in bihash, it should've been an atomic
1016 // fragment and thus finalized
1017 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1022 if (reass->fragments_n > rm->max_reass_len)
1024 return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
1029 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1030 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
1033 return IP6_FULL_REASS_RC_OK;
1037 ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
1039 ip6_ext_hdr_chain_t *hc)
1041 int nh = hc->eh[hc->length - 1].protocol;
1042 /* Checking to see if it's a terminating header */
1043 if (ip6_ext_hdr (nh))
1045 icmp6_error_set_vnet_buffer (
1046 b, ICMP6_parameter_problem,
1047 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
1048 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
1055 ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
1057 ip6_frag_hdr_t * frag_hdr)
1059 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1060 ip6_header_t *ip = vlib_buffer_get_current (b);
1061 int more_fragments = ip6_frag_hdr_more (frag_hdr);
1062 u32 fragment_length =
1063 vlib_buffer_length_in_chain (vm, b) -
1064 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1065 if (more_fragments && 0 != fragment_length % 8)
1067 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1068 ICMP6_parameter_problem_erroneous_header_field,
1069 (u8 *) & ip->payload_length - (u8 *) ip);
1076 ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
1078 ip6_frag_hdr_t * frag_hdr)
1080 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1081 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
1082 u32 fragment_length =
1083 vlib_buffer_length_in_chain (vm, b) -
1084 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1085 if (fragment_first + fragment_length > 65535)
1087 ip6_header_t *ip0 = vlib_buffer_get_current (b);
1088 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1089 ICMP6_parameter_problem_erroneous_header_field,
1090 (u8 *) & frag_hdr->fragment_offset_and_more
1098 ip6_full_reassembly_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1099 vlib_frame_t *frame, bool is_feature,
1100 bool is_custom_app, bool is_local)
1102 u32 *from = vlib_frame_vector_args (frame);
1103 u32 n_left_from, n_left_to_next, *to_next, next_index;
1104 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1105 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
1106 clib_spinlock_lock (&rt->lock);
1108 n_left_from = frame->n_vectors;
1109 next_index = node->cached_next_index;
1110 while (n_left_from > 0)
1112 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1114 while (n_left_from > 0 && n_left_to_next > 0)
1118 u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1119 u32 error0 = IP6_ERROR_NONE;
1123 b0 = vlib_get_buffer (vm, bi0);
1125 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
1126 ip6_frag_hdr_t *frag_hdr;
1127 ip6_ext_hdr_chain_t hdr_chain;
1128 int res = ip6_ext_header_walk (
1129 b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
1131 hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION)
1133 // this is a mangled packet - no fragmentation
1134 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1135 ip6_full_reass_add_trace (vm, node, NULL, bi0, NULL, PASSTHROUGH,
1139 if (is_local && !rm->is_local_reass_enabled)
1141 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1145 ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
1146 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1147 hdr_chain.eh[res].offset;
1149 if (0 == ip6_frag_hdr_offset (frag_hdr))
1151 // first fragment - verify upper-layer is present
1152 if (!ip6_full_reass_verify_upper_layer_present (node, b0,
1155 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1159 if (!ip6_full_reass_verify_fragment_multiple_8 (vm, b0, frag_hdr) ||
1160 !ip6_full_reass_verify_packet_size_lt_64k (vm, b0, frag_hdr))
1162 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1166 int skip_bihash = 0;
1167 ip6_full_reass_kv_t kv;
1170 if (0 == ip6_frag_hdr_offset (frag_hdr) &&
1171 !ip6_frag_hdr_more (frag_hdr))
1173 // this is atomic fragment and needs to be processed separately
1178 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1179 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1180 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1181 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1183 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1184 vnet_buffer (b0)->sw_if_index[VLIB_RX]))
1186 (u64) frag_hdr->identification;
1187 kv.k.as_u64[5] = ip0->protocol;
1190 ip6_full_reass_t *reass = ip6_full_reass_find_or_create (
1191 vm, node, rm, rt, &kv, &icmp_bi, &do_handoff, skip_bihash);
1195 const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1196 if (0 == fragment_first)
1198 reass->sendout_thread_index = vm->thread_index;
1201 if (PREDICT_FALSE (do_handoff))
1203 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1204 vnet_buffer (b0)->ip.reass.owner_thread_index =
1205 kv.v.memory_owner_thread_index;
1209 u32 handoff_thread_idx;
1211 switch (ip6_full_reass_update (
1212 vm, node, rm, rt, reass, &bi0, &next0, &error0, frag_hdr,
1213 is_custom_app, &handoff_thread_idx, skip_bihash))
1215 case IP6_FULL_REASS_RC_OK:
1216 /* nothing to do here */
1218 case IP6_FULL_REASS_RC_HANDOFF:
1219 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1220 b0 = vlib_get_buffer (vm, bi0);
1221 vnet_buffer (b0)->ip.reass.owner_thread_index =
1224 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
1225 counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
1227 case IP6_FULL_REASS_RC_NO_BUF:
1228 counter = IP6_ERROR_REASS_NO_BUF;
1230 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
1231 counter = IP6_ERROR_REASS_INTERNAL_ERROR;
1233 case IP6_FULL_REASS_RC_INVALID_FRAG_LEN:
1234 counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
1236 case IP6_FULL_REASS_RC_OVERLAP:
1237 counter = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
1242 vlib_node_increment_counter (vm, node->node_index, counter,
1244 ip6_full_reass_drop_all (vm, node, reass, bi0);
1245 ip6_full_reass_free (rm, rt, reass);
1254 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1258 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1259 next0 = fvnb->ip.reass.error_next_index;
1261 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1269 n_left_to_next -= 1;
1271 /* bi0 might have been updated by reass_finalize, reload */
1272 b0 = vlib_get_buffer (vm, bi0);
1273 if (IP6_ERROR_NONE != error0)
1275 b0->error = node->errors[error0];
1278 if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
1280 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1282 ip6_full_reass_add_trace (
1283 vm, node, NULL, bi0, frag_hdr, HANDOFF,
1284 vnet_buffer (b0)->ip.reass.owner_thread_index);
1287 else if (is_feature && IP6_ERROR_NONE == error0)
1289 vnet_feature_next (&next0, b0);
1291 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1292 n_left_to_next, bi0, next0);
1297 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1298 to_next[0] = icmp_bi;
1300 n_left_to_next -= 1;
1301 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1302 n_left_to_next, icmp_bi,
1310 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1313 clib_spinlock_unlock (&rt->lock);
1314 return frame->n_vectors;
1317 static char *ip6_full_reassembly_error_strings[] = {
1318 #define _(sym, string) string,
1323 VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1324 vlib_node_runtime_t * node,
1325 vlib_frame_t * frame)
1327 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1328 false /* is_custom_app */,
1329 false /* is_local */);
1332 VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1333 .name = "ip6-full-reassembly",
1334 .vector_size = sizeof (u32),
1335 .format_trace = format_ip6_full_reass_trace,
1336 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1337 .error_strings = ip6_full_reassembly_error_strings,
1338 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1341 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1342 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1343 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1344 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
1348 VLIB_NODE_FN (ip6_local_full_reass_node)
1349 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1351 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1352 false /* is_custom_app */,
1353 true /* is_local */);
1356 VLIB_REGISTER_NODE (ip6_local_full_reass_node) = {
1357 .name = "ip6-local-full-reassembly",
1358 .vector_size = sizeof (u32),
1359 .format_trace = format_ip6_full_reass_trace,
1360 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1361 .error_strings = ip6_full_reassembly_error_strings,
1362 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1365 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1366 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1367 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1368 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-local-full-reassembly-handoff",
1372 VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1373 vlib_node_runtime_t * node,
1374 vlib_frame_t * frame)
1376 return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */,
1377 false /* is_custom_app */,
1378 false /* is_local */);
1381 VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1382 .name = "ip6-full-reassembly-feature",
1383 .vector_size = sizeof (u32),
1384 .format_trace = format_ip6_full_reass_trace,
1385 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1386 .error_strings = ip6_full_reassembly_error_strings,
1387 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1390 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1391 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1392 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1393 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
1397 VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
1398 .arc_name = "ip6-unicast",
1399 .node_name = "ip6-full-reassembly-feature",
1400 .runs_before = VNET_FEATURES ("ip6-lookup",
1401 "ipsec6-input-feature"),
1405 #ifndef CLIB_MARCH_VARIANT
1407 ip6_full_reass_get_nbuckets ()
1409 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1413 nbuckets = (u32) (rm->max_reass_n / IP6_FULL_REASS_HT_LOAD_FACTOR);
1415 for (i = 0; i < 31; i++)
1416 if ((1 << i) >= nbuckets)
1422 #endif /* CLIB_MARCH_VARIANT */
1426 IP6_EVENT_CONFIG_CHANGED = 1,
1427 } ip6_full_reass_event_t;
1429 #ifndef CLIB_MARCH_VARIANT
1433 clib_bihash_48_8_t *new_hash;
1434 } ip6_rehash_cb_ctx;
1437 ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1439 ip6_rehash_cb_ctx *ctx = _ctx;
1440 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1444 return (BIHASH_WALK_CONTINUE);
1448 ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1449 u32 max_reassembly_length,
1450 u32 expire_walk_interval_ms)
1452 ip6_full_reass_main.timeout_ms = timeout_ms;
1453 ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1454 ip6_full_reass_main.max_reass_n = max_reassemblies;
1455 ip6_full_reass_main.max_reass_len = max_reassembly_length;
1456 ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1460 ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1461 u32 max_reassembly_length, u32 expire_walk_interval_ms)
1463 u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1464 ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1465 max_reassembly_length, expire_walk_interval_ms);
1466 vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1467 ip6_full_reass_main.ip6_full_reass_expire_node_idx,
1468 IP6_EVENT_CONFIG_CHANGED, 0);
1469 u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1470 if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
1472 clib_bihash_48_8_t new_hash;
1473 clib_memset (&new_hash, 0, sizeof (new_hash));
1474 ip6_rehash_cb_ctx ctx;
1476 ctx.new_hash = &new_hash;
1477 clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
1478 new_nbuckets * 1024);
1479 clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
1480 ip6_rehash_cb, &ctx);
1483 clib_bihash_free_48_8 (&new_hash);
1488 clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1489 clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1490 sizeof (ip6_full_reass_main.hash));
1491 clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
1498 ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1499 u32 * max_reassembly_length,
1500 u32 * expire_walk_interval_ms)
1502 *timeout_ms = ip6_full_reass_main.timeout_ms;
1503 *max_reassemblies = ip6_full_reass_main.max_reass_n;
1504 *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1505 *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
1509 static clib_error_t *
1510 ip6_full_reass_init_function (vlib_main_t * vm)
1512 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1513 clib_error_t *error = 0;
1519 vec_validate (rm->per_thread_data, vlib_num_workers ());
1520 ip6_full_reass_per_thread_t *rt;
1521 vec_foreach (rt, rm->per_thread_data)
1523 clib_spinlock_init (&rt->lock);
1524 pool_alloc (rt->pool, rm->max_reass_n);
1527 node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
1529 rm->ip6_full_reass_expire_node_idx = node->index;
1531 ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1532 IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1533 IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1534 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1536 nbuckets = ip6_full_reass_get_nbuckets ();
1537 clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1540 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1542 rm->ip6_icmp_error_idx = node->index;
1544 if ((error = vlib_call_init_function (vm, ip_main_init)))
1546 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1547 ip6_local_full_reass_node.index);
1548 rm->is_local_reass_enabled = 1;
1550 rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
1551 rm->fq_local_index =
1552 vlib_frame_queue_main_init (ip6_local_full_reass_node.index, 0);
1553 rm->fq_feature_index =
1554 vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
1556 rm->feature_use_refcount_per_intf = NULL;
1560 VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
1561 #endif /* CLIB_MARCH_VARIANT */
1564 ip6_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
1565 CLIB_UNUSED (vlib_frame_t *f))
1567 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1568 uword event_type, *event_data = 0;
1572 vlib_process_wait_for_event_or_clock (vm,
1573 (f64) rm->expire_walk_interval_ms
1574 / (f64) MSEC_PER_SEC);
1575 event_type = vlib_process_get_events (vm, &event_data);
1580 /* no events => timeout */
1582 case IP6_EVENT_CONFIG_CHANGED:
1583 /* nothing to do here */
1586 clib_warning ("BUG: event type 0x%wx", event_type);
1589 f64 now = vlib_time_now (vm);
1591 ip6_full_reass_t *reass;
1592 int *pool_indexes_to_free = NULL;
1594 uword thread_index = 0;
1596 const uword nthreads = vlib_num_workers () + 1;
1597 u32 *vec_icmp_bi = NULL;
1598 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1600 ip6_full_reass_per_thread_t *rt =
1601 &rm->per_thread_data[thread_index];
1602 clib_spinlock_lock (&rt->lock);
1604 vec_reset_length (pool_indexes_to_free);
1605 pool_foreach_index (index, rt->pool) {
1606 reass = pool_elt_at_index (rt->pool, index);
1607 if (now > reass->last_heard + rm->timeout)
1609 vec_add1 (pool_indexes_to_free, index);
1613 vec_foreach (i, pool_indexes_to_free)
1615 ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1617 ip6_full_reass_on_timeout (vm, node, reass, &icmp_bi);
1619 vec_add1 (vec_icmp_bi, icmp_bi);
1621 ip6_full_reass_free (rm, rt, reass);
1624 clib_spinlock_unlock (&rt->lock);
1627 while (vec_len (vec_icmp_bi) > 0)
1630 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1631 u32 *to_next = vlib_frame_vector_args (f);
1632 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
1633 int trace_frame = 0;
1634 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1636 u32 bi = vec_pop (vec_icmp_bi);
1637 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1638 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1640 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1644 n_left_to_next -= 1;
1646 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
1647 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1650 vec_free (pool_indexes_to_free);
1651 vec_free (vec_icmp_bi);
1654 vec_set_len (event_data, 0);
1661 VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
1662 .function = ip6_full_reass_walk_expired,
1663 .format_trace = format_ip6_full_reass_trace,
1664 .type = VLIB_NODE_TYPE_PROCESS,
1665 .name = "ip6-full-reassembly-expire-walk",
1667 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1668 .error_strings = ip6_full_reassembly_error_strings,
1673 format_ip6_full_reass_key (u8 * s, va_list * args)
1675 ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
1676 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1677 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1678 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1683 format_ip6_full_reass (u8 * s, va_list * args)
1685 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1686 ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
1688 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
1689 "last_packet_octet: %u, trace_op_counter: %u\n",
1690 reass->id, format_ip6_full_reass_key, &reass->key,
1691 reass->first_bi, reass->data_len, reass->last_packet_octet,
1692 reass->trace_op_counter);
1693 u32 bi = reass->first_bi;
1697 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1698 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1699 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1700 "fragment[%u, %u]\n",
1701 counter, vnb->ip.reass.range_first,
1702 vnb->ip.reass.range_last, bi,
1703 ip6_full_reass_buffer_get_data_offset (b),
1704 ip6_full_reass_buffer_get_data_len (b),
1705 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1706 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1708 bi = b->next_buffer;
1718 static clib_error_t *
1719 show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1720 CLIB_UNUSED (vlib_cli_command_t * lmd))
1722 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1724 vlib_cli_output (vm, "---------------------");
1725 vlib_cli_output (vm, "IP6 reassembly status");
1726 vlib_cli_output (vm, "---------------------");
1727 bool details = false;
1728 if (unformat (input, "details"))
1733 u32 sum_reass_n = 0;
1734 u64 sum_buffers_n = 0;
1735 ip6_full_reass_t *reass;
1737 const uword nthreads = vlib_num_workers () + 1;
1738 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1740 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1741 clib_spinlock_lock (&rt->lock);
1744 pool_foreach (reass, rt->pool) {
1745 vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
1748 sum_reass_n += rt->reass_n;
1749 clib_spinlock_unlock (&rt->lock);
1751 vlib_cli_output (vm, "---------------------");
1752 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1753 (long unsigned) sum_reass_n);
1754 vlib_cli_output (vm,
1755 "Maximum configured concurrent full IP6 reassemblies per worker-thread: %lu\n",
1756 (long unsigned) rm->max_reass_n);
1757 vlib_cli_output (vm,
1758 "Maximum configured amount of fragments "
1759 "per full IP6 reassembly: %lu\n",
1760 (long unsigned) rm->max_reass_len);
1761 vlib_cli_output (vm,
1762 "Maximum configured full IP6 reassembly timeout: %lums\n",
1763 (long unsigned) rm->timeout_ms);
1764 vlib_cli_output (vm,
1765 "Maximum configured full IP6 reassembly expire walk interval: %lums\n",
1766 (long unsigned) rm->expire_walk_interval_ms);
1767 vlib_cli_output (vm, "Buffers in use: %lu\n",
1768 (long unsigned) sum_buffers_n);
1772 VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1773 .path = "show ip6-full-reassembly",
1774 .short_help = "show ip6-full-reassembly [details]",
1775 .function = show_ip6_full_reass,
1778 #ifndef CLIB_MARCH_VARIANT
1780 ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1782 return vnet_feature_enable_disable ("ip6-unicast",
1783 "ip6-full-reassembly-feature",
1784 sw_if_index, enable_disable, 0, 0);
1786 #endif /* CLIB_MARCH_VARIANT */
1788 #define foreach_ip6_full_reassembly_handoff_error \
1789 _(CONGESTION_DROP, "congestion drop")
1794 #define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1795 foreach_ip6_full_reassembly_handoff_error
1797 IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1798 } ip6_full_reassembly_handoff_error_t;
1800 static char *ip6_full_reassembly_handoff_error_strings[] = {
1801 #define _(sym,string) string,
1802 foreach_ip6_full_reassembly_handoff_error
1808 u32 next_worker_index;
1809 } ip6_full_reassembly_handoff_trace_t;
1812 format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
1814 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1815 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1816 ip6_full_reassembly_handoff_trace_t *t =
1817 va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
1820 format (s, "ip6-full-reassembly-handoff: next-worker %d",
1821 t->next_worker_index);
1827 ip6_full_reassembly_handoff_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1828 vlib_frame_t *frame, bool is_feature,
1831 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1833 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1834 u32 n_enq, n_left_from, *from;
1835 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1838 from = vlib_frame_vector_args (frame);
1839 n_left_from = frame->n_vectors;
1840 vlib_get_buffers (vm, from, bufs, n_left_from);
1843 ti = thread_indices;
1847 fq_index = rm->fq_feature_index;
1853 fq_index = rm->fq_local_index;
1857 fq_index = rm->fq_index;
1861 while (n_left_from > 0)
1863 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1866 ((node->flags & VLIB_NODE_FLAG_TRACE)
1867 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1869 ip6_full_reassembly_handoff_trace_t *t =
1870 vlib_add_trace (vm, node, b[0], sizeof (*t));
1871 t->next_worker_index = ti[0];
1878 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
1879 thread_indices, frame->n_vectors, 1);
1881 if (n_enq < frame->n_vectors)
1882 vlib_node_increment_counter (vm, node->node_index,
1883 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1884 frame->n_vectors - n_enq);
1885 return frame->n_vectors;
1888 VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
1889 vlib_node_runtime_t * node,
1890 vlib_frame_t * frame)
1892 return ip6_full_reassembly_handoff_inline (
1893 vm, node, frame, false /* is_feature */, false /* is_local */);
1896 VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
1897 .name = "ip6-full-reassembly-handoff",
1898 .vector_size = sizeof (u32),
1899 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1900 .error_strings = ip6_full_reassembly_handoff_error_strings,
1901 .format_trace = format_ip6_full_reassembly_handoff_trace,
1910 VLIB_NODE_FN (ip6_local_full_reassembly_handoff_node)
1911 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1913 return ip6_full_reassembly_handoff_inline (
1914 vm, node, frame, false /* is_feature */, true /* is_feature */);
1917 VLIB_REGISTER_NODE (ip6_local_full_reassembly_handoff_node) = {
1918 .name = "ip6-local-full-reassembly-handoff",
1919 .vector_size = sizeof (u32),
1920 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1921 .error_strings = ip6_full_reassembly_handoff_error_strings,
1922 .format_trace = format_ip6_full_reassembly_handoff_trace,
1931 VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
1932 vlib_node_runtime_t * node, vlib_frame_t * frame)
1934 return ip6_full_reassembly_handoff_inline (
1935 vm, node, frame, true /* is_feature */, false /* is_local */);
1938 VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
1939 .name = "ip6-full-reass-feature-hoff",
1940 .vector_size = sizeof (u32),
1941 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1942 .error_strings = ip6_full_reassembly_handoff_error_strings,
1943 .format_trace = format_ip6_full_reassembly_handoff_trace,
1952 #ifndef CLIB_MARCH_VARIANT
1954 ip6_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1956 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1957 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1960 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1962 ++rm->feature_use_refcount_per_intf[sw_if_index];
1963 return vnet_feature_enable_disable ("ip6-unicast",
1964 "ip6-full-reassembly-feature",
1965 sw_if_index, 1, 0, 0);
1967 ++rm->feature_use_refcount_per_intf[sw_if_index];
1971 --rm->feature_use_refcount_per_intf[sw_if_index];
1972 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1973 return vnet_feature_enable_disable ("ip6-unicast",
1974 "ip6-full-reassembly-feature",
1975 sw_if_index, 0, 0, 0);
1981 ip6_local_full_reass_enable_disable (int enable)
1985 if (!ip6_full_reass_main.is_local_reass_enabled)
1987 ip6_full_reass_main.is_local_reass_enabled = 1;
1988 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1989 ip6_local_full_reass_node.index);
1994 if (ip6_full_reass_main.is_local_reass_enabled)
1996 ip6_full_reass_main.is_local_reass_enabled = 0;
1997 ip6_unregister_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION);
2003 ip6_local_full_reass_enabled ()
2005 return ip6_full_reass_main.is_local_reass_enabled;
2011 * fd.io coding-style-patch-verification: ON
2014 * eval: (c-set-style "gnu")