FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
ip6_reassembly.c
Go to the documentation of this file.
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 Reassembly.
19  *
20  * This file contains the source code for IPv6 reassembly.
21  */
22 
23 #include <vppinfra/vec.h>
24 #include <vnet/vnet.h>
25 #include <vnet/ip/ip.h>
26 #include <vppinfra/bihash_48_8.h>
27 #include <vnet/ip/ip6_reassembly.h>
28 
29 #define MSEC_PER_SEC 1000
30 #define IP6_REASS_TIMEOUT_DEFAULT_MS 100
31 #define IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
32 #define IP6_REASS_MAX_REASSEMBLIES_DEFAULT 1024
33 #define IP6_REASS_HT_LOAD_FACTOR (0.75)
34 
35 typedef enum
36 {
41 
42 typedef struct
43 {
44  union
45  {
46  struct
47  {
52  u8 unused[7];
54  };
55  u64 as_u64[6];
56  };
58 
59 typedef union
60 {
61  struct
62  {
65  };
68 
69 typedef union
70 {
71  struct
72  {
75  };
78 
79 
82 {
84  return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
85 }
86 
89 {
91  return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
92  (vnb->ip.reass.fragment_first + ip6_reass_buffer_get_data_offset (b)) + 1;
93 }
94 
95 typedef struct
96 {
97  // hash table key
99  // time when last packet was received
101  // internal id of this reassembly
103  // buffer index of first buffer in this reassembly context
105  // last octet of packet, ~0 until fragment without more_fragments arrives
107  // length of data collected so far
109  // trace operation counter
111  // next index - used by non-feature node
113  // minimum fragment length for this reassembly - used to estimate MTU
115 } ip6_reass_t;
116 
117 typedef struct
118 {
124 
125 typedef struct
126 {
127  // IPv6 config
132 
133  // IPv6 runtime
134  clib_bihash_48_8_t hash;
135 
136  // per-thread data
138 
139  // convenience
142 
143  // node index of ip6-drop node
147 
148  /** Worker handoff */
151 
153 
155 
156 #ifndef CLIB_MARCH_VARIANT
158 #endif /* CLIB_MARCH_VARIANT */
159 
160 typedef enum
161 {
168 
169 typedef enum
170 {
178 
179 typedef struct
180 {
188 
189 typedef struct
190 {
191  ip6_reass_trace_operation_e action;
200 
201 static void
204 {
205  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
207  trace->range_first = vnb->ip.reass.range_first;
208  trace->range_last = vnb->ip.reass.range_last;
211  trace->range_bi = bi;
212 }
213 
214 static u8 *
215 format_ip6_reass_range_trace (u8 * s, va_list * args)
216 {
218  s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
219  trace->range_last, trace->data_offset, trace->data_len,
220  trace->range_bi);
221  return s;
222 }
223 
224 static u8 *
225 format_ip6_reass_trace (u8 * s, va_list * args)
226 {
227  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
228  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
229  ip6_reass_trace_t *t = va_arg (*args, ip6_reass_trace_t *);
230  s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
231  u32 indent = format_get_indent (s);
232  s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
234  t->fragment_last);
235  switch (t->action)
236  {
237  case RANGE_NEW:
238  s = format (s, "\n%Unew %U", format_white_space, indent,
240  break;
241  case RANGE_OVERLAP:
242  s = format (s, "\n%Uoverlap %U", format_white_space, indent,
244  break;
246  s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
248  &t->trace_range);
249  break;
251  s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
253  &t->trace_range);
254  break;
256  s = format (s, "\n%Uicmp-error - reassembly time exceeded",
257  format_white_space, indent);
258  break;
259  case FINALIZE:
260  s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
261  break;
262  }
263  return s;
264 }
265 
266 static void
268  ip6_reass_main_t * rm, ip6_reass_t * reass,
269  u32 bi, ip6_reass_trace_operation_e action,
270  u32 size_diff)
271 {
272  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
275  {
276  // this buffer's trace is gone
277  b->flags &= ~VLIB_BUFFER_IS_TRACED;
278  return;
279  }
280  ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
281  t->reass_id = reass->id;
282  t->action = action;
283  ip6_reass_trace_details (vm, bi, &t->trace_range);
284  t->size_diff = size_diff;
285  t->op_id = reass->trace_op_counter;
286  ++reass->trace_op_counter;
287  t->fragment_first = vnb->ip.reass.fragment_first;
288  t->fragment_last = vnb->ip.reass.fragment_last;
289  t->trace_range.first_bi = reass->first_bi;
290  t->total_data_len = reass->data_len;
291 #if 0
292  static u8 *s = NULL;
293  s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t);
294  printf ("%.*s\n", vec_len (s), s);
295  fflush (stdout);
296  vec_reset_length (s);
297 #endif
298 }
299 
300 always_inline void
302  ip6_reass_t * reass)
303 {
305  kv.key[0] = reass->key.as_u64[0];
306  kv.key[1] = reass->key.as_u64[1];
307  kv.key[2] = reass->key.as_u64[2];
308  kv.key[3] = reass->key.as_u64[3];
309  kv.key[4] = reass->key.as_u64[4];
310  kv.key[5] = reass->key.as_u64[5];
311  clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
312  pool_put (rt->pool, reass);
313  --rt->reass_n;
314 }
315 
316 always_inline void
318  ip6_reass_t * reass)
319 {
320  u32 range_bi = reass->first_bi;
321  vlib_buffer_t *range_b;
322  vnet_buffer_opaque_t *range_vnb;
323  u32 *to_free = NULL;
324  while (~0 != range_bi)
325  {
326  range_b = vlib_get_buffer (vm, range_bi);
327  range_vnb = vnet_buffer (range_b);
328  u32 bi = range_bi;
329  while (~0 != bi)
330  {
331  vec_add1 (to_free, bi);
332  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
333  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
334  {
335  bi = b->next_buffer;
336  b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
337  }
338  else
339  {
340  bi = ~0;
341  }
342  }
343  range_bi = range_vnb->ip.reass.next_range_bi;
344  }
345  vlib_buffer_free (vm, to_free, vec_len (to_free));
346  vec_free (to_free);
347 }
348 
349 always_inline void
351  ip6_reass_main_t * rm, ip6_reass_t * reass,
352  u32 * icmp_bi)
353 {
354  if (~0 == reass->first_bi)
355  {
356  return;
357  }
358  vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
359  if (0 == vnet_buffer (b)->ip.reass.fragment_first)
360  {
361  *icmp_bi = reass->first_bi;
362  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
363  {
364  ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi,
366  }
367  // fragment with offset zero received - send icmp message back
368  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
369  {
370  // separate first buffer from chain and steer it towards icmp node
371  b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
372  reass->first_bi = b->next_buffer;
373  }
374  else
375  {
376  reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
377  }
378  icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
379  ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
380  0);
381  }
382  ip6_reass_drop_all (vm, rm, reass);
383 }
384 
388  ip6_reass_kv_t * kv, u32 * icmp_bi, u8 * do_handoff)
389 {
390  ip6_reass_t *reass = NULL;
391  f64 now = vlib_time_now (rm->vlib_main);
392 
393  if (!clib_bihash_search_48_8
394  (&rm->hash, (clib_bihash_kv_48_8_t *) kv, (clib_bihash_kv_48_8_t *) kv))
395  {
396  if (vm->thread_index != kv->v.thread_index)
397  {
398  *do_handoff = 1;
399  return NULL;
400  }
401  reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
402 
403  if (now > reass->last_heard + rm->timeout)
404  {
405  ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi);
406  ip6_reass_free (rm, rt, reass);
407  reass = NULL;
408  }
409  }
410 
411  if (reass)
412  {
413  reass->last_heard = now;
414  return reass;
415  }
416 
417  if (rt->reass_n >= rm->max_reass_n)
418  {
419  reass = NULL;
420  return reass;
421  }
422  else
423  {
424  pool_get (rt->pool, reass);
425  clib_memset (reass, 0, sizeof (*reass));
426  reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
427  ++rt->id_counter;
428  reass->first_bi = ~0;
429  reass->last_packet_octet = ~0;
430  reass->data_len = 0;
431  ++rt->reass_n;
432  }
433 
434  reass->key.as_u64[0] = ((clib_bihash_kv_48_8_t *) kv)->key[0];
435  reass->key.as_u64[1] = ((clib_bihash_kv_48_8_t *) kv)->key[1];
436  reass->key.as_u64[2] = ((clib_bihash_kv_48_8_t *) kv)->key[2];
437  reass->key.as_u64[3] = ((clib_bihash_kv_48_8_t *) kv)->key[3];
438  reass->key.as_u64[4] = ((clib_bihash_kv_48_8_t *) kv)->key[4];
439  reass->key.as_u64[5] = ((clib_bihash_kv_48_8_t *) kv)->key[5];
440  kv->v.reass_index = (reass - rt->pool);
441  kv->v.thread_index = vm->thread_index;
442  reass->last_heard = now;
443 
444  if (clib_bihash_add_del_48_8 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, 1))
445  {
446  ip6_reass_free (rm, rt, reass);
447  reass = NULL;
448  }
449 
450  return reass;
451 }
452 
453 always_inline ip6_reass_rc_t
456  ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
457  bool is_feature)
458 {
459  *bi0 = reass->first_bi;
460  *error0 = IP6_ERROR_NONE;
461  ip6_frag_hdr_t *frag_hdr;
462  vlib_buffer_t *last_b = NULL;
463  u32 sub_chain_bi = reass->first_bi;
464  u32 total_length = 0;
465  u32 buf_cnt = 0;
466  u32 dropped_cnt = 0;
467  u32 *vec_drop_compress = NULL;
468  ip6_reass_rc_t rv = IP6_REASS_RC_OK;
469  do
470  {
471  u32 tmp_bi = sub_chain_bi;
472  vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
473  vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
474  if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
475  !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
476  {
478  goto free_buffers_and_return;
479  }
480 
481  u32 data_len = ip6_reass_buffer_get_data_len (tmp);
482  u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
483  sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp);
484  u32 trim_end =
485  vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
486  if (tmp_bi == reass->first_bi)
487  {
488  /* first buffer - keep ip6 header */
489  if (0 != ip6_reass_buffer_get_data_offset (tmp))
490  {
492  goto free_buffers_and_return;
493  }
494  trim_front = 0;
495  trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
496  (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
497  sizeof (*frag_hdr));
498  if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
499  {
501  goto free_buffers_and_return;
502  }
503  }
504  u32 keep_data =
505  vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
506  while (1)
507  {
508  ++buf_cnt;
509  if (trim_front)
510  {
511  if (trim_front > tmp->current_length)
512  {
513  /* drop whole buffer */
514  vec_add1 (vec_drop_compress, tmp_bi);
515  trim_front -= tmp->current_length;
516  if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
517  {
519  goto free_buffers_and_return;
520  }
521  tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
522  tmp_bi = tmp->next_buffer;
523  tmp = vlib_get_buffer (vm, tmp_bi);
524  continue;
525  }
526  else
527  {
528  vlib_buffer_advance (tmp, trim_front);
529  trim_front = 0;
530  }
531  }
532  if (keep_data)
533  {
534  if (last_b)
535  {
536  last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
537  last_b->next_buffer = tmp_bi;
538  }
539  last_b = tmp;
540  if (keep_data <= tmp->current_length)
541  {
542  tmp->current_length = keep_data;
543  keep_data = 0;
544  }
545  else
546  {
547  keep_data -= tmp->current_length;
548  if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
549  {
551  goto free_buffers_and_return;
552  }
553  }
554  total_length += tmp->current_length;
555  }
556  else
557  {
558  vec_add1 (vec_drop_compress, tmp_bi);
559  if (reass->first_bi == tmp_bi)
560  {
562  goto free_buffers_and_return;
563  }
564  ++dropped_cnt;
565  }
566  if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
567  {
568  tmp_bi = tmp->next_buffer;
569  tmp = vlib_get_buffer (vm, tmp->next_buffer);
570  }
571  else
572  {
573  break;
574  }
575  }
576  sub_chain_bi =
577  vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
578  reass.next_range_bi;
579  }
580  while (~0 != sub_chain_bi);
581 
582  if (!last_b)
583  {
585  goto free_buffers_and_return;
586  }
587  last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
588  vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
589  if (total_length < first_b->current_length)
590  {
592  goto free_buffers_and_return;
593  }
594  total_length -= first_b->current_length;
595  first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
596  first_b->total_length_not_including_first_buffer = total_length;
597  // drop fragment header
598  vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
599  ip6_header_t *ip = vlib_buffer_get_current (first_b);
600  u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
601  ip6_ext_header_t *prev_hdr;
602  ip6_ext_header_find_t (ip, prev_hdr, frag_hdr,
603  IP_PROTOCOL_IPV6_FRAGMENTATION);
604  if (prev_hdr)
605  {
606  prev_hdr->next_hdr = frag_hdr->next_hdr;
607  }
608  else
609  {
610  ip->protocol = frag_hdr->next_hdr;
611  }
612  if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
613  {
615  goto free_buffers_and_return;
616  }
617  memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
618  first_b->current_length - ip6_frag_hdr_offset -
619  sizeof (ip6_frag_hdr_t));
620  first_b->current_length -= sizeof (*frag_hdr);
621  ip->payload_length =
622  clib_host_to_net_u16 (total_length + first_b->current_length -
623  sizeof (*ip));
624  if (!vlib_buffer_chain_linearize (vm, first_b))
625  {
626  rv = IP6_REASS_RC_NO_BUF;
627  goto free_buffers_and_return;
628  }
629  if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
630  {
631  ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
632 #if 0
633  // following code does a hexdump of packet fragments to stdout ...
634  do
635  {
636  u32 bi = reass->first_bi;
637  u8 *s = NULL;
638  while (~0 != bi)
639  {
640  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
641  s = format (s, "%u: %U\n", bi, format_hexdump,
643  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
644  {
645  bi = b->next_buffer;
646  }
647  else
648  {
649  break;
650  }
651  }
652  printf ("%.*s\n", vec_len (s), s);
653  fflush (stdout);
654  vec_free (s);
655  }
656  while (0);
657 #endif
658  }
659  if (is_feature)
660  {
661  *next0 = IP6_REASSEMBLY_NEXT_INPUT;
662  }
663  else
664  {
665  *next0 = reass->next_index;
666  }
667  vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
668  ip6_reass_free (rm, rt, reass);
669  reass = NULL;
670 free_buffers_and_return:
671  vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
672  vec_free (vec_drop_compress);
673  return rv;
674 }
675 
676 always_inline void
679  ip6_reass_t * reass, u32 prev_range_bi,
680  u32 new_next_bi)
681 {
682 
683  vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
684  vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
685  if (~0 != prev_range_bi)
686  {
687  vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
688  vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
689  new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
690  prev_vnb->ip.reass.next_range_bi = new_next_bi;
691  }
692  else
693  {
694  if (~0 != reass->first_bi)
695  {
696  new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
697  }
698  reass->first_bi = new_next_bi;
699  }
700  reass->data_len += ip6_reass_buffer_get_data_len (new_next_b);
701 }
702 
703 always_inline ip6_reass_rc_t
706  ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
707  ip6_frag_hdr_t * frag_hdr, bool is_feature)
708 {
709  int consumed = 0;
710  vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
711  vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
712  reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
713  fvnb->ip.reass.ip6_frag_hdr_offset =
714  (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
716  if (fb->current_length < sizeof (*fip) ||
717  fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
718  fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
719  {
721  }
722 
723  u32 fragment_first = fvnb->ip.reass.fragment_first =
724  ip6_frag_hdr_offset_bytes (frag_hdr);
725  u32 fragment_length =
726  vlib_buffer_length_in_chain (vm, fb) -
727  (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
728  u32 fragment_last = fvnb->ip.reass.fragment_last =
729  fragment_first + fragment_length - 1;
730  int more_fragments = ip6_frag_hdr_more (frag_hdr);
731  u32 candidate_range_bi = reass->first_bi;
732  u32 prev_range_bi = ~0;
733  fvnb->ip.reass.range_first = fragment_first;
734  fvnb->ip.reass.range_last = fragment_last;
735  fvnb->ip.reass.next_range_bi = ~0;
736  if (!more_fragments)
737  {
738  reass->last_packet_octet = fragment_last;
739  }
740  if (~0 == reass->first_bi)
741  {
742  // starting a new reassembly
743  ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
744  *bi0);
745  reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
746  consumed = 1;
747  goto check_if_done_maybe;
748  }
749  reass->min_fragment_length =
750  clib_min (clib_net_to_host_u16 (fip->payload_length),
751  fvnb->ip.reass.estimated_mtu);
752  while (~0 != candidate_range_bi)
753  {
754  vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
755  vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
756  if (fragment_first > candidate_vnb->ip.reass.range_last)
757  {
758  // this fragments starts after candidate range
759  prev_range_bi = candidate_range_bi;
760  candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
761  if (candidate_vnb->ip.reass.range_last < fragment_last &&
762  ~0 == candidate_range_bi)
763  {
764  // special case - this fragment falls beyond all known ranges
765  ip6_reass_insert_range_in_chain (vm, rm, rt, reass,
766  prev_range_bi, *bi0);
767  consumed = 1;
768  break;
769  }
770  continue;
771  }
772  if (fragment_last < candidate_vnb->ip.reass.range_first)
773  {
774  // this fragment ends before candidate range without any overlap
775  ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
776  *bi0);
777  consumed = 1;
778  }
779  else if (fragment_first == candidate_vnb->ip.reass.range_first &&
780  fragment_last == candidate_vnb->ip.reass.range_last)
781  {
782  // duplicate fragment - ignore
783  }
784  else
785  {
786  // overlapping fragment - not allowed by RFC 8200
787  ip6_reass_drop_all (vm, rm, reass);
788  ip6_reass_free (rm, rt, reass);
789  if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
790  {
791  ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP,
792  0);
793  }
794  *next0 = IP6_REASSEMBLY_NEXT_DROP;
795  *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
796  return IP6_REASS_RC_OK;
797  }
798  break;
799  }
800 check_if_done_maybe:
801  if (consumed)
802  {
803  if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
804  {
805  ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
806  }
807  }
808  if (~0 != reass->last_packet_octet &&
809  reass->data_len == reass->last_packet_octet + 1)
810  {
811  return ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
812  is_feature);
813  }
814  else
815  {
816  if (consumed)
817  {
818  *bi0 = ~0;
819  }
820  else
821  {
822  *next0 = IP6_REASSEMBLY_NEXT_DROP;
823  *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
824  }
825  }
826  return IP6_REASS_RC_OK;
827 }
828 
829 always_inline bool
831  vlib_buffer_t * b,
832  ip6_frag_hdr_t * frag_hdr)
833 {
834  ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
835  while (ip6_ext_hdr (tmp->next_hdr))
836  {
837  tmp = ip6_ext_next_header (tmp);
838  }
839  if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
840  {
841  icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
842  ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
843  0);
844  b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
845 
846  return false;
847  }
848  return true;
849 }
850 
851 always_inline bool
853  vlib_node_runtime_t * node,
854  vlib_buffer_t * b,
855  ip6_frag_hdr_t * frag_hdr)
856 {
859  int more_fragments = ip6_frag_hdr_more (frag_hdr);
860  u32 fragment_length =
862  (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
863  if (more_fragments && 0 != fragment_length % 8)
864  {
865  icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
866  ICMP6_parameter_problem_erroneous_header_field,
867  (u8 *) & ip->payload_length - (u8 *) ip);
868  return false;
869  }
870  return true;
871 }
872 
873 always_inline bool
875  vlib_node_runtime_t * node,
876  vlib_buffer_t * b,
877  ip6_frag_hdr_t * frag_hdr)
878 {
880  u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
881  u32 fragment_length =
883  (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
884  if (fragment_first + fragment_length > 65535)
885  {
887  icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
888  ICMP6_parameter_problem_erroneous_header_field,
889  (u8 *) & frag_hdr->fragment_offset_and_more
890  - (u8 *) ip0);
891  return false;
892  }
893  return true;
894 }
895 
898  vlib_node_runtime_t * node,
899  vlib_frame_t * frame, bool is_feature)
900 {
901  u32 *from = vlib_frame_vector_args (frame);
902  u32 n_left_from, n_left_to_next, *to_next, next_index;
905  clib_spinlock_lock (&rt->lock);
906 
907  n_left_from = frame->n_vectors;
908  next_index = node->cached_next_index;
909  while (n_left_from > 0)
910  {
911  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
912 
913  while (n_left_from > 0 && n_left_to_next > 0)
914  {
915  u32 bi0;
916  vlib_buffer_t *b0;
918  u32 error0 = IP6_ERROR_NONE;
919  u32 icmp_bi = ~0;
920 
921  bi0 = from[0];
922  b0 = vlib_get_buffer (vm, bi0);
923 
925  ip6_frag_hdr_t *frag_hdr = NULL;
926  ip6_ext_header_t *prev_hdr;
927  if (ip6_ext_hdr (ip0->protocol))
928  {
929  ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr,
930  IP_PROTOCOL_IPV6_FRAGMENTATION);
931  }
932  if (!frag_hdr)
933  {
934  // this is a regular packet - no fragmentation
936  goto skip_reass;
937  }
938  if (0 == ip6_frag_hdr_offset (frag_hdr))
939  {
940  // first fragment - verify upper-layer is present
941  if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr))
942  {
944  goto skip_reass;
945  }
946  }
947  if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr)
948  || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0,
949  frag_hdr))
950  {
952  goto skip_reass;
953  }
954  vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
955  (u8 *) frag_hdr - (u8 *) ip0;
956 
957  ip6_reass_kv_t kv;
958  u8 do_handoff = 0;
959 
960  kv.k.as_u64[0] = ip0->src_address.as_u64[0];
961  kv.k.as_u64[1] = ip0->src_address.as_u64[1];
962  kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
963  kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
964  kv.k.as_u64[4] =
966  vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
967  (u64) frag_hdr->identification;
968  kv.k.as_u64[5] = ip0->protocol;
969 
970  ip6_reass_t *reass =
971  ip6_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
972  &do_handoff);
973 
974  if (PREDICT_FALSE (do_handoff))
975  {
977  if (is_feature)
978  vnet_buffer (b0)->ip.reass.owner_feature_thread_index =
979  kv.v.thread_index;
980  else
981  vnet_buffer (b0)->ip.reass.owner_thread_index =
982  kv.v.thread_index;
983  }
984  else if (reass)
985  {
986  switch (ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
987  &error0, frag_hdr, is_feature))
988  {
989  case IP6_REASS_RC_OK:
990  /* nothing to do here */
991  break;
992  case IP6_REASS_RC_NO_BUF:
993  /* fallthrough */
995  /* drop everything and start with a clean slate */
996  ip6_reass_drop_all (vm, rm, reass);
997  ip6_reass_free (rm, rt, reass);
998  goto next_packet;
999  break;
1000  }
1001  }
1002  else
1003  {
1004  next0 = IP6_REASSEMBLY_NEXT_DROP;
1005  error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1006  }
1007 
1008  b0->error = node->errors[error0];
1009 
1010  if (~0 != bi0)
1011  {
1012  skip_reass:
1013  to_next[0] = bi0;
1014  to_next += 1;
1015  n_left_to_next -= 1;
1016  if (is_feature && IP6_ERROR_NONE == error0)
1017  {
1018  b0 = vlib_get_buffer (vm, bi0);
1019  vnet_feature_next (&next0, b0);
1020  }
1021  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1022  n_left_to_next, bi0, next0);
1023  }
1024 
1025  if (~0 != icmp_bi)
1026  {
1028  to_next[0] = icmp_bi;
1029  to_next += 1;
1030  n_left_to_next -= 1;
1031  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1032  n_left_to_next, icmp_bi,
1033  next0);
1034  }
1035  next_packet:
1036  from += 1;
1037  n_left_from -= 1;
1038  }
1039 
1040  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1041  }
1042 
1043  clib_spinlock_unlock (&rt->lock);
1044  return frame->n_vectors;
1045 }
1046 
1048 #define _(sym, string) string,
1050 #undef _
1051 };
1052 
1054  vlib_frame_t * frame)
1055 {
1056  return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ );
1057 }
1058 
1059 /* *INDENT-OFF* */
1061  .name = "ip6-reassembly",
1062  .vector_size = sizeof (u32),
1063  .format_trace = format_ip6_reass_trace,
1064  .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1065  .error_strings = ip6_reassembly_error_strings,
1066  .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1067  .next_nodes =
1068  {
1069  [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1070  [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1071  [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1072  [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reassembly-handoff",
1073  },
1074 };
1075 /* *INDENT-ON* */
1076 
1078  vlib_node_runtime_t * node,
1079  vlib_frame_t * frame)
1080 {
1081  return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ );
1082 }
1083 
1084 /* *INDENT-OFF* */
1086  .name = "ip6-reassembly-feature",
1087  .vector_size = sizeof (u32),
1088  .format_trace = format_ip6_reass_trace,
1089  .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1090  .error_strings = ip6_reassembly_error_strings,
1091  .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1092  .next_nodes =
1093  {
1094  [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1095  [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1096  [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1097  [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reass-feature-hoff",
1098  },
1099 };
1100 /* *INDENT-ON* */
1101 
1102 /* *INDENT-OFF* */
1103 VNET_FEATURE_INIT (ip6_reassembly_feature, static) = {
1104  .arc_name = "ip6-unicast",
1105  .node_name = "ip6-reassembly-feature",
1106  .runs_before = VNET_FEATURES ("ip6-lookup"),
1107  .runs_after = 0,
1108 };
1109 /* *INDENT-ON* */
1110 
1111 #ifndef CLIB_MARCH_VARIANT
1112 static u32
1114 {
1116  u32 nbuckets;
1117  u8 i;
1118 
1119  nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR);
1120 
1121  for (i = 0; i < 31; i++)
1122  if ((1 << i) >= nbuckets)
1123  break;
1124  nbuckets = 1 << i;
1125 
1126  return nbuckets;
1127 }
1128 #endif /* CLIB_MARCH_VARIANT */
1129 
1130 typedef enum
1131 {
1134 
1135 #ifndef CLIB_MARCH_VARIANT
1136 typedef struct
1137 {
1138  int failure;
1139  clib_bihash_48_8_t *new_hash;
1141 
1142 static void
1144 {
1145  ip6_rehash_cb_ctx *ctx = _ctx;
1146  if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1147  {
1148  ctx->failure = 1;
1149  }
1150 }
1151 
1152 static void
1153 ip6_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1154  u32 expire_walk_interval_ms)
1155 {
1156  ip6_reass_main.timeout_ms = timeout_ms;
1157  ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1158  ip6_reass_main.max_reass_n = max_reassemblies;
1159  ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1160 }
1161 
1163 ip6_reass_set (u32 timeout_ms, u32 max_reassemblies,
1164  u32 expire_walk_interval_ms)
1165 {
1166  u32 old_nbuckets = ip6_reass_get_nbuckets ();
1167  ip6_reass_set_params (timeout_ms, max_reassemblies,
1168  expire_walk_interval_ms);
1169  vlib_process_signal_event (ip6_reass_main.vlib_main,
1170  ip6_reass_main.ip6_reass_expire_node_idx,
1172  u32 new_nbuckets = ip6_reass_get_nbuckets ();
1173  if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
1174  {
1175  clib_bihash_48_8_t new_hash;
1176  clib_memset (&new_hash, 0, sizeof (new_hash));
1178  ctx.failure = 0;
1179  ctx.new_hash = &new_hash;
1180  clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets,
1181  new_nbuckets * 1024);
1182  clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash,
1183  ip6_rehash_cb, &ctx);
1184  if (ctx.failure)
1185  {
1186  clib_bihash_free_48_8 (&new_hash);
1187  return -1;
1188  }
1189  else
1190  {
1191  clib_bihash_free_48_8 (&ip6_reass_main.hash);
1192  clib_memcpy_fast (&ip6_reass_main.hash, &new_hash,
1193  sizeof (ip6_reass_main.hash));
1194  }
1195  }
1196  return 0;
1197 }
1198 
1200 ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1201  u32 * expire_walk_interval_ms)
1202 {
1203  *timeout_ms = ip6_reass_main.timeout_ms;
1204  *max_reassemblies = ip6_reass_main.max_reass_n;
1205  *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms;
1206  return 0;
1207 }
1208 
1209 static clib_error_t *
1211 {
1213  clib_error_t *error = 0;
1214  u32 nbuckets;
1215  vlib_node_t *node;
1216 
1217  rm->vlib_main = vm;
1218  rm->vnet_main = vnet_get_main ();
1219 
1222  vec_foreach (rt, rm->per_thread_data)
1223  {
1224  clib_spinlock_init (&rt->lock);
1225  pool_alloc (rt->pool, rm->max_reass_n);
1226  }
1227 
1228  node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk");
1229  ASSERT (node);
1230  rm->ip6_reass_expire_node_idx = node->index;
1231 
1235 
1236  nbuckets = ip6_reass_get_nbuckets ();
1237  clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024);
1238 
1239  node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
1240  ASSERT (node);
1241  rm->ip6_drop_idx = node->index;
1242  node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1243  ASSERT (node);
1244  rm->ip6_icmp_error_idx = node->index;
1245 
1246  if ((error = vlib_call_init_function (vm, ip_main_init)))
1247  return error;
1248  ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1249  ip6_reass_node.index);
1250 
1252  rm->fq_feature_index =
1254 
1255  return error;
1256 }
1257 
1259 #endif /* CLIB_MARCH_VARIANT */
1260 
1261 static uword
1263  vlib_node_runtime_t * node, vlib_frame_t * f)
1264 {
1266  uword event_type, *event_data = 0;
1267 
1268  while (true)
1269  {
1272  / (f64) MSEC_PER_SEC);
1273  event_type = vlib_process_get_events (vm, &event_data);
1274 
1275  switch (event_type)
1276  {
1277  case ~0: /* no events => timeout */
1278  /* nothing to do here */
1279  break;
1281  break;
1282  default:
1283  clib_warning ("BUG: event type 0x%wx", event_type);
1284  break;
1285  }
1286  f64 now = vlib_time_now (vm);
1287 
1288  ip6_reass_t *reass;
1289  int *pool_indexes_to_free = NULL;
1290 
1291  uword thread_index = 0;
1292  int index;
1293  const uword nthreads = vlib_num_workers () + 1;
1294  u32 *vec_icmp_bi = NULL;
1295  for (thread_index = 0; thread_index < nthreads; ++thread_index)
1296  {
1297  ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1298  clib_spinlock_lock (&rt->lock);
1299 
1300  vec_reset_length (pool_indexes_to_free);
1301  /* *INDENT-OFF* */
1302  pool_foreach_index (index, rt->pool, ({
1303  reass = pool_elt_at_index (rt->pool, index);
1304  if (now > reass->last_heard + rm->timeout)
1305  {
1306  vec_add1 (pool_indexes_to_free, index);
1307  }
1308  }));
1309  /* *INDENT-ON* */
1310  int *i;
1311  /* *INDENT-OFF* */
1312  vec_foreach (i, pool_indexes_to_free)
1313  {
1314  ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1315  u32 icmp_bi = ~0;
1316  vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1317  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1318  {
1320  b->trace_index))
1321  {
1322  /* the trace is gone, don't trace this buffer anymore */
1323  b->flags &= ~VLIB_BUFFER_IS_TRACED;
1324  }
1325  }
1326  ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
1327  if (~0 != icmp_bi)
1328  {
1329  vec_add1 (vec_icmp_bi, icmp_bi);
1330  }
1331  ip6_reass_free (rm, rt, reass);
1332  }
1333  /* *INDENT-ON* */
1334 
1335  clib_spinlock_unlock (&rt->lock);
1336  }
1337 
1338  while (vec_len (vec_icmp_bi) > 0)
1339  {
1340  vlib_frame_t *f =
1342  u32 *to_next = vlib_frame_vector_args (f);
1343  u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
1344  int trace_frame = 0;
1345  while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1346  {
1347  u32 bi = vec_pop (vec_icmp_bi);
1348  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1349  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1350  {
1352  b->trace_index))
1353  {
1354  /* the trace is gone, don't trace this buffer anymore */
1355  b->flags &= ~VLIB_BUFFER_IS_TRACED;
1356  }
1357  else
1358  {
1359  trace_frame = 1;
1360  }
1361  }
1362  b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1363  to_next[0] = bi;
1364  ++f->n_vectors;
1365  to_next += 1;
1366  n_left_to_next -= 1;
1367  }
1368  f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
1370  }
1371 
1372  vec_free (pool_indexes_to_free);
1373  vec_free (vec_icmp_bi);
1374  if (event_data)
1375  {
1376  _vec_len (event_data) = 0;
1377  }
1378  }
1379 
1380  return 0;
1381 }
1382 
1383 /* *INDENT-OFF* */
1385  .function = ip6_reass_walk_expired,
1386  .format_trace = format_ip6_reass_trace,
1387  .type = VLIB_NODE_TYPE_PROCESS,
1388  .name = "ip6-reassembly-expire-walk",
1389 
1391  .error_strings = ip6_reassembly_error_strings,
1392 
1393 };
1394 /* *INDENT-ON* */
1395 
1396 static u8 *
1397 format_ip6_reass_key (u8 * s, va_list * args)
1398 {
1399  ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *);
1400  s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1402  &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1403  return s;
1404 }
1405 
1406 static u8 *
1407 format_ip6_reass (u8 * s, va_list * args)
1408 {
1409  vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1410  ip6_reass_t *reass = va_arg (*args, ip6_reass_t *);
1411 
1412  s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
1413  "last_packet_octet: %u, trace_op_counter: %u\n",
1414  reass->id, format_ip6_reass_key, &reass->key, reass->first_bi,
1415  reass->data_len, reass->last_packet_octet,
1416  reass->trace_op_counter);
1417  u32 bi = reass->first_bi;
1418  u32 counter = 0;
1419  while (~0 != bi)
1420  {
1421  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1422  vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1423  s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1424  "fragment[%u, %u]\n",
1425  counter, vnb->ip.reass.range_first,
1426  vnb->ip.reass.range_last, bi,
1429  vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1430  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1431  {
1432  bi = b->next_buffer;
1433  }
1434  else
1435  {
1436  bi = ~0;
1437  }
1438  }
1439  return s;
1440 }
1441 
1442 static clib_error_t *
1445 {
1447 
1448  vlib_cli_output (vm, "---------------------");
1449  vlib_cli_output (vm, "IP6 reassembly status");
1450  vlib_cli_output (vm, "---------------------");
1451  bool details = false;
1452  if (unformat (input, "details"))
1453  {
1454  details = true;
1455  }
1456 
1457  u32 sum_reass_n = 0;
1458  u64 sum_buffers_n = 0;
1459  ip6_reass_t *reass;
1460  uword thread_index;
1461  const uword nthreads = vlib_num_workers () + 1;
1462  for (thread_index = 0; thread_index < nthreads; ++thread_index)
1463  {
1464  ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1465  clib_spinlock_lock (&rt->lock);
1466  if (details)
1467  {
1468  /* *INDENT-OFF* */
1469  pool_foreach (reass, rt->pool, {
1470  vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass);
1471  });
1472  /* *INDENT-ON* */
1473  }
1474  sum_reass_n += rt->reass_n;
1475  clib_spinlock_unlock (&rt->lock);
1476  }
1477  vlib_cli_output (vm, "---------------------");
1478  vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1479  (long unsigned) sum_reass_n);
1480  vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1481  "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
1482  vlib_cli_output (vm, "Buffers in use: %lu\n",
1483  (long unsigned) sum_buffers_n);
1484  return 0;
1485 }
1486 
1487 /* *INDENT-OFF* */
1489  .path = "show ip6-reassembly",
1490  .short_help = "show ip6-reassembly [details]",
1491  .function = show_ip6_reass,
1492 };
1493 /* *INDENT-ON* */
1494 
1495 #ifndef CLIB_MARCH_VARIANT
1498 {
1499  return vnet_feature_enable_disable ("ip6-unicast", "ip6-reassembly-feature",
1500  sw_if_index, enable_disable, 0, 0);
1501 }
1502 #endif /* CLIB_MARCH_VARIANT */
1503 
1504 #define foreach_ip6_reassembly_handoff_error \
1505 _(CONGESTION_DROP, "congestion drop")
1506 
1507 
1508 typedef enum
1509 {
1510 #define _(sym,str) IP6_REASSEMBLY_HANDOFF_ERROR_##sym,
1512 #undef _
1515 
1517 #define _(sym,string) string,
1519 #undef _
1520 };
1521 
1522 typedef struct
1523 {
1526 
1527 static u8 *
1529 {
1530  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1531  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1533  va_arg (*args, ip6_reassembly_handoff_trace_t *);
1534 
1535  s =
1536  format (s, "ip6-reassembly-handoff: next-worker %d",
1537  t->next_worker_index);
1538 
1539  return s;
1540 }
1541 
1544  vlib_frame_t * frame, bool is_feature)
1545 {
1547 
1548  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1549  u32 n_enq, n_left_from, *from;
1550  u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1551  u32 fq_index;
1552 
1553  from = vlib_frame_vector_args (frame);
1554  n_left_from = frame->n_vectors;
1555  vlib_get_buffers (vm, from, bufs, n_left_from);
1556 
1557  b = bufs;
1558  ti = thread_indices;
1559 
1560  fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1561 
1562  while (n_left_from > 0)
1563  {
1564  ti[0] =
1565  (is_feature) ? vnet_buffer (b[0])->ip.
1566  reass.owner_feature_thread_index : vnet_buffer (b[0])->ip.
1567  reass.owner_thread_index;
1568 
1569  if (PREDICT_FALSE
1570  ((node->flags & VLIB_NODE_FLAG_TRACE)
1571  && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1572  {
1574  vlib_add_trace (vm, node, b[0], sizeof (*t));
1575  t->next_worker_index = ti[0];
1576  }
1577 
1578  n_left_from -= 1;
1579  ti += 1;
1580  b += 1;
1581  }
1582  n_enq =
1583  vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1584  frame->n_vectors, 1);
1585 
1586  if (n_enq < frame->n_vectors)
1588  IP6_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1589  frame->n_vectors - n_enq);
1590  return frame->n_vectors;
1591 }
1592 
1594  vlib_node_runtime_t * node,
1595  vlib_frame_t * frame)
1596 {
1597  return ip6_reassembly_handoff_inline (vm, node, frame,
1598  false /* is_feature */ );
1599 }
1600 
1601 /* *INDENT-OFF* */
1603  .name = "ip6-reassembly-handoff",
1604  .vector_size = sizeof (u32),
1605  .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1606  .error_strings = ip6_reassembly_handoff_error_strings,
1607  .format_trace = format_ip6_reassembly_handoff_trace,
1608 
1609  .n_next_nodes = 1,
1610 
1611  .next_nodes = {
1612  [0] = "error-drop",
1613  },
1614 };
1615 
1616 
1618  vlib_node_runtime_t * node, vlib_frame_t * frame)
1619 {
1620  return ip6_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1621 }
1622 
1623 
1624 /* *INDENT-OFF* */
1626  .name = "ip6-reass-feature-hoff",
1627  .vector_size = sizeof (u32),
1628  .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1629  .error_strings = ip6_reassembly_handoff_error_strings,
1630  .format_trace = format_ip6_reassembly_handoff_trace,
1631 
1632  .n_next_nodes = 1,
1633 
1634  .next_nodes = {
1635  [0] = "error-drop",
1636  },
1637 };
1638 /* *INDENT-ON* */
1639 
1640 /*
1641  * fd.io coding-style-patch-verification: ON
1642  *
1643  * Local Variables:
1644  * eval: (c-set-style "gnu")
1645  * End:
1646  */
#define IP6_REASS_TIMEOUT_DEFAULT_MS
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
u32 sw_if_index
Definition: ipsec_gre.api:37
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define foreach_ip6_reassembly_handoff_error
static void ip6_reass_drop_all(vlib_main_t *vm, ip6_reass_main_t *rm, ip6_reass_t *reass)
static ip6_reass_rc_t ip6_reass_update(vlib_main_t *vm, vlib_node_runtime_t *node, ip6_reass_main_t *rm, ip6_reass_per_thread_t *rt, ip6_reass_t *reass, u32 *bi0, u32 *next0, u32 *error0, ip6_frag_hdr_t *frag_hdr, bool is_feature)
vnet_api_error_t
Definition: api_errno.h:153
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:862
#define clib_min(x, y)
Definition: clib.h:295
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:89
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:74
#define CLIB_UNUSED(x)
Definition: clib.h:82
ip6_reass_next_t
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:673
vnet_main_t * vnet_main
void ip6_register_protocol(u32 protocol, u32 node_index)
Definition: ip6_forward.c:1474
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:865
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
#define pool_alloc(P, N)
Allocate N more free elements to pool (unspecified alignment).
Definition: pool.h:341
u64 as_u64
Definition: bihash_doc.h:63
IPv6 Reassembly.
#define IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS
u64 as_u64[2]
Definition: ip6_packet.h:51
unsigned long u64
Definition: types.h:89
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
#define NULL
Definition: clib.h:58
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 index
Definition: node.h:279
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
static clib_error_t * ip6_reass_init_function(vlib_main_t *vm)
u32 vlib_frame_queue_main_init(u32 node_index, u32 frame_queue_nelts)
Definition: threads.c:1762
u32 thread_index
Definition: main.h:197
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
ip6_reass_trace_operation_e action
static void * ip6_ext_next_header(ip6_ext_header_t *ext_hdr)
Definition: ip6_packet.h:552
int i
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:201
ip6_reass_event_t
ip6_reass_per_thread_t * per_thread_data
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:468
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:366
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
ip6_address_t src_address
Definition: ip6_packet.h:385
static void ip6_reass_trace_details(vlib_main_t *vm, u32 bi, ip6_reass_range_trace_t *trace)
unsigned char u8
Definition: types.h:56
#define vec_pop(V)
Returns last element of a vector and decrements its length.
Definition: vec.h:615
static u16 ip6_reass_buffer_get_data_len(vlib_buffer_t *b)
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
#define IP6_REASS_HT_LOAD_FACTOR
double f64
Definition: types.h:142
static u32 ip6_reass_get_nbuckets()
vlib_trace_header_t ** trace_buffer_pool
Definition: trace.h:72
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:516
static void ip6_reass_free(ip6_reass_main_t *rm, ip6_reass_per_thread_t *rt, ip6_reass_t *reass)
#define always_inline
Definition: clib.h:98
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:187
vnet_api_error_t ip6_reass_set(u32 timeout_ms, u32 max_reassemblies, u32 expire_walk_interval_ms)
set ip6 reassembly configuration
clib_bihash_48_8_t hash
static u8 * format_ip6_reass_range_trace(u8 *s, va_list *args)
unsigned int u32
Definition: types.h:88
static uword ip6_reassembly_handoff_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, bool is_feature)
#define vlib_call_init_function(vm, x)
Definition: init.h:260
#define VLIB_FRAME_SIZE
Definition: node.h:376
ip6_reass_main_t ip6_reass_main
void icmp6_error_set_vnet_buffer(vlib_buffer_t *b, u8 type, u8 code, u32 data)
Definition: icmp6.c:446
static char * ip6_reassembly_handoff_error_strings[]
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:57
static char * ip6_reassembly_error_strings[]
#define ip6_frag_hdr_more(hdr)
Definition: ip6_packet.h:606
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
static u32 vlib_buffer_chain_linearize(vlib_main_t *vm, vlib_buffer_t *b)
ip6_reass_key_t key
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:934
u16 frame_flags
Definition: node.h:383
long ctx[MAX_CONNS]
Definition: main.c:144
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:196
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
#define PREDICT_FALSE(x)
Definition: clib.h:111
static bool ip6_reass_verify_packet_size_lt_64k(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b, ip6_frag_hdr_t *frag_hdr)
static u32 ip6_reass_buffer_get_data_offset(vlib_buffer_t *b)
u32 node_index
Node index.
Definition: node.h:494
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
ip6_reass_range_trace_t trace_range
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
VNET_FEATURE_INIT(ip6_reassembly_feature, static)
ip6_reassembly_handoff_error_t
static vlib_cli_command_t show_ip6_reassembly_cmd
(constructor) VLIB_CLI_COMMAND (show_ip6_reassembly_cmd)
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
clib_error_t * ip_main_init(vlib_main_t *vm)
Definition: ip_init.c:45
ip6_reass_val_t v
u16 n_vectors
Definition: node.h:395
vlib_node_registration_t ip6_reass_node_feature
(constructor) VLIB_REGISTER_NODE (ip6_reass_node_feature)
format_function_t format_ip6_address
Definition: format.h:93
vlib_main_t * vm
Definition: buffer.c:312
static u8 * format_ip6_reassembly_handoff_trace(u8 *s, va_list *args)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:295
u32 trace_index
Specifies index into trace buffer if VLIB_PACKET_IS_TRACED flag is set.
Definition: buffer.h:163
#define clib_warning(format, args...)
Definition: error.h:59
static void ip6_rehash_cb(clib_bihash_kv_48_8_t *kv, void *_ctx)
u8 * format_hexdump(u8 *s, va_list *va)
Definition: std-formats.c:281
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
#define ip6_frag_hdr_offset_bytes(hdr)
Definition: ip6_packet.h:603
#define ARRAY_LEN(x)
Definition: clib.h:62
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:458
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
u32 fq_index
Worker handoff.
ip6_reass_rc_t
static u8 ip6_ext_hdr(u8 nexthdr)
Definition: ip6_packet.h:527
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
vlib_node_registration_t ip6_reassembly_handoff_node
(constructor) VLIB_REGISTER_NODE (ip6_reassembly_handoff_node)
signed int i32
Definition: types.h:77
static bool ip6_reass_verify_fragment_multiple_8(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b, ip6_frag_hdr_t *frag_hdr)
#define ip6_frag_hdr_offset(hdr)
Definition: ip6_packet.h:600
clib_spinlock_t lock
static u8 * format_ip6_reass_key(u8 *s, va_list *args)
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:513
static uword ip6_reassembly_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, bool is_feature)
#define ASSERT(truth)
ip6_main_t ip6_main
Definition: ip6_forward.c:2688
static ip6_reass_rc_t ip6_reass_finalize(vlib_main_t *vm, vlib_node_runtime_t *node, ip6_reass_main_t *rm, ip6_reass_per_thread_t *rt, ip6_reass_t *reass, u32 *bi0, u32 *next0, u32 *error0, bool is_feature)
vlib_node_registration_t ip6_reassembly_feature_handoff_node
(constructor) VLIB_REGISTER_NODE (ip6_reassembly_feature_handoff_node)
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:248
vlib_trace_main_t trace_main
Definition: main.h:141
static clib_error_t * show_ip6_reass(vlib_main_t *vm, unformat_input_t *input, CLIB_UNUSED(vlib_cli_command_t *lmd))
u16 min_fragment_length
#define VNET_FEATURES(...)
Definition: feature.h:435
vlib_main_t * vlib_main
static void ip6_reass_insert_range_in_chain(vlib_main_t *vm, ip6_reass_main_t *rm, ip6_reass_per_thread_t *rt, ip6_reass_t *reass, u32 prev_range_bi, u32 new_next_bi)
static u8 * format_ip6_reass_trace(u8 *s, va_list *args)
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
static u8 * format_ip6_reass(u8 *s, va_list *args)
#define vec_elt(v, i)
Get vector value at index i.
clib_bihash_kv_48_8_t kv
static void ip6_reass_on_timeout(vlib_main_t *vm, vlib_node_runtime_t *node, ip6_reass_main_t *rm, ip6_reass_t *reass, u32 *icmp_bi)
u16 payload_length
Definition: ip6_packet.h:376
#define MSEC_PER_SEC
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static uword ip6_reass_walk_expired(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *f)
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void ip6_reass_add_trace(vlib_main_t *vm, vlib_node_runtime_t *node, ip6_reass_main_t *rm, ip6_reass_t *reass, u32 bi, ip6_reass_trace_operation_e action, u32 size_diff)
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
typedef key
Definition: ipsec.api:244
ip6_reass_trace_operation_e
#define VLIB_FRAME_TRACE
Definition: node.h:433
#define foreach_ip6_error
Definition: ip6_error.h:43
static ip6_reass_t * ip6_reass_find_or_create(vlib_main_t *vm, vlib_node_runtime_t *node, ip6_reass_main_t *rm, ip6_reass_per_thread_t *rt, ip6_reass_kv_t *kv, u32 *icmp_bi, u8 *do_handoff)
#define IP6_REASS_MAX_REASSEMBLIES_DEFAULT
static_always_inline u32 vlib_buffer_enqueue_to_thread(vlib_main_t *vm, u32 frame_queue_index, u32 *buffer_indices, u16 *thread_indices, u32 n_packets, int drop_on_congestion)
Definition: buffer_node.h:487
vnet_api_error_t ip6_reass_get(u32 *timeout_ms, u32 *max_reassemblies, u32 *expire_walk_interval_ms)
get ip6 reassembly configuration
#define vnet_buffer(b)
Definition: buffer.h:369
clib_bihash_48_8_t * new_hash
static u32 vlib_num_workers()
Definition: threads.h:366
vlib_node_registration_t ip6_reass_node
(constructor) VLIB_REGISTER_NODE (ip6_reass_node)
#define vec_foreach(var, vec)
Vector iterator.
vnet_api_error_t ip6_reass_enable_disable(u32 sw_if_index, u8 enable_disable)
ip6_reass_key_t k
#define ip6_ext_header_find_t(i, p, m, t)
Definition: ip6_packet.h:561
u16 flags
Copy of main node flags.
Definition: node.h:507
static void ip6_reass_set_params(u32 timeout_ms, u32 max_reassemblies, u32 expire_walk_interval_ms)
static bool ip6_reass_verify_upper_layer_present(vlib_node_runtime_t *node, vlib_buffer_t *b, ip6_frag_hdr_t *frag_hdr)
ip6_address_t dst
struct vnet_buffer_opaque_t::@58::@60 ip
#define pool_foreach_index(i, v, body)
Iterate pool by index.
Definition: pool.h:538
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:244
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:301
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:167
ip6_address_t src
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:762
u32 * fib_index_by_sw_if_index
Definition: ip6.h:194
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
static vlib_node_registration_t ip6_reass_expire_node
(constructor) VLIB_REGISTER_NODE (ip6_reass_expire_node)
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
Definition: defs.h:46
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:274
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
ip6_address_t dst_address
Definition: ip6_packet.h:385