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