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