FD.io VPP  v17.07.01-10-g3be13f0
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  * node.c - ipfix probe graph node
3  *
4  * Copyright (c) 2017 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vppinfra/crc32.h>
21 #include <vppinfra/error.h>
22 #include <flowprobe/flowprobe.h>
23 #include <vnet/ip/ip6_packet.h>
24 
26 
27 /**
28  * @file flow record generator graph node
29  */
30 
31 typedef struct
32 {
33  /** interface handle */
36  /** packet timestamp */
38  /** size of the buffer */
40 
41  /** L2 information */
42  u8 src_mac[6];
43  u8 dst_mac[6];
44  /** Ethertype */
46 
47  /** L3 information */
48  ip46_address_t src_address;
49  ip46_address_t dst_address;
52 
53  /** L4 information */
56 
59 
60 static char *flowprobe_variant_strings[] = {
61  [FLOW_VARIANT_IP4] = "IP4",
62  [FLOW_VARIANT_IP6] = "IP6",
63  [FLOW_VARIANT_L2] = "L2",
64  [FLOW_VARIANT_L2_IP4] = "L2-IP4",
65  [FLOW_VARIANT_L2_IP6] = "L2-IP6",
66 };
67 
68 /* packet trace format function */
69 static u8 *
70 format_flowprobe_trace (u8 * s, va_list * args)
71 {
72  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74  flowprobe_trace_t *t = va_arg (*args, flowprobe_trace_t *);
75  uword indent = format_get_indent (s);
76 
77  s = format (s,
78  "FLOWPROBE[%s]: rx_sw_if_index %d, tx_sw_if_index %d, "
79  "timestamp %lld, size %d", flowprobe_variant_strings[t->which],
81  t->timestamp, t->buffer_size);
82 
83  if (t->which == FLOW_VARIANT_L2)
84  s = format (s, "\n%U -> %U", format_white_space, indent,
87 
88  if (t->protocol > 0
91  s =
92  format (s, "\n%U%U: %U -> %U", format_white_space, indent,
96  return s;
97 }
98 
102 
103 /* No counters at the moment */
104 #define foreach_flowprobe_error \
105 _(COLLISION, "Hash table collisions") \
106 _(BUFFER, "Buffer allocation error") \
107 _(EXPORTED_PACKETS, "Exported packets") \
108 _(INPATH, "Exported packets in path")
109 
110 typedef enum
111 {
112 #define _(sym,str) FLOWPROBE_ERROR_##sym,
114 #undef _
117 
118 static char *flowprobe_error_strings[] = {
119 #define _(sym,string) string,
121 #undef _
122 };
123 
124 typedef enum
125 {
130 
131 #define FLOWPROBE_NEXT_NODES { \
132  [FLOWPROBE_NEXT_DROP] = "error-drop", \
133  [FLOWPROBE_NEXT_IP4_LOOKUP] = "ip4-lookup", \
134 }
135 
136 static inline flowprobe_variant_t
138  flowprobe_record_t flags, u16 ethertype)
139 {
140  if (which == FLOW_VARIANT_L2
141  && (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4))
142  return ethertype == ETHERNET_TYPE_IP6 ? FLOW_VARIANT_L2_IP6 : ethertype ==
143  ETHERNET_TYPE_IP4 ? FLOW_VARIANT_L2_IP4 : FLOW_VARIANT_L2;
144  return which;
145 }
146 
147 static inline u32
149 {
150  u16 start = offset;
151 
152  /* Ingress interface */
153  u32 rx_if = clib_host_to_net_u32 (e->key.rx_sw_if_index);
154  clib_memcpy (to_b->data + offset, &rx_if, sizeof (rx_if));
155  offset += sizeof (rx_if);
156 
157  /* Egress interface */
158  u32 tx_if = clib_host_to_net_u32 (e->key.tx_sw_if_index);
159  clib_memcpy (to_b->data + offset, &tx_if, sizeof (tx_if));
160  offset += sizeof (tx_if);
161 
162  /* packet delta count */
163  u64 packetdelta = clib_host_to_net_u64 (e->packetcount);
164  clib_memcpy (to_b->data + offset, &packetdelta, sizeof (u64));
165  offset += sizeof (u64);
166 
167  return offset - start;
168 }
169 
170 static inline u32
172 {
173  u16 start = offset;
174 
175  /* src mac address */
176  clib_memcpy (to_b->data + offset, &e->key.src_mac, 6);
177  offset += 6;
178 
179  /* dst mac address */
180  clib_memcpy (to_b->data + offset, &e->key.dst_mac, 6);
181  offset += 6;
182 
183  /* ethertype */
184  clib_memcpy (to_b->data + offset, &e->key.ethertype, 2);
185  offset += 2;
186 
187  return offset - start;
188 }
189 
190 static inline u32
192 {
193  u16 start = offset;
194 
195  /* ip6 src address */
196  clib_memcpy (to_b->data + offset, &e->key.src_address,
197  sizeof (ip6_address_t));
198  offset += sizeof (ip6_address_t);
199 
200  /* ip6 dst address */
201  clib_memcpy (to_b->data + offset, &e->key.dst_address,
202  sizeof (ip6_address_t));
203  offset += sizeof (ip6_address_t);
204 
205  /* Protocol */
206  to_b->data[offset++] = e->key.protocol;
207 
208  /* octetDeltaCount */
209  u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
210  clib_memcpy (to_b->data + offset, &octetdelta, sizeof (u64));
211  offset += sizeof (u64);
212 
213  return offset - start;
214 }
215 
216 static inline u32
218 {
219  u16 start = offset;
220 
221  /* ip4 src address */
222  clib_memcpy (to_b->data + offset, &e->key.src_address.ip4,
223  sizeof (ip4_address_t));
224  offset += sizeof (ip4_address_t);
225 
226  /* ip4 dst address */
227  clib_memcpy (to_b->data + offset, &e->key.dst_address.ip4,
228  sizeof (ip4_address_t));
229  offset += sizeof (ip4_address_t);
230 
231  /* Protocol */
232  to_b->data[offset++] = e->key.protocol;
233 
234  /* octetDeltaCount */
235  u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
236  clib_memcpy (to_b->data + offset, &octetdelta, sizeof (u64));
237  offset += sizeof (u64);
238 
239  return offset - start;
240 }
241 
242 static inline u32
244 {
245  u16 start = offset;
246 
247  /* src port */
248  clib_memcpy (to_b->data + offset, &e->key.src_port, 2);
249  offset += 2;
250 
251  /* dst port */
252  clib_memcpy (to_b->data + offset, &e->key.dst_port, 2);
253  offset += 2;
254 
255  return offset - start;
256 }
257 
258 static inline u32
259 flowprobe_hash (flowprobe_key_t * k)
260 {
262  u32 h = 0;
263 
264 #ifdef clib_crc32c_uses_intrinsics
265  h = clib_crc32c ((u8 *) k->as_u32, FLOWPROBE_KEY_IN_U32);
266 #else
267  u64 tmp =
268  k->as_u32[0] ^ k->as_u32[1] ^ k->as_u32[2] ^ k->as_u32[3] ^ k->as_u32[4];
269  h = clib_xxhash (tmp);
270 #endif
271 
272  return h >> (32 - fm->ht_log2len);
273 }
274 
276 flowprobe_lookup (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex,
277  bool * collision)
278 {
281  u32 h;
282 
283  h = (fm->active_timer) ? flowprobe_hash (k) : 0;
284 
285  /* Lookup in the flow state pool */
286  *poolindex = fm->hash_per_worker[my_cpu_number][h];
287  if (*poolindex != ~0)
288  {
289  e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], *poolindex);
290  if (e)
291  {
292  /* Verify key or report collision */
293  if (memcmp (k, &e->key, sizeof (flowprobe_key_t)))
294  *collision = true;
295  return e;
296  }
297  }
298 
299  return 0;
300 }
301 
303 flowprobe_create (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex)
304 {
306  u32 h;
307 
309 
310  /* Get my index */
311  h = (fm->active_timer) ? flowprobe_hash (k) : 0;
312 
313  pool_get (fm->pool_per_worker[my_cpu_number], e);
314  *poolindex = e - fm->pool_per_worker[my_cpu_number];
315  fm->hash_per_worker[my_cpu_number][h] = *poolindex;
316 
317  e->key = *k;
318 
319  if (fm->passive_timer > 0)
320  {
321  e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
322  (fm->timers_per_worker[my_cpu_number], *poolindex, 0,
323  fm->passive_timer);
324  }
325  return e;
326 }
327 
328 static inline void
331  u64 timestamp, u16 length,
333 {
334  if (fm->disabled)
335  return;
336 
337  u32 my_cpu_number = vm->thread_index;
338  u16 octets = 0;
339 
340  flowprobe_record_t flags = fm->context[which].flags;
341  bool collect_ip4 = false, collect_ip6 = false;
342  ASSERT (b);
344  u16 ethertype = clib_net_to_host_u16 (eth->type);
345  /* *INDENT-OFF* */
346  flowprobe_key_t k = { {0} };
347  /* *INDENT-ON* */
348  ip4_header_t *ip4 = 0;
349  ip6_header_t *ip6 = 0;
350  udp_header_t *udp = 0;
351 
352  if (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4)
353  {
354  collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
355  collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
356  }
357 
358  k.rx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
359  k.tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
360 
361  k.which = which;
362 
363  if (flags & FLOW_RECORD_L2)
364  {
365  clib_memcpy (k.src_mac, eth->src_address, 6);
366  clib_memcpy (k.dst_mac, eth->dst_address, 6);
367  k.ethertype = ethertype;
368  }
369  if (collect_ip6 && ethertype == ETHERNET_TYPE_IP6)
370  {
371  ip6 = (ip6_header_t *) (eth + 1);
372  udp = (udp_header_t *) (ip6 + 1);
373  if (flags & FLOW_RECORD_L3)
374  {
375  k.src_address.as_u64[0] = ip6->src_address.as_u64[0];
376  k.src_address.as_u64[1] = ip6->src_address.as_u64[1];
377  k.dst_address.as_u64[0] = ip6->dst_address.as_u64[0];
378  k.dst_address.as_u64[1] = ip6->dst_address.as_u64[1];
379  }
380  k.protocol = ip6->protocol;
381  octets = clib_net_to_host_u16 (ip6->payload_length)
382  + sizeof (ip6_header_t);
383  }
384  if (collect_ip4 && ethertype == ETHERNET_TYPE_IP4)
385  {
386  ip4 = (ip4_header_t *) (eth + 1);
387  udp = (udp_header_t *) (ip4 + 1);
388  if (flags & FLOW_RECORD_L3)
389  {
390  k.src_address.ip4.as_u32 = ip4->src_address.as_u32;
391  k.dst_address.ip4.as_u32 = ip4->dst_address.as_u32;
392  }
393  k.protocol = ip4->protocol;
394  octets = clib_net_to_host_u16 (ip4->length);
395  }
396  if ((flags & FLOW_RECORD_L4) && udp &&
397  (k.protocol == IP_PROTOCOL_TCP || k.protocol == IP_PROTOCOL_UDP))
398  {
399  k.src_port = udp->src_port;
400  k.dst_port = udp->dst_port;
401  }
402 
403  if (t)
404  {
405  t->rx_sw_if_index = k.rx_sw_if_index;
406  t->tx_sw_if_index = k.tx_sw_if_index;
407  clib_memcpy (t->src_mac, k.src_mac, 6);
408  clib_memcpy (t->dst_mac, k.dst_mac, 6);
409  t->ethertype = k.ethertype;
410  t->src_address.ip4.as_u32 = k.src_address.ip4.as_u32;
411  t->dst_address.ip4.as_u32 = k.dst_address.ip4.as_u32;
412  t->protocol = k.protocol;
413  t->src_port = k.src_port;
414  t->dst_port = k.dst_port;
415  t->which = k.which;
416  }
417 
418  flowprobe_entry_t *e = 0;
419  f64 now = vlib_time_now (vm);
420  if (fm->active_timer > 0)
421  {
422  u32 poolindex = ~0;
423  bool collision = false;
424 
425  e = flowprobe_lookup (my_cpu_number, &k, &poolindex, &collision);
426  if (collision)
427  {
428  /* Flush data and clean up entry for reuse. */
429  if (e->packetcount)
430  flowprobe_export_entry (vm, e);
431  e->key = k;
433  FLOWPROBE_ERROR_COLLISION, 1);
434  }
435  if (!e) /* Create new entry */
436  {
437  e = flowprobe_create (my_cpu_number, &k, &poolindex);
438  e->last_exported = now;
439  }
440  }
441  else
442  {
443  e = &fm->stateless_entry[my_cpu_number];
444  e->key = k;
445  }
446 
447  if (e)
448  {
449  /* Updating entry */
450  e->packetcount++;
451  e->octetcount += octets;
452  e->last_updated = now;
453 
454  if (fm->active_timer == 0
455  || (now > e->last_exported + fm->active_timer))
456  flowprobe_export_entry (vm, e);
457  }
458 }
459 
460 static u16
462 {
463  return sizeof (ip4_header_t) + sizeof (udp_header_t) +
464  sizeof (ipfix_message_header_t) + sizeof (ipfix_set_header_t);
465 }
466 
467 static void
469  flowprobe_variant_t which)
470 {
473  vlib_frame_t *f;
477  ip4_header_t *ip;
478  udp_header_t *udp;
479  flowprobe_record_t flags = fm->context[which].flags;
480  u32 my_cpu_number = vm->thread_index;
481 
482  /* Fill in header */
483  flow_report_stream_t *stream;
484 
485  /* Nothing to send */
486  if (fm->context[which].next_record_offset_per_worker[my_cpu_number] <=
488  return;
489 
490  u32 i, index = vec_len (frm->streams);
491  for (i = 0; i < index; i++)
492  if (frm->streams[i].domain_id == 1)
493  {
494  index = i;
495  break;
496  }
497  if (i == vec_len (frm->streams))
498  {
499  vec_validate (frm->streams, index);
500  frm->streams[index].domain_id = 1;
501  }
502  stream = &frm->streams[index];
503 
504  tp = vlib_buffer_get_current (b0);
505  ip = (ip4_header_t *) & tp->ip4;
506  udp = (udp_header_t *) (ip + 1);
507  h = (ipfix_message_header_t *) (udp + 1);
508  s = (ipfix_set_header_t *) (h + 1);
509 
510  ip->ip_version_and_header_length = 0x45;
511  ip->ttl = 254;
512  ip->protocol = IP_PROTOCOL_UDP;
516  udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipfix);
517  udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipfix);
518  udp->checksum = 0;
519 
520  /* FIXUP: message header export_time */
521  h->export_time = (u32)
522  (((f64) frm->unix_time_0) +
523  (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
524  h->export_time = clib_host_to_net_u32 (h->export_time);
525  h->domain_id = clib_host_to_net_u32 (stream->domain_id);
526 
527  /* FIXUP: message header sequence_number */
528  h->sequence_number = stream->sequence_number++;
529  h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
530 
532  b0->current_length -
533  (sizeof (*ip) + sizeof (*udp) +
534  sizeof (*h)));
536  (sizeof (*ip) + sizeof (*udp)));
537 
538  ip->length = clib_host_to_net_u16 (b0->current_length);
539 
540  ip->checksum = ip4_header_checksum (ip);
541  udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
542 
543  if (frm->udp_checksum)
544  {
545  /* RFC 7011 section 10.3.2. */
546  udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
547  if (udp->checksum == 0)
548  udp->checksum = 0xffff;
549  }
550 
551  ASSERT (ip->checksum == ip4_header_checksum (ip));
552 
553  /* Find or allocate a frame */
554  f = fm->context[which].frames_per_worker[my_cpu_number];
555  if (PREDICT_FALSE (f == 0))
556  {
557  u32 *to_next;
558  f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
559  fm->context[which].frames_per_worker[my_cpu_number] = f;
560  u32 bi0 = vlib_get_buffer_index (vm, b0);
561 
562  /* Enqueue the buffer */
563  to_next = vlib_frame_vector_args (f);
564  to_next[0] = bi0;
565  f->n_vectors = 1;
566  }
567 
570  FLOWPROBE_ERROR_EXPORTED_PACKETS, 1);
571 
572  fm->context[which].frames_per_worker[my_cpu_number] = 0;
573  fm->context[which].buffers_per_worker[my_cpu_number] = 0;
574  fm->context[which].next_record_offset_per_worker[my_cpu_number] =
576 }
577 
578 static vlib_buffer_t *
580 {
583  vlib_buffer_t *b0;
584  u32 bi0;
586  u32 my_cpu_number = vm->thread_index;
587 
588  /* Find or allocate a buffer */
589  b0 = fm->context[which].buffers_per_worker[my_cpu_number];
590 
591  /* Need to allocate a buffer? */
592  if (PREDICT_FALSE (b0 == 0))
593  {
594  if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
595  {
597  FLOWPROBE_ERROR_BUFFER, 1);
598  return 0;
599  }
600 
601  /* Initialize the buffer */
602  b0 = fm->context[which].buffers_per_worker[my_cpu_number] =
603  vlib_get_buffer (vm, bi0);
604  fl =
608 
609  b0->current_data = 0;
612  vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
613  vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
614  fm->context[which].next_record_offset_per_worker[my_cpu_number] =
615  b0->current_length;
616  }
617 
618  return b0;
619 }
620 
621 static void
623 {
624  u32 my_cpu_number = vm->thread_index;
627  vlib_buffer_t *b0;
628  bool collect_ip4 = false, collect_ip6 = false;
629  flowprobe_variant_t which = e->key.which;
630  flowprobe_record_t flags = fm->context[which].flags;
631  u16 offset =
632  fm->context[which].next_record_offset_per_worker[my_cpu_number];
633 
634  if (offset < flowprobe_get_headersize ())
635  offset = flowprobe_get_headersize ();
636 
637  b0 = flowprobe_get_buffer (vm, which);
638  /* No available buffer, what to do... */
639  if (b0 == 0)
640  return;
641 
642  if (flags & FLOW_RECORD_L3)
643  {
644  collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
645  collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
646  }
647 
648  offset += flowprobe_common_add (b0, e, offset);
649 
650  if (flags & FLOW_RECORD_L2)
651  offset += flowprobe_l2_add (b0, e, offset);
652  if (collect_ip6)
653  offset += flowprobe_l3_ip6_add (b0, e, offset);
654  if (collect_ip4)
655  offset += flowprobe_l3_ip4_add (b0, e, offset);
656  if (flags & FLOW_RECORD_L4)
657  offset += flowprobe_l4_add (b0, e, offset);
658 
659  /* Reset per flow-export counters */
660  e->packetcount = 0;
661  e->octetcount = 0;
662  e->last_exported = vlib_time_now (vm);
663 
664  b0->current_length = offset;
665 
666  fm->context[which].next_record_offset_per_worker[my_cpu_number] = offset;
667  /* Time to flush the buffer? */
668  if (offset + fm->template_size[flags] > frm->path_mtu)
669  flowprobe_export_send (vm, b0, which);
670 }
671 
672 uword
674  vlib_node_runtime_t * node, vlib_frame_t * frame,
675  flowprobe_variant_t which)
676 {
677  u32 n_left_from, *from, *to_next;
678  flowprobe_next_t next_index;
680  u64 now;
681 
682  now = (u64) ((vlib_time_now (vm) - fm->vlib_time_0) * 1e9);
683  now += fm->nanosecond_time_0;
684 
685  from = vlib_frame_vector_args (frame);
686  n_left_from = frame->n_vectors;
687  next_index = node->cached_next_index;
688 
689  while (n_left_from > 0)
690  {
691  u32 n_left_to_next;
692 
693  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
694 
695  while (n_left_from >= 4 && n_left_to_next >= 2)
696  {
697  u32 next0 = FLOWPROBE_NEXT_DROP;
698  u32 next1 = FLOWPROBE_NEXT_DROP;
699  u16 len0, len1;
700  u32 bi0, bi1;
701  vlib_buffer_t *b0, *b1;
702 
703  /* Prefetch next iteration. */
704  {
705  vlib_buffer_t *p2, *p3;
706 
707  p2 = vlib_get_buffer (vm, from[2]);
708  p3 = vlib_get_buffer (vm, from[3]);
709 
710  vlib_prefetch_buffer_header (p2, LOAD);
711  vlib_prefetch_buffer_header (p3, LOAD);
712 
715  }
716 
717  /* speculatively enqueue b0 and b1 to the current next frame */
718  to_next[0] = bi0 = from[0];
719  to_next[1] = bi1 = from[1];
720  from += 2;
721  to_next += 2;
722  n_left_from -= 2;
723  n_left_to_next -= 2;
724 
725  b0 = vlib_get_buffer (vm, bi0);
726  b1 = vlib_get_buffer (vm, bi1);
727 
728  vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_TX],
729  &next0, b0);
730  vnet_feature_next (vnet_buffer (b1)->sw_if_index[VLIB_TX],
731  &next1, b1);
732 
733  len0 = vlib_buffer_length_in_chain (vm, b0);
735  u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
736 
737  if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_FLOW_REPORT) == 0))
738  add_to_flow_record_state (vm, node, fm, b0, now, len0,
740  (which, fm->context[which].flags,
741  ethertype0), 0);
742 
743  len1 = vlib_buffer_length_in_chain (vm, b1);
745  u16 ethertype1 = clib_net_to_host_u16 (eh1->type);
746 
747  if (PREDICT_TRUE ((b1->flags & VLIB_BUFFER_FLOW_REPORT) == 0))
748  add_to_flow_record_state (vm, node, fm, b1, now, len1,
750  (which, fm->context[which].flags,
751  ethertype1), 0);
752 
753  /* verify speculative enqueues, maybe switch current next frame */
754  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
755  to_next, n_left_to_next,
756  bi0, bi1, next0, next1);
757  }
758 
759  while (n_left_from > 0 && n_left_to_next > 0)
760  {
761  u32 bi0;
762  vlib_buffer_t *b0;
763  u32 next0 = FLOWPROBE_NEXT_DROP;
764  u16 len0;
765 
766  /* speculatively enqueue b0 to the current next frame */
767  bi0 = from[0];
768  to_next[0] = bi0;
769  from += 1;
770  to_next += 1;
771  n_left_from -= 1;
772  n_left_to_next -= 1;
773 
774  b0 = vlib_get_buffer (vm, bi0);
775 
776  vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_TX],
777  &next0, b0);
778 
779  len0 = vlib_buffer_length_in_chain (vm, b0);
781  u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
782 
783  if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_FLOW_REPORT) == 0))
784  {
785  flowprobe_trace_t *t = 0;
787  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
788  t = vlib_add_trace (vm, node, b0, sizeof (*t));
789 
790  add_to_flow_record_state (vm, node, fm, b0, now, len0,
792  (which, fm->context[which].flags,
793  ethertype0), t);
794  }
795 
796  /* verify speculative enqueue, maybe switch current next frame */
797  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
798  to_next, n_left_to_next,
799  bi0, next0);
800  }
801 
802  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
803  }
804  return frame->n_vectors;
805 }
806 
807 static uword
809  vlib_node_runtime_t * node, vlib_frame_t * frame)
810 {
811  return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP4);
812 }
813 
814 static uword
816  vlib_node_runtime_t * node, vlib_frame_t * frame)
817 {
818  return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP6);
819 }
820 
821 static uword
823  vlib_node_runtime_t * node, vlib_frame_t * frame)
824 {
825  return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_L2);
826 }
827 
828 static inline void
830 {
831  vlib_main_t *vm = vlib_get_main ();
832  vlib_buffer_t *b = flowprobe_get_buffer (vm, which);
833  if (b)
834  flowprobe_export_send (vm, b, which);
835 }
836 
837 void
839 {
841 }
842 
843 void
845 {
847 }
848 
849 void
851 {
855 }
856 
857 
858 static void
859 flowprobe_delete_by_index (u32 my_cpu_number, u32 poolindex)
860 {
863  u32 h;
864 
865  e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], poolindex);
866 
867  /* Get my index */
868  h = flowprobe_hash (&e->key);
869 
870  /* Reset hash */
871  fm->hash_per_worker[my_cpu_number][h] = ~0;
872 
873  pool_put_index (fm->pool_per_worker[my_cpu_number], poolindex);
874 }
875 
876 
877 /* Per worker process processing the active/passive expired entries */
878 static uword
881 {
885 
886  /*
887  * $$$$ Remove this check from here and track FRM status and disable
888  * this process if required.
889  */
890  if (frm->ipfix_collector.as_u32 == 0 || frm->src_address.as_u32 == 0)
891  {
892  fm->disabled = true;
893  return 0;
894  }
895  fm->disabled = false;
896 
897  u32 cpu_index = os_get_thread_index ();
898  u32 *to_be_removed = 0, *i;
899 
900  /*
901  * Tick the timer when required and process the vector of expired
902  * timers
903  */
904  f64 start_time = vlib_time_now (vm);
905  u32 count = 0;
906 
907  tw_timer_expire_timers_2t_1w_2048sl (fm->timers_per_worker[cpu_index],
908  start_time);
909 
910  vec_foreach (i, fm->expired_passive_per_worker[cpu_index])
911  {
912  u32 exported = 0;
913  f64 now = vlib_time_now (vm);
914  if (now > start_time + 100e-6
915  || exported > FLOW_MAXIMUM_EXPORT_ENTRIES - 1)
916  break;
917 
918  if (pool_is_free_index (fm->pool_per_worker[cpu_index], *i))
919  {
920  clib_warning ("Element is %d is freed already\n", *i);
921  continue;
922  }
923  else
924  e = pool_elt_at_index (fm->pool_per_worker[cpu_index], *i);
925 
926  /* Check last update timestamp. If it is longer than passive time nuke
927  * entry. Otherwise restart timer with what's left
928  * Premature passive timer by more than 10%
929  */
930  if ((now - e->last_updated) < (fm->passive_timer * 0.9))
931  {
932  f64 delta = fm->passive_timer - (now - e->last_updated);
933  e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
934  (fm->timers_per_worker[cpu_index], *i, 0, delta);
935  }
936  else /* Nuke entry */
937  {
938  vec_add1 (to_be_removed, *i);
939  }
940  /* If anything to report send it to the exporter */
941  if (e->packetcount && now > e->last_exported + fm->active_timer)
942  {
943  exported++;
944  flowprobe_export_entry (vm, e);
945  }
946  count++;
947  }
948  if (count)
949  vec_delete (fm->expired_passive_per_worker[cpu_index], count, 0);
950 
951  vec_foreach (i, to_be_removed) flowprobe_delete_by_index (cpu_index, *i);
952  vec_free (to_be_removed);
953 
954  return 0;
955 }
956 
957 /* *INDENT-OFF* */
959  .function = flowprobe_ip4_node_fn,
960  .name = "flowprobe-ip4",
961  .vector_size = sizeof (u32),
962  .format_trace = format_flowprobe_trace,
963  .type = VLIB_NODE_TYPE_INTERNAL,
964  .n_errors = ARRAY_LEN(flowprobe_error_strings),
965  .error_strings = flowprobe_error_strings,
966  .n_next_nodes = FLOWPROBE_N_NEXT,
967  .next_nodes = FLOWPROBE_NEXT_NODES,
968 };
970  .function = flowprobe_ip6_node_fn,
971  .name = "flowprobe-ip6",
972  .vector_size = sizeof (u32),
973  .format_trace = format_flowprobe_trace,
974  .type = VLIB_NODE_TYPE_INTERNAL,
975  .n_errors = ARRAY_LEN(flowprobe_error_strings),
976  .error_strings = flowprobe_error_strings,
977  .n_next_nodes = FLOWPROBE_N_NEXT,
978  .next_nodes = FLOWPROBE_NEXT_NODES,
979 };
981  .function = flowprobe_l2_node_fn,
982  .name = "flowprobe-l2",
983  .vector_size = sizeof (u32),
984  .format_trace = format_flowprobe_trace,
985  .type = VLIB_NODE_TYPE_INTERNAL,
986  .n_errors = ARRAY_LEN(flowprobe_error_strings),
987  .error_strings = flowprobe_error_strings,
988  .n_next_nodes = FLOWPROBE_N_NEXT,
989  .next_nodes = FLOWPROBE_NEXT_NODES,
990 };
992  .function = flowprobe_walker_process,
993  .name = "flowprobe-walker",
994  .type = VLIB_NODE_TYPE_INPUT,
995  .state = VLIB_NODE_STATE_INTERRUPT,
996 };
997 /* *INDENT-ON* */
998 
999 /*
1000  * fd.io coding-style-patch-verification: ON
1001  *
1002  * Local Variables:
1003  * eval: (c-set-style "gnu")
1004  * End:
1005  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
flowprobe_protocol_context_t context[FLOW_N_VARIANTS]
Definition: flowprobe.h:118
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:187
format_function_t format_ip_protocol
Definition: format.h:45
flowprobe_variant_t
Definition: flowprobe.h:46
u8 dst_mac[6]
Definition: node.c:43
u16 buffer_size
size of the buffer
Definition: node.c:39
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
u32 ** expired_passive_per_worker
Definition: flowprobe.h:133
#define CLIB_UNUSED(x)
Definition: clib.h:79
ip46_address_t src_address
L3 information.
Definition: node.c:48
#define FLOWPROBE_KEY_IN_U32
Definition: flowprobe.h:75
static u32 flowprobe_l4_add(vlib_buffer_t *to_b, flowprobe_entry_t *e, u16 offset)
Definition: node.c:243
ip4_address_t src_address
Definition: ip4_packet.h:164
u8 src_mac[6]
L2 information.
Definition: node.c:42
vlib_node_registration_t flowprobe_ip6_node
(constructor) VLIB_REGISTER_NODE (flowprobe_ip6_node)
Definition: node.c:100
static char * flowprobe_error_strings[]
Definition: node.c:118
#define PREDICT_TRUE(x)
Definition: clib.h:98
u64 as_u64[2]
Definition: ip6_packet.h:51
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:192
flowprobe_key_t key
Definition: flowprobe.h:101
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:459
u8 src_address[6]
Definition: packet.h:54
static u32 flowprobe_l3_ip6_add(vlib_buffer_t *to_b, flowprobe_entry_t *e, u16 offset)
Definition: node.c:191
u32 thread_index
Definition: main.h:159
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
static u64 clib_xxhash(u64 key)
Definition: xxhash.h:58
struct _vlib_node_registration vlib_node_registration_t
static u32 flowprobe_l3_ip4_add(vlib_buffer_t *to_b, flowprobe_entry_t *e, u16 offset)
Definition: node.c:217
ip4_address_t src_address
Definition: flow_report.h:96
format_function_t format_ip46_address
Definition: format.h:61
flowprobe_entry_t * stateless_entry
Definition: flowprobe.h:138
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
u16 flags_and_fragment_offset
Definition: ip4_packet.h:145
u16 template_reports[FLOW_N_RECORDS]
Definition: flowprobe.h:119
static uword flowprobe_ip6_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:815
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:100
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
ip6_address_t src_address
Definition: ip6_packet.h:341
ip46_address_t dst_address
Definition: node.c:49
static void flush_record(flowprobe_variant_t which)
Definition: node.c:829
ip4_address_t ipfix_collector
Definition: flow_report.h:94
vlib_node_registration_t ip4_lookup_node
(constructor) VLIB_REGISTER_NODE (ip4_lookup_node)
Definition: ip4_forward.c:479
flowprobe_entry_t ** pool_per_worker
Definition: flowprobe.h:129
static u32 flowprobe_hash(flowprobe_key_t *k)
Definition: node.c:259
flow_report_stream_t * streams
Definition: flow_report.h:91
flow-per-packet plugin header file
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:67
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
u64 timestamp
packet timestamp
Definition: node.c:37
static uword format_get_indent(u8 *s)
Definition: format.h:72
ip4_address_t dst_address
Definition: ip4_packet.h:164
u8 dst_address[6]
Definition: packet.h:53
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
vlib_frame_t ** frames_per_worker
frames containing ipfix buffers, per-worker thread
Definition: flowprobe.h:70
u16 ethertype
Ethertype.
Definition: node.c:45
u32 ** hash_per_worker
Definition: flowprobe.h:128
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:89
void flowprobe_flush_callback_ip6(void)
Definition: node.c:844
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:164
unsigned long u64
Definition: types.h:89
vlib_node_registration_t flowprobe_walker_node
(constructor) VLIB_REGISTER_NODE (flowprobe_walker_node)
Definition: node.c:991
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:70
u64 nanosecond_time_0
Time reference pair.
Definition: flowprobe.h:123
flowprobe_error_t
Definition: node.c:110
static uword flowprobe_ip4_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:808
u8 ht_log2len
Per CPU flow-state.
Definition: flowprobe.h:127
flowprobe_next_t
Definition: node.c:124
uword flowprobe_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, flowprobe_variant_t which)
Definition: node.c:673
u32 tx_sw_if_index
Definition: node.c:35
flow_report_main_t flow_report_main
Definition: flow_report.c:21
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:71
#define FLOWPROBE_NEXT_NODES
Definition: node.c:131
static void flowprobe_export_send(vlib_main_t *vm, vlib_buffer_t *b0, flowprobe_variant_t which)
Definition: node.c:468
static u32 flowprobe_common_add(vlib_buffer_t *to_b, flowprobe_entry_t *e, u16 offset)
Definition: node.c:148
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:188
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:402
#define VLIB_BUFFER_FLOW_REPORT
Definition: buffer.h:92
u16 src_port
L4 information.
Definition: node.c:54
#define PREDICT_FALSE(x)
Definition: clib.h:97
static char * flowprobe_variant_strings[]
Definition: node.c:60
u32 passive_timer_handle
Definition: flowprobe.h:106
u32 node_index
Node index.
Definition: node.h:441
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
static_always_inline void vnet_feature_next(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:221
#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:216
static vlib_buffer_t * flowprobe_get_buffer(vlib_main_t *vm, flowprobe_variant_t which)
Definition: node.c:579
#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:366
static u32 version_length(u16 length)
Definition: ipfix_packet.h:31
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1131
static u32 flowprobe_l2_add(vlib_buffer_t *to_b, flowprobe_entry_t *e, u16 offset)
Definition: node.c:171
#define foreach_flowprobe_error
Definition: node.c:104
u16 dst_port
Definition: node.c:55
u16 n_vectors
Definition: node.h:345
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
u64 packetcount
Definition: flowprobe.h:102
f64 last_exported
Definition: flowprobe.h:105
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
#define clib_warning(format, args...)
Definition: error.h:59
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:85
flowprobe_entry_t * flowprobe_lookup(u32 my_cpu_number, flowprobe_key_t *k, u32 *poolindex, bool *collision)
Definition: node.c:276
static u8 * format_flowprobe_trace(u8 *s, va_list *args)
Definition: node.c:70
#define clib_memcpy(a, b, c)
Definition: string.h:69
void flowprobe_flush_callback_l2(void)
Definition: node.c:850
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:238
#define ARRAY_LEN(x)
Definition: clib.h:59
flowprobe_record_t
Definition: flowprobe.h:35
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:460
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:255
#define ASSERT(truth)
u32 rx_sw_if_index
interface handle
Definition: node.c:34
unsigned int u32
Definition: types.h:88
vlib_main_t * vlib_main
Definition: flow_report.h:113
u16 ip4_tcp_udp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip4_header_t *ip0)
Definition: ip4_forward.c:1445
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:785
void flowprobe_flush_callback_ip4(void)
Definition: node.c:838
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:260
#define FLOW_MAXIMUM_EXPORT_ENTRIES
Definition: flowprobe.h:61
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
flowprobe_main_t flowprobe_main
Definition: flowprobe.c:54
u64 uword
Definition: types.h:112
vlib_node_registration_t flowprobe_ip4_node
(constructor) VLIB_REGISTER_NODE (flowprobe_ip4_node)
Definition: node.c:99
static uword flowprobe_walker_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: node.c:879
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
flowprobe_variant_t which
Definition: node.c:57
template key/value backing page structure
Definition: bihash_doc.h:44
static u32 ipfix_set_id_length(u16 set_id, u16 length)
Definition: ipfix_packet.h:114
vlib_buffer_t ** buffers_per_worker
ipfix buffers under construction, per-worker thread
Definition: flowprobe.h:68
Definition: defs.h:47
static void flowprobe_export_entry(vlib_main_t *vm, flowprobe_entry_t *e)
Definition: node.c:622
unsigned short u16
Definition: types.h:57
u16 payload_length
Definition: ip6_packet.h:332
vlib_node_registration_t flowprobe_l2_node
(constructor) VLIB_REGISTER_NODE (flowprobe_l2_node)
Definition: node.c:101
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:496
u16 template_size[FLOW_N_RECORDS]
Definition: flowprobe.h:120
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:269
static_always_inline uword os_get_thread_index(void)
Definition: os.h:62
f64 last_updated
Definition: flowprobe.h:104
struct clib_bihash_value offset
template key/value backing page structure
static void vlib_buffer_init_for_free_list(vlib_buffer_t *dst, vlib_buffer_free_list_t *fl)
Definition: buffer_funcs.h:777
#define vnet_buffer(b)
Definition: buffer.h:304
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:144
u8 data[0]
Packet data.
Definition: buffer.h:152
#define vec_foreach(var, vec)
Vector iterator.
static flowprobe_variant_t flowprobe_get_variant(flowprobe_variant_t which, flowprobe_record_t flags, u16 ethertype)
Definition: node.c:137
u16 flags
Copy of main node flags.
Definition: node.h:454
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:196
u8 ip_version_and_header_length
Definition: ip4_packet.h:132
u16 * next_record_offset_per_worker
next record offset, per worker thread
Definition: flowprobe.h:72
static vlib_buffer_free_list_t * vlib_buffer_get_free_list(vlib_main_t *vm, u32 free_list_index)
Definition: buffer_funcs.h:385
flowprobe_entry_t * flowprobe_create(u32 my_cpu_number, flowprobe_key_t *k, u32 *poolindex)
Definition: node.c:303
u32 flags
Definition: vhost-user.h:76
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:74
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:245
static void add_to_flow_record_state(vlib_main_t *vm, vlib_node_runtime_t *node, flowprobe_main_t *fm, vlib_buffer_t *b, u64 timestamp, u16 length, flowprobe_variant_t which, flowprobe_trace_t *t)
Definition: node.c:329
flowprobe_record_t flags
Definition: flowprobe.h:66
u64 octetcount
Definition: flowprobe.h:103
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
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:239
Definition: flowprobe.h:99
static void flowprobe_delete_by_index(u32 my_cpu_number, u32 poolindex)
Definition: node.c:859
static u16 flowprobe_get_headersize(void)
Definition: node.c:461
Definition: defs.h:46
static uword flowprobe_l2_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:822
ip6_address_t dst_address
Definition: ip6_packet.h:341