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