2 * flowprobe.c - ipfix probe plugin
4 * Copyright (c) 2016 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.
20 * @brief Per-packet IPFIX flow record generator plugin
22 * This file implements vpp plugin registration mechanics,
23 * debug CLI, and binary API handling.
26 #include <vnet/vnet.h>
27 #include <vpp/app/version.h>
28 #include <vnet/plugin/plugin.h>
29 #include <vnet/udp/udp_local.h>
30 #include <flowprobe/flowprobe.h>
32 #include <vlibapi/api.h>
33 #include <vlibmemory/api.h>
35 /* define message IDs */
36 #include <flowprobe/flowprobe.api_enum.h>
37 #include <flowprobe/flowprobe.api_types.h>
39 flowprobe_main_t flowprobe_main;
40 static vlib_node_registration_t flowprobe_timer_node;
41 uword flowprobe_walker_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
44 #define REPLY_MSG_ID_BASE fm->msg_id_base
45 #include <vlibapi/api_helper_macros.h>
47 /* Define the per-interface configurable features */
49 VNET_FEATURE_INIT (flow_perpacket_ip4, static) =
51 .arc_name = "ip4-output",
52 .node_name = "flowprobe-ip4",
53 .runs_before = VNET_FEATURES ("interface-output"),
56 VNET_FEATURE_INIT (flow_perpacket_ip6, static) =
58 .arc_name = "ip6-output",
59 .node_name = "flowprobe-ip6",
60 .runs_before = VNET_FEATURES ("interface-output"),
63 VNET_FEATURE_INIT (flow_perpacket_l2, static) =
65 .arc_name = "interface-output",
66 .node_name = "flowprobe-l2",
67 .runs_before = VNET_FEATURES ("interface-tx"),
71 /* Macro to finish up custom dump fns */
72 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
75 vl_print (handle, (char *)s); \
79 static inline ipfix_field_specifier_t *
80 flowprobe_template_ip4_fields (ipfix_field_specifier_t * f)
82 #define flowprobe_template_ip4_field_count() 4
83 /* sourceIpv4Address, TLV type 8, u32 */
84 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
85 sourceIPv4Address, 4);
87 /* destinationIPv4Address, TLV type 12, u32 */
88 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
89 destinationIPv4Address, 4);
91 /* protocolIdentifier, TLV type 4, u8 */
92 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
93 protocolIdentifier, 1);
95 /* octetDeltaCount, TLV type 1, u64 */
96 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
102 static inline ipfix_field_specifier_t *
103 flowprobe_template_ip6_fields (ipfix_field_specifier_t * f)
105 #define flowprobe_template_ip6_field_count() 4
106 /* sourceIpv6Address, TLV type 27, 16 octets */
107 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
108 sourceIPv6Address, 16);
110 /* destinationIPv6Address, TLV type 28, 16 octets */
111 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
112 destinationIPv6Address, 16);
114 /* protocolIdentifier, TLV type 4, u8 */
115 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
116 protocolIdentifier, 1);
118 /* octetDeltaCount, TLV type 1, u64 */
119 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
125 static inline ipfix_field_specifier_t *
126 flowprobe_template_l2_fields (ipfix_field_specifier_t * f)
128 #define flowprobe_template_l2_field_count() 3
129 /* sourceMacAddress, TLV type 56, u8[6] we hope */
130 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
131 sourceMacAddress, 6);
133 /* destinationMacAddress, TLV type 80, u8[6] we hope */
134 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
135 destinationMacAddress, 6);
137 /* ethernetType, TLV type 256, u16 */
138 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
144 static inline ipfix_field_specifier_t *
145 flowprobe_template_common_fields (ipfix_field_specifier_t * f)
147 #define flowprobe_template_common_field_count() 5
148 /* ingressInterface, TLV type 10, u32 */
149 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
150 ingressInterface, 4);
153 /* egressInterface, TLV type 14, u32 */
154 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
158 /* packetDeltaCount, TLV type 2, u64 */
159 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
160 packetDeltaCount, 8);
163 /* flowStartNanoseconds, TLV type 156, u64 */
164 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
165 flowStartNanoseconds, 8);
168 /* flowEndNanoseconds, TLV type 157, u64 */
169 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
170 flowEndNanoseconds, 8);
176 static inline ipfix_field_specifier_t *
177 flowprobe_template_l4_fields (ipfix_field_specifier_t * f)
179 #define flowprobe_template_l4_field_count() 3
180 /* sourceTransportPort, TLV type 7, u16 */
181 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
182 sourceTransportPort, 2);
184 /* destinationTransportPort, TLV type 11, u16 */
185 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
186 destinationTransportPort, 2);
188 /* tcpControlBits, TLV type 6, u16 */
189 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
197 * @brief Create an IPFIX template packet rewrite string
198 * @param frm flow_report_main_t *
199 * @param fr flow_report_t *
200 * @param collector_address ip4_address_t * the IPFIX collector address
201 * @param src_address ip4_address_t * the source address we should use
202 * @param collector_port u16 the collector port we should use, host byte order
203 * @returns u8 * vector containing the indicated IPFIX template packet
206 flowprobe_template_rewrite_inline (flow_report_main_t * frm,
208 ip4_address_t * collector_address,
209 ip4_address_t * src_address,
211 flowprobe_variant_t which)
215 ipfix_message_header_t *h;
216 ipfix_set_header_t *s;
217 ipfix_template_header_t *t;
218 ipfix_field_specifier_t *f;
219 ipfix_field_specifier_t *first_field;
221 ip4_ipfix_template_packet_t *tp;
223 flow_report_stream_t *stream;
224 flowprobe_main_t *fm = &flowprobe_main;
225 flowprobe_record_t flags = fr->opaque.as_uword;
226 bool collect_ip4 = false, collect_ip6 = false;
228 stream = &frm->streams[fr->stream_index];
230 if (flags & FLOW_RECORD_L3)
232 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
233 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
234 if (which == FLOW_VARIANT_L2_IP4)
235 flags |= FLOW_RECORD_L2_IP4;
236 if (which == FLOW_VARIANT_L2_IP6)
237 flags |= FLOW_RECORD_L2_IP6;
240 field_count += flowprobe_template_common_field_count ();
241 if (flags & FLOW_RECORD_L2)
242 field_count += flowprobe_template_l2_field_count ();
244 field_count += flowprobe_template_ip4_field_count ();
246 field_count += flowprobe_template_ip6_field_count ();
247 if (flags & FLOW_RECORD_L4)
248 field_count += flowprobe_template_l4_field_count ();
250 /* allocate rewrite space */
252 (rewrite, sizeof (ip4_ipfix_template_packet_t)
253 + field_count * sizeof (ipfix_field_specifier_t) - 1,
254 CLIB_CACHE_LINE_BYTES);
256 tp = (ip4_ipfix_template_packet_t *) rewrite;
257 ip = (ip4_header_t *) & tp->ip4;
258 udp = (udp_header_t *) (ip + 1);
259 h = (ipfix_message_header_t *) (udp + 1);
260 s = (ipfix_set_header_t *) (h + 1);
261 t = (ipfix_template_header_t *) (s + 1);
262 first_field = f = (ipfix_field_specifier_t *) (t + 1);
264 ip->ip_version_and_header_length = 0x45;
266 ip->protocol = IP_PROTOCOL_UDP;
267 ip->src_address.as_u32 = src_address->as_u32;
268 ip->dst_address.as_u32 = collector_address->as_u32;
269 udp->src_port = clib_host_to_net_u16 (stream->src_port);
270 udp->dst_port = clib_host_to_net_u16 (collector_port);
271 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
273 /* FIXUP: message header export_time */
274 /* FIXUP: message header sequence_number */
275 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
277 /* Add TLVs to the template */
278 f = flowprobe_template_common_fields (f);
280 if (flags & FLOW_RECORD_L2)
281 f = flowprobe_template_l2_fields (f);
283 f = flowprobe_template_ip4_fields (f);
285 f = flowprobe_template_ip6_fields (f);
286 if (flags & FLOW_RECORD_L4)
287 f = flowprobe_template_l4_fields (f);
289 /* Back to the template packet... */
290 ip = (ip4_header_t *) & tp->ip4;
291 udp = (udp_header_t *) (ip + 1);
293 ASSERT (f - first_field);
294 /* Field count in this template */
295 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
297 fm->template_size[flags] = (u8 *) f - (u8 *) s;
299 /* set length in octets */
301 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
303 /* message length in octets */
304 h->version_length = version_length ((u8 *) f - (u8 *) h);
306 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
307 ip->checksum = ip4_header_checksum (ip);
313 flowprobe_template_rewrite_ip6 (flow_report_main_t * frm,
315 ip4_address_t * collector_address,
316 ip4_address_t * src_address,
318 ipfix_report_element_t * elts,
319 u32 n_elts, u32 * stream_index)
321 return flowprobe_template_rewrite_inline
322 (frm, fr, collector_address, src_address, collector_port,
327 flowprobe_template_rewrite_ip4 (flow_report_main_t * frm,
329 ip4_address_t * collector_address,
330 ip4_address_t * src_address,
332 ipfix_report_element_t * elts,
333 u32 n_elts, u32 * stream_index)
335 return flowprobe_template_rewrite_inline
336 (frm, fr, collector_address, src_address, collector_port,
341 flowprobe_template_rewrite_l2 (flow_report_main_t * frm,
343 ip4_address_t * collector_address,
344 ip4_address_t * src_address,
346 ipfix_report_element_t * elts,
347 u32 n_elts, u32 * stream_index)
349 return flowprobe_template_rewrite_inline
350 (frm, fr, collector_address, src_address, collector_port,
355 flowprobe_template_rewrite_l2_ip4 (flow_report_main_t * frm,
357 ip4_address_t * collector_address,
358 ip4_address_t * src_address,
360 ipfix_report_element_t * elts,
361 u32 n_elts, u32 * stream_index)
363 return flowprobe_template_rewrite_inline
364 (frm, fr, collector_address, src_address, collector_port,
365 FLOW_VARIANT_L2_IP4);
369 flowprobe_template_rewrite_l2_ip6 (flow_report_main_t * frm,
371 ip4_address_t * collector_address,
372 ip4_address_t * src_address,
374 ipfix_report_element_t * elts,
375 u32 n_elts, u32 * stream_index)
377 return flowprobe_template_rewrite_inline
378 (frm, fr, collector_address, src_address, collector_port,
379 FLOW_VARIANT_L2_IP6);
383 * @brief Flush accumulated data
384 * @param frm flow_report_main_t *
385 * @param fr flow_report_t *
386 * @param f vlib_frame_t *
389 * This function must simply return the incoming frame, or no template packets
393 flowprobe_data_callback_ip4 (flow_report_main_t * frm,
395 vlib_frame_t * f, u32 * to_next, u32 node_index)
397 flowprobe_flush_callback_ip4 ();
402 flowprobe_data_callback_ip6 (flow_report_main_t * frm,
404 vlib_frame_t * f, u32 * to_next, u32 node_index)
406 flowprobe_flush_callback_ip6 ();
411 flowprobe_data_callback_l2 (flow_report_main_t * frm,
413 vlib_frame_t * f, u32 * to_next, u32 node_index)
415 flowprobe_flush_callback_l2 ();
420 flowprobe_template_add_del (u32 domain_id, u16 src_port,
421 flowprobe_record_t flags,
422 vnet_flow_data_callback_t * flow_data_callback,
423 vnet_flow_rewrite_callback_t * rewrite_callback,
424 bool is_add, u16 * template_id)
426 flow_report_main_t *frm = &flow_report_main;
427 vnet_flow_report_add_del_args_t a = {
428 .rewrite_callback = rewrite_callback,
429 .flow_data_callback = flow_data_callback,
431 .domain_id = domain_id,
432 .src_port = src_port,
433 .opaque.as_uword = flags,
435 return vnet_flow_report_add_del (frm, &a, template_id);
439 flowprobe_expired_timer_callback (u32 * expired_timers)
441 vlib_main_t *vm = vlib_get_main ();
442 flowprobe_main_t *fm = &flowprobe_main;
443 u32 my_cpu_number = vm->thread_index;
447 for (i = 0; i < vec_len (expired_timers); i++)
449 poolindex = expired_timers[i] & 0x7FFFFFFF;
450 vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex);
454 static clib_error_t *
455 flowprobe_create_state_tables (u32 active_timer)
457 flowprobe_main_t *fm = &flowprobe_main;
458 vlib_thread_main_t *tm = &vlib_thread_main;
459 vlib_main_t *vm = vlib_get_main ();
460 clib_error_t *error = 0;
464 /* Decide how many worker threads we have */
465 num_threads = 1 /* main thread */ + tm->n_threads;
467 /* Hash table per worker */
468 fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE;
470 /* Init per worker flow state and timer wheels */
473 vec_validate (fm->timers_per_worker, num_threads - 1);
474 vec_validate (fm->expired_passive_per_worker, num_threads - 1);
475 vec_validate (fm->hash_per_worker, num_threads - 1);
476 vec_validate (fm->pool_per_worker, num_threads - 1);
478 for (i = 0; i < num_threads; i++)
481 pool_alloc (fm->pool_per_worker[i], 1 << fm->ht_log2len);
482 vec_resize (fm->hash_per_worker[i], 1 << fm->ht_log2len);
483 for (j = 0; j < (1 << fm->ht_log2len); j++)
484 fm->hash_per_worker[i][j] = ~0;
485 fm->timers_per_worker[i] =
486 clib_mem_alloc (sizeof (TWT (tw_timer_wheel)));
487 tw_timer_wheel_init_2t_1w_2048sl (fm->timers_per_worker[i],
488 flowprobe_expired_timer_callback,
495 f64 now = vlib_time_now (vm);
496 vec_validate (fm->stateless_entry, num_threads - 1);
497 for (i = 0; i < num_threads; i++)
498 fm->stateless_entry[i].last_exported = now;
499 fm->disabled = false;
501 fm->initialized = true;
506 validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index,
509 vec_validate_init_empty (fm->flow_per_interface, sw_if_index, ~0);
511 if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0)
513 else if (fm->flow_per_interface[sw_if_index] != which)
520 * @brief configure / deconfigure the IPFIX flow-per-packet
521 * @param fm flowprobe_main_t * fm
522 * @param sw_if_index u32 the desired interface
523 * @param is_add int 1 to enable the feature, 0 to disable it
524 * @returns 0 if successful, non-zero otherwise
528 flowprobe_tx_interface_add_del_feature (flowprobe_main_t * fm,
529 u32 sw_if_index, u8 which, int is_add)
531 vlib_main_t *vm = vlib_get_main ();
534 flowprobe_record_t flags = fm->record;
536 fm->flow_per_interface[sw_if_index] = (is_add) ? which : (u8) ~ 0;
537 fm->template_per_flow[which] += (is_add) ? 1 : -1;
538 if (is_add && fm->template_per_flow[which] > 1)
539 template_id = fm->template_reports[flags];
541 if ((is_add && fm->template_per_flow[which] == 1) ||
542 (!is_add && fm->template_per_flow[which] == 0))
544 if (which == FLOW_VARIANT_L2)
546 if (fm->record & FLOW_RECORD_L2)
548 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
549 flowprobe_data_callback_l2,
550 flowprobe_template_rewrite_l2,
551 is_add, &template_id);
553 if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
555 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
556 flowprobe_data_callback_l2,
557 flowprobe_template_rewrite_l2_ip4,
558 is_add, &template_id);
559 fm->template_reports[flags | FLOW_RECORD_L2_IP4] =
560 (is_add) ? template_id : 0;
562 flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
563 flowprobe_data_callback_l2,
564 flowprobe_template_rewrite_l2_ip6,
565 is_add, &template_id);
566 fm->template_reports[flags | FLOW_RECORD_L2_IP6] =
567 (is_add) ? template_id : 0;
569 /* Special case L2 */
570 fm->context[FLOW_VARIANT_L2_IP4].flags =
571 flags | FLOW_RECORD_L2_IP4;
572 fm->context[FLOW_VARIANT_L2_IP6].flags =
573 flags | FLOW_RECORD_L2_IP6;
575 fm->template_reports[flags] = template_id;
578 else if (which == FLOW_VARIANT_IP4)
579 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
580 flowprobe_data_callback_ip4,
581 flowprobe_template_rewrite_ip4,
582 is_add, &template_id);
583 else if (which == FLOW_VARIANT_IP6)
584 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
585 flowprobe_data_callback_ip6,
586 flowprobe_template_rewrite_ip6,
587 is_add, &template_id);
589 if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
591 clib_warning ("vnet_flow_report_add_del returned %d", rv);
595 if (which != (u8) ~ 0)
597 fm->context[which].flags = fm->record;
598 fm->template_reports[flags] = (is_add) ? template_id : 0;
601 if (which == FLOW_VARIANT_IP4)
602 vnet_feature_enable_disable ("ip4-output", "flowprobe-ip4",
603 sw_if_index, is_add, 0, 0);
604 else if (which == FLOW_VARIANT_IP6)
605 vnet_feature_enable_disable ("ip6-output", "flowprobe-ip6",
606 sw_if_index, is_add, 0, 0);
607 else if (which == FLOW_VARIANT_L2)
608 vnet_feature_enable_disable ("interface-output", "flowprobe-l2",
609 sw_if_index, is_add, 0, 0);
611 /* Stateful flow collection */
612 if (is_add && !fm->initialized)
614 flowprobe_create_state_tables (fm->active_timer);
615 if (fm->active_timer)
616 vlib_process_signal_event (vm, flowprobe_timer_node.index, 1, 0);
623 * @brief API message handler
624 * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
626 void vl_api_flowprobe_tx_interface_add_del_t_handler
627 (vl_api_flowprobe_tx_interface_add_del_t * mp)
629 flowprobe_main_t *fm = &flowprobe_main;
630 vl_api_flowprobe_tx_interface_add_del_reply_t *rmp;
631 u32 sw_if_index = ntohl (mp->sw_if_index);
634 VALIDATE_SW_IF_INDEX (mp);
638 clib_warning ("Please specify flowprobe params record first...");
639 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
643 rv = validate_feature_on_interface (fm, sw_if_index, mp->which);
644 if ((rv == 1 && mp->is_add == 1) || rv == 0)
646 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
650 rv = flowprobe_tx_interface_add_del_feature
651 (fm, sw_if_index, mp->which, mp->is_add);
654 BAD_SW_IF_INDEX_LABEL;
656 REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY);
660 * @brief API message custom-dump function
661 * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
662 * @param handle void * print function handle
663 * @returns u8 * output string
665 static void *vl_api_flowprobe_tx_interface_add_del_t_print
666 (vl_api_flowprobe_tx_interface_add_del_t * mp, void *handle)
670 s = format (0, "SCRIPT: flowprobe_tx_interface_add_del ");
671 s = format (s, "sw_if_index %d is_add %d which %d ",
672 clib_host_to_net_u32 (mp->sw_if_index),
673 (int) mp->is_add, (int) mp->which);
677 #define vec_neg_search(v,E) \
680 while (_v(i) < vec_len(v) && v[_v(i)] == E) \
684 if (_v(i) == vec_len(v)) \
690 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
691 u8 record_l3, u8 record_l4,
692 u32 active_timer, u32 passive_timer)
694 flowprobe_record_t flags = 0;
696 if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
700 flags |= FLOW_RECORD_L2;
702 flags |= FLOW_RECORD_L3;
704 flags |= FLOW_RECORD_L4;
709 * Timers: ~0 is default, 0 is off
712 (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
714 (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
720 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
722 flowprobe_main_t *fm = &flowprobe_main;
723 vl_api_flowprobe_params_reply_t *rmp;
726 rv = flowprobe_params
728 mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
729 mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
730 mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
731 clib_net_to_host_u32 (mp->active_timer),
732 clib_net_to_host_u32 (mp->passive_timer));
734 REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
738 VLIB_PLUGIN_REGISTER () = {
739 .version = VPP_BUILD_VER,
740 .description = "Flow per Packet",
745 format_flowprobe_entry (u8 * s, va_list * args)
747 flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
748 s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
750 s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
751 format_ethernet_address, &e->key.dst_mac);
752 s = format (s, " %U -> %U",
753 format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
754 format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
755 s = format (s, " %d", e->key.protocol);
756 s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
757 clib_net_to_host_u16 (e->key.dst_port));
763 format_flowprobe_feature (u8 * s, va_list * args)
765 u8 *which = va_arg (*args, u8 *);
766 if (*which == FLOW_VARIANT_IP4)
767 s = format (s, "ip4");
768 else if (*which == FLOW_VARIANT_IP6)
769 s = format (s, "ip6");
770 else if (*which == FLOW_VARIANT_L2)
771 s = format (s, "l2");
777 format_flowprobe_params (u8 * s, va_list * args)
779 flowprobe_record_t flags = va_arg (*args, flowprobe_record_t);
780 u32 active_timer = va_arg (*args, u32);
781 u32 passive_timer = va_arg (*args, u32);
783 if (flags & FLOW_RECORD_L2)
784 s = format (s, " l2");
785 if (flags & FLOW_RECORD_L3)
786 s = format (s, " l3");
787 if (flags & FLOW_RECORD_L4)
788 s = format (s, " l4");
790 if (active_timer != (u32) ~ 0)
791 s = format (s, " active: %d", active_timer);
793 if (passive_timer != (u32) ~ 0)
794 s = format (s, " passive: %d", passive_timer);
799 static clib_error_t *
800 flowprobe_show_table_fn (vlib_main_t * vm,
801 unformat_input_t * input, vlib_cli_command_t * cm)
803 flowprobe_main_t *fm = &flowprobe_main;
805 flowprobe_entry_t *e;
807 vlib_cli_output (vm, "Dumping IPFIX table");
809 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
812 pool_foreach (e, fm->pool_per_worker[i], (
814 vlib_cli_output (vm, "%U",
815 format_flowprobe_entry,
824 static clib_error_t *
825 flowprobe_show_stats_fn (vlib_main_t * vm,
826 unformat_input_t * input, vlib_cli_command_t * cm)
828 flowprobe_main_t *fm = &flowprobe_main;
831 vlib_cli_output (vm, "IPFIX table statistics");
832 vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
833 vlib_cli_output (vm, "Flow pool size per thread: %d\n",
834 0x1 << FLOWPROBE_LOG2_HASHSIZE);
836 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
837 vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
838 (100 * pool_elts (fm->pool_per_worker[i])) /
839 (0x1 << FLOWPROBE_LOG2_HASHSIZE));
843 static clib_error_t *
844 flowprobe_tx_interface_add_del_feature_command_fn (vlib_main_t * vm,
845 unformat_input_t * input,
846 vlib_cli_command_t * cmd)
848 flowprobe_main_t *fm = &flowprobe_main;
849 u32 sw_if_index = ~0;
851 u8 which = FLOW_VARIANT_IP4;
854 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
856 if (unformat (input, "disable"))
858 else if (unformat (input, "%U", unformat_vnet_sw_interface,
859 fm->vnet_main, &sw_if_index));
860 else if (unformat (input, "ip4"))
861 which = FLOW_VARIANT_IP4;
862 else if (unformat (input, "ip6"))
863 which = FLOW_VARIANT_IP6;
864 else if (unformat (input, "l2"))
865 which = FLOW_VARIANT_L2;
871 return clib_error_return (0,
872 "Please specify flowprobe params record first...");
874 if (sw_if_index == ~0)
875 return clib_error_return (0, "Please specify an interface...");
877 rv = validate_feature_on_interface (fm, sw_if_index, which);
881 return clib_error_return (0,
882 "Datapath is already enabled for given interface...");
885 return clib_error_return (0,
886 "Interface has enable different datapath ...");
889 flowprobe_tx_interface_add_del_feature (fm, sw_if_index, which, is_add);
895 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
896 return clib_error_return
897 (0, "Invalid interface, only works on physical ports");
900 case VNET_API_ERROR_UNIMPLEMENTED:
901 return clib_error_return (0, "ip6 not supported");
905 return clib_error_return (0, "flowprobe_enable_disable returned %d",
911 static clib_error_t *
912 flowprobe_show_feature_command_fn (vlib_main_t * vm,
913 unformat_input_t * input,
914 vlib_cli_command_t * cmd)
916 flowprobe_main_t *fm = &flowprobe_main;
920 vec_foreach (which, fm->flow_per_interface)
922 if (*which == (u8) ~ 0)
925 sw_if_index = which - fm->flow_per_interface;
926 vlib_cli_output (vm, " %U %U", format_vnet_sw_if_index_name,
927 vnet_get_main (), sw_if_index, format_flowprobe_feature,
933 static clib_error_t *
934 flowprobe_params_command_fn (vlib_main_t * vm,
935 unformat_input_t * input,
936 vlib_cli_command_t * cmd)
938 flowprobe_main_t *fm = &flowprobe_main;
939 bool record_l2 = false, record_l3 = false, record_l4 = false;
940 u32 active_timer = ~0;
941 u32 passive_timer = ~0;
943 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
945 if (unformat (input, "active %d", &active_timer))
947 else if (unformat (input, "passive %d", &passive_timer))
949 else if (unformat (input, "record"))
950 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
952 if (unformat (input, "l2"))
954 else if (unformat (input, "l3"))
956 else if (unformat (input, "l4"))
965 if (passive_timer > 0 && active_timer > passive_timer)
966 return clib_error_return (0,
967 "Passive timer has to be greater than active one...");
969 if (flowprobe_params (fm, record_l2, record_l3, record_l4,
970 active_timer, passive_timer))
971 return clib_error_return (0,
972 "Couldn't change flowperpacket params when feature is enabled on some interface ...");
976 static clib_error_t *
977 flowprobe_show_params_command_fn (vlib_main_t * vm,
978 unformat_input_t * input,
979 vlib_cli_command_t * cmd)
981 flowprobe_main_t *fm = &flowprobe_main;
982 flowprobe_record_t flags = fm->record;
983 u32 active_timer = fm->active_timer;
984 u32 passive_timer = fm->passive_timer;
986 vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
992 * '<em>flowprobe feature add-del</em>' commands to enable/disable
993 * per-packet IPFIX flow record generation on an interface
997 * To enable per-packet IPFIX flow-record generation on an interface:
998 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
1000 * To disable per-packet IPFIX flow-record generation on an interface:
1001 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
1006 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
1007 .path = "flowprobe feature add-del",
1009 "flowprobe feature add-del <interface-name> <l2|ip4|ip6> disable",
1010 .function = flowprobe_tx_interface_add_del_feature_command_fn,
1012 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
1013 .path = "flowprobe params",
1015 "flowprobe params record <[l2] [l3] [l4]> [active <timer> passive <timer>]",
1016 .function = flowprobe_params_command_fn,
1019 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
1020 .path = "show flowprobe feature",
1022 "show flowprobe feature",
1023 .function = flowprobe_show_feature_command_fn,
1025 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
1026 .path = "show flowprobe params",
1028 "show flowprobe params",
1029 .function = flowprobe_show_params_command_fn,
1031 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
1032 .path = "show flowprobe table",
1033 .short_help = "show flowprobe table",
1034 .function = flowprobe_show_table_fn,
1036 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
1037 .path = "show flowprobe statistics",
1038 .short_help = "show flowprobe statistics",
1039 .function = flowprobe_show_stats_fn,
1044 * Main-core process, sending an interrupt to the per worker input
1045 * process that spins the per worker timer wheel.
1048 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1050 uword *event_data = 0;
1051 vlib_main_t **worker_vms = 0, *worker_vm;
1052 flowprobe_main_t *fm = &flowprobe_main;
1054 /* Wait for Godot... */
1055 vlib_process_wait_for_event_or_clock (vm, 1e9);
1056 uword event_type = vlib_process_get_events (vm, &event_data);
1057 if (event_type != 1)
1058 clib_warning ("bogus kickoff event received, %d", event_type);
1059 vec_reset_length (event_data);
1062 if (vec_len (vlib_mains) == 0)
1063 vec_add1 (worker_vms, vm);
1066 for (i = 0; i < vec_len (vlib_mains); i++)
1068 worker_vm = vlib_mains[i];
1070 vec_add1 (worker_vms, worker_vm);
1073 f64 sleep_duration = 0.1;
1077 /* Send an interrupt to each timer input node */
1078 sleep_duration = 0.1;
1079 for (i = 0; i < vec_len (worker_vms); i++)
1081 worker_vm = worker_vms[i];
1084 vlib_node_set_interrupt_pending (worker_vm,
1085 flowprobe_walker_node.index);
1087 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1090 vlib_process_suspend (vm, sleep_duration);
1092 return 0; /* or not */
1096 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1097 .function = timer_process,
1098 .name = "flowprobe-timer-process",
1099 .type = VLIB_NODE_TYPE_PROCESS,
1103 #include <flowprobe/flowprobe.api.c>
1106 * @brief Set up the API message handling tables
1107 * @param vm vlib_main_t * vlib main data structure pointer
1108 * @returns 0 to indicate all is well, or a clib_error_t
1110 static clib_error_t *
1111 flowprobe_init (vlib_main_t * vm)
1113 flowprobe_main_t *fm = &flowprobe_main;
1114 vlib_thread_main_t *tm = &vlib_thread_main;
1115 clib_error_t *error = 0;
1119 fm->vnet_main = vnet_get_main ();
1121 /* Ask for a correctly-sized block of API message decode slots */
1122 fm->msg_id_base = setup_message_id_table ();
1124 /* Set up time reference pair */
1125 fm->vlib_time_0 = vlib_time_now (vm);
1126 fm->nanosecond_time_0 = unix_time_now_nsec ();
1128 clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1129 clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1130 clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1132 /* Decide how many worker threads we have */
1133 num_threads = 1 /* main thread */ + tm->n_threads;
1135 /* Allocate per worker thread vectors per flavour */
1136 for (i = 0; i < FLOW_N_VARIANTS; i++)
1138 vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1139 vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1140 vec_validate (fm->context[i].next_record_offset_per_worker,
1144 fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1145 fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1150 VLIB_INIT_FUNCTION (flowprobe_init);
1153 * fd.io coding-style-patch-verification: ON
1156 * eval: (c-set-style "gnu")