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_ip4_node;
103 vlib_node_registration_t flowprobe_ip6_node;
104 vlib_node_registration_t flowprobe_l2_node;
106 /* No counters at the moment */
107 #define foreach_flowprobe_error \
108 _(COLLISION, "Hash table collisions") \
109 _(BUFFER, "Buffer allocation error") \
110 _(EXPORTED_PACKETS, "Exported packets") \
111 _(INPATH, "Exported packets in path")
115 #define _(sym,str) FLOWPROBE_ERROR_##sym,
116 foreach_flowprobe_error
121 static char *flowprobe_error_strings[] = {
122 #define _(sym,string) string,
123 foreach_flowprobe_error
130 FLOWPROBE_NEXT_IP4_LOOKUP,
134 #define FLOWPROBE_NEXT_NODES { \
135 [FLOWPROBE_NEXT_DROP] = "error-drop", \
136 [FLOWPROBE_NEXT_IP4_LOOKUP] = "ip4-lookup", \
139 static inline flowprobe_variant_t
140 flowprobe_get_variant (flowprobe_variant_t which,
141 flowprobe_record_t flags, u16 ethertype)
143 if (which == FLOW_VARIANT_L2
144 && (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4))
145 return ethertype == ETHERNET_TYPE_IP6 ? FLOW_VARIANT_L2_IP6 : ethertype ==
146 ETHERNET_TYPE_IP4 ? FLOW_VARIANT_L2_IP4 : FLOW_VARIANT_L2;
151 * NTP rfc868 : 2 208 988 800 corresponds to 00:00 1 Jan 1970 GMT
153 #define NTP_TIMESTAMP 2208988800LU
156 flowprobe_common_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
160 /* Ingress interface */
161 u32 rx_if = clib_host_to_net_u32 (e->key.rx_sw_if_index);
162 clib_memcpy_fast (to_b->data + offset, &rx_if, sizeof (rx_if));
163 offset += sizeof (rx_if);
165 /* Egress interface */
166 u32 tx_if = clib_host_to_net_u32 (e->key.tx_sw_if_index);
167 clib_memcpy_fast (to_b->data + offset, &tx_if, sizeof (tx_if));
168 offset += sizeof (tx_if);
170 /* packet delta count */
171 u64 packetdelta = clib_host_to_net_u64 (e->packetcount);
172 clib_memcpy_fast (to_b->data + offset, &packetdelta, sizeof (u64));
173 offset += sizeof (u64);
175 /* flowStartNanoseconds */
176 u32 t = clib_host_to_net_u32 (e->flow_start.sec + NTP_TIMESTAMP);
177 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
178 offset += sizeof (u32);
179 t = clib_host_to_net_u32 (e->flow_start.nsec);
180 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
181 offset += sizeof (u32);
183 /* flowEndNanoseconds */
184 t = clib_host_to_net_u32 (e->flow_end.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_end.nsec);
188 clib_memcpy_fast (to_b->data + offset, &t, sizeof (u32));
189 offset += sizeof (u32);
191 return offset - start;
195 flowprobe_l2_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
199 /* src mac address */
200 clib_memcpy_fast (to_b->data + offset, &e->key.src_mac, 6);
203 /* dst mac address */
204 clib_memcpy_fast (to_b->data + offset, &e->key.dst_mac, 6);
208 clib_memcpy_fast (to_b->data + offset, &e->key.ethertype, 2);
211 return offset - start;
215 flowprobe_l3_ip6_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
219 /* ip6 src address */
220 clib_memcpy_fast (to_b->data + offset, &e->key.src_address,
221 sizeof (ip6_address_t));
222 offset += sizeof (ip6_address_t);
224 /* ip6 dst address */
225 clib_memcpy_fast (to_b->data + offset, &e->key.dst_address,
226 sizeof (ip6_address_t));
227 offset += sizeof (ip6_address_t);
230 to_b->data[offset++] = e->key.protocol;
232 /* octetDeltaCount */
233 u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
234 clib_memcpy_fast (to_b->data + offset, &octetdelta, sizeof (u64));
235 offset += sizeof (u64);
237 return offset - start;
241 flowprobe_l3_ip4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
245 /* ip4 src address */
246 clib_memcpy_fast (to_b->data + offset, &e->key.src_address.ip4,
247 sizeof (ip4_address_t));
248 offset += sizeof (ip4_address_t);
250 /* ip4 dst address */
251 clib_memcpy_fast (to_b->data + offset, &e->key.dst_address.ip4,
252 sizeof (ip4_address_t));
253 offset += sizeof (ip4_address_t);
256 to_b->data[offset++] = e->key.protocol;
258 /* octetDeltaCount */
259 u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
260 clib_memcpy_fast (to_b->data + offset, &octetdelta, sizeof (u64));
261 offset += sizeof (u64);
263 return offset - start;
267 flowprobe_l4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
272 clib_memcpy_fast (to_b->data + offset, &e->key.src_port, 2);
276 clib_memcpy_fast (to_b->data + offset, &e->key.dst_port, 2);
279 /* tcp control bits */
280 u16 control_bits = htons (e->prot.tcp.flags);
281 clib_memcpy_fast (to_b->data + offset, &control_bits, 2);
284 return offset - start;
288 flowprobe_hash (flowprobe_key_t * k)
290 flowprobe_main_t *fm = &flowprobe_main;
293 #ifdef clib_crc32c_uses_intrinsics
294 h = clib_crc32c ((u8 *) k, sizeof (*k));
298 for (i = 0; i < sizeof (*k) / 8; i++)
299 tmp ^= ((u64 *) k)[i];
301 h = clib_xxhash (tmp);
304 return h >> (32 - fm->ht_log2len);
308 flowprobe_lookup (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex,
311 flowprobe_main_t *fm = &flowprobe_main;
312 flowprobe_entry_t *e;
315 h = (fm->active_timer) ? flowprobe_hash (k) : 0;
317 /* Lookup in the flow state pool */
318 *poolindex = fm->hash_per_worker[my_cpu_number][h];
319 if (*poolindex != ~0)
321 e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], *poolindex);
324 /* Verify key or report collision */
325 if (memcmp (k, &e->key, sizeof (flowprobe_key_t)))
335 flowprobe_create (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex)
337 flowprobe_main_t *fm = &flowprobe_main;
340 flowprobe_entry_t *e;
343 h = (fm->active_timer) ? flowprobe_hash (k) : 0;
345 pool_get (fm->pool_per_worker[my_cpu_number], e);
346 *poolindex = e - fm->pool_per_worker[my_cpu_number];
347 fm->hash_per_worker[my_cpu_number][h] = *poolindex;
351 if (fm->passive_timer > 0)
353 e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
354 (fm->timers_per_worker[my_cpu_number], *poolindex, 0,
361 add_to_flow_record_state (vlib_main_t * vm, vlib_node_runtime_t * node,
362 flowprobe_main_t * fm, vlib_buffer_t * b,
363 timestamp_nsec_t timestamp, u16 length,
364 flowprobe_variant_t which, flowprobe_trace_t * t)
369 u32 my_cpu_number = vm->thread_index;
372 flowprobe_record_t flags = fm->context[which].flags;
373 bool collect_ip4 = false, collect_ip6 = false;
375 ethernet_header_t *eth = vlib_buffer_get_current (b);
376 u16 ethertype = clib_net_to_host_u16 (eth->type);
378 flowprobe_key_t k = {};
380 ip4_header_t *ip4 = 0;
381 ip6_header_t *ip6 = 0;
382 udp_header_t *udp = 0;
383 tcp_header_t *tcp = 0;
386 if (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4)
388 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
389 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
392 k.rx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
393 k.tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
397 if (flags & FLOW_RECORD_L2)
399 clib_memcpy_fast (k.src_mac, eth->src_address, 6);
400 clib_memcpy_fast (k.dst_mac, eth->dst_address, 6);
401 k.ethertype = ethertype;
403 if (ethertype == ETHERNET_TYPE_VLAN)
406 ethernet_vlan_header_tv_t *ethv =
407 (ethernet_vlan_header_tv_t *) (&(eth->type));
408 /*Q in Q possibility */
409 while (clib_net_to_host_u16 (ethv->type) == ETHERNET_TYPE_VLAN)
413 k.ethertype = ethertype = clib_net_to_host_u16 ((ethv)->type);
415 if (collect_ip6 && ethertype == ETHERNET_TYPE_IP6)
417 ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
418 if (flags & FLOW_RECORD_L3)
420 k.src_address.as_u64[0] = ip6->src_address.as_u64[0];
421 k.src_address.as_u64[1] = ip6->src_address.as_u64[1];
422 k.dst_address.as_u64[0] = ip6->dst_address.as_u64[0];
423 k.dst_address.as_u64[1] = ip6->dst_address.as_u64[1];
425 k.protocol = ip6->protocol;
426 if (k.protocol == IP_PROTOCOL_UDP)
427 udp = (udp_header_t *) (ip6 + 1);
428 else if (k.protocol == IP_PROTOCOL_TCP)
429 tcp = (tcp_header_t *) (ip6 + 1);
431 octets = clib_net_to_host_u16 (ip6->payload_length)
432 + sizeof (ip6_header_t);
434 if (collect_ip4 && ethertype == ETHERNET_TYPE_IP4)
436 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
437 if (flags & FLOW_RECORD_L3)
439 k.src_address.ip4.as_u32 = ip4->src_address.as_u32;
440 k.dst_address.ip4.as_u32 = ip4->dst_address.as_u32;
442 k.protocol = ip4->protocol;
443 if ((flags & FLOW_RECORD_L4) && k.protocol == IP_PROTOCOL_UDP)
444 udp = (udp_header_t *) (ip4 + 1);
445 else if ((flags & FLOW_RECORD_L4) && k.protocol == IP_PROTOCOL_TCP)
446 tcp = (tcp_header_t *) (ip4 + 1);
448 octets = clib_net_to_host_u16 (ip4->length);
453 k.src_port = udp->src_port;
454 k.dst_port = udp->dst_port;
458 k.src_port = tcp->src_port;
459 k.dst_port = tcp->dst_port;
460 tcp_flags = tcp->flags;
465 t->rx_sw_if_index = k.rx_sw_if_index;
466 t->tx_sw_if_index = k.tx_sw_if_index;
467 clib_memcpy_fast (t->src_mac, k.src_mac, 6);
468 clib_memcpy_fast (t->dst_mac, k.dst_mac, 6);
469 t->ethertype = k.ethertype;
470 t->src_address.ip4.as_u32 = k.src_address.ip4.as_u32;
471 t->dst_address.ip4.as_u32 = k.dst_address.ip4.as_u32;
472 t->protocol = k.protocol;
473 t->src_port = k.src_port;
474 t->dst_port = k.dst_port;
478 flowprobe_entry_t *e = 0;
479 f64 now = vlib_time_now (vm);
480 if (fm->active_timer > 0)
483 bool collision = false;
485 e = flowprobe_lookup (my_cpu_number, &k, &poolindex, &collision);
488 /* Flush data and clean up entry for reuse. */
490 flowprobe_export_entry (vm, e);
492 e->flow_start = timestamp;
493 vlib_node_increment_counter (vm, node->node_index,
494 FLOWPROBE_ERROR_COLLISION, 1);
496 if (!e) /* Create new entry */
498 e = flowprobe_create (my_cpu_number, &k, &poolindex);
499 e->last_exported = now;
500 e->flow_start = timestamp;
505 e = &fm->stateless_entry[my_cpu_number];
513 e->octetcount += octets;
514 e->last_updated = now;
515 e->flow_end = timestamp;
516 e->prot.tcp.flags |= tcp_flags;
517 if (fm->active_timer == 0
518 || (now > e->last_exported + fm->active_timer))
519 flowprobe_export_entry (vm, e);
524 flowprobe_get_headersize (void)
526 return sizeof (ip4_header_t) + sizeof (udp_header_t) +
527 sizeof (ipfix_message_header_t) + sizeof (ipfix_set_header_t);
531 flowprobe_export_send (vlib_main_t * vm, vlib_buffer_t * b0,
532 flowprobe_variant_t which)
534 flowprobe_main_t *fm = &flowprobe_main;
535 flow_report_main_t *frm = &flow_report_main;
536 ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, 0);
538 ip4_ipfix_template_packet_t *tp;
539 ipfix_set_header_t *s;
540 ipfix_message_header_t *h;
543 flowprobe_record_t flags = fm->context[which].flags;
544 u32 my_cpu_number = vm->thread_index;
547 flow_report_stream_t *stream;
549 /* Nothing to send */
550 if (fm->context[which].next_record_offset_per_worker[my_cpu_number] <=
551 flowprobe_get_headersize ())
554 u32 i, index = vec_len (exp->streams);
555 for (i = 0; i < index; i++)
556 if (exp->streams[i].domain_id == 1)
561 if (i == vec_len (exp->streams))
563 vec_validate (exp->streams, index);
564 exp->streams[index].domain_id = 1;
566 stream = &exp->streams[index];
568 tp = vlib_buffer_get_current (b0);
569 ip = (ip4_header_t *) & tp->ip4;
570 udp = (udp_header_t *) (ip + 1);
571 h = (ipfix_message_header_t *) (udp + 1);
572 s = (ipfix_set_header_t *) (h + 1);
574 ip->ip_version_and_header_length = 0x45;
576 ip->protocol = IP_PROTOCOL_UDP;
577 ip->flags_and_fragment_offset = 0;
578 ip->src_address.as_u32 = exp->src_address.ip.ip4.as_u32;
579 ip->dst_address.as_u32 = exp->ipfix_collector.ip.ip4.as_u32;
580 udp->src_port = clib_host_to_net_u16 (stream->src_port);
581 udp->dst_port = clib_host_to_net_u16 (exp->collector_port);
584 /* FIXUP: message header export_time */
585 h->export_time = (u32)
586 (((f64) frm->unix_time_0) +
587 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
588 h->export_time = clib_host_to_net_u32 (h->export_time);
589 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
591 /* FIXUP: message header sequence_number */
592 h->sequence_number = stream->sequence_number++;
593 h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
595 s->set_id_length = ipfix_set_id_length (fm->template_reports[flags],
597 (sizeof (*ip) + sizeof (*udp) +
599 h->version_length = version_length (b0->current_length -
600 (sizeof (*ip) + sizeof (*udp)));
602 ip->length = clib_host_to_net_u16 (b0->current_length);
604 ip->checksum = ip4_header_checksum (ip);
605 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
607 if (exp->udp_checksum)
609 /* RFC 7011 section 10.3.2. */
610 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
611 if (udp->checksum == 0)
612 udp->checksum = 0xffff;
615 ASSERT (ip4_header_checksum_is_valid (ip));
617 /* Find or allocate a frame */
618 f = fm->context[which].frames_per_worker[my_cpu_number];
619 if (PREDICT_FALSE (f == 0))
622 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
623 fm->context[which].frames_per_worker[my_cpu_number] = f;
624 u32 bi0 = vlib_get_buffer_index (vm, b0);
626 /* Enqueue the buffer */
627 to_next = vlib_frame_vector_args (f);
632 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
633 vlib_node_increment_counter (vm, flowprobe_l2_node.index,
634 FLOWPROBE_ERROR_EXPORTED_PACKETS, 1);
636 fm->context[which].frames_per_worker[my_cpu_number] = 0;
637 fm->context[which].buffers_per_worker[my_cpu_number] = 0;
638 fm->context[which].next_record_offset_per_worker[my_cpu_number] =
639 flowprobe_get_headersize ();
642 static vlib_buffer_t *
643 flowprobe_get_buffer (vlib_main_t * vm, flowprobe_variant_t which)
645 flowprobe_main_t *fm = &flowprobe_main;
646 ipfix_exporter_t *exp = pool_elt_at_index (flow_report_main.exporters, 0);
649 u32 my_cpu_number = vm->thread_index;
651 /* Find or allocate a buffer */
652 b0 = fm->context[which].buffers_per_worker[my_cpu_number];
654 /* Need to allocate a buffer? */
655 if (PREDICT_FALSE (b0 == 0))
657 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
659 vlib_node_increment_counter (vm, flowprobe_l2_node.index,
660 FLOWPROBE_ERROR_BUFFER, 1);
664 /* Initialize the buffer */
665 b0 = fm->context[which].buffers_per_worker[my_cpu_number] =
666 vlib_get_buffer (vm, bi0);
668 b0->current_data = 0;
669 b0->current_length = flowprobe_get_headersize ();
671 (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
672 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
673 vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index;
674 fm->context[which].next_record_offset_per_worker[my_cpu_number] =
682 flowprobe_export_entry (vlib_main_t * vm, flowprobe_entry_t * e)
684 u32 my_cpu_number = vm->thread_index;
685 flowprobe_main_t *fm = &flowprobe_main;
686 ipfix_exporter_t *exp = pool_elt_at_index (flow_report_main.exporters, 0);
688 bool collect_ip4 = false, collect_ip6 = false;
689 flowprobe_variant_t which = e->key.which;
690 flowprobe_record_t flags = fm->context[which].flags;
692 fm->context[which].next_record_offset_per_worker[my_cpu_number];
694 if (offset < flowprobe_get_headersize ())
695 offset = flowprobe_get_headersize ();
697 b0 = flowprobe_get_buffer (vm, which);
698 /* No available buffer, what to do... */
702 if (flags & FLOW_RECORD_L3)
704 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
705 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
708 offset += flowprobe_common_add (b0, e, offset);
710 if (flags & FLOW_RECORD_L2)
711 offset += flowprobe_l2_add (b0, e, offset);
713 offset += flowprobe_l3_ip6_add (b0, e, offset);
715 offset += flowprobe_l3_ip4_add (b0, e, offset);
716 if (flags & FLOW_RECORD_L4)
717 offset += flowprobe_l4_add (b0, e, offset);
719 /* Reset per flow-export counters */
722 e->last_exported = vlib_time_now (vm);
724 b0->current_length = offset;
726 fm->context[which].next_record_offset_per_worker[my_cpu_number] = offset;
727 /* Time to flush the buffer? */
728 if (offset + fm->template_size[flags] > exp->path_mtu)
729 flowprobe_export_send (vm, b0, which);
733 flowprobe_node_fn (vlib_main_t * vm,
734 vlib_node_runtime_t * node, vlib_frame_t * frame,
735 flowprobe_variant_t which)
737 u32 n_left_from, *from, *to_next;
738 flowprobe_next_t next_index;
739 flowprobe_main_t *fm = &flowprobe_main;
740 timestamp_nsec_t timestamp;
742 unix_time_now_nsec_fraction (×tamp.sec, ×tamp.nsec);
744 from = vlib_frame_vector_args (frame);
745 n_left_from = frame->n_vectors;
746 next_index = node->cached_next_index;
748 while (n_left_from > 0)
752 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
754 while (n_left_from >= 4 && n_left_to_next >= 2)
756 u32 next0 = FLOWPROBE_NEXT_DROP;
757 u32 next1 = FLOWPROBE_NEXT_DROP;
760 vlib_buffer_t *b0, *b1;
762 /* Prefetch next iteration. */
764 vlib_buffer_t *p2, *p3;
766 p2 = vlib_get_buffer (vm, from[2]);
767 p3 = vlib_get_buffer (vm, from[3]);
769 vlib_prefetch_buffer_header (p2, LOAD);
770 vlib_prefetch_buffer_header (p3, LOAD);
772 clib_prefetch_store (p2->data);
773 clib_prefetch_store (p3->data);
776 /* speculatively enqueue b0 and b1 to the current next frame */
777 to_next[0] = bi0 = from[0];
778 to_next[1] = bi1 = from[1];
784 b0 = vlib_get_buffer (vm, bi0);
785 b1 = vlib_get_buffer (vm, bi1);
787 vnet_feature_next (&next0, b0);
788 vnet_feature_next (&next1, b1);
790 len0 = vlib_buffer_length_in_chain (vm, b0);
791 ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
792 u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
794 if (PREDICT_TRUE ((b0->flags & VNET_BUFFER_F_FLOW_REPORT) == 0))
795 add_to_flow_record_state (vm, node, fm, b0, timestamp, len0,
796 flowprobe_get_variant
797 (which, fm->context[which].flags,
800 len1 = vlib_buffer_length_in_chain (vm, b1);
801 ethernet_header_t *eh1 = vlib_buffer_get_current (b1);
802 u16 ethertype1 = clib_net_to_host_u16 (eh1->type);
804 if (PREDICT_TRUE ((b1->flags & VNET_BUFFER_F_FLOW_REPORT) == 0))
805 add_to_flow_record_state (vm, node, fm, b1, timestamp, len1,
806 flowprobe_get_variant
807 (which, fm->context[which].flags,
810 /* verify speculative enqueues, maybe switch current next frame */
811 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
812 to_next, n_left_to_next,
813 bi0, bi1, next0, next1);
816 while (n_left_from > 0 && n_left_to_next > 0)
820 u32 next0 = FLOWPROBE_NEXT_DROP;
823 /* speculatively enqueue b0 to the current next frame */
831 b0 = vlib_get_buffer (vm, bi0);
833 vnet_feature_next (&next0, b0);
835 len0 = vlib_buffer_length_in_chain (vm, b0);
836 ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
837 u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
839 if (PREDICT_TRUE ((b0->flags & VNET_BUFFER_F_FLOW_REPORT) == 0))
841 flowprobe_trace_t *t = 0;
842 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
843 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
844 t = vlib_add_trace (vm, node, b0, sizeof (*t));
846 add_to_flow_record_state (vm, node, fm, b0, timestamp, len0,
847 flowprobe_get_variant
848 (which, fm->context[which].flags,
852 /* verify speculative enqueue, maybe switch current next frame */
853 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
854 to_next, n_left_to_next,
858 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
860 return frame->n_vectors;
864 flowprobe_ip4_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_IP4);
871 flowprobe_ip6_node_fn (vlib_main_t * vm,
872 vlib_node_runtime_t * node, vlib_frame_t * frame)
874 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP6);
878 flowprobe_l2_node_fn (vlib_main_t * vm,
879 vlib_node_runtime_t * node, vlib_frame_t * frame)
881 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_L2);
885 flush_record (flowprobe_variant_t which)
887 vlib_main_t *vm = vlib_get_main ();
888 vlib_buffer_t *b = flowprobe_get_buffer (vm, which);
890 flowprobe_export_send (vm, b, which);
894 flowprobe_flush_callback_ip4 (void)
896 flush_record (FLOW_VARIANT_IP4);
900 flowprobe_flush_callback_ip6 (void)
902 flush_record (FLOW_VARIANT_IP6);
906 flowprobe_flush_callback_l2 (void)
908 flush_record (FLOW_VARIANT_L2);
909 flush_record (FLOW_VARIANT_L2_IP4);
910 flush_record (FLOW_VARIANT_L2_IP6);
915 flowprobe_delete_by_index (u32 my_cpu_number, u32 poolindex)
917 flowprobe_main_t *fm = &flowprobe_main;
918 flowprobe_entry_t *e;
921 e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], poolindex);
924 h = flowprobe_hash (&e->key);
927 fm->hash_per_worker[my_cpu_number][h] = ~0;
929 pool_put_index (fm->pool_per_worker[my_cpu_number], poolindex);
933 /* Per worker process processing the active/passive expired entries */
935 flowprobe_walker_process (vlib_main_t * vm,
936 vlib_node_runtime_t * rt, vlib_frame_t * f)
938 flowprobe_main_t *fm = &flowprobe_main;
939 flowprobe_entry_t *e;
940 ipfix_exporter_t *exp = pool_elt_at_index (flow_report_main.exporters, 0);
943 * $$$$ Remove this check from here and track FRM status and disable
944 * this process if required.
946 if (ip_address_is_zero (&exp->ipfix_collector) ||
947 ip_address_is_zero (&exp->src_address))
952 fm->disabled = false;
954 u32 cpu_index = os_get_thread_index ();
955 u32 *to_be_removed = 0, *i;
958 * Tick the timer when required and process the vector of expired
961 f64 start_time = vlib_time_now (vm);
964 tw_timer_expire_timers_2t_1w_2048sl (fm->timers_per_worker[cpu_index],
967 vec_foreach (i, fm->expired_passive_per_worker[cpu_index])
970 f64 now = vlib_time_now (vm);
971 if (now > start_time + 100e-6
972 || exported > FLOW_MAXIMUM_EXPORT_ENTRIES - 1)
975 if (pool_is_free_index (fm->pool_per_worker[cpu_index], *i))
977 clib_warning ("Element is %d is freed already\n", *i);
981 e = pool_elt_at_index (fm->pool_per_worker[cpu_index], *i);
983 /* Check last update timestamp. If it is longer than passive time nuke
984 * entry. Otherwise restart timer with what's left
985 * Premature passive timer by more than 10%
987 if ((now - e->last_updated) < (u64) (fm->passive_timer * 0.9))
989 u64 delta = fm->passive_timer - (now - e->last_updated);
990 e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
991 (fm->timers_per_worker[cpu_index], *i, 0, delta);
993 else /* Nuke entry */
995 vec_add1 (to_be_removed, *i);
997 /* If anything to report send it to the exporter */
998 if (e->packetcount && now > e->last_exported + fm->active_timer)
1001 flowprobe_export_entry (vm, e);
1006 vec_delete (fm->expired_passive_per_worker[cpu_index], count, 0);
1008 vec_foreach (i, to_be_removed) flowprobe_delete_by_index (cpu_index, *i);
1009 vec_free (to_be_removed);
1015 VLIB_REGISTER_NODE (flowprobe_ip4_node) = {
1016 .function = flowprobe_ip4_node_fn,
1017 .name = "flowprobe-ip4",
1018 .vector_size = sizeof (u32),
1019 .format_trace = format_flowprobe_trace,
1020 .type = VLIB_NODE_TYPE_INTERNAL,
1021 .n_errors = ARRAY_LEN(flowprobe_error_strings),
1022 .error_strings = flowprobe_error_strings,
1023 .n_next_nodes = FLOWPROBE_N_NEXT,
1024 .next_nodes = FLOWPROBE_NEXT_NODES,
1026 VLIB_REGISTER_NODE (flowprobe_ip6_node) = {
1027 .function = flowprobe_ip6_node_fn,
1028 .name = "flowprobe-ip6",
1029 .vector_size = sizeof (u32),
1030 .format_trace = format_flowprobe_trace,
1031 .type = VLIB_NODE_TYPE_INTERNAL,
1032 .n_errors = ARRAY_LEN(flowprobe_error_strings),
1033 .error_strings = flowprobe_error_strings,
1034 .n_next_nodes = FLOWPROBE_N_NEXT,
1035 .next_nodes = FLOWPROBE_NEXT_NODES,
1037 VLIB_REGISTER_NODE (flowprobe_l2_node) = {
1038 .function = flowprobe_l2_node_fn,
1039 .name = "flowprobe-l2",
1040 .vector_size = sizeof (u32),
1041 .format_trace = format_flowprobe_trace,
1042 .type = VLIB_NODE_TYPE_INTERNAL,
1043 .n_errors = ARRAY_LEN(flowprobe_error_strings),
1044 .error_strings = flowprobe_error_strings,
1045 .n_next_nodes = FLOWPROBE_N_NEXT,
1046 .next_nodes = FLOWPROBE_NEXT_NODES,
1048 VLIB_REGISTER_NODE (flowprobe_walker_node) = {
1049 .function = flowprobe_walker_process,
1050 .name = "flowprobe-walker",
1051 .type = VLIB_NODE_TYPE_INPUT,
1052 .state = VLIB_NODE_STATE_INTERRUPT,
1057 * fd.io coding-style-patch-verification: ON
1060 * eval: (c-set-style "gnu")