ip: svr: fix feature refcounts
[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 - per interface
150   u32 *feature_use_refcount_per_intf;
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   rm->feature_use_refcount_per_intf = NULL;
949
950   return error;
951 }
952
953 VLIB_INIT_FUNCTION (ip6_sv_reass_init_function);
954 #endif /* CLIB_MARCH_VARIANT */
955
956 static uword
957 ip6_sv_reass_walk_expired (vlib_main_t * vm,
958                            vlib_node_runtime_t * node, vlib_frame_t * f)
959 {
960   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
961   uword event_type, *event_data = 0;
962
963   while (true)
964     {
965       vlib_process_wait_for_event_or_clock (vm,
966                                             (f64) rm->expire_walk_interval_ms
967                                             / (f64) MSEC_PER_SEC);
968       event_type = vlib_process_get_events (vm, &event_data);
969
970       switch (event_type)
971         {
972         case ~0:                /* no events => timeout */
973           /* nothing to do here */
974           break;
975         case IP6_EVENT_CONFIG_CHANGED:
976           break;
977         default:
978           clib_warning ("BUG: event type 0x%wx", event_type);
979           break;
980         }
981       f64 now = vlib_time_now (vm);
982
983       ip6_sv_reass_t *reass;
984       int *pool_indexes_to_free = NULL;
985
986       uword thread_index = 0;
987       int index;
988       const uword nthreads = vlib_num_workers () + 1;
989       u32 *vec_icmp_bi = NULL;
990       for (thread_index = 0; thread_index < nthreads; ++thread_index)
991         {
992           ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
993           clib_spinlock_lock (&rt->lock);
994
995           vec_reset_length (pool_indexes_to_free);
996           /* *INDENT-OFF* */
997           pool_foreach_index (index, rt->pool, ({
998                                 reass = pool_elt_at_index (rt->pool, index);
999                                 if (now > reass->last_heard + rm->timeout)
1000                                   {
1001                                     vec_add1 (pool_indexes_to_free, index);
1002                                   }
1003                               }));
1004           /* *INDENT-ON* */
1005           int *i;
1006           /* *INDENT-OFF* */
1007           vec_foreach (i, pool_indexes_to_free)
1008           {
1009             ip6_sv_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1010             ip6_sv_reass_free (vm, rm, rt, reass);
1011           }
1012           /* *INDENT-ON* */
1013
1014           clib_spinlock_unlock (&rt->lock);
1015         }
1016
1017       while (vec_len (vec_icmp_bi) > 0)
1018         {
1019           vlib_frame_t *f =
1020             vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1021           u32 *to_next = vlib_frame_vector_args (f);
1022           u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
1023           int trace_frame = 0;
1024           while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1025             {
1026               u32 bi = vec_pop (vec_icmp_bi);
1027               vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1028               if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1029                 {
1030                   trace_frame = 1;
1031                 }
1032               b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1033               to_next[0] = bi;
1034               ++f->n_vectors;
1035               to_next += 1;
1036               n_left_to_next -= 1;
1037             }
1038           f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
1039           vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1040         }
1041
1042       vec_free (pool_indexes_to_free);
1043       vec_free (vec_icmp_bi);
1044       if (event_data)
1045         {
1046           _vec_len (event_data) = 0;
1047         }
1048     }
1049
1050   return 0;
1051 }
1052
1053 /* *INDENT-OFF* */
1054 VLIB_REGISTER_NODE (ip6_sv_reass_expire_node) = {
1055     .function = ip6_sv_reass_walk_expired,
1056     .format_trace = format_ip6_sv_reass_trace,
1057     .type = VLIB_NODE_TYPE_PROCESS,
1058     .name = "ip6-sv-reassembly-expire-walk",
1059
1060     .n_errors = ARRAY_LEN (ip6_sv_reassembly_error_strings),
1061     .error_strings = ip6_sv_reassembly_error_strings,
1062
1063 };
1064 /* *INDENT-ON* */
1065
1066 static u8 *
1067 format_ip6_sv_reass_key (u8 * s, va_list * args)
1068 {
1069   ip6_sv_reass_key_t *key = va_arg (*args, ip6_sv_reass_key_t *);
1070   s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1071               key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1072               &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1073   return s;
1074 }
1075
1076 static u8 *
1077 format_ip6_sv_reass (u8 * s, va_list * args)
1078 {
1079   vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1080   ip6_sv_reass_t *reass = va_arg (*args, ip6_sv_reass_t *);
1081
1082   s = format (s, "ID: %lu, key: %U, trace_op_counter: %u\n",
1083               reass->id, format_ip6_sv_reass_key, &reass->key,
1084               reass->trace_op_counter);
1085   vlib_buffer_t *b;
1086   u32 *bip;
1087   u32 counter = 0;
1088   vec_foreach (bip, reass->cached_buffers)
1089   {
1090     u32 bi = *bip;
1091     do
1092       {
1093         b = vlib_get_buffer (vm, bi);
1094         s = format (s, "  #%03u: bi: %u\n", counter, bi);
1095         ++counter;
1096         bi = b->next_buffer;
1097       }
1098     while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
1099   }
1100   return s;
1101 }
1102
1103 static clib_error_t *
1104 show_ip6_sv_reass (vlib_main_t * vm, unformat_input_t * input,
1105                    CLIB_UNUSED (vlib_cli_command_t * lmd))
1106 {
1107   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1108
1109   vlib_cli_output (vm, "---------------------");
1110   vlib_cli_output (vm, "IP6 reassembly status");
1111   vlib_cli_output (vm, "---------------------");
1112   bool details = false;
1113   if (unformat (input, "details"))
1114     {
1115       details = true;
1116     }
1117
1118   u32 sum_reass_n = 0;
1119   u64 sum_buffers_n = 0;
1120   ip6_sv_reass_t *reass;
1121   uword thread_index;
1122   const uword nthreads = vlib_num_workers () + 1;
1123   for (thread_index = 0; thread_index < nthreads; ++thread_index)
1124     {
1125       ip6_sv_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1126       clib_spinlock_lock (&rt->lock);
1127       if (details)
1128         {
1129           /* *INDENT-OFF* */
1130           pool_foreach (reass, rt->pool, {
1131             vlib_cli_output (vm, "%U", format_ip6_sv_reass, vm, reass);
1132           });
1133           /* *INDENT-ON* */
1134         }
1135       sum_reass_n += rt->reass_n;
1136       clib_spinlock_unlock (&rt->lock);
1137     }
1138   vlib_cli_output (vm, "---------------------");
1139   vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1140                    (long unsigned) sum_reass_n);
1141   vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1142                    "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
1143   vlib_cli_output (vm, "Buffers in use: %lu\n",
1144                    (long unsigned) sum_buffers_n);
1145   return 0;
1146 }
1147
1148 /* *INDENT-OFF* */
1149 VLIB_CLI_COMMAND (show_ip6_sv_reassembly_cmd, static) = {
1150     .path = "show ip6-sv-reassembly",
1151     .short_help = "show ip6-sv-reassembly [details]",
1152     .function = show_ip6_sv_reass,
1153 };
1154 /* *INDENT-ON* */
1155
1156 #ifndef CLIB_MARCH_VARIANT
1157 vnet_api_error_t
1158 ip6_sv_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1159 {
1160   return vnet_feature_enable_disable ("ip6-unicast",
1161                                       "ip6-sv-reassembly-feature",
1162                                       sw_if_index, enable_disable, 0, 0);
1163 }
1164 #endif /* CLIB_MARCH_VARIANT */
1165
1166 #define foreach_ip6_sv_reassembly_handoff_error                       \
1167 _(CONGESTION_DROP, "congestion drop")
1168
1169
1170 typedef enum
1171 {
1172 #define _(sym,str) IP6_SV_REASSEMBLY_HANDOFF_ERROR_##sym,
1173   foreach_ip6_sv_reassembly_handoff_error
1174 #undef _
1175     IP6_SV_REASSEMBLY_HANDOFF_N_ERROR,
1176 } ip6_sv_reassembly_handoff_error_t;
1177
1178 static char *ip6_sv_reassembly_handoff_error_strings[] = {
1179 #define _(sym,string) string,
1180   foreach_ip6_sv_reassembly_handoff_error
1181 #undef _
1182 };
1183
1184 typedef struct
1185 {
1186   u32 next_worker_index;
1187 } ip6_sv_reassembly_handoff_trace_t;
1188
1189 static u8 *
1190 format_ip6_sv_reassembly_handoff_trace (u8 * s, va_list * args)
1191 {
1192   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1193   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1194   ip6_sv_reassembly_handoff_trace_t *t =
1195     va_arg (*args, ip6_sv_reassembly_handoff_trace_t *);
1196
1197   s =
1198     format (s, "ip6-sv-reassembly-handoff: next-worker %d",
1199             t->next_worker_index);
1200
1201   return s;
1202 }
1203
1204 always_inline uword
1205 ip6_sv_reassembly_handoff_inline (vlib_main_t * vm,
1206                                   vlib_node_runtime_t * node,
1207                                   vlib_frame_t * frame, bool is_feature)
1208 {
1209   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1210
1211   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1212   u32 n_enq, n_left_from, *from;
1213   u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1214   u32 fq_index;
1215
1216   from = vlib_frame_vector_args (frame);
1217   n_left_from = frame->n_vectors;
1218   vlib_get_buffers (vm, from, bufs, n_left_from);
1219
1220   b = bufs;
1221   ti = thread_indices;
1222
1223   fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1224
1225   while (n_left_from > 0)
1226     {
1227       ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
1228
1229       if (PREDICT_FALSE
1230           ((node->flags & VLIB_NODE_FLAG_TRACE)
1231            && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1232         {
1233           ip6_sv_reassembly_handoff_trace_t *t =
1234             vlib_add_trace (vm, node, b[0], sizeof (*t));
1235           t->next_worker_index = ti[0];
1236         }
1237
1238       n_left_from -= 1;
1239       ti += 1;
1240       b += 1;
1241     }
1242   n_enq =
1243     vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1244                                    frame->n_vectors, 1);
1245
1246   if (n_enq < frame->n_vectors)
1247     vlib_node_increment_counter (vm, node->node_index,
1248                                  IP6_SV_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1249                                  frame->n_vectors - n_enq);
1250   return frame->n_vectors;
1251 }
1252
1253 VLIB_NODE_FN (ip6_sv_reassembly_handoff_node) (vlib_main_t * vm,
1254                                                vlib_node_runtime_t * node,
1255                                                vlib_frame_t * frame)
1256 {
1257   return ip6_sv_reassembly_handoff_inline (vm, node, frame,
1258                                            false /* is_feature */ );
1259 }
1260
1261 /* *INDENT-OFF* */
1262 VLIB_REGISTER_NODE (ip6_sv_reassembly_handoff_node) = {
1263   .name = "ip6-sv-reassembly-handoff",
1264   .vector_size = sizeof (u32),
1265   .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1266   .error_strings = ip6_sv_reassembly_handoff_error_strings,
1267   .format_trace = format_ip6_sv_reassembly_handoff_trace,
1268
1269   .n_next_nodes = 1,
1270
1271   .next_nodes = {
1272     [0] = "error-drop",
1273   },
1274 };
1275
1276
1277 VLIB_NODE_FN (ip6_sv_reassembly_feature_handoff_node) (vlib_main_t * vm,
1278                                vlib_node_runtime_t * node, vlib_frame_t * frame)
1279 {
1280   return ip6_sv_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1281 }
1282
1283
1284 /* *INDENT-OFF* */
1285 VLIB_REGISTER_NODE (ip6_sv_reassembly_feature_handoff_node) = {
1286   .name = "ip6-sv-reass-feature-hoff",
1287   .vector_size = sizeof (u32),
1288   .n_errors = ARRAY_LEN(ip6_sv_reassembly_handoff_error_strings),
1289   .error_strings = ip6_sv_reassembly_handoff_error_strings,
1290   .format_trace = format_ip6_sv_reassembly_handoff_trace,
1291
1292   .n_next_nodes = 1,
1293
1294   .next_nodes = {
1295     [0] = "error-drop",
1296   },
1297 };
1298 /* *INDENT-ON* */
1299
1300 #ifndef CLIB_MARCH_VARIANT
1301 int
1302 ip6_sv_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
1303 {
1304   ip6_sv_reass_main_t *rm = &ip6_sv_reass_main;
1305   vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
1306   if (is_enable)
1307     {
1308       if (!rm->feature_use_refcount_per_intf[sw_if_index])
1309         {
1310           ++rm->feature_use_refcount_per_intf[sw_if_index];
1311           return vnet_feature_enable_disable ("ip6-unicast",
1312                                               "ip6-sv-reassembly-feature",
1313                                               sw_if_index, 1, 0, 0);
1314         }
1315       ++rm->feature_use_refcount_per_intf[sw_if_index];
1316     }
1317   else
1318     {
1319       --rm->feature_use_refcount_per_intf[sw_if_index];
1320       if (!rm->feature_use_refcount_per_intf[sw_if_index])
1321         return vnet_feature_enable_disable ("ip6-unicast",
1322                                             "ip6-sv-reassembly-feature",
1323                                             sw_if_index, 0, 0, 0);
1324     }
1325   return -1;
1326 }
1327 #endif
1328
1329 /*
1330  * fd.io coding-style-patch-verification: ON
1331  *
1332  * Local Variables:
1333  * eval: (c-set-style "gnu")
1334  * End:
1335  */