2 * node.c - ipfix probe graph node
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/crc32.h>
20 #include <vppinfra/error.h>
21 #include <flowprobe/flowprobe.h>
22 #include <vnet/ip/ip6_packet.h>
23 #include <vnet/udp/udp_local.h>
24 #include <vlibmemory/api.h>
26 static void flowprobe_export_entry (vlib_main_t * vm, flowprobe_entry_t * e);
30 * flow record generator graph node
35 /** interface handle */
38 /** packet timestamp */
40 /** size of the buffer */
50 ip46_address_t src_address;
51 ip46_address_t dst_address;
59 flowprobe_variant_t which;
62 static char *flowprobe_variant_strings[] = {
63 [FLOW_VARIANT_IP4] = "IP4",
64 [FLOW_VARIANT_IP6] = "IP6",
65 [FLOW_VARIANT_L2] = "L2",
66 [FLOW_VARIANT_L2_IP4] = "L2-IP4",
67 [FLOW_VARIANT_L2_IP6] = "L2-IP6",
70 /* packet trace format function */
72 format_flowprobe_trace (u8 * s, va_list * args)
74 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
75 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
76 flowprobe_trace_t *t = va_arg (*args, flowprobe_trace_t *);
77 u32 indent = format_get_indent (s);
80 "FLOWPROBE[%s]: rx_sw_if_index %d, tx_sw_if_index %d, "
81 "timestamp %lld, size %d", flowprobe_variant_strings[t->which],
82 t->rx_sw_if_index, t->tx_sw_if_index,
83 t->timestamp, t->buffer_size);
85 if (t->which == FLOW_VARIANT_L2)
86 s = format (s, "\n%U -> %U", format_white_space, indent,
87 format_ethernet_address, &t->src_mac,
88 format_ethernet_address, &t->dst_mac);
91 && (t->which == FLOW_VARIANT_L2_IP4 || t->which == FLOW_VARIANT_IP4
92 || t->which == FLOW_VARIANT_L2_IP6 || t->which == FLOW_VARIANT_IP6))
94 format (s, "\n%U%U: %U -> %U", format_white_space, indent,
95 format_ip_protocol, t->protocol, format_ip46_address,
96 &t->src_address, IP46_TYPE_ANY, format_ip46_address,
97 &t->dst_address, IP46_TYPE_ANY);
101 vlib_node_registration_t flowprobe_ip4_node;
102 vlib_node_registration_t flowprobe_ip6_node;
103 vlib_node_registration_t flowprobe_l2_node;
105 /* No counters at the moment */
106 #define foreach_flowprobe_error \
107 _(COLLISION, "Hash table collisions") \
108 _(BUFFER, "Buffer allocation error") \
109 _(EXPORTED_PACKETS, "Exported packets") \
110 _(INPATH, "Exported packets in path")
114 #define _(sym,str) FLOWPROBE_ERROR_##sym,
115 foreach_flowprobe_error
120 static char *flowprobe_error_strings[] = {
121 #define _(sym,string) string,
122 foreach_flowprobe_error
129 FLOWPROBE_NEXT_IP4_LOOKUP,
133 #define FLOWPROBE_NEXT_NODES { \
134 [FLOWPROBE_NEXT_DROP] = "error-drop", \
135 [FLOWPROBE_NEXT_IP4_LOOKUP] = "ip4-lookup", \
138 static inline flowprobe_variant_t
139 flowprobe_get_variant (flowprobe_variant_t which,
140 flowprobe_record_t flags, u16 ethertype)
142 if (which == FLOW_VARIANT_L2
143 && (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4))
144 return ethertype == ETHERNET_TYPE_IP6 ? FLOW_VARIANT_L2_IP6 : ethertype ==
145 ETHERNET_TYPE_IP4 ? FLOW_VARIANT_L2_IP4 : FLOW_VARIANT_L2;
150 * NTP rfc868 : 2 208 988 800 corresponds to 00:00 1 Jan 1970 GMT
152 #define NTP_TIMESTAMP 2208988800LU
155 flowprobe_common_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
159 /* Ingress interface */
160 u32 rx_if = clib_host_to_net_u32 (e->key.rx_sw_if_index);
161 clib_memcpy_fast (to_b->data + offset, &rx_if, sizeof (rx_if));
162 offset += sizeof (rx_if);
164 /* Egress interface */
165 u32 tx_if = clib_host_to_net_u32 (e->key.tx_sw_if_index);
166 clib_memcpy_fast (to_b->data + offset, &tx_if, sizeof (tx_if));
167 offset += sizeof (tx_if);
169 /* packet delta count */
170 u64 packetdelta = clib_host_to_net_u64 (e->packetcount);
171 clib_memcpy_fast (to_b->data + offset, &packetdelta, sizeof (u64));
172 offset += sizeof (u64);
174 /* flowStartNanoseconds */
175 u32 t = clib_host_to_net_u32 (e->flow_start.sec + NTP_TIMESTAMP);
176 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
177 offset += sizeof (u32);
178 t = clib_host_to_net_u32 (e->flow_start.nsec);
179 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
180 offset += sizeof (u32);
182 /* flowEndNanoseconds */
183 t = clib_host_to_net_u32 (e->flow_end.sec + NTP_TIMESTAMP);
184 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
185 offset += sizeof (u32);
186 t = clib_host_to_net_u32 (e->flow_end.nsec);
187 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
188 offset += sizeof (u32);
190 return offset - start;
194 flowprobe_l2_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
198 /* src mac address */
199 clib_memcpy_fast (to_b->data + offset, &e->key.src_mac, 6);
202 /* dst mac address */
203 clib_memcpy_fast (to_b->data + offset, &e->key.dst_mac, 6);
207 clib_memcpy_fast (to_b->data + offset, &e->key.ethertype, 2);
210 return offset - start;
214 flowprobe_l3_ip6_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
218 /* ip6 src address */
219 clib_memcpy_fast (to_b->data + offset, &e->key.src_address,
220 sizeof (ip6_address_t));
221 offset += sizeof (ip6_address_t);
223 /* ip6 dst address */
224 clib_memcpy_fast (to_b->data + offset, &e->key.dst_address,
225 sizeof (ip6_address_t));
226 offset += sizeof (ip6_address_t);
229 to_b->data[offset++] = e->key.protocol;
231 /* octetDeltaCount */
232 u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
233 clib_memcpy_fast (to_b->data + offset, &octetdelta, sizeof (u64));
234 offset += sizeof (u64);
236 return offset - start;
240 flowprobe_l3_ip4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
244 /* ip4 src address */
245 clib_memcpy_fast (to_b->data + offset, &e->key.src_address.ip4,
246 sizeof (ip4_address_t));
247 offset += sizeof (ip4_address_t);
249 /* ip4 dst address */
250 clib_memcpy_fast (to_b->data + offset, &e->key.dst_address.ip4,
251 sizeof (ip4_address_t));
252 offset += sizeof (ip4_address_t);
255 to_b->data[offset++] = e->key.protocol;
257 /* octetDeltaCount */
258 u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
259 clib_memcpy_fast (to_b->data + offset, &octetdelta, sizeof (u64));
260 offset += sizeof (u64);
262 return offset - start;
266 flowprobe_l4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
271 clib_memcpy_fast (to_b->data + offset, &e->key.src_port, 2);
275 clib_memcpy_fast (to_b->data + offset, &e->key.dst_port, 2);
278 /* tcp control bits */
279 u16 control_bits = htons (e->prot.tcp.flags);
280 clib_memcpy_fast (to_b->data + offset, &control_bits, 2);
283 return offset - start;
287 flowprobe_hash (flowprobe_key_t * k)
289 flowprobe_main_t *fm = &flowprobe_main;
292 #ifdef clib_crc32c_uses_intrinsics
293 h = clib_crc32c ((u8 *) k, sizeof (*k));
297 for (i = 0; i < sizeof (*k) / 8; i++)
298 tmp ^= ((u64 *) k)[i];
300 h = clib_xxhash (tmp);
303 return h >> (32 - fm->ht_log2len);
307 flowprobe_lookup (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex,
310 flowprobe_main_t *fm = &flowprobe_main;
311 flowprobe_entry_t *e;
314 h = (fm->active_timer) ? flowprobe_hash (k) : 0;
316 /* Lookup in the flow state pool */
317 *poolindex = fm->hash_per_worker[my_cpu_number][h];
318 if (*poolindex != ~0)
320 e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], *poolindex);
323 /* Verify key or report collision */
324 if (memcmp (k, &e->key, sizeof (flowprobe_key_t)))
334 flowprobe_create (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex)
336 flowprobe_main_t *fm = &flowprobe_main;
339 flowprobe_entry_t *e;
342 h = (fm->active_timer) ? flowprobe_hash (k) : 0;
344 pool_get (fm->pool_per_worker[my_cpu_number], e);
345 *poolindex = e - fm->pool_per_worker[my_cpu_number];
346 fm->hash_per_worker[my_cpu_number][h] = *poolindex;
350 if (fm->passive_timer > 0)
352 e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
353 (fm->timers_per_worker[my_cpu_number], *poolindex, 0,
360 add_to_flow_record_state (vlib_main_t * vm, vlib_node_runtime_t * node,
361 flowprobe_main_t * fm, vlib_buffer_t * b,
362 timestamp_nsec_t timestamp, u16 length,
363 flowprobe_variant_t which, flowprobe_trace_t * t)
368 u32 my_cpu_number = vm->thread_index;
371 flowprobe_record_t flags = fm->context[which].flags;
372 bool collect_ip4 = false, collect_ip6 = false;
374 ethernet_header_t *eth = vlib_buffer_get_current (b);
375 u16 ethertype = clib_net_to_host_u16 (eth->type);
377 flowprobe_key_t k = {};
379 ip4_header_t *ip4 = 0;
380 ip6_header_t *ip6 = 0;
381 udp_header_t *udp = 0;
382 tcp_header_t *tcp = 0;
385 if (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4)
387 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
388 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
391 k.rx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
392 k.tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
396 if (flags & FLOW_RECORD_L2)
398 clib_memcpy_fast (k.src_mac, eth->src_address, 6);
399 clib_memcpy_fast (k.dst_mac, eth->dst_address, 6);
400 k.ethertype = ethertype;
402 if (collect_ip6 && ethertype == ETHERNET_TYPE_IP6)
404 ip6 = (ip6_header_t *) (eth + 1);
405 if (flags & FLOW_RECORD_L3)
407 k.src_address.as_u64[0] = ip6->src_address.as_u64[0];
408 k.src_address.as_u64[1] = ip6->src_address.as_u64[1];
409 k.dst_address.as_u64[0] = ip6->dst_address.as_u64[0];
410 k.dst_address.as_u64[1] = ip6->dst_address.as_u64[1];
412 k.protocol = ip6->protocol;
413 if (k.protocol == IP_PROTOCOL_UDP)
414 udp = (udp_header_t *) (ip6 + 1);
415 else if (k.protocol == IP_PROTOCOL_TCP)
416 tcp = (tcp_header_t *) (ip6 + 1);
418 octets = clib_net_to_host_u16 (ip6->payload_length)
419 + sizeof (ip6_header_t);
421 if (collect_ip4 && ethertype == ETHERNET_TYPE_IP4)
423 ip4 = (ip4_header_t *) (eth + 1);
424 if (flags & FLOW_RECORD_L3)
426 k.src_address.ip4.as_u32 = ip4->src_address.as_u32;
427 k.dst_address.ip4.as_u32 = ip4->dst_address.as_u32;
429 k.protocol = ip4->protocol;
430 if ((flags & FLOW_RECORD_L4) && k.protocol == IP_PROTOCOL_UDP)
431 udp = (udp_header_t *) (ip4 + 1);
432 else if ((flags & FLOW_RECORD_L4) && k.protocol == IP_PROTOCOL_TCP)
433 tcp = (tcp_header_t *) (ip4 + 1);
435 octets = clib_net_to_host_u16 (ip4->length);
440 k.src_port = udp->src_port;
441 k.dst_port = udp->dst_port;
445 k.src_port = tcp->src_port;
446 k.dst_port = tcp->dst_port;
447 tcp_flags = tcp->flags;
452 t->rx_sw_if_index = k.rx_sw_if_index;
453 t->tx_sw_if_index = k.tx_sw_if_index;
454 clib_memcpy_fast (t->src_mac, k.src_mac, 6);
455 clib_memcpy_fast (t->dst_mac, k.dst_mac, 6);
456 t->ethertype = k.ethertype;
457 t->src_address.ip4.as_u32 = k.src_address.ip4.as_u32;
458 t->dst_address.ip4.as_u32 = k.dst_address.ip4.as_u32;
459 t->protocol = k.protocol;
460 t->src_port = k.src_port;
461 t->dst_port = k.dst_port;
465 flowprobe_entry_t *e = 0;
466 f64 now = vlib_time_now (vm);
467 if (fm->active_timer > 0)
470 bool collision = false;
472 e = flowprobe_lookup (my_cpu_number, &k, &poolindex, &collision);
475 /* Flush data and clean up entry for reuse. */
477 flowprobe_export_entry (vm, e);
479 e->flow_start = timestamp;
480 vlib_node_increment_counter (vm, node->node_index,
481 FLOWPROBE_ERROR_COLLISION, 1);
483 if (!e) /* Create new entry */
485 e = flowprobe_create (my_cpu_number, &k, &poolindex);
486 e->last_exported = now;
487 e->flow_start = timestamp;
492 e = &fm->stateless_entry[my_cpu_number];
500 e->octetcount += octets;
501 e->last_updated = now;
502 e->flow_end = timestamp;
503 e->prot.tcp.flags |= tcp_flags;
504 if (fm->active_timer == 0
505 || (now > e->last_exported + fm->active_timer))
506 flowprobe_export_entry (vm, e);
511 flowprobe_get_headersize (void)
513 return sizeof (ip4_header_t) + sizeof (udp_header_t) +
514 sizeof (ipfix_message_header_t) + sizeof (ipfix_set_header_t);
518 flowprobe_export_send (vlib_main_t * vm, vlib_buffer_t * b0,
519 flowprobe_variant_t which)
521 flowprobe_main_t *fm = &flowprobe_main;
522 flow_report_main_t *frm = &flow_report_main;
524 ip4_ipfix_template_packet_t *tp;
525 ipfix_set_header_t *s;
526 ipfix_message_header_t *h;
529 flowprobe_record_t flags = fm->context[which].flags;
530 u32 my_cpu_number = vm->thread_index;
533 flow_report_stream_t *stream;
535 /* Nothing to send */
536 if (fm->context[which].next_record_offset_per_worker[my_cpu_number] <=
537 flowprobe_get_headersize ())
540 u32 i, index = vec_len (frm->streams);
541 for (i = 0; i < index; i++)
542 if (frm->streams[i].domain_id == 1)
547 if (i == vec_len (frm->streams))
549 vec_validate (frm->streams, index);
550 frm->streams[index].domain_id = 1;
552 stream = &frm->streams[index];
554 tp = vlib_buffer_get_current (b0);
555 ip = (ip4_header_t *) & tp->ip4;
556 udp = (udp_header_t *) (ip + 1);
557 h = (ipfix_message_header_t *) (udp + 1);
558 s = (ipfix_set_header_t *) (h + 1);
560 ip->ip_version_and_header_length = 0x45;
562 ip->protocol = IP_PROTOCOL_UDP;
563 ip->flags_and_fragment_offset = 0;
564 ip->src_address.as_u32 = frm->src_address.as_u32;
565 ip->dst_address.as_u32 = frm->ipfix_collector.as_u32;
566 udp->src_port = clib_host_to_net_u16 (stream->src_port);
567 udp->dst_port = clib_host_to_net_u16 (frm->collector_port);
570 /* FIXUP: message header export_time */
571 h->export_time = (u32)
572 (((f64) frm->unix_time_0) +
573 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
574 h->export_time = clib_host_to_net_u32 (h->export_time);
575 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
577 /* FIXUP: message header sequence_number */
578 h->sequence_number = stream->sequence_number++;
579 h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
581 s->set_id_length = ipfix_set_id_length (fm->template_reports[flags],
583 (sizeof (*ip) + sizeof (*udp) +
585 h->version_length = version_length (b0->current_length -
586 (sizeof (*ip) + sizeof (*udp)));
588 ip->length = clib_host_to_net_u16 (b0->current_length);
590 ip->checksum = ip4_header_checksum (ip);
591 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
593 if (frm->udp_checksum)
595 /* RFC 7011 section 10.3.2. */
596 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
597 if (udp->checksum == 0)
598 udp->checksum = 0xffff;
601 ASSERT (ip4_header_checksum_is_valid (ip));
603 /* Find or allocate a frame */
604 f = fm->context[which].frames_per_worker[my_cpu_number];
605 if (PREDICT_FALSE (f == 0))
608 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
609 fm->context[which].frames_per_worker[my_cpu_number] = f;
610 u32 bi0 = vlib_get_buffer_index (vm, b0);
612 /* Enqueue the buffer */
613 to_next = vlib_frame_vector_args (f);
618 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
619 vlib_node_increment_counter (vm, flowprobe_l2_node.index,
620 FLOWPROBE_ERROR_EXPORTED_PACKETS, 1);
622 fm->context[which].frames_per_worker[my_cpu_number] = 0;
623 fm->context[which].buffers_per_worker[my_cpu_number] = 0;
624 fm->context[which].next_record_offset_per_worker[my_cpu_number] =
625 flowprobe_get_headersize ();
628 static vlib_buffer_t *
629 flowprobe_get_buffer (vlib_main_t * vm, flowprobe_variant_t which)
631 flowprobe_main_t *fm = &flowprobe_main;
632 flow_report_main_t *frm = &flow_report_main;
635 u32 my_cpu_number = vm->thread_index;
637 /* Find or allocate a buffer */
638 b0 = fm->context[which].buffers_per_worker[my_cpu_number];
640 /* Need to allocate a buffer? */
641 if (PREDICT_FALSE (b0 == 0))
643 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
645 vlib_node_increment_counter (vm, flowprobe_l2_node.index,
646 FLOWPROBE_ERROR_BUFFER, 1);
650 /* Initialize the buffer */
651 b0 = fm->context[which].buffers_per_worker[my_cpu_number] =
652 vlib_get_buffer (vm, bi0);
654 b0->current_data = 0;
655 b0->current_length = flowprobe_get_headersize ();
657 (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
658 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
659 vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
660 fm->context[which].next_record_offset_per_worker[my_cpu_number] =
668 flowprobe_export_entry (vlib_main_t * vm, flowprobe_entry_t * e)
670 u32 my_cpu_number = vm->thread_index;
671 flowprobe_main_t *fm = &flowprobe_main;
672 flow_report_main_t *frm = &flow_report_main;
674 bool collect_ip4 = false, collect_ip6 = false;
675 flowprobe_variant_t which = e->key.which;
676 flowprobe_record_t flags = fm->context[which].flags;
678 fm->context[which].next_record_offset_per_worker[my_cpu_number];
680 if (offset < flowprobe_get_headersize ())
681 offset = flowprobe_get_headersize ();
683 b0 = flowprobe_get_buffer (vm, which);
684 /* No available buffer, what to do... */
688 if (flags & FLOW_RECORD_L3)
690 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
691 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
694 offset += flowprobe_common_add (b0, e, offset);
696 if (flags & FLOW_RECORD_L2)
697 offset += flowprobe_l2_add (b0, e, offset);
699 offset += flowprobe_l3_ip6_add (b0, e, offset);
701 offset += flowprobe_l3_ip4_add (b0, e, offset);
702 if (flags & FLOW_RECORD_L4)
703 offset += flowprobe_l4_add (b0, e, offset);
705 /* Reset per flow-export counters */
708 e->last_exported = vlib_time_now (vm);
710 b0->current_length = offset;
712 fm->context[which].next_record_offset_per_worker[my_cpu_number] = offset;
713 /* Time to flush the buffer? */
714 if (offset + fm->template_size[flags] > frm->path_mtu)
715 flowprobe_export_send (vm, b0, which);
719 flowprobe_node_fn (vlib_main_t * vm,
720 vlib_node_runtime_t * node, vlib_frame_t * frame,
721 flowprobe_variant_t which)
723 u32 n_left_from, *from, *to_next;
724 flowprobe_next_t next_index;
725 flowprobe_main_t *fm = &flowprobe_main;
726 timestamp_nsec_t timestamp;
728 unix_time_now_nsec_fraction (×tamp.sec, ×tamp.nsec);
730 from = vlib_frame_vector_args (frame);
731 n_left_from = frame->n_vectors;
732 next_index = node->cached_next_index;
734 while (n_left_from > 0)
738 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
740 while (n_left_from >= 4 && n_left_to_next >= 2)
742 u32 next0 = FLOWPROBE_NEXT_DROP;
743 u32 next1 = FLOWPROBE_NEXT_DROP;
746 vlib_buffer_t *b0, *b1;
748 /* Prefetch next iteration. */
750 vlib_buffer_t *p2, *p3;
752 p2 = vlib_get_buffer (vm, from[2]);
753 p3 = vlib_get_buffer (vm, from[3]);
755 vlib_prefetch_buffer_header (p2, LOAD);
756 vlib_prefetch_buffer_header (p3, LOAD);
758 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
759 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
762 /* speculatively enqueue b0 and b1 to the current next frame */
763 to_next[0] = bi0 = from[0];
764 to_next[1] = bi1 = from[1];
770 b0 = vlib_get_buffer (vm, bi0);
771 b1 = vlib_get_buffer (vm, bi1);
773 vnet_feature_next (&next0, b0);
774 vnet_feature_next (&next1, b1);
776 len0 = vlib_buffer_length_in_chain (vm, b0);
777 ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
778 u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
780 if (PREDICT_TRUE ((b0->flags & VNET_BUFFER_F_FLOW_REPORT) == 0))
781 add_to_flow_record_state (vm, node, fm, b0, timestamp, len0,
782 flowprobe_get_variant
783 (which, fm->context[which].flags,
786 len1 = vlib_buffer_length_in_chain (vm, b1);
787 ethernet_header_t *eh1 = vlib_buffer_get_current (b1);
788 u16 ethertype1 = clib_net_to_host_u16 (eh1->type);
790 if (PREDICT_TRUE ((b1->flags & VNET_BUFFER_F_FLOW_REPORT) == 0))
791 add_to_flow_record_state (vm, node, fm, b1, timestamp, len1,
792 flowprobe_get_variant
793 (which, fm->context[which].flags,
796 /* verify speculative enqueues, maybe switch current next frame */
797 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
798 to_next, n_left_to_next,
799 bi0, bi1, next0, next1);
802 while (n_left_from > 0 && n_left_to_next > 0)
806 u32 next0 = FLOWPROBE_NEXT_DROP;
809 /* speculatively enqueue b0 to the current next frame */
817 b0 = vlib_get_buffer (vm, bi0);
819 vnet_feature_next (&next0, b0);
821 len0 = vlib_buffer_length_in_chain (vm, b0);
822 ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
823 u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
825 if (PREDICT_TRUE ((b0->flags & VNET_BUFFER_F_FLOW_REPORT) == 0))
827 flowprobe_trace_t *t = 0;
828 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
829 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
830 t = vlib_add_trace (vm, node, b0, sizeof (*t));
832 add_to_flow_record_state (vm, node, fm, b0, timestamp, len0,
833 flowprobe_get_variant
834 (which, fm->context[which].flags,
838 /* verify speculative enqueue, maybe switch current next frame */
839 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
840 to_next, n_left_to_next,
844 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
846 return frame->n_vectors;
850 flowprobe_ip4_node_fn (vlib_main_t * vm,
851 vlib_node_runtime_t * node, vlib_frame_t * frame)
853 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP4);
857 flowprobe_ip6_node_fn (vlib_main_t * vm,
858 vlib_node_runtime_t * node, vlib_frame_t * frame)
860 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP6);
864 flowprobe_l2_node_fn (vlib_main_t * vm,
865 vlib_node_runtime_t * node, vlib_frame_t * frame)
867 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_L2);
871 flush_record (flowprobe_variant_t which)
873 vlib_main_t *vm = vlib_get_main ();
874 vlib_buffer_t *b = flowprobe_get_buffer (vm, which);
876 flowprobe_export_send (vm, b, which);
880 flowprobe_flush_callback_ip4 (void)
882 flush_record (FLOW_VARIANT_IP4);
886 flowprobe_flush_callback_ip6 (void)
888 flush_record (FLOW_VARIANT_IP6);
892 flowprobe_flush_callback_l2 (void)
894 flush_record (FLOW_VARIANT_L2);
895 flush_record (FLOW_VARIANT_L2_IP4);
896 flush_record (FLOW_VARIANT_L2_IP6);
901 flowprobe_delete_by_index (u32 my_cpu_number, u32 poolindex)
903 flowprobe_main_t *fm = &flowprobe_main;
904 flowprobe_entry_t *e;
907 e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], poolindex);
910 h = flowprobe_hash (&e->key);
913 fm->hash_per_worker[my_cpu_number][h] = ~0;
915 pool_put_index (fm->pool_per_worker[my_cpu_number], poolindex);
919 /* Per worker process processing the active/passive expired entries */
921 flowprobe_walker_process (vlib_main_t * vm,
922 vlib_node_runtime_t * rt, vlib_frame_t * f)
924 flowprobe_main_t *fm = &flowprobe_main;
925 flow_report_main_t *frm = &flow_report_main;
926 flowprobe_entry_t *e;
929 * $$$$ Remove this check from here and track FRM status and disable
930 * this process if required.
932 if (frm->ipfix_collector.as_u32 == 0 || frm->src_address.as_u32 == 0)
937 fm->disabled = false;
939 u32 cpu_index = os_get_thread_index ();
940 u32 *to_be_removed = 0, *i;
943 * Tick the timer when required and process the vector of expired
946 f64 start_time = vlib_time_now (vm);
949 tw_timer_expire_timers_2t_1w_2048sl (fm->timers_per_worker[cpu_index],
952 vec_foreach (i, fm->expired_passive_per_worker[cpu_index])
955 f64 now = vlib_time_now (vm);
956 if (now > start_time + 100e-6
957 || exported > FLOW_MAXIMUM_EXPORT_ENTRIES - 1)
960 if (pool_is_free_index (fm->pool_per_worker[cpu_index], *i))
962 clib_warning ("Element is %d is freed already\n", *i);
966 e = pool_elt_at_index (fm->pool_per_worker[cpu_index], *i);
968 /* Check last update timestamp. If it is longer than passive time nuke
969 * entry. Otherwise restart timer with what's left
970 * Premature passive timer by more than 10%
972 if ((now - e->last_updated) < (u64) (fm->passive_timer * 0.9))
974 u64 delta = fm->passive_timer - (now - e->last_updated);
975 e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
976 (fm->timers_per_worker[cpu_index], *i, 0, delta);
978 else /* Nuke entry */
980 vec_add1 (to_be_removed, *i);
982 /* If anything to report send it to the exporter */
983 if (e->packetcount && now > e->last_exported + fm->active_timer)
986 flowprobe_export_entry (vm, e);
991 vec_delete (fm->expired_passive_per_worker[cpu_index], count, 0);
993 vec_foreach (i, to_be_removed) flowprobe_delete_by_index (cpu_index, *i);
994 vec_free (to_be_removed);
1000 VLIB_REGISTER_NODE (flowprobe_ip4_node) = {
1001 .function = flowprobe_ip4_node_fn,
1002 .name = "flowprobe-ip4",
1003 .vector_size = sizeof (u32),
1004 .format_trace = format_flowprobe_trace,
1005 .type = VLIB_NODE_TYPE_INTERNAL,
1006 .n_errors = ARRAY_LEN(flowprobe_error_strings),
1007 .error_strings = flowprobe_error_strings,
1008 .n_next_nodes = FLOWPROBE_N_NEXT,
1009 .next_nodes = FLOWPROBE_NEXT_NODES,
1011 VLIB_REGISTER_NODE (flowprobe_ip6_node) = {
1012 .function = flowprobe_ip6_node_fn,
1013 .name = "flowprobe-ip6",
1014 .vector_size = sizeof (u32),
1015 .format_trace = format_flowprobe_trace,
1016 .type = VLIB_NODE_TYPE_INTERNAL,
1017 .n_errors = ARRAY_LEN(flowprobe_error_strings),
1018 .error_strings = flowprobe_error_strings,
1019 .n_next_nodes = FLOWPROBE_N_NEXT,
1020 .next_nodes = FLOWPROBE_NEXT_NODES,
1022 VLIB_REGISTER_NODE (flowprobe_l2_node) = {
1023 .function = flowprobe_l2_node_fn,
1024 .name = "flowprobe-l2",
1025 .vector_size = sizeof (u32),
1026 .format_trace = format_flowprobe_trace,
1027 .type = VLIB_NODE_TYPE_INTERNAL,
1028 .n_errors = ARRAY_LEN(flowprobe_error_strings),
1029 .error_strings = flowprobe_error_strings,
1030 .n_next_nodes = FLOWPROBE_N_NEXT,
1031 .next_nodes = FLOWPROBE_NEXT_NODES,
1033 VLIB_REGISTER_NODE (flowprobe_walker_node) = {
1034 .function = flowprobe_walker_process,
1035 .name = "flowprobe-walker",
1036 .type = VLIB_NODE_TYPE_INPUT,
1037 .state = VLIB_NODE_STATE_INTERRUPT,
1042 * fd.io coding-style-patch-verification: ON
1045 * eval: (c-set-style "gnu")