ip: speed up reassembly code compilation
[vpp.git] / src / vnet / ip / reass / ip4_full_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 Full Reassembly.
19  *
20  * This file contains the source code for IPv4 full reassembly.
21  */
22
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>
29 #include <stddef.h>
30
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)
37
38 #define IP4_REASS_DEBUG_BUFFERS 0
39 #if IP4_REASS_DEBUG_BUFFERS
40 #define IP4_REASS_DEBUG_BUFFER(bi, what)             \
41   do                                                 \
42     {                                                \
43       u32 _bi = bi;                                  \
44       printf (#what "buffer %u", _bi);               \
45       vlib_buffer_t *_b = vlib_get_buffer (vm, _bi); \
46       while (_b->flags & VLIB_BUFFER_NEXT_PRESENT)   \
47         {                                            \
48           _bi = _b->next_buffer;                     \
49           printf ("[%u]", _bi);                      \
50           _b = vlib_get_buffer (vm, _bi);            \
51         }                                            \
52       printf ("\n");                                 \
53       fflush (stdout);                               \
54     }                                                \
55   while (0)
56 #else
57 #define IP4_REASS_DEBUG_BUFFER(...)
58 #endif
59
60 typedef enum
61 {
62   IP4_REASS_RC_OK,
63   IP4_REASS_RC_TOO_MANY_FRAGMENTS,
64   IP4_REASS_RC_INTERNAL_ERROR,
65   IP4_REASS_RC_NO_BUF,
66   IP4_REASS_RC_HANDOFF,
67 } ip4_full_reass_rc_t;
68
69 typedef struct
70 {
71   union
72   {
73     struct
74     {
75       u32 xx_id;
76       ip4_address_t src;
77       ip4_address_t dst;
78       u16 frag_id;
79       u8 proto;
80       u8 unused;
81     };
82     u64 as_u64[2];
83   };
84 } ip4_full_reass_key_t;
85
86 typedef union
87 {
88   struct
89   {
90     u32 reass_index;
91     u32 memory_owner_thread_index;
92   };
93   u64 as_u64;
94 } ip4_full_reass_val_t;
95
96 typedef union
97 {
98   struct
99   {
100     ip4_full_reass_key_t k;
101     ip4_full_reass_val_t v;
102   };
103   clib_bihash_kv_16_8_t kv;
104 } ip4_full_reass_kv_t;
105
106 always_inline u32
107 ip4_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
108 {
109   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
110   return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
111 }
112
113 always_inline u16
114 ip4_full_reass_buffer_get_data_len (vlib_buffer_t * b)
115 {
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;
120 }
121
122 typedef struct
123 {
124   // hash table key
125   ip4_full_reass_key_t key;
126   // time when last packet was received
127   f64 last_heard;
128   // internal id of this reassembly
129   u64 id;
130   // buffer index of first buffer in this reassembly context
131   u32 first_bi;
132   // last octet of packet, ~0 until fragment without more_fragments arrives
133   u32 last_packet_octet;
134   // length of data collected so far
135   u32 data_len;
136   // trace operation counter
137   u32 trace_op_counter;
138   // next index - used by non-feature node
139   u32 next_index;
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
145   u32 fragments_n;
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;
151 } ip4_full_reass_t;
152
153 typedef struct
154 {
155   ip4_full_reass_t *pool;
156   u32 reass_n;
157   u32 id_counter;
158   clib_spinlock_t lock;
159 } ip4_full_reass_per_thread_t;
160
161 typedef struct
162 {
163   // IPv4 config
164   u32 timeout_ms;
165   f64 timeout;
166   u32 expire_walk_interval_ms;
167   // maximum number of fragments in one reassembly
168   u32 max_reass_len;
169   // maximum number of reassemblies
170   u32 max_reass_n;
171
172   // IPv4 runtime
173   clib_bihash_16_8_t hash;
174   // per-thread data
175   ip4_full_reass_per_thread_t *per_thread_data;
176
177   // convenience
178   vlib_main_t *vlib_main;
179
180   // node index of ip4-drop node
181   u32 ip4_drop_idx;
182   u32 ip4_full_reass_expire_node_idx;
183
184   /** Worker handoff */
185   u32 fq_index;
186   u32 fq_feature_index;
187   u32 fq_custom_index;
188
189   // reference count for enabling/disabling feature - per interface
190   u32 *feature_use_refcount_per_intf;
191 } ip4_full_reass_main_t;
192
193 extern ip4_full_reass_main_t ip4_full_reass_main;
194
195 #ifndef CLIB_MARCH_VARIANT
196 ip4_full_reass_main_t ip4_full_reass_main;
197 #endif /* CLIB_MARCH_VARIANT */
198
199 typedef enum
200 {
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;
206
207 typedef enum
208 {
209   NORMAL,
210   FEATURE,
211   CUSTOM
212 } ip4_full_reass_node_type_t;
213
214 typedef enum
215 {
216   RANGE_NEW,
217   RANGE_SHRINK,
218   RANGE_DISCARD,
219   RANGE_OVERLAP,
220   FINALIZE,
221   HANDOFF,
222 } ip4_full_reass_trace_operation_e;
223
224 typedef struct
225 {
226   u16 range_first;
227   u16 range_last;
228   u32 range_bi;
229   i32 data_offset;
230   u32 data_len;
231   u32 first_bi;
232 } ip4_full_reass_range_trace_t;
233
234 typedef struct
235 {
236   ip4_full_reass_trace_operation_e action;
237   u32 reass_id;
238   ip4_full_reass_range_trace_t trace_range;
239   u32 size_diff;
240   u32 op_id;
241   u32 thread_id;
242   u32 thread_id_to;
243   u32 fragment_first;
244   u32 fragment_last;
245   u32 total_data_len;
246   bool is_after_handoff;
247   ip4_header_t ip4_header;
248 } ip4_full_reass_trace_t;
249
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;
253
254 static void
255 ip4_full_reass_trace_details (vlib_main_t * vm, u32 bi,
256                               ip4_full_reass_range_trace_t * trace)
257 {
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;
265 }
266
267 static u8 *
268 format_ip4_full_reass_range_trace (u8 * s, va_list * args)
269 {
270   ip4_full_reass_range_trace_t *trace =
271     va_arg (*args, ip4_full_reass_range_trace_t *);
272   s =
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,
275             trace->range_bi);
276   return s;
277 }
278
279 static u8 *
280 format_ip4_full_reass_trace (u8 * s, va_list * args)
281 {
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 *);
285   u32 indent = 0;
286   if (~0 != t->reass_id)
287     {
288       if (t->is_after_handoff)
289         {
290           s =
291             format (s, "%U\n", format_ip4_header, &t->ip4_header,
292                     sizeof (t->ip4_header));
293           indent = 2;
294         }
295       s =
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);
299       s =
300         format (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,
303                 t->fragment_last);
304     }
305   switch (t->action)
306     {
307     case RANGE_SHRINK:
308       s = format (s, "\n%Ushrink %U by %u", format_white_space, indent,
309                   format_ip4_full_reass_range_trace, &t->trace_range,
310                   t->size_diff);
311       break;
312     case RANGE_DISCARD:
313       s = format (s, "\n%Udiscard %U", format_white_space, indent,
314                   format_ip4_full_reass_range_trace, &t->trace_range);
315       break;
316     case RANGE_NEW:
317       s = format (s, "\n%Unew %U", format_white_space, indent,
318                   format_ip4_full_reass_range_trace, &t->trace_range);
319       break;
320     case RANGE_OVERLAP:
321       s = format (s, "\n%Uoverlapping/ignored %U", format_white_space, indent,
322                   format_ip4_full_reass_range_trace, &t->trace_range);
323       break;
324     case FINALIZE:
325       s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
326       break;
327     case HANDOFF:
328       s =
329         format (s, "handoff from thread #%u to thread #%u", t->thread_id,
330                 t->thread_id_to);
331       break;
332     }
333   return s;
334 }
335
336 static void
337 ip4_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
338                           ip4_full_reass_t * reass, u32 bi,
339                           ip4_full_reass_trace_operation_e action,
340                           u32 size_diff, u32 thread_id_to)
341 {
342   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
343   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
344   if (pool_is_free_index
345       (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
346     {
347       // this buffer's trace is gone
348       b->flags &= ~VLIB_BUFFER_IS_TRACED;
349       return;
350     }
351   bool is_after_handoff = false;
352   if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
353     {
354       is_after_handoff = true;
355     }
356   ip4_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
357   t->is_after_handoff = is_after_handoff;
358   if (t->is_after_handoff)
359     {
360       clib_memcpy (&t->ip4_header, vlib_buffer_get_current (b),
361                    clib_min (sizeof (t->ip4_header), b->current_length));
362     }
363   if (reass)
364     {
365       t->reass_id = reass->id;
366       t->op_id = reass->trace_op_counter;
367       t->trace_range.first_bi = reass->first_bi;
368       t->total_data_len = reass->data_len;
369       ++reass->trace_op_counter;
370     }
371   else
372     {
373       t->reass_id = ~0;
374       t->op_id = 0;
375       t->trace_range.first_bi = 0;
376       t->total_data_len = 0;
377     }
378   t->action = action;
379   ip4_full_reass_trace_details (vm, bi, &t->trace_range);
380   t->size_diff = size_diff;
381   t->thread_id = vm->thread_index;
382   t->thread_id_to = thread_id_to;
383   t->fragment_first = vnb->ip.reass.fragment_first;
384   t->fragment_last = vnb->ip.reass.fragment_last;
385 #if 0
386   static u8 *s = NULL;
387   s = format (s, "%U", format_ip4_full_reass_trace, NULL, NULL, t);
388   printf ("%.*s\n", vec_len (s), s);
389   fflush (stdout);
390   vec_reset_length (s);
391 #endif
392 }
393
394 always_inline void
395 ip4_full_reass_free_ctx (ip4_full_reass_per_thread_t * rt,
396                          ip4_full_reass_t * reass)
397 {
398   pool_put (rt->pool, reass);
399   --rt->reass_n;
400 }
401
402 always_inline void
403 ip4_full_reass_free (ip4_full_reass_main_t * rm,
404                      ip4_full_reass_per_thread_t * rt,
405                      ip4_full_reass_t * reass)
406 {
407   clib_bihash_kv_16_8_t kv;
408   kv.key[0] = reass->key.as_u64[0];
409   kv.key[1] = reass->key.as_u64[1];
410   clib_bihash_add_del_16_8 (&rm->hash, &kv, 0);
411   return ip4_full_reass_free_ctx (rt, reass);
412 }
413
414 always_inline void
415 ip4_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
416                          ip4_full_reass_t *reass)
417 {
418   u32 range_bi = reass->first_bi;
419   vlib_buffer_t *range_b;
420   vnet_buffer_opaque_t *range_vnb;
421   u32 *to_free = NULL;
422   while (~0 != range_bi)
423     {
424       range_b = vlib_get_buffer (vm, range_bi);
425       range_vnb = vnet_buffer (range_b);
426       u32 bi = range_bi;
427       while (~0 != bi)
428         {
429           vec_add1 (to_free, bi);
430           vlib_buffer_t *b = vlib_get_buffer (vm, bi);
431           if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
432             {
433               bi = b->next_buffer;
434               b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
435             }
436           else
437             {
438               bi = ~0;
439             }
440         }
441       range_bi = range_vnb->ip.reass.next_range_bi;
442     }
443   /* send to next_error_index */
444   if (~0 != reass->error_next_index)
445     {
446       u32 n_left_to_next, *to_next, next_index;
447
448       next_index = reass->error_next_index;
449       u32 bi = ~0;
450
451       while (vec_len (to_free) > 0)
452         {
453           vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
454
455           while (vec_len (to_free) > 0 && n_left_to_next > 0)
456             {
457               bi = vec_pop (to_free);
458
459               if (~0 != bi)
460                 {
461                   to_next[0] = bi;
462                   to_next += 1;
463                   n_left_to_next -= 1;
464                 }
465             }
466           vlib_put_next_frame (vm, node, next_index, n_left_to_next);
467         }
468     }
469   else
470     {
471       vlib_buffer_free (vm, to_free, vec_len (to_free));
472     }
473   vec_free (to_free);
474 }
475
476 always_inline void
477 ip4_full_reass_init (ip4_full_reass_t * reass)
478 {
479   reass->first_bi = ~0;
480   reass->last_packet_octet = ~0;
481   reass->data_len = 0;
482   reass->next_index = ~0;
483   reass->error_next_index = ~0;
484 }
485
486 always_inline ip4_full_reass_t *
487 ip4_full_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
488                                ip4_full_reass_main_t * rm,
489                                ip4_full_reass_per_thread_t * rt,
490                                ip4_full_reass_kv_t * kv, u8 * do_handoff)
491 {
492   ip4_full_reass_t *reass;
493   f64 now;
494
495 again:
496
497   reass = NULL;
498   now = vlib_time_now (vm);
499   if (!clib_bihash_search_16_8 (&rm->hash, &kv->kv, &kv->kv))
500     {
501       if (vm->thread_index != kv->v.memory_owner_thread_index)
502         {
503           *do_handoff = 1;
504           return NULL;
505         }
506       reass =
507         pool_elt_at_index (rm->per_thread_data
508                            [kv->v.memory_owner_thread_index].pool,
509                            kv->v.reass_index);
510
511       if (now > reass->last_heard + rm->timeout)
512         {
513           ip4_full_reass_drop_all (vm, node, reass);
514           ip4_full_reass_free (rm, rt, reass);
515           reass = NULL;
516         }
517     }
518
519   if (reass)
520     {
521       reass->last_heard = now;
522       return reass;
523     }
524
525   if (rt->reass_n >= rm->max_reass_n)
526     {
527       reass = NULL;
528       return reass;
529     }
530   else
531     {
532       pool_get (rt->pool, reass);
533       clib_memset (reass, 0, sizeof (*reass));
534       reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
535       reass->memory_owner_thread_index = vm->thread_index;
536       ++rt->id_counter;
537       ip4_full_reass_init (reass);
538       ++rt->reass_n;
539     }
540
541   reass->key.as_u64[0] = kv->kv.key[0];
542   reass->key.as_u64[1] = kv->kv.key[1];
543   kv->v.reass_index = (reass - rt->pool);
544   kv->v.memory_owner_thread_index = vm->thread_index;
545   reass->last_heard = now;
546
547   int rv = clib_bihash_add_del_16_8 (&rm->hash, &kv->kv, 2);
548   if (rv)
549     {
550       ip4_full_reass_free_ctx (rt, reass);
551       reass = NULL;
552       // if other worker created a context already work with the other copy
553       if (-2 == rv)
554         goto again;
555     }
556
557   return reass;
558 }
559
560 always_inline ip4_full_reass_rc_t
561 ip4_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
562                          ip4_full_reass_main_t * rm,
563                          ip4_full_reass_per_thread_t * rt,
564                          ip4_full_reass_t * reass, u32 * bi0,
565                          u32 * next0, u32 * error0, bool is_custom)
566 {
567   vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
568   vlib_buffer_t *last_b = NULL;
569   u32 sub_chain_bi = reass->first_bi;
570   u32 total_length = 0;
571   u32 buf_cnt = 0;
572   do
573     {
574       u32 tmp_bi = sub_chain_bi;
575       vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
576       ip4_header_t *ip = vlib_buffer_get_current (tmp);
577       vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
578       if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
579           !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
580         {
581           return IP4_REASS_RC_INTERNAL_ERROR;
582         }
583
584       u32 data_len = ip4_full_reass_buffer_get_data_len (tmp);
585       u32 trim_front =
586         ip4_header_bytes (ip) + ip4_full_reass_buffer_get_data_offset (tmp);
587       u32 trim_end =
588         vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
589       if (tmp_bi == reass->first_bi)
590         {
591           /* first buffer - keep ip4 header */
592           if (0 != ip4_full_reass_buffer_get_data_offset (tmp))
593             {
594               return IP4_REASS_RC_INTERNAL_ERROR;
595             }
596           trim_front = 0;
597           trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
598             ip4_header_bytes (ip);
599           if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
600             {
601               return IP4_REASS_RC_INTERNAL_ERROR;
602             }
603         }
604       u32 keep_data =
605         vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
606       while (1)
607         {
608           ++buf_cnt;
609           if (trim_front)
610             {
611               if (trim_front > tmp->current_length)
612                 {
613                   /* drop whole buffer */
614                   u32 to_be_freed_bi = tmp_bi;
615                   trim_front -= tmp->current_length;
616                   if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
617                     {
618                       return IP4_REASS_RC_INTERNAL_ERROR;
619                     }
620                   tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
621                   tmp_bi = tmp->next_buffer;
622                   tmp->next_buffer = 0;
623                   tmp = vlib_get_buffer (vm, tmp_bi);
624                   vlib_buffer_free_one (vm, to_be_freed_bi);
625                   continue;
626                 }
627               else
628                 {
629                   vlib_buffer_advance (tmp, trim_front);
630                   trim_front = 0;
631                 }
632             }
633           if (keep_data)
634             {
635               if (last_b)
636                 {
637                   last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
638                   last_b->next_buffer = tmp_bi;
639                 }
640               last_b = tmp;
641               if (keep_data <= tmp->current_length)
642                 {
643                   tmp->current_length = keep_data;
644                   keep_data = 0;
645                 }
646               else
647                 {
648                   keep_data -= tmp->current_length;
649                   if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
650                     {
651                       return IP4_REASS_RC_INTERNAL_ERROR;
652                     }
653                 }
654               total_length += tmp->current_length;
655               if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
656                 {
657                   tmp_bi = tmp->next_buffer;
658                   tmp = vlib_get_buffer (vm, tmp->next_buffer);
659                 }
660               else
661                 {
662                   break;
663                 }
664             }
665           else
666             {
667               u32 to_be_freed_bi = tmp_bi;
668               if (reass->first_bi == tmp_bi)
669                 {
670                   return IP4_REASS_RC_INTERNAL_ERROR;
671                 }
672               if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
673                 {
674                   tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
675                   tmp_bi = tmp->next_buffer;
676                   tmp->next_buffer = 0;
677                   tmp = vlib_get_buffer (vm, tmp_bi);
678                   vlib_buffer_free_one (vm, to_be_freed_bi);
679                 }
680               else
681                 {
682                   tmp->next_buffer = 0;
683                   vlib_buffer_free_one (vm, to_be_freed_bi);
684                   break;
685                 }
686             }
687         }
688       sub_chain_bi =
689         vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
690         reass.next_range_bi;
691     }
692   while (~0 != sub_chain_bi);
693
694   if (!last_b)
695     {
696       return IP4_REASS_RC_INTERNAL_ERROR;
697     }
698   last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
699
700   if (total_length < first_b->current_length)
701     {
702       return IP4_REASS_RC_INTERNAL_ERROR;
703     }
704   total_length -= first_b->current_length;
705   first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
706   first_b->total_length_not_including_first_buffer = total_length;
707   ip4_header_t *ip = vlib_buffer_get_current (first_b);
708   ip->flags_and_fragment_offset = 0;
709   ip->length = clib_host_to_net_u16 (first_b->current_length + total_length);
710   ip->checksum = ip4_header_checksum (ip);
711   if (!vlib_buffer_chain_linearize (vm, first_b))
712     {
713       return IP4_REASS_RC_NO_BUF;
714     }
715   // reset to reconstruct the mbuf linking
716   first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
717   if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
718     {
719       ip4_full_reass_add_trace (vm, node, reass, reass->first_bi, FINALIZE, 0,
720                                 ~0);
721 #if 0
722       // following code does a hexdump of packet fragments to stdout ...
723       do
724         {
725           u32 bi = reass->first_bi;
726           u8 *s = NULL;
727           while (~0 != bi)
728             {
729               vlib_buffer_t *b = vlib_get_buffer (vm, bi);
730               s = format (s, "%u: %U\n", bi, format_hexdump,
731                           vlib_buffer_get_current (b), b->current_length);
732               if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
733                 {
734                   bi = b->next_buffer;
735                 }
736               else
737                 {
738                   break;
739                 }
740             }
741           printf ("%.*s\n", vec_len (s), s);
742           fflush (stdout);
743           vec_free (s);
744         }
745       while (0);
746 #endif
747     }
748   *bi0 = reass->first_bi;
749   if (!is_custom)
750     {
751       *next0 = IP4_FULL_REASS_NEXT_INPUT;
752     }
753   else
754     {
755       *next0 = reass->next_index;
756     }
757   vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
758   *error0 = IP4_ERROR_NONE;
759   ip4_full_reass_free (rm, rt, reass);
760   reass = NULL;
761   return IP4_REASS_RC_OK;
762 }
763
764 always_inline ip4_full_reass_rc_t
765 ip4_full_reass_insert_range_in_chain (vlib_main_t * vm,
766                                       ip4_full_reass_t * reass,
767                                       u32 prev_range_bi, u32 new_next_bi)
768 {
769   vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
770   vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
771   if (~0 != prev_range_bi)
772     {
773       vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
774       vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
775       new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
776       prev_vnb->ip.reass.next_range_bi = new_next_bi;
777     }
778   else
779     {
780       if (~0 != reass->first_bi)
781         {
782           new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
783         }
784       reass->first_bi = new_next_bi;
785     }
786   vnet_buffer_opaque_t *vnb = vnet_buffer (new_next_b);
787   if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
788       !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
789     {
790       return IP4_REASS_RC_INTERNAL_ERROR;
791     }
792   reass->data_len += ip4_full_reass_buffer_get_data_len (new_next_b);
793   return IP4_REASS_RC_OK;
794 }
795
796 always_inline ip4_full_reass_rc_t
797 ip4_full_reass_remove_range_from_chain (vlib_main_t * vm,
798                                         vlib_node_runtime_t * node,
799                                         ip4_full_reass_t * reass,
800                                         u32 prev_range_bi, u32 discard_bi)
801 {
802   vlib_buffer_t *discard_b = vlib_get_buffer (vm, discard_bi);
803   vnet_buffer_opaque_t *discard_vnb = vnet_buffer (discard_b);
804   if (~0 != prev_range_bi)
805     {
806       vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
807       vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
808       if (!(prev_vnb->ip.reass.next_range_bi == discard_bi))
809         {
810           return IP4_REASS_RC_INTERNAL_ERROR;
811         }
812       prev_vnb->ip.reass.next_range_bi = discard_vnb->ip.reass.next_range_bi;
813     }
814   else
815     {
816       reass->first_bi = discard_vnb->ip.reass.next_range_bi;
817     }
818   vnet_buffer_opaque_t *vnb = vnet_buffer (discard_b);
819   if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
820       !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
821     {
822       return IP4_REASS_RC_INTERNAL_ERROR;
823     }
824   reass->data_len -= ip4_full_reass_buffer_get_data_len (discard_b);
825   while (1)
826     {
827       u32 to_be_freed_bi = discard_bi;
828       if (PREDICT_FALSE (discard_b->flags & VLIB_BUFFER_IS_TRACED))
829         {
830           ip4_full_reass_add_trace (vm, node, reass, discard_bi, RANGE_DISCARD,
831                                     0, ~0);
832         }
833       if (discard_b->flags & VLIB_BUFFER_NEXT_PRESENT)
834         {
835           discard_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
836           discard_bi = discard_b->next_buffer;
837           discard_b->next_buffer = 0;
838           discard_b = vlib_get_buffer (vm, discard_bi);
839           vlib_buffer_free_one (vm, to_be_freed_bi);
840         }
841       else
842         {
843           discard_b->next_buffer = 0;
844           vlib_buffer_free_one (vm, to_be_freed_bi);
845           break;
846         }
847     }
848   return IP4_REASS_RC_OK;
849 }
850
851 always_inline ip4_full_reass_rc_t
852 ip4_full_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
853                        ip4_full_reass_main_t * rm,
854                        ip4_full_reass_per_thread_t * rt,
855                        ip4_full_reass_t * reass, u32 * bi0, u32 * next0,
856                        u32 * error0, bool is_custom, u32 * handoff_thread_idx)
857 {
858   vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
859   vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
860   if (is_custom)
861     {
862       // store (error_)next_index before it's overwritten
863       reass->next_index = fvnb->ip.reass.next_index;
864       reass->error_next_index = fvnb->ip.reass.error_next_index;
865     }
866   ip4_full_reass_rc_t rc = IP4_REASS_RC_OK;
867   int consumed = 0;
868   ip4_header_t *fip = vlib_buffer_get_current (fb);
869   const u32 fragment_first = ip4_get_fragment_offset_bytes (fip);
870   const u32 fragment_length =
871     clib_net_to_host_u16 (fip->length) - ip4_header_bytes (fip);
872   const u32 fragment_last = fragment_first + fragment_length - 1;
873   fvnb->ip.reass.fragment_first = fragment_first;
874   fvnb->ip.reass.fragment_last = fragment_last;
875   int more_fragments = ip4_get_fragment_more (fip);
876   u32 candidate_range_bi = reass->first_bi;
877   u32 prev_range_bi = ~0;
878   fvnb->ip.reass.range_first = fragment_first;
879   fvnb->ip.reass.range_last = fragment_last;
880   fvnb->ip.reass.next_range_bi = ~0;
881   if (!more_fragments)
882     {
883       reass->last_packet_octet = fragment_last;
884     }
885   if (~0 == reass->first_bi)
886     {
887       // starting a new reassembly
888       rc =
889         ip4_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
890       if (IP4_REASS_RC_OK != rc)
891         {
892           return rc;
893         }
894       if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
895         {
896           ip4_full_reass_add_trace (vm, node, reass, *bi0, RANGE_NEW, 0, ~0);
897         }
898       *bi0 = ~0;
899       reass->min_fragment_length = clib_net_to_host_u16 (fip->length);
900       reass->fragments_n = 1;
901       return IP4_REASS_RC_OK;
902     }
903   reass->min_fragment_length =
904     clib_min (clib_net_to_host_u16 (fip->length),
905               fvnb->ip.reass.estimated_mtu);
906   while (~0 != candidate_range_bi)
907     {
908       vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
909       vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
910       if (fragment_first > candidate_vnb->ip.reass.range_last)
911         {
912           // this fragments starts after candidate range
913           prev_range_bi = candidate_range_bi;
914           candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
915           if (candidate_vnb->ip.reass.range_last < fragment_last &&
916               ~0 == candidate_range_bi)
917             {
918               // special case - this fragment falls beyond all known ranges
919               rc = ip4_full_reass_insert_range_in_chain (vm, reass,
920                                                          prev_range_bi, *bi0);
921               if (IP4_REASS_RC_OK != rc)
922                 {
923                   return rc;
924                 }
925               consumed = 1;
926               break;
927             }
928           continue;
929         }
930       if (fragment_last < candidate_vnb->ip.reass.range_first)
931         {
932           // this fragment ends before candidate range without any overlap
933           rc = ip4_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
934                                                      *bi0);
935           if (IP4_REASS_RC_OK != rc)
936             {
937               return rc;
938             }
939           consumed = 1;
940         }
941       else
942         {
943           if (fragment_first >= candidate_vnb->ip.reass.range_first &&
944               fragment_last <= candidate_vnb->ip.reass.range_last)
945             {
946               // this fragment is a (sub)part of existing range, ignore it
947               if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
948                 {
949                   ip4_full_reass_add_trace (vm, node, reass, *bi0,
950                                             RANGE_OVERLAP, 0, ~0);
951                 }
952               break;
953             }
954           int discard_candidate = 0;
955           if (fragment_first < candidate_vnb->ip.reass.range_first)
956             {
957               u32 overlap =
958                 fragment_last - candidate_vnb->ip.reass.range_first + 1;
959               if (overlap < ip4_full_reass_buffer_get_data_len (candidate_b))
960                 {
961                   candidate_vnb->ip.reass.range_first += overlap;
962                   if (reass->data_len < overlap)
963                     {
964                       return IP4_REASS_RC_INTERNAL_ERROR;
965                     }
966                   reass->data_len -= overlap;
967                   if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
968                     {
969                       ip4_full_reass_add_trace (vm, node, reass,
970                                                 candidate_range_bi,
971                                                 RANGE_SHRINK, 0, ~0);
972                     }
973                   rc = ip4_full_reass_insert_range_in_chain (
974                     vm, reass, prev_range_bi, *bi0);
975                   if (IP4_REASS_RC_OK != rc)
976                     {
977                       return rc;
978                     }
979                   consumed = 1;
980                 }
981               else
982                 {
983                   discard_candidate = 1;
984                 }
985             }
986           else if (fragment_last > candidate_vnb->ip.reass.range_last)
987             {
988               u32 overlap =
989                 candidate_vnb->ip.reass.range_last - fragment_first + 1;
990               if (overlap < ip4_full_reass_buffer_get_data_len (candidate_b))
991                 {
992                   fvnb->ip.reass.range_first += overlap;
993                   if (~0 != candidate_vnb->ip.reass.next_range_bi)
994                     {
995                       prev_range_bi = candidate_range_bi;
996                       candidate_range_bi =
997                         candidate_vnb->ip.reass.next_range_bi;
998                       continue;
999                     }
1000                   else
1001                     {
1002                       // special case - last range discarded
1003                       rc = ip4_full_reass_insert_range_in_chain (
1004                         vm, reass, candidate_range_bi, *bi0);
1005                       if (IP4_REASS_RC_OK != rc)
1006                         {
1007                           return rc;
1008                         }
1009                       consumed = 1;
1010                     }
1011                 }
1012               else
1013                 {
1014                   discard_candidate = 1;
1015                 }
1016             }
1017           else
1018             {
1019               discard_candidate = 1;
1020             }
1021           if (discard_candidate)
1022             {
1023               u32 next_range_bi = candidate_vnb->ip.reass.next_range_bi;
1024               // discard candidate range, probe next range
1025               rc = ip4_full_reass_remove_range_from_chain (
1026                 vm, node, reass, prev_range_bi, candidate_range_bi);
1027               if (IP4_REASS_RC_OK != rc)
1028                 {
1029                   return rc;
1030                 }
1031               if (~0 != next_range_bi)
1032                 {
1033                   candidate_range_bi = next_range_bi;
1034                   continue;
1035                 }
1036               else
1037                 {
1038                   // special case - last range discarded
1039                   rc = ip4_full_reass_insert_range_in_chain (
1040                     vm, reass, prev_range_bi, *bi0);
1041                   if (IP4_REASS_RC_OK != rc)
1042                     {
1043                       return rc;
1044                     }
1045                   consumed = 1;
1046                 }
1047             }
1048         }
1049       break;
1050     }
1051   ++reass->fragments_n;
1052   if (consumed)
1053     {
1054       if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1055         {
1056           ip4_full_reass_add_trace (vm, node, reass, *bi0, RANGE_NEW, 0, ~0);
1057         }
1058     }
1059   if (~0 != reass->last_packet_octet &&
1060       reass->data_len == reass->last_packet_octet + 1)
1061     {
1062       *handoff_thread_idx = reass->sendout_thread_index;
1063       int handoff =
1064         reass->memory_owner_thread_index != reass->sendout_thread_index;
1065       rc =
1066         ip4_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
1067                                  is_custom);
1068       if (IP4_REASS_RC_OK == rc && handoff)
1069         {
1070           rc = IP4_REASS_RC_HANDOFF;
1071         }
1072     }
1073   else
1074     {
1075       if (consumed)
1076         {
1077           *bi0 = ~0;
1078           if (reass->fragments_n > rm->max_reass_len)
1079             {
1080               rc = IP4_REASS_RC_TOO_MANY_FRAGMENTS;
1081             }
1082         }
1083       else
1084         {
1085           *next0 = IP4_FULL_REASS_NEXT_DROP;
1086           *error0 = IP4_ERROR_REASS_DUPLICATE_FRAGMENT;
1087         }
1088     }
1089   return rc;
1090 }
1091
1092 always_inline uword
1093 ip4_full_reass_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1094                        vlib_frame_t * frame, ip4_full_reass_node_type_t type)
1095 {
1096   u32 *from = vlib_frame_vector_args (frame);
1097   u32 n_left_from, n_left_to_next, *to_next, next_index;
1098   ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1099   ip4_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
1100   clib_spinlock_lock (&rt->lock);
1101
1102   n_left_from = frame->n_vectors;
1103   next_index = node->cached_next_index;
1104   while (n_left_from > 0)
1105     {
1106       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1107
1108       while (n_left_from > 0 && n_left_to_next > 0)
1109         {
1110           u32 bi0;
1111           vlib_buffer_t *b0;
1112           u32 next0;
1113           u32 error0 = IP4_ERROR_NONE;
1114
1115           bi0 = from[0];
1116           b0 = vlib_get_buffer (vm, bi0);
1117
1118           ip4_header_t *ip0 = vlib_buffer_get_current (b0);
1119           if (!ip4_get_fragment_more (ip0) && !ip4_get_fragment_offset (ip0))
1120             {
1121               // this is a whole packet - no fragmentation
1122               if (CUSTOM != type)
1123                 {
1124                   next0 = IP4_FULL_REASS_NEXT_INPUT;
1125                 }
1126               else
1127                 {
1128                   next0 = vnet_buffer (b0)->ip.reass.next_index;
1129                 }
1130               goto packet_enqueue;
1131             }
1132           const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
1133           const u32 fragment_length =
1134             clib_net_to_host_u16 (ip0->length) - ip4_header_bytes (ip0);
1135           const u32 fragment_last = fragment_first + fragment_length - 1;
1136           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
1137             {
1138               next0 = IP4_FULL_REASS_NEXT_DROP;
1139               error0 = IP4_ERROR_REASS_MALFORMED_PACKET;
1140               goto packet_enqueue;
1141             }
1142           ip4_full_reass_kv_t kv;
1143           u8 do_handoff = 0;
1144
1145           kv.k.as_u64[0] =
1146             (u64) vec_elt (ip4_main.fib_index_by_sw_if_index,
1147                            vnet_buffer (b0)->sw_if_index[VLIB_RX]) |
1148             (u64) ip0->src_address.as_u32 << 32;
1149           kv.k.as_u64[1] =
1150             (u64) ip0->dst_address.
1151             as_u32 | (u64) ip0->fragment_id << 32 | (u64) ip0->protocol << 48;
1152
1153           ip4_full_reass_t *reass =
1154             ip4_full_reass_find_or_create (vm, node, rm, rt, &kv,
1155                                            &do_handoff);
1156
1157           if (reass)
1158             {
1159               const u32 fragment_first = ip4_get_fragment_offset_bytes (ip0);
1160               if (0 == fragment_first)
1161                 {
1162                   reass->sendout_thread_index = vm->thread_index;
1163                 }
1164             }
1165
1166           if (PREDICT_FALSE (do_handoff))
1167             {
1168               next0 = IP4_FULL_REASS_NEXT_HANDOFF;
1169               vnet_buffer (b0)->ip.reass.owner_thread_index =
1170                 kv.v.memory_owner_thread_index;
1171             }
1172           else if (reass)
1173             {
1174               u32 handoff_thread_idx;
1175               u32 counter = ~0;
1176               switch (ip4_full_reass_update
1177                       (vm, node, rm, rt, reass, &bi0, &next0,
1178                        &error0, CUSTOM == type, &handoff_thread_idx))
1179                 {
1180                 case IP4_REASS_RC_OK:
1181                   /* nothing to do here */
1182                   break;
1183                 case IP4_REASS_RC_HANDOFF:
1184                   next0 = IP4_FULL_REASS_NEXT_HANDOFF;
1185                   b0 = vlib_get_buffer (vm, bi0);
1186                   vnet_buffer (b0)->ip.reass.owner_thread_index =
1187                     handoff_thread_idx;
1188                   break;
1189                 case IP4_REASS_RC_TOO_MANY_FRAGMENTS:
1190                   counter = IP4_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
1191                   break;
1192                 case IP4_REASS_RC_NO_BUF:
1193                   counter = IP4_ERROR_REASS_NO_BUF;
1194                   break;
1195                 case IP4_REASS_RC_INTERNAL_ERROR:
1196                   counter = IP4_ERROR_REASS_INTERNAL_ERROR;
1197                   break;
1198                 }
1199
1200               if (~0 != counter)
1201                 {
1202                   vlib_node_increment_counter (vm, node->node_index, counter,
1203                                                1);
1204                   ip4_full_reass_drop_all (vm, node, reass);
1205                   ip4_full_reass_free (rm, rt, reass);
1206                   goto next_packet;
1207                 }
1208             }
1209           else
1210             {
1211               next0 = IP4_FULL_REASS_NEXT_DROP;
1212               error0 = IP4_ERROR_REASS_LIMIT_REACHED;
1213             }
1214
1215
1216         packet_enqueue:
1217
1218           if (bi0 != ~0)
1219             {
1220               to_next[0] = bi0;
1221               to_next += 1;
1222               n_left_to_next -= 1;
1223
1224               /* bi0 might have been updated by reass_finalize, reload */
1225               b0 = vlib_get_buffer (vm, bi0);
1226               if (IP4_ERROR_NONE != error0)
1227                 {
1228                   b0->error = node->errors[error0];
1229                 }
1230
1231               if (next0 == IP4_FULL_REASS_NEXT_HANDOFF)
1232                 {
1233                   if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1234                     {
1235                       ip4_full_reass_add_trace (
1236                         vm, node, NULL, bi0, HANDOFF, 0,
1237                         vnet_buffer (b0)->ip.reass.owner_thread_index);
1238                     }
1239                 }
1240               else if (FEATURE == type && IP4_ERROR_NONE == error0)
1241                 {
1242                   vnet_feature_next (&next0, b0);
1243                 }
1244               vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1245                                                to_next, n_left_to_next,
1246                                                bi0, next0);
1247               IP4_REASS_DEBUG_BUFFER (bi0, enqueue_next);
1248             }
1249
1250         next_packet:
1251           from += 1;
1252           n_left_from -= 1;
1253         }
1254
1255       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1256     }
1257
1258   clib_spinlock_unlock (&rt->lock);
1259   return frame->n_vectors;
1260 }
1261
1262 static char *ip4_full_reass_error_strings[] = {
1263 #define _(sym, string) string,
1264   foreach_ip4_error
1265 #undef _
1266 };
1267
1268 VLIB_NODE_FN (ip4_full_reass_node) (vlib_main_t * vm,
1269                                     vlib_node_runtime_t * node,
1270                                     vlib_frame_t * frame)
1271 {
1272   return ip4_full_reass_inline (vm, node, frame, NORMAL);
1273 }
1274
1275 VLIB_REGISTER_NODE (ip4_full_reass_node) = {
1276     .name = "ip4-full-reassembly",
1277     .vector_size = sizeof (u32),
1278     .format_trace = format_ip4_full_reass_trace,
1279     .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1280     .error_strings = ip4_full_reass_error_strings,
1281     .n_next_nodes = IP4_FULL_REASS_N_NEXT,
1282     .next_nodes =
1283         {
1284                 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1285                 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1286                 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reassembly-handoff",
1287
1288         },
1289 };
1290
1291 VLIB_NODE_FN (ip4_full_reass_node_feature) (vlib_main_t * vm,
1292                                             vlib_node_runtime_t * node,
1293                                             vlib_frame_t * frame)
1294 {
1295   return ip4_full_reass_inline (vm, node, frame, FEATURE);
1296 }
1297
1298 VLIB_REGISTER_NODE (ip4_full_reass_node_feature) = {
1299     .name = "ip4-full-reassembly-feature",
1300     .vector_size = sizeof (u32),
1301     .format_trace = format_ip4_full_reass_trace,
1302     .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1303     .error_strings = ip4_full_reass_error_strings,
1304     .n_next_nodes = IP4_FULL_REASS_N_NEXT,
1305     .next_nodes =
1306         {
1307                 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1308                 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1309                 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reass-feature-hoff",
1310         },
1311 };
1312
1313 VNET_FEATURE_INIT (ip4_full_reass_feature, static) = {
1314     .arc_name = "ip4-unicast",
1315     .node_name = "ip4-full-reassembly-feature",
1316     .runs_before = VNET_FEATURES ("ip4-lookup",
1317                                   "ipsec4-input-feature"),
1318     .runs_after = 0,
1319 };
1320
1321 VLIB_NODE_FN (ip4_full_reass_node_custom) (vlib_main_t * vm,
1322                                            vlib_node_runtime_t * node,
1323                                            vlib_frame_t * frame)
1324 {
1325   return ip4_full_reass_inline (vm, node, frame, CUSTOM);
1326 }
1327
1328 VLIB_REGISTER_NODE (ip4_full_reass_node_custom) = {
1329     .name = "ip4-full-reassembly-custom",
1330     .vector_size = sizeof (u32),
1331     .format_trace = format_ip4_full_reass_trace,
1332     .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1333     .error_strings = ip4_full_reass_error_strings,
1334     .n_next_nodes = IP4_FULL_REASS_N_NEXT,
1335     .next_nodes =
1336         {
1337                 [IP4_FULL_REASS_NEXT_INPUT] = "ip4-input",
1338                 [IP4_FULL_REASS_NEXT_DROP] = "ip4-drop",
1339                 [IP4_FULL_REASS_NEXT_HANDOFF] = "ip4-full-reass-custom-hoff",
1340         },
1341 };
1342
1343 VNET_FEATURE_INIT (ip4_full_reass_custom, static) = {
1344     .arc_name = "ip4-unicast",
1345     .node_name = "ip4-full-reassembly-feature",
1346     .runs_before = VNET_FEATURES ("ip4-lookup",
1347                                   "ipsec4-input-feature"),
1348     .runs_after = 0,
1349 };
1350
1351
1352 #ifndef CLIB_MARCH_VARIANT
1353 uword
1354 ip4_full_reass_custom_register_next_node (uword node_index)
1355 {
1356   return vlib_node_add_next (vlib_get_main (),
1357                              ip4_full_reass_node_custom.index, node_index);
1358 }
1359
1360 always_inline u32
1361 ip4_full_reass_get_nbuckets ()
1362 {
1363   ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1364   u32 nbuckets;
1365   u8 i;
1366
1367   nbuckets = (u32) (rm->max_reass_n / IP4_REASS_HT_LOAD_FACTOR);
1368
1369   for (i = 0; i < 31; i++)
1370     if ((1 << i) >= nbuckets)
1371       break;
1372   nbuckets = 1 << i;
1373
1374   return nbuckets;
1375 }
1376 #endif /* CLIB_MARCH_VARIANT */
1377
1378 typedef enum
1379 {
1380   IP4_EVENT_CONFIG_CHANGED = 1,
1381 } ip4_full_reass_event_t;
1382
1383 typedef struct
1384 {
1385   int failure;
1386   clib_bihash_16_8_t *new_hash;
1387 } ip4_rehash_cb_ctx;
1388
1389 #ifndef CLIB_MARCH_VARIANT
1390 static int
1391 ip4_rehash_cb (clib_bihash_kv_16_8_t * kv, void *_ctx)
1392 {
1393   ip4_rehash_cb_ctx *ctx = _ctx;
1394   if (clib_bihash_add_del_16_8 (ctx->new_hash, kv, 1))
1395     {
1396       ctx->failure = 1;
1397     }
1398   return (BIHASH_WALK_CONTINUE);
1399 }
1400
1401 static void
1402 ip4_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1403                            u32 max_reassembly_length,
1404                            u32 expire_walk_interval_ms)
1405 {
1406   ip4_full_reass_main.timeout_ms = timeout_ms;
1407   ip4_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1408   ip4_full_reass_main.max_reass_n = max_reassemblies;
1409   ip4_full_reass_main.max_reass_len = max_reassembly_length;
1410   ip4_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1411 }
1412
1413 vnet_api_error_t
1414 ip4_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1415                     u32 max_reassembly_length, u32 expire_walk_interval_ms)
1416 {
1417   u32 old_nbuckets = ip4_full_reass_get_nbuckets ();
1418   ip4_full_reass_set_params (timeout_ms, max_reassemblies,
1419                              max_reassembly_length, expire_walk_interval_ms);
1420   vlib_process_signal_event (ip4_full_reass_main.vlib_main,
1421                              ip4_full_reass_main.ip4_full_reass_expire_node_idx,
1422                              IP4_EVENT_CONFIG_CHANGED, 0);
1423   u32 new_nbuckets = ip4_full_reass_get_nbuckets ();
1424   if (ip4_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
1425     {
1426       clib_bihash_16_8_t new_hash;
1427       clib_memset (&new_hash, 0, sizeof (new_hash));
1428       ip4_rehash_cb_ctx ctx;
1429       ctx.failure = 0;
1430       ctx.new_hash = &new_hash;
1431       clib_bihash_init_16_8 (&new_hash, "ip4-dr", new_nbuckets,
1432                              new_nbuckets * 1024);
1433       clib_bihash_foreach_key_value_pair_16_8 (&ip4_full_reass_main.hash,
1434                                                ip4_rehash_cb, &ctx);
1435       if (ctx.failure)
1436         {
1437           clib_bihash_free_16_8 (&new_hash);
1438           return -1;
1439         }
1440       else
1441         {
1442           clib_bihash_free_16_8 (&ip4_full_reass_main.hash);
1443           clib_memcpy_fast (&ip4_full_reass_main.hash, &new_hash,
1444                             sizeof (ip4_full_reass_main.hash));
1445           clib_bihash_copied (&ip4_full_reass_main.hash, &new_hash);
1446         }
1447     }
1448   return 0;
1449 }
1450
1451 vnet_api_error_t
1452 ip4_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1453                     u32 * max_reassembly_length,
1454                     u32 * expire_walk_interval_ms)
1455 {
1456   *timeout_ms = ip4_full_reass_main.timeout_ms;
1457   *max_reassemblies = ip4_full_reass_main.max_reass_n;
1458   *max_reassembly_length = ip4_full_reass_main.max_reass_len;
1459   *expire_walk_interval_ms = ip4_full_reass_main.expire_walk_interval_ms;
1460   return 0;
1461 }
1462
1463 static clib_error_t *
1464 ip4_full_reass_init_function (vlib_main_t * vm)
1465 {
1466   ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1467   clib_error_t *error = 0;
1468   u32 nbuckets;
1469   vlib_node_t *node;
1470
1471   rm->vlib_main = vm;
1472
1473   vec_validate (rm->per_thread_data, vlib_num_workers ());
1474   ip4_full_reass_per_thread_t *rt;
1475   vec_foreach (rt, rm->per_thread_data)
1476   {
1477     clib_spinlock_init (&rt->lock);
1478     pool_alloc (rt->pool, rm->max_reass_n);
1479   }
1480
1481   node = vlib_get_node_by_name (vm, (u8 *) "ip4-full-reassembly-expire-walk");
1482   ASSERT (node);
1483   rm->ip4_full_reass_expire_node_idx = node->index;
1484
1485   ip4_full_reass_set_params (IP4_REASS_TIMEOUT_DEFAULT_MS,
1486                              IP4_REASS_MAX_REASSEMBLIES_DEFAULT,
1487                              IP4_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1488                              IP4_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1489
1490   nbuckets = ip4_full_reass_get_nbuckets ();
1491   clib_bihash_init_16_8 (&rm->hash, "ip4-dr", nbuckets, nbuckets * 1024);
1492
1493   node = vlib_get_node_by_name (vm, (u8 *) "ip4-drop");
1494   ASSERT (node);
1495   rm->ip4_drop_idx = node->index;
1496
1497   rm->fq_index = vlib_frame_queue_main_init (ip4_full_reass_node.index, 0);
1498   rm->fq_feature_index =
1499     vlib_frame_queue_main_init (ip4_full_reass_node_feature.index, 0);
1500   rm->fq_custom_index =
1501     vlib_frame_queue_main_init (ip4_full_reass_node_custom.index, 0);
1502
1503   rm->feature_use_refcount_per_intf = NULL;
1504   return error;
1505 }
1506
1507 VLIB_INIT_FUNCTION (ip4_full_reass_init_function);
1508 #endif /* CLIB_MARCH_VARIANT */
1509
1510 static uword
1511 ip4_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
1512                              CLIB_UNUSED (vlib_frame_t *f))
1513 {
1514   ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1515   uword event_type, *event_data = 0;
1516
1517   while (true)
1518     {
1519       vlib_process_wait_for_event_or_clock (vm,
1520                                             (f64)
1521                                             rm->expire_walk_interval_ms /
1522                                             (f64) MSEC_PER_SEC);
1523       event_type = vlib_process_get_events (vm, &event_data);
1524
1525       switch (event_type)
1526         {
1527         case ~0:
1528           /* no events => timeout */
1529           /* fallthrough */
1530         case IP4_EVENT_CONFIG_CHANGED:
1531           /* nothing to do here */
1532           break;
1533         default:
1534           clib_warning ("BUG: event type 0x%wx", event_type);
1535           break;
1536         }
1537       f64 now = vlib_time_now (vm);
1538
1539       ip4_full_reass_t *reass;
1540       int *pool_indexes_to_free = NULL;
1541
1542       uword thread_index = 0;
1543       int index;
1544       const uword nthreads = vlib_num_workers () + 1;
1545       for (thread_index = 0; thread_index < nthreads; ++thread_index)
1546         {
1547           ip4_full_reass_per_thread_t *rt =
1548             &rm->per_thread_data[thread_index];
1549           clib_spinlock_lock (&rt->lock);
1550
1551           vec_reset_length (pool_indexes_to_free);
1552           pool_foreach_index (index, rt->pool)  {
1553                                 reass = pool_elt_at_index (rt->pool, index);
1554                                 if (now > reass->last_heard + rm->timeout)
1555                                   {
1556                                     vec_add1 (pool_indexes_to_free, index);
1557                                   }
1558                               }
1559           int *i;
1560           vec_foreach (i, pool_indexes_to_free)
1561           {
1562             ip4_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1563             ip4_full_reass_drop_all (vm, node, reass);
1564             ip4_full_reass_free (rm, rt, reass);
1565           }
1566
1567           clib_spinlock_unlock (&rt->lock);
1568         }
1569
1570       vec_free (pool_indexes_to_free);
1571       if (event_data)
1572         {
1573           _vec_len (event_data) = 0;
1574         }
1575     }
1576
1577   return 0;
1578 }
1579
1580 VLIB_REGISTER_NODE (ip4_full_reass_expire_node) = {
1581     .function = ip4_full_reass_walk_expired,
1582     .type = VLIB_NODE_TYPE_PROCESS,
1583     .name = "ip4-full-reassembly-expire-walk",
1584     .format_trace = format_ip4_full_reass_trace,
1585     .n_errors = ARRAY_LEN (ip4_full_reass_error_strings),
1586     .error_strings = ip4_full_reass_error_strings,
1587
1588 };
1589
1590 static u8 *
1591 format_ip4_full_reass_key (u8 * s, va_list * args)
1592 {
1593   ip4_full_reass_key_t *key = va_arg (*args, ip4_full_reass_key_t *);
1594   s =
1595     format (s,
1596             "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1597             key->xx_id, format_ip4_address, &key->src, format_ip4_address,
1598             &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1599   return s;
1600 }
1601
1602 static u8 *
1603 format_ip4_reass (u8 * s, va_list * args)
1604 {
1605   vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1606   ip4_full_reass_t *reass = va_arg (*args, ip4_full_reass_t *);
1607
1608   s = format (s, "ID: %lu, key: %U\n  first_bi: %u, data_len: %u, "
1609               "last_packet_octet: %u, trace_op_counter: %u\n",
1610               reass->id, format_ip4_full_reass_key, &reass->key,
1611               reass->first_bi, reass->data_len,
1612               reass->last_packet_octet, reass->trace_op_counter);
1613
1614   u32 bi = reass->first_bi;
1615   u32 counter = 0;
1616   while (~0 != bi)
1617     {
1618       vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1619       vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1620       s =
1621         format (s,
1622                 "  #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1623                 "fragment[%u, %u]\n", counter, vnb->ip.reass.range_first,
1624                 vnb->ip.reass.range_last, bi,
1625                 ip4_full_reass_buffer_get_data_offset (b),
1626                 ip4_full_reass_buffer_get_data_len (b),
1627                 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1628       if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1629         {
1630           bi = b->next_buffer;
1631         }
1632       else
1633         {
1634           bi = ~0;
1635         }
1636     }
1637   return s;
1638 }
1639
1640 static clib_error_t *
1641 show_ip4_reass (vlib_main_t * vm,
1642                 unformat_input_t * input,
1643                 CLIB_UNUSED (vlib_cli_command_t * lmd))
1644 {
1645   ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1646
1647   vlib_cli_output (vm, "---------------------");
1648   vlib_cli_output (vm, "IP4 reassembly status");
1649   vlib_cli_output (vm, "---------------------");
1650   bool details = false;
1651   if (unformat (input, "details"))
1652     {
1653       details = true;
1654     }
1655
1656   u32 sum_reass_n = 0;
1657   ip4_full_reass_t *reass;
1658   uword thread_index;
1659   const uword nthreads = vlib_num_workers () + 1;
1660   for (thread_index = 0; thread_index < nthreads; ++thread_index)
1661     {
1662       ip4_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1663       clib_spinlock_lock (&rt->lock);
1664       if (details)
1665         {
1666           pool_foreach (reass, rt->pool) {
1667             vlib_cli_output (vm, "%U", format_ip4_reass, vm, reass);
1668           }
1669         }
1670       sum_reass_n += rt->reass_n;
1671       clib_spinlock_unlock (&rt->lock);
1672     }
1673   vlib_cli_output (vm, "---------------------");
1674   vlib_cli_output (vm, "Current full IP4 reassemblies count: %lu\n",
1675                    (long unsigned) sum_reass_n);
1676   vlib_cli_output (vm,
1677                    "Maximum configured concurrent full IP4 reassemblies per worker-thread: %lu\n",
1678                    (long unsigned) rm->max_reass_n);
1679   vlib_cli_output (vm,
1680                    "Maximum configured amount of fragments "
1681                    "per full IP4 reassembly: %lu\n",
1682                    (long unsigned) rm->max_reass_len);
1683   vlib_cli_output (vm,
1684                    "Maximum configured full IP4 reassembly timeout: %lums\n",
1685                    (long unsigned) rm->timeout_ms);
1686   vlib_cli_output (vm,
1687                    "Maximum configured full IP4 reassembly expire walk interval: %lums\n",
1688                    (long unsigned) rm->expire_walk_interval_ms);
1689   return 0;
1690 }
1691
1692 VLIB_CLI_COMMAND (show_ip4_full_reass_cmd, static) = {
1693     .path = "show ip4-full-reassembly",
1694     .short_help = "show ip4-full-reassembly [details]",
1695     .function = show_ip4_reass,
1696 };
1697
1698 #ifndef CLIB_MARCH_VARIANT
1699 vnet_api_error_t
1700 ip4_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1701 {
1702   return vnet_feature_enable_disable ("ip4-unicast",
1703                                       "ip4-full-reassembly-feature",
1704                                       sw_if_index, enable_disable, 0, 0);
1705 }
1706 #endif /* CLIB_MARCH_VARIANT */
1707
1708
1709 #define foreach_ip4_full_reass_handoff_error                       \
1710 _(CONGESTION_DROP, "congestion drop")
1711
1712
1713 typedef enum
1714 {
1715 #define _(sym,str) IP4_FULL_REASS_HANDOFF_ERROR_##sym,
1716   foreach_ip4_full_reass_handoff_error
1717 #undef _
1718     IP4_FULL_REASS_HANDOFF_N_ERROR,
1719 } ip4_full_reass_handoff_error_t;
1720
1721 static char *ip4_full_reass_handoff_error_strings[] = {
1722 #define _(sym,string) string,
1723   foreach_ip4_full_reass_handoff_error
1724 #undef _
1725 };
1726
1727 typedef struct
1728 {
1729   u32 next_worker_index;
1730 } ip4_full_reass_handoff_trace_t;
1731
1732 static u8 *
1733 format_ip4_full_reass_handoff_trace (u8 * s, va_list * args)
1734 {
1735   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1736   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1737   ip4_full_reass_handoff_trace_t *t =
1738     va_arg (*args, ip4_full_reass_handoff_trace_t *);
1739
1740   s =
1741     format (s, "ip4-full-reassembly-handoff: next-worker %d",
1742             t->next_worker_index);
1743
1744   return s;
1745 }
1746
1747 always_inline uword
1748 ip4_full_reass_handoff_node_inline (vlib_main_t * vm,
1749                                     vlib_node_runtime_t * node,
1750                                     vlib_frame_t * frame,
1751                                     ip4_full_reass_node_type_t type)
1752 {
1753   ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1754
1755   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1756   u32 n_enq, n_left_from, *from;
1757   u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1758   u32 fq_index;
1759
1760   from = vlib_frame_vector_args (frame);
1761   n_left_from = frame->n_vectors;
1762   vlib_get_buffers (vm, from, bufs, n_left_from);
1763
1764   b = bufs;
1765   ti = thread_indices;
1766
1767   switch (type)
1768     {
1769     case NORMAL:
1770       fq_index = rm->fq_index;
1771       break;
1772     case FEATURE:
1773       fq_index = rm->fq_feature_index;
1774       break;
1775     case CUSTOM:
1776       fq_index = rm->fq_custom_index;
1777       break;
1778     default:
1779       clib_warning ("Unexpected `type' (%d)!", type);
1780       ASSERT (0);
1781     }
1782
1783   while (n_left_from > 0)
1784     {
1785       ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1786
1787       if (PREDICT_FALSE
1788           ((node->flags & VLIB_NODE_FLAG_TRACE)
1789            && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1790         {
1791           ip4_full_reass_handoff_trace_t *t =
1792             vlib_add_trace (vm, node, b[0], sizeof (*t));
1793           t->next_worker_index = ti[0];
1794         }
1795
1796       n_left_from -= 1;
1797       ti += 1;
1798       b += 1;
1799     }
1800   n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
1801                                          thread_indices, frame->n_vectors, 1);
1802
1803   if (n_enq < frame->n_vectors)
1804     vlib_node_increment_counter (vm, node->node_index,
1805                                  IP4_FULL_REASS_HANDOFF_ERROR_CONGESTION_DROP,
1806                                  frame->n_vectors - n_enq);
1807   return frame->n_vectors;
1808 }
1809
1810 VLIB_NODE_FN (ip4_full_reass_handoff_node) (vlib_main_t * vm,
1811                                             vlib_node_runtime_t * node,
1812                                             vlib_frame_t * frame)
1813 {
1814   return ip4_full_reass_handoff_node_inline (vm, node, frame, NORMAL);
1815 }
1816
1817
1818 VLIB_REGISTER_NODE (ip4_full_reass_handoff_node) = {
1819   .name = "ip4-full-reassembly-handoff",
1820   .vector_size = sizeof (u32),
1821   .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
1822   .error_strings = ip4_full_reass_handoff_error_strings,
1823   .format_trace = format_ip4_full_reass_handoff_trace,
1824
1825   .n_next_nodes = 1,
1826
1827   .next_nodes = {
1828     [0] = "error-drop",
1829   },
1830 };
1831
1832
1833 VLIB_NODE_FN (ip4_full_reass_feature_handoff_node) (vlib_main_t * vm,
1834                                                     vlib_node_runtime_t *
1835                                                     node,
1836                                                     vlib_frame_t * frame)
1837 {
1838   return ip4_full_reass_handoff_node_inline (vm, node, frame, FEATURE);
1839 }
1840
1841
1842 VLIB_REGISTER_NODE (ip4_full_reass_feature_handoff_node) = {
1843   .name = "ip4-full-reass-feature-hoff",
1844   .vector_size = sizeof (u32),
1845   .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
1846   .error_strings = ip4_full_reass_handoff_error_strings,
1847   .format_trace = format_ip4_full_reass_handoff_trace,
1848
1849   .n_next_nodes = 1,
1850
1851   .next_nodes = {
1852     [0] = "error-drop",
1853   },
1854 };
1855
1856 VLIB_NODE_FN (ip4_full_reass_custom_handoff_node) (vlib_main_t * vm,
1857                                                     vlib_node_runtime_t *
1858                                                     node,
1859                                                     vlib_frame_t * frame)
1860 {
1861   return ip4_full_reass_handoff_node_inline (vm, node, frame, CUSTOM);
1862 }
1863
1864
1865 VLIB_REGISTER_NODE (ip4_full_reass_custom_handoff_node) = {
1866   .name = "ip4-full-reass-custom-hoff",
1867   .vector_size = sizeof (u32),
1868   .n_errors = ARRAY_LEN(ip4_full_reass_handoff_error_strings),
1869   .error_strings = ip4_full_reass_handoff_error_strings,
1870   .format_trace = format_ip4_full_reass_handoff_trace,
1871
1872   .n_next_nodes = 1,
1873
1874   .next_nodes = {
1875     [0] = "error-drop",
1876   },
1877 };
1878
1879 #ifndef CLIB_MARCH_VARIANT
1880 int
1881 ip4_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1882 {
1883   ip4_full_reass_main_t *rm = &ip4_full_reass_main;
1884   vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1885   if (is_enable)
1886     {
1887       if (!rm->feature_use_refcount_per_intf[sw_if_index])
1888         {
1889           ++rm->feature_use_refcount_per_intf[sw_if_index];
1890           return vnet_feature_enable_disable ("ip4-unicast",
1891                                               "ip4-full-reassembly-feature",
1892                                               sw_if_index, 1, 0, 0);
1893         }
1894       ++rm->feature_use_refcount_per_intf[sw_if_index];
1895     }
1896   else
1897     {
1898       --rm->feature_use_refcount_per_intf[sw_if_index];
1899       if (!rm->feature_use_refcount_per_intf[sw_if_index])
1900         return vnet_feature_enable_disable ("ip4-unicast",
1901                                             "ip4-full-reassembly-feature",
1902                                             sw_if_index, 0, 0, 0);
1903     }
1904   return -1;
1905 }
1906 #endif
1907
1908 /*
1909  * fd.io coding-style-patch-verification: ON
1910  *
1911  * Local Variables:
1912  * eval: (c-set-style "gnu")
1913  * End:
1914  */