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