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 Shallow Virtual Reassembly.
20 * This file contains the source code for IPv4 Shallow Virtual reassembly.
23 #include <vppinfra/vec.h>
24 #include <vnet/vnet.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/ip/ip4_to_ip6.h>
27 #include <vppinfra/fifo.h>
28 #include <vppinfra/bihash_16_8.h>
29 #include <vnet/ip/reass/ip4_sv_reass.h>
31 #define MSEC_PER_SEC 1000
32 #define IP4_SV_REASS_TIMEOUT_DEFAULT_MS 100
33 #define IP4_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
34 #define IP4_SV_REASS_MAX_REASSEMBLIES_DEFAULT 1024
35 #define IP4_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
36 #define IP4_SV_REASS_HT_LOAD_FACTOR (0.75)
41 IP4_SV_REASS_RC_TOO_MANY_FRAGMENTS,
42 IP4_SV_REASS_RC_UNSUPP_IP_PROTO,
79 clib_bihash_kv_16_8_t kv;
85 ip4_sv_reass_key_t key;
86 // time when last packet was received
88 // internal id of this reassembly
90 // trace operation counter
92 // minimum fragment length for this reassembly - used to estimate MTU
93 u16 min_fragment_length;
94 // buffer indexes of buffers in this reassembly in chronological order -
95 // including overlaps and duplicate fragments
97 // set to true when this reassembly is completed
101 u8 icmp_type_or_tcp_flags;
116 ip4_sv_reass_t *pool;
119 clib_spinlock_t lock;
124 } ip4_sv_reass_per_thread_t;
131 u32 expire_walk_interval_ms;
132 // maximum number of fragments in one reassembly
134 // maximum number of reassemblies
138 clib_bihash_16_8_t hash;
140 ip4_sv_reass_per_thread_t *per_thread_data;
143 vlib_main_t *vlib_main;
144 vnet_main_t *vnet_main;
146 // node index of ip4-drop node
148 u32 ip4_sv_reass_expire_node_idx;
150 /** Worker handoff */
152 u32 fq_feature_index;
154 // reference count for enabling/disabling feature - per interface
155 u32 *feature_use_refcount_per_intf;
157 // reference count for enabling/disabling feature - per interface
158 u32 *output_feature_use_refcount_per_intf;
160 } ip4_sv_reass_main_t;
162 extern ip4_sv_reass_main_t ip4_sv_reass_main;
164 #ifndef CLIB_MARCH_VARIANT
165 ip4_sv_reass_main_t ip4_sv_reass_main;
166 #endif /* CLIB_MARCH_VARIANT */
170 IP4_SV_REASSEMBLY_NEXT_INPUT,
171 IP4_SV_REASSEMBLY_NEXT_DROP,
172 IP4_SV_REASSEMBLY_NEXT_HANDOFF,
173 IP4_SV_REASSEMBLY_N_NEXT,
174 } ip4_sv_reass_next_t;
178 REASS_FRAGMENT_CACHE,
180 REASS_FRAGMENT_FORWARD,
182 } ip4_sv_reass_trace_operation_e;
186 ip4_sv_reass_trace_operation_e action;
192 } ip4_sv_reass_trace_t;
194 extern vlib_node_registration_t ip4_sv_reass_node;
195 extern vlib_node_registration_t ip4_sv_reass_node_feature;
198 format_ip4_sv_reass_trace (u8 * s, va_list * args)
200 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
201 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
202 ip4_sv_reass_trace_t *t = va_arg (*args, ip4_sv_reass_trace_t *);
203 if (REASS_PASSTHROUGH != t->action)
205 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
209 case REASS_FRAGMENT_CACHE:
210 s = format (s, "[cached]");
214 format (s, "[finish, ip proto=%u, src_port=%u, dst_port=%u]",
215 t->ip_proto, clib_net_to_host_u16 (t->l4_src_port),
216 clib_net_to_host_u16 (t->l4_dst_port));
218 case REASS_FRAGMENT_FORWARD:
220 format (s, "[forward, ip proto=%u, src_port=%u, dst_port=%u]",
221 t->ip_proto, clib_net_to_host_u16 (t->l4_src_port),
222 clib_net_to_host_u16 (t->l4_dst_port));
224 case REASS_PASSTHROUGH:
225 s = format (s, "[not-fragmented]");
232 ip4_sv_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
233 ip4_sv_reass_main_t * rm, ip4_sv_reass_t * reass,
234 u32 bi, ip4_sv_reass_trace_operation_e action,
235 u32 ip_proto, u16 l4_src_port, u16 l4_dst_port)
237 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
238 ip4_sv_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
241 t->reass_id = reass->id;
242 t->op_id = reass->trace_op_counter;
243 ++reass->trace_op_counter;
246 t->ip_proto = ip_proto;
247 t->l4_src_port = l4_src_port;
248 t->l4_dst_port = l4_dst_port;
251 s = format (s, "%U", format_ip4_sv_reass_trace, NULL, NULL, t);
252 printf ("%.*s\n", vec_len (s), s);
254 vec_reset_length (s);
260 ip4_sv_reass_free (vlib_main_t * vm, ip4_sv_reass_main_t * rm,
261 ip4_sv_reass_per_thread_t * rt, ip4_sv_reass_t * reass)
263 clib_bihash_kv_16_8_t kv;
264 kv.key[0] = reass->key.as_u64[0];
265 kv.key[1] = reass->key.as_u64[1];
266 clib_bihash_add_del_16_8 (&rm->hash, &kv, 0);
267 vlib_buffer_free (vm, reass->cached_buffers,
268 vec_len (reass->cached_buffers));
269 vec_free (reass->cached_buffers);
270 reass->cached_buffers = NULL;
271 if (~0 != reass->lru_prev)
273 ip4_sv_reass_t *lru_prev =
274 pool_elt_at_index (rt->pool, reass->lru_prev);
275 lru_prev->lru_next = reass->lru_next;
277 if (~0 != reass->lru_next)
279 ip4_sv_reass_t *lru_next =
280 pool_elt_at_index (rt->pool, reass->lru_next);
281 lru_next->lru_prev = reass->lru_prev;
283 if (rt->lru_first == reass - rt->pool)
285 rt->lru_first = reass->lru_next;
287 if (rt->lru_last == reass - rt->pool)
289 rt->lru_last = reass->lru_prev;
291 pool_put (rt->pool, reass);
296 ip4_sv_reass_init (ip4_sv_reass_t * reass)
298 reass->cached_buffers = NULL;
299 reass->is_complete = false;
302 always_inline ip4_sv_reass_t *
303 ip4_sv_reass_find_or_create (vlib_main_t * vm, ip4_sv_reass_main_t * rm,
304 ip4_sv_reass_per_thread_t * rt,
305 ip4_sv_reass_kv_t * kv, u8 * do_handoff)
307 ip4_sv_reass_t *reass = NULL;
308 f64 now = vlib_time_now (rm->vlib_main);
310 if (!clib_bihash_search_16_8
311 (&rm->hash, (clib_bihash_kv_16_8_t *) kv, (clib_bihash_kv_16_8_t *) kv))
313 if (vm->thread_index != kv->v.thread_index)
318 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
320 if (now > reass->last_heard + rm->timeout)
322 ip4_sv_reass_free (vm, rm, rt, reass);
329 reass->last_heard = now;
333 if (rt->reass_n >= rm->max_reass_n && rm->max_reass_n)
335 reass = pool_elt_at_index (rt->pool, rt->lru_last);
336 ip4_sv_reass_free (vm, rm, rt, reass);
339 pool_get (rt->pool, reass);
340 clib_memset (reass, 0, sizeof (*reass));
341 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
343 ip4_sv_reass_init (reass);
345 reass->lru_prev = reass->lru_next = ~0;
347 if (~0 != rt->lru_last)
349 ip4_sv_reass_t *lru_last = pool_elt_at_index (rt->pool, rt->lru_last);
350 reass->lru_prev = rt->lru_last;
351 lru_last->lru_next = rt->lru_last = reass - rt->pool;
354 if (~0 == rt->lru_first)
356 rt->lru_first = rt->lru_last = reass - rt->pool;
359 reass->key.as_u64[0] = ((clib_bihash_kv_16_8_t *) kv)->key[0];
360 reass->key.as_u64[1] = ((clib_bihash_kv_16_8_t *) kv)->key[1];
361 kv->v.reass_index = (reass - rt->pool);
362 kv->v.thread_index = vm->thread_index;
363 reass->last_heard = now;
365 if (clib_bihash_add_del_16_8 (&rm->hash, (clib_bihash_kv_16_8_t *) kv, 1))
367 ip4_sv_reass_free (vm, rm, rt, reass);
374 always_inline ip4_sv_reass_rc_t
375 ip4_sv_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
376 ip4_sv_reass_main_t * rm, ip4_sv_reass_per_thread_t * rt,
377 ip4_header_t * ip0, ip4_sv_reass_t * reass, u32 bi0)
379 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
380 ip4_sv_reass_rc_t rc = IP4_SV_REASS_RC_OK;
381 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
382 if (0 == fragment_first)
384 reass->ip_proto = ip0->protocol;
385 reass->l4_src_port = ip4_get_port (ip0, 1);
386 reass->l4_dst_port = ip4_get_port (ip0, 0);
387 if (!reass->l4_src_port || !reass->l4_dst_port)
388 return IP4_SV_REASS_RC_UNSUPP_IP_PROTO;
389 if (IP_PROTOCOL_TCP == reass->ip_proto)
391 reass->icmp_type_or_tcp_flags = ((tcp_header_t *) (ip0 + 1))->flags;
392 reass->tcp_ack_number = ((tcp_header_t *) (ip0 + 1))->ack_number;
393 reass->tcp_seq_number = ((tcp_header_t *) (ip0 + 1))->seq_number;
395 else if (IP_PROTOCOL_ICMP == reass->ip_proto)
397 reass->icmp_type_or_tcp_flags =
398 ((icmp46_header_t *) (ip0 + 1))->type;
400 reass->is_complete = true;
401 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
402 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
404 ip4_sv_reass_add_trace (vm, node, rm, reass, bi0, REASS_FINISH,
405 reass->ip_proto, reass->l4_src_port,
409 vec_add1 (reass->cached_buffers, bi0);
410 if (!reass->is_complete)
412 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
414 ip4_sv_reass_add_trace (vm, node, rm, reass, bi0,
415 REASS_FRAGMENT_CACHE, ~0, ~0, ~0);
417 if (vec_len (reass->cached_buffers) > rm->max_reass_len)
419 rc = IP4_SV_REASS_RC_TOO_MANY_FRAGMENTS;
426 ip4_sv_reass_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
427 vlib_frame_t * frame, bool is_feature,
428 bool is_output_feature, bool is_custom)
430 u32 *from = vlib_frame_vector_args (frame);
431 u32 n_left_from, n_left_to_next, *to_next, next_index;
432 ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
433 ip4_sv_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
434 clib_spinlock_lock (&rt->lock);
436 n_left_from = frame->n_vectors;
437 next_index = node->cached_next_index;
439 while (n_left_from > 0)
441 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
443 while (n_left_from > 0 && n_left_to_next > 0)
448 u32 error0 = IP4_ERROR_NONE;
451 b0 = vlib_get_buffer (vm, bi0);
454 (ip4_header_t *) u8_ptr_add (vlib_buffer_get_current (b0),
457 ip.save_rewrite_length);
458 if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
460 // this is a regular packet - no fragmentation
463 next0 = vnet_buffer (b0)->ip.reass.next_index;
467 next0 = IP4_SV_REASSEMBLY_NEXT_INPUT;
469 vnet_buffer (b0)->ip.reass.is_non_first_fragment = 0;
470 vnet_buffer (b0)->ip.reass.ip_proto = ip0->protocol;
471 if (IP_PROTOCOL_TCP == ip0->protocol)
473 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
474 ((tcp_header_t *) (ip0 + 1))->flags;
475 vnet_buffer (b0)->ip.reass.tcp_ack_number =
476 ((tcp_header_t *) (ip0 + 1))->ack_number;
477 vnet_buffer (b0)->ip.reass.tcp_seq_number =
478 ((tcp_header_t *) (ip0 + 1))->seq_number;
480 else if (IP_PROTOCOL_ICMP == ip0->protocol)
482 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
483 ((icmp46_header_t *) (ip0 + 1))->type;
485 vnet_buffer (b0)->ip.reass.l4_src_port = ip4_get_port (ip0, 1);
486 vnet_buffer (b0)->ip.reass.l4_dst_port = ip4_get_port (ip0, 0);
487 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
489 ip4_sv_reass_add_trace (vm, node, rm, NULL, bi0,
491 vnet_buffer (b0)->ip.reass.ip_proto,
492 vnet_buffer (b0)->ip.
494 vnet_buffer (b0)->ip.
499 const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
500 const u32 fragment_length =
501 clib_net_to_host_u16 (ip0->length) - ip4_header_bytes (ip0);
502 const u32 fragment_last = fragment_first + fragment_length - 1;
503 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
505 next0 = IP4_SV_REASSEMBLY_NEXT_DROP;
506 error0 = IP4_ERROR_REASS_MALFORMED_PACKET;
509 ip4_sv_reass_kv_t kv;
513 (u64) vec_elt (ip4_main.fib_index_by_sw_if_index,
514 vnet_buffer (b0)->sw_if_index[VLIB_RX]) |
515 (u64) ip0->src_address.as_u32 << 32;
517 (u64) ip0->dst_address.
518 as_u32 | (u64) ip0->fragment_id << 32 | (u64) ip0->protocol << 48;
520 ip4_sv_reass_t *reass =
521 ip4_sv_reass_find_or_create (vm, rm, rt, &kv, &do_handoff);
523 if (PREDICT_FALSE (do_handoff))
525 next0 = IP4_SV_REASSEMBLY_NEXT_HANDOFF;
526 vnet_buffer (b0)->ip.reass.owner_thread_index =
533 next0 = IP4_SV_REASSEMBLY_NEXT_DROP;
534 error0 = IP4_ERROR_REASS_LIMIT_REACHED;
538 if (reass->is_complete)
542 next0 = vnet_buffer (b0)->ip.reass.next_index;
546 next0 = IP4_SV_REASSEMBLY_NEXT_INPUT;
548 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
550 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
551 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
552 reass->icmp_type_or_tcp_flags;
553 vnet_buffer (b0)->ip.reass.tcp_ack_number =
554 reass->tcp_ack_number;
555 vnet_buffer (b0)->ip.reass.tcp_seq_number =
556 reass->tcp_seq_number;
557 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
558 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
559 error0 = IP4_ERROR_NONE;
560 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
562 ip4_sv_reass_add_trace (vm, node, rm, reass, bi0,
563 REASS_FRAGMENT_FORWARD,
571 ip4_sv_reass_rc_t rc =
572 ip4_sv_reass_update (vm, node, rm, rt, ip0, reass, bi0);
575 case IP4_SV_REASS_RC_OK:
576 /* nothing to do here */
578 case IP4_SV_REASS_RC_TOO_MANY_FRAGMENTS:
579 vlib_node_increment_counter (vm, node->node_index,
580 IP4_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
582 ip4_sv_reass_free (vm, rm, rt, reass);
585 case IP4_SV_REASS_RC_UNSUPP_IP_PROTO:
586 vlib_node_increment_counter (vm, node->node_index,
587 IP4_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
589 ip4_sv_reass_free (vm, rm, rt, reass);
593 if (reass->is_complete)
596 vec_foreach_index (idx, reass->cached_buffers)
598 u32 bi0 = vec_elt (reass->cached_buffers, idx);
599 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
600 u32 next0 = IP4_SV_REASSEMBLY_NEXT_INPUT;
603 vnet_feature_next (&next0, b0);
607 next0 = vnet_buffer (b0)->ip.reass.next_index;
609 if (0 == n_left_to_next)
611 vlib_put_next_frame (vm, node, next_index,
613 vlib_get_next_frame (vm, node, next_index, to_next,
619 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
620 ! !ip4_get_fragment_offset (vlib_buffer_get_current (b0));
621 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
622 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
623 reass->icmp_type_or_tcp_flags;
624 vnet_buffer (b0)->ip.reass.tcp_ack_number =
625 reass->tcp_ack_number;
626 vnet_buffer (b0)->ip.reass.tcp_seq_number =
627 reass->tcp_seq_number;
628 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
629 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
630 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
632 ip4_sv_reass_add_trace (vm, node, rm, reass, bi0,
633 REASS_FRAGMENT_FORWARD,
638 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
639 to_next, n_left_to_next, bi0,
642 _vec_len (reass->cached_buffers) = 0; // buffers are owned by frame now
647 b0->error = node->errors[error0];
652 if (is_feature && IP4_ERROR_NONE == error0)
654 b0 = vlib_get_buffer (vm, bi0);
655 vnet_feature_next (&next0, b0);
657 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
658 to_next, n_left_to_next,
666 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
669 clib_spinlock_unlock (&rt->lock);
670 return frame->n_vectors;
673 static char *ip4_sv_reass_error_strings[] = {
674 #define _(sym, string) string,
679 VLIB_NODE_FN (ip4_sv_reass_node) (vlib_main_t * vm,
680 vlib_node_runtime_t * node,
681 vlib_frame_t * frame)
683 return ip4_sv_reass_inline (vm, node, frame, false /* is_feature */ ,
684 false /* is_output_feature */ ,
685 false /* is_custom */ );
689 VLIB_REGISTER_NODE (ip4_sv_reass_node) = {
690 .name = "ip4-sv-reassembly",
691 .vector_size = sizeof (u32),
692 .format_trace = format_ip4_sv_reass_trace,
693 .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
694 .error_strings = ip4_sv_reass_error_strings,
695 .n_next_nodes = IP4_SV_REASSEMBLY_N_NEXT,
698 [IP4_SV_REASSEMBLY_NEXT_INPUT] = "ip4-input",
699 [IP4_SV_REASSEMBLY_NEXT_DROP] = "ip4-drop",
700 [IP4_SV_REASSEMBLY_NEXT_HANDOFF] = "ip4-sv-reassembly-handoff",
706 VLIB_NODE_FN (ip4_sv_reass_node_feature) (vlib_main_t * vm,
707 vlib_node_runtime_t * node,
708 vlib_frame_t * frame)
710 return ip4_sv_reass_inline (vm, node, frame, true /* is_feature */ ,
711 false /* is_output_feature */ ,
712 false /* is_custom */ );
716 VLIB_REGISTER_NODE (ip4_sv_reass_node_feature) = {
717 .name = "ip4-sv-reassembly-feature",
718 .vector_size = sizeof (u32),
719 .format_trace = format_ip4_sv_reass_trace,
720 .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
721 .error_strings = ip4_sv_reass_error_strings,
722 .n_next_nodes = IP4_SV_REASSEMBLY_N_NEXT,
725 [IP4_SV_REASSEMBLY_NEXT_INPUT] = "ip4-input",
726 [IP4_SV_REASSEMBLY_NEXT_DROP] = "ip4-drop",
727 [IP4_SV_REASSEMBLY_NEXT_HANDOFF] = "ip4-sv-reass-feature-hoff",
733 VNET_FEATURE_INIT (ip4_sv_reass_feature) = {
734 .arc_name = "ip4-unicast",
735 .node_name = "ip4-sv-reassembly-feature",
736 .runs_before = VNET_FEATURES ("ip4-lookup"),
741 VLIB_NODE_FN (ip4_sv_reass_node_output_feature) (vlib_main_t * vm,
742 vlib_node_runtime_t * node,
743 vlib_frame_t * frame)
745 return ip4_sv_reass_inline (vm, node, frame, true /* is_feature */ ,
746 true /* is_output_feature */ ,
747 false /* is_custom */ );
752 VLIB_REGISTER_NODE (ip4_sv_reass_node_output_feature) = {
753 .name = "ip4-sv-reassembly-output-feature",
754 .vector_size = sizeof (u32),
755 .format_trace = format_ip4_sv_reass_trace,
756 .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
757 .error_strings = ip4_sv_reass_error_strings,
758 .n_next_nodes = IP4_SV_REASSEMBLY_N_NEXT,
761 [IP4_SV_REASSEMBLY_NEXT_INPUT] = "ip4-input",
762 [IP4_SV_REASSEMBLY_NEXT_DROP] = "ip4-drop",
763 [IP4_SV_REASSEMBLY_NEXT_HANDOFF] = "ip4-sv-reass-feature-hoff",
769 VNET_FEATURE_INIT (ip4_sv_reass_output_feature) = {
770 .arc_name = "ip4-output",
771 .node_name = "ip4-sv-reassembly-output-feature",
778 VLIB_REGISTER_NODE (ip4_sv_reass_custom_node) = {
779 .name = "ip4-sv-reassembly-custom-next",
780 .vector_size = sizeof (u32),
781 .format_trace = format_ip4_sv_reass_trace,
782 .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
783 .error_strings = ip4_sv_reass_error_strings,
784 .n_next_nodes = IP4_SV_REASSEMBLY_N_NEXT,
787 [IP4_SV_REASSEMBLY_NEXT_INPUT] = "ip4-input",
788 [IP4_SV_REASSEMBLY_NEXT_DROP] = "ip4-drop",
789 [IP4_SV_REASSEMBLY_NEXT_HANDOFF] = "ip4-sv-reassembly-handoff",
795 VLIB_NODE_FN (ip4_sv_reass_custom_node) (vlib_main_t * vm,
796 vlib_node_runtime_t * node,
797 vlib_frame_t * frame)
799 return ip4_sv_reass_inline (vm, node, frame, false /* is_feature */ ,
800 false /* is_output_feature */ ,
801 true /* is_custom */ );
804 #ifndef CLIB_MARCH_VARIANT
806 ip4_sv_reass_get_nbuckets ()
808 ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
812 nbuckets = (u32) (rm->max_reass_n / IP4_SV_REASS_HT_LOAD_FACTOR);
814 for (i = 0; i < 31; i++)
815 if ((1 << i) >= nbuckets)
821 #endif /* CLIB_MARCH_VARIANT */
825 IP4_EVENT_CONFIG_CHANGED = 1,
826 } ip4_sv_reass_event_t;
831 clib_bihash_16_8_t *new_hash;
834 #ifndef CLIB_MARCH_VARIANT
836 ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
838 ip4_rehash_cb_ctx *ctx = _ctx;
839 if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
843 return (BIHASH_WALK_CONTINUE);
847 ip4_sv_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
848 u32 max_reassembly_length,
849 u32 expire_walk_interval_ms)
851 ip4_sv_reass_main.timeout_ms = timeout_ms;
852 ip4_sv_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
853 ip4_sv_reass_main.max_reass_n = max_reassemblies;
854 ip4_sv_reass_main.max_reass_len = max_reassembly_length;
855 ip4_sv_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
859 ip4_sv_reass_set (u32 timeout_ms, u32 max_reassemblies,
860 u32 max_reassembly_length, u32 expire_walk_interval_ms)
862 u32 old_nbuckets = ip4_sv_reass_get_nbuckets ();
863 ip4_sv_reass_set_params (timeout_ms, max_reassemblies,
864 max_reassembly_length, expire_walk_interval_ms);
865 vlib_process_signal_event (ip4_sv_reass_main.vlib_main,
866 ip4_sv_reass_main.ip4_sv_reass_expire_node_idx,
867 IP4_EVENT_CONFIG_CHANGED, 0);
868 u32 new_nbuckets = ip4_sv_reass_get_nbuckets ();
869 if (ip4_sv_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
871 clib_bihash_16_8_t new_hash;
872 clib_memset (&new_hash, 0, sizeof (new_hash));
873 ip4_rehash_cb_ctx ctx;
875 ctx.new_hash = &new_hash;
876 clib_bihash_init_16_8 (&new_hash, "ip4-dr", new_nbuckets,
877 new_nbuckets * 1024);
878 clib_bihash_foreach_key_value_pair_16_8 (&ip4_sv_reass_main.hash,
879 ip4_rehash_cb, &ctx);
882 clib_bihash_free_16_8 (&new_hash);
887 clib_bihash_free_16_8 (&ip4_sv_reass_main.hash);
888 clib_memcpy_fast (&ip4_sv_reass_main.hash, &new_hash,
889 sizeof (ip4_sv_reass_main.hash));
890 clib_bihash_copied (&ip4_sv_reass_main.hash, &new_hash);
897 ip4_sv_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
898 u32 * max_reassembly_length, u32 * expire_walk_interval_ms)
900 *timeout_ms = ip4_sv_reass_main.timeout_ms;
901 *max_reassemblies = ip4_sv_reass_main.max_reass_n;
902 *max_reassembly_length = ip4_sv_reass_main.max_reass_len;
903 *expire_walk_interval_ms = ip4_sv_reass_main.expire_walk_interval_ms;
907 static clib_error_t *
908 ip4_sv_reass_init_function (vlib_main_t * vm)
910 ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
911 clib_error_t *error = 0;
916 rm->vnet_main = vnet_get_main ();
918 vec_validate (rm->per_thread_data, vlib_num_workers ());
919 ip4_sv_reass_per_thread_t *rt;
920 vec_foreach (rt, rm->per_thread_data)
922 clib_spinlock_init (&rt->lock);
923 pool_alloc (rt->pool, rm->max_reass_n);
924 rt->lru_first = rt->lru_last = ~0;
927 node = vlib_get_node_by_name (vm, (u8 *) "ip4-sv-reassembly-expire-walk");
929 rm->ip4_sv_reass_expire_node_idx = node->index;
931 ip4_sv_reass_set_params (IP4_SV_REASS_TIMEOUT_DEFAULT_MS,
932 IP4_SV_REASS_MAX_REASSEMBLIES_DEFAULT,
933 IP4_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
934 IP4_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
936 nbuckets = ip4_sv_reass_get_nbuckets ();
937 clib_bihash_init_16_8 (&rm->hash, "ip4-dr", nbuckets, nbuckets * 1024);
939 node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop");
941 rm->ip4_drop_idx = node->index;
943 rm->fq_index = vlib_frame_queue_main_init (ip4_sv_reass_node.index, 0);
944 rm->fq_feature_index =
945 vlib_frame_queue_main_init (ip4_sv_reass_node_feature.index, 0);
947 rm->feature_use_refcount_per_intf = NULL;
948 rm->output_feature_use_refcount_per_intf = NULL;
953 VLIB_INIT_FUNCTION (ip4_sv_reass_init_function);
954 #endif /* CLIB_MARCH_VARIANT */
957 ip4_sv_reass_walk_expired (vlib_main_t * vm,
958 vlib_node_runtime_t * node, vlib_frame_t * f)
960 ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
961 uword event_type, *event_data = 0;
965 vlib_process_wait_for_event_or_clock (vm,
967 rm->expire_walk_interval_ms /
969 event_type = vlib_process_get_events (vm, &event_data);
973 case ~0: /* no events => timeout */
974 /* nothing to do here */
976 case IP4_EVENT_CONFIG_CHANGED:
979 clib_warning ("BUG: event type 0x%wx", event_type);
982 f64 now = vlib_time_now (vm);
984 ip4_sv_reass_t *reass;
985 int *pool_indexes_to_free = NULL;
987 uword thread_index = 0;
989 const uword nthreads = vlib_num_workers () + 1;
990 for (thread_index = 0; thread_index < nthreads; ++thread_index)
992 ip4_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
993 clib_spinlock_lock (&rt->lock);
995 vec_reset_length (pool_indexes_to_free);
997 pool_foreach_index (index, rt->pool, ({
998 reass = pool_elt_at_index (rt->pool, index);
999 if (now > reass->last_heard + rm->timeout)
1001 vec_add1 (pool_indexes_to_free, index);
1007 vec_foreach (i, pool_indexes_to_free)
1009 ip4_sv_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1010 ip4_sv_reass_free (vm, rm, rt, reass);
1014 clib_spinlock_unlock (&rt->lock);
1017 vec_free (pool_indexes_to_free);
1020 _vec_len (event_data) = 0;
1028 VLIB_REGISTER_NODE (ip4_sv_reass_expire_node) = {
1029 .function = ip4_sv_reass_walk_expired,
1030 .type = VLIB_NODE_TYPE_PROCESS,
1031 .name = "ip4-sv-reassembly-expire-walk",
1032 .format_trace = format_ip4_sv_reass_trace,
1033 .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
1034 .error_strings = ip4_sv_reass_error_strings,
1040 format_ip4_sv_reass_key (u8 * s, va_list * args)
1042 ip4_sv_reass_key_t *key = va_arg (*args, ip4_sv_reass_key_t *);
1045 "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1046 key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1047 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1052 format_ip4_sv_reass (u8 * s, va_list * args)
1054 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1055 ip4_sv_reass_t *reass = va_arg (*args, ip4_sv_reass_t *);
1057 s = format (s, "ID: %lu, key: %U trace_op_counter: %u\n",
1058 reass->id, format_ip4_sv_reass_key, &reass->key,
1059 reass->trace_op_counter);
1064 vec_foreach (bip, reass->cached_buffers)
1069 b = vlib_get_buffer (vm, bi);
1070 s = format (s, " #%03u: bi: %u, ", counter, bi);
1072 bi = b->next_buffer;
1074 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
1079 static clib_error_t *
1080 show_ip4_reass (vlib_main_t * vm,
1081 unformat_input_t * input,
1082 CLIB_UNUSED (vlib_cli_command_t * lmd))
1084 ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
1086 vlib_cli_output (vm, "---------------------");
1087 vlib_cli_output (vm, "IP4 reassembly status");
1088 vlib_cli_output (vm, "---------------------");
1089 bool details = false;
1090 if (unformat (input, "details"))
1095 u32 sum_reass_n = 0;
1096 ip4_sv_reass_t *reass;
1098 const uword nthreads = vlib_num_workers () + 1;
1099 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1101 ip4_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1102 clib_spinlock_lock (&rt->lock);
1106 pool_foreach (reass, rt->pool, {
1107 vlib_cli_output (vm, "%U", format_ip4_sv_reass, vm, reass);
1111 sum_reass_n += rt->reass_n;
1112 clib_spinlock_unlock (&rt->lock);
1114 vlib_cli_output (vm, "---------------------");
1115 vlib_cli_output (vm, "Current IP4 reassemblies count: %lu\n",
1116 (long unsigned) sum_reass_n);
1117 vlib_cli_output (vm,
1118 "Maximum configured concurrent shallow virtual IP4 reassemblies per worker-thread: %lu\n",
1119 (long unsigned) rm->max_reass_n);
1120 vlib_cli_output (vm,
1121 "Maximum configured shallow virtual IP4 reassembly timeout: %lums\n",
1122 (long unsigned) rm->timeout_ms);
1123 vlib_cli_output (vm,
1124 "Maximum configured shallow virtual IP4 reassembly expire walk interval: %lums\n",
1125 (long unsigned) rm->expire_walk_interval_ms);
1130 VLIB_CLI_COMMAND (show_ip4_sv_reass_cmd, static) = {
1131 .path = "show ip4-sv-reassembly",
1132 .short_help = "show ip4-sv-reassembly [details]",
1133 .function = show_ip4_reass,
1137 #ifndef CLIB_MARCH_VARIANT
1139 ip4_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1141 return ip4_sv_reass_enable_disable_with_refcnt (sw_if_index,
1144 #endif /* CLIB_MARCH_VARIANT */
1147 #define foreach_ip4_sv_reass_handoff_error \
1148 _(CONGESTION_DROP, "congestion drop")
1153 #define _(sym,str) IP4_SV_REASSEMBLY_HANDOFF_ERROR_##sym,
1154 foreach_ip4_sv_reass_handoff_error
1156 IP4_SV_REASSEMBLY_HANDOFF_N_ERROR,
1157 } ip4_sv_reass_handoff_error_t;
1159 static char *ip4_sv_reass_handoff_error_strings[] = {
1160 #define _(sym,string) string,
1161 foreach_ip4_sv_reass_handoff_error
1167 u32 next_worker_index;
1168 } ip4_sv_reass_handoff_trace_t;
1171 format_ip4_sv_reass_handoff_trace (u8 * s, va_list * args)
1173 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1174 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1175 ip4_sv_reass_handoff_trace_t *t =
1176 va_arg (*args, ip4_sv_reass_handoff_trace_t *);
1179 format (s, "ip4-sv-reassembly-handoff: next-worker %d",
1180 t->next_worker_index);
1186 ip4_sv_reass_handoff_node_inline (vlib_main_t * vm,
1187 vlib_node_runtime_t * node,
1188 vlib_frame_t * frame, bool is_feature)
1190 ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
1192 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1193 u32 n_enq, n_left_from, *from;
1194 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1197 from = vlib_frame_vector_args (frame);
1198 n_left_from = frame->n_vectors;
1199 vlib_get_buffers (vm, from, bufs, n_left_from);
1202 ti = thread_indices;
1204 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1206 while (n_left_from > 0)
1208 ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1211 ((node->flags & VLIB_NODE_FLAG_TRACE)
1212 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1214 ip4_sv_reass_handoff_trace_t *t =
1215 vlib_add_trace (vm, node, b[0], sizeof (*t));
1216 t->next_worker_index = ti[0];
1224 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1225 frame->n_vectors, 1);
1227 if (n_enq < frame->n_vectors)
1228 vlib_node_increment_counter (vm, node->node_index,
1229 IP4_SV_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1230 frame->n_vectors - n_enq);
1231 return frame->n_vectors;
1234 VLIB_NODE_FN (ip4_sv_reass_handoff_node) (vlib_main_t * vm,
1235 vlib_node_runtime_t * node,
1236 vlib_frame_t * frame)
1238 return ip4_sv_reass_handoff_node_inline (vm, node, frame,
1239 false /* is_feature */ );
1244 VLIB_REGISTER_NODE (ip4_sv_reass_handoff_node) = {
1245 .name = "ip4-sv-reassembly-handoff",
1246 .vector_size = sizeof (u32),
1247 .n_errors = ARRAY_LEN(ip4_sv_reass_handoff_error_strings),
1248 .error_strings = ip4_sv_reass_handoff_error_strings,
1249 .format_trace = format_ip4_sv_reass_handoff_trace,
1261 VLIB_NODE_FN (ip4_sv_reass_feature_handoff_node) (vlib_main_t * vm,
1262 vlib_node_runtime_t *
1264 vlib_frame_t * frame)
1266 return ip4_sv_reass_handoff_node_inline (vm, node, frame,
1267 true /* is_feature */ );
1273 VLIB_REGISTER_NODE (ip4_sv_reass_feature_handoff_node) = {
1274 .name = "ip4-sv-reass-feature-hoff",
1275 .vector_size = sizeof (u32),
1276 .n_errors = ARRAY_LEN(ip4_sv_reass_handoff_error_strings),
1277 .error_strings = ip4_sv_reass_handoff_error_strings,
1278 .format_trace = format_ip4_sv_reass_handoff_trace,
1288 #ifndef CLIB_MARCH_VARIANT
1290 ip4_sv_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1292 ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
1293 vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1296 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1298 ++rm->feature_use_refcount_per_intf[sw_if_index];
1299 return vnet_feature_enable_disable ("ip4-unicast",
1300 "ip4-sv-reassembly-feature",
1301 sw_if_index, 1, 0, 0);
1303 ++rm->feature_use_refcount_per_intf[sw_if_index];
1307 if (rm->feature_use_refcount_per_intf[sw_if_index])
1308 --rm->feature_use_refcount_per_intf[sw_if_index];
1309 if (!rm->feature_use_refcount_per_intf[sw_if_index])
1310 return vnet_feature_enable_disable ("ip4-unicast",
1311 "ip4-sv-reassembly-feature",
1312 sw_if_index, 0, 0, 0);
1318 ip4_sv_reass_custom_register_next_node (uword node_index)
1320 return vlib_node_add_next (vlib_get_main (), ip4_sv_reass_custom_node.index,
1325 ip4_sv_reass_output_enable_disable_with_refcnt (u32 sw_if_index,
1328 ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
1329 vec_validate (rm->output_feature_use_refcount_per_intf, sw_if_index);
1332 if (!rm->output_feature_use_refcount_per_intf[sw_if_index])
1334 ++rm->output_feature_use_refcount_per_intf[sw_if_index];
1335 return vnet_feature_enable_disable ("ip4-output",
1336 "ip4-sv-reassembly-output-feature",
1337 sw_if_index, 1, 0, 0);
1339 ++rm->output_feature_use_refcount_per_intf[sw_if_index];
1343 if (rm->output_feature_use_refcount_per_intf[sw_if_index])
1344 --rm->output_feature_use_refcount_per_intf[sw_if_index];
1345 if (!rm->output_feature_use_refcount_per_intf[sw_if_index])
1346 return vnet_feature_enable_disable ("ip4-output",
1347 "ip4-sv-reassembly-output-feature",
1348 sw_if_index, 0, 0, 0);
1355 * fd.io coding-style-patch-verification: ON
1358 * eval: (c-set-style "gnu")