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 IPv4 Full Reassembly.
20 * This file contains the source code for IPv4 full reassembly.
23 #include <vppinfra/vec.h>
24 #include <vnet/vnet.h>
25 #include <vnet/ip/ip.h>
26 #include <vppinfra/fifo.h>
27 #include <vppinfra/bihash_16_8.h>
28 #include <vnet/ip/reass/ip4_full_reass.h>
31 #define MSEC_PER_SEC 1000
32 #define IP4_REASS_TIMEOUT_DEFAULT_MS 100
33 #define IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
34 #define IP4_REASS_MAX_REASSEMBLIES_DEFAULT 1024
35 #define IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
36 #define IP4_REASS_HT_LOAD_FACTOR (0.75)
38 #define IP4_REASS_DEBUG_BUFFERS 0
39 #if IP4_REASS_DEBUG_BUFFERS
40 #define IP4_REASS_DEBUG_BUFFER(bi, what) \
44 printf (#what "buffer %u", _bi); \
45 vlib_buffer_t *_b = vlib_get_buffer (vm, _bi); \
46 while (_b->flags & VLIB_BUFFER_NEXT_PRESENT) \
48 _bi = _b->next_buffer; \
49 printf ("[%u]", _bi); \
50 _b = vlib_get_buffer (vm, _bi); \
57 #define IP4_REASS_DEBUG_BUFFER(...)
63 IP4_REASS_RC_TOO_MANY_FRAGMENTS,
64 IP4_REASS_RC_INTERNAL_ERROR,
67 } ip4_full_reass_rc_t;
84 } ip4_full_reass_key_t;
91 u32 memory_owner_thread_index;
94 } ip4_full_reass_val_t;
100 ip4_full_reass_key_t k;
101 ip4_full_reass_val_t v;
103 clib_bihash_kv_16_8_t kv;
104 } ip4_full_reass_kv_t;
107 ip4_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
109 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
110 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
114 ip4_full_reass_buffer_get_data_len (vlib_buffer_t * b)
116 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
117 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
118 (vnb->ip.reass.fragment_first +
119 ip4_full_reass_buffer_get_data_offset (b)) + 1;
125 ip4_full_reass_key_t key;
126 // time when last packet was received
128 // internal id of this reassembly
130 // buffer index of first buffer in this reassembly context
132 // last octet of packet, ~0 until fragment without more_fragments arrives
133 u32 last_packet_octet;
134 // length of data collected so far
136 // trace operation counter
137 u32 trace_op_counter;
138 // next index - used by non-feature node
140 // error next index - used by custom apps (~0 if not used)
141 u32 error_next_index;
142 // minimum fragment length for this reassembly - used to estimate MTU
143 u16 min_fragment_length;
144 // number of fragments in this reassembly
146 // thread owning memory for this context (whose pool contains this ctx)
147 u32 memory_owner_thread_index;
148 // thread which received fragment with offset 0 and which sends out the
149 // completed reassembly
150 u32 sendout_thread_index;
155 ip4_full_reass_t *pool;
158 clib_spinlock_t lock;
159 } ip4_full_reass_per_thread_t;
166 u32 expire_walk_interval_ms;
167 // maximum number of fragments in one reassembly
169 // maximum number of reassemblies
173 clib_bihash_16_8_t hash;
175 ip4_full_reass_per_thread_t *per_thread_data;
178 vlib_main_t *vlib_main;
180 // node index of ip4-drop node
182 u32 ip4_full_reass_expire_node_idx;
184 /** Worker handoff */
186 u32 fq_feature_index;
189 // reference count for enabling/disabling feature - per interface
190 u32 *feature_use_refcount_per_intf;
191 } ip4_full_reass_main_t;
193 extern ip4_full_reass_main_t ip4_full_reass_main;
195 #ifndef CLIB_MARCH_VARIANT
196 ip4_full_reass_main_t ip4_full_reass_main;
197 #endif /* CLIB_MARCH_VARIANT */
201 IP4_FULL_REASS_NEXT_INPUT,
202 IP4_FULL_REASS_NEXT_DROP,
203 IP4_FULL_REASS_NEXT_HANDOFF,
204 IP4_FULL_REASS_N_NEXT,
205 } ip4_full_reass_next_t;
212 } ip4_full_reass_node_type_t;
222 } ip4_full_reass_trace_operation_e;
232 } ip4_full_reass_range_trace_t;
236 ip4_full_reass_trace_operation_e action;
238 ip4_full_reass_range_trace_t trace_range;
246 bool is_after_handoff;
247 ip4_header_t ip4_header;
248 } ip4_full_reass_trace_t;
250 extern vlib_node_registration_t ip4_full_reass_node;
251 extern vlib_node_registration_t ip4_full_reass_node_feature;
252 extern vlib_node_registration_t ip4_full_reass_node_custom;
255 ip4_full_reass_trace_details (vlib_main_t * vm, u32 bi,
256 ip4_full_reass_range_trace_t * trace)
258 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
259 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
260 trace->range_first = vnb->ip.reass.range_first;
261 trace->range_last = vnb->ip.reass.range_last;
262 trace->data_offset = ip4_full_reass_buffer_get_data_offset (b);
263 trace->data_len = ip4_full_reass_buffer_get_data_len (b);
264 trace->range_bi = bi;
268 format_ip4_full_reass_range_trace (u8 * s, va_list * args)
270 ip4_full_reass_range_trace_t *trace =
271 va_arg (*args, ip4_full_reass_range_trace_t *);
273 format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
274 trace->range_last, trace->data_offset, trace->data_len,
280 format_ip4_full_reass_trace (u8 * s, va_list * args)
282 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
283 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
284 ip4_full_reass_trace_t *t = va_arg (*args, ip4_full_reass_trace_t *);
286 if (~0 != t->reass_id)
288 if (t->is_after_handoff)
291 format (s, "%U\n", format_ip4_header, &t->ip4_header,
292 sizeof (t->ip4_header));
296 format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
297 t->reass_id, t->op_id);
298 indent = format_get_indent (s);
301 "first bi: %u, data len: %u, ip/fragment[%u, %u]",
302 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
308 s = format (s, "\n%Ushrink %U by %u", format_white_space, indent,
309 format_ip4_full_reass_range_trace, &t->trace_range,
313 s = format (s, "\n%Udiscard %U", format_white_space, indent,
314 format_ip4_full_reass_range_trace, &t->trace_range);
317 s = format (s, "\n%Unew %U", format_white_space, indent,
318 format_ip4_full_reass_range_trace, &t->trace_range);
321 s = format (s, "\n%Uoverlapping/ignored %U", format_white_space, indent,
322 format_ip4_full_reass_range_trace, &t->trace_range);
325 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
329 format (s, "handoff from thread #%u to thread #%u", t->thread_id,
337 ip4_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
338 ip4_full_reass_main_t * rm,
339 ip4_full_reass_t * reass, u32 bi,
340 ip4_full_reass_trace_operation_e action,
341 u32 size_diff, u32 thread_id_to)
343 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
344 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
345 if (pool_is_free_index
346 (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
348 // this buffer's trace is gone
349 b->flags &= ~VLIB_BUFFER_IS_TRACED;
352 bool is_after_handoff = false;
353 if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
355 is_after_handoff = true;
357 ip4_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
358 t->is_after_handoff = is_after_handoff;
359 if (t->is_after_handoff)
361 clib_memcpy (&t->ip4_header, vlib_buffer_get_current (b),
362 clib_min (sizeof (t->ip4_header), b->current_length));
366 t->reass_id = reass->id;
367 t->op_id = reass->trace_op_counter;
368 t->trace_range.first_bi = reass->first_bi;
369 t->total_data_len = reass->data_len;
370 ++reass->trace_op_counter;
376 t->trace_range.first_bi = 0;
377 t->total_data_len = 0;
380 ip4_full_reass_trace_details (vm, bi, &t->trace_range);
381 t->size_diff = size_diff;
382 t->thread_id = vm->thread_index;
383 t->thread_id_to = thread_id_to;
384 t->fragment_first = vnb->ip.reass.fragment_first;
385 t->fragment_last = vnb->ip.reass.fragment_last;
388 s = format (s, "%U", format_ip4_full_reass_trace, NULL, NULL, t);
389 printf ("%.*s\n", vec_len (s), s);
391 vec_reset_length (s);
396 ip4_full_reass_free_ctx (ip4_full_reass_per_thread_t * rt,
397 ip4_full_reass_t * reass)
399 pool_put (rt->pool, reass);
404 ip4_full_reass_free (ip4_full_reass_main_t * rm,
405 ip4_full_reass_per_thread_t * rt,
406 ip4_full_reass_t * reass)
408 clib_bihash_kv_16_8_t kv;
409 kv.key[0] = reass->key.as_u64[0];
410 kv.key[1] = reass->key.as_u64[1];
411 clib_bihash_add_del_16_8 (&rm->hash, &kv, 0);
412 return ip4_full_reass_free_ctx (rt, reass);
416 ip4_full_reass_drop_all (vlib_main_t * vm, vlib_node_runtime_t * node,
417 ip4_full_reass_main_t * rm, ip4_full_reass_t * reass)
419 u32 range_bi = reass->first_bi;
420 vlib_buffer_t *range_b;
421 vnet_buffer_opaque_t *range_vnb;
423 while (~0 != range_bi)
425 range_b = vlib_get_buffer (vm, range_bi);
426 range_vnb = vnet_buffer (range_b);
430 vec_add1 (to_free, bi);
431 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
432 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
435 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
442 range_bi = range_vnb->ip.reass.next_range_bi;
444 /* send to next_error_index */
445 if (~0 != reass->error_next_index)
447 u32 n_left_to_next, *to_next, next_index;
449 next_index = reass->error_next_index;
452 while (vec_len (to_free) > 0)
454 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
456 while (vec_len (to_free) > 0 && n_left_to_next > 0)
458 bi = vec_pop (to_free);
467 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
472 vlib_buffer_free (vm, to_free, vec_len (to_free));
478 ip4_full_reass_init (ip4_full_reass_t * reass)
480 reass->first_bi = ~0;
481 reass->last_packet_octet = ~0;
483 reass->next_index = ~0;
484 reass->error_next_index = ~0;
487 always_inline ip4_full_reass_t *
488 ip4_full_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
489 ip4_full_reass_main_t * rm,
490 ip4_full_reass_per_thread_t * rt,
491 ip4_full_reass_kv_t * kv, u8 * do_handoff)
493 ip4_full_reass_t *reass;
499 now = vlib_time_now (vm);
500 if (!clib_bihash_search_16_8 (&rm->hash, &kv->kv, &kv->kv))
502 if (vm->thread_index != kv->v.memory_owner_thread_index)
508 pool_elt_at_index (rm->per_thread_data
509 [kv->v.memory_owner_thread_index].pool,
512 if (now > reass->last_heard + rm->timeout)
514 ip4_full_reass_drop_all (vm, node, rm, reass);
515 ip4_full_reass_free (rm, rt, reass);
522 reass->last_heard = now;
526 if (rt->reass_n >= rm->max_reass_n)
533 pool_get (rt->pool, reass);
534 clib_memset (reass, 0, sizeof (*reass));
535 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
536 reass->memory_owner_thread_index = vm->thread_index;
538 ip4_full_reass_init (reass);
542 reass->key.as_u64[0] = kv->kv.key[0];
543 reass->key.as_u64[1] = kv->kv.key[1];
544 kv->v.reass_index = (reass - rt->pool);
545 kv->v.memory_owner_thread_index = vm->thread_index;
546 reass->last_heard = now;
548 int rv = clib_bihash_add_del_16_8 (&rm->hash, &kv->kv, 2);
551 ip4_full_reass_free_ctx (rt, reass);
553 // if other worker created a context already work with the other copy
561 always_inline ip4_full_reass_rc_t
562 ip4_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
563 ip4_full_reass_main_t * rm,
564 ip4_full_reass_per_thread_t * rt,
565 ip4_full_reass_t * reass, u32 * bi0,
566 u32 * next0, u32 * error0, bool is_custom)
568 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
569 vlib_buffer_t *last_b = NULL;
570 u32 sub_chain_bi = reass->first_bi;
571 u32 total_length = 0;
575 u32 tmp_bi = sub_chain_bi;
576 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
577 ip4_header_t *ip = vlib_buffer_get_current (tmp);
578 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
579 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
580 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
582 return IP4_REASS_RC_INTERNAL_ERROR;
585 u32 data_len = ip4_full_reass_buffer_get_data_len (tmp);
587 ip4_header_bytes (ip) + ip4_full_reass_buffer_get_data_offset (tmp);
589 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
590 if (tmp_bi == reass->first_bi)
592 /* first buffer - keep ip4 header */
593 if (0 != ip4_full_reass_buffer_get_data_offset (tmp))
595 return IP4_REASS_RC_INTERNAL_ERROR;
598 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
599 ip4_header_bytes (ip);
600 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
602 return IP4_REASS_RC_INTERNAL_ERROR;
606 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
612 if (trim_front > tmp->current_length)
614 /* drop whole buffer */
615 u32 to_be_freed_bi = tmp_bi;
616 trim_front -= tmp->current_length;
617 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
619 return IP4_REASS_RC_INTERNAL_ERROR;
621 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
622 tmp_bi = tmp->next_buffer;
623 tmp->next_buffer = 0;
624 tmp = vlib_get_buffer (vm, tmp_bi);
625 vlib_buffer_free_one (vm, to_be_freed_bi);
630 vlib_buffer_advance (tmp, trim_front);
638 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
639 last_b->next_buffer = tmp_bi;
642 if (keep_data <= tmp->current_length)
644 tmp->current_length = keep_data;
649 keep_data -= tmp->current_length;
650 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
652 return IP4_REASS_RC_INTERNAL_ERROR;
655 total_length += tmp->current_length;
656 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
658 tmp_bi = tmp->next_buffer;
659 tmp = vlib_get_buffer (vm, tmp->next_buffer);
668 u32 to_be_freed_bi = tmp_bi;
669 if (reass->first_bi == tmp_bi)
671 return IP4_REASS_RC_INTERNAL_ERROR;
673 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
675 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
676 tmp_bi = tmp->next_buffer;
677 tmp->next_buffer = 0;
678 tmp = vlib_get_buffer (vm, tmp_bi);
679 vlib_buffer_free_one (vm, to_be_freed_bi);
683 tmp->next_buffer = 0;
684 vlib_buffer_free_one (vm, to_be_freed_bi);
690 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
693 while (~0 != sub_chain_bi);
697 return IP4_REASS_RC_INTERNAL_ERROR;
699 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
701 if (total_length < first_b->current_length)
703 return IP4_REASS_RC_INTERNAL_ERROR;
705 total_length -= first_b->current_length;
706 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
707 first_b->total_length_not_including_first_buffer = total_length;
708 ip4_header_t *ip = vlib_buffer_get_current (first_b);
709 ip->flags_and_fragment_offset = 0;
710 ip->length = clib_host_to_net_u16 (first_b->current_length + total_length);
711 ip->checksum = ip4_header_checksum (ip);
712 if (!vlib_buffer_chain_linearize (vm, first_b))
714 return IP4_REASS_RC_NO_BUF;
716 // reset to reconstruct the mbuf linking
717 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
718 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
720 ip4_full_reass_add_trace (vm, node, rm, reass, reass->first_bi,
723 // following code does a hexdump of packet fragments to stdout ...
726 u32 bi = reass->first_bi;
730 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
731 s = format (s, "%u: %U\n", bi, format_hexdump,
732 vlib_buffer_get_current (b), b->current_length);
733 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
742 printf ("%.*s\n", vec_len (s), s);
749 *bi0 = reass->first_bi;
752 *next0 = IP4_FULL_REASS_NEXT_INPUT;
756 *next0 = reass->next_index;
758 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
759 *error0 = IP4_ERROR_NONE;
760 ip4_full_reass_free (rm, rt, reass);
762 return IP4_REASS_RC_OK;
765 always_inline ip4_full_reass_rc_t
766 ip4_full_reass_insert_range_in_chain (vlib_main_t * vm,
767 ip4_full_reass_main_t * rm,
768 ip4_full_reass_per_thread_t * rt,
769 ip4_full_reass_t * reass,
770 u32 prev_range_bi, u32 new_next_bi)
772 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
773 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
774 if (~0 != prev_range_bi)
776 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
777 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
778 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
779 prev_vnb->ip.reass.next_range_bi = new_next_bi;
783 if (~0 != reass->first_bi)
785 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
787 reass->first_bi = new_next_bi;
789 vnet_buffer_opaque_t *vnb = vnet_buffer (new_next_b);
790 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
791 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
793 return IP4_REASS_RC_INTERNAL_ERROR;
795 reass->data_len += ip4_full_reass_buffer_get_data_len (new_next_b);
796 return IP4_REASS_RC_OK;
799 always_inline ip4_full_reass_rc_t
800 ip4_full_reass_remove_range_from_chain (vlib_main_t * vm,
801 vlib_node_runtime_t * node,
802 ip4_full_reass_main_t * rm,
803 ip4_full_reass_t * reass,
804 u32 prev_range_bi, u32 discard_bi)
806 vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi);
807 vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b);
808 if (~0 != prev_range_bi)
810 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
811 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
812 if (!(prev_vnb->ip.reass.next_range_bi == discard_bi))
814 return IP4_REASS_RC_INTERNAL_ERROR;
816 prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi;
820 reass->first_bi = discard_vnb->ip.reass.next_range_bi;
822 vnet_buffer_opaque_t *vnb = vnet_buffer (discard_b);
823 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
824 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
826 return IP4_REASS_RC_INTERNAL_ERROR;
828 reass->data_len -= ip4_full_reass_buffer_get_data_len (discard_b);
831 u32 to_be_freed_bi = discard_bi;
832 if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED))
834 ip4_full_reass_add_trace (vm, node, rm, reass, discard_bi,
835 RANGE_DISCARD, 0, ~0);
837 if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT)
839 discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
840 discard_bi = discard_b->next_buffer;
841 discard_b->next_buffer = 0;
842 discard_b = vlib_get_buffer (vm, discard_bi);
843 vlib_buffer_free_one (vm, to_be_freed_bi);
847 discard_b->next_buffer = 0;
848 vlib_buffer_free_one (vm, to_be_freed_bi);
852 return IP4_REASS_RC_OK;
855 always_inline ip4_full_reass_rc_t
856 ip4_full_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
857 ip4_full_reass_main_t * rm,
858 ip4_full_reass_per_thread_t * rt,
859 ip4_full_reass_t * reass, u32 * bi0, u32 * next0,
860 u32 * error0, bool is_custom, u32 * handoff_thread_idx)
862 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
863 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
866 // store (error_)next_index before it's overwritten
867 reass->next_index = fvnb->ip.reass.next_index;
868 reass->error_next_index = fvnb->ip.reass.error_next_index;
870 ip4_full_reass_rc_t rc = IP4_REASS_RC_OK;
872 ip4_header_t *fip = vlib_buffer_get_current (fb);
873 const u32 fragment_first = ip4_get_fragment_offset_bytes (fip);
874 const u32 fragment_length =
875 clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip);
876 const u32 fragment_last = fragment_first + fragment_length - 1;
877 fvnb->ip.reass.fragment_first = fragment_first;
878 fvnb->ip.reass.fragment_last = fragment_last;
879 int more_fragments = ip4_get_fragment_more (fip);
880 u32 candidate_range_bi = reass->first_bi;
881 u32 prev_range_bi = ~0;
882 fvnb->ip.reass.range_first = fragment_first;
883 fvnb->ip.reass.range_last = fragment_last;
884 fvnb->ip.reass.next_range_bi = ~0;
887 reass->last_packet_octet = fragment_last;
889 if (~0 == reass->first_bi)
891 // starting a new reassembly
893 ip4_full_reass_insert_range_in_chain (vm, rm, rt, reass,
894 prev_range_bi, *bi0);
895 if (IP4_REASS_RC_OK != rc)
899 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
901 ip4_full_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0,
905 reass->min_fragment_length = clib_net_to_host_u16 (fip->length);
906 reass->fragments_n = 1;
907 return IP4_REASS_RC_OK;
909 reass->min_fragment_length =
910 clib_min (clib_net_to_host_u16 (fip->length),
911 fvnb->ip.reass.estimated_mtu);
912 while (~0 != candidate_range_bi)
914 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
915 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
916 if (fragment_first > candidate_vnb->ip.reass.range_last)
918 // this fragments starts after candidate range
919 prev_range_bi = candidate_range_bi;
920 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
921 if (candidate_vnb->ip.reass.range_last < fragment_last &&
922 ~0 == candidate_range_bi)
924 // special case - this fragment falls beyond all known ranges
926 ip4_full_reass_insert_range_in_chain (vm, rm, rt, reass,
927 prev_range_bi, *bi0);
928 if (IP4_REASS_RC_OK != rc)
937 if (fragment_last < candidate_vnb->ip.reass.range_first)
939 // this fragment ends before candidate range without any overlap
941 ip4_full_reass_insert_range_in_chain (vm, rm, rt, reass,
942 prev_range_bi, *bi0);
943 if (IP4_REASS_RC_OK != rc)
951 if (fragment_first >= candidate_vnb->ip.reass.range_first &&
952 fragment_last <= candidate_vnb->ip.reass.range_last)
954 // this fragment is a (sub)part of existing range, ignore it
955 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
957 ip4_full_reass_add_trace (vm, node, rm, reass, *bi0,
958 RANGE_OVERLAP, 0, ~0);
962 int discard_candidate = 0;
963 if (fragment_first < candidate_vnb->ip.reass.range_first)
966 fragment_last - candidate_vnb->ip.reass.range_first + 1;
967 if (overlap < ip4_full_reass_buffer_get_data_len (candidate_b))
969 candidate_vnb->ip.reass.range_first += overlap;
970 if (reass->data_len < overlap)
972 return IP4_REASS_RC_INTERNAL_ERROR;
974 reass->data_len -= overlap;
975 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
977 ip4_full_reass_add_trace (vm, node, rm, reass,
979 RANGE_SHRINK, 0, ~0);
982 ip4_full_reass_insert_range_in_chain (vm, rm, rt, reass,
985 if (IP4_REASS_RC_OK != rc)
993 discard_candidate = 1;
996 else if (fragment_last > candidate_vnb->ip.reass.range_last)
999 candidate_vnb->ip.reass.range_last - fragment_first + 1;
1000 if (overlap < ip4_full_reass_buffer_get_data_len (candidate_b))
1002 fvnb->ip.reass.range_first += overlap;
1003 if (~0 != candidate_vnb->ip.reass.next_range_bi)
1005 prev_range_bi = candidate_range_bi;
1006 candidate_range_bi =
1007 candidate_vnb->ip.reass.next_range_bi;
1012 // special case - last range discarded
1014 ip4_full_reass_insert_range_in_chain (vm, rm, rt,
1018 if (IP4_REASS_RC_OK != rc)
1027 discard_candidate = 1;
1032 discard_candidate = 1;
1034 if (discard_candidate)
1036 u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi;
1037 // discard candidate range, probe next range
1039 ip4_full_reass_remove_range_from_chain (vm, node, rm, reass,
1041 candidate_range_bi);
1042 if (IP4_REASS_RC_OK != rc)
1046 if (~0 != next_range_bi)
1048 candidate_range_bi = next_range_bi;
1053 // special case - last range discarded
1055 ip4_full_reass_insert_range_in_chain (vm, rm, rt, reass,
1058 if (IP4_REASS_RC_OK != rc)
1068 ++reass->fragments_n;
1071 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1073 ip4_full_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0,
1077 if (~0 != reass->last_packet_octet &&
1078 reass->data_len == reass->last_packet_octet + 1)
1080 *handoff_thread_idx = reass->sendout_thread_index;
1082 reass->memory_owner_thread_index != reass->sendout_thread_index;
1084 ip4_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
1086 if (IP4_REASS_RC_OK == rc && handoff)
1088 rc = IP4_REASS_RC_HANDOFF;
1096 if (reass->fragments_n > rm->max_reass_len)
1098 rc = IP4_REASS_RC_TOO_MANY_FRAGMENTS;
1103 *next0 = IP4_FULL_REASS_NEXT_DROP;
1104 *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT;
1111 ip4_full_reass_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1112 vlib_frame_t * frame, ip4_full_reass_node_type_t type)
1114 u32 *from = vlib_frame_vector_args (frame);
1115 u32 n_left_from, n_left_to_next, *to_next, next_index;
1116 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1117 ip4_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
1118 clib_spinlock_lock (&rt->lock);
1120 n_left_from = frame->n_vectors;
1121 next_index = node->cached_next_index;
1122 while (n_left_from > 0)
1124 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1126 while (n_left_from > 0 && n_left_to_next > 0)
1131 u32 error0 = IP4_ERROR_NONE;
1134 b0 = vlib_get_buffer (vm, bi0);
1136 ip4_header_t *ip0 = vlib_buffer_get_current (b0);
1137 if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
1139 // this is a whole packet - no fragmentation
1142 next0 = IP4_FULL_REASS_NEXT_INPUT;
1146 next0 = vnet_buffer (b0)->ip.reass.next_index;
1148 goto packet_enqueue;
1150 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
1151 const u32 fragment_length =
1152 clib_net_to_host_u16 (ip0->length) - ip4_header_bytes (ip0);
1153 const u32 fragment_last = fragment_first + fragment_length - 1;
1154 if (fragment_first > fragment_last || fragment_first + fragment_length > UINT16_MAX - 20 || (fragment_length < 8 && ip4_get_fragment_more (ip0))) // 8 is minimum frag length per RFC 791
1156 next0 = IP4_FULL_REASS_NEXT_DROP;
1157 error0 = IP4_ERROR_REASS_MALFORMED_PACKET;
1158 goto packet_enqueue;
1160 ip4_full_reass_kv_t kv;
1164 (u64) vec_elt (ip4_main.fib_index_by_sw_if_index,
1165 vnet_buffer (b0)->sw_if_index[VLIB_RX]) |
1166 (u64) ip0->src_address.as_u32 << 32;
1168 (u64) ip0->dst_address.
1169 as_u32 | (u64) ip0->fragment_id << 32 | (u64) ip0->protocol << 48;
1171 ip4_full_reass_t *reass =
1172 ip4_full_reass_find_or_create (vm, node, rm, rt, &kv,
1177 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
1178 if (0 == fragment_first)
1180 reass->sendout_thread_index = vm->thread_index;
1184 if (PREDICT_FALSE (do_handoff))
1186 next0 = IP4_FULL_REASS_NEXT_HANDOFF;
1187 vnet_buffer (b0)->ip.reass.owner_thread_index =
1188 kv.v.memory_owner_thread_index;
1192 u32 handoff_thread_idx;
1193 switch (ip4_full_reass_update
1194 (vm, node, rm, rt, reass, &bi0, &next0,
1195 &error0, CUSTOM == type, &handoff_thread_idx))
1197 case IP4_REASS_RC_OK:
1198 /* nothing to do here */
1200 case IP4_REASS_RC_HANDOFF:
1201 next0 = IP4_FULL_REASS_NEXT_HANDOFF;
1202 b0 = vlib_get_buffer (vm, bi0);
1203 vnet_buffer (b0)->ip.reass.owner_thread_index =
1206 case IP4_REASS_RC_TOO_MANY_FRAGMENTS:
1207 vlib_node_increment_counter (vm, node->node_index,
1208 IP4_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1210 ip4_full_reass_drop_all (vm, node, rm, reass);
1211 ip4_full_reass_free (rm, rt, reass);
1214 case IP4_REASS_RC_NO_BUF:
1215 vlib_node_increment_counter (vm, node->node_index,
1216 IP4_ERROR_REASS_NO_BUF, 1);
1217 ip4_full_reass_drop_all (vm, node, rm, reass);
1218 ip4_full_reass_free (rm, rt, reass);
1221 case IP4_REASS_RC_INTERNAL_ERROR:
1222 /* drop everything and start with a clean slate */
1223 vlib_node_increment_counter (vm, node->node_index,
1224 IP4_ERROR_REASS_INTERNAL_ERROR,
1226 ip4_full_reass_drop_all (vm, node, rm, reass);
1227 ip4_full_reass_free (rm, rt, reass);
1234 next0 = IP4_FULL_REASS_NEXT_DROP;
1235 error0 = IP4_ERROR_REASS_LIMIT_REACHED;
1245 n_left_to_next -= 1;
1247 /* bi0 might have been updated by reass_finalize, reload */
1248 b0 = vlib_get_buffer (vm, bi0);
1249 if (IP4_ERROR_NONE != error0)
1251 b0->error = node->errors[error0];
1254 if (next0 == IP4_FULL_REASS_NEXT_HANDOFF)
1256 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1258 ip4_full_reass_add_trace (vm, node, rm, NULL, bi0,
1260 vnet_buffer (b0)->ip.
1261 reass.owner_thread_index);
1264 else if (FEATURE == type && IP4_ERROR_NONE == error0)
1266 vnet_feature_next (&next0, b0);
1268 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1269 to_next, n_left_to_next,
1271 IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next);
1279 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1282 clib_spinlock_unlock (&rt->lock);
1283 return frame->n_vectors;
1286 static char *ip4_full_reass_error_strings[] = {
1287 #define _(sym, string) string,
1292 VLIB_NODE_FN (ip4_full_reass_node) (vlib_main_t * vm,
1293 vlib_node_runtime_t * node,
1294 vlib_frame_t * frame)
1296 return ip4_full_reass_inline (vm, node, frame, NORMAL);
1300 VLIB_REGISTER_NODE (ip4_full_reass_node) = {
1301 .name = "ip4-full-reassembly",
1302 .vector_size = sizeof (u32),
1303 .format_trace = format_ip4_full_reass_trace,
1304 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1305 .error_strings = ip4_full_reass_error_strings,
1306 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
1309 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1310 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1311 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reassembly-handoff",
1317 VLIB_NODE_FN (ip4_full_reass_node_feature) (vlib_main_t * vm,
1318 vlib_node_runtime_t * node,
1319 vlib_frame_t * frame)
1321 return ip4_full_reass_inline (vm, node, frame, FEATURE);
1325 VLIB_REGISTER_NODE (ip4_full_reass_node_feature) = {
1326 .name = "ip4-full-reassembly-feature",
1327 .vector_size = sizeof (u32),
1328 .format_trace = format_ip4_full_reass_trace,
1329 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1330 .error_strings = ip4_full_reass_error_strings,
1331 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
1334 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1335 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1336 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reass-feature-hoff",
1342 VNET_FEATURE_INIT (ip4_full_reass_feature, static) = {
1343 .arc_name = "ip4-unicast",
1344 .node_name = "ip4-full-reassembly-feature",
1345 .runs_before = VNET_FEATURES ("ip4-lookup",
1346 "ipsec4-input-feature"),
1351 VLIB_NODE_FN (ip4_full_reass_node_custom) (vlib_main_t * vm,
1352 vlib_node_runtime_t * node,
1353 vlib_frame_t * frame)
1355 return ip4_full_reass_inline (vm, node, frame, CUSTOM);
1359 VLIB_REGISTER_NODE (ip4_full_reass_node_custom) = {
1360 .name = "ip4-full-reassembly-custom",
1361 .vector_size = sizeof (u32),
1362 .format_trace = format_ip4_full_reass_trace,
1363 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1364 .error_strings = ip4_full_reass_error_strings,
1365 .n_next_nodes = IP4_FULL_REASS_N_NEXT,
1368 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1369 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1370 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reass-custom-hoff",
1376 VNET_FEATURE_INIT (ip4_full_reass_custom, static) = {
1377 .arc_name = "ip4-unicast",
1378 .node_name = "ip4-full-reassembly-feature",
1379 .runs_before = VNET_FEATURES ("ip4-lookup",
1380 "ipsec4-input-feature"),
1386 #ifndef CLIB_MARCH_VARIANT
1388 ip4_full_reass_custom_register_next_node (uword node_index)
1390 return vlib_node_add_next (vlib_get_main (),
1391 ip4_full_reass_node_custom.index, node_index);
1395 ip4_full_reass_get_nbuckets ()
1397 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1401 nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR);
1403 for (i = 0; i < 31; i++)
1404 if ((1 << i) >= nbuckets)
1410 #endif /* CLIB_MARCH_VARIANT */
1414 IP4_EVENT_CONFIG_CHANGED = 1,
1415 } ip4_full_reass_event_t;
1420 clib_bihash_16_8_t *new_hash;
1421 } ip4_rehash_cb_ctx;
1423 #ifndef CLIB_MARCH_VARIANT
1425 ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
1427 ip4_rehash_cb_ctx *ctx = _ctx;
1428 if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
1432 return (BIHASH_WALK_CONTINUE);
1436 ip4_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1437 u32 max_reassembly_length,
1438 u32 expire_walk_interval_ms)
1440 ip4_full_reass_main.timeout_ms = timeout_ms;
1441 ip4_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1442 ip4_full_reass_main.max_reass_n = max_reassemblies;
1443 ip4_full_reass_main.max_reass_len = max_reassembly_length;
1444 ip4_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1448 ip4_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1449 u32 max_reassembly_length, u32 expire_walk_interval_ms)
1451 u32 old_nbuckets = ip4_full_reass_get_nbuckets ();
1452 ip4_full_reass_set_params (timeout_ms, max_reassemblies,
1453 max_reassembly_length, expire_walk_interval_ms);
1454 vlib_process_signal_event (ip4_full_reass_main.vlib_main,
1455 ip4_full_reass_main.ip4_full_reass_expire_node_idx,
1456 IP4_EVENT_CONFIG_CHANGED, 0);
1457 u32 new_nbuckets = ip4_full_reass_get_nbuckets ();
1458 if (ip4_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
1460 clib_bihash_16_8_t new_hash;
1461 clib_memset (&new_hash, 0, sizeof (new_hash));
1462 ip4_rehash_cb_ctx ctx;
1464 ctx.new_hash = &new_hash;
1465 clib_bihash_init_16_8 (&new_hash, "ip4-dr", new_nbuckets,
1466 new_nbuckets * 1024);
1467 clib_bihash_foreach_key_value_pair_16_8 (&ip4_full_reass_main.hash,
1468 ip4_rehash_cb, &ctx);
1471 clib_bihash_free_16_8 (&new_hash);
1476 clib_bihash_free_16_8 (&ip4_full_reass_main.hash);
1477 clib_memcpy_fast (&ip4_full_reass_main.hash, &new_hash,
1478 sizeof (ip4_full_reass_main.hash));
1479 clib_bihash_copied (&ip4_full_reass_main.hash, &new_hash);
1486 ip4_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1487 u32 * max_reassembly_length,
1488 u32 * expire_walk_interval_ms)
1490 *timeout_ms = ip4_full_reass_main.timeout_ms;
1491 *max_reassemblies = ip4_full_reass_main.max_reass_n;
1492 *max_reassembly_length = ip4_full_reass_main.max_reass_len;
1493 *expire_walk_interval_ms = ip4_full_reass_main.expire_walk_interval_ms;
1497 static clib_error_t *
1498 ip4_full_reass_init_function (vlib_main_t * vm)
1500 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1501 clib_error_t *error = 0;
1507 vec_validate (rm->per_thread_data, vlib_num_workers ());
1508 ip4_full_reass_per_thread_t *rt;
1509 vec_foreach (rt, rm->per_thread_data)
1511 clib_spinlock_init (&rt->lock);
1512 pool_alloc (rt->pool, rm->max_reass_n);
1515 node = vlib_get_node_by_name (vm, (u8 *) "ip4-full-reassembly-expire-walk");
1517 rm->ip4_full_reass_expire_node_idx = node->index;
1519 ip4_full_reass_set_params (IP4_REASS_TIMEOUT_DEFAULT_MS,
1520 IP4_REASS_MAX_REASSEMBLIES_DEFAULT,
1521 IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1522 IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1524 nbuckets = ip4_full_reass_get_nbuckets ();
1525 clib_bihash_init_16_8 (&rm->hash, "ip4-dr", nbuckets, nbuckets * 1024);
1527 node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop");
1529 rm->ip4_drop_idx = node->index;
1531 rm->fq_index = vlib_frame_queue_main_init (ip4_full_reass_node.index, 0);
1532 rm->fq_feature_index =
1533 vlib_frame_queue_main_init (ip4_full_reass_node_feature.index, 0);
1534 rm->fq_custom_index =
1535 vlib_frame_queue_main_init (ip4_full_reass_node_custom.index, 0);
1537 rm->feature_use_refcount_per_intf = NULL;
1541 VLIB_INIT_FUNCTION (ip4_full_reass_init_function);
1542 #endif /* CLIB_MARCH_VARIANT */
1545 ip4_full_reass_walk_expired (vlib_main_t * vm,
1546 vlib_node_runtime_t * node, vlib_frame_t * f)
1548 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1549 uword event_type, *event_data = 0;
1553 vlib_process_wait_for_event_or_clock (vm,
1555 rm->expire_walk_interval_ms /
1556 (f64) MSEC_PER_SEC);
1557 event_type = vlib_process_get_events (vm, &event_data);
1561 case ~0: /* no events => timeout */
1562 /* nothing to do here */
1564 case IP4_EVENT_CONFIG_CHANGED:
1567 clib_warning ("BUG: event type 0x%wx", event_type);
1570 f64 now = vlib_time_now (vm);
1572 ip4_full_reass_t *reass;
1573 int *pool_indexes_to_free = NULL;
1575 uword thread_index = 0;
1577 const uword nthreads = vlib_num_workers () + 1;
1578 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1580 ip4_full_reass_per_thread_t *rt =
1581 &rm->per_thread_data[thread_index];
1582 clib_spinlock_lock (&rt->lock);
1584 vec_reset_length (pool_indexes_to_free);
1586 pool_foreach_index (index, rt->pool) {
1587 reass = pool_elt_at_index (rt->pool, index);
1588 if (now > reass->last_heard + rm->timeout)
1590 vec_add1 (pool_indexes_to_free, index);
1596 vec_foreach (i, pool_indexes_to_free)
1598 ip4_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1599 ip4_full_reass_drop_all (vm, node, rm, reass);
1600 ip4_full_reass_free (rm, rt, reass);
1604 clib_spinlock_unlock (&rt->lock);
1607 vec_free (pool_indexes_to_free);
1610 _vec_len (event_data) = 0;
1618 VLIB_REGISTER_NODE (ip4_full_reass_expire_node) = {
1619 .function = ip4_full_reass_walk_expired,
1620 .type = VLIB_NODE_TYPE_PROCESS,
1621 .name = "ip4-full-reassembly-expire-walk",
1622 .format_trace = format_ip4_full_reass_trace,
1623 .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1624 .error_strings = ip4_full_reass_error_strings,
1630 format_ip4_full_reass_key (u8 * s, va_list * args)
1632 ip4_full_reass_key_t *key = va_arg (*args, ip4_full_reass_key_t *);
1635 "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1636 key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1637 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1642 format_ip4_reass (u8 * s, va_list * args)
1644 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1645 ip4_full_reass_t *reass = va_arg (*args, ip4_full_reass_t *);
1647 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
1648 "last_packet_octet: %u, trace_op_counter: %u\n",
1649 reass->id, format_ip4_full_reass_key, &reass->key,
1650 reass->first_bi, reass->data_len,
1651 reass->last_packet_octet, reass->trace_op_counter);
1653 u32 bi = reass->first_bi;
1657 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1658 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1661 " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1662 "fragment[%u, %u]\n", counter, vnb->ip.reass.range_first,
1663 vnb->ip.reass.range_last, bi,
1664 ip4_full_reass_buffer_get_data_offset (b),
1665 ip4_full_reass_buffer_get_data_len (b),
1666 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1667 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1669 bi = b->next_buffer;
1679 static clib_error_t *
1680 show_ip4_reass (vlib_main_t * vm,
1681 unformat_input_t * input,
1682 CLIB_UNUSED (vlib_cli_command_t * lmd))
1684 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1686 vlib_cli_output (vm, "---------------------");
1687 vlib_cli_output (vm, "IP4 reassembly status");
1688 vlib_cli_output (vm, "---------------------");
1689 bool details = false;
1690 if (unformat (input, "details"))
1695 u32 sum_reass_n = 0;
1696 ip4_full_reass_t *reass;
1698 const uword nthreads = vlib_num_workers () + 1;
1699 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1701 ip4_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1702 clib_spinlock_lock (&rt->lock);
1706 pool_foreach (reass, rt->pool) {
1707 vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass);
1711 sum_reass_n += rt->reass_n;
1712 clib_spinlock_unlock (&rt->lock);
1714 vlib_cli_output (vm, "---------------------");
1715 vlib_cli_output (vm, "Current full IP4 reassemblies count: %lu\n",
1716 (long unsigned) sum_reass_n);
1717 vlib_cli_output (vm,
1718 "Maximum configured concurrent full IP4 reassemblies per worker-thread: %lu\n",
1719 (long unsigned) rm->max_reass_n);
1720 vlib_cli_output (vm,
1721 "Maximum configured full IP4 reassembly timeout: %lums\n",
1722 (long unsigned) rm->timeout_ms);
1723 vlib_cli_output (vm,
1724 "Maximum configured full IP4 reassembly expire walk interval: %lums\n",
1725 (long unsigned) rm->expire_walk_interval_ms);
1730 VLIB_CLI_COMMAND (show_ip4_full_reass_cmd, static) = {
1731 .path = "show ip4-full-reassembly",
1732 .short_help = "show ip4-full-reassembly [details]",
1733 .function = show_ip4_reass,
1737 #ifndef CLIB_MARCH_VARIANT
1739 ip4_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1741 return vnet_feature_enable_disable ("ip4-unicast",
1742 "ip4-full-reassembly-feature",
1743 sw_if_index, enable_disable, 0, 0);
1745 #endif /* CLIB_MARCH_VARIANT */
1748 #define foreach_ip4_full_reass_handoff_error \
1749 _(CONGESTION_DROP, "congestion drop")
1754 #define _(sym,str) IP4_FULL_REASS_HANDOFF_ERROR_##sym,
1755 foreach_ip4_full_reass_handoff_error
1757 IP4_FULL_REASS_HANDOFF_N_ERROR,
1758 } ip4_full_reass_handoff_error_t;
1760 static char *ip4_full_reass_handoff_error_strings[] = {
1761 #define _(sym,string) string,
1762 foreach_ip4_full_reass_handoff_error
1768 u32 next_worker_index;
1769 } ip4_full_reass_handoff_trace_t;
1772 format_ip4_full_reass_handoff_trace (u8 * s, va_list * args)
1774 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1775 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1776 ip4_full_reass_handoff_trace_t *t =
1777 va_arg (*args, ip4_full_reass_handoff_trace_t *);
1780 format (s, "ip4-full-reassembly-handoff: next-worker %d",
1781 t->next_worker_index);
1787 ip4_full_reass_handoff_node_inline (vlib_main_t * vm,
1788 vlib_node_runtime_t * node,
1789 vlib_frame_t * frame,
1790 ip4_full_reass_node_type_t type)
1792 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1794 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1795 u32 n_enq, n_left_from, *from;
1796 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1799 from = vlib_frame_vector_args (frame);
1800 n_left_from = frame->n_vectors;
1801 vlib_get_buffers (vm, from, bufs, n_left_from);
1804 ti = thread_indices;
1809 fq_index = rm->fq_index;
1812 fq_index = rm->fq_feature_index;
1815 fq_index = rm->fq_custom_index;
1818 clib_warning ("Unexpected `type' (%d)!", type);
1822 while (n_left_from > 0)
1824 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1827 ((node->flags & VLIB_NODE_FLAG_TRACE)
1828 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1830 ip4_full_reass_handoff_trace_t *t =
1831 vlib_add_trace (vm, node, b[0], sizeof (*t));
1832 t->next_worker_index = ti[0];
1840 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1841 frame->n_vectors, 1);
1843 if (n_enq < frame->n_vectors)
1844 vlib_node_increment_counter (vm, node->node_index,
1845 IP4_FULL_REASS_HANDOFF_ERROR_CONGESTION_DROP,
1846 frame->n_vectors - n_enq);
1847 return frame->n_vectors;
1850 VLIB_NODE_FN (ip4_full_reass_handoff_node) (vlib_main_t * vm,
1851 vlib_node_runtime_t * node,
1852 vlib_frame_t * frame)
1854 return ip4_full_reass_handoff_node_inline (vm, node, frame, NORMAL);
1859 VLIB_REGISTER_NODE (ip4_full_reass_handoff_node) = {
1860 .name = "ip4-full-reassembly-handoff",
1861 .vector_size = sizeof (u32),
1862 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
1863 .error_strings = ip4_full_reass_handoff_error_strings,
1864 .format_trace = format_ip4_full_reass_handoff_trace,
1876 VLIB_NODE_FN (ip4_full_reass_feature_handoff_node) (vlib_main_t * vm,
1877 vlib_node_runtime_t *
1879 vlib_frame_t * frame)
1881 return ip4_full_reass_handoff_node_inline (vm, node, frame, FEATURE);
1887 VLIB_REGISTER_NODE (ip4_full_reass_feature_handoff_node) = {
1888 .name = "ip4-full-reass-feature-hoff",
1889 .vector_size = sizeof (u32),
1890 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
1891 .error_strings = ip4_full_reass_handoff_error_strings,
1892 .format_trace = format_ip4_full_reass_handoff_trace,
1903 VLIB_NODE_FN (ip4_full_reass_custom_handoff_node) (vlib_main_t * vm,
1904 vlib_node_runtime_t *
1906 vlib_frame_t * frame)
1908 return ip4_full_reass_handoff_node_inline (vm, node, frame, CUSTOM);
1914 VLIB_REGISTER_NODE (ip4_full_reass_custom_handoff_node) = {
1915 .name = "ip4-full-reass-custom-hoff",
1916 .vector_size = sizeof (u32),
1917 .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
1918 .error_strings = ip4_full_reass_handoff_error_strings,
1919 .format_trace = format_ip4_full_reass_handoff_trace,
1929 #ifndef CLIB_MARCH_VARIANT
1931 ip4_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1933 ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1934 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1937 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1939 ++rm->feature_use_refcount_per_intf[sw_if_index];
1940 return vnet_feature_enable_disable ("ip4-unicast",
1941 "ip4-full-reassembly-feature",
1942 sw_if_index, 1, 0, 0);
1944 ++rm->feature_use_refcount_per_intf[sw_if_index];
1948 --rm->feature_use_refcount_per_intf[sw_if_index];
1949 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1950 return vnet_feature_enable_disable ("ip4-unicast",
1951 "ip4-full-reassembly-feature",
1952 sw_if_index, 0, 0, 0);
1959 * fd.io coding-style-patch-verification: ON
1962 * eval: (c-set-style "gnu")