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