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>
29 #define MSEC_PER_SEC 1000
30 #define IP6_FULL_REASS_TIMEOUT_DEFAULT_MS 100
31 #define IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
32 #define IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT 1024
33 #define IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
34 #define IP6_FULL_REASS_HT_LOAD_FACTOR (0.75)
39 IP6_FULL_REASS_RC_INTERNAL_ERROR,
40 IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS,
41 IP6_FULL_REASS_RC_NO_BUF,
42 IP6_FULL_REASS_RC_HANDOFF,
43 } ip6_full_reass_rc_t;
60 } ip6_full_reass_key_t;
67 u32 memory_owner_thread_index;
70 } ip6_full_reass_val_t;
76 ip6_full_reass_key_t k;
77 ip6_full_reass_val_t v;
79 clib_bihash_kv_48_8_t kv;
80 } ip6_full_reass_kv_t;
84 ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
86 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
87 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
91 ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
93 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
94 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
95 (vnb->ip.reass.fragment_first +
96 ip6_full_reass_buffer_get_data_offset (b)) + 1;
102 ip6_full_reass_key_t key;
103 // time when last packet was received
105 // internal id of this reassembly
107 // buffer index of first buffer in this reassembly context
109 // last octet of packet, ~0 until fragment without more_fragments arrives
110 u32 last_packet_octet;
111 // length of data collected so far
113 // trace operation counter
114 u32 trace_op_counter;
115 // next index - used by custom apps (~0 if not set)
117 // error next index - used by custom apps (~0 if not set)
118 u32 error_next_index;
119 // minimum fragment length for this reassembly - used to estimate MTU
120 u16 min_fragment_length;
121 // number of fragments for this reassembly
123 // thread owning memory for this context (whose pool contains this ctx)
124 u32 memory_owner_thread_index;
125 // thread which received fragment with offset 0 and which sends out the
126 // completed reassembly
127 u32 sendout_thread_index;
132 ip6_full_reass_t *pool;
135 clib_spinlock_t lock;
136 } ip6_full_reass_per_thread_t;
143 u32 expire_walk_interval_ms;
144 // maximum number of fragments in one reassembly
146 // maximum number of reassemblies
150 clib_bihash_48_8_t hash;
153 ip6_full_reass_per_thread_t *per_thread_data;
156 vlib_main_t *vlib_main;
158 // node index of ip6-drop node
160 u32 ip6_icmp_error_idx;
161 u32 ip6_full_reass_expire_node_idx;
163 /** Worker handoff */
165 u32 fq_feature_index;
167 } ip6_full_reass_main_t;
169 extern ip6_full_reass_main_t ip6_full_reass_main;
171 #ifndef CLIB_MARCH_VARIANT
172 ip6_full_reass_main_t ip6_full_reass_main;
173 #endif /* CLIB_MARCH_VARIANT */
177 IP6_FULL_REASSEMBLY_NEXT_INPUT,
178 IP6_FULL_REASSEMBLY_NEXT_DROP,
179 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
180 IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
181 IP6_FULL_REASSEMBLY_N_NEXT,
182 } ip6_full_reass_next_t;
188 ICMP_ERROR_RT_EXCEEDED,
189 ICMP_ERROR_FL_TOO_BIG,
190 ICMP_ERROR_FL_NOT_MULT_8,
193 } ip6_full_reass_trace_operation_e;
203 } ip6_full_reass_range_trace_t;
207 ip6_full_reass_trace_operation_e action;
209 ip6_full_reass_range_trace_t trace_range;
216 } ip6_full_reass_trace_t;
219 ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
220 ip6_full_reass_range_trace_t * trace)
222 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
223 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
224 trace->range_first = vnb->ip.reass.range_first;
225 trace->range_last = vnb->ip.reass.range_last;
226 trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
227 trace->data_len = ip6_full_reass_buffer_get_data_len (b);
228 trace->range_bi = bi;
232 format_ip6_full_reass_range_trace (u8 * s, va_list * args)
234 ip6_full_reass_range_trace_t *trace =
235 va_arg (*args, ip6_full_reass_range_trace_t *);
237 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
238 trace->range_last, trace->data_offset, trace->data_len,
244 format_ip6_full_reass_trace (u8 * s, va_list * args)
246 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
247 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
248 ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
250 if (~0 != t->reass_id)
252 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
253 indent = format_get_indent (s);
254 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
255 t->trace_range.first_bi, t->total_data_len,
256 t->fragment_first, t->fragment_last);
261 s = format (s, "\n%Unew %U", format_white_space, indent,
262 format_ip6_full_reass_range_trace, &t->trace_range);
265 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
266 format_ip6_full_reass_range_trace, &t->trace_range);
268 case ICMP_ERROR_FL_TOO_BIG:
269 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
270 format_white_space, indent,
271 format_ip6_full_reass_range_trace, &t->trace_range);
273 case ICMP_ERROR_FL_NOT_MULT_8:
274 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
275 format_white_space, indent,
276 format_ip6_full_reass_range_trace, &t->trace_range);
278 case ICMP_ERROR_RT_EXCEEDED:
279 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
280 format_white_space, indent);
283 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
287 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
295 ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
296 ip6_full_reass_main_t * rm,
297 ip6_full_reass_t * reass, u32 bi,
298 ip6_full_reass_trace_operation_e action,
301 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
302 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
303 ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
306 t->reass_id = reass->id;
307 t->op_id = reass->trace_op_counter;
308 t->trace_range.first_bi = reass->first_bi;
309 t->total_data_len = reass->data_len;
310 ++reass->trace_op_counter;
317 t->thread_id = vm->thread_index;
318 t->thread_id_to = thread_id_to;
319 ip6_full_reass_trace_details (vm, bi, &t->trace_range);
320 t->fragment_first = vnb->ip.reass.fragment_first;
321 t->fragment_last = vnb->ip.reass.fragment_last;
324 s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
325 printf ("%.*s\n", vec_len (s), s);
327 vec_reset_length (s);
332 ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
333 ip6_full_reass_t * reass)
335 pool_put (rt->pool, reass);
340 ip6_full_reass_free (ip6_full_reass_main_t * rm,
341 ip6_full_reass_per_thread_t * rt,
342 ip6_full_reass_t * reass)
344 clib_bihash_kv_48_8_t kv;
345 kv.key[0] = reass->key.as_u64[0];
346 kv.key[1] = reass->key.as_u64[1];
347 kv.key[2] = reass->key.as_u64[2];
348 kv.key[3] = reass->key.as_u64[3];
349 kv.key[4] = reass->key.as_u64[4];
350 kv.key[5] = reass->key.as_u64[5];
351 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
352 ip6_full_reass_free_ctx (rt, reass);
356 ip6_full_reass_drop_all (vlib_main_t * vm, vlib_node_runtime_t * node,
357 ip6_full_reass_main_t * rm, ip6_full_reass_t * reass)
359 u32 range_bi = reass->first_bi;
360 vlib_buffer_t *range_b;
361 vnet_buffer_opaque_t *range_vnb;
363 while (~0 != range_bi)
365 range_b = vlib_get_buffer (vm, range_bi);
366 range_vnb = vnet_buffer (range_b);
370 vec_add1 (to_free, bi);
371 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
372 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
375 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
382 range_bi = range_vnb->ip.reass.next_range_bi;
384 /* send to next_error_index */
385 if (~0 != reass->error_next_index)
387 u32 n_left_to_next, *to_next, next_index;
389 next_index = reass->error_next_index;
392 while (vec_len (to_free) > 0)
394 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
396 while (vec_len (to_free) > 0 && n_left_to_next > 0)
398 bi = vec_pop (to_free);
407 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
412 vlib_buffer_free (vm, to_free, vec_len (to_free));
418 ip6_full_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
419 ip6_full_reass_main_t * rm,
420 ip6_full_reass_t * reass, u32 * icmp_bi)
422 if (~0 == reass->first_bi)
426 if (~0 == reass->next_index) // custom apps don't want icmp
428 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
429 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
431 *icmp_bi = reass->first_bi;
432 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
434 ip6_full_reass_add_trace (vm, node, rm, reass, reass->first_bi,
435 ICMP_ERROR_RT_EXCEEDED, ~0);
437 // fragment with offset zero received - send icmp message back
438 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
440 // separate first buffer from chain and steer it towards icmp node
441 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
442 reass->first_bi = b->next_buffer;
446 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
448 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
449 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
453 ip6_full_reass_drop_all (vm, node, rm, reass);
456 always_inline ip6_full_reass_t *
457 ip6_full_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
458 ip6_full_reass_main_t * rm,
459 ip6_full_reass_per_thread_t * rt,
460 ip6_full_reass_kv_t * kv, u32 * icmp_bi,
463 ip6_full_reass_t *reass;
469 now = vlib_time_now (vm);
471 if (!clib_bihash_search_48_8
472 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, (clib_bihash_kv_48_8_t *) kv))
475 pool_elt_at_index (rm->per_thread_data
476 [kv->v.memory_owner_thread_index].pool,
478 if (vm->thread_index != kv->v.memory_owner_thread_index)
484 if (now > reass->last_heard + rm->timeout)
486 ip6_full_reass_on_timeout (vm, node, rm, reass, icmp_bi);
487 ip6_full_reass_free (rm, rt, reass);
494 reass->last_heard = now;
498 if (rt->reass_n >= rm->max_reass_n)
505 pool_get (rt->pool, reass);
506 clib_memset (reass, 0, sizeof (*reass));
507 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
509 reass->first_bi = ~0;
510 reass->last_packet_octet = ~0;
512 reass->next_index = ~0;
513 reass->error_next_index = ~0;
517 reass->key.as_u64[0] = ((clib_bihash_kv_48_8_t *) kv)->key[0];
518 reass->key.as_u64[1] = ((clib_bihash_kv_48_8_t *) kv)->key[1];
519 reass->key.as_u64[2] = ((clib_bihash_kv_48_8_t *) kv)->key[2];
520 reass->key.as_u64[3] = ((clib_bihash_kv_48_8_t *) kv)->key[3];
521 reass->key.as_u64[4] = ((clib_bihash_kv_48_8_t *) kv)->key[4];
522 reass->key.as_u64[5] = ((clib_bihash_kv_48_8_t *) kv)->key[5];
523 kv->v.reass_index = (reass - rt->pool);
524 kv->v.memory_owner_thread_index = vm->thread_index;
525 reass->last_heard = now;
528 clib_bihash_add_del_48_8 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, 2);
531 ip6_full_reass_free (rm, rt, reass);
533 // if other worker created a context already work with the other copy
541 always_inline ip6_full_reass_rc_t
542 ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
543 ip6_full_reass_main_t * rm,
544 ip6_full_reass_per_thread_t * rt,
545 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
546 u32 * error0, bool is_custom_app)
548 *bi0 = reass->first_bi;
549 *error0 = IP6_ERROR_NONE;
550 ip6_frag_hdr_t *frag_hdr;
551 vlib_buffer_t *last_b = NULL;
552 u32 sub_chain_bi = reass->first_bi;
553 u32 total_length = 0;
556 u32 *vec_drop_compress = NULL;
557 ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
560 u32 tmp_bi = sub_chain_bi;
561 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
562 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
563 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
564 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
566 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
567 goto free_buffers_and_return;
570 u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
571 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
572 sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
574 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
575 if (tmp_bi == reass->first_bi)
577 /* first buffer - keep ip6 header */
578 if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
580 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
581 goto free_buffers_and_return;
584 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
585 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
587 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
589 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
590 goto free_buffers_and_return;
594 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
600 if (trim_front > tmp->current_length)
602 /* drop whole buffer */
603 vec_add1 (vec_drop_compress, tmp_bi);
604 trim_front -= tmp->current_length;
605 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
607 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
608 goto free_buffers_and_return;
610 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
611 tmp_bi = tmp->next_buffer;
612 tmp = vlib_get_buffer (vm, tmp_bi);
617 vlib_buffer_advance (tmp, trim_front);
625 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
626 last_b->next_buffer = tmp_bi;
629 if (keep_data <= tmp->current_length)
631 tmp->current_length = keep_data;
636 keep_data -= tmp->current_length;
637 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
639 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
640 goto free_buffers_and_return;
643 total_length += tmp->current_length;
647 vec_add1 (vec_drop_compress, tmp_bi);
648 if (reass->first_bi == tmp_bi)
650 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
651 goto free_buffers_and_return;
655 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
657 tmp_bi = tmp->next_buffer;
658 tmp = vlib_get_buffer (vm, tmp->next_buffer);
666 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
669 while (~0 != sub_chain_bi);
673 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
674 goto free_buffers_and_return;
676 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
677 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
678 if (total_length < first_b->current_length)
680 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
681 goto free_buffers_and_return;
683 total_length -= first_b->current_length;
684 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
685 first_b->total_length_not_including_first_buffer = total_length;
686 // drop fragment header
687 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
688 ip6_header_t *ip = vlib_buffer_get_current (first_b);
689 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
690 ip6_ext_header_t *prev_hdr;
692 ip6_ext_header_find (vm, first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
696 prev_hdr->next_hdr = frag_hdr->next_hdr;
700 ip->protocol = frag_hdr->next_hdr;
702 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
704 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
705 goto free_buffers_and_return;
707 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
708 first_b->current_length - ip6_frag_hdr_offset -
709 sizeof (ip6_frag_hdr_t));
710 first_b->current_length -= sizeof (*frag_hdr);
712 clib_host_to_net_u16 (total_length + first_b->current_length -
714 if (!vlib_buffer_chain_linearize (vm, first_b))
716 rv = IP6_FULL_REASS_RC_NO_BUF;
717 goto free_buffers_and_return;
719 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
720 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
722 ip6_full_reass_add_trace (vm, node, rm, reass, reass->first_bi,
725 // following code does a hexdump of packet fragments to stdout ...
728 u32 bi = reass->first_bi;
732 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
733 s = format (s, "%u: %U\n", bi, format_hexdump,
734 vlib_buffer_get_current (b), b->current_length);
735 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
744 printf ("%.*s\n", vec_len (s), s);
753 *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
757 *next0 = reass->next_index;
759 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
760 ip6_full_reass_free (rm, rt, reass);
762 free_buffers_and_return:
763 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
764 vec_free (vec_drop_compress);
769 ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
770 ip6_full_reass_main_t * rm,
771 ip6_full_reass_per_thread_t * rt,
772 ip6_full_reass_t * reass,
773 u32 prev_range_bi, u32 new_next_bi)
776 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
777 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
778 if (~0 != prev_range_bi)
780 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
781 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
782 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
783 prev_vnb->ip.reass.next_range_bi = new_next_bi;
787 if (~0 != reass->first_bi)
789 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
791 reass->first_bi = new_next_bi;
793 reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
796 always_inline ip6_full_reass_rc_t
797 ip6_full_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
798 ip6_full_reass_main_t * rm,
799 ip6_full_reass_per_thread_t * rt,
800 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
801 u32 * error0, ip6_frag_hdr_t * frag_hdr,
802 bool is_custom_app, u32 * handoff_thread_idx)
805 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
806 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
809 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
810 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
813 fvnb->ip.reass.ip6_frag_hdr_offset =
814 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
815 ip6_header_t *fip = vlib_buffer_get_current (fb);
816 if (fb->current_length < sizeof (*fip) ||
817 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
818 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
820 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
823 u32 fragment_first = fvnb->ip.reass.fragment_first =
824 ip6_frag_hdr_offset_bytes (frag_hdr);
825 u32 fragment_length =
826 vlib_buffer_length_in_chain (vm, fb) -
827 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
828 u32 fragment_last = fvnb->ip.reass.fragment_last =
829 fragment_first + fragment_length - 1;
830 int more_fragments = ip6_frag_hdr_more (frag_hdr);
831 u32 candidate_range_bi = reass->first_bi;
832 u32 prev_range_bi = ~0;
833 fvnb->ip.reass.range_first = fragment_first;
834 fvnb->ip.reass.range_last = fragment_last;
835 fvnb->ip.reass.next_range_bi = ~0;
838 reass->last_packet_octet = fragment_last;
840 if (~0 == reass->first_bi)
842 // starting a new reassembly
843 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
845 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
847 reass->fragments_n = 1;
848 goto check_if_done_maybe;
850 reass->min_fragment_length =
851 clib_min (clib_net_to_host_u16 (fip->payload_length),
852 fvnb->ip.reass.estimated_mtu);
853 while (~0 != candidate_range_bi)
855 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
856 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
857 if (fragment_first > candidate_vnb->ip.reass.range_last)
859 // this fragments starts after candidate range
860 prev_range_bi = candidate_range_bi;
861 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
862 if (candidate_vnb->ip.reass.range_last < fragment_last &&
863 ~0 == candidate_range_bi)
865 // special case - this fragment falls beyond all known ranges
866 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass,
867 prev_range_bi, *bi0);
873 if (fragment_last < candidate_vnb->ip.reass.range_first)
875 // this fragment ends before candidate range without any overlap
876 ip6_full_reass_insert_range_in_chain (vm, rm, rt, reass,
877 prev_range_bi, *bi0);
880 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
881 fragment_last == candidate_vnb->ip.reass.range_last)
883 // duplicate fragment - ignore
887 // overlapping fragment - not allowed by RFC 8200
888 ip6_full_reass_drop_all (vm, node, rm, reass);
889 ip6_full_reass_free (rm, rt, reass);
890 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
892 ip6_full_reass_add_trace (vm, node, rm, reass, *bi0,
895 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
896 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
897 return IP6_FULL_REASS_RC_OK;
901 ++reass->fragments_n;
905 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
907 ip6_full_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, ~0);
910 if (~0 != reass->last_packet_octet &&
911 reass->data_len == reass->last_packet_octet + 1)
913 *handoff_thread_idx = reass->sendout_thread_index;
914 ip6_full_reass_rc_t rc =
915 ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
917 if (IP6_FULL_REASS_RC_OK == rc
918 && reass->memory_owner_thread_index != reass->sendout_thread_index)
920 return IP6_FULL_REASS_RC_HANDOFF;
929 if (reass->fragments_n > rm->max_reass_len)
931 return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
936 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
937 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
940 return IP6_FULL_REASS_RC_OK;
944 ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
946 ip6_frag_hdr_t * frag_hdr)
948 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
949 while (ip6_ext_hdr (tmp->next_hdr))
951 tmp = ip6_ext_next_header (tmp);
953 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
955 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
956 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
958 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
966 ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
967 vlib_node_runtime_t * node,
969 ip6_frag_hdr_t * frag_hdr)
971 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
972 ip6_header_t *ip = vlib_buffer_get_current (b);
973 int more_fragments = ip6_frag_hdr_more (frag_hdr);
974 u32 fragment_length =
975 vlib_buffer_length_in_chain (vm, b) -
976 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
977 if (more_fragments && 0 != fragment_length % 8)
979 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
980 ICMP6_parameter_problem_erroneous_header_field,
981 (u8 *) & ip->payload_length - (u8 *) ip);
988 ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
989 vlib_node_runtime_t * node,
991 ip6_frag_hdr_t * frag_hdr)
993 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
994 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
995 u32 fragment_length =
996 vlib_buffer_length_in_chain (vm, b) -
997 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
998 if (fragment_first + fragment_length > 65535)
1000 ip6_header_t *ip0 = vlib_buffer_get_current (b);
1001 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1002 ICMP6_parameter_problem_erroneous_header_field,
1003 (u8 *) & frag_hdr->fragment_offset_and_more
1011 ip6_full_reassembly_inline (vlib_main_t * vm,
1012 vlib_node_runtime_t * node,
1013 vlib_frame_t * frame, bool is_feature,
1016 u32 *from = vlib_frame_vector_args (frame);
1017 u32 n_left_from, n_left_to_next, *to_next, next_index;
1018 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1019 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
1020 clib_spinlock_lock (&rt->lock);
1022 n_left_from = frame->n_vectors;
1023 next_index = node->cached_next_index;
1024 while (n_left_from > 0)
1026 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1028 while (n_left_from > 0 && n_left_to_next > 0)
1032 u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1033 u32 error0 = IP6_ERROR_NONE;
1037 b0 = vlib_get_buffer (vm, bi0);
1039 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
1040 ip6_frag_hdr_t *frag_hdr = NULL;
1041 ip6_ext_header_t *prev_hdr;
1042 if (ip6_ext_hdr (ip0->protocol))
1045 ip6_ext_header_find (vm, b0, ip0,
1046 IP_PROTOCOL_IPV6_FRAGMENTATION,
1051 // this is a regular packet - no fragmentation
1052 next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
1055 if (0 == ip6_frag_hdr_offset (frag_hdr))
1057 // first fragment - verify upper-layer is present
1058 if (!ip6_full_reass_verify_upper_layer_present
1059 (node, b0, frag_hdr))
1061 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1065 if (!ip6_full_reass_verify_fragment_multiple_8
1066 (vm, node, b0, frag_hdr)
1067 || !ip6_full_reass_verify_packet_size_lt_64k (vm, node, b0,
1070 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1073 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1074 (u8 *) frag_hdr - (u8 *) ip0;
1076 ip6_full_reass_kv_t kv;
1079 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1080 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1081 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1082 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1084 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1085 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
1086 (u64) frag_hdr->identification;
1087 kv.k.as_u64[5] = ip0->protocol;
1089 ip6_full_reass_t *reass =
1090 ip6_full_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
1095 const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1096 if (0 == fragment_first)
1098 reass->sendout_thread_index = vm->thread_index;
1101 if (PREDICT_FALSE (do_handoff))
1103 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1104 vnet_buffer (b0)->ip.reass.owner_thread_index =
1105 kv.v.memory_owner_thread_index;
1109 u32 handoff_thread_idx;
1110 switch (ip6_full_reass_update
1111 (vm, node, rm, rt, reass, &bi0, &next0, &error0,
1112 frag_hdr, is_custom_app, &handoff_thread_idx))
1114 case IP6_FULL_REASS_RC_OK:
1115 /* nothing to do here */
1117 case IP6_FULL_REASS_RC_HANDOFF:
1118 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1119 b0 = vlib_get_buffer (vm, bi0);
1120 vnet_buffer (b0)->ip.reass.owner_thread_index =
1123 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
1124 vlib_node_increment_counter (vm, node->node_index,
1125 IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1127 ip6_full_reass_drop_all (vm, node, rm, reass);
1128 ip6_full_reass_free (rm, rt, reass);
1131 case IP6_FULL_REASS_RC_NO_BUF:
1132 vlib_node_increment_counter (vm, node->node_index,
1133 IP6_ERROR_REASS_NO_BUF, 1);
1134 ip6_full_reass_drop_all (vm, node, rm, reass);
1135 ip6_full_reass_free (rm, rt, reass);
1138 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
1139 vlib_node_increment_counter (vm, node->node_index,
1140 IP6_ERROR_REASS_INTERNAL_ERROR,
1142 ip6_full_reass_drop_all (vm, node, rm, reass);
1143 ip6_full_reass_free (rm, rt, reass);
1152 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1156 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1157 next0 = fvnb->ip.reass.error_next_index;
1159 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1162 b0->error = node->errors[error0];
1169 n_left_to_next -= 1;
1170 if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
1172 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1174 ip6_full_reass_add_trace (vm, node, rm, NULL, bi0,
1176 vnet_buffer (b0)->ip.
1177 reass.owner_thread_index);
1180 else if (is_feature && IP6_ERROR_NONE == error0)
1182 b0 = vlib_get_buffer (vm, bi0);
1183 vnet_feature_next (&next0, b0);
1185 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1186 n_left_to_next, bi0, next0);
1191 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1192 to_next[0] = icmp_bi;
1194 n_left_to_next -= 1;
1195 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1196 n_left_to_next, icmp_bi,
1204 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1207 clib_spinlock_unlock (&rt->lock);
1208 return frame->n_vectors;
1211 static char *ip6_full_reassembly_error_strings[] = {
1212 #define _(sym, string) string,
1217 VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1218 vlib_node_runtime_t * node,
1219 vlib_frame_t * frame)
1221 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */ ,
1222 false /* is_custom_app */ );
1226 VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1227 .name = "ip6-full-reassembly",
1228 .vector_size = sizeof (u32),
1229 .format_trace = format_ip6_full_reass_trace,
1230 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1231 .error_strings = ip6_full_reassembly_error_strings,
1232 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1235 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1236 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1237 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1238 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
1243 VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1244 vlib_node_runtime_t * node,
1245 vlib_frame_t * frame)
1247 return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */ ,
1248 false /* is_custom_app */ );
1252 VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1253 .name = "ip6-full-reassembly-feature",
1254 .vector_size = sizeof (u32),
1255 .format_trace = format_ip6_full_reass_trace,
1256 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1257 .error_strings = ip6_full_reassembly_error_strings,
1258 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1261 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1262 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1263 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1264 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
1270 VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
1271 .arc_name = "ip6-unicast",
1272 .node_name = "ip6-full-reassembly-feature",
1273 .runs_before = VNET_FEATURES ("ip6-lookup",
1274 "ipsec6-input-feature"),
1279 #ifndef CLIB_MARCH_VARIANT
1281 ip6_full_reass_get_nbuckets ()
1283 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1287 nbuckets = (u32) (rm->max_reass_n / IP6_FULL_REASS_HT_LOAD_FACTOR);
1289 for (i = 0; i < 31; i++)
1290 if ((1 << i) >= nbuckets)
1296 #endif /* CLIB_MARCH_VARIANT */
1300 IP6_EVENT_CONFIG_CHANGED = 1,
1301 } ip6_full_reass_event_t;
1303 #ifndef CLIB_MARCH_VARIANT
1307 clib_bihash_48_8_t *new_hash;
1308 } ip6_rehash_cb_ctx;
1311 ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1313 ip6_rehash_cb_ctx *ctx = _ctx;
1314 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1321 ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1322 u32 max_reassembly_length,
1323 u32 expire_walk_interval_ms)
1325 ip6_full_reass_main.timeout_ms = timeout_ms;
1326 ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1327 ip6_full_reass_main.max_reass_n = max_reassemblies;
1328 ip6_full_reass_main.max_reass_len = max_reassembly_length;
1329 ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1333 ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1334 u32 max_reassembly_length, u32 expire_walk_interval_ms)
1336 u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1337 ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1338 max_reassembly_length, expire_walk_interval_ms);
1339 vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1340 ip6_full_reass_main.ip6_full_reass_expire_node_idx,
1341 IP6_EVENT_CONFIG_CHANGED, 0);
1342 u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1343 if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
1345 clib_bihash_48_8_t new_hash;
1346 clib_memset (&new_hash, 0, sizeof (new_hash));
1347 ip6_rehash_cb_ctx ctx;
1349 ctx.new_hash = &new_hash;
1350 clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
1351 new_nbuckets * 1024);
1352 clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
1353 ip6_rehash_cb, &ctx);
1356 clib_bihash_free_48_8 (&new_hash);
1361 clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1362 clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1363 sizeof (ip6_full_reass_main.hash));
1364 clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
1371 ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1372 u32 * max_reassembly_length,
1373 u32 * expire_walk_interval_ms)
1375 *timeout_ms = ip6_full_reass_main.timeout_ms;
1376 *max_reassemblies = ip6_full_reass_main.max_reass_n;
1377 *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1378 *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
1382 static clib_error_t *
1383 ip6_full_reass_init_function (vlib_main_t * vm)
1385 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1386 clib_error_t *error = 0;
1392 vec_validate (rm->per_thread_data, vlib_num_workers ());
1393 ip6_full_reass_per_thread_t *rt;
1394 vec_foreach (rt, rm->per_thread_data)
1396 clib_spinlock_init (&rt->lock);
1397 pool_alloc (rt->pool, rm->max_reass_n);
1400 node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
1402 rm->ip6_full_reass_expire_node_idx = node->index;
1404 ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1405 IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1406 IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1407 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1409 nbuckets = ip6_full_reass_get_nbuckets ();
1410 clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1413 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
1415 rm->ip6_drop_idx = node->index;
1416 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1418 rm->ip6_icmp_error_idx = node->index;
1420 if ((error = vlib_call_init_function (vm, ip_main_init)))
1422 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1423 ip6_full_reass_node.index);
1425 rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
1426 rm->fq_feature_index =
1427 vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
1432 VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
1433 #endif /* CLIB_MARCH_VARIANT */
1436 ip6_full_reass_walk_expired (vlib_main_t * vm,
1437 vlib_node_runtime_t * node, vlib_frame_t * f)
1439 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1440 uword event_type, *event_data = 0;
1444 vlib_process_wait_for_event_or_clock (vm,
1445 (f64) rm->expire_walk_interval_ms
1446 / (f64) MSEC_PER_SEC);
1447 event_type = vlib_process_get_events (vm, &event_data);
1451 case ~0: /* no events => timeout */
1452 /* nothing to do here */
1454 case IP6_EVENT_CONFIG_CHANGED:
1457 clib_warning ("BUG: event type 0x%wx", event_type);
1460 f64 now = vlib_time_now (vm);
1462 ip6_full_reass_t *reass;
1463 int *pool_indexes_to_free = NULL;
1465 uword thread_index = 0;
1467 const uword nthreads = vlib_num_workers () + 1;
1468 u32 *vec_icmp_bi = NULL;
1469 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1471 ip6_full_reass_per_thread_t *rt =
1472 &rm->per_thread_data[thread_index];
1473 clib_spinlock_lock (&rt->lock);
1475 vec_reset_length (pool_indexes_to_free);
1477 pool_foreach_index (index, rt->pool, ({
1478 reass = pool_elt_at_index (rt->pool, index);
1479 if (now > reass->last_heard + rm->timeout)
1481 vec_add1 (pool_indexes_to_free, index);
1487 vec_foreach (i, pool_indexes_to_free)
1489 ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1491 ip6_full_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
1493 vec_add1 (vec_icmp_bi, icmp_bi);
1495 ip6_full_reass_free (rm, rt, reass);
1499 clib_spinlock_unlock (&rt->lock);
1502 while (vec_len (vec_icmp_bi) > 0)
1505 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1506 u32 *to_next = vlib_frame_vector_args (f);
1507 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
1508 int trace_frame = 0;
1509 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1511 u32 bi = vec_pop (vec_icmp_bi);
1512 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1513 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1515 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1519 n_left_to_next -= 1;
1521 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
1522 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1525 vec_free (pool_indexes_to_free);
1526 vec_free (vec_icmp_bi);
1529 _vec_len (event_data) = 0;
1537 VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
1538 .function = ip6_full_reass_walk_expired,
1539 .format_trace = format_ip6_full_reass_trace,
1540 .type = VLIB_NODE_TYPE_PROCESS,
1541 .name = "ip6-full-reassembly-expire-walk",
1543 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1544 .error_strings = ip6_full_reassembly_error_strings,
1550 format_ip6_full_reass_key (u8 * s, va_list * args)
1552 ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
1553 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1554 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1555 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1560 format_ip6_full_reass (u8 * s, va_list * args)
1562 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1563 ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
1565 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
1566 "last_packet_octet: %u, trace_op_counter: %u\n",
1567 reass->id, format_ip6_full_reass_key, &reass->key,
1568 reass->first_bi, reass->data_len, reass->last_packet_octet,
1569 reass->trace_op_counter);
1570 u32 bi = reass->first_bi;
1574 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1575 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1576 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1577 "fragment[%u, %u]\n",
1578 counter, vnb->ip.reass.range_first,
1579 vnb->ip.reass.range_last, bi,
1580 ip6_full_reass_buffer_get_data_offset (b),
1581 ip6_full_reass_buffer_get_data_len (b),
1582 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1583 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1585 bi = b->next_buffer;
1595 static clib_error_t *
1596 show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1597 CLIB_UNUSED (vlib_cli_command_t * lmd))
1599 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1601 vlib_cli_output (vm, "---------------------");
1602 vlib_cli_output (vm, "IP6 reassembly status");
1603 vlib_cli_output (vm, "---------------------");
1604 bool details = false;
1605 if (unformat (input, "details"))
1610 u32 sum_reass_n = 0;
1611 u64 sum_buffers_n = 0;
1612 ip6_full_reass_t *reass;
1614 const uword nthreads = vlib_num_workers () + 1;
1615 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1617 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1618 clib_spinlock_lock (&rt->lock);
1622 pool_foreach (reass, rt->pool, {
1623 vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
1627 sum_reass_n += rt->reass_n;
1628 clib_spinlock_unlock (&rt->lock);
1630 vlib_cli_output (vm, "---------------------");
1631 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1632 (long unsigned) sum_reass_n);
1633 vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1634 "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
1635 vlib_cli_output (vm, "Buffers in use: %lu\n",
1636 (long unsigned) sum_buffers_n);
1641 VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1642 .path = "show ip6-full-reassembly",
1643 .short_help = "show ip6-full-reassembly [details]",
1644 .function = show_ip6_full_reass,
1648 #ifndef CLIB_MARCH_VARIANT
1650 ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1652 return vnet_feature_enable_disable ("ip6-unicast",
1653 "ip6-full-reassembly-feature",
1654 sw_if_index, enable_disable, 0, 0);
1656 #endif /* CLIB_MARCH_VARIANT */
1658 #define foreach_ip6_full_reassembly_handoff_error \
1659 _(CONGESTION_DROP, "congestion drop")
1664 #define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1665 foreach_ip6_full_reassembly_handoff_error
1667 IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1668 } ip6_full_reassembly_handoff_error_t;
1670 static char *ip6_full_reassembly_handoff_error_strings[] = {
1671 #define _(sym,string) string,
1672 foreach_ip6_full_reassembly_handoff_error
1678 u32 next_worker_index;
1679 } ip6_full_reassembly_handoff_trace_t;
1682 format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
1684 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1685 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1686 ip6_full_reassembly_handoff_trace_t *t =
1687 va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
1690 format (s, "ip6-full-reassembly-handoff: next-worker %d",
1691 t->next_worker_index);
1697 ip6_full_reassembly_handoff_inline (vlib_main_t * vm,
1698 vlib_node_runtime_t * node,
1699 vlib_frame_t * frame, bool is_feature)
1701 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1703 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1704 u32 n_enq, n_left_from, *from;
1705 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1708 from = vlib_frame_vector_args (frame);
1709 n_left_from = frame->n_vectors;
1710 vlib_get_buffers (vm, from, bufs, n_left_from);
1713 ti = thread_indices;
1715 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1717 while (n_left_from > 0)
1719 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1722 ((node->flags & VLIB_NODE_FLAG_TRACE)
1723 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1725 ip6_full_reassembly_handoff_trace_t *t =
1726 vlib_add_trace (vm, node, b[0], sizeof (*t));
1727 t->next_worker_index = ti[0];
1735 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1736 frame->n_vectors, 1);
1738 if (n_enq < frame->n_vectors)
1739 vlib_node_increment_counter (vm, node->node_index,
1740 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1741 frame->n_vectors - n_enq);
1742 return frame->n_vectors;
1745 VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
1746 vlib_node_runtime_t * node,
1747 vlib_frame_t * frame)
1749 return ip6_full_reassembly_handoff_inline (vm, node, frame,
1750 false /* is_feature */ );
1754 VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
1755 .name = "ip6-full-reassembly-handoff",
1756 .vector_size = sizeof (u32),
1757 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1758 .error_strings = ip6_full_reassembly_handoff_error_strings,
1759 .format_trace = format_ip6_full_reassembly_handoff_trace,
1769 VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
1770 vlib_node_runtime_t * node, vlib_frame_t * frame)
1772 return ip6_full_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1777 VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
1778 .name = "ip6-full-reass-feature-hoff",
1779 .vector_size = sizeof (u32),
1780 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1781 .error_strings = ip6_full_reassembly_handoff_error_strings,
1782 .format_trace = format_ip6_full_reassembly_handoff_trace,
1793 * fd.io coding-style-patch-verification: ON
1796 * eval: (c-set-style "gnu")