9b6116c9778055f8f900e58d06efba64321e6e19
[vpp.git] / src / vnet / ip / reass / ip4_sv_reass.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 /**
17  * @file
18  * @brief IPv4 Shallow Virtual Reassembly.
19  *
20  * This file contains the source code for IPv4 Shallow Virtual reassembly.
21  */
22
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>
30
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)
37
38 typedef enum
39 {
40   IP4_SV_REASS_RC_OK,
41   IP4_SV_REASS_RC_TOO_MANY_FRAGMENTS,
42   IP4_SV_REASS_RC_UNSUPP_IP_PROTO,
43 } ip4_sv_reass_rc_t;
44
45 typedef struct
46 {
47   union
48   {
49     struct
50     {
51       u32 xx_id;
52       ip4_address_t src;
53       ip4_address_t dst;
54       u16 frag_id;
55       u8 proto;
56       u8 unused;
57     };
58     u64 as_u64[2];
59   };
60 } ip4_sv_reass_key_t;
61
62 typedef union
63 {
64   struct
65   {
66     u32 reass_index;
67     u32 thread_index;
68   };
69   u64 as_u64;
70 } ip4_sv_reass_val_t;
71
72 typedef union
73 {
74   struct
75   {
76     ip4_sv_reass_key_t k;
77     ip4_sv_reass_val_t v;
78   };
79   clib_bihash_kv_16_8_t kv;
80 } ip4_sv_reass_kv_t;
81
82 typedef struct
83 {
84   // hash table key
85   ip4_sv_reass_key_t key;
86   // time when last packet was received
87   f64 last_heard;
88   // internal id of this reassembly
89   u64 id;
90   // trace operation counter
91   u32 trace_op_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
96   u32 *cached_buffers;
97   // set to true when this reassembly is completed
98   bool is_complete;
99   // ip protocol
100   u8 ip_proto;
101   u8 icmp_type_or_tcp_flags;
102   u32 tcp_ack_number;
103   u32 tcp_seq_number;
104   // l4 src port
105   u16 l4_src_port;
106   // l4 dst port
107   u16 l4_dst_port;
108   u32 next_index;
109   // lru indexes
110   u32 lru_prev;
111   u32 lru_next;
112 } ip4_sv_reass_t;
113
114 typedef struct
115 {
116   ip4_sv_reass_t *pool;
117   u32 reass_n;
118   u32 id_counter;
119   clib_spinlock_t lock;
120   // lru indexes
121   u32 lru_first;
122   u32 lru_last;
123
124 } ip4_sv_reass_per_thread_t;
125
126 typedef struct
127 {
128   // IPv4 config
129   u32 timeout_ms;
130   f64 timeout;
131   u32 expire_walk_interval_ms;
132   // maximum number of fragments in one reassembly
133   u32 max_reass_len;
134   // maximum number of reassemblies
135   u32 max_reass_n;
136
137   // IPv4 runtime
138   clib_bihash_16_8_t hash;
139   // per-thread data
140   ip4_sv_reass_per_thread_t *per_thread_data;
141
142   // convenience
143   vlib_main_t *vlib_main;
144   vnet_main_t *vnet_main;
145
146   // node index of ip4-drop node
147   u32 ip4_drop_idx;
148   u32 ip4_sv_reass_expire_node_idx;
149
150   /** Worker handoff */
151   u32 fq_index;
152   u32 fq_feature_index;
153
154   // reference count for enabling/disabling feature - per interface
155   u32 *feature_use_refcount_per_intf;
156
157   // reference count for enabling/disabling feature - per interface
158   u32 *output_feature_use_refcount_per_intf;
159
160 } ip4_sv_reass_main_t;
161
162 extern ip4_sv_reass_main_t ip4_sv_reass_main;
163
164 #ifndef CLIB_MARCH_VARIANT
165 ip4_sv_reass_main_t ip4_sv_reass_main;
166 #endif /* CLIB_MARCH_VARIANT */
167
168 typedef enum
169 {
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;
175
176 typedef enum
177 {
178   REASS_FRAGMENT_CACHE,
179   REASS_FINISH,
180   REASS_FRAGMENT_FORWARD,
181   REASS_PASSTHROUGH,
182 } ip4_sv_reass_trace_operation_e;
183
184 typedef struct
185 {
186   ip4_sv_reass_trace_operation_e action;
187   u32 reass_id;
188   u32 op_id;
189   u8 ip_proto;
190   u16 l4_src_port;
191   u16 l4_dst_port;
192 } ip4_sv_reass_trace_t;
193
194 extern vlib_node_registration_t ip4_sv_reass_node;
195 extern vlib_node_registration_t ip4_sv_reass_node_feature;
196
197 static u8 *
198 format_ip4_sv_reass_trace (u8 * s, va_list * args)
199 {
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)
204     {
205       s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
206     }
207   switch (t->action)
208     {
209     case REASS_FRAGMENT_CACHE:
210       s = format (s, "[cached]");
211       break;
212     case REASS_FINISH:
213       s =
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));
217       break;
218     case REASS_FRAGMENT_FORWARD:
219       s =
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));
223       break;
224     case REASS_PASSTHROUGH:
225       s = format (s, "[not-fragmented]");
226       break;
227     }
228   return s;
229 }
230
231 static void
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)
236 {
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]));
239   if (reass)
240     {
241       t->reass_id = reass->id;
242       t->op_id = reass->trace_op_counter;
243       ++reass->trace_op_counter;
244     }
245   t->action = action;
246   t->ip_proto = ip_proto;
247   t->l4_src_port = l4_src_port;
248   t->l4_dst_port = l4_dst_port;
249 #if 0
250   static u8 *s = NULL;
251   s = format (s, "%U", format_ip4_sv_reass_trace, NULL, NULL, t);
252   printf ("%.*s\n", vec_len (s), s);
253   fflush (stdout);
254   vec_reset_length (s);
255 #endif
256 }
257
258
259 always_inline void
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)
262 {
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)
272     {
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;
276     }
277   if (~0 != reass->lru_next)
278     {
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;
282     }
283   if (rt->lru_first == reass - rt->pool)
284     {
285       rt->lru_first = reass->lru_next;
286     }
287   if (rt->lru_last == reass - rt->pool)
288     {
289       rt->lru_last = reass->lru_prev;
290     }
291   pool_put (rt->pool, reass);
292   --rt->reass_n;
293 }
294
295 always_inline void
296 ip4_sv_reass_init (ip4_sv_reass_t * reass)
297 {
298   reass->cached_buffers = NULL;
299   reass->is_complete = false;
300 }
301
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)
306 {
307   ip4_sv_reass_t *reass = NULL;
308   f64 now = vlib_time_now (rm->vlib_main);
309
310   if (!clib_bihash_search_16_8
311       (&rm->hash, (clib_bihash_kv_16_8_t *) kv, (clib_bihash_kv_16_8_t *) kv))
312     {
313       if (vm->thread_index != kv->v.thread_index)
314         {
315           *do_handoff = 1;
316           return NULL;
317         }
318       reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
319
320       if (now > reass->last_heard + rm->timeout)
321         {
322           ip4_sv_reass_free (vm, rm, rt, reass);
323           reass = NULL;
324         }
325     }
326
327   if (reass)
328     {
329       reass->last_heard = now;
330       return reass;
331     }
332
333   if (rt->reass_n >= rm->max_reass_n && rm->max_reass_n)
334     {
335       reass = pool_elt_at_index (rt->pool, rt->lru_last);
336       ip4_sv_reass_free (vm, rm, rt, reass);
337     }
338
339   pool_get (rt->pool, reass);
340   clib_memset (reass, 0, sizeof (*reass));
341   reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
342   ++rt->id_counter;
343   ip4_sv_reass_init (reass);
344   ++rt->reass_n;
345   reass->lru_prev = reass->lru_next = ~0;
346
347   if (~0 != rt->lru_last)
348     {
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;
352     }
353
354   if (~0 == rt->lru_first)
355     {
356       rt->lru_first = rt->lru_last = reass - rt->pool;
357     }
358
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;
364
365   if (clib_bihash_add_del_16_8 (&rm->hash, (clib_bihash_kv_16_8_t *) kv, 1))
366     {
367       ip4_sv_reass_free (vm, rm, rt, reass);
368       reass = NULL;
369     }
370
371   return reass;
372 }
373
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)
378 {
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)
383     {
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)
390         {
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;
394         }
395       else if (IP_PROTOCOL_ICMP == reass->ip_proto)
396         {
397           reass->icmp_type_or_tcp_flags =
398             ((icmp46_header_t *) (ip0 + 1))->type;
399         }
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))
403         {
404           ip4_sv_reass_add_trace (vm, node, rm, reass, bi0, REASS_FINISH,
405                                   reass->ip_proto, reass->l4_src_port,
406                                   reass->l4_dst_port);
407         }
408     }
409   vec_add1 (reass->cached_buffers, bi0);
410   if (!reass->is_complete)
411     {
412       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
413         {
414           ip4_sv_reass_add_trace (vm, node, rm, reass, bi0,
415                                   REASS_FRAGMENT_CACHE, ~0, ~0, ~0);
416         }
417       if (vec_len (reass->cached_buffers) > rm->max_reass_len)
418         {
419           rc = IP4_SV_REASS_RC_TOO_MANY_FRAGMENTS;
420         }
421     }
422   return rc;
423 }
424
425 always_inline uword
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)
429 {
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);
435
436   n_left_from = frame->n_vectors;
437   next_index = node->cached_next_index;
438
439   while (n_left_from > 0)
440     {
441       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
442
443       while (n_left_from > 0 && n_left_to_next > 0)
444         {
445           u32 bi0;
446           vlib_buffer_t *b0;
447           u32 next0;
448           u32 error0 = IP4_ERROR_NONE;
449
450           bi0 = from[0];
451           b0 = vlib_get_buffer (vm, bi0);
452
453           ip4_header_t *ip0 =
454             (ip4_header_t *) u8_ptr_add (vlib_buffer_get_current (b0),
455                                          is_output_feature *
456                                          vnet_buffer (b0)->
457                                          ip.save_rewrite_length);
458           if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
459             {
460               // this is a regular packet - no fragmentation
461               if (is_custom)
462                 {
463                   next0 = vnet_buffer (b0)->ip.reass.next_index;
464                 }
465               else
466                 {
467                   next0 = IP4_SV_REASSEMBLY_NEXT_INPUT;
468                 }
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)
472                 {
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;
479                 }
480               else if (IP_PROTOCOL_ICMP == ip0->protocol)
481                 {
482                   vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
483                     ((icmp46_header_t *) (ip0 + 1))->type;
484                 }
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))
488                 {
489                   ip4_sv_reass_add_trace (vm, node, rm, NULL, bi0,
490                                           REASS_PASSTHROUGH,
491                                           vnet_buffer (b0)->ip.reass.ip_proto,
492                                           vnet_buffer (b0)->ip.
493                                           reass.l4_src_port,
494                                           vnet_buffer (b0)->ip.
495                                           reass.l4_dst_port);
496                 }
497               goto packet_enqueue;
498             }
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
504             {
505               next0 = IP4_SV_REASSEMBLY_NEXT_DROP;
506               error0 = IP4_ERROR_REASS_MALFORMED_PACKET;
507               b0->error = node->errors[error0];
508               goto packet_enqueue;
509             }
510           ip4_sv_reass_kv_t kv;
511           u8 do_handoff = 0;
512
513           kv.k.as_u64[0] =
514             (u64) vec_elt (ip4_main.fib_index_by_sw_if_index,
515                            vnet_buffer (b0)->sw_if_index[VLIB_RX]) |
516             (u64) ip0->src_address.as_u32 << 32;
517           kv.k.as_u64[1] =
518             (u64) ip0->dst_address.
519             as_u32 | (u64) ip0->fragment_id << 32 | (u64) ip0->protocol << 48;
520
521           ip4_sv_reass_t *reass =
522             ip4_sv_reass_find_or_create (vm, rm, rt, &kv, &do_handoff);
523
524           if (PREDICT_FALSE (do_handoff))
525             {
526               next0 = IP4_SV_REASSEMBLY_NEXT_HANDOFF;
527               vnet_buffer (b0)->ip.reass.owner_thread_index =
528                 kv.v.thread_index;
529               goto packet_enqueue;
530             }
531
532           if (!reass)
533             {
534               next0 = IP4_SV_REASSEMBLY_NEXT_DROP;
535               error0 = IP4_ERROR_REASS_LIMIT_REACHED;
536               b0->error = node->errors[error0];
537               goto packet_enqueue;
538             }
539
540           if (reass->is_complete)
541             {
542               if (is_custom)
543                 {
544                   next0 = vnet_buffer (b0)->ip.reass.next_index;
545                 }
546               else
547                 {
548                   next0 = IP4_SV_REASSEMBLY_NEXT_INPUT;
549                 }
550               vnet_buffer (b0)->ip.reass.is_non_first_fragment =
551                 ! !fragment_first;
552               vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
553               vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
554                 reass->icmp_type_or_tcp_flags;
555               vnet_buffer (b0)->ip.reass.tcp_ack_number =
556                 reass->tcp_ack_number;
557               vnet_buffer (b0)->ip.reass.tcp_seq_number =
558                 reass->tcp_seq_number;
559               vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
560               vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
561               if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
562                 {
563                   ip4_sv_reass_add_trace (vm, node, rm, reass, bi0,
564                                           REASS_FRAGMENT_FORWARD,
565                                           reass->ip_proto,
566                                           reass->l4_src_port,
567                                           reass->l4_dst_port);
568                 }
569               goto packet_enqueue;
570             }
571
572           ip4_sv_reass_rc_t rc =
573             ip4_sv_reass_update (vm, node, rm, rt, ip0, reass, bi0);
574           switch (rc)
575             {
576             case IP4_SV_REASS_RC_OK:
577               /* nothing to do here */
578               break;
579             case IP4_SV_REASS_RC_TOO_MANY_FRAGMENTS:
580               vlib_node_increment_counter (vm, node->node_index,
581                                            IP4_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
582                                            1);
583               ip4_sv_reass_free (vm, rm, rt, reass);
584               goto next_packet;
585               break;
586             case IP4_SV_REASS_RC_UNSUPP_IP_PROTO:
587               vlib_node_increment_counter (vm, node->node_index,
588                                            IP4_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
589                                            1);
590               ip4_sv_reass_free (vm, rm, rt, reass);
591               goto next_packet;
592               break;
593             }
594           if (reass->is_complete)
595             {
596               u32 idx;
597               vec_foreach_index (idx, reass->cached_buffers)
598               {
599                 u32 bi0 = vec_elt (reass->cached_buffers, idx);
600                 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
601                 u32 next0 = IP4_SV_REASSEMBLY_NEXT_INPUT;
602                 if (is_feature)
603                   {
604                     vnet_feature_next (&next0, b0);
605                   }
606                 if (is_custom)
607                   {
608                     next0 = vnet_buffer (b0)->ip.reass.next_index;
609                   }
610                 if (0 == n_left_to_next)
611                   {
612                     vlib_put_next_frame (vm, node, next_index,
613                                          n_left_to_next);
614                     vlib_get_next_frame (vm, node, next_index, to_next,
615                                          n_left_to_next);
616                   }
617                 to_next[0] = bi0;
618                 to_next += 1;
619                 n_left_to_next -= 1;
620                 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
621                   ! !ip4_get_fragment_offset (vlib_buffer_get_current (b0));
622                 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
623                 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
624                   reass->icmp_type_or_tcp_flags;
625                 vnet_buffer (b0)->ip.reass.tcp_ack_number =
626                   reass->tcp_ack_number;
627                 vnet_buffer (b0)->ip.reass.tcp_seq_number =
628                   reass->tcp_seq_number;
629                 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
630                 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
631                 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
632                   {
633                     ip4_sv_reass_add_trace (vm, node, rm, reass, bi0,
634                                             REASS_FRAGMENT_FORWARD,
635                                             reass->ip_proto,
636                                             reass->l4_src_port,
637                                             reass->l4_dst_port);
638                   }
639                 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
640                                                  to_next, n_left_to_next, bi0,
641                                                  next0);
642               }
643               _vec_len (reass->cached_buffers) = 0;     // buffers are owned by frame now
644             }
645           goto next_packet;
646
647         packet_enqueue:
648           to_next[0] = bi0;
649           to_next += 1;
650           n_left_to_next -= 1;
651           if (is_feature && IP4_ERROR_NONE == error0)
652             {
653               b0 = vlib_get_buffer (vm, bi0);
654               vnet_feature_next (&next0, b0);
655             }
656           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
657                                            to_next, n_left_to_next,
658                                            bi0, next0);
659
660         next_packet:
661           from += 1;
662           n_left_from -= 1;
663         }
664
665       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
666     }
667
668   clib_spinlock_unlock (&rt->lock);
669   return frame->n_vectors;
670 }
671
672 static char *ip4_sv_reass_error_strings[] = {
673 #define _(sym, string) string,
674   foreach_ip4_error
675 #undef _
676 };
677
678 VLIB_NODE_FN (ip4_sv_reass_node) (vlib_main_t * vm,
679                                   vlib_node_runtime_t * node,
680                                   vlib_frame_t * frame)
681 {
682   return ip4_sv_reass_inline (vm, node, frame, false /* is_feature */ ,
683                               false /* is_output_feature */ ,
684                               false /* is_custom */ );
685 }
686
687 /* *INDENT-OFF* */
688 VLIB_REGISTER_NODE (ip4_sv_reass_node) = {
689     .name = "ip4-sv-reassembly",
690     .vector_size = sizeof (u32),
691     .format_trace = format_ip4_sv_reass_trace,
692     .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
693     .error_strings = ip4_sv_reass_error_strings,
694     .n_next_nodes = IP4_SV_REASSEMBLY_N_NEXT,
695     .next_nodes =
696         {
697                 [IP4_SV_REASSEMBLY_NEXT_INPUT] = "ip4-input",
698                 [IP4_SV_REASSEMBLY_NEXT_DROP] = "ip4-drop",
699                 [IP4_SV_REASSEMBLY_NEXT_HANDOFF] = "ip4-sv-reassembly-handoff",
700
701         },
702 };
703 /* *INDENT-ON* */
704
705 VLIB_NODE_FN (ip4_sv_reass_node_feature) (vlib_main_t * vm,
706                                           vlib_node_runtime_t * node,
707                                           vlib_frame_t * frame)
708 {
709   return ip4_sv_reass_inline (vm, node, frame, true /* is_feature */ ,
710                               false /* is_output_feature */ ,
711                               false /* is_custom */ );
712 }
713
714 /* *INDENT-OFF* */
715 VLIB_REGISTER_NODE (ip4_sv_reass_node_feature) = {
716     .name = "ip4-sv-reassembly-feature",
717     .vector_size = sizeof (u32),
718     .format_trace = format_ip4_sv_reass_trace,
719     .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
720     .error_strings = ip4_sv_reass_error_strings,
721     .n_next_nodes = IP4_SV_REASSEMBLY_N_NEXT,
722     .next_nodes =
723         {
724                 [IP4_SV_REASSEMBLY_NEXT_INPUT] = "ip4-input",
725                 [IP4_SV_REASSEMBLY_NEXT_DROP] = "ip4-drop",
726                 [IP4_SV_REASSEMBLY_NEXT_HANDOFF] = "ip4-sv-reass-feature-hoff",
727         },
728 };
729 /* *INDENT-ON* */
730
731 /* *INDENT-OFF* */
732 VNET_FEATURE_INIT (ip4_sv_reass_feature) = {
733     .arc_name = "ip4-unicast",
734     .node_name = "ip4-sv-reassembly-feature",
735     .runs_before = VNET_FEATURES ("ip4-lookup"),
736     .runs_after = 0,
737 };
738 /* *INDENT-ON* */
739
740 VLIB_NODE_FN (ip4_sv_reass_node_output_feature) (vlib_main_t * vm,
741                                                  vlib_node_runtime_t * node,
742                                                  vlib_frame_t * frame)
743 {
744   return ip4_sv_reass_inline (vm, node, frame, true /* is_feature */ ,
745                               true /* is_output_feature */ ,
746                               false /* is_custom */ );
747 }
748
749
750 /* *INDENT-OFF* */
751 VLIB_REGISTER_NODE (ip4_sv_reass_node_output_feature) = {
752     .name = "ip4-sv-reassembly-output-feature",
753     .vector_size = sizeof (u32),
754     .format_trace = format_ip4_sv_reass_trace,
755     .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
756     .error_strings = ip4_sv_reass_error_strings,
757     .n_next_nodes = IP4_SV_REASSEMBLY_N_NEXT,
758     .next_nodes =
759         {
760                 [IP4_SV_REASSEMBLY_NEXT_INPUT] = "ip4-input",
761                 [IP4_SV_REASSEMBLY_NEXT_DROP] = "ip4-drop",
762                 [IP4_SV_REASSEMBLY_NEXT_HANDOFF] = "ip4-sv-reass-feature-hoff",
763         },
764 };
765 /* *INDENT-ON* */
766
767 /* *INDENT-OFF* */
768 VNET_FEATURE_INIT (ip4_sv_reass_output_feature) = {
769     .arc_name = "ip4-output",
770     .node_name = "ip4-sv-reassembly-output-feature",
771     .runs_before = 0,
772     .runs_after = 0,
773 };
774 /* *INDENT-ON* */
775
776 /* *INDENT-OFF* */
777 VLIB_REGISTER_NODE (ip4_sv_reass_custom_node) = {
778     .name = "ip4-sv-reassembly-custom-next",
779     .vector_size = sizeof (u32),
780     .format_trace = format_ip4_sv_reass_trace,
781     .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
782     .error_strings = ip4_sv_reass_error_strings,
783     .n_next_nodes = IP4_SV_REASSEMBLY_N_NEXT,
784     .next_nodes =
785         {
786                 [IP4_SV_REASSEMBLY_NEXT_INPUT] = "ip4-input",
787                 [IP4_SV_REASSEMBLY_NEXT_DROP] = "ip4-drop",
788                 [IP4_SV_REASSEMBLY_NEXT_HANDOFF] = "ip4-sv-reassembly-handoff",
789
790         },
791 };
792 /* *INDENT-ON* */
793
794 VLIB_NODE_FN (ip4_sv_reass_custom_node) (vlib_main_t * vm,
795                                          vlib_node_runtime_t * node,
796                                          vlib_frame_t * frame)
797 {
798   return ip4_sv_reass_inline (vm, node, frame, false /* is_feature */ ,
799                               false /* is_output_feature */ ,
800                               true /* is_custom */ );
801 }
802
803 #ifndef CLIB_MARCH_VARIANT
804 always_inline u32
805 ip4_sv_reass_get_nbuckets ()
806 {
807   ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
808   u32 nbuckets;
809   u8 i;
810
811   nbuckets = (u32) (rm->max_reass_n / IP4_SV_REASS_HT_LOAD_FACTOR);
812
813   for (i = 0; i < 31; i++)
814     if ((1 << i) >= nbuckets)
815       break;
816   nbuckets = 1 << i;
817
818   return nbuckets;
819 }
820 #endif /* CLIB_MARCH_VARIANT */
821
822 typedef enum
823 {
824   IP4_EVENT_CONFIG_CHANGED = 1,
825 } ip4_sv_reass_event_t;
826
827 typedef struct
828 {
829   int failure;
830   clib_bihash_16_8_t *new_hash;
831 } ip4_rehash_cb_ctx;
832
833 #ifndef CLIB_MARCH_VARIANT
834 static int
835 ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
836 {
837   ip4_rehash_cb_ctx *ctx = _ctx;
838   if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
839     {
840       ctx->failure = 1;
841     }
842   return (BIHASH_WALK_CONTINUE);
843 }
844
845 static void
846 ip4_sv_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
847                          u32 max_reassembly_length,
848                          u32 expire_walk_interval_ms)
849 {
850   ip4_sv_reass_main.timeout_ms = timeout_ms;
851   ip4_sv_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
852   ip4_sv_reass_main.max_reass_n = max_reassemblies;
853   ip4_sv_reass_main.max_reass_len = max_reassembly_length;
854   ip4_sv_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
855 }
856
857 vnet_api_error_t
858 ip4_sv_reass_set (u32 timeout_ms, u32 max_reassemblies,
859                   u32 max_reassembly_length, u32 expire_walk_interval_ms)
860 {
861   u32 old_nbuckets = ip4_sv_reass_get_nbuckets ();
862   ip4_sv_reass_set_params (timeout_ms, max_reassemblies,
863                            max_reassembly_length, expire_walk_interval_ms);
864   vlib_process_signal_event (ip4_sv_reass_main.vlib_main,
865                              ip4_sv_reass_main.ip4_sv_reass_expire_node_idx,
866                              IP4_EVENT_CONFIG_CHANGED, 0);
867   u32 new_nbuckets = ip4_sv_reass_get_nbuckets ();
868   if (ip4_sv_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
869     {
870       clib_bihash_16_8_t new_hash;
871       clib_memset (&new_hash, 0, sizeof (new_hash));
872       ip4_rehash_cb_ctx ctx;
873       ctx.failure = 0;
874       ctx.new_hash = &new_hash;
875       clib_bihash_init_16_8 (&new_hash, "ip4-dr", new_nbuckets,
876                              new_nbuckets * 1024);
877       clib_bihash_foreach_key_value_pair_16_8 (&ip4_sv_reass_main.hash,
878                                                ip4_rehash_cb, &ctx);
879       if (ctx.failure)
880         {
881           clib_bihash_free_16_8 (&new_hash);
882           return -1;
883         }
884       else
885         {
886           clib_bihash_free_16_8 (&ip4_sv_reass_main.hash);
887           clib_memcpy_fast (&ip4_sv_reass_main.hash, &new_hash,
888                             sizeof (ip4_sv_reass_main.hash));
889           clib_bihash_copied (&ip4_sv_reass_main.hash, &new_hash);
890         }
891     }
892   return 0;
893 }
894
895 vnet_api_error_t
896 ip4_sv_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
897                   u32 * max_reassembly_length, u32 * expire_walk_interval_ms)
898 {
899   *timeout_ms = ip4_sv_reass_main.timeout_ms;
900   *max_reassemblies = ip4_sv_reass_main.max_reass_n;
901   *max_reassembly_length = ip4_sv_reass_main.max_reass_len;
902   *expire_walk_interval_ms = ip4_sv_reass_main.expire_walk_interval_ms;
903   return 0;
904 }
905
906 static clib_error_t *
907 ip4_sv_reass_init_function (vlib_main_t * vm)
908 {
909   ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
910   clib_error_t *error = 0;
911   u32 nbuckets;
912   vlib_node_t *node;
913
914   rm->vlib_main = vm;
915   rm->vnet_main = vnet_get_main ();
916
917   vec_validate (rm->per_thread_data, vlib_num_workers ());
918   ip4_sv_reass_per_thread_t *rt;
919   vec_foreach (rt, rm->per_thread_data)
920   {
921     clib_spinlock_init (&rt->lock);
922     pool_alloc (rt->pool, rm->max_reass_n);
923     rt->lru_first = rt->lru_last = ~0;
924   }
925
926   node = vlib_get_node_by_name (vm, (u8 *) "ip4-sv-reassembly-expire-walk");
927   ASSERT (node);
928   rm->ip4_sv_reass_expire_node_idx = node->index;
929
930   ip4_sv_reass_set_params (IP4_SV_REASS_TIMEOUT_DEFAULT_MS,
931                            IP4_SV_REASS_MAX_REASSEMBLIES_DEFAULT,
932                            IP4_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
933                            IP4_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
934
935   nbuckets = ip4_sv_reass_get_nbuckets ();
936   clib_bihash_init_16_8 (&rm->hash, "ip4-dr", nbuckets, nbuckets * 1024);
937
938   node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop");
939   ASSERT (node);
940   rm->ip4_drop_idx = node->index;
941
942   rm->fq_index = vlib_frame_queue_main_init (ip4_sv_reass_node.index, 0);
943   rm->fq_feature_index =
944     vlib_frame_queue_main_init (ip4_sv_reass_node_feature.index, 0);
945
946   rm->feature_use_refcount_per_intf = NULL;
947   rm->output_feature_use_refcount_per_intf = NULL;
948
949   return error;
950 }
951
952 VLIB_INIT_FUNCTION (ip4_sv_reass_init_function);
953 #endif /* CLIB_MARCH_VARIANT */
954
955 static uword
956 ip4_sv_reass_walk_expired (vlib_main_t * vm,
957                            vlib_node_runtime_t * node, vlib_frame_t * f)
958 {
959   ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
960   uword event_type, *event_data = 0;
961
962   while (true)
963     {
964       vlib_process_wait_for_event_or_clock (vm,
965                                             (f64)
966                                             rm->expire_walk_interval_ms /
967                                             (f64) MSEC_PER_SEC);
968       event_type = vlib_process_get_events (vm, &event_data);
969
970       switch (event_type)
971         {
972         case ~0:                /* no events => timeout */
973           /* nothing to do here */
974           break;
975         case IP4_EVENT_CONFIG_CHANGED:
976           break;
977         default:
978           clib_warning ("BUG: event type 0x%wx", event_type);
979           break;
980         }
981       f64 now = vlib_time_now (vm);
982
983       ip4_sv_reass_t *reass;
984       int *pool_indexes_to_free = NULL;
985
986       uword thread_index = 0;
987       int index;
988       const uword nthreads = vlib_num_workers () + 1;
989       for (thread_index = 0; thread_index < nthreads; ++thread_index)
990         {
991           ip4_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
992           clib_spinlock_lock (&rt->lock);
993
994           vec_reset_length (pool_indexes_to_free);
995           /* *INDENT-OFF* */
996           pool_foreach_index (index, rt->pool, ({
997                                 reass = pool_elt_at_index (rt->pool, index);
998                                 if (now > reass->last_heard + rm->timeout)
999                                   {
1000                                     vec_add1 (pool_indexes_to_free, index);
1001                                   }
1002                               }));
1003           /* *INDENT-ON* */
1004           int *i;
1005           /* *INDENT-OFF* */
1006           vec_foreach (i, pool_indexes_to_free)
1007           {
1008             ip4_sv_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1009             ip4_sv_reass_free (vm, rm, rt, reass);
1010           }
1011           /* *INDENT-ON* */
1012
1013           clib_spinlock_unlock (&rt->lock);
1014         }
1015
1016       vec_free (pool_indexes_to_free);
1017       if (event_data)
1018         {
1019           _vec_len (event_data) = 0;
1020         }
1021     }
1022
1023   return 0;
1024 }
1025
1026 /* *INDENT-OFF* */
1027 VLIB_REGISTER_NODE (ip4_sv_reass_expire_node) = {
1028     .function = ip4_sv_reass_walk_expired,
1029     .type = VLIB_NODE_TYPE_PROCESS,
1030     .name = "ip4-sv-reassembly-expire-walk",
1031     .format_trace = format_ip4_sv_reass_trace,
1032     .n_errors = ARRAY_LEN (ip4_sv_reass_error_strings),
1033     .error_strings = ip4_sv_reass_error_strings,
1034
1035 };
1036 /* *INDENT-ON* */
1037
1038 static u8 *
1039 format_ip4_sv_reass_key (u8 * s, va_list * args)
1040 {
1041   ip4_sv_reass_key_t *key = va_arg (*args, ip4_sv_reass_key_t *);
1042   s =
1043     format (s,
1044             "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1045             key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1046             &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1047   return s;
1048 }
1049
1050 static u8 *
1051 format_ip4_sv_reass (u8 * s, va_list * args)
1052 {
1053   vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1054   ip4_sv_reass_t *reass = va_arg (*args, ip4_sv_reass_t *);
1055
1056   s = format (s, "ID: %lu, key: %U trace_op_counter: %u\n",
1057               reass->id, format_ip4_sv_reass_key, &reass->key,
1058               reass->trace_op_counter);
1059
1060   vlib_buffer_t *b;
1061   u32 *bip;
1062   u32 counter = 0;
1063   vec_foreach (bip, reass->cached_buffers)
1064   {
1065     u32 bi = *bip;
1066     do
1067       {
1068         b = vlib_get_buffer (vm, bi);
1069         s = format (s, "  #%03u: bi: %u, ", counter, bi);
1070         ++counter;
1071         bi = b->next_buffer;
1072       }
1073     while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
1074   }
1075   return s;
1076 }
1077
1078 static clib_error_t *
1079 show_ip4_reass (vlib_main_t * vm,
1080                 unformat_input_t * input,
1081                 CLIB_UNUSED (vlib_cli_command_t * lmd))
1082 {
1083   ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
1084
1085   vlib_cli_output (vm, "---------------------");
1086   vlib_cli_output (vm, "IP4 reassembly status");
1087   vlib_cli_output (vm, "---------------------");
1088   bool details = false;
1089   if (unformat (input, "details"))
1090     {
1091       details = true;
1092     }
1093
1094   u32 sum_reass_n = 0;
1095   ip4_sv_reass_t *reass;
1096   uword thread_index;
1097   const uword nthreads = vlib_num_workers () + 1;
1098   for (thread_index = 0; thread_index < nthreads; ++thread_index)
1099     {
1100       ip4_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1101       clib_spinlock_lock (&rt->lock);
1102       if (details)
1103         {
1104           /* *INDENT-OFF* */
1105           pool_foreach (reass, rt->pool, {
1106             vlib_cli_output (vm, "%U", format_ip4_sv_reass, vm, reass);
1107           });
1108           /* *INDENT-ON* */
1109         }
1110       sum_reass_n += rt->reass_n;
1111       clib_spinlock_unlock (&rt->lock);
1112     }
1113   vlib_cli_output (vm, "---------------------");
1114   vlib_cli_output (vm, "Current IP4 reassemblies count: %lu\n",
1115                    (long unsigned) sum_reass_n);
1116   vlib_cli_output (vm,
1117                    "Maximum configured concurrent shallow virtual IP4 reassemblies per worker-thread: %lu\n",
1118                    (long unsigned) rm->max_reass_n);
1119   vlib_cli_output (vm,
1120                    "Maximum configured shallow virtual IP4 reassembly timeout: %lums\n",
1121                    (long unsigned) rm->timeout_ms);
1122   vlib_cli_output (vm,
1123                    "Maximum configured shallow virtual IP4 reassembly expire walk interval: %lums\n",
1124                    (long unsigned) rm->expire_walk_interval_ms);
1125   return 0;
1126 }
1127
1128 /* *INDENT-OFF* */
1129 VLIB_CLI_COMMAND (show_ip4_sv_reass_cmd, static) = {
1130     .path = "show ip4-sv-reassembly",
1131     .short_help = "show ip4-sv-reassembly [details]",
1132     .function = show_ip4_reass,
1133 };
1134 /* *INDENT-ON* */
1135
1136 #ifndef CLIB_MARCH_VARIANT
1137 vnet_api_error_t
1138 ip4_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1139 {
1140   return ip4_sv_reass_enable_disable_with_refcnt (sw_if_index,
1141                                                   enable_disable);
1142 }
1143 #endif /* CLIB_MARCH_VARIANT */
1144
1145
1146 #define foreach_ip4_sv_reass_handoff_error                       \
1147 _(CONGESTION_DROP, "congestion drop")
1148
1149
1150 typedef enum
1151 {
1152 #define _(sym,str) IP4_SV_REASSEMBLY_HANDOFF_ERROR_##sym,
1153   foreach_ip4_sv_reass_handoff_error
1154 #undef _
1155     IP4_SV_REASSEMBLY_HANDOFF_N_ERROR,
1156 } ip4_sv_reass_handoff_error_t;
1157
1158 static char *ip4_sv_reass_handoff_error_strings[] = {
1159 #define _(sym,string) string,
1160   foreach_ip4_sv_reass_handoff_error
1161 #undef _
1162 };
1163
1164 typedef struct
1165 {
1166   u32 next_worker_index;
1167 } ip4_sv_reass_handoff_trace_t;
1168
1169 static u8 *
1170 format_ip4_sv_reass_handoff_trace (u8 * s, va_list * args)
1171 {
1172   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1173   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1174   ip4_sv_reass_handoff_trace_t *t =
1175     va_arg (*args, ip4_sv_reass_handoff_trace_t *);
1176
1177   s =
1178     format (s, "ip4-sv-reassembly-handoff: next-worker %d",
1179             t->next_worker_index);
1180
1181   return s;
1182 }
1183
1184 always_inline uword
1185 ip4_sv_reass_handoff_node_inline (vlib_main_t * vm,
1186                                   vlib_node_runtime_t * node,
1187                                   vlib_frame_t * frame, bool is_feature)
1188 {
1189   ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
1190
1191   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1192   u32 n_enq, n_left_from, *from;
1193   u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1194   u32 fq_index;
1195
1196   from = vlib_frame_vector_args (frame);
1197   n_left_from = frame->n_vectors;
1198   vlib_get_buffers (vm, from, bufs, n_left_from);
1199
1200   b = bufs;
1201   ti = thread_indices;
1202
1203   fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1204
1205   while (n_left_from > 0)
1206     {
1207       ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1208
1209       if (PREDICT_FALSE
1210           ((node->flags & VLIB_NODE_FLAG_TRACE)
1211            && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1212         {
1213           ip4_sv_reass_handoff_trace_t *t =
1214             vlib_add_trace (vm, node, b[0], sizeof (*t));
1215           t->next_worker_index = ti[0];
1216         }
1217
1218       n_left_from -= 1;
1219       ti += 1;
1220       b += 1;
1221     }
1222   n_enq =
1223     vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1224                                    frame->n_vectors, 1);
1225
1226   if (n_enq < frame->n_vectors)
1227     vlib_node_increment_counter (vm, node->node_index,
1228                                  IP4_SV_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1229                                  frame->n_vectors - n_enq);
1230   return frame->n_vectors;
1231 }
1232
1233 VLIB_NODE_FN (ip4_sv_reass_handoff_node) (vlib_main_t * vm,
1234                                           vlib_node_runtime_t * node,
1235                                           vlib_frame_t * frame)
1236 {
1237   return ip4_sv_reass_handoff_node_inline (vm, node, frame,
1238                                            false /* is_feature */ );
1239 }
1240
1241
1242 /* *INDENT-OFF* */
1243 VLIB_REGISTER_NODE (ip4_sv_reass_handoff_node) = {
1244   .name = "ip4-sv-reassembly-handoff",
1245   .vector_size = sizeof (u32),
1246   .n_errors = ARRAY_LEN(ip4_sv_reass_handoff_error_strings),
1247   .error_strings = ip4_sv_reass_handoff_error_strings,
1248   .format_trace = format_ip4_sv_reass_handoff_trace,
1249
1250   .n_next_nodes = 1,
1251
1252   .next_nodes = {
1253     [0] = "error-drop",
1254   },
1255 };
1256 /* *INDENT-ON* */
1257
1258
1259 /* *INDENT-OFF* */
1260 VLIB_NODE_FN (ip4_sv_reass_feature_handoff_node) (vlib_main_t * vm,
1261                                                     vlib_node_runtime_t *
1262                                                     node,
1263                                                     vlib_frame_t * frame)
1264 {
1265   return ip4_sv_reass_handoff_node_inline (vm, node, frame,
1266                                              true /* is_feature */ );
1267 }
1268 /* *INDENT-ON* */
1269
1270
1271 /* *INDENT-OFF* */
1272 VLIB_REGISTER_NODE (ip4_sv_reass_feature_handoff_node) = {
1273   .name = "ip4-sv-reass-feature-hoff",
1274   .vector_size = sizeof (u32),
1275   .n_errors = ARRAY_LEN(ip4_sv_reass_handoff_error_strings),
1276   .error_strings = ip4_sv_reass_handoff_error_strings,
1277   .format_trace = format_ip4_sv_reass_handoff_trace,
1278
1279   .n_next_nodes = 1,
1280
1281   .next_nodes = {
1282     [0] = "error-drop",
1283   },
1284 };
1285 /* *INDENT-ON* */
1286
1287 #ifndef CLIB_MARCH_VARIANT
1288 int
1289 ip4_sv_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1290 {
1291   ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
1292   vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1293   if (is_enable)
1294     {
1295       if (!rm->feature_use_refcount_per_intf[sw_if_index])
1296         {
1297           ++rm->feature_use_refcount_per_intf[sw_if_index];
1298           return vnet_feature_enable_disable ("ip4-unicast",
1299                                               "ip4-sv-reassembly-feature",
1300                                               sw_if_index, 1, 0, 0);
1301         }
1302       ++rm->feature_use_refcount_per_intf[sw_if_index];
1303     }
1304   else
1305     {
1306       if (rm->feature_use_refcount_per_intf[sw_if_index])
1307         --rm->feature_use_refcount_per_intf[sw_if_index];
1308       if (!rm->feature_use_refcount_per_intf[sw_if_index])
1309         return vnet_feature_enable_disable ("ip4-unicast",
1310                                             "ip4-sv-reassembly-feature",
1311                                             sw_if_index, 0, 0, 0);
1312     }
1313   return 0;
1314 }
1315
1316 uword
1317 ip4_sv_reass_custom_register_next_node (uword node_index)
1318 {
1319   return vlib_node_add_next (vlib_get_main (), ip4_sv_reass_custom_node.index,
1320                              node_index);
1321 }
1322
1323 int
1324 ip4_sv_reass_output_enable_disable_with_refcnt (u32 sw_if_index,
1325                                                 int is_enable)
1326 {
1327   ip4_sv_reass_main_t *rm = &ip4_sv_reass_main;
1328   vec_validate (rm->output_feature_use_refcount_per_intf, sw_if_index);
1329   if (is_enable)
1330     {
1331       if (!rm->output_feature_use_refcount_per_intf[sw_if_index])
1332         {
1333           ++rm->output_feature_use_refcount_per_intf[sw_if_index];
1334           return vnet_feature_enable_disable ("ip4-output",
1335                                               "ip4-sv-reassembly-output-feature",
1336                                               sw_if_index, 1, 0, 0);
1337         }
1338       ++rm->output_feature_use_refcount_per_intf[sw_if_index];
1339     }
1340   else
1341     {
1342       if (rm->output_feature_use_refcount_per_intf[sw_if_index])
1343         --rm->output_feature_use_refcount_per_intf[sw_if_index];
1344       if (!rm->output_feature_use_refcount_per_intf[sw_if_index])
1345         return vnet_feature_enable_disable ("ip4-output",
1346                                             "ip4-sv-reassembly-output-feature",
1347                                             sw_if_index, 0, 0, 0);
1348     }
1349   return 0;
1350 }
1351 #endif
1352
1353 /*
1354  * fd.io coding-style-patch-verification: ON
1355  *
1356  * Local Variables:
1357  * eval: (c-set-style "gnu")
1358  * End:
1359  */