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 <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>
25 static void flowprobe_export_entry (vlib_main_t * vm, flowprobe_entry_t * e);
28 * @file flow record generator graph node
33 /** interface handle */
36 /** packet timestamp */
38 /** size of the buffer */
48 ip46_address_t src_address;
49 ip46_address_t dst_address;
57 flowprobe_variant_t which;
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",
68 /* packet trace format function */
70 format_flowprobe_trace (u8 * s, va_list * args)
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);
78 "FLOWPROBE[%s]: rx_sw_if_index %d, tx_sw_if_index %d, "
79 "timestamp %lld, size %d", flowprobe_variant_strings[t->which],
80 t->rx_sw_if_index, t->tx_sw_if_index,
81 t->timestamp, t->buffer_size);
83 if (t->which == FLOW_VARIANT_L2)
84 s = format (s, "\n%U -> %U", format_white_space, indent,
85 format_ethernet_address, &t->src_mac,
86 format_ethernet_address, &t->dst_mac);
89 && (t->which == FLOW_VARIANT_L2_IP4 || t->which == FLOW_VARIANT_IP4
90 || t->which == FLOW_VARIANT_L2_IP6 || t->which == FLOW_VARIANT_IP6))
92 format (s, "\n%U%U: %U -> %U", format_white_space, indent,
93 format_ip_protocol, t->protocol, format_ip46_address,
94 &t->src_address, IP46_TYPE_ANY, format_ip46_address,
95 &t->dst_address, IP46_TYPE_ANY);
99 vlib_node_registration_t flowprobe_ip4_node;
100 vlib_node_registration_t flowprobe_ip6_node;
101 vlib_node_registration_t flowprobe_l2_node;
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")
112 #define _(sym,str) FLOWPROBE_ERROR_##sym,
113 foreach_flowprobe_error
118 static char *flowprobe_error_strings[] = {
119 #define _(sym,string) string,
120 foreach_flowprobe_error
127 FLOWPROBE_NEXT_IP4_LOOKUP,
131 #define FLOWPROBE_NEXT_NODES { \
132 [FLOWPROBE_NEXT_DROP] = "error-drop", \
133 [FLOWPROBE_NEXT_IP4_LOOKUP] = "ip4-lookup", \
136 static inline flowprobe_variant_t
137 flowprobe_get_variant (flowprobe_variant_t which,
138 flowprobe_record_t flags, u16 ethertype)
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;
148 flowprobe_common_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
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);
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);
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);
167 return offset - start;
171 flowprobe_l2_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
175 /* src mac address */
176 clib_memcpy (to_b->data + offset, &e->key.src_mac, 6);
179 /* dst mac address */
180 clib_memcpy (to_b->data + offset, &e->key.dst_mac, 6);
184 clib_memcpy (to_b->data + offset, &e->key.ethertype, 2);
187 return offset - start;
191 flowprobe_l3_ip6_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
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);
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);
206 to_b->data[offset++] = e->key.protocol;
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);
213 return offset - start;
217 flowprobe_l3_ip4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
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);
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);
232 to_b->data[offset++] = e->key.protocol;
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);
239 return offset - start;
243 flowprobe_l4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
248 clib_memcpy (to_b->data + offset, &e->key.src_port, 2);
252 clib_memcpy (to_b->data + offset, &e->key.dst_port, 2);
255 return offset - start;
259 flowprobe_hash (flowprobe_key_t * k)
261 flowprobe_main_t *fm = &flowprobe_main;
264 #ifdef clib_crc32c_uses_intrinsics
265 h = clib_crc32c ((u8 *) k->as_u32, FLOWPROBE_KEY_IN_U32);
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);
272 return h >> (32 - fm->ht_log2len);
276 flowprobe_lookup (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex,
279 flowprobe_main_t *fm = &flowprobe_main;
280 flowprobe_entry_t *e;
283 h = (fm->active_timer) ? flowprobe_hash (k) : 0;
285 /* Lookup in the flow state pool */
286 *poolindex = fm->hash_per_worker[my_cpu_number][h];
287 if (*poolindex != ~0)
289 e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], *poolindex);
292 /* Verify key or report collision */
293 if (memcmp (k, &e->key, sizeof (flowprobe_key_t)))
303 flowprobe_create (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex)
305 flowprobe_main_t *fm = &flowprobe_main;
308 flowprobe_entry_t *e;
311 h = (fm->active_timer) ? flowprobe_hash (k) : 0;
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;
319 if (fm->passive_timer > 0)
321 e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
322 (fm->timers_per_worker[my_cpu_number], *poolindex, 0,
329 add_to_flow_record_state (vlib_main_t * vm, vlib_node_runtime_t * node,
330 flowprobe_main_t * fm, vlib_buffer_t * b,
331 u64 timestamp, u16 length,
332 flowprobe_variant_t which, flowprobe_trace_t * t)
337 u32 my_cpu_number = vm->thread_index;
340 flowprobe_record_t flags = fm->context[which].flags;
341 bool collect_ip4 = false, collect_ip6 = false;
343 ethernet_header_t *eth = vlib_buffer_get_current (b);
344 u16 ethertype = clib_net_to_host_u16 (eth->type);
346 flowprobe_key_t k = { {0} };
348 ip4_header_t *ip4 = 0;
349 ip6_header_t *ip6 = 0;
350 udp_header_t *udp = 0;
352 if (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4)
354 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
355 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
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];
363 if (flags & FLOW_RECORD_L2)
365 clib_memcpy (k.src_mac, eth->src_address, 6);
366 clib_memcpy (k.dst_mac, eth->dst_address, 6);
367 k.ethertype = ethertype;
369 if (collect_ip6 && ethertype == ETHERNET_TYPE_IP6)
371 ip6 = (ip6_header_t *) (eth + 1);
372 udp = (udp_header_t *) (ip6 + 1);
373 if (flags & FLOW_RECORD_L3)
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];
380 k.protocol = ip6->protocol;
381 octets = clib_net_to_host_u16 (ip6->payload_length)
382 + sizeof (ip6_header_t);
384 if (collect_ip4 && ethertype == ETHERNET_TYPE_IP4)
386 ip4 = (ip4_header_t *) (eth + 1);
387 udp = (udp_header_t *) (ip4 + 1);
388 if (flags & FLOW_RECORD_L3)
390 k.src_address.ip4.as_u32 = ip4->src_address.as_u32;
391 k.dst_address.ip4.as_u32 = ip4->dst_address.as_u32;
393 k.protocol = ip4->protocol;
394 octets = clib_net_to_host_u16 (ip4->length);
396 if ((flags & FLOW_RECORD_L4) && udp &&
397 (k.protocol == IP_PROTOCOL_TCP || k.protocol == IP_PROTOCOL_UDP))
399 k.src_port = udp->src_port;
400 k.dst_port = udp->dst_port;
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;
418 flowprobe_entry_t *e = 0;
419 f64 now = vlib_time_now (vm);
420 if (fm->active_timer > 0)
423 bool collision = false;
425 e = flowprobe_lookup (my_cpu_number, &k, &poolindex, &collision);
428 /* Flush data and clean up entry for reuse. */
430 flowprobe_export_entry (vm, e);
432 vlib_node_increment_counter (vm, node->node_index,
433 FLOWPROBE_ERROR_COLLISION, 1);
435 if (!e) /* Create new entry */
437 e = flowprobe_create (my_cpu_number, &k, &poolindex);
438 e->last_exported = now;
443 e = &fm->stateless_entry[my_cpu_number];
451 e->octetcount += octets;
452 e->last_updated = now;
454 if (fm->active_timer == 0
455 || (now > e->last_exported + fm->active_timer))
456 flowprobe_export_entry (vm, e);
461 flowprobe_get_headersize (void)
463 return sizeof (ip4_header_t) + sizeof (udp_header_t) +
464 sizeof (ipfix_message_header_t) + sizeof (ipfix_set_header_t);
468 flowprobe_export_send (vlib_main_t * vm, vlib_buffer_t * b0,
469 flowprobe_variant_t which)
471 flowprobe_main_t *fm = &flowprobe_main;
472 flow_report_main_t *frm = &flow_report_main;
474 ip4_ipfix_template_packet_t *tp;
475 ipfix_set_header_t *s;
476 ipfix_message_header_t *h;
479 flowprobe_record_t flags = fm->context[which].flags;
480 u32 my_cpu_number = vm->thread_index;
483 flow_report_stream_t *stream;
485 /* Nothing to send */
486 if (fm->context[which].next_record_offset_per_worker[my_cpu_number] <=
487 flowprobe_get_headersize ())
490 u32 i, index = vec_len (frm->streams);
491 for (i = 0; i < index; i++)
492 if (frm->streams[i].domain_id == 1)
497 if (i == vec_len (frm->streams))
499 vec_validate (frm->streams, index);
500 frm->streams[index].domain_id = 1;
502 stream = &frm->streams[index];
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);
510 ip->ip_version_and_header_length = 0x45;
512 ip->protocol = IP_PROTOCOL_UDP;
513 ip->flags_and_fragment_offset = 0;
514 ip->src_address.as_u32 = frm->src_address.as_u32;
515 ip->dst_address.as_u32 = frm->ipfix_collector.as_u32;
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);
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);
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);
531 s->set_id_length = ipfix_set_id_length (fm->template_reports[flags],
533 (sizeof (*ip) + sizeof (*udp) +
535 h->version_length = version_length (b0->current_length -
536 (sizeof (*ip) + sizeof (*udp)));
538 ip->length = clib_host_to_net_u16 (b0->current_length);
540 ip->checksum = ip4_header_checksum (ip);
541 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
543 if (frm->udp_checksum)
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;
551 ASSERT (ip->checksum == ip4_header_checksum (ip));
553 /* Find or allocate a frame */
554 f = fm->context[which].frames_per_worker[my_cpu_number];
555 if (PREDICT_FALSE (f == 0))
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);
562 /* Enqueue the buffer */
563 to_next = vlib_frame_vector_args (f);
568 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
569 vlib_node_increment_counter (vm, flowprobe_l2_node.index,
570 FLOWPROBE_ERROR_EXPORTED_PACKETS, 1);
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] =
575 flowprobe_get_headersize ();
578 static vlib_buffer_t *
579 flowprobe_get_buffer (vlib_main_t * vm, flowprobe_variant_t which)
581 flowprobe_main_t *fm = &flowprobe_main;
582 flow_report_main_t *frm = &flow_report_main;
585 vlib_buffer_free_list_t *fl;
586 u32 my_cpu_number = vm->thread_index;
588 /* Find or allocate a buffer */
589 b0 = fm->context[which].buffers_per_worker[my_cpu_number];
591 /* Need to allocate a buffer? */
592 if (PREDICT_FALSE (b0 == 0))
594 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
596 vlib_node_increment_counter (vm, flowprobe_l2_node.index,
597 FLOWPROBE_ERROR_BUFFER, 1);
601 /* Initialize the buffer */
602 b0 = fm->context[which].buffers_per_worker[my_cpu_number] =
603 vlib_get_buffer (vm, bi0);
605 vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
606 vlib_buffer_init_for_free_list (b0, fl);
607 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
609 b0->current_data = 0;
610 b0->current_length = flowprobe_get_headersize ();
611 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_FLOW_REPORT);
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] =
622 flowprobe_export_entry (vlib_main_t * vm, flowprobe_entry_t * e)
624 u32 my_cpu_number = vm->thread_index;
625 flowprobe_main_t *fm = &flowprobe_main;
626 flow_report_main_t *frm = &flow_report_main;
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;
632 fm->context[which].next_record_offset_per_worker[my_cpu_number];
634 if (offset < flowprobe_get_headersize ())
635 offset = flowprobe_get_headersize ();
637 b0 = flowprobe_get_buffer (vm, which);
638 /* No available buffer, what to do... */
642 if (flags & FLOW_RECORD_L3)
644 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
645 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
648 offset += flowprobe_common_add (b0, e, offset);
650 if (flags & FLOW_RECORD_L2)
651 offset += flowprobe_l2_add (b0, e, offset);
653 offset += flowprobe_l3_ip6_add (b0, e, offset);
655 offset += flowprobe_l3_ip4_add (b0, e, offset);
656 if (flags & FLOW_RECORD_L4)
657 offset += flowprobe_l4_add (b0, e, offset);
659 /* Reset per flow-export counters */
662 e->last_exported = vlib_time_now (vm);
664 b0->current_length = offset;
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);
673 flowprobe_node_fn (vlib_main_t * vm,
674 vlib_node_runtime_t * node, vlib_frame_t * frame,
675 flowprobe_variant_t which)
677 u32 n_left_from, *from, *to_next;
678 flowprobe_next_t next_index;
679 flowprobe_main_t *fm = &flowprobe_main;
682 now = (u64) ((vlib_time_now (vm) - fm->vlib_time_0) * 1e9);
683 now += fm->nanosecond_time_0;
685 from = vlib_frame_vector_args (frame);
686 n_left_from = frame->n_vectors;
687 next_index = node->cached_next_index;
689 while (n_left_from > 0)
693 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
695 while (n_left_from >= 4 && n_left_to_next >= 2)
697 u32 next0 = FLOWPROBE_NEXT_DROP;
698 u32 next1 = FLOWPROBE_NEXT_DROP;
701 vlib_buffer_t *b0, *b1;
703 /* Prefetch next iteration. */
705 vlib_buffer_t *p2, *p3;
707 p2 = vlib_get_buffer (vm, from[2]);
708 p3 = vlib_get_buffer (vm, from[3]);
710 vlib_prefetch_buffer_header (p2, LOAD);
711 vlib_prefetch_buffer_header (p3, LOAD);
713 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
714 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
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];
725 b0 = vlib_get_buffer (vm, bi0);
726 b1 = vlib_get_buffer (vm, bi1);
728 vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_TX],
730 vnet_feature_next (vnet_buffer (b1)->sw_if_index[VLIB_TX],
733 len0 = vlib_buffer_length_in_chain (vm, b0);
734 ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
735 u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
737 if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_FLOW_REPORT) == 0))
738 add_to_flow_record_state (vm, node, fm, b0, now, len0,
739 flowprobe_get_variant
740 (which, fm->context[which].flags,
743 len1 = vlib_buffer_length_in_chain (vm, b1);
744 ethernet_header_t *eh1 = vlib_buffer_get_current (b1);
745 u16 ethertype1 = clib_net_to_host_u16 (eh1->type);
747 if (PREDICT_TRUE ((b1->flags & VLIB_BUFFER_FLOW_REPORT) == 0))
748 add_to_flow_record_state (vm, node, fm, b1, now, len1,
749 flowprobe_get_variant
750 (which, fm->context[which].flags,
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);
759 while (n_left_from > 0 && n_left_to_next > 0)
763 u32 next0 = FLOWPROBE_NEXT_DROP;
766 /* speculatively enqueue b0 to the current next frame */
774 b0 = vlib_get_buffer (vm, bi0);
776 vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_TX],
779 len0 = vlib_buffer_length_in_chain (vm, b0);
780 ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
781 u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
783 if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_FLOW_REPORT) == 0))
785 flowprobe_trace_t *t = 0;
786 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
787 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
788 t = vlib_add_trace (vm, node, b0, sizeof (*t));
790 add_to_flow_record_state (vm, node, fm, b0, now, len0,
791 flowprobe_get_variant
792 (which, fm->context[which].flags,
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,
802 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
804 return frame->n_vectors;
808 flowprobe_ip4_node_fn (vlib_main_t * vm,
809 vlib_node_runtime_t * node, vlib_frame_t * frame)
811 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP4);
815 flowprobe_ip6_node_fn (vlib_main_t * vm,
816 vlib_node_runtime_t * node, vlib_frame_t * frame)
818 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP6);
822 flowprobe_l2_node_fn (vlib_main_t * vm,
823 vlib_node_runtime_t * node, vlib_frame_t * frame)
825 return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_L2);
829 flush_record (flowprobe_variant_t which)
831 vlib_main_t *vm = vlib_get_main ();
832 vlib_buffer_t *b = flowprobe_get_buffer (vm, which);
834 flowprobe_export_send (vm, b, which);
838 flowprobe_flush_callback_ip4 (void)
840 flush_record (FLOW_VARIANT_IP4);
844 flowprobe_flush_callback_ip6 (void)
846 flush_record (FLOW_VARIANT_IP6);
850 flowprobe_flush_callback_l2 (void)
852 flush_record (FLOW_VARIANT_L2);
853 flush_record (FLOW_VARIANT_L2_IP4);
854 flush_record (FLOW_VARIANT_L2_IP6);
859 flowprobe_delete_by_index (u32 my_cpu_number, u32 poolindex)
861 flowprobe_main_t *fm = &flowprobe_main;
862 flowprobe_entry_t *e;
865 e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], poolindex);
868 h = flowprobe_hash (&e->key);
871 fm->hash_per_worker[my_cpu_number][h] = ~0;
873 pool_put_index (fm->pool_per_worker[my_cpu_number], poolindex);
877 /* Per worker process processing the active/passive expired entries */
879 flowprobe_walker_process (vlib_main_t * vm,
880 vlib_node_runtime_t * rt, vlib_frame_t * f)
882 flowprobe_main_t *fm = &flowprobe_main;
883 flow_report_main_t *frm = &flow_report_main;
884 flowprobe_entry_t *e;
887 * $$$$ Remove this check from here and track FRM status and disable
888 * this process if required.
890 if (frm->ipfix_collector.as_u32 == 0 || frm->src_address.as_u32 == 0)
895 fm->disabled = false;
897 u32 cpu_index = os_get_thread_index ();
898 u32 *to_be_removed = 0, *i;
901 * Tick the timer when required and process the vector of expired
904 f64 start_time = vlib_time_now (vm);
907 tw_timer_expire_timers_2t_1w_2048sl (fm->timers_per_worker[cpu_index],
910 vec_foreach (i, fm->expired_passive_per_worker[cpu_index])
913 f64 now = vlib_time_now (vm);
914 if (now > start_time + 100e-6
915 || exported > FLOW_MAXIMUM_EXPORT_ENTRIES - 1)
918 if (pool_is_free_index (fm->pool_per_worker[cpu_index], *i))
920 clib_warning ("Element is %d is freed already\n", *i);
924 e = pool_elt_at_index (fm->pool_per_worker[cpu_index], *i);
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%
930 if ((now - e->last_updated) < (fm->passive_timer * 0.9))
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);
936 else /* Nuke entry */
938 vec_add1 (to_be_removed, *i);
940 /* If anything to report send it to the exporter */
941 if (e->packetcount && now > e->last_exported + fm->active_timer)
944 flowprobe_export_entry (vm, e);
949 vec_delete (fm->expired_passive_per_worker[cpu_index], count, 0);
951 vec_foreach (i, to_be_removed) flowprobe_delete_by_index (cpu_index, *i);
952 vec_free (to_be_removed);
958 VLIB_REGISTER_NODE (flowprobe_ip4_node) = {
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,
969 VLIB_REGISTER_NODE (flowprobe_ip6_node) = {
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,
980 VLIB_REGISTER_NODE (flowprobe_l2_node) = {
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,
991 VLIB_REGISTER_NODE (flowprobe_walker_node) = {
992 .function = flowprobe_walker_process,
993 .name = "flowprobe-walker",
994 .type = VLIB_NODE_TYPE_INPUT,
995 .state = VLIB_NODE_STATE_INTERRUPT,
1000 * fd.io coding-style-patch-verification: ON
1003 * eval: (c-set-style "gnu")