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/xxhash.h>
21 #include <vppinfra/error.h>
22 #include <flowprobe/flowprobe.h>
23 #include <vnet/ip/ip6_packet.h>
24 #include <vnet/udp/udp_local.h>
25 #include <vlibmemory/api.h>
27 static void flowprobe_export_entry (vlib_main_t * vm, flowprobe_entry_t * e);
31 * flow record generator graph node
36 /** interface handle */
39 /** packet timestamp */
41 /** size of the buffer */
51 ip46_address_t src_address;
52 ip46_address_t dst_address;
60 flowprobe_variant_t which;
63 static char *flowprobe_variant_strings[] = {
64 [FLOW_VARIANT_IP4] = "IP4",
65 [FLOW_VARIANT_IP6] = "IP6",
66 [FLOW_VARIANT_L2] = "L2",
67 [FLOW_VARIANT_L2_IP4] = "L2-IP4",
68 [FLOW_VARIANT_L2_IP6] = "L2-IP6",
71 /* packet trace format function */
73 format_flowprobe_trace (u8 * s, va_list * args)
75 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
76 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
77 flowprobe_trace_t *t = va_arg (*args, flowprobe_trace_t *);
78 u32 indent = format_get_indent (s);
81 "FLOWPROBE[%s]: rx_sw_if_index %d, tx_sw_if_index %d, "
82 "timestamp %lld, size %d", flowprobe_variant_strings[t->which],
83 t->rx_sw_if_index, t->tx_sw_if_index,
84 t->timestamp, t->buffer_size);
86 if (t->which == FLOW_VARIANT_L2)
87 s = format (s, "\n%U -> %U", format_white_space, indent,
88 format_ethernet_address, &t->src_mac,
89 format_ethernet_address, &t->dst_mac);
92 && (t->which == FLOW_VARIANT_L2_IP4 || t->which == FLOW_VARIANT_IP4
93 || t->which == FLOW_VARIANT_L2_IP6 || t->which == FLOW_VARIANT_IP6))
95 format (s, "\n%U%U: %U -> %U", format_white_space, indent,
96 format_ip_protocol, t->protocol, format_ip46_address,
97 &t->src_address, IP46_TYPE_ANY, format_ip46_address,
98 &t->dst_address, IP46_TYPE_ANY);
102 vlib_node_registration_t flowprobe_input_ip4_node;
103 vlib_node_registration_t flowprobe_input_ip6_node;
104 vlib_node_registration_t flowprobe_input_l2_node;
105 vlib_node_registration_t flowprobe_output_ip4_node;
106 vlib_node_registration_t flowprobe_output_ip6_node;
107 vlib_node_registration_t flowprobe_output_l2_node;
109 /* No counters at the moment */
110 #define foreach_flowprobe_error \
111 _(COLLISION, "Hash table collisions") \
112 _(BUFFER, "Buffer allocation error") \
113 _(EXPORTED_PACKETS, "Exported packets") \
114 _(INPATH, "Exported packets in path")
118 #define _(sym,str) FLOWPROBE_ERROR_##sym,
119 foreach_flowprobe_error
124 static char *flowprobe_error_strings[] = {
125 #define _(sym,string) string,
126 foreach_flowprobe_error
133 FLOWPROBE_NEXT_IP4_LOOKUP,
137 #define FLOWPROBE_NEXT_NODES { \
138 [FLOWPROBE_NEXT_DROP] = "error-drop", \
139 [FLOWPROBE_NEXT_IP4_LOOKUP] = "ip4-lookup", \
142 static inline flowprobe_variant_t
143 flowprobe_get_variant (flowprobe_variant_t which,
144 flowprobe_record_t flags, u16 ethertype)
146 if (which == FLOW_VARIANT_L2
147 && (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4))
148 return ethertype == ETHERNET_TYPE_IP6 ? FLOW_VARIANT_L2_IP6 : ethertype ==
149 ETHERNET_TYPE_IP4 ? FLOW_VARIANT_L2_IP4 : FLOW_VARIANT_L2;
154 * NTP rfc868 : 2 208 988 800 corresponds to 00:00 1 Jan 1970 GMT
156 #define NTP_TIMESTAMP 2208988800LU
159 flowprobe_common_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
163 /* Ingress interface */
164 u32 rx_if = clib_host_to_net_u32 (e->key.rx_sw_if_index);
165 clib_memcpy_fast (to_b->data + offset, &rx_if, sizeof (rx_if));
166 offset += sizeof (rx_if);
168 /* Egress interface */
169 u32 tx_if = clib_host_to_net_u32 (e->key.tx_sw_if_index);
170 clib_memcpy_fast (to_b->data + offset, &tx_if, sizeof (tx_if));
171 offset += sizeof (tx_if);
176 to_b->data[offset++] = (e->key.direction == FLOW_DIRECTION_TX);
178 /* packet delta count */
179 u64 packetdelta = clib_host_to_net_u64 (e->packetcount);
180 clib_memcpy_fast (to_b->data + offset, &packetdelta, sizeof (u64));
181 offset += sizeof (u64);
183 /* flowStartNanoseconds */
184 u32 t = clib_host_to_net_u32 (e->flow_start.sec + NTP_TIMESTAMP);
185 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
186 offset += sizeof (u32);
187 t = clib_host_to_net_u32 (e->flow_start.nsec);
188 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
189 offset += sizeof (u32);
191 /* flowEndNanoseconds */
192 t = clib_host_to_net_u32 (e->flow_end.sec + NTP_TIMESTAMP);
193 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
194 offset += sizeof (u32);
195 t = clib_host_to_net_u32 (e->flow_end.nsec);
196 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
197 offset += sizeof (u32);
199 return offset - start;
203 flowprobe_l2_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
207 /* src mac address */
208 clib_memcpy_fast (to_b->data + offset, &e->key.src_mac, 6);
211 /* dst mac address */
212 clib_memcpy_fast (to_b->data + offset, &e->key.dst_mac, 6);
216 clib_memcpy_fast (to_b->data + offset, &e->key.ethertype, 2);
219 return offset - start;
223 flowprobe_l3_ip6_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
227 /* ip6 src address */
228 clib_memcpy_fast (to_b->data + offset, &e->key.src_address,
229 sizeof (ip6_address_t));
230 offset += sizeof (ip6_address_t);
232 /* ip6 dst address */
233 clib_memcpy_fast (to_b->data + offset, &e->key.dst_address,
234 sizeof (ip6_address_t));
235 offset += sizeof (ip6_address_t);
238 to_b->data[offset++] = e->key.protocol;
240 /* octetDeltaCount */
241 u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
242 clib_memcpy_fast (to_b->data + offset, &octetdelta, sizeof (u64));
243 offset += sizeof (u64);
245 return offset - start;
249 flowprobe_l3_ip4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
253 /* ip4 src address */
254 clib_memcpy_fast (to_b->data + offset, &e->key.src_address.ip4,
255 sizeof (ip4_address_t));
256 offset += sizeof (ip4_address_t);
258 /* ip4 dst address */
259 clib_memcpy_fast (to_b->data + offset, &e->key.dst_address.ip4,
260 sizeof (ip4_address_t));
261 offset += sizeof (ip4_address_t);
264 to_b->data[offset++] = e->key.protocol;
266 /* octetDeltaCount */
267 u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
268 clib_memcpy_fast (to_b->data + offset, &octetdelta, sizeof (u64));
269 offset += sizeof (u64);
271 return offset - start;
275 flowprobe_l4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
280 clib_memcpy_fast (to_b->data + offset, &e->key.src_port, 2);
284 clib_memcpy_fast (to_b->data + offset, &e->key.dst_port, 2);
287 /* tcp control bits */
288 u16 control_bits = htons (e->prot.tcp.flags);
289 clib_memcpy_fast (to_b->data + offset, &control_bits, 2);
292 return offset - start;
296 flowprobe_hash (flowprobe_key_t * k)
298 flowprobe_main_t *fm = &flowprobe_main;
301 #ifdef clib_crc32c_uses_intrinsics
302 h = clib_crc32c ((u8 *) k, sizeof (*k));
306 for (i = 0; i < sizeof (*k) / 8; i++)
307 tmp ^= ((u64 *) k)[i];
309 h = clib_xxhash (tmp);
312 return h >> (32 - fm->ht_log2len);
316 flowprobe_lookup (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex,
319 flowprobe_main_t *fm = &flowprobe_main;
320 flowprobe_entry_t *e;
323 h = (fm->active_timer) ? flowprobe_hash (k) : 0;
325 /* Lookup in the flow state pool */
326 *poolindex = fm->hash_per_worker[my_cpu_number][h];
327 if (*poolindex != ~0)
329 e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], *poolindex);
332 /* Verify key or report collision */
333 if (memcmp (k, &e->key, sizeof (flowprobe_key_t)))
343 flowprobe_create (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex)
345 flowprobe_main_t *fm = &flowprobe_main;
348 flowprobe_entry_t *e;
351 h = (fm->active_timer) ? flowprobe_hash (k) : 0;
353 pool_get (fm->pool_per_worker[my_cpu_number], e);
354 *poolindex = e - fm->pool_per_worker[my_cpu_number];
355 fm->hash_per_worker[my_cpu_number][h] = *poolindex;
359 if (fm->passive_timer > 0)
361 e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
362 (fm->timers_per_worker[my_cpu_number], *poolindex, 0,
369 add_to_flow_record_state (vlib_main_t *vm, vlib_node_runtime_t *node,
370 flowprobe_main_t *fm, vlib_buffer_t *b,
371 timestamp_nsec_t timestamp, u16 length,
372 flowprobe_variant_t which,
373 flowprobe_direction_t direction,
374 flowprobe_trace_t *t)
379 ASSERT (direction == FLOW_DIRECTION_RX || direction == FLOW_DIRECTION_TX);
381 u32 my_cpu_number = vm->thread_index;
384 flowprobe_record_t flags = fm->context[which].flags;
385 bool collect_ip4 = false, collect_ip6 = false;
387 ethernet_header_t *eth = (direction == FLOW_DIRECTION_TX) ?
388 vlib_buffer_get_current (b) :
389 ethernet_buffer_get_header (b);
390 u16 ethertype = clib_net_to_host_u16 (eth->type);
391 i16 l3_hdr_offset = (u8 *) eth - b->data + sizeof (ethernet_header_t);
393 flowprobe_key_t k = {};
395 ip4_header_t *ip4 = 0;
396 ip6_header_t *ip6 = 0;
397 udp_header_t *udp = 0;
398 tcp_header_t *tcp = 0;
401 if (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4)
403 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
404 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
407 k.rx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
408 k.tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
411 k.direction = direction;
413 if (flags & FLOW_RECORD_L2)
415 clib_memcpy_fast (k.src_mac, eth->src_address, 6);
416 clib_memcpy_fast (k.dst_mac, eth->dst_address, 6);
417 k.ethertype = ethertype;
419 if (ethertype == ETHERNET_TYPE_VLAN)
422 ethernet_vlan_header_tv_t *ethv =
423 (ethernet_vlan_header_tv_t *) (&(eth->type));
424 /*Q in Q possibility */
425 while (clib_net_to_host_u16 (ethv->type) == ETHERNET_TYPE_VLAN)
428 l3_hdr_offset += sizeof (ethernet_vlan_header_tv_t);
430 k.ethertype = ethertype = clib_net_to_host_u16 ((ethv)->type);
432 if (collect_ip6 && ethertype == ETHERNET_TYPE_IP6)
434 ip6 = (ip6_header_t *) (b->data + l3_hdr_offset);
435 if (flags & FLOW_RECORD_L3)
437 k.src_address.as_u64[0] = ip6->src_address.as_u64[0];
438 k.src_address.as_u64[1] = ip6->src_address.as_u64[1];
439 k.dst_address.as_u64[0] = ip6->dst_address.as_u64[0];
440 k.dst_address.as_u64[1] = ip6->dst_address.as_u64[1];
442 k.protocol = ip6->protocol;
443 if (k.protocol == IP_PROTOCOL_UDP)
444 udp = (udp_header_t *) (ip6 + 1);
445 else if (k.protocol == IP_PROTOCOL_TCP)
446 tcp = (tcp_header_t *) (ip6 + 1);
448 octets = clib_net_to_host_u16 (ip6->payload_length)
449 + sizeof (ip6_header_t);
451 if (collect_ip4 && ethertype == ETHERNET_TYPE_IP4)
453 ip4 = (ip4_header_t *) (b->data + l3_hdr_offset);
454 if (flags & FLOW_RECORD_L3)
456 k.src_address.ip4.as_u32 = ip4->src_address.as_u32;
457 k.dst_address.ip4.as_u32 = ip4->dst_address.as_u32;
459 k.protocol = ip4->protocol;
460 if ((flags & FLOW_RECORD_L4) && k.protocol == IP_PROTOCOL_UDP)
461 udp = (udp_header_t *) (ip4 + 1);
462 else if ((flags & FLOW_RECORD_L4) && k.protocol == IP_PROTOCOL_TCP)
463 tcp = (tcp_header_t *) (ip4 + 1);
465 octets = clib_net_to_host_u16 (ip4->length);
470 k.src_port = udp->src_port;
471 k.dst_port = udp->dst_port;
475 k.src_port = tcp->src_port;
476 k.dst_port = tcp->dst_port;
477 tcp_flags = tcp->flags;
482 t->rx_sw_if_index = k.rx_sw_if_index;
483 t->tx_sw_if_index = k.tx_sw_if_index;
484 clib_memcpy_fast (t->src_mac, k.src_mac, 6);
485 clib_memcpy_fast (t->dst_mac, k.dst_mac, 6);
486 t->ethertype = k.ethertype;
487 t->src_address.ip4.as_u32 = k.src_address.ip4.as_u32;
488 t->dst_address.ip4.as_u32 = k.dst_address.ip4.as_u32;
489 t->protocol = k.protocol;
490 t->src_port = k.src_port;
491 t->dst_port = k.dst_port;
495 flowprobe_entry_t *e = 0;
496 f64 now = vlib_time_now (vm);
497 if (fm->active_timer > 0)
500 bool collision = false;
502 e = flowprobe_lookup (my_cpu_number, &k, &poolindex, &collision);
505 /* Flush data and clean up entry for reuse. */
507 flowprobe_export_entry (vm, e);
509 e->flow_start = timestamp;
510 vlib_node_increment_counter (vm, node->node_index,
511 FLOWPROBE_ERROR_COLLISION, 1);
513 if (!e) /* Create new entry */
515 e = flowprobe_create (my_cpu_number, &k, &poolindex);
516 e->last_exported = now;
517 e->flow_start = timestamp;
522 e = &fm->stateless_entry[my_cpu_number];
530 e->octetcount += octets;
531 e->last_updated = now;
532 e->flow_end = timestamp;
533 e->prot.tcp.flags |= tcp_flags;
534 if (fm->active_timer == 0
535 || (now > e->last_exported + fm->active_timer))
536 flowprobe_export_entry (vm, e);
541 flowprobe_get_headersize (void)
543 return sizeof (ip4_header_t) + sizeof (udp_header_t) +
544 sizeof (ipfix_message_header_t) + sizeof (ipfix_set_header_t);
548 flowprobe_export_send (vlib_main_t * vm, vlib_buffer_t * b0,
549 flowprobe_variant_t which)
551 flowprobe_main_t *fm = &flowprobe_main;
552 flow_report_main_t *frm = &flow_report_main;
553 ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, 0);
555 ip4_ipfix_template_packet_t *tp;
556 ipfix_set_header_t *s;
557 ipfix_message_header_t *h;
560 flowprobe_record_t flags = fm->context[which].flags;
561 u32 my_cpu_number = vm->thread_index;
564 flow_report_stream_t *stream;
566 /* Nothing to send */
567 if (fm->context[which].next_record_offset_per_worker[my_cpu_number] <=
568 flowprobe_get_headersize ())
571 u32 i, index = vec_len (exp->streams);
572 for (i = 0; i < index; i++)
573 if (exp->streams[i].domain_id == 1)
578 if (i == vec_len (exp->streams))
580 vec_validate (exp->streams, index);
581 exp->streams[index].domain_id = 1;
583 stream = &exp->streams[index];
585 tp = vlib_buffer_get_current (b0);
586 ip = (ip4_header_t *) & tp->ip4;
587 udp = (udp_header_t *) (ip + 1);
588 h = (ipfix_message_header_t *) (udp + 1);
589 s = (ipfix_set_header_t *) (h + 1);
591 ip->ip_version_and_header_length = 0x45;
593 ip->protocol = IP_PROTOCOL_UDP;
594 ip->flags_and_fragment_offset = 0;
595 ip->src_address.as_u32 = exp->src_address.ip.ip4.as_u32;
596 ip->dst_address.as_u32 = exp->ipfix_collector.ip.ip4.as_u32;
597 udp->src_port = clib_host_to_net_u16 (stream->src_port);
598 udp->dst_port = clib_host_to_net_u16 (exp->collector_port);
601 /* FIXUP: message header export_time */
603 (u32) (((f64) frm->unix_time_0) + (vlib_time_now (vm) - frm->vlib_time_0));
604 h->export_time = clib_host_to_net_u32 (h->export_time);
605 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
607 /* FIXUP: message header sequence_number */
608 h->sequence_number = stream->sequence_number++;
609 h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
611 s->set_id_length = ipfix_set_id_length (fm->template_reports[flags],
613 (sizeof (*ip) + sizeof (*udp) +
615 h->version_length = version_length (b0->current_length -
616 (sizeof (*ip) + sizeof (*udp)));
618 ip->length = clib_host_to_net_u16 (b0->current_length);
620 ip->checksum = ip4_header_checksum (ip);
621 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
623 if (exp->udp_checksum)
625 /* RFC 7011 section 10.3.2. */
626 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
627 if (udp->checksum == 0)
628 udp->checksum = 0xffff;
631 ASSERT (ip4_header_checksum_is_valid (ip));
633 /* Find or allocate a frame */
634 f = fm->context[which].frames_per_worker[my_cpu_number];
635 if (PREDICT_FALSE (f == 0))
638 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
639 fm->context[which].frames_per_worker[my_cpu_number] = f;
640 u32 bi0 = vlib_get_buffer_index (vm, b0);
642 /* Enqueue the buffer */
643 to_next = vlib_frame_vector_args (f);
648 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
649 vlib_node_increment_counter (vm, flowprobe_output_l2_node.index,
650 FLOWPROBE_ERROR_EXPORTED_PACKETS, 1);
652 fm->context[which].frames_per_worker[my_cpu_number] = 0;
653 fm->context[which].buffers_per_worker[my_cpu_number] = 0;
654 fm->context[which].next_record_offset_per_worker[my_cpu_number] =
655 flowprobe_get_headersize ();
658 static vlib_buffer_t *
659 flowprobe_get_buffer (vlib_main_t * vm, flowprobe_variant_t which)
661 flowprobe_main_t *fm = &flowprobe_main;
662 ipfix_exporter_t *exp = pool_elt_at_index (flow_report_main.exporters, 0);
665 u32 my_cpu_number = vm->thread_index;
667 /* Find or allocate a buffer */
668 b0 = fm->context[which].buffers_per_worker[my_cpu_number];
670 /* Need to allocate a buffer? */
671 if (PREDICT_FALSE (b0 == 0))
673 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
675 vlib_node_increment_counter (vm, flowprobe_output_l2_node.index,
676 FLOWPROBE_ERROR_BUFFER, 1);
680 /* Initialize the buffer */
681 b0 = fm->context[which].buffers_per_worker[my_cpu_number] =
682 vlib_get_buffer (vm, bi0);
684 b0->current_data = 0;
685 b0->current_length = flowprobe_get_headersize ();
687 (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
688 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
689 vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index;
690 fm->context[which].next_record_offset_per_worker[my_cpu_number] =
698 flowprobe_export_entry (vlib_main_t * vm, flowprobe_entry_t * e)
700 u32 my_cpu_number = vm->thread_index;
701 flowprobe_main_t *fm = &flowprobe_main;
702 ipfix_exporter_t *exp = pool_elt_at_index (flow_report_main.exporters, 0);
704 bool collect_ip4 = false, collect_ip6 = false;
705 bool collect_l4 = false;
706 flowprobe_variant_t which = e->key.which;
707 flowprobe_record_t flags = fm->context[which].flags;
709 fm->context[which].next_record_offset_per_worker[my_cpu_number];
711 if (offset < flowprobe_get_headersize ())
712 offset = flowprobe_get_headersize ();
714 b0 = flowprobe_get_buffer (vm, which);
715 /* No available buffer, what to do... */
719 if (flags & FLOW_RECORD_L3)
721 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
722 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
724 if (flags & FLOW_RECORD_L4)
726 collect_l4 = (which != FLOW_VARIANT_L2);
729 offset += flowprobe_common_add (b0, e, offset);
731 if (flags & FLOW_RECORD_L2)
732 offset += flowprobe_l2_add (b0, e, offset);
734 offset += flowprobe_l3_ip6_add (b0, e, offset);
736 offset += flowprobe_l3_ip4_add (b0, e, offset);
738 offset += flowprobe_l4_add (b0, e, offset);
740 /* Reset per flow-export counters */
743 e->last_exported = vlib_time_now (vm);
744 e->prot.tcp.flags = 0;
746 b0->current_length = offset;
748 fm->context[which].next_record_offset_per_worker[my_cpu_number] = offset;
749 /* Time to flush the buffer? */
750 if (offset + fm->template_size[flags] > exp->path_mtu)
751 flowprobe_export_send (vm, b0, which);
755 flowprobe_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
756 vlib_frame_t *frame, flowprobe_variant_t which,
757 flowprobe_direction_t direction)
759 u32 n_left_from, *from, *to_next;
760 flowprobe_next_t next_index;
761 flowprobe_main_t *fm = &flowprobe_main;
762 timestamp_nsec_t timestamp;
764 unix_time_now_nsec_fraction (×tamp.sec, ×tamp.nsec);
766 from = vlib_frame_vector_args (frame);
767 n_left_from = frame->n_vectors;
768 next_index = node->cached_next_index;
770 while (n_left_from > 0)
774 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
776 while (n_left_from >= 4 && n_left_to_next >= 2)
778 u32 next0 = FLOWPROBE_NEXT_DROP;
779 u32 next1 = FLOWPROBE_NEXT_DROP;
782 vlib_buffer_t *b0, *b1;
784 /* Prefetch next iteration. */
786 vlib_buffer_t *p2, *p3;
788 p2 = vlib_get_buffer (vm, from[2]);
789 p3 = vlib_get_buffer (vm, from[3]);
791 vlib_prefetch_buffer_header (p2, LOAD);
792 vlib_prefetch_buffer_header (p3, LOAD);
794 clib_prefetch_store (p2->data);
795 clib_prefetch_store (p3->data);
798 /* speculatively enqueue b0 and b1 to the current next frame */
799 to_next[0] = bi0 = from[0];
800 to_next[1] = bi1 = from[1];
806 b0 = vlib_get_buffer (vm, bi0);
807 b1 = vlib_get_buffer (vm, bi1);
809 vnet_feature_next (&next0, b0);
810 vnet_feature_next (&next1, b1);
812 len0 = vlib_buffer_length_in_chain (vm, b0);
813 ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
814 u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
816 if (PREDICT_TRUE ((b0->flags & VNET_BUFFER_F_FLOW_REPORT) == 0))
817 add_to_flow_record_state (
818 vm, node, fm, b0, timestamp, len0,
819 flowprobe_get_variant (which, fm->context[which].flags,
823 len1 = vlib_buffer_length_in_chain (vm, b1);
824 ethernet_header_t *eh1 = vlib_buffer_get_current (b1);
825 u16 ethertype1 = clib_net_to_host_u16 (eh1->type);
827 if (PREDICT_TRUE ((b1->flags & VNET_BUFFER_F_FLOW_REPORT) == 0))
828 add_to_flow_record_state (
829 vm, node, fm, b1, timestamp, len1,
830 flowprobe_get_variant (which, fm->context[which].flags,
834 /* verify speculative enqueues, maybe switch current next frame */
835 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
836 to_next, n_left_to_next,
837 bi0, bi1, next0, next1);
840 while (n_left_from > 0 && n_left_to_next > 0)
844 u32 next0 = FLOWPROBE_NEXT_DROP;
847 /* speculatively enqueue b0 to the current next frame */
855 b0 = vlib_get_buffer (vm, bi0);
857 vnet_feature_next (&next0, b0);
859 len0 = vlib_buffer_length_in_chain (vm, b0);
860 ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
861 u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
863 if (PREDICT_TRUE ((b0->flags & VNET_BUFFER_F_FLOW_REPORT) == 0))
865 flowprobe_trace_t *t = 0;
866 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
867 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
868 t = vlib_add_trace (vm, node, b0, sizeof (*t));
870 add_to_flow_record_state (
871 vm, node, fm, b0, timestamp, len0,
872 flowprobe_get_variant (which, fm->context[which].flags,
877 /* verify speculative enqueue, maybe switch current next frame */
878 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
879 to_next, n_left_to_next,
883 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
885 return frame->n_vectors;
889 flowprobe_input_ip4_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
892 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP4,
897 flowprobe_input_ip6_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
900 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP6,
905 flowprobe_input_l2_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
908 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_L2,
913 flowprobe_output_ip4_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
916 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP4,
921 flowprobe_output_ip6_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
924 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP6,
929 flowprobe_output_l2_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
932 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_L2,
937 flush_record (flowprobe_variant_t which)
939 vlib_main_t *vm = vlib_get_main ();
940 vlib_buffer_t *b = flowprobe_get_buffer (vm, which);
942 flowprobe_export_send (vm, b, which);
946 flowprobe_flush_callback_ip4 (void)
948 flush_record (FLOW_VARIANT_IP4);
952 flowprobe_flush_callback_ip6 (void)
954 flush_record (FLOW_VARIANT_IP6);
958 flowprobe_flush_callback_l2 (void)
960 flush_record (FLOW_VARIANT_L2);
961 flush_record (FLOW_VARIANT_L2_IP4);
962 flush_record (FLOW_VARIANT_L2_IP6);
966 flowprobe_delete_by_index (u32 my_cpu_number, u32 poolindex)
968 flowprobe_main_t *fm = &flowprobe_main;
969 flowprobe_entry_t *e;
972 e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], poolindex);
975 h = flowprobe_hash (&e->key);
978 fm->hash_per_worker[my_cpu_number][h] = ~0;
980 pool_put_index (fm->pool_per_worker[my_cpu_number], poolindex);
984 /* Per worker process processing the active/passive expired entries */
986 flowprobe_walker_process (vlib_main_t * vm,
987 vlib_node_runtime_t * rt, vlib_frame_t * f)
989 flowprobe_main_t *fm = &flowprobe_main;
990 flowprobe_entry_t *e;
991 ipfix_exporter_t *exp = pool_elt_at_index (flow_report_main.exporters, 0);
994 * $$$$ Remove this check from here and track FRM status and disable
995 * this process if required.
997 if (ip_address_is_zero (&exp->ipfix_collector) ||
998 ip_address_is_zero (&exp->src_address))
1000 fm->disabled = true;
1003 fm->disabled = false;
1005 u32 cpu_index = os_get_thread_index ();
1006 u32 *to_be_removed = 0, *i;
1009 * Tick the timer when required and process the vector of expired
1012 f64 start_time = vlib_time_now (vm);
1015 tw_timer_expire_timers_2t_1w_2048sl (fm->timers_per_worker[cpu_index],
1018 vec_foreach (i, fm->expired_passive_per_worker[cpu_index])
1021 f64 now = vlib_time_now (vm);
1022 if (now > start_time + 100e-6
1023 || exported > FLOW_MAXIMUM_EXPORT_ENTRIES - 1)
1026 if (pool_is_free_index (fm->pool_per_worker[cpu_index], *i))
1028 clib_warning ("Element is %d is freed already\n", *i);
1032 e = pool_elt_at_index (fm->pool_per_worker[cpu_index], *i);
1034 /* Check last update timestamp. If it is longer than passive time nuke
1035 * entry. Otherwise restart timer with what's left
1036 * Premature passive timer by more than 10%
1038 if ((now - e->last_updated) < (u64) (fm->passive_timer * 0.9))
1040 u64 delta = fm->passive_timer - (now - e->last_updated);
1041 e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
1042 (fm->timers_per_worker[cpu_index], *i, 0, delta);
1044 else /* Nuke entry */
1046 vec_add1 (to_be_removed, *i);
1048 /* If anything to report send it to the exporter */
1049 if (e->packetcount && now > e->last_exported + fm->active_timer)
1052 flowprobe_export_entry (vm, e);
1057 vec_delete (fm->expired_passive_per_worker[cpu_index], count, 0);
1059 vec_foreach (i, to_be_removed) flowprobe_delete_by_index (cpu_index, *i);
1060 vec_free (to_be_removed);
1066 VLIB_REGISTER_NODE (flowprobe_input_ip4_node) = {
1067 .function = flowprobe_input_ip4_node_fn,
1068 .name = "flowprobe-input-ip4",
1069 .vector_size = sizeof (u32),
1070 .format_trace = format_flowprobe_trace,
1071 .type = VLIB_NODE_TYPE_INTERNAL,
1072 .n_errors = ARRAY_LEN (flowprobe_error_strings),
1073 .error_strings = flowprobe_error_strings,
1074 .n_next_nodes = FLOWPROBE_N_NEXT,
1075 .next_nodes = FLOWPROBE_NEXT_NODES,
1077 VLIB_REGISTER_NODE (flowprobe_input_ip6_node) = {
1078 .function = flowprobe_input_ip6_node_fn,
1079 .name = "flowprobe-input-ip6",
1080 .vector_size = sizeof (u32),
1081 .format_trace = format_flowprobe_trace,
1082 .type = VLIB_NODE_TYPE_INTERNAL,
1083 .n_errors = ARRAY_LEN (flowprobe_error_strings),
1084 .error_strings = flowprobe_error_strings,
1085 .n_next_nodes = FLOWPROBE_N_NEXT,
1086 .next_nodes = FLOWPROBE_NEXT_NODES,
1088 VLIB_REGISTER_NODE (flowprobe_input_l2_node) = {
1089 .function = flowprobe_input_l2_node_fn,
1090 .name = "flowprobe-input-l2",
1091 .vector_size = sizeof (u32),
1092 .format_trace = format_flowprobe_trace,
1093 .type = VLIB_NODE_TYPE_INTERNAL,
1094 .n_errors = ARRAY_LEN (flowprobe_error_strings),
1095 .error_strings = flowprobe_error_strings,
1096 .n_next_nodes = FLOWPROBE_N_NEXT,
1097 .next_nodes = FLOWPROBE_NEXT_NODES,
1099 VLIB_REGISTER_NODE (flowprobe_output_ip4_node) = {
1100 .function = flowprobe_output_ip4_node_fn,
1101 .name = "flowprobe-output-ip4",
1102 .vector_size = sizeof (u32),
1103 .format_trace = format_flowprobe_trace,
1104 .type = VLIB_NODE_TYPE_INTERNAL,
1105 .n_errors = ARRAY_LEN (flowprobe_error_strings),
1106 .error_strings = flowprobe_error_strings,
1107 .n_next_nodes = FLOWPROBE_N_NEXT,
1108 .next_nodes = FLOWPROBE_NEXT_NODES,
1110 VLIB_REGISTER_NODE (flowprobe_output_ip6_node) = {
1111 .function = flowprobe_output_ip6_node_fn,
1112 .name = "flowprobe-output-ip6",
1113 .vector_size = sizeof (u32),
1114 .format_trace = format_flowprobe_trace,
1115 .type = VLIB_NODE_TYPE_INTERNAL,
1116 .n_errors = ARRAY_LEN (flowprobe_error_strings),
1117 .error_strings = flowprobe_error_strings,
1118 .n_next_nodes = FLOWPROBE_N_NEXT,
1119 .next_nodes = FLOWPROBE_NEXT_NODES,
1121 VLIB_REGISTER_NODE (flowprobe_output_l2_node) = {
1122 .function = flowprobe_output_l2_node_fn,
1123 .name = "flowprobe-output-l2",
1124 .vector_size = sizeof (u32),
1125 .format_trace = format_flowprobe_trace,
1126 .type = VLIB_NODE_TYPE_INTERNAL,
1127 .n_errors = ARRAY_LEN (flowprobe_error_strings),
1128 .error_strings = flowprobe_error_strings,
1129 .n_next_nodes = FLOWPROBE_N_NEXT,
1130 .next_nodes = FLOWPROBE_NEXT_NODES,
1132 VLIB_REGISTER_NODE (flowprobe_walker_node) = {
1133 .function = flowprobe_walker_process,
1134 .name = "flowprobe-walker",
1135 .type = VLIB_NODE_TYPE_INPUT,
1136 .state = VLIB_NODE_STATE_INTERRUPT,
1141 * fd.io coding-style-patch-verification: ON
1144 * eval: (c-set-style "gnu")