hs-test: added filenames to test names
[vpp.git] / src / vnet / ip / reass / ip6_sv_reass.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /**
17  * @file
18  * @brief IPv6 Shallow Virtual Reassembly.
19  *
20  * This file contains the source code for IPv6 Shallow Virtual reassembly.
21  */
22
23 #include <vppinfra/vec.h>
24 #include <vnet/vnet.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/ip/ip6_to_ip4.h>
27 #include <vppinfra/bihash_48_8.h>
28 #include <vnet/ip/reass/ip6_sv_reass.h>
29 #include <vnet/ip/ip6_inlines.h>
30
31 #define MSEC_PER_SEC 1000
32 #define IP6_SV_REASS_TIMEOUT_DEFAULT_MS 100
33 #define IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000      // 10 seconds default
34 #define IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT 1024
35 #define IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
36 #define IP6_SV_REASS_HT_LOAD_FACTOR (0.75)
37
38 typedef enum
39 {
40   IP6_SV_REASS_RC_OK,
41   IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS,
42   IP6_SV_REASS_RC_INTERNAL_ERROR,
43   IP6_SV_REASS_RC_UNSUPP_IP_PROTO,
44   IP6_SV_REASS_RC_INVALID_FRAG_LEN,
45 } ip6_sv_reass_rc_t;
46
47 typedef struct
48 {
49   union
50   {
51     struct
52     {
53       ip6_address_t src;
54       ip6_address_t dst;
55       u32 fib_index;
56       u32 frag_id;
57       u8 unused[7];
58       u8 proto;
59     };
60     u64 as_u64[6];
61   };
62 } ip6_sv_reass_key_t;
63
64 typedef union
65 {
66   struct
67   {
68     u32 reass_index;
69     u32 thread_index;
70   };
71   u64 as_u64;
72 } ip6_sv_reass_val_t;
73
74 typedef union
75 {
76   struct
77   {
78     ip6_sv_reass_key_t k;
79     ip6_sv_reass_val_t v;
80   };
81   clib_bihash_kv_48_8_t kv;
82 } ip6_sv_reass_kv_t;
83
84 typedef struct
85 {
86   // hash table key
87   ip6_sv_reass_key_t key;
88   // time when last packet was received
89   f64 last_heard;
90   // internal id of this reassembly
91   u64 id;
92   // trace operation counter
93   u32 trace_op_counter;
94   // buffer indexes of buffers in this reassembly in chronological order -
95   // including overlaps and duplicate fragments
96   u32 *cached_buffers;
97   // set to true when this reassembly is completed
98   bool is_complete;
99   // ip protocol
100   u8 ip_proto;
101   u8 icmp_type_or_tcp_flags;
102   u32 tcp_ack_number;
103   u32 tcp_seq_number;
104   // l4 src port
105   u16 l4_src_port;
106   // l4 dst port
107   u16 l4_dst_port;
108   // lru indexes
109   u32 lru_prev;
110   u32 lru_next;
111 } ip6_sv_reass_t;
112
113 typedef struct
114 {
115   ip6_sv_reass_t *pool;
116   u32 reass_n;
117   u32 id_counter;
118   clib_spinlock_t lock;
119   // lru indexes
120   u32 lru_first;
121   u32 lru_last;
122 } ip6_sv_reass_per_thread_t;
123
124 typedef struct
125 {
126   // IPv6 config
127   u32 timeout_ms;
128   f64 timeout;
129   u32 expire_walk_interval_ms;
130   // maximum number of fragments in one reassembly
131   u32 max_reass_len;
132   // maximum number of reassemblies
133   u32 max_reass_n;
134
135   // IPv6 runtime
136   clib_bihash_48_8_t hash;
137
138   // per-thread data
139   ip6_sv_reass_per_thread_t *per_thread_data;
140
141   // convenience
142   vlib_main_t *vlib_main;
143   vnet_main_t *vnet_main;
144
145   // node index of ip6-drop node
146   u32 ip6_drop_idx;
147   u32 ip6_icmp_error_idx;
148   u32 ip6_sv_reass_expire_node_idx;
149
150   /** Worker handoff */
151   u32 fq_index;
152   u32 fq_feature_index;
153   u32 fq_custom_context_index;
154
155   // reference count for enabling/disabling feature - per interface
156   u32 *feature_use_refcount_per_intf;
157 } ip6_sv_reass_main_t;
158
159 extern ip6_sv_reass_main_t ip6_sv_reass_main;
160
161 #ifndef CLIB_MARCH_VARIANT
162 ip6_sv_reass_main_t ip6_sv_reass_main;
163 #endif /* CLIB_MARCH_VARIANT */
164
165 typedef enum
166 {
167   IP6_SV_REASSEMBLY_NEXT_INPUT,
168   IP6_SV_REASSEMBLY_NEXT_DROP,
169   IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR,
170   IP6_SV_REASSEMBLY_NEXT_HANDOFF,
171   IP6_SV_REASSEMBLY_N_NEXT,
172 } ip6_sv_reass_next_t;
173
174 typedef enum
175 {
176   REASS_FRAGMENT_CACHE,
177   REASS_FINISH,
178   REASS_FRAGMENT_FORWARD,
179   REASS_PASSTHROUGH,
180 } ip6_sv_reass_trace_operation_e;
181
182 typedef struct
183 {
184   ip6_sv_reass_trace_operation_e action;
185   u32 reass_id;
186   u32 op_id;
187   u8 ip_proto;
188   u16 l4_src_port;
189   u16 l4_dst_port;
190 } ip6_sv_reass_trace_t;
191
192 static u8 *
193 format_ip6_sv_reass_trace (u8 * s, va_list * args)
194 {
195   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
196   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
197   ip6_sv_reass_trace_t *t = va_arg (*args, ip6_sv_reass_trace_t *);
198   if (REASS_PASSTHROUGH != t->action)
199     {
200       s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
201     }
202   switch (t->action)
203     {
204     case REASS_FRAGMENT_CACHE:
205       s = format (s, "[cached]");
206       break;
207     case REASS_FINISH:
208       s =
209         format (s, "[finish, ip proto=%u, src_port=%u, dst_port=%u]",
210                 t->ip_proto, clib_net_to_host_u16 (t->l4_src_port),
211                 clib_net_to_host_u16 (t->l4_dst_port));
212       break;
213     case REASS_FRAGMENT_FORWARD:
214       s =
215         format (s, "[forward, ip proto=%u, src_port=%u, dst_port=%u]",
216                 t->ip_proto, clib_net_to_host_u16 (t->l4_src_port),
217                 clib_net_to_host_u16 (t->l4_dst_port));
218       break;
219     case REASS_PASSTHROUGH:
220       s = format (s, "[not fragmented or atomic fragment]");
221       break;
222     }
223   return s;
224 }
225
226 static void
227 ip6_sv_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
228                         ip6_sv_reass_t * reass, u32 bi,
229                         ip6_sv_reass_trace_operation_e action,
230                         u32 ip_proto, u16 l4_src_port, u16 l4_dst_port)
231 {
232   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
233   if (pool_is_free_index
234       (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
235     {
236       // this buffer's trace is gone
237       b->flags &= ~VLIB_BUFFER_IS_TRACED;
238       return;
239     }
240   ip6_sv_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
241   if (reass)
242     {
243       t->reass_id = reass->id;
244       t->op_id = reass->trace_op_counter;
245       ++reass->trace_op_counter;
246     }
247   t->action = action;
248   t->ip_proto = ip_proto;
249   t->l4_src_port = l4_src_port;
250   t->l4_dst_port = l4_dst_port;
251 #if 0
252   static u8 *s = NULL;
253   s = format (s, "%U", format_ip6_sv_reass_trace, NULL, NULL, t);
254   printf ("%.*s\n", vec_len (s), s);
255   fflush (stdout);
256   vec_reset_length (s);
257 #endif
258 }
259
260 always_inline void
261 ip6_sv_reass_free (vlib_main_t * vm, ip6_sv_reass_main_t * rm,
262                    ip6_sv_reass_per_thread_t * rt, ip6_sv_reass_t * reass)
263 {
264   clib_bihash_kv_48_8_t kv;
265   kv.key[0] = reass->key.as_u64[0];
266   kv.key[1] = reass->key.as_u64[1];
267   kv.key[2] = reass->key.as_u64[2];
268   kv.key[3] = reass->key.as_u64[3];
269   kv.key[4] = reass->key.as_u64[4];
270   kv.key[5] = reass->key.as_u64[5];
271   clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
272   vlib_buffer_free (vm, reass->cached_buffers,
273                     vec_len (reass->cached_buffers));
274   vec_free (reass->cached_buffers);
275   reass->cached_buffers = NULL;
276   if (~0 != reass->lru_prev)
277     {
278       ip6_sv_reass_t *lru_prev =
279         pool_elt_at_index (rt->pool, reass->lru_prev);
280       lru_prev->lru_next = reass->lru_next;
281     }
282   if (~0 != reass->lru_next)
283     {
284       ip6_sv_reass_t *lru_next =
285         pool_elt_at_index (rt->pool, reass->lru_next);
286       lru_next->lru_prev = reass->lru_prev;
287     }
288   if (rt->lru_first == reass - rt->pool)
289     {
290       rt->lru_first = reass->lru_next;
291     }
292   if (rt->lru_last == reass - rt->pool)
293     {
294       rt->lru_last = reass->lru_prev;
295     }
296   pool_put (rt->pool, reass);
297   --rt->reass_n;
298 }
299
300 always_inline void
301 ip6_sv_reass_init (ip6_sv_reass_t * reass)
302 {
303   reass->cached_buffers = NULL;
304   reass->is_complete = false;
305 }
306
307 always_inline ip6_sv_reass_t *
308 ip6_sv_reass_find_or_create (vlib_main_t *vm, ip6_sv_reass_main_t *rm,
309                              ip6_sv_reass_per_thread_t *rt,
310                              ip6_sv_reass_kv_t *kv, u8 *do_handoff)
311 {
312   ip6_sv_reass_t *reass = NULL;
313   f64 now = vlib_time_now (vm);
314
315 again:
316
317   if (!clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
318     {
319       if (vm->thread_index != kv->v.thread_index)
320         {
321           *do_handoff = 1;
322           return NULL;
323         }
324       reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
325
326       if (now > reass->last_heard + rm->timeout)
327         {
328           ip6_sv_reass_free (vm, rm, rt, reass);
329           reass = NULL;
330         }
331     }
332
333   if (reass)
334     {
335       reass->last_heard = now;
336       return reass;
337     }
338
339   if (rt->reass_n >= rm->max_reass_n)
340     {
341       reass = pool_elt_at_index (rt->pool, rt->lru_first);
342       ip6_sv_reass_free (vm, rm, rt, reass);
343     }
344
345   pool_get (rt->pool, reass);
346   clib_memset (reass, 0, sizeof (*reass));
347   reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
348   ++rt->id_counter;
349   ip6_sv_reass_init (reass);
350   ++rt->reass_n;
351
352   reass->lru_prev = reass->lru_next = ~0;
353
354   if (~0 != rt->lru_last)
355     {
356       ip6_sv_reass_t *lru_last = pool_elt_at_index (rt->pool, rt->lru_last);
357       reass->lru_prev = rt->lru_last;
358       lru_last->lru_next = rt->lru_last = reass - rt->pool;
359     }
360
361   if (~0 == rt->lru_first)
362     {
363       rt->lru_first = rt->lru_last = reass - rt->pool;
364     }
365
366   reass->key.as_u64[0] = kv->kv.key[0];
367   reass->key.as_u64[1] = kv->kv.key[1];
368   reass->key.as_u64[2] = kv->kv.key[2];
369   reass->key.as_u64[3] = kv->kv.key[3];
370   reass->key.as_u64[4] = kv->kv.key[4];
371   reass->key.as_u64[5] = kv->kv.key[5];
372   kv->v.reass_index = (reass - rt->pool);
373   kv->v.thread_index = vm->thread_index;
374   reass->last_heard = now;
375
376   int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
377   if (rv)
378     {
379       ip6_sv_reass_free (vm, rm, rt, reass);
380       reass = NULL;
381       // if other worker created a context already work with the other copy
382       if (-2 == rv)
383         goto again;
384     }
385
386   return reass;
387 }
388
389 always_inline ip6_sv_reass_rc_t
390 ip6_sv_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
391                      ip6_sv_reass_main_t *rm, ip6_sv_reass_t *reass, u32 bi0,
392                      ip6_frag_hdr_t *frag_hdr)
393 {
394   vlib_buffer_t *fb = vlib_get_buffer (vm, bi0);
395   vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
396   fvnb->ip.reass.ip6_frag_hdr_offset =
397     (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
398   ip6_header_t *fip = vlib_buffer_get_current (fb);
399   if (fb->current_length < sizeof (*fip) ||
400       fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
401       fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
402     {
403       return IP6_SV_REASS_RC_INTERNAL_ERROR;
404     }
405
406   u32 fragment_first = fvnb->ip.reass.fragment_first =
407     ip6_frag_hdr_offset_bytes (frag_hdr);
408   u32 fragment_length =
409     vlib_buffer_length_in_chain (vm, fb) -
410     (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
411   if (0 == fragment_length)
412     {
413       return IP6_SV_REASS_RC_INVALID_FRAG_LEN;
414     }
415   u32 fragment_last = fvnb->ip.reass.fragment_last =
416     fragment_first + fragment_length - 1;
417   fvnb->ip.reass.range_first = fragment_first;
418   fvnb->ip.reass.range_last = fragment_last;
419   fvnb->ip.reass.next_range_bi = ~0;
420   if (0 == fragment_first)
421     {
422       if (!ip6_get_port
423           (vm, fb, fip, fb->current_length, &reass->ip_proto,
424            &reass->l4_src_port, &reass->l4_dst_port,
425            &reass->icmp_type_or_tcp_flags, &reass->tcp_ack_number,
426            &reass->tcp_seq_number))
427         return IP6_SV_REASS_RC_UNSUPP_IP_PROTO;
428
429       reass->is_complete = true;
430       vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
431       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
432         {
433           ip6_sv_reass_add_trace (vm, node, reass, bi0, REASS_FINISH,
434                                   reass->ip_proto, reass->l4_src_port,
435                                   reass->l4_dst_port);
436         }
437     }
438   vec_add1 (reass->cached_buffers, bi0);
439   if (!reass->is_complete)
440     {
441       if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
442         {
443           ip6_sv_reass_add_trace (vm, node, reass, bi0, REASS_FRAGMENT_CACHE,
444                                   reass->ip_proto, reass->l4_src_port,
445                                   reass->l4_dst_port);
446         }
447       if (vec_len (reass->cached_buffers) > rm->max_reass_len)
448         {
449           return IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS;
450         }
451     }
452   return IP6_SV_REASS_RC_OK;
453 }
454
455 always_inline bool
456 ip6_sv_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
457                                          vlib_buffer_t *b,
458                                          ip6_ext_hdr_chain_t *hc)
459 {
460   int nh = hc->eh[hc->length - 1].protocol;
461   /* Checking to see if it's a terminating header */
462   if (ip6_ext_hdr (nh))
463     {
464       icmp6_error_set_vnet_buffer (
465         b, ICMP6_parameter_problem,
466         ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
467       b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
468       return false;
469     }
470   return true;
471 }
472
473 always_inline bool
474 ip6_sv_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
475                                          vlib_buffer_t * b,
476                                          ip6_frag_hdr_t * frag_hdr)
477 {
478   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
479   ip6_header_t *ip = vlib_buffer_get_current (b);
480   int more_fragments = ip6_frag_hdr_more (frag_hdr);
481   u32 fragment_length =
482     vlib_buffer_length_in_chain (vm, b) -
483     (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
484   if (more_fragments && 0 != fragment_length % 8)
485     {
486       icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
487                                    ICMP6_parameter_problem_erroneous_header_field,
488                                    (u8 *) & ip->payload_length - (u8 *) ip);
489       return false;
490     }
491   return true;
492 }
493
494 always_inline bool
495 ip6_sv_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
496                                         vlib_buffer_t * b,
497                                         ip6_frag_hdr_t * frag_hdr)
498 {
499   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
500   u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
501   u32 fragment_length =
502     vlib_buffer_length_in_chain (vm, b) -
503     (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
504   if (fragment_first + fragment_length > 65535)
505     {
506       ip6_header_t *ip0 = vlib_buffer_get_current (b);
507       icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
508                                    ICMP6_parameter_problem_erroneous_header_field,
509                                    (u8 *) & frag_hdr->fragment_offset_and_more
510                                    - (u8 *) ip0);
511       return false;
512     }
513   return true;
514 }
515
516 always_inline uword
517 ip6_sv_reassembly_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
518                           vlib_frame_t *frame, bool is_feature,
519                           bool custom_next, bool custom_context)
520 {
521   u32 *from = vlib_frame_vector_args (frame);
522   u32 n_left_from, n_left_to_next, *to_next, *to_next_aux, next_index;
523   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
524   ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
525   u32 *context;
526   if (custom_context)
527     context = vlib_frame_aux_args (frame);
528
529   clib_spinlock_lock (&rt->lock);
530
531   n_left_from = frame->n_vectors;
532   next_index = node->cached_next_index;
533
534   while (n_left_from > 0)
535     {
536       if (custom_context)
537         vlib_get_next_frame_with_aux_safe (vm, node, next_index, to_next,
538                                            to_next_aux, n_left_to_next);
539       else
540         vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
541
542       while (n_left_from > 0 && n_left_to_next > 0)
543         {
544           u32 bi0;
545           vlib_buffer_t *b0;
546           u32 next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
547           u32 error0 = IP6_ERROR_NONE;
548           u8 forward_context = 0;
549           bi0 = from[0];
550           b0 = vlib_get_buffer (vm, bi0);
551
552           ip6_header_t *ip0 = vlib_buffer_get_current (b0);
553           ip6_frag_hdr_t *frag_hdr;
554           ip6_ext_hdr_chain_t hdr_chain;
555           bool is_atomic_fragment = false;
556
557           int res = ip6_ext_header_walk (
558             b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
559           if (res >= 0 &&
560               hdr_chain.eh[res].protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
561             {
562               frag_hdr =
563                 ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
564               is_atomic_fragment = (0 == ip6_frag_hdr_offset (frag_hdr) &&
565                                     !ip6_frag_hdr_more (frag_hdr));
566             }
567
568           if (res < 0 ||
569               hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION ||
570               is_atomic_fragment)
571             {
572               // this is a regular unfragmented packet or an atomic fragment
573               if (!ip6_get_port
574                   (vm, b0, ip0, b0->current_length,
575                    &(vnet_buffer (b0)->ip.reass.ip_proto),
576                    &(vnet_buffer (b0)->ip.reass.l4_src_port),
577                    &(vnet_buffer (b0)->ip.reass.l4_dst_port),
578                    &(vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags),
579                    &(vnet_buffer (b0)->ip.reass.tcp_ack_number),
580                    &(vnet_buffer (b0)->ip.reass.tcp_seq_number)))
581                 {
582                   error0 = IP6_ERROR_REASS_UNSUPP_IP_PROTO;
583                   b0->error = node->errors[error0];
584                   next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
585                   goto packet_enqueue;
586                 }
587               vnet_buffer (b0)->ip.reass.is_non_first_fragment = 0;
588               next0 = custom_next ? vnet_buffer (b0)->ip.reass.next_index :
589                                           IP6_SV_REASSEMBLY_NEXT_INPUT;
590               if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
591                 {
592                   ip6_sv_reass_add_trace (
593                     vm, node, NULL, bi0, REASS_PASSTHROUGH,
594                     vnet_buffer (b0)->ip.reass.ip_proto,
595                     vnet_buffer (b0)->ip.reass.l4_src_port,
596                     vnet_buffer (b0)->ip.reass.l4_dst_port);
597                 }
598               goto packet_enqueue;
599             }
600
601           vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
602             hdr_chain.eh[res].offset;
603
604           if (0 == ip6_frag_hdr_offset (frag_hdr))
605             {
606               // first fragment - verify upper-layer is present
607               if (!ip6_sv_reass_verify_upper_layer_present (node, b0,
608                                                             &hdr_chain))
609                 {
610                   next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
611                   goto packet_enqueue;
612                 }
613             }
614           if (!ip6_sv_reass_verify_fragment_multiple_8 (vm, b0, frag_hdr) ||
615               !ip6_sv_reass_verify_packet_size_lt_64k (vm, b0, frag_hdr))
616             {
617               next0 = IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR;
618               goto packet_enqueue;
619             }
620
621           ip6_sv_reass_kv_t kv;
622           u8 do_handoff = 0;
623
624           kv.k.as_u64[0] = ip0->src_address.as_u64[0];
625           kv.k.as_u64[1] = ip0->src_address.as_u64[1];
626           kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
627           kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
628           if (custom_context)
629             kv.k.as_u64[4] =
630               (u64) *context << 32 | (u64) frag_hdr->identification;
631           else
632             kv.k.as_u64[4] =
633               ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
634                               vnet_buffer (b0)->sw_if_index[VLIB_RX]))
635                 << 32 |
636               (u64) frag_hdr->identification;
637           kv.k.as_u64[5] = ip0->protocol;
638
639           ip6_sv_reass_t *reass =
640             ip6_sv_reass_find_or_create (vm, rm, rt, &kv, &do_handoff);
641
642           if (PREDICT_FALSE (do_handoff))
643             {
644               next0 = IP6_SV_REASSEMBLY_NEXT_HANDOFF;
645               vnet_buffer (b0)->ip.reass.owner_thread_index =
646                 kv.v.thread_index;
647               if (custom_context)
648                 forward_context = 1;
649               goto packet_enqueue;
650             }
651
652           if (!reass)
653             {
654               next0 = IP6_SV_REASSEMBLY_NEXT_DROP;
655               error0 = IP6_ERROR_REASS_LIMIT_REACHED;
656               b0->error = node->errors[error0];
657               goto packet_enqueue;
658             }
659
660           if (reass->is_complete)
661             {
662               vnet_buffer (b0)->ip.reass.is_non_first_fragment =
663                 ! !ip6_frag_hdr_offset (frag_hdr);
664               vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
665               vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
666                 reass->icmp_type_or_tcp_flags;
667               vnet_buffer (b0)->ip.reass.tcp_ack_number =
668                 reass->tcp_ack_number;
669               vnet_buffer (b0)->ip.reass.tcp_seq_number =
670                 reass->tcp_seq_number;
671               vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
672               vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
673               next0 = custom_next ? vnet_buffer (b0)->ip.reass.next_index :
674                                           IP6_SV_REASSEMBLY_NEXT_INPUT;
675               if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
676                 {
677                   ip6_sv_reass_add_trace (
678                     vm, node, reass, bi0, REASS_FRAGMENT_FORWARD,
679                     reass->ip_proto, reass->l4_src_port, reass->l4_dst_port);
680                 }
681               goto packet_enqueue;
682             }
683
684           u32 counter = ~0;
685           switch (ip6_sv_reass_update (vm, node, rm, reass, bi0, frag_hdr))
686             {
687             case IP6_SV_REASS_RC_OK:
688               /* nothing to do here */
689               break;
690             case IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS:
691               counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
692               break;
693             case IP6_SV_REASS_RC_UNSUPP_IP_PROTO:
694               counter = IP6_ERROR_REASS_UNSUPP_IP_PROTO;
695               break;
696             case IP6_SV_REASS_RC_INTERNAL_ERROR:
697               counter = IP6_ERROR_REASS_INTERNAL_ERROR;
698               break;
699             case IP6_SV_REASS_RC_INVALID_FRAG_LEN:
700               counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
701               break;
702             }
703           if (~0 != counter)
704             {
705               vlib_node_increment_counter (vm, node->node_index, counter, 1);
706               ip6_sv_reass_free (vm, rm, rt, reass);
707               goto next_packet;
708             }
709
710           if (reass->is_complete)
711             {
712               u32 idx;
713               vec_foreach_index (idx, reass->cached_buffers)
714               {
715                 u32 bi0 = vec_elt (reass->cached_buffers, idx);
716                 if (0 == n_left_to_next)
717                   {
718                     vlib_put_next_frame (vm, node, next_index,
719                                          n_left_to_next);
720                     vlib_get_next_frame (vm, node, next_index, to_next,
721                                          n_left_to_next);
722                   }
723                 to_next[0] = bi0;
724                 to_next += 1;
725                 n_left_to_next -= 1;
726                 b0 = vlib_get_buffer (vm, bi0);
727                 if (is_feature)
728                   {
729                     vnet_feature_next (&next0, b0);
730                   }
731                 frag_hdr =
732                   vlib_buffer_get_current (b0) +
733                   vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset;
734                 vnet_buffer (b0)->ip.reass.is_non_first_fragment =
735                   ! !ip6_frag_hdr_offset (frag_hdr);
736                 vnet_buffer (b0)->ip.reass.ip_proto = reass->ip_proto;
737                 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags =
738                   reass->icmp_type_or_tcp_flags;
739                 vnet_buffer (b0)->ip.reass.tcp_ack_number =
740                   reass->tcp_ack_number;
741                 vnet_buffer (b0)->ip.reass.tcp_seq_number =
742                   reass->tcp_seq_number;
743                 vnet_buffer (b0)->ip.reass.l4_src_port = reass->l4_src_port;
744                 vnet_buffer (b0)->ip.reass.l4_dst_port = reass->l4_dst_port;
745                 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
746                   {
747                     ip6_sv_reass_add_trace (
748                       vm, node, reass, bi0, REASS_FRAGMENT_FORWARD,
749                       reass->ip_proto, reass->l4_src_port, reass->l4_dst_port);
750                   }
751                 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
752                                                  to_next, n_left_to_next, bi0,
753                                                  next0);
754               }
755               vec_set_len (reass->cached_buffers,
756                            0); // buffers are owned by frame now
757             }
758           goto next_packet;
759
760         packet_enqueue:
761           to_next[0] = bi0;
762           to_next += 1;
763           n_left_to_next -= 1;
764           if (is_feature && IP6_ERROR_NONE == error0)
765             {
766               b0 = vlib_get_buffer (vm, bi0);
767               vnet_feature_next (&next0, b0);
768             }
769           if (custom_context && forward_context)
770             {
771               if (to_next_aux)
772                 {
773                   to_next_aux[0] = *context;
774                   to_next_aux += 1;
775                 }
776               vlib_validate_buffer_enqueue_with_aux_x1 (
777                 vm, node, next_index, to_next, to_next_aux, n_left_to_next,
778                 bi0, *context, next0);
779             }
780           else
781             vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
782                                              n_left_to_next, bi0, next0);
783
784         next_packet:
785           from += 1;
786           if (custom_context)
787             context += 1;
788           n_left_from -= 1;
789         }
790
791       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
792     }
793
794   clib_spinlock_unlock (&rt->lock);
795   return frame->n_vectors;
796 }
797
798 VLIB_NODE_FN (ip6_sv_reass_node) (vlib_main_t * vm,
799                                   vlib_node_runtime_t * node,
800                                   vlib_frame_t * frame)
801 {
802   return ip6_sv_reassembly_inline (vm, node, frame, false /* is_feature */,
803                                    false /* custom next */,
804                                    false /* custom context */);
805 }
806
807 VLIB_REGISTER_NODE (ip6_sv_reass_node) = {
808     .name = "ip6-sv-reassembly",
809     .vector_size = sizeof (u32),
810     .format_trace = format_ip6_sv_reass_trace,
811     .n_errors = IP6_N_ERROR,
812     .error_counters = ip6_error_counters,
813     .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
814     .next_nodes =
815         {
816                 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
817                 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
818                 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
819                 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reassembly-handoff",
820         },
821 };
822
823 VLIB_NODE_FN (ip6_sv_reass_node_feature) (vlib_main_t * vm,
824                                           vlib_node_runtime_t * node,
825                                           vlib_frame_t * frame)
826 {
827   return ip6_sv_reassembly_inline (vm, node, frame, true /* is_feature */,
828                                    false /* custom next */,
829                                    false /* custom context */);
830 }
831
832 VLIB_REGISTER_NODE (ip6_sv_reass_node_feature) = {
833     .name = "ip6-sv-reassembly-feature",
834     .vector_size = sizeof (u32),
835     .format_trace = format_ip6_sv_reass_trace,
836     .n_errors = IP6_N_ERROR,
837     .error_counters = ip6_error_counters,
838     .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
839     .next_nodes =
840         {
841                 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
842                 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
843                 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
844                 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reass-feature-hoff",
845         },
846 };
847
848 VNET_FEATURE_INIT (ip6_sv_reassembly_feature) = {
849     .arc_name = "ip6-unicast",
850     .node_name = "ip6-sv-reassembly-feature",
851     .runs_before = VNET_FEATURES ("ip6-lookup"),
852     .runs_after = 0,
853 };
854
855 VLIB_NODE_FN (ip6_sv_reass_custom_context_node)
856 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
857 {
858   return ip6_sv_reassembly_inline (vm, node, frame, false /* is_feature */,
859                                    true /* custom next */,
860                                    true /* custom context */);
861 }
862
863 VLIB_REGISTER_NODE (ip6_sv_reass_custom_context_node) = {
864     .name = "ip6-sv-reassembly-custom-context",
865     .vector_size = sizeof (u32),
866     .aux_size = sizeof (u32),
867     .format_trace = format_ip6_sv_reass_trace,
868     .n_errors = IP6_N_ERROR,
869     .error_counters = ip6_error_counters,
870     .n_next_nodes = IP6_SV_REASSEMBLY_N_NEXT,
871     .next_nodes =
872         {
873                 [IP6_SV_REASSEMBLY_NEXT_INPUT] = "ip6-input",
874                 [IP6_SV_REASSEMBLY_NEXT_DROP] = "ip6-drop",
875                 [IP6_SV_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
876                 [IP6_SV_REASSEMBLY_NEXT_HANDOFF] = "ip6-sv-reassembly-custom-context-handoff",
877         },
878 };
879
880 #ifndef CLIB_MARCH_VARIANT
881 static u32
882 ip6_sv_reass_get_nbuckets ()
883 {
884   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
885   u32 nbuckets;
886   u8 i;
887
888   nbuckets = (u32) (rm->max_reass_n / IP6_SV_REASS_HT_LOAD_FACTOR);
889
890   for (i = 0; i < 31; i++)
891     if ((1 << i) >= nbuckets)
892       break;
893   nbuckets = 1 << i;
894
895   return nbuckets;
896 }
897 #endif /* CLIB_MARCH_VARIANT */
898
899 typedef enum
900 {
901   IP6_EVENT_CONFIG_CHANGED = 1,
902 } ip6_sv_reass_event_t;
903
904 #ifndef CLIB_MARCH_VARIANT
905 typedef struct
906 {
907   int failure;
908   clib_bihash_48_8_t *new_hash;
909 } ip6_rehash_cb_ctx;
910
911 static int
912 ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
913 {
914   ip6_rehash_cb_ctx *ctx = _ctx;
915   if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
916     {
917       ctx->failure = 1;
918     }
919   return (BIHASH_WALK_CONTINUE);
920 }
921
922 static void
923 ip6_sv_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
924                          u32 max_reassembly_length,
925                          u32 expire_walk_interval_ms)
926 {
927   ip6_sv_reass_main.timeout_ms = timeout_ms;
928   ip6_sv_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
929   ip6_sv_reass_main.max_reass_n = max_reassemblies;
930   ip6_sv_reass_main.max_reass_len = max_reassembly_length;
931   ip6_sv_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
932 }
933
934 vnet_api_error_t
935 ip6_sv_reass_set (u32 timeout_ms, u32 max_reassemblies,
936                   u32 max_reassembly_length, u32 expire_walk_interval_ms)
937 {
938   u32 old_nbuckets = ip6_sv_reass_get_nbuckets ();
939   ip6_sv_reass_set_params (timeout_ms, max_reassemblies,
940                            max_reassembly_length, expire_walk_interval_ms);
941   vlib_process_signal_event (ip6_sv_reass_main.vlib_main,
942                              ip6_sv_reass_main.ip6_sv_reass_expire_node_idx,
943                              IP6_EVENT_CONFIG_CHANGED, 0);
944   u32 new_nbuckets = ip6_sv_reass_get_nbuckets ();
945   if (ip6_sv_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
946     {
947       clib_bihash_48_8_t new_hash;
948       clib_memset (&new_hash, 0, sizeof (new_hash));
949       ip6_rehash_cb_ctx ctx;
950       ctx.failure = 0;
951       ctx.new_hash = &new_hash;
952       clib_bihash_init_48_8 (&new_hash, "ip6-sv-reass", new_nbuckets,
953                              new_nbuckets * 1024);
954       clib_bihash_foreach_key_value_pair_48_8 (&ip6_sv_reass_main.hash,
955                                                ip6_rehash_cb, &ctx);
956       if (ctx.failure)
957         {
958           clib_bihash_free_48_8 (&new_hash);
959           return -1;
960         }
961       else
962         {
963           clib_bihash_free_48_8 (&ip6_sv_reass_main.hash);
964           clib_memcpy_fast (&ip6_sv_reass_main.hash, &new_hash,
965                             sizeof (ip6_sv_reass_main.hash));
966           clib_bihash_copied (&ip6_sv_reass_main.hash, &new_hash);
967         }
968     }
969   return 0;
970 }
971
972 vnet_api_error_t
973 ip6_sv_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
974                   u32 * max_reassembly_length, u32 * expire_walk_interval_ms)
975 {
976   *timeout_ms = ip6_sv_reass_main.timeout_ms;
977   *max_reassemblies = ip6_sv_reass_main.max_reass_n;
978   *max_reassembly_length = ip6_sv_reass_main.max_reass_len;
979   *expire_walk_interval_ms = ip6_sv_reass_main.expire_walk_interval_ms;
980   return 0;
981 }
982
983 static clib_error_t *
984 ip6_sv_reass_init_function (vlib_main_t * vm)
985 {
986   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
987   clib_error_t *error = 0;
988   u32 nbuckets;
989   vlib_node_t *node;
990
991   rm->vlib_main = vm;
992   rm->vnet_main = vnet_get_main ();
993
994   vec_validate (rm->per_thread_data, vlib_num_workers ());
995   ip6_sv_reass_per_thread_t *rt;
996   vec_foreach (rt, rm->per_thread_data)
997   {
998     clib_spinlock_init (&rt->lock);
999     pool_alloc (rt->pool, rm->max_reass_n);
1000     rt->lru_first = rt->lru_last = ~0;
1001   }
1002
1003   node = vlib_get_node_by_name (vm, (u8 *) "ip6-sv-reassembly-expire-walk");
1004   ASSERT (node);
1005   rm->ip6_sv_reass_expire_node_idx = node->index;
1006
1007   ip6_sv_reass_set_params (IP6_SV_REASS_TIMEOUT_DEFAULT_MS,
1008                            IP6_SV_REASS_MAX_REASSEMBLIES_DEFAULT,
1009                            IP6_SV_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1010                            IP6_SV_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1011
1012   nbuckets = ip6_sv_reass_get_nbuckets ();
1013   clib_bihash_init_48_8 (&rm->hash, "ip6-sv-reass", nbuckets,
1014                          nbuckets * 1024);
1015
1016   node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
1017   ASSERT (node);
1018   rm->ip6_drop_idx = node->index;
1019   node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1020   ASSERT (node);
1021   rm->ip6_icmp_error_idx = node->index;
1022
1023   if ((error = vlib_call_init_function (vm, ip_main_init)))
1024     return error;
1025
1026   rm->fq_index = vlib_frame_queue_main_init (ip6_sv_reass_node.index, 0);
1027   rm->fq_feature_index =
1028     vlib_frame_queue_main_init (ip6_sv_reass_node_feature.index, 0);
1029   rm->fq_custom_context_index =
1030     vlib_frame_queue_main_init (ip6_sv_reass_custom_context_node.index, 0);
1031
1032   rm->feature_use_refcount_per_intf = NULL;
1033
1034   return error;
1035 }
1036
1037 VLIB_INIT_FUNCTION (ip6_sv_reass_init_function);
1038 #endif /* CLIB_MARCH_VARIANT */
1039
1040 static uword
1041 ip6_sv_reass_walk_expired (vlib_main_t *vm,
1042                            CLIB_UNUSED (vlib_node_runtime_t *node),
1043                            CLIB_UNUSED (vlib_frame_t *f))
1044 {
1045   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1046   uword event_type, *event_data = 0;
1047
1048   while (true)
1049     {
1050       vlib_process_wait_for_event_or_clock (vm,
1051                                             (f64) rm->expire_walk_interval_ms
1052                                             / (f64) MSEC_PER_SEC);
1053       event_type = vlib_process_get_events (vm, &event_data);
1054
1055       switch (event_type)
1056         {
1057         case ~0:
1058           /* no events => timeout */
1059           /* fallthrough */
1060         case IP6_EVENT_CONFIG_CHANGED:
1061           /* nothing to do here */
1062           break;
1063         default:
1064           clib_warning ("BUG: event type 0x%wx", event_type);
1065           break;
1066         }
1067       f64 now = vlib_time_now (vm);
1068
1069       ip6_sv_reass_t *reass;
1070       int *pool_indexes_to_free = NULL;
1071
1072       uword thread_index = 0;
1073       int index;
1074       const uword nthreads = vlib_num_workers () + 1;
1075       for (thread_index = 0; thread_index < nthreads; ++thread_index)
1076         {
1077           ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1078           clib_spinlock_lock (&rt->lock);
1079
1080           vec_reset_length (pool_indexes_to_free);
1081           pool_foreach_index (index, rt->pool)  {
1082                                 reass = pool_elt_at_index (rt->pool, index);
1083                                 if (now > reass->last_heard + rm->timeout)
1084                                   {
1085                                     vec_add1 (pool_indexes_to_free, index);
1086                                   }
1087                               }
1088           int *i;
1089           vec_foreach (i, pool_indexes_to_free)
1090           {
1091             ip6_sv_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1092             ip6_sv_reass_free (vm, rm, rt, reass);
1093           }
1094
1095           clib_spinlock_unlock (&rt->lock);
1096         }
1097
1098       vec_free (pool_indexes_to_free);
1099       if (event_data)
1100         {
1101           vec_set_len (event_data, 0);
1102         }
1103     }
1104
1105   return 0;
1106 }
1107
1108 VLIB_REGISTER_NODE (ip6_sv_reass_expire_node) = {
1109   .function = ip6_sv_reass_walk_expired,
1110   .format_trace = format_ip6_sv_reass_trace,
1111   .type = VLIB_NODE_TYPE_PROCESS,
1112   .name = "ip6-sv-reassembly-expire-walk",
1113
1114   .n_errors = IP6_N_ERROR,
1115   .error_counters = ip6_error_counters,
1116 };
1117
1118 static u8 *
1119 format_ip6_sv_reass_key (u8 * s, va_list * args)
1120 {
1121   ip6_sv_reass_key_t *key = va_arg (*args, ip6_sv_reass_key_t *);
1122   s =
1123     format (s, "fib_index: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1124             key->fib_index, format_ip6_address, &key->src, format_ip6_address,
1125             &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1126   return s;
1127 }
1128
1129 static u8 *
1130 format_ip6_sv_reass (u8 * s, va_list * args)
1131 {
1132   vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1133   ip6_sv_reass_t *reass = va_arg (*args, ip6_sv_reass_t *);
1134
1135   s = format (s, "ID: %lu, key: %U, trace_op_counter: %u\n",
1136               reass->id, format_ip6_sv_reass_key, &reass->key,
1137               reass->trace_op_counter);
1138   vlib_buffer_t *b;
1139   u32 *bip;
1140   u32 counter = 0;
1141   vec_foreach (bip, reass->cached_buffers)
1142   {
1143     u32 bi = *bip;
1144     do
1145       {
1146         b = vlib_get_buffer (vm, bi);
1147         s = format (s, "  #%03u: bi: %u\n", counter, bi);
1148         ++counter;
1149         bi = b->next_buffer;
1150       }
1151     while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
1152   }
1153   return s;
1154 }
1155
1156 static clib_error_t *
1157 show_ip6_sv_reass (vlib_main_t * vm, unformat_input_t * input,
1158                    CLIB_UNUSED (vlib_cli_command_t * lmd))
1159 {
1160   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1161
1162   vlib_cli_output (vm, "---------------------");
1163   vlib_cli_output (vm, "IP6 reassembly status");
1164   vlib_cli_output (vm, "---------------------");
1165   bool details = false;
1166   if (unformat (input, "details"))
1167     {
1168       details = true;
1169     }
1170
1171   u32 sum_reass_n = 0;
1172   u64 sum_buffers_n = 0;
1173   ip6_sv_reass_t *reass;
1174   uword thread_index;
1175   const uword nthreads = vlib_num_workers () + 1;
1176   for (thread_index = 0; thread_index < nthreads; ++thread_index)
1177     {
1178       ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1179       clib_spinlock_lock (&rt->lock);
1180       if (details)
1181         {
1182           pool_foreach (reass, rt->pool) {
1183             vlib_cli_output (vm, "%U", format_ip6_sv_reass, vm, reass);
1184           }
1185         }
1186       sum_reass_n += rt->reass_n;
1187       clib_spinlock_unlock (&rt->lock);
1188     }
1189   vlib_cli_output (vm, "---------------------");
1190   vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1191                    (long unsigned) sum_reass_n);
1192   vlib_cli_output (vm,
1193                    "Maximum configured concurrent shallow virtual IP6 reassemblies per worker-thread: %lu\n",
1194                    (long unsigned) rm->max_reass_n);
1195   vlib_cli_output (vm,
1196                    "Maximum configured amount of fragments per shallow "
1197                    "virtual IP6 reassembly: %lu\n",
1198                    (long unsigned) rm->max_reass_len);
1199   vlib_cli_output (vm,
1200                    "Maximum configured shallow virtual IP6 reassembly timeout: %lums\n",
1201                    (long unsigned) rm->timeout_ms);
1202   vlib_cli_output (vm,
1203                    "Maximum configured shallow virtual IP6 reassembly expire walk interval: %lums\n",
1204                    (long unsigned) rm->expire_walk_interval_ms);
1205   vlib_cli_output (vm, "Buffers in use: %lu\n",
1206                    (long unsigned) sum_buffers_n);
1207   return 0;
1208 }
1209
1210 VLIB_CLI_COMMAND (show_ip6_sv_reassembly_cmd, static) = {
1211     .path = "show ip6-sv-reassembly",
1212     .short_help = "show ip6-sv-reassembly [details]",
1213     .function = show_ip6_sv_reass,
1214 };
1215
1216 #ifndef CLIB_MARCH_VARIANT
1217 vnet_api_error_t
1218 ip6_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1219 {
1220   return ip6_sv_reass_enable_disable_with_refcnt (sw_if_index,
1221                                                   enable_disable);
1222 }
1223 #endif /* CLIB_MARCH_VARIANT */
1224
1225 #define foreach_ip6_sv_reassembly_handoff_error                       \
1226 _(CONGESTION_DROP, "congestion drop")
1227
1228
1229 typedef enum
1230 {
1231 #define _(sym,str) IP6_SV_REASSEMBLY_HANDOFF_ERROR_##sym,
1232   foreach_ip6_sv_reassembly_handoff_error
1233 #undef _
1234     IP6_SV_REASSEMBLY_HANDOFF_N_ERROR,
1235 } ip6_sv_reassembly_handoff_error_t;
1236
1237 static char *ip6_sv_reassembly_handoff_error_strings[] = {
1238 #define _(sym,string) string,
1239   foreach_ip6_sv_reassembly_handoff_error
1240 #undef _
1241 };
1242
1243 typedef struct
1244 {
1245   u32 next_worker_index;
1246 } ip6_sv_reassembly_handoff_trace_t;
1247
1248 static u8 *
1249 format_ip6_sv_reassembly_handoff_trace (u8 * s, va_list * args)
1250 {
1251   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1252   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1253   ip6_sv_reassembly_handoff_trace_t *t =
1254     va_arg (*args, ip6_sv_reassembly_handoff_trace_t *);
1255
1256   s =
1257     format (s, "ip6-sv-reassembly-handoff: next-worker %d",
1258             t->next_worker_index);
1259
1260   return s;
1261 }
1262
1263 always_inline uword
1264 ip6_sv_reassembly_handoff_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1265                                   vlib_frame_t *frame, bool is_feature,
1266                                   bool custom_context)
1267 {
1268   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1269
1270   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1271   u32 n_enq, n_left_from, *from, *context;
1272   u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1273   u32 fq_index;
1274
1275   from = vlib_frame_vector_args (frame);
1276   if (custom_context)
1277     context = vlib_frame_aux_args (frame);
1278   n_left_from = frame->n_vectors;
1279   vlib_get_buffers (vm, from, bufs, n_left_from);
1280
1281   b = bufs;
1282   ti = thread_indices;
1283
1284   fq_index = (is_feature) ?
1285                      rm->fq_feature_index :
1286                      (custom_context ? rm->fq_custom_context_index : rm->fq_index);
1287
1288   while (n_left_from > 0)
1289     {
1290       ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1291
1292       if (PREDICT_FALSE
1293           ((node->flags & VLIB_NODE_FLAG_TRACE)
1294            && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1295         {
1296           ip6_sv_reassembly_handoff_trace_t *t =
1297             vlib_add_trace (vm, node, b[0], sizeof (*t));
1298           t->next_worker_index = ti[0];
1299         }
1300
1301       n_left_from -= 1;
1302       ti += 1;
1303       b += 1;
1304     }
1305   if (custom_context)
1306     n_enq = vlib_buffer_enqueue_to_thread_with_aux (
1307       vm, node, fq_index, from, context, thread_indices, frame->n_vectors, 1);
1308   else
1309     n_enq = vlib_buffer_enqueue_to_thread (
1310       vm, node, fq_index, from, thread_indices, frame->n_vectors, 1);
1311
1312   if (n_enq < frame->n_vectors)
1313     vlib_node_increment_counter (vm, node->node_index,
1314                                  IP6_SV_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1315                                  frame->n_vectors - n_enq);
1316   return frame->n_vectors;
1317 }
1318
1319 VLIB_NODE_FN (ip6_sv_reassembly_handoff_node) (vlib_main_t * vm,
1320                                                vlib_node_runtime_t * node,
1321                                                vlib_frame_t * frame)
1322 {
1323   return ip6_sv_reassembly_handoff_inline (
1324     vm, node, frame, false /* is_feature */, false /* custom_context */);
1325 }
1326
1327 VLIB_REGISTER_NODE (ip6_sv_reassembly_handoff_node) = {
1328   .name = "ip6-sv-reassembly-handoff",
1329   .vector_size = sizeof (u32),
1330   .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1331   .error_strings = ip6_sv_reassembly_handoff_error_strings,
1332   .format_trace = format_ip6_sv_reassembly_handoff_trace,
1333
1334   .n_next_nodes = 1,
1335
1336   .next_nodes = {
1337     [0] = "error-drop",
1338   },
1339 };
1340
1341
1342 VLIB_NODE_FN (ip6_sv_reassembly_feature_handoff_node) (vlib_main_t * vm,
1343                                vlib_node_runtime_t * node, vlib_frame_t * frame)
1344 {
1345   return ip6_sv_reassembly_handoff_inline (
1346     vm, node, frame, true /* is_feature */, false /* custom_context */);
1347 }
1348
1349
1350 VLIB_REGISTER_NODE (ip6_sv_reassembly_feature_handoff_node) = {
1351   .name = "ip6-sv-reass-feature-hoff",
1352   .vector_size = sizeof (u32),
1353   .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1354   .error_strings = ip6_sv_reassembly_handoff_error_strings,
1355   .format_trace = format_ip6_sv_reassembly_handoff_trace,
1356
1357   .n_next_nodes = 1,
1358
1359   .next_nodes = {
1360     [0] = "error-drop",
1361   },
1362 };
1363
1364 VLIB_NODE_FN (ip6_sv_reassembly_custom_context_handoff_node)
1365 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1366 {
1367   return ip6_sv_reassembly_handoff_inline (
1368     vm, node, frame, false /* is_feature */, true /* custom_context */);
1369 }
1370
1371 VLIB_REGISTER_NODE (ip6_sv_reassembly_custom_context_handoff_node) = {
1372   .name = "ip6-sv-reassembly-custom-context-handoff",
1373   .vector_size = sizeof (u32),
1374   .aux_size = sizeof (u32),
1375   .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1376   .error_strings = ip6_sv_reassembly_handoff_error_strings,
1377   .format_trace = format_ip6_sv_reassembly_handoff_trace,
1378
1379   .n_next_nodes = 1,
1380
1381   .next_nodes = {
1382     [0] = "error-drop",
1383   },
1384 };
1385
1386 #ifndef CLIB_MARCH_VARIANT
1387 int
1388 ip6_sv_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1389 {
1390   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1391   vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1392   if (is_enable)
1393     {
1394       if (!rm->feature_use_refcount_per_intf[sw_if_index])
1395         {
1396           ++rm->feature_use_refcount_per_intf[sw_if_index];
1397           return vnet_feature_enable_disable ("ip6-unicast",
1398                                               "ip6-sv-reassembly-feature",
1399                                               sw_if_index, 1, 0, 0);
1400         }
1401       ++rm->feature_use_refcount_per_intf[sw_if_index];
1402     }
1403   else
1404     {
1405       --rm->feature_use_refcount_per_intf[sw_if_index];
1406       if (!rm->feature_use_refcount_per_intf[sw_if_index])
1407         return vnet_feature_enable_disable ("ip6-unicast",
1408                                             "ip6-sv-reassembly-feature",
1409                                             sw_if_index, 0, 0, 0);
1410     }
1411   return 0;
1412 }
1413
1414 uword
1415 ip6_sv_reass_custom_context_register_next_node (uword node_index)
1416 {
1417   return vlib_node_add_next (
1418     vlib_get_main (), ip6_sv_reassembly_custom_context_handoff_node.index,
1419     node_index);
1420 }
1421 #endif
1422
1423 /*
1424  * fd.io coding-style-patch-verification: ON
1425  *
1426  * Local Variables:
1427  * eval: (c-set-style "gnu")
1428  * End:
1429  */