FD.io VPP  v18.07-34-g55fbdb9
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 
36 
37 typedef struct
38 {
39  union
40  {
41  struct
42  {
47  u8 unused[7];
49  };
50  u64 as_u64[6];
51  };
53 
56 {
58  return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
59 }
60 
63 {
65  ASSERT (vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first);
67 }
68 
71 {
73  return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
74  (vnb->ip.reass.fragment_first + ip6_reass_buffer_get_data_offset (b)) + 1;
75 }
76 
79 {
81  ASSERT (vnb->ip.reass.range_last > vnb->ip.reass.fragment_first);
83 }
84 
85 typedef struct
86 {
87  // hash table key
89  // time when last packet was received
91  // internal id of this reassembly
93  // buffer index of first buffer in this reassembly context
95  // last octet of packet, ~0 until fragment without more_fragments arrives
97  // length of data collected so far
99  // trace operation counter
101  // next index - used by non-feature node
103  // minimum fragment length for this reassembly - used to estimate MTU
105 } ip6_reass_t;
106 
107 typedef struct
108 {
115 
116 typedef struct
117 {
118  // IPv6 config
123 
124  // IPv6 runtime
125  clib_bihash_48_8_t hash;
126 
127  // per-thread data
129 
130  // convenience
133 
134  // node index of ip6-drop node
138 
140 
142 
143 typedef enum
144 {
150 
151 typedef enum
152 {
160 
161 typedef struct
162 {
170 
171 typedef struct
172 {
173  ip6_reass_trace_operation_e action;
182 
183 static void
186 {
187  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
189  trace->range_first = vnb->ip.reass.range_first;
190  trace->range_last = vnb->ip.reass.range_last;
193  trace->range_bi = bi;
194 }
195 
196 static u8 *
197 format_ip6_reass_range_trace (u8 * s, va_list * args)
198 {
200  s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
201  trace->range_last, trace->data_offset, trace->data_len,
202  trace->range_bi);
203  return s;
204 }
205 
206 static u8 *
207 format_ip6_reass_trace (u8 * s, va_list * args)
208 {
209  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
210  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
211  ip6_reass_trace_t *t = va_arg (*args, ip6_reass_trace_t *);
212  s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
213  u32 indent = format_get_indent (s);
214  s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
216  t->fragment_last);
217  switch (t->action)
218  {
219  case RANGE_NEW:
220  s = format (s, "\n%Unew %U", format_white_space, indent,
222  break;
223  case RANGE_OVERLAP:
224  s = format (s, "\n%Uoverlap %U", format_white_space, indent,
226  break;
228  s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
230  &t->trace_range);
231  break;
233  s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
235  &t->trace_range);
236  break;
238  s = format (s, "\n%Uicmp-error - reassembly time exceeded",
239  format_white_space, indent);
240  break;
241  case FINALIZE:
242  s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
243  break;
244  }
245  return s;
246 }
247 
248 static void
250  ip6_reass_main_t * rm, ip6_reass_t * reass,
251  u32 bi, ip6_reass_trace_operation_e action,
252  u32 size_diff)
253 {
254  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
257  {
258  // this buffer's trace is gone
259  b->flags &= ~VLIB_BUFFER_IS_TRACED;
260  return;
261  }
262  ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
263  t->reass_id = reass->id;
264  t->action = action;
265  ip6_reass_trace_details (vm, bi, &t->trace_range);
266  t->size_diff = size_diff;
267  t->op_id = reass->trace_op_counter;
268  ++reass->trace_op_counter;
269  t->fragment_first = vnb->ip.reass.fragment_first;
270  t->fragment_last = vnb->ip.reass.fragment_last;
271  t->trace_range.first_bi = reass->first_bi;
272  t->total_data_len = reass->data_len;
273 #if 0
274  static u8 *s = NULL;
275  s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t);
276  printf ("%.*s\n", vec_len (s), s);
277  fflush (stdout);
278  vec_reset_length (s);
279 #endif
280 }
281 
282 always_inline void
284  ip6_reass_t * reass)
285 {
287  kv.key[0] = reass->key.as_u64[0];
288  kv.key[1] = reass->key.as_u64[1];
289  kv.key[2] = reass->key.as_u64[2];
290  kv.key[3] = reass->key.as_u64[3];
291  kv.key[4] = reass->key.as_u64[4];
292  kv.key[5] = reass->key.as_u64[5];
293  clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
294  pool_put (rt->pool, reass);
295  --rt->reass_n;
296 }
297 
298 always_inline void
300  ip6_reass_t * reass, u32 ** vec_drop_bi)
301 {
302  u32 range_bi = reass->first_bi;
303  vlib_buffer_t *range_b;
304  vnet_buffer_opaque_t *range_vnb;
305  while (~0 != range_bi)
306  {
307  range_b = vlib_get_buffer (vm, range_bi);
308  range_vnb = vnet_buffer (range_b);
309  u32 bi = range_bi;
310  while (~0 != bi)
311  {
312  vec_add1 (*vec_drop_bi, bi);
313  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
314  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
315  {
316  bi = b->next_buffer;
317  b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
318  }
319  else
320  {
321  bi = ~0;
322  }
323  }
324  range_bi = range_vnb->ip.reass.next_range_bi;
325  }
326 }
327 
328 always_inline void
330  ip6_reass_main_t * rm, ip6_reass_t * reass,
331  u32 * icmp_bi, u32 ** vec_timeout)
332 {
333  if (~0 == reass->first_bi)
334  {
335  return;
336  }
337  vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
338  if (0 == vnet_buffer (b)->ip.reass.fragment_first)
339  {
340  *icmp_bi = reass->first_bi;
341  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
342  {
343  ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi,
345  }
346  // fragment with offset zero received - send icmp message back
347  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
348  {
349  // separate first buffer from chain and steer it towards icmp node
350  b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
351  reass->first_bi = b->next_buffer;
352  }
353  else
354  {
355  reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
356  }
357  icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
358  ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
359  0);
360  }
361  ip6_reass_drop_all (vm, rm, reass, vec_timeout);
362 }
363 
367  ip6_reass_key_t * k, u32 * icmp_bi,
368  u32 ** vec_timeout)
369 {
370  ip6_reass_t *reass = NULL;
371  f64 now = vlib_time_now (rm->vlib_main);
372  clib_bihash_kv_48_8_t kv, value;
373  kv.key[0] = k->as_u64[0];
374  kv.key[1] = k->as_u64[1];
375  kv.key[2] = k->as_u64[2];
376  kv.key[3] = k->as_u64[3];
377  kv.key[4] = k->as_u64[4];
378  kv.key[5] = k->as_u64[5];
379 
380  if (!clib_bihash_search_48_8 (&rm->hash, &kv, &value))
381  {
382  reass = pool_elt_at_index (rt->pool, value.value);
383  if (now > reass->last_heard + rm->timeout)
384  {
385  ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi, vec_timeout);
386  ip6_reass_free (rm, rt, reass);
387  reass = NULL;
388  }
389  }
390 
391  if (reass)
392  {
393  reass->last_heard = now;
394  return reass;
395  }
396 
397  if (rt->reass_n >= rm->max_reass_n)
398  {
399  reass = NULL;
400  return reass;
401  }
402  else
403  {
404  pool_get (rt->pool, reass);
405  memset (reass, 0, sizeof (*reass));
406  reass->id =
407  ((u64) os_get_thread_index () * 1000000000) + rt->id_counter;
408  ++rt->id_counter;
409  reass->first_bi = ~0;
410  reass->last_packet_octet = ~0;
411  reass->data_len = 0;
412  ++rt->reass_n;
413  }
414 
415  reass->key.as_u64[0] = kv.key[0] = k->as_u64[0];
416  reass->key.as_u64[1] = kv.key[1] = k->as_u64[1];
417  reass->key.as_u64[2] = kv.key[2] = k->as_u64[2];
418  reass->key.as_u64[3] = kv.key[3] = k->as_u64[3];
419  reass->key.as_u64[4] = kv.key[4] = k->as_u64[4];
420  reass->key.as_u64[5] = kv.key[5] = k->as_u64[5];
421  kv.value = reass - rt->pool;
422  reass->last_heard = now;
423 
424  if (clib_bihash_add_del_48_8 (&rm->hash, &kv, 1))
425  {
426  ip6_reass_free (rm, rt, reass);
427  reass = NULL;
428  }
429 
430  return reass;
431 }
432 
433 always_inline void
436  ip6_reass_t * reass, u32 * bi0, u32 * next0,
437  u32 * error0, u32 ** vec_drop_compress, bool is_feature)
438 {
439  ASSERT (~0 != reass->first_bi);
440  *bi0 = reass->first_bi;
441  *error0 = IP6_ERROR_NONE;
442  ip6_frag_hdr_t *frag_hdr;
443  vlib_buffer_t *last_b = NULL;
444  u32 sub_chain_bi = reass->first_bi;
445  u32 total_length = 0;
446  u32 buf_cnt = 0;
447  u32 dropped_cnt = 0;
448  do
449  {
450  u32 tmp_bi = sub_chain_bi;
451  vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
452  u32 data_len = ip6_reass_buffer_get_data_len (tmp);
453  u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
454  sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp);
455  u32 trim_end =
456  vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
457  if (tmp_bi == reass->first_bi)
458  {
459  /* first buffer - keep ip6 header */
460  ASSERT (0 == ip6_reass_buffer_get_data_offset (tmp));
461  trim_front = 0;
462  trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
463  (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
464  sizeof (*frag_hdr));
465  ASSERT (vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0);
466  }
467  u32 keep_data =
468  vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
469  while (1)
470  {
471  ++buf_cnt;
472  if (trim_front)
473  {
474  if (trim_front > tmp->current_length)
475  {
476  /* drop whole buffer */
477  vec_add1 (*vec_drop_compress, tmp_bi);
478  ++dropped_cnt;
479  trim_front -= tmp->current_length;
480  ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT);
481  tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
482  tmp_bi = tmp->next_buffer;
483  tmp = vlib_get_buffer (vm, tmp_bi);
484  continue;
485  }
486  else
487  {
488  vlib_buffer_advance (tmp, trim_front);
489  trim_front = 0;
490  }
491  }
492  if (keep_data)
493  {
494  if (last_b)
495  {
496  last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
497  last_b->next_buffer = tmp_bi;
498  }
499  last_b = tmp;
500  if (keep_data <= tmp->current_length)
501  {
502  tmp->current_length = keep_data;
503  keep_data = 0;
504  }
505  else
506  {
507  keep_data -= tmp->current_length;
508  ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT);
509  }
510  total_length += tmp->current_length;
511  }
512  else
513  {
514  vec_add1 (*vec_drop_compress, tmp_bi);
515  ASSERT (reass->first_bi != tmp_bi);
516  ++dropped_cnt;
517  }
518  if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
519  {
520  tmp_bi = tmp->next_buffer;
521  tmp = vlib_get_buffer (vm, tmp->next_buffer);
522  }
523  else
524  {
525  break;
526  }
527  }
528  sub_chain_bi =
529  vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
530  reass.next_range_bi;
531  }
532  while (~0 != sub_chain_bi);
533 
534  ASSERT (last_b != NULL);
535  last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
536  vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
537  ASSERT (total_length >= first_b->current_length);
538  total_length -= first_b->current_length;
539  first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
540  first_b->total_length_not_including_first_buffer = total_length;
541  // drop fragment header
542  vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
543  ip6_header_t *ip = vlib_buffer_get_current (first_b);
544  u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
545  ip6_ext_header_t *prev_hdr;
546  ip6_ext_header_find_t (ip, prev_hdr, frag_hdr,
547  IP_PROTOCOL_IPV6_FRAGMENTATION);
548  if (prev_hdr)
549  {
550  prev_hdr->next_hdr = frag_hdr->next_hdr;
551  }
552  else
553  {
554  ip->protocol = frag_hdr->next_hdr;
555  }
556  ASSERT ((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset);
557  memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
558  first_b->current_length - ip6_frag_hdr_offset -
559  sizeof (ip6_frag_hdr_t));
560  first_b->current_length -= sizeof (*frag_hdr);
561  ip->payload_length =
562  clib_host_to_net_u16 (total_length + first_b->current_length -
563  sizeof (*ip));
564  vlib_buffer_chain_compress (vm, first_b, vec_drop_compress);
565  if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
566  {
567  ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
568 #if 0
569  // following code does a hexdump of packet fragments to stdout ...
570  do
571  {
572  u32 bi = reass->first_bi;
573  u8 *s = NULL;
574  while (~0 != bi)
575  {
576  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
577  s = format (s, "%u: %U\n", bi, format_hexdump,
579  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
580  {
581  bi = b->next_buffer;
582  }
583  else
584  {
585  break;
586  }
587  }
588  printf ("%.*s\n", vec_len (s), s);
589  fflush (stdout);
590  vec_free (s);
591  }
592  while (0);
593 #endif
594  }
595  if (is_feature)
596  {
597  *next0 = IP6_REASSEMBLY_NEXT_INPUT;
598  }
599  else
600  {
601  *next0 = reass->next_index;
602  }
603  vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
604  ip6_reass_free (rm, rt, reass);
605  reass = NULL;
606 }
607 
610 {
611  u32 len = 0;
612  while (b)
613  {
614  ++len;
615  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
616  {
617  b = vlib_get_buffer (vm, b->next_buffer);
618  }
619  else
620  {
621  break;
622  }
623  }
624  return len;
625 }
626 
627 always_inline void
630  ip6_reass_t * reass, u32 prev_range_bi,
631  u32 new_next_bi)
632 {
633 
634  vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
635  vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
636  if (~0 != prev_range_bi)
637  {
638  vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
639  vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
640  new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
641  prev_vnb->ip.reass.next_range_bi = new_next_bi;
642  }
643  else
644  {
645  if (~0 != reass->first_bi)
646  {
647  new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
648  }
649  reass->first_bi = new_next_bi;
650  }
651  reass->data_len += ip6_reass_buffer_get_data_len (new_next_b);
652  rt->buffers_n += ip6_reass_get_buffer_chain_length (vm, new_next_b);
653 }
654 
655 always_inline void
658  ip6_reass_t * reass, u32 * bi0, u32 * next0,
659  u32 * error0, ip6_frag_hdr_t * frag_hdr,
660  u32 ** vec_drop_overlap, u32 ** vec_drop_compress,
661  bool is_feature)
662 {
663  int consumed = 0;
664  vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
665  vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
666  reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
667  fvnb->ip.reass.ip6_frag_hdr_offset =
668  (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
670  ASSERT (fb->current_length > sizeof (*fip));
671  ASSERT (fvnb->ip.reass.ip6_frag_hdr_offset > 0 &&
672  fvnb->ip.reass.ip6_frag_hdr_offset < fb->current_length);
673  u32 fragment_first = fvnb->ip.reass.fragment_first =
674  ip6_frag_hdr_offset_bytes (frag_hdr);
675  u32 fragment_length =
676  vlib_buffer_length_in_chain (vm, fb) -
677  (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
678  u32 fragment_last = fvnb->ip.reass.fragment_last =
679  fragment_first + fragment_length - 1;
680  int more_fragments = ip6_frag_hdr_more (frag_hdr);
681  u32 candidate_range_bi = reass->first_bi;
682  u32 prev_range_bi = ~0;
683  fvnb->ip.reass.range_first = fragment_first;
684  fvnb->ip.reass.range_last = fragment_last;
685  fvnb->ip.reass.next_range_bi = ~0;
686  if (!more_fragments)
687  {
688  reass->last_packet_octet = fragment_last;
689  }
690  if (~0 == reass->first_bi)
691  {
692  // starting a new reassembly
693  ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
694  *bi0);
695  if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
696  {
697  ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
698  }
699  reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
700  *bi0 = ~0;
701  return;
702  }
703  reass->min_fragment_length =
704  clib_min (clib_net_to_host_u16 (fip->payload_length),
705  fvnb->ip.reass.estimated_mtu);
706  while (~0 != candidate_range_bi)
707  {
708  vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
709  vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
710  if (fragment_first > candidate_vnb->ip.reass.range_last)
711  {
712  // this fragments starts after candidate range
713  prev_range_bi = candidate_range_bi;
714  candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
715  if (candidate_vnb->ip.reass.range_last < fragment_last &&
716  ~0 == candidate_range_bi)
717  {
718  // special case - this fragment falls beyond all known ranges
719  ip6_reass_insert_range_in_chain (vm, rm, rt, reass,
720  prev_range_bi, *bi0);
721  consumed = 1;
722  break;
723  }
724  continue;
725  }
726  if (fragment_last < candidate_vnb->ip.reass.range_first)
727  {
728  // this fragment ends before candidate range without any overlap
729  ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
730  *bi0);
731  consumed = 1;
732  }
733  else if (fragment_first == candidate_vnb->ip.reass.range_first &&
734  fragment_last == candidate_vnb->ip.reass.range_last)
735  {
736  // duplicate fragment - ignore
737  }
738  else
739  {
740  // overlapping fragment - not allowed by RFC 8200
741  ip6_reass_drop_all (vm, rm, reass, vec_drop_overlap);
742  ip6_reass_free (rm, rt, reass);
743  if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
744  {
745  ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP,
746  0);
747  }
748  *next0 = IP6_REASSEMBLY_NEXT_DROP;
749  *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
750  }
751  break;
752  }
753  if (consumed)
754  {
755  if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
756  {
757  ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
758  }
759  }
760  if (~0 != reass->last_packet_octet &&
761  reass->data_len == reass->last_packet_octet + 1)
762  {
763  ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
764  vec_drop_compress, is_feature);
765  }
766  else
767  {
768  if (consumed)
769  {
770  *bi0 = ~0;
771  }
772  else
773  {
774  *next0 = IP6_REASSEMBLY_NEXT_DROP;
775  ;
776  *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
777  }
778  }
779 }
780 
781 always_inline bool
783  vlib_buffer_t * b,
784  ip6_frag_hdr_t * frag_hdr)
785 {
786  ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
787  while (ip6_ext_hdr (tmp->next_hdr))
788  {
789  tmp = ip6_ext_next_header (tmp);
790  }
791  if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
792  {
793  icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
794  ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
795  0);
796  b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
797 
798  return false;
799  }
800  return true;
801 }
802 
803 always_inline bool
805  vlib_node_runtime_t * node,
806  vlib_buffer_t * b,
807  ip6_frag_hdr_t * frag_hdr)
808 {
811  int more_fragments = ip6_frag_hdr_more (frag_hdr);
812  u32 fragment_length =
814  (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
815  if (more_fragments && 0 != fragment_length % 8)
816  {
817  icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
818  ICMP6_parameter_problem_erroneous_header_field,
819  (u8 *) & ip->payload_length - (u8 *) ip);
820  return false;
821  }
822  return true;
823 }
824 
825 always_inline bool
827  vlib_node_runtime_t * node,
828  vlib_buffer_t * b,
829  ip6_frag_hdr_t * frag_hdr)
830 {
832  u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
833  u32 fragment_length =
835  (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
836  if (fragment_first + fragment_length > 65535)
837  {
839  icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
840  ICMP6_parameter_problem_erroneous_header_field,
841  (u8 *) & frag_hdr->fragment_offset_and_more
842  - (u8 *) ip0);
843  return false;
844  }
845  return true;
846 }
847 
850  vlib_node_runtime_t * node,
851  vlib_frame_t * frame, bool is_feature)
852 {
853  u32 *from = vlib_frame_vector_args (frame);
854  u32 n_left_from, n_left_to_next, *to_next, next_index;
857  clib_spinlock_lock (&rt->lock);
858 
859  n_left_from = frame->n_vectors;
860  next_index = node->cached_next_index;
861  static u32 *vec_timeout = NULL; // indexes of buffers which timed out
862  static u32 *vec_drop_overlap = NULL; // indexes of buffers dropped due to overlap
863  static u32 *vec_drop_compress = NULL; // indexes of buffers dropped due to buffer compression
864  while (n_left_from > 0 || vec_len (vec_timeout) > 0 ||
865  vec_len (vec_drop_overlap) > 0 || vec_len (vec_drop_compress) > 0)
866  {
867  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
868 
869  while (vec_len (vec_timeout) > 0 && n_left_to_next > 0)
870  {
871  u32 bi = vec_pop (vec_timeout);
872  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
873  b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
874  to_next[0] = bi;
875  to_next += 1;
876  n_left_to_next -= 1;
877  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
878  n_left_to_next, bi,
880  ASSERT (rt->buffers_n > 0);
881  --rt->buffers_n;
882  }
883 
884  while (vec_len (vec_drop_overlap) > 0 && n_left_to_next > 0)
885  {
886  u32 bi = vec_pop (vec_drop_overlap);
887  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
888  b->error = node->errors[IP6_ERROR_REASS_OVERLAPPING_FRAGMENT];
889  to_next[0] = bi;
890  to_next += 1;
891  n_left_to_next -= 1;
892  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
893  n_left_to_next, bi,
895  ASSERT (rt->buffers_n > 0);
896  --rt->buffers_n;
897  }
898 
899  while (vec_len (vec_drop_compress) > 0 && n_left_to_next > 0)
900  {
901  u32 bi = vec_pop (vec_drop_compress);
902  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
903  b->error = node->errors[IP6_ERROR_NONE];
904  to_next[0] = bi;
905  to_next += 1;
906  n_left_to_next -= 1;
907  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
908  n_left_to_next, bi,
910  ASSERT (rt->buffers_n > 0);
911  --rt->buffers_n;
912  }
913 
914  while (n_left_from > 0 && n_left_to_next > 0)
915  {
916  u32 bi0;
917  vlib_buffer_t *b0;
918  u32 next0;
919  u32 error0 = IP6_ERROR_NONE;
920  u32 icmp_bi = ~0;
921 
922  bi0 = from[0];
923  b0 = vlib_get_buffer (vm, bi0);
924 
926  ip6_frag_hdr_t *frag_hdr = NULL;
927  ip6_ext_header_t *prev_hdr;
928  if (ip6_ext_hdr (ip0->protocol))
929  {
930  ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr,
931  IP_PROTOCOL_IPV6_FRAGMENTATION);
932  }
933  if (!frag_hdr)
934  {
935  // this is a regular packet - no fragmentation
937  goto skip_reass;
938  }
939  if (0 == ip6_frag_hdr_offset (frag_hdr))
940  {
941  // first fragment - verify upper-layer is present
942  if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr))
943  {
945  goto skip_reass;
946  }
947  }
948  if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr)
949  || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0,
950  frag_hdr))
951  {
953  goto skip_reass;
954  }
955  vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
956  (u8 *) frag_hdr - (u8 *) ip0;
957 
958  ip6_reass_key_t k;
959  k.as_u64[0] = ip0->src_address.as_u64[0];
960  k.as_u64[1] = ip0->src_address.as_u64[1];
961  k.as_u64[2] = ip0->dst_address.as_u64[0];
962  k.as_u64[3] = ip0->dst_address.as_u64[1];
963  k.as_u64[4] =
964  (u64) vnet_buffer (b0)->
965  sw_if_index[VLIB_RX] << 32 | frag_hdr->identification;
966  k.as_u64[5] = ip0->protocol;
967  ip6_reass_t *reass =
968  ip6_reass_find_or_create (vm, node, rm, rt, &k, &icmp_bi,
969  &vec_timeout);
970 
971  if (reass)
972  {
973  ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
974  &error0, frag_hdr, &vec_drop_overlap,
975  &vec_drop_compress, is_feature);
976  }
977  else
978  {
979  next0 = IP6_REASSEMBLY_NEXT_DROP;
980  error0 = IP6_ERROR_REASS_LIMIT_REACHED;
981  }
982 
983  b0->error = node->errors[error0];
984 
985  if (~0 != bi0)
986  {
987  skip_reass:
988  to_next[0] = bi0;
989  to_next += 1;
990  n_left_to_next -= 1;
991  if (is_feature && IP6_ERROR_NONE == error0)
992  {
993  vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_RX],
994  &next0, b0);
995  }
996  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
997  n_left_to_next, bi0, next0);
998  }
999 
1000  if (~0 != icmp_bi)
1001  {
1003  to_next[0] = icmp_bi;
1004  to_next += 1;
1005  n_left_to_next -= 1;
1006  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1007  n_left_to_next, icmp_bi,
1008  next0);
1009  }
1010  from += 1;
1011  n_left_from -= 1;
1012  }
1013 
1014  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1015  }
1016 
1017  clib_spinlock_unlock (&rt->lock);
1018  return frame->n_vectors;
1019 }
1020 
1022 #define _(sym, string) string,
1024 #undef _
1025 };
1026 
1027 static uword
1029  vlib_frame_t * frame)
1030 {
1031  return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ );
1032 }
1033 
1034 /* *INDENT-OFF* */
1035 VLIB_REGISTER_NODE (ip6_reass_node, static) = {
1036  .function = ip6_reassembly,
1037  .name = "ip6-reassembly",
1038  .vector_size = sizeof (u32),
1039  .format_trace = format_ip6_reass_trace,
1040  .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1041  .error_strings = ip6_reassembly_error_strings,
1042  .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1043  .next_nodes =
1044  {
1045  [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1046  [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1047  [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1048  },
1049 };
1050 /* *INDENT-ON* */
1051 
1053 
1054 static uword
1056  vlib_node_runtime_t * node, vlib_frame_t * frame)
1057 {
1058  return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ );
1059 }
1060 
1061 /* *INDENT-OFF* */
1063  .function = ip6_reassembly_feature,
1064  .name = "ip6-reassembly-feature",
1065  .vector_size = sizeof (u32),
1066  .format_trace = format_ip6_reass_trace,
1067  .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1068  .error_strings = ip6_reassembly_error_strings,
1069  .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1070  .next_nodes =
1071  {
1072  [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1073  [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1074  [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1075  },
1076 };
1077 /* *INDENT-ON* */
1078 
1080 
1081 /* *INDENT-OFF* */
1083  .arc_name = "ip6-unicast",
1084  .node_name = "ip6-reassembly-feature",
1085  .runs_before = VNET_FEATURES ("ip6-lookup"),
1086  .runs_after = 0,
1087 };
1088 /* *INDENT-ON* */
1089 
1090 static u32
1092 {
1094  u32 nbuckets;
1095  u8 i;
1096 
1097  nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR);
1098 
1099  for (i = 0; i < 31; i++)
1100  if ((1 << i) >= nbuckets)
1101  break;
1102  nbuckets = 1 << i;
1103 
1104  return nbuckets;
1105 }
1106 
1107 typedef enum
1108 {
1111 
1112 typedef struct
1113 {
1114  int failure;
1115  clib_bihash_48_8_t *new_hash;
1117 
1118 static void
1120 {
1121  ip6_rehash_cb_ctx *ctx = _ctx;
1122  if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1123  {
1124  ctx->failure = 1;
1125  }
1126 }
1127 
1128 static void
1129 ip6_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1130  u32 expire_walk_interval_ms)
1131 {
1132  ip6_reass_main.timeout_ms = timeout_ms;
1133  ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1134  ip6_reass_main.max_reass_n = max_reassemblies;
1135  ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1136 }
1137 
1139 ip6_reass_set (u32 timeout_ms, u32 max_reassemblies,
1140  u32 expire_walk_interval_ms)
1141 {
1142  u32 old_nbuckets = ip6_reass_get_nbuckets ();
1143  ip6_reass_set_params (timeout_ms, max_reassemblies,
1144  expire_walk_interval_ms);
1145  vlib_process_signal_event (ip6_reass_main.vlib_main,
1146  ip6_reass_main.ip6_reass_expire_node_idx,
1148  u32 new_nbuckets = ip6_reass_get_nbuckets ();
1149  if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
1150  {
1151  clib_bihash_48_8_t new_hash;
1152  memset (&new_hash, 0, sizeof (new_hash));
1154  ctx.failure = 0;
1155  ctx.new_hash = &new_hash;
1156  clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets,
1157  new_nbuckets * 1024);
1158  clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash,
1159  ip6_rehash_cb, &ctx);
1160  if (ctx.failure)
1161  {
1162  clib_bihash_free_48_8 (&new_hash);
1163  return -1;
1164  }
1165  else
1166  {
1167  clib_bihash_free_48_8 (&ip6_reass_main.hash);
1168  clib_memcpy (&ip6_reass_main.hash, &new_hash,
1169  sizeof (ip6_reass_main.hash));
1170  }
1171  }
1172  return 0;
1173 }
1174 
1176 ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1177  u32 * expire_walk_interval_ms)
1178 {
1179  *timeout_ms = ip6_reass_main.timeout_ms;
1180  *max_reassemblies = ip6_reass_main.max_reass_n;
1181  *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms;
1182  return 0;
1183 }
1184 
1185 static clib_error_t *
1187 {
1189  clib_error_t *error = 0;
1190  u32 nbuckets;
1191  vlib_node_t *node;
1192 
1193  rm->vlib_main = vm;
1194  rm->vnet_main = vnet_get_main ();
1195 
1198  vec_foreach (rt, rm->per_thread_data)
1199  {
1200  clib_spinlock_init (&rt->lock);
1201  pool_alloc (rt->pool, rm->max_reass_n);
1202  }
1203 
1204  node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk");
1205  ASSERT (node);
1206  rm->ip6_reass_expire_node_idx = node->index;
1207 
1211 
1212  nbuckets = ip6_reass_get_nbuckets ();
1213  clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024);
1214 
1215  node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
1216  ASSERT (node);
1217  rm->ip6_drop_idx = node->index;
1218  node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1219  ASSERT (node);
1220  rm->ip6_icmp_error_idx = node->index;
1221 
1222  if ((error = vlib_call_init_function (vm, ip_main_init)))
1223  return error;
1224  ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1225  ip6_reass_node.index);
1226 
1227  return error;
1228 }
1229 
1231 
1232 static uword
1234  vlib_node_runtime_t * node, vlib_frame_t * f)
1235 {
1237  uword event_type, *event_data = 0;
1238 
1239  while (true)
1240  {
1243  / (f64) MSEC_PER_SEC);
1244  event_type = vlib_process_get_events (vm, &event_data);
1245 
1246  switch (event_type)
1247  {
1248  case ~0: /* no events => timeout */
1249  /* nothing to do here */
1250  break;
1252  break;
1253  default:
1254  clib_warning ("BUG: event type 0x%wx", event_type);
1255  break;
1256  }
1257  f64 now = vlib_time_now (vm);
1258 
1259  ip6_reass_t *reass;
1260  u32 *vec_timeout = NULL;
1261  int *pool_indexes_to_free = NULL;
1262 
1263  uword thread_index = 0;
1264  int index;
1265  const uword nthreads = os_get_nthreads ();
1266  u32 *vec_icmp_bi = NULL;
1267  for (thread_index = 0; thread_index < nthreads; ++thread_index)
1268  {
1269  ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1270  clib_spinlock_lock (&rt->lock);
1271 
1272  vec_reset_length (pool_indexes_to_free);
1273  /* *INDENT-OFF* */
1274  pool_foreach_index (index, rt->pool, ({
1275  reass = pool_elt_at_index (rt->pool, index);
1276  if (now > reass->last_heard + rm->timeout)
1277  {
1278  vec_add1 (pool_indexes_to_free, index);
1279  }
1280  }));
1281  /* *INDENT-ON* */
1282  int *i;
1283  /* *INDENT-OFF* */
1284  vec_foreach (i, pool_indexes_to_free)
1285  {
1286  ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1287  u32 icmp_bi = ~0;
1288  u32 before = vec_len (vec_timeout);
1289  vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1290  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1291  {
1293  b->trace_index))
1294  {
1295  /* the trace is gone, don't trace this buffer anymore */
1296  b->flags &= ~VLIB_BUFFER_IS_TRACED;
1297  }
1298  }
1299  ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi, &vec_timeout);
1300  u32 after = vec_len (vec_timeout);
1301  ASSERT (rt->buffers_n >= (after - before));
1302  rt->buffers_n -= (after - before);
1303  if (~0 != icmp_bi)
1304  {
1305  vec_add1 (vec_icmp_bi, icmp_bi);
1306  ASSERT (rt->buffers_n > 0);
1307  --rt->buffers_n;
1308  }
1309  ip6_reass_free (rm, rt, reass);
1310  }
1311  /* *INDENT-ON* */
1312 
1313  clib_spinlock_unlock (&rt->lock);
1314  }
1315 
1316  while (vec_len (vec_timeout) > 0)
1317  {
1319  u32 *to_next = vlib_frame_vector_args (f);
1320  u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
1321  int trace_frame = 0;
1322  while (vec_len (vec_timeout) > 0 && n_left_to_next > 0)
1323  {
1324  u32 bi = vec_pop (vec_timeout);
1325  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1326  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1327  {
1329  b->trace_index))
1330  {
1331  /* the trace is gone, don't trace this buffer anymore */
1332  b->flags &= ~VLIB_BUFFER_IS_TRACED;
1333  }
1334  else
1335  {
1336  trace_frame = 1;
1337  }
1338  }
1339  b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1340  to_next[0] = bi;
1341  ++f->n_vectors;
1342  to_next += 1;
1343  n_left_to_next -= 1;
1344  }
1345  f->flags |= (trace_frame * VLIB_FRAME_TRACE);
1346  vlib_put_frame_to_node (vm, rm->ip6_drop_idx, f);
1347  }
1348 
1349  while (vec_len (vec_icmp_bi) > 0)
1350  {
1351  vlib_frame_t *f =
1353  u32 *to_next = vlib_frame_vector_args (f);
1354  u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
1355  int trace_frame = 0;
1356  while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1357  {
1358  u32 bi = vec_pop (vec_icmp_bi);
1359  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1360  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1361  {
1363  b->trace_index))
1364  {
1365  /* the trace is gone, don't trace this buffer anymore */
1366  b->flags &= ~VLIB_BUFFER_IS_TRACED;
1367  }
1368  else
1369  {
1370  trace_frame = 1;
1371  }
1372  }
1373  b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1374  to_next[0] = bi;
1375  ++f->n_vectors;
1376  to_next += 1;
1377  n_left_to_next -= 1;
1378  }
1379  f->flags |= (trace_frame * VLIB_FRAME_TRACE);
1381  }
1382 
1383  vec_free (pool_indexes_to_free);
1384  vec_free (vec_timeout);
1385  vec_free (vec_icmp_bi);
1386  if (event_data)
1387  {
1388  _vec_len (event_data) = 0;
1389  }
1390  }
1391 
1392  return 0;
1393 }
1394 
1396 
1397 /* *INDENT-OFF* */
1399  .function = ip6_reass_walk_expired,
1400  .format_trace = format_ip6_reass_trace,
1401  .type = VLIB_NODE_TYPE_PROCESS,
1402  .name = "ip6-reassembly-expire-walk",
1403 
1405  .error_strings = ip6_reassembly_error_strings,
1406 
1407 };
1408 /* *INDENT-ON* */
1409 
1410 static u8 *
1411 format_ip6_reass_key (u8 * s, va_list * args)
1412 {
1413  ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *);
1414  s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1416  &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1417  return s;
1418 }
1419 
1420 static u8 *
1421 format_ip6_reass (u8 * s, va_list * args)
1422 {
1423  vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1424  ip6_reass_t *reass = va_arg (*args, ip6_reass_t *);
1425 
1426  s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
1427  "last_packet_octet: %u, trace_op_counter: %u\n",
1428  reass->id, format_ip6_reass_key, &reass->key, reass->first_bi,
1429  reass->data_len, reass->last_packet_octet,
1430  reass->trace_op_counter);
1431  u32 bi = reass->first_bi;
1432  u32 counter = 0;
1433  while (~0 != bi)
1434  {
1435  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1436  vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1437  s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1438  "fragment[%u, %u]\n",
1439  counter, vnb->ip.reass.range_first,
1440  vnb->ip.reass.range_last, bi,
1443  vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1444  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1445  {
1446  bi = b->next_buffer;
1447  }
1448  else
1449  {
1450  bi = ~0;
1451  }
1452  }
1453  return s;
1454 }
1455 
1456 static clib_error_t *
1459 {
1461 
1462  vlib_cli_output (vm, "---------------------");
1463  vlib_cli_output (vm, "IP6 reassembly status");
1464  vlib_cli_output (vm, "---------------------");
1465  bool details = false;
1466  if (unformat (input, "details"))
1467  {
1468  details = true;
1469  }
1470 
1471  u32 sum_reass_n = 0;
1472  u64 sum_buffers_n = 0;
1473  ip6_reass_t *reass;
1474  uword thread_index;
1475  const uword nthreads = os_get_nthreads ();
1476  for (thread_index = 0; thread_index < nthreads; ++thread_index)
1477  {
1478  ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1479  clib_spinlock_lock (&rt->lock);
1480  if (details)
1481  {
1482  /* *INDENT-OFF* */
1483  pool_foreach (reass, rt->pool, {
1484  vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass);
1485  });
1486  /* *INDENT-ON* */
1487  }
1488  sum_reass_n += rt->reass_n;
1489  sum_buffers_n += rt->buffers_n;
1490  clib_spinlock_unlock (&rt->lock);
1491  }
1492  vlib_cli_output (vm, "---------------------");
1493  vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1494  (long unsigned) sum_reass_n);
1495  vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1496  "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
1497  vlib_cli_output (vm, "Buffers in use: %lu\n",
1498  (long unsigned) sum_buffers_n);
1499  return 0;
1500 }
1501 
1502 /* *INDENT-OFF* */
1504  .path = "show ip6-reassembly",
1505  .short_help = "show ip6-reassembly [details]",
1506  .function = show_ip6_reass,
1507 };
1508 /* *INDENT-ON* */
1509 
1511 ip6_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1512 {
1513  return vnet_feature_enable_disable ("ip6-unicast", "ip6-reassembly-feature",
1514  sw_if_index, enable_disable, 0, 0);
1515 }
1516 
1517 /*
1518  * fd.io coding-style-patch-verification: ON
1519  *
1520  * Local Variables:
1521  * eval: (c-set-style "gnu")
1522  * End:
1523  */
#define IP6_REASS_TIMEOUT_DEFAULT_MS
static u32 ip6_reass_buffer_get_data_offset_no_check(vlib_buffer_t *b)
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:437
vnet_api_error_t
Definition: api_errno.h:147
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:289
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:79
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:699
vnet_main_t * vnet_main
static uword ip6_reassembly(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
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_key_t *k, u32 *icmp_bi, u32 **vec_timeout)
void ip6_register_protocol(u32 protocol, u32 node_index)
Definition: ip6_forward.c:1426
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:329
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
static void vlib_buffer_chain_compress(vlib_main_t *vm, vlib_buffer_t *first, u32 **discard_vector)
compress buffer chain in a way where the first buffer is at least VLIB_BUFFER_CHAIN_MIN_FIRST_DATA_SI...
#define NULL
Definition: clib.h:55
u32 index
Definition: node.h:273
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:225
static clib_error_t * ip6_reass_init_function(vlib_main_t *vm)
static vlib_node_registration_t ip6_reass_expire_node
(constructor) VLIB_REGISTER_NODE (ip6_reass_expire_node)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
ip6_reass_trace_operation_e action
static void * ip6_ext_next_header(ip6_ext_header_t *ext_hdr)
Definition: ip6_packet.h:494
int i
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
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:451
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:250
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:228
ip6_address_t src_address
Definition: ip6_packet.h:347
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
VLIB_NODE_FUNCTION_MULTIARCH(ip6_reass_node, ip6_reassembly)
#define vec_pop(V)
Returns last element of a vector and decrements its length.
Definition: vec.h:617
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:443
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:156
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:542
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:92
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:182
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
#define vlib_call_init_function(vm, x)
Definition: init.h:227
#define VLIB_FRAME_SIZE
Definition: node.h:364
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 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:548
ip6_reass_key_t key
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:464
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:108
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:952
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:191
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:202
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:274
#define PREDICT_FALSE(x)
Definition: clib.h:105
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)
static_always_inline void vnet_feature_next(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:237
#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:364
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:135
VNET_FEATURE_INIT(ip6_reassembly_feature, static)
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:153
clib_error_t * ip_main_init(vlib_main_t *vm)
Definition: ip_init.c:45
u16 n_vectors
Definition: node.h:380
static 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:99
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
#define clib_warning(format, args...)
Definition: error.h:59
#define clib_memcpy(a, b, c)
Definition: string.h:75
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:271
#define ip6_frag_hdr_offset_bytes(hdr)
Definition: ip6_packet.h:545
#define ARRAY_LEN(x)
Definition: clib.h:59
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:454
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
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, u32 **vec_timeout)
static u8 ip6_ext_hdr(u8 nexthdr)
Definition: ip6_packet.h:478
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
signed int i32
Definition: types.h:81
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:542
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:492
static uword ip6_reassembly_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, bool is_feature)
static vlib_node_registration_t ip6_reass_node
(constructor) VLIB_REGISTER_NODE (ip6_reass_node)
#define ASSERT(truth)
long ctx[MAX_CONNS]
Definition: main.c:126
static void ip6_reass_drop_all(vlib_main_t *vm, ip6_reass_main_t *rm, ip6_reass_t *reass, u32 **vec_drop_bi)
u16 flags
Definition: node.h:371
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:126
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:215
vlib_trace_main_t trace_main
Definition: main.h:138
struct vnet_buffer_opaque_t::@59::@61 ip
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:391
static void 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, u32 **vec_drop_overlap, u32 **vec_drop_compress, bool is_feature)
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)
u32 last_packet_octet
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:55
static u8 * format_ip6_reass(u8 *s, va_list *args)
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:152
struct _vlib_node_registration vlib_node_registration_t
u16 payload_length
Definition: ip6_packet.h:338
static u32 ip6_reass_get_buffer_chain_length(vlib_main_t *vm, vlib_buffer_t *b)
#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)
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:267
ip6_reass_trace_operation_e
uword os_get_nthreads(void)
Definition: unix-misc.c:226
#define VLIB_FRAME_TRACE
Definition: node.h:415
static_always_inline uword os_get_thread_index(void)
Definition: os.h:62
#define foreach_ip6_error
Definition: ip6_error.h:43
#define IP6_REASS_MAX_REASSEMBLIES_DEFAULT
static u16 ip6_reass_buffer_get_data_len_no_check(vlib_buffer_t *b)
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:360
clib_bihash_48_8_t * new_hash
static u32 vlib_num_workers()
Definition: threads.h:375
static uword ip6_reassembly_feature(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
#define vec_foreach(var, vec)
Vector iterator.
vnet_api_error_t ip6_reass_enable_disable(u32 sw_if_index, u8 enable_disable)
#define ip6_ext_header_find_t(i, p, m, t)
Definition: ip6_packet.h:503
static void ip6_reass_set_params(u32 timeout_ms, u32 max_reassemblies, u32 expire_walk_interval_ms)
static void 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, u32 **vec_drop_compress, bool is_feature)
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
#define pool_foreach_index(i, v, body)
Iterate pool by index.
Definition: pool.h:488
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:111
ip6_address_t src
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:681
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
u32 trace_index
Specifies index into trace buffer if VLIB_PACKET_IS_TRACED flag is set.
Definition: buffer.h:147
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:233
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
ip6_address_t dst_address
Definition: ip6_packet.h:347