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_t;
61 } ip6_full_reass_key_t;
68 u32 memory_owner_thread_index;
71 } ip6_full_reass_val_t;
77 ip6_full_reass_key_t k;
78 ip6_full_reass_val_t v;
80 clib_bihash_kv_48_8_t kv;
81 } ip6_full_reass_kv_t;
85 ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
87 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
88 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
92 ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
94 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
95 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
96 (vnb->ip.reass.fragment_first +
97 ip6_full_reass_buffer_get_data_offset (b)) + 1;
103 ip6_full_reass_key_t key;
104 // time when last packet was received
106 // internal id of this reassembly
108 // buffer index of first buffer in this reassembly context
110 // last octet of packet, ~0 until fragment without more_fragments arrives
111 u32 last_packet_octet;
112 // length of data collected so far
114 // trace operation counter
115 u32 trace_op_counter;
116 // next index - used by custom apps (~0 if not set)
118 // error next index - used by custom apps (~0 if not set)
119 u32 error_next_index;
120 // minimum fragment length for this reassembly - used to estimate MTU
121 u16 min_fragment_length;
122 // number of fragments for this reassembly
124 // thread owning memory for this context (whose pool contains this ctx)
125 u32 memory_owner_thread_index;
126 // thread which received fragment with offset 0 and which sends out the
127 // completed reassembly
128 u32 sendout_thread_index;
133 ip6_full_reass_t *pool;
136 clib_spinlock_t lock;
137 } ip6_full_reass_per_thread_t;
144 u32 expire_walk_interval_ms;
145 // maximum number of fragments in one reassembly
147 // maximum number of reassemblies
151 clib_bihash_48_8_t hash;
154 ip6_full_reass_per_thread_t *per_thread_data;
157 vlib_main_t *vlib_main;
159 // node index of ip6-drop node
161 u32 ip6_icmp_error_idx;
162 u32 ip6_full_reass_expire_node_idx;
164 /** Worker handoff */
166 u32 fq_feature_index;
168 // reference count for enabling/disabling feature - per interface
169 u32 *feature_use_refcount_per_intf;
170 } ip6_full_reass_main_t;
172 extern ip6_full_reass_main_t ip6_full_reass_main;
174 #ifndef CLIB_MARCH_VARIANT
175 ip6_full_reass_main_t ip6_full_reass_main;
176 #endif /* CLIB_MARCH_VARIANT */
180 IP6_FULL_REASSEMBLY_NEXT_INPUT,
181 IP6_FULL_REASSEMBLY_NEXT_DROP,
182 IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
183 IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
184 IP6_FULL_REASSEMBLY_N_NEXT,
185 } ip6_full_reass_next_t;
191 ICMP_ERROR_RT_EXCEEDED,
192 ICMP_ERROR_FL_TOO_BIG,
193 ICMP_ERROR_FL_NOT_MULT_8,
196 } ip6_full_reass_trace_operation_e;
206 } ip6_full_reass_range_trace_t;
210 ip6_full_reass_trace_operation_e action;
212 ip6_full_reass_range_trace_t trace_range;
219 bool is_after_handoff;
220 ip6_header_t ip6_header;
221 ip6_frag_hdr_t ip6_frag_header;
222 } ip6_full_reass_trace_t;
225 ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
226 ip6_full_reass_range_trace_t * trace)
228 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
229 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
230 trace->range_first = vnb->ip.reass.range_first;
231 trace->range_last = vnb->ip.reass.range_last;
232 trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
233 trace->data_len = ip6_full_reass_buffer_get_data_len (b);
234 trace->range_bi = bi;
238 format_ip6_full_reass_range_trace (u8 * s, va_list * args)
240 ip6_full_reass_range_trace_t *trace =
241 va_arg (*args, ip6_full_reass_range_trace_t *);
243 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
244 trace->range_last, trace->data_offset, trace->data_len,
250 format_ip6_full_reass_trace (u8 * s, va_list * args)
252 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
253 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
254 ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
256 if (~0 != t->reass_id)
258 if (t->is_after_handoff)
261 format (s, "%U\n", format_ip6_header, &t->ip6_header,
262 sizeof (t->ip6_header));
264 format (s, " %U\n", format_ip6_frag_hdr, &t->ip6_frag_header,
265 sizeof (t->ip6_frag_header));
269 format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
270 t->reass_id, t->op_id);
271 indent = format_get_indent (s);
272 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
273 t->trace_range.first_bi, t->total_data_len,
274 t->fragment_first, t->fragment_last);
279 s = format (s, "\n%Unew %U", format_white_space, indent,
280 format_ip6_full_reass_range_trace, &t->trace_range);
283 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
284 format_ip6_full_reass_range_trace, &t->trace_range);
286 case ICMP_ERROR_FL_TOO_BIG:
287 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
288 format_white_space, indent,
289 format_ip6_full_reass_range_trace, &t->trace_range);
291 case ICMP_ERROR_FL_NOT_MULT_8:
292 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
293 format_white_space, indent,
294 format_ip6_full_reass_range_trace, &t->trace_range);
296 case ICMP_ERROR_RT_EXCEEDED:
297 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
298 format_white_space, indent);
301 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
305 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
313 ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
314 ip6_full_reass_t * reass, u32 bi,
315 ip6_frag_hdr_t * ip6_frag_header,
316 ip6_full_reass_trace_operation_e action,
319 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
320 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
321 bool is_after_handoff = false;
322 if (pool_is_free_index
323 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
325 // this buffer's trace is gone
326 b->flags &= ~VLIB_BUFFER_IS_TRACED;
329 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
331 is_after_handoff = true;
333 ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
334 t->is_after_handoff = is_after_handoff;
335 if (t->is_after_handoff)
337 clib_memcpy (&t->ip6_header, vlib_buffer_get_current (b),
338 clib_min (sizeof (t->ip6_header), b->current_length));
341 clib_memcpy (&t->ip6_frag_header, ip6_frag_header,
342 sizeof (t->ip6_frag_header));
346 clib_memset (&t->ip6_frag_header, 0, sizeof (t->ip6_frag_header));
351 t->reass_id = reass->id;
352 t->op_id = reass->trace_op_counter;
353 t->trace_range.first_bi = reass->first_bi;
354 t->total_data_len = reass->data_len;
355 ++reass->trace_op_counter;
362 t->thread_id = vm->thread_index;
363 t->thread_id_to = thread_id_to;
364 ip6_full_reass_trace_details (vm, bi, &t->trace_range);
365 t->fragment_first = vnb->ip.reass.fragment_first;
366 t->fragment_last = vnb->ip.reass.fragment_last;
369 s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
370 printf ("%.*s\n", vec_len (s), s);
372 vec_reset_length (s);
377 ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
378 ip6_full_reass_t * reass)
380 pool_put (rt->pool, reass);
385 ip6_full_reass_free (ip6_full_reass_main_t * rm,
386 ip6_full_reass_per_thread_t * rt,
387 ip6_full_reass_t * reass)
389 clib_bihash_kv_48_8_t kv;
390 kv.key[0] = reass->key.as_u64[0];
391 kv.key[1] = reass->key.as_u64[1];
392 kv.key[2] = reass->key.as_u64[2];
393 kv.key[3] = reass->key.as_u64[3];
394 kv.key[4] = reass->key.as_u64[4];
395 kv.key[5] = reass->key.as_u64[5];
396 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
397 ip6_full_reass_free_ctx (rt, reass);
401 ip6_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
402 ip6_full_reass_t *reass)
404 u32 range_bi = reass->first_bi;
405 vlib_buffer_t *range_b;
406 vnet_buffer_opaque_t *range_vnb;
408 while (~0 != range_bi)
410 range_b = vlib_get_buffer (vm, range_bi);
411 range_vnb = vnet_buffer (range_b);
415 vec_add1 (to_free, bi);
416 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
417 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
420 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
427 range_bi = range_vnb->ip.reass.next_range_bi;
429 /* send to next_error_index */
430 if (~0 != reass->error_next_index)
432 u32 n_left_to_next, *to_next, next_index;
434 next_index = reass->error_next_index;
437 while (vec_len (to_free) > 0)
439 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
441 while (vec_len (to_free) > 0 && n_left_to_next > 0)
443 bi = vec_pop (to_free);
452 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
457 vlib_buffer_free (vm, to_free, vec_len (to_free));
463 ip6_full_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
464 ip6_full_reass_t * reass, u32 * icmp_bi)
466 if (~0 == reass->first_bi)
470 if (~0 == reass->next_index) // custom apps don't want icmp
472 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
473 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
475 *icmp_bi = reass->first_bi;
476 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
478 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
479 ICMP_ERROR_RT_EXCEEDED, ~0);
481 // fragment with offset zero received - send icmp message back
482 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
484 // separate first buffer from chain and steer it towards icmp node
485 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
486 reass->first_bi = b->next_buffer;
490 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
492 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
493 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
497 ip6_full_reass_drop_all (vm, node, reass);
500 always_inline ip6_full_reass_t *
501 ip6_full_reass_find_or_create (vlib_main_t *vm, vlib_node_runtime_t *node,
502 ip6_full_reass_main_t *rm,
503 ip6_full_reass_per_thread_t *rt,
504 ip6_full_reass_kv_t *kv, u32 *icmp_bi,
505 u8 *do_handoff, int skip_bihash)
507 ip6_full_reass_t *reass;
513 now = vlib_time_now (vm);
515 if (!skip_bihash && !clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
517 if (vm->thread_index != kv->v.memory_owner_thread_index)
524 pool_elt_at_index (rm->per_thread_data
525 [kv->v.memory_owner_thread_index].pool,
528 if (now > reass->last_heard + rm->timeout)
530 ip6_full_reass_on_timeout (vm, node, reass, icmp_bi);
531 ip6_full_reass_free (rm, rt, reass);
538 reass->last_heard = now;
542 if (rt->reass_n >= rm->max_reass_n)
549 pool_get (rt->pool, reass);
550 clib_memset (reass, 0, sizeof (*reass));
551 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
553 reass->first_bi = ~0;
554 reass->last_packet_octet = ~0;
556 reass->next_index = ~0;
557 reass->error_next_index = ~0;
561 kv->v.reass_index = (reass - rt->pool);
562 kv->v.memory_owner_thread_index = vm->thread_index;
563 reass->last_heard = now;
567 reass->key.as_u64[0] = kv->kv.key[0];
568 reass->key.as_u64[1] = kv->kv.key[1];
569 reass->key.as_u64[2] = kv->kv.key[2];
570 reass->key.as_u64[3] = kv->kv.key[3];
571 reass->key.as_u64[4] = kv->kv.key[4];
572 reass->key.as_u64[5] = kv->kv.key[5];
574 int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
577 ip6_full_reass_free (rm, rt, reass);
579 // if other worker created a context already work with the other copy
586 reass->key.as_u64[0] = ~0;
587 reass->key.as_u64[1] = ~0;
588 reass->key.as_u64[2] = ~0;
589 reass->key.as_u64[3] = ~0;
590 reass->key.as_u64[4] = ~0;
591 reass->key.as_u64[5] = ~0;
597 always_inline ip6_full_reass_rc_t
598 ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
599 ip6_full_reass_main_t * rm,
600 ip6_full_reass_per_thread_t * rt,
601 ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
602 u32 * error0, bool is_custom_app)
604 *bi0 = reass->first_bi;
605 *error0 = IP6_ERROR_NONE;
606 ip6_frag_hdr_t *frag_hdr;
607 vlib_buffer_t *last_b = NULL;
608 u32 sub_chain_bi = reass->first_bi;
609 u32 total_length = 0;
612 u32 *vec_drop_compress = NULL;
613 ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
616 u32 tmp_bi = sub_chain_bi;
617 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
618 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
619 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
620 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
622 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
623 goto free_buffers_and_return;
626 u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
627 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
628 sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
630 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
631 if (tmp_bi == reass->first_bi)
633 /* first buffer - keep ip6 header */
634 if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
636 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
637 goto free_buffers_and_return;
640 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
641 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
643 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
645 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
646 goto free_buffers_and_return;
650 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
656 if (trim_front > tmp->current_length)
658 /* drop whole buffer */
659 vec_add1 (vec_drop_compress, tmp_bi);
660 trim_front -= tmp->current_length;
661 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
663 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
664 goto free_buffers_and_return;
666 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
667 tmp_bi = tmp->next_buffer;
668 tmp = vlib_get_buffer (vm, tmp_bi);
673 vlib_buffer_advance (tmp, trim_front);
681 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
682 last_b->next_buffer = tmp_bi;
685 if (keep_data <= tmp->current_length)
687 tmp->current_length = keep_data;
692 keep_data -= tmp->current_length;
693 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
695 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
696 goto free_buffers_and_return;
699 total_length += tmp->current_length;
703 vec_add1 (vec_drop_compress, tmp_bi);
704 if (reass->first_bi == tmp_bi)
706 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
707 goto free_buffers_and_return;
711 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
713 tmp_bi = tmp->next_buffer;
714 tmp = vlib_get_buffer (vm, tmp->next_buffer);
722 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
725 while (~0 != sub_chain_bi);
729 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
730 goto free_buffers_and_return;
732 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
733 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
734 if (total_length < first_b->current_length)
736 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
737 goto free_buffers_and_return;
739 total_length -= first_b->current_length;
740 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
741 first_b->total_length_not_including_first_buffer = total_length;
742 // drop fragment header
743 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
744 ip6_header_t *ip = vlib_buffer_get_current (first_b);
745 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
746 ip6_ext_hdr_chain_t hdr_chain;
747 ip6_ext_header_t *prev_hdr = 0;
748 int res = ip6_ext_header_walk (first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
751 (hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION))
753 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
754 goto free_buffers_and_return;
756 frag_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res].offset);
759 prev_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res - 1].offset);
760 prev_hdr->next_hdr = frag_hdr->next_hdr;
764 ip->protocol = frag_hdr->next_hdr;
766 if (hdr_chain.eh[res].offset != ip6_frag_hdr_offset)
768 rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
769 goto free_buffers_and_return;
771 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
772 first_b->current_length - ip6_frag_hdr_offset -
773 sizeof (ip6_frag_hdr_t));
774 first_b->current_length -= sizeof (*frag_hdr);
776 clib_host_to_net_u16 (total_length + first_b->current_length -
778 if (!vlib_buffer_chain_linearize (vm, first_b))
780 rv = IP6_FULL_REASS_RC_NO_BUF;
781 goto free_buffers_and_return;
783 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
784 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
786 ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
789 // following code does a hexdump of packet fragments to stdout ...
792 u32 bi = reass->first_bi;
796 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
797 s = format (s, "%u: %U\n", bi, format_hexdump,
798 vlib_buffer_get_current (b), b->current_length);
799 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
808 printf ("%.*s\n", vec_len (s), s);
817 *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
821 *next0 = reass->next_index;
823 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
824 ip6_full_reass_free (rm, rt, reass);
826 free_buffers_and_return:
827 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
828 vec_free (vec_drop_compress);
833 ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
834 ip6_full_reass_t * reass,
835 u32 prev_range_bi, u32 new_next_bi)
838 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
839 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
840 if (~0 != prev_range_bi)
842 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
843 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
844 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
845 prev_vnb->ip.reass.next_range_bi = new_next_bi;
849 if (~0 != reass->first_bi)
851 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
853 reass->first_bi = new_next_bi;
855 reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
858 always_inline ip6_full_reass_rc_t
859 ip6_full_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
860 ip6_full_reass_main_t *rm,
861 ip6_full_reass_per_thread_t *rt,
862 ip6_full_reass_t *reass, u32 *bi0, u32 *next0,
863 u32 *error0, ip6_frag_hdr_t *frag_hdr,
864 bool is_custom_app, u32 *handoff_thread_idx,
868 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
869 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
872 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
873 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
876 fvnb->ip.reass.ip6_frag_hdr_offset =
877 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
878 ip6_header_t *fip = vlib_buffer_get_current (fb);
879 if (fb->current_length < sizeof (*fip) ||
880 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
881 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
883 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
886 u32 fragment_first = fvnb->ip.reass.fragment_first =
887 ip6_frag_hdr_offset_bytes (frag_hdr);
888 u32 fragment_length =
889 vlib_buffer_length_in_chain (vm, fb) -
890 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
891 u32 fragment_last = fvnb->ip.reass.fragment_last =
892 fragment_first + fragment_length - 1;
893 int more_fragments = ip6_frag_hdr_more (frag_hdr);
894 u32 candidate_range_bi = reass->first_bi;
895 u32 prev_range_bi = ~0;
896 fvnb->ip.reass.range_first = fragment_first;
897 fvnb->ip.reass.range_last = fragment_last;
898 fvnb->ip.reass.next_range_bi = ~0;
901 reass->last_packet_octet = fragment_last;
903 if (~0 == reass->first_bi)
905 // starting a new reassembly
906 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
907 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
909 reass->fragments_n = 1;
910 goto check_if_done_maybe;
912 reass->min_fragment_length =
913 clib_min (clib_net_to_host_u16 (fip->payload_length),
914 fvnb->ip.reass.estimated_mtu);
915 while (~0 != candidate_range_bi)
917 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
918 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
919 if (fragment_first > candidate_vnb->ip.reass.range_last)
921 // this fragments starts after candidate range
922 prev_range_bi = candidate_range_bi;
923 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
924 if (candidate_vnb->ip.reass.range_last < fragment_last &&
925 ~0 == candidate_range_bi)
927 // special case - this fragment falls beyond all known ranges
928 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
935 if (fragment_last < candidate_vnb->ip.reass.range_first)
937 // this fragment ends before candidate range without any overlap
938 ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
942 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
943 fragment_last == candidate_vnb->ip.reass.range_last)
945 // duplicate fragment - ignore
949 // overlapping fragment - not allowed by RFC 8200
950 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
952 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr,
955 ip6_full_reass_drop_all (vm, node, reass);
956 ip6_full_reass_free (rm, rt, reass);
957 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
958 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
959 return IP6_FULL_REASS_RC_OK;
963 ++reass->fragments_n;
967 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
969 ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr, RANGE_NEW,
973 else if (skip_bihash)
975 // if this reassembly is not in bihash, then the packet must have been
977 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
979 if (~0 != reass->last_packet_octet &&
980 reass->data_len == reass->last_packet_octet + 1)
982 *handoff_thread_idx = reass->sendout_thread_index;
984 reass->memory_owner_thread_index != reass->sendout_thread_index;
985 ip6_full_reass_rc_t rc =
986 ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
988 if (IP6_FULL_REASS_RC_OK == rc && handoff)
990 return IP6_FULL_REASS_RC_HANDOFF;
998 // if this reassembly is not in bihash, it should've been an atomic
999 // fragment and thus finalized
1000 return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1005 if (reass->fragments_n > rm->max_reass_len)
1007 return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
1012 *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1013 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
1016 return IP6_FULL_REASS_RC_OK;
1020 ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
1022 ip6_ext_hdr_chain_t *hc)
1024 int nh = hc->eh[hc->length - 1].protocol;
1025 /* Checking to see if it's a terminating header */
1026 if (ip6_ext_hdr (nh))
1028 icmp6_error_set_vnet_buffer (
1029 b, ICMP6_parameter_problem,
1030 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
1031 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
1038 ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
1040 ip6_frag_hdr_t * frag_hdr)
1042 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1043 ip6_header_t *ip = vlib_buffer_get_current (b);
1044 int more_fragments = ip6_frag_hdr_more (frag_hdr);
1045 u32 fragment_length =
1046 vlib_buffer_length_in_chain (vm, b) -
1047 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1048 if (more_fragments && 0 != fragment_length % 8)
1050 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1051 ICMP6_parameter_problem_erroneous_header_field,
1052 (u8 *) & ip->payload_length - (u8 *) ip);
1059 ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
1061 ip6_frag_hdr_t * frag_hdr)
1063 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1064 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
1065 u32 fragment_length =
1066 vlib_buffer_length_in_chain (vm, b) -
1067 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1068 if (fragment_first + fragment_length > 65535)
1070 ip6_header_t *ip0 = vlib_buffer_get_current (b);
1071 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1072 ICMP6_parameter_problem_erroneous_header_field,
1073 (u8 *) & frag_hdr->fragment_offset_and_more
1081 ip6_full_reassembly_inline (vlib_main_t * vm,
1082 vlib_node_runtime_t * node,
1083 vlib_frame_t * frame, bool is_feature,
1086 u32 *from = vlib_frame_vector_args (frame);
1087 u32 n_left_from, n_left_to_next, *to_next, next_index;
1088 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1089 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
1090 clib_spinlock_lock (&rt->lock);
1092 n_left_from = frame->n_vectors;
1093 next_index = node->cached_next_index;
1094 while (n_left_from > 0)
1096 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1098 while (n_left_from > 0 && n_left_to_next > 0)
1102 u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1103 u32 error0 = IP6_ERROR_NONE;
1107 b0 = vlib_get_buffer (vm, bi0);
1109 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
1110 ip6_frag_hdr_t *frag_hdr;
1111 ip6_ext_hdr_chain_t hdr_chain;
1112 int res = ip6_ext_header_walk (
1113 b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
1115 hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION)
1117 // this is a mangled packet - no fragmentation
1118 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1122 ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
1123 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1124 hdr_chain.eh[res].offset;
1126 if (0 == ip6_frag_hdr_offset (frag_hdr))
1128 // first fragment - verify upper-layer is present
1129 if (!ip6_full_reass_verify_upper_layer_present (node, b0,
1132 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1136 if (!ip6_full_reass_verify_fragment_multiple_8 (vm, b0, frag_hdr) ||
1137 !ip6_full_reass_verify_packet_size_lt_64k (vm, b0, frag_hdr))
1139 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1143 int skip_bihash = 0;
1144 ip6_full_reass_kv_t kv;
1147 if (0 == ip6_frag_hdr_offset (frag_hdr) &&
1148 !ip6_frag_hdr_more (frag_hdr))
1150 // this is atomic fragment and needs to be processed separately
1155 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1156 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1157 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1158 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1160 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1161 vnet_buffer (b0)->sw_if_index[VLIB_RX]))
1163 (u64) frag_hdr->identification;
1164 kv.k.as_u64[5] = ip0->protocol;
1167 ip6_full_reass_t *reass = ip6_full_reass_find_or_create (
1168 vm, node, rm, rt, &kv, &icmp_bi, &do_handoff, skip_bihash);
1172 const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1173 if (0 == fragment_first)
1175 reass->sendout_thread_index = vm->thread_index;
1178 if (PREDICT_FALSE (do_handoff))
1180 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1181 vnet_buffer (b0)->ip.reass.owner_thread_index =
1182 kv.v.memory_owner_thread_index;
1186 u32 handoff_thread_idx;
1188 switch (ip6_full_reass_update (
1189 vm, node, rm, rt, reass, &bi0, &next0, &error0, frag_hdr,
1190 is_custom_app, &handoff_thread_idx, skip_bihash))
1192 case IP6_FULL_REASS_RC_OK:
1193 /* nothing to do here */
1195 case IP6_FULL_REASS_RC_HANDOFF:
1196 next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1197 b0 = vlib_get_buffer (vm, bi0);
1198 vnet_buffer (b0)->ip.reass.owner_thread_index =
1201 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
1202 counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
1204 case IP6_FULL_REASS_RC_NO_BUF:
1205 counter = IP6_ERROR_REASS_NO_BUF;
1207 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
1208 counter = IP6_ERROR_REASS_INTERNAL_ERROR;
1213 vlib_node_increment_counter (vm, node->node_index, counter,
1215 ip6_full_reass_drop_all (vm, node, reass);
1216 ip6_full_reass_free (rm, rt, reass);
1224 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1228 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1229 next0 = fvnb->ip.reass.error_next_index;
1231 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1239 n_left_to_next -= 1;
1241 /* bi0 might have been updated by reass_finalize, reload */
1242 b0 = vlib_get_buffer (vm, bi0);
1243 if (IP6_ERROR_NONE != error0)
1245 b0->error = node->errors[error0];
1248 if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
1250 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1252 ip6_full_reass_add_trace (
1253 vm, node, NULL, bi0, frag_hdr, HANDOFF,
1254 vnet_buffer (b0)->ip.reass.owner_thread_index);
1257 else if (is_feature && IP6_ERROR_NONE == error0)
1259 vnet_feature_next (&next0, b0);
1261 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1262 n_left_to_next, bi0, next0);
1267 next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1268 to_next[0] = icmp_bi;
1270 n_left_to_next -= 1;
1271 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1272 n_left_to_next, icmp_bi,
1280 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1283 clib_spinlock_unlock (&rt->lock);
1284 return frame->n_vectors;
1287 static char *ip6_full_reassembly_error_strings[] = {
1288 #define _(sym, string) string,
1293 VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1294 vlib_node_runtime_t * node,
1295 vlib_frame_t * frame)
1297 return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */ ,
1298 false /* is_custom_app */ );
1301 VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1302 .name = "ip6-full-reassembly",
1303 .vector_size = sizeof (u32),
1304 .format_trace = format_ip6_full_reass_trace,
1305 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1306 .error_strings = ip6_full_reassembly_error_strings,
1307 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1310 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1311 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1312 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1313 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
1317 VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1318 vlib_node_runtime_t * node,
1319 vlib_frame_t * frame)
1321 return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */ ,
1322 false /* is_custom_app */ );
1325 VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1326 .name = "ip6-full-reassembly-feature",
1327 .vector_size = sizeof (u32),
1328 .format_trace = format_ip6_full_reass_trace,
1329 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1330 .error_strings = ip6_full_reassembly_error_strings,
1331 .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1334 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1335 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1336 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1337 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
1341 VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
1342 .arc_name = "ip6-unicast",
1343 .node_name = "ip6-full-reassembly-feature",
1344 .runs_before = VNET_FEATURES ("ip6-lookup",
1345 "ipsec6-input-feature"),
1349 #ifndef CLIB_MARCH_VARIANT
1351 ip6_full_reass_get_nbuckets ()
1353 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1357 nbuckets = (u32) (rm->max_reass_n / IP6_FULL_REASS_HT_LOAD_FACTOR);
1359 for (i = 0; i < 31; i++)
1360 if ((1 << i) >= nbuckets)
1366 #endif /* CLIB_MARCH_VARIANT */
1370 IP6_EVENT_CONFIG_CHANGED = 1,
1371 } ip6_full_reass_event_t;
1373 #ifndef CLIB_MARCH_VARIANT
1377 clib_bihash_48_8_t *new_hash;
1378 } ip6_rehash_cb_ctx;
1381 ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1383 ip6_rehash_cb_ctx *ctx = _ctx;
1384 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1388 return (BIHASH_WALK_CONTINUE);
1392 ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1393 u32 max_reassembly_length,
1394 u32 expire_walk_interval_ms)
1396 ip6_full_reass_main.timeout_ms = timeout_ms;
1397 ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1398 ip6_full_reass_main.max_reass_n = max_reassemblies;
1399 ip6_full_reass_main.max_reass_len = max_reassembly_length;
1400 ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1404 ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1405 u32 max_reassembly_length, u32 expire_walk_interval_ms)
1407 u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1408 ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1409 max_reassembly_length, expire_walk_interval_ms);
1410 vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1411 ip6_full_reass_main.ip6_full_reass_expire_node_idx,
1412 IP6_EVENT_CONFIG_CHANGED, 0);
1413 u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1414 if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
1416 clib_bihash_48_8_t new_hash;
1417 clib_memset (&new_hash, 0, sizeof (new_hash));
1418 ip6_rehash_cb_ctx ctx;
1420 ctx.new_hash = &new_hash;
1421 clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
1422 new_nbuckets * 1024);
1423 clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
1424 ip6_rehash_cb, &ctx);
1427 clib_bihash_free_48_8 (&new_hash);
1432 clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1433 clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1434 sizeof (ip6_full_reass_main.hash));
1435 clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
1442 ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1443 u32 * max_reassembly_length,
1444 u32 * expire_walk_interval_ms)
1446 *timeout_ms = ip6_full_reass_main.timeout_ms;
1447 *max_reassemblies = ip6_full_reass_main.max_reass_n;
1448 *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1449 *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
1453 static clib_error_t *
1454 ip6_full_reass_init_function (vlib_main_t * vm)
1456 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1457 clib_error_t *error = 0;
1463 vec_validate (rm->per_thread_data, vlib_num_workers ());
1464 ip6_full_reass_per_thread_t *rt;
1465 vec_foreach (rt, rm->per_thread_data)
1467 clib_spinlock_init (&rt->lock);
1468 pool_alloc (rt->pool, rm->max_reass_n);
1471 node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
1473 rm->ip6_full_reass_expire_node_idx = node->index;
1475 ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1476 IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1477 IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1478 IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1480 nbuckets = ip6_full_reass_get_nbuckets ();
1481 clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1484 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
1486 rm->ip6_drop_idx = node->index;
1487 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1489 rm->ip6_icmp_error_idx = node->index;
1491 if ((error = vlib_call_init_function (vm, ip_main_init)))
1493 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1494 ip6_full_reass_node.index);
1496 rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
1497 rm->fq_feature_index =
1498 vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
1500 rm->feature_use_refcount_per_intf = NULL;
1504 VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
1505 #endif /* CLIB_MARCH_VARIANT */
1508 ip6_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
1509 CLIB_UNUSED (vlib_frame_t *f))
1511 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1512 uword event_type, *event_data = 0;
1516 vlib_process_wait_for_event_or_clock (vm,
1517 (f64) rm->expire_walk_interval_ms
1518 / (f64) MSEC_PER_SEC);
1519 event_type = vlib_process_get_events (vm, &event_data);
1524 /* no events => timeout */
1526 case IP6_EVENT_CONFIG_CHANGED:
1527 /* nothing to do here */
1530 clib_warning ("BUG: event type 0x%wx", event_type);
1533 f64 now = vlib_time_now (vm);
1535 ip6_full_reass_t *reass;
1536 int *pool_indexes_to_free = NULL;
1538 uword thread_index = 0;
1540 const uword nthreads = vlib_num_workers () + 1;
1541 u32 *vec_icmp_bi = NULL;
1542 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1544 ip6_full_reass_per_thread_t *rt =
1545 &rm->per_thread_data[thread_index];
1546 clib_spinlock_lock (&rt->lock);
1548 vec_reset_length (pool_indexes_to_free);
1549 pool_foreach_index (index, rt->pool) {
1550 reass = pool_elt_at_index (rt->pool, index);
1551 if (now > reass->last_heard + rm->timeout)
1553 vec_add1 (pool_indexes_to_free, index);
1557 vec_foreach (i, pool_indexes_to_free)
1559 ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1561 ip6_full_reass_on_timeout (vm, node, reass, &icmp_bi);
1563 vec_add1 (vec_icmp_bi, icmp_bi);
1565 ip6_full_reass_free (rm, rt, reass);
1568 clib_spinlock_unlock (&rt->lock);
1571 while (vec_len (vec_icmp_bi) > 0)
1574 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1575 u32 *to_next = vlib_frame_vector_args (f);
1576 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
1577 int trace_frame = 0;
1578 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1580 u32 bi = vec_pop (vec_icmp_bi);
1581 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1582 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1584 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1588 n_left_to_next -= 1;
1590 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
1591 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1594 vec_free (pool_indexes_to_free);
1595 vec_free (vec_icmp_bi);
1598 _vec_len (event_data) = 0;
1605 VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
1606 .function = ip6_full_reass_walk_expired,
1607 .format_trace = format_ip6_full_reass_trace,
1608 .type = VLIB_NODE_TYPE_PROCESS,
1609 .name = "ip6-full-reassembly-expire-walk",
1611 .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1612 .error_strings = ip6_full_reassembly_error_strings,
1617 format_ip6_full_reass_key (u8 * s, va_list * args)
1619 ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
1620 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1621 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1622 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1627 format_ip6_full_reass (u8 * s, va_list * args)
1629 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1630 ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
1632 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
1633 "last_packet_octet: %u, trace_op_counter: %u\n",
1634 reass->id, format_ip6_full_reass_key, &reass->key,
1635 reass->first_bi, reass->data_len, reass->last_packet_octet,
1636 reass->trace_op_counter);
1637 u32 bi = reass->first_bi;
1641 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1642 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1643 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1644 "fragment[%u, %u]\n",
1645 counter, vnb->ip.reass.range_first,
1646 vnb->ip.reass.range_last, bi,
1647 ip6_full_reass_buffer_get_data_offset (b),
1648 ip6_full_reass_buffer_get_data_len (b),
1649 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1650 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1652 bi = b->next_buffer;
1662 static clib_error_t *
1663 show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1664 CLIB_UNUSED (vlib_cli_command_t * lmd))
1666 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1668 vlib_cli_output (vm, "---------------------");
1669 vlib_cli_output (vm, "IP6 reassembly status");
1670 vlib_cli_output (vm, "---------------------");
1671 bool details = false;
1672 if (unformat (input, "details"))
1677 u32 sum_reass_n = 0;
1678 u64 sum_buffers_n = 0;
1679 ip6_full_reass_t *reass;
1681 const uword nthreads = vlib_num_workers () + 1;
1682 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1684 ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1685 clib_spinlock_lock (&rt->lock);
1688 pool_foreach (reass, rt->pool) {
1689 vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
1692 sum_reass_n += rt->reass_n;
1693 clib_spinlock_unlock (&rt->lock);
1695 vlib_cli_output (vm, "---------------------");
1696 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1697 (long unsigned) sum_reass_n);
1698 vlib_cli_output (vm,
1699 "Maximum configured concurrent full IP6 reassemblies per worker-thread: %lu\n",
1700 (long unsigned) rm->max_reass_n);
1701 vlib_cli_output (vm,
1702 "Maximum configured amount of fragments "
1703 "per full IP6 reassembly: %lu\n",
1704 (long unsigned) rm->max_reass_len);
1705 vlib_cli_output (vm,
1706 "Maximum configured full IP6 reassembly timeout: %lums\n",
1707 (long unsigned) rm->timeout_ms);
1708 vlib_cli_output (vm,
1709 "Maximum configured full IP6 reassembly expire walk interval: %lums\n",
1710 (long unsigned) rm->expire_walk_interval_ms);
1711 vlib_cli_output (vm, "Buffers in use: %lu\n",
1712 (long unsigned) sum_buffers_n);
1716 VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1717 .path = "show ip6-full-reassembly",
1718 .short_help = "show ip6-full-reassembly [details]",
1719 .function = show_ip6_full_reass,
1722 #ifndef CLIB_MARCH_VARIANT
1724 ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1726 return vnet_feature_enable_disable ("ip6-unicast",
1727 "ip6-full-reassembly-feature",
1728 sw_if_index, enable_disable, 0, 0);
1730 #endif /* CLIB_MARCH_VARIANT */
1732 #define foreach_ip6_full_reassembly_handoff_error \
1733 _(CONGESTION_DROP, "congestion drop")
1738 #define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1739 foreach_ip6_full_reassembly_handoff_error
1741 IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1742 } ip6_full_reassembly_handoff_error_t;
1744 static char *ip6_full_reassembly_handoff_error_strings[] = {
1745 #define _(sym,string) string,
1746 foreach_ip6_full_reassembly_handoff_error
1752 u32 next_worker_index;
1753 } ip6_full_reassembly_handoff_trace_t;
1756 format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
1758 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1759 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1760 ip6_full_reassembly_handoff_trace_t *t =
1761 va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
1764 format (s, "ip6-full-reassembly-handoff: next-worker %d",
1765 t->next_worker_index);
1771 ip6_full_reassembly_handoff_inline (vlib_main_t * vm,
1772 vlib_node_runtime_t * node,
1773 vlib_frame_t * frame, bool is_feature)
1775 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1777 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1778 u32 n_enq, n_left_from, *from;
1779 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1782 from = vlib_frame_vector_args (frame);
1783 n_left_from = frame->n_vectors;
1784 vlib_get_buffers (vm, from, bufs, n_left_from);
1787 ti = thread_indices;
1789 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1791 while (n_left_from > 0)
1793 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1796 ((node->flags & VLIB_NODE_FLAG_TRACE)
1797 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1799 ip6_full_reassembly_handoff_trace_t *t =
1800 vlib_add_trace (vm, node, b[0], sizeof (*t));
1801 t->next_worker_index = ti[0];
1808 n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
1809 thread_indices, frame->n_vectors, 1);
1811 if (n_enq < frame->n_vectors)
1812 vlib_node_increment_counter (vm, node->node_index,
1813 IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1814 frame->n_vectors - n_enq);
1815 return frame->n_vectors;
1818 VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
1819 vlib_node_runtime_t * node,
1820 vlib_frame_t * frame)
1822 return ip6_full_reassembly_handoff_inline (vm, node, frame,
1823 false /* is_feature */ );
1826 VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
1827 .name = "ip6-full-reassembly-handoff",
1828 .vector_size = sizeof (u32),
1829 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1830 .error_strings = ip6_full_reassembly_handoff_error_strings,
1831 .format_trace = format_ip6_full_reassembly_handoff_trace,
1841 VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
1842 vlib_node_runtime_t * node, vlib_frame_t * frame)
1844 return ip6_full_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1848 VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
1849 .name = "ip6-full-reass-feature-hoff",
1850 .vector_size = sizeof (u32),
1851 .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
1852 .error_strings = ip6_full_reassembly_handoff_error_strings,
1853 .format_trace = format_ip6_full_reassembly_handoff_trace,
1862 #ifndef CLIB_MARCH_VARIANT
1864 ip6_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1866 ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1867 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1870 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1872 ++rm->feature_use_refcount_per_intf[sw_if_index];
1873 return vnet_feature_enable_disable ("ip6-unicast",
1874 "ip6-full-reassembly-feature",
1875 sw_if_index, 1, 0, 0);
1877 ++rm->feature_use_refcount_per_intf[sw_if_index];
1881 --rm->feature_use_refcount_per_intf[sw_if_index];
1882 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1883 return vnet_feature_enable_disable ("ip6-unicast",
1884 "ip6-full-reassembly-feature",
1885 sw_if_index, 0, 0, 0);
1892 * fd.io coding-style-patch-verification: ON
1895 * eval: (c-set-style "gnu")