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 <flowprobe/flowprobe.h>
31 #include <vlibapi/api.h>
32 #include <vlibmemory/api.h>
34 /* define message IDs */
35 #include <flowprobe/flowprobe.api_enum.h>
36 #include <flowprobe/flowprobe.api_types.h>
38 flowprobe_main_t flowprobe_main;
39 static vlib_node_registration_t flowprobe_timer_node;
40 uword flowprobe_walker_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
43 #define REPLY_MSG_ID_BASE fm->msg_id_base
44 #include <vlibapi/api_helper_macros.h>
46 /* Define the per-interface configurable features */
48 VNET_FEATURE_INIT (flow_perpacket_ip4, static) =
50 .arc_name = "ip4-output",
51 .node_name = "flowprobe-ip4",
52 .runs_before = VNET_FEATURES ("interface-output"),
55 VNET_FEATURE_INIT (flow_perpacket_ip6, static) =
57 .arc_name = "ip6-output",
58 .node_name = "flowprobe-ip6",
59 .runs_before = VNET_FEATURES ("interface-output"),
62 VNET_FEATURE_INIT (flow_perpacket_l2, static) =
64 .arc_name = "interface-output",
65 .node_name = "flowprobe-l2",
66 .runs_before = VNET_FEATURES ("interface-tx"),
70 /* Macro to finish up custom dump fns */
71 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
74 vl_print (handle, (char *)s); \
78 static inline ipfix_field_specifier_t *
79 flowprobe_template_ip4_fields (ipfix_field_specifier_t * f)
81 #define flowprobe_template_ip4_field_count() 4
82 /* sourceIpv4Address, TLV type 8, u32 */
83 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
84 sourceIPv4Address, 4);
86 /* destinationIPv4Address, TLV type 12, u32 */
87 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
88 destinationIPv4Address, 4);
90 /* protocolIdentifier, TLV type 4, u8 */
91 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
92 protocolIdentifier, 1);
94 /* octetDeltaCount, TLV type 1, u64 */
95 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
101 static inline ipfix_field_specifier_t *
102 flowprobe_template_ip6_fields (ipfix_field_specifier_t * f)
104 #define flowprobe_template_ip6_field_count() 4
105 /* sourceIpv6Address, TLV type 27, 16 octets */
106 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
107 sourceIPv6Address, 16);
109 /* destinationIPv6Address, TLV type 28, 16 octets */
110 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
111 destinationIPv6Address, 16);
113 /* protocolIdentifier, TLV type 4, u8 */
114 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
115 protocolIdentifier, 1);
117 /* octetDeltaCount, TLV type 1, u64 */
118 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
124 static inline ipfix_field_specifier_t *
125 flowprobe_template_l2_fields (ipfix_field_specifier_t * f)
127 #define flowprobe_template_l2_field_count() 3
128 /* sourceMacAddress, TLV type 56, u8[6] we hope */
129 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
130 sourceMacAddress, 6);
132 /* destinationMacAddress, TLV type 80, u8[6] we hope */
133 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
134 destinationMacAddress, 6);
136 /* ethernetType, TLV type 256, u16 */
137 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
143 static inline ipfix_field_specifier_t *
144 flowprobe_template_common_fields (ipfix_field_specifier_t * f)
146 #define flowprobe_template_common_field_count() 5
147 /* ingressInterface, TLV type 10, u32 */
148 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
149 ingressInterface, 4);
152 /* egressInterface, TLV type 14, u32 */
153 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
157 /* packetDeltaCount, TLV type 2, u64 */
158 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
159 packetDeltaCount, 8);
162 /* flowStartNanoseconds, TLV type 156, u64 */
163 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
164 flowStartNanoseconds, 8);
167 /* flowEndNanoseconds, TLV type 157, u64 */
168 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
169 flowEndNanoseconds, 8);
175 static inline ipfix_field_specifier_t *
176 flowprobe_template_l4_fields (ipfix_field_specifier_t * f)
178 #define flowprobe_template_l4_field_count() 3
179 /* sourceTransportPort, TLV type 7, u16 */
180 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
181 sourceTransportPort, 2);
183 /* destinationTransportPort, TLV type 11, u16 */
184 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
185 destinationTransportPort, 2);
187 /* tcpControlBits, TLV type 6, u16 */
188 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
196 * @brief Create an IPFIX template packet rewrite string
197 * @param frm flow_report_main_t *
198 * @param fr flow_report_t *
199 * @param collector_address ip4_address_t * the IPFIX collector address
200 * @param src_address ip4_address_t * the source address we should use
201 * @param collector_port u16 the collector port we should use, host byte order
202 * @returns u8 * vector containing the indicated IPFIX template packet
205 flowprobe_template_rewrite_inline (flow_report_main_t * frm,
207 ip4_address_t * collector_address,
208 ip4_address_t * src_address,
210 flowprobe_variant_t which)
214 ipfix_message_header_t *h;
215 ipfix_set_header_t *s;
216 ipfix_template_header_t *t;
217 ipfix_field_specifier_t *f;
218 ipfix_field_specifier_t *first_field;
220 ip4_ipfix_template_packet_t *tp;
222 flow_report_stream_t *stream;
223 flowprobe_main_t *fm = &flowprobe_main;
224 flowprobe_record_t flags = fr->opaque.as_uword;
225 bool collect_ip4 = false, collect_ip6 = false;
227 stream = &frm->streams[fr->stream_index];
229 if (flags & FLOW_RECORD_L3)
231 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
232 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
233 if (which == FLOW_VARIANT_L2_IP4)
234 flags |= FLOW_RECORD_L2_IP4;
235 if (which == FLOW_VARIANT_L2_IP6)
236 flags |= FLOW_RECORD_L2_IP6;
239 field_count += flowprobe_template_common_field_count ();
240 if (flags & FLOW_RECORD_L2)
241 field_count += flowprobe_template_l2_field_count ();
243 field_count += flowprobe_template_ip4_field_count ();
245 field_count += flowprobe_template_ip6_field_count ();
246 if (flags & FLOW_RECORD_L4)
247 field_count += flowprobe_template_l4_field_count ();
249 /* allocate rewrite space */
251 (rewrite, sizeof (ip4_ipfix_template_packet_t)
252 + field_count * sizeof (ipfix_field_specifier_t) - 1,
253 CLIB_CACHE_LINE_BYTES);
255 tp = (ip4_ipfix_template_packet_t *) rewrite;
256 ip = (ip4_header_t *) & tp->ip4;
257 udp = (udp_header_t *) (ip + 1);
258 h = (ipfix_message_header_t *) (udp + 1);
259 s = (ipfix_set_header_t *) (h + 1);
260 t = (ipfix_template_header_t *) (s + 1);
261 first_field = f = (ipfix_field_specifier_t *) (t + 1);
263 ip->ip_version_and_header_length = 0x45;
265 ip->protocol = IP_PROTOCOL_UDP;
266 ip->src_address.as_u32 = src_address->as_u32;
267 ip->dst_address.as_u32 = collector_address->as_u32;
268 udp->src_port = clib_host_to_net_u16 (stream->src_port);
269 udp->dst_port = clib_host_to_net_u16 (collector_port);
270 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
272 /* FIXUP: message header export_time */
273 /* FIXUP: message header sequence_number */
274 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
276 /* Add TLVs to the template */
277 f = flowprobe_template_common_fields (f);
279 if (flags & FLOW_RECORD_L2)
280 f = flowprobe_template_l2_fields (f);
282 f = flowprobe_template_ip4_fields (f);
284 f = flowprobe_template_ip6_fields (f);
285 if (flags & FLOW_RECORD_L4)
286 f = flowprobe_template_l4_fields (f);
288 /* Back to the template packet... */
289 ip = (ip4_header_t *) & tp->ip4;
290 udp = (udp_header_t *) (ip + 1);
292 ASSERT (f - first_field);
293 /* Field count in this template */
294 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
296 fm->template_size[flags] = (u8 *) f - (u8 *) s;
298 /* set length in octets */
300 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
302 /* message length in octets */
303 h->version_length = version_length ((u8 *) f - (u8 *) h);
305 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
306 ip->checksum = ip4_header_checksum (ip);
312 flowprobe_template_rewrite_ip6 (flow_report_main_t * frm,
314 ip4_address_t * collector_address,
315 ip4_address_t * src_address,
317 ipfix_report_element_t * elts,
318 u32 n_elts, u32 * stream_index)
320 return flowprobe_template_rewrite_inline
321 (frm, fr, collector_address, src_address, collector_port,
326 flowprobe_template_rewrite_ip4 (flow_report_main_t * frm,
328 ip4_address_t * collector_address,
329 ip4_address_t * src_address,
331 ipfix_report_element_t * elts,
332 u32 n_elts, u32 * stream_index)
334 return flowprobe_template_rewrite_inline
335 (frm, fr, collector_address, src_address, collector_port,
340 flowprobe_template_rewrite_l2 (flow_report_main_t * frm,
342 ip4_address_t * collector_address,
343 ip4_address_t * src_address,
345 ipfix_report_element_t * elts,
346 u32 n_elts, u32 * stream_index)
348 return flowprobe_template_rewrite_inline
349 (frm, fr, collector_address, src_address, collector_port,
354 flowprobe_template_rewrite_l2_ip4 (flow_report_main_t * frm,
356 ip4_address_t * collector_address,
357 ip4_address_t * src_address,
359 ipfix_report_element_t * elts,
360 u32 n_elts, u32 * stream_index)
362 return flowprobe_template_rewrite_inline
363 (frm, fr, collector_address, src_address, collector_port,
364 FLOW_VARIANT_L2_IP4);
368 flowprobe_template_rewrite_l2_ip6 (flow_report_main_t * frm,
370 ip4_address_t * collector_address,
371 ip4_address_t * src_address,
373 ipfix_report_element_t * elts,
374 u32 n_elts, u32 * stream_index)
376 return flowprobe_template_rewrite_inline
377 (frm, fr, collector_address, src_address, collector_port,
378 FLOW_VARIANT_L2_IP6);
382 * @brief Flush accumulated data
383 * @param frm flow_report_main_t *
384 * @param fr flow_report_t *
385 * @param f vlib_frame_t *
388 * This function must simply return the incoming frame, or no template packets
392 flowprobe_data_callback_ip4 (flow_report_main_t * frm,
394 vlib_frame_t * f, u32 * to_next, u32 node_index)
396 flowprobe_flush_callback_ip4 ();
401 flowprobe_data_callback_ip6 (flow_report_main_t * frm,
403 vlib_frame_t * f, u32 * to_next, u32 node_index)
405 flowprobe_flush_callback_ip6 ();
410 flowprobe_data_callback_l2 (flow_report_main_t * frm,
412 vlib_frame_t * f, u32 * to_next, u32 node_index)
414 flowprobe_flush_callback_l2 ();
419 flowprobe_template_add_del (u32 domain_id, u16 src_port,
420 flowprobe_record_t flags,
421 vnet_flow_data_callback_t * flow_data_callback,
422 vnet_flow_rewrite_callback_t * rewrite_callback,
423 bool is_add, u16 * template_id)
425 flow_report_main_t *frm = &flow_report_main;
426 vnet_flow_report_add_del_args_t a = {
427 .rewrite_callback = rewrite_callback,
428 .flow_data_callback = flow_data_callback,
430 .domain_id = domain_id,
431 .src_port = src_port,
432 .opaque.as_uword = flags,
434 return vnet_flow_report_add_del (frm, &a, template_id);
438 flowprobe_expired_timer_callback (u32 * expired_timers)
440 vlib_main_t *vm = vlib_get_main ();
441 flowprobe_main_t *fm = &flowprobe_main;
442 u32 my_cpu_number = vm->thread_index;
446 for (i = 0; i < vec_len (expired_timers); i++)
448 poolindex = expired_timers[i] & 0x7FFFFFFF;
449 vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex);
453 static clib_error_t *
454 flowprobe_create_state_tables (u32 active_timer)
456 flowprobe_main_t *fm = &flowprobe_main;
457 vlib_thread_main_t *tm = &vlib_thread_main;
458 vlib_main_t *vm = vlib_get_main ();
459 clib_error_t *error = 0;
463 /* Decide how many worker threads we have */
464 num_threads = 1 /* main thread */ + tm->n_threads;
466 /* Hash table per worker */
467 fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE;
469 /* Init per worker flow state and timer wheels */
472 vec_validate (fm->timers_per_worker, num_threads - 1);
473 vec_validate (fm->expired_passive_per_worker, num_threads - 1);
474 vec_validate (fm->hash_per_worker, num_threads - 1);
475 vec_validate (fm->pool_per_worker, num_threads - 1);
477 for (i = 0; i < num_threads; i++)
480 pool_alloc (fm->pool_per_worker[i], 1 << fm->ht_log2len);
481 vec_resize (fm->hash_per_worker[i], 1 << fm->ht_log2len);
482 for (j = 0; j < (1 << fm->ht_log2len); j++)
483 fm->hash_per_worker[i][j] = ~0;
484 fm->timers_per_worker[i] =
485 clib_mem_alloc (sizeof (TWT (tw_timer_wheel)));
486 tw_timer_wheel_init_2t_1w_2048sl (fm->timers_per_worker[i],
487 flowprobe_expired_timer_callback,
494 f64 now = vlib_time_now (vm);
495 vec_validate (fm->stateless_entry, num_threads - 1);
496 for (i = 0; i < num_threads; i++)
497 fm->stateless_entry[i].last_exported = now;
498 fm->disabled = false;
500 fm->initialized = true;
505 validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index,
508 vec_validate_init_empty (fm->flow_per_interface, sw_if_index, ~0);
510 if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0)
512 else if (fm->flow_per_interface[sw_if_index] != which)
519 * @brief configure / deconfigure the IPFIX flow-per-packet
520 * @param fm flowprobe_main_t * fm
521 * @param sw_if_index u32 the desired interface
522 * @param is_add int 1 to enable the feature, 0 to disable it
523 * @returns 0 if successful, non-zero otherwise
527 flowprobe_tx_interface_add_del_feature (flowprobe_main_t * fm,
528 u32 sw_if_index, u8 which, int is_add)
530 vlib_main_t *vm = vlib_get_main ();
533 flowprobe_record_t flags = fm->record;
535 fm->flow_per_interface[sw_if_index] = (is_add) ? which : (u8) ~ 0;
536 fm->template_per_flow[which] += (is_add) ? 1 : -1;
537 if (is_add && fm->template_per_flow[which] > 1)
538 template_id = fm->template_reports[flags];
540 if ((is_add && fm->template_per_flow[which] == 1) ||
541 (!is_add && fm->template_per_flow[which] == 0))
543 if (which == FLOW_VARIANT_L2)
545 if (fm->record & FLOW_RECORD_L2)
547 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
548 flowprobe_data_callback_l2,
549 flowprobe_template_rewrite_l2,
550 is_add, &template_id);
552 if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
554 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
555 flowprobe_data_callback_l2,
556 flowprobe_template_rewrite_l2_ip4,
557 is_add, &template_id);
558 fm->template_reports[flags | FLOW_RECORD_L2_IP4] =
559 (is_add) ? template_id : 0;
561 flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
562 flowprobe_data_callback_l2,
563 flowprobe_template_rewrite_l2_ip6,
564 is_add, &template_id);
565 fm->template_reports[flags | FLOW_RECORD_L2_IP6] =
566 (is_add) ? template_id : 0;
568 /* Special case L2 */
569 fm->context[FLOW_VARIANT_L2_IP4].flags =
570 flags | FLOW_RECORD_L2_IP4;
571 fm->context[FLOW_VARIANT_L2_IP6].flags =
572 flags | FLOW_RECORD_L2_IP6;
574 fm->template_reports[flags] = template_id;
577 else if (which == FLOW_VARIANT_IP4)
578 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
579 flowprobe_data_callback_ip4,
580 flowprobe_template_rewrite_ip4,
581 is_add, &template_id);
582 else if (which == FLOW_VARIANT_IP6)
583 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
584 flowprobe_data_callback_ip6,
585 flowprobe_template_rewrite_ip6,
586 is_add, &template_id);
588 if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
590 clib_warning ("vnet_flow_report_add_del returned %d", rv);
594 if (which != (u8) ~ 0)
596 fm->context[which].flags = fm->record;
597 fm->template_reports[flags] = (is_add) ? template_id : 0;
600 if (which == FLOW_VARIANT_IP4)
601 vnet_feature_enable_disable ("ip4-output", "flowprobe-ip4",
602 sw_if_index, is_add, 0, 0);
603 else if (which == FLOW_VARIANT_IP6)
604 vnet_feature_enable_disable ("ip6-output", "flowprobe-ip6",
605 sw_if_index, is_add, 0, 0);
606 else if (which == FLOW_VARIANT_L2)
607 vnet_feature_enable_disable ("interface-output", "flowprobe-l2",
608 sw_if_index, is_add, 0, 0);
610 /* Stateful flow collection */
611 if (is_add && !fm->initialized)
613 flowprobe_create_state_tables (fm->active_timer);
614 if (fm->active_timer)
615 vlib_process_signal_event (vm, flowprobe_timer_node.index, 1, 0);
622 * @brief API message handler
623 * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
625 void vl_api_flowprobe_tx_interface_add_del_t_handler
626 (vl_api_flowprobe_tx_interface_add_del_t * mp)
628 flowprobe_main_t *fm = &flowprobe_main;
629 vl_api_flowprobe_tx_interface_add_del_reply_t *rmp;
630 u32 sw_if_index = ntohl (mp->sw_if_index);
633 VALIDATE_SW_IF_INDEX (mp);
637 clib_warning ("Please specify flowprobe params record first...");
638 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
642 rv = validate_feature_on_interface (fm, sw_if_index, mp->which);
643 if ((rv == 1 && mp->is_add == 1) || rv == 0)
645 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
649 rv = flowprobe_tx_interface_add_del_feature
650 (fm, sw_if_index, mp->which, mp->is_add);
653 BAD_SW_IF_INDEX_LABEL;
655 REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY);
659 * @brief API message custom-dump function
660 * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
661 * @param handle void * print function handle
662 * @returns u8 * output string
664 static void *vl_api_flowprobe_tx_interface_add_del_t_print
665 (vl_api_flowprobe_tx_interface_add_del_t * mp, void *handle)
669 s = format (0, "SCRIPT: flowprobe_tx_interface_add_del ");
670 s = format (s, "sw_if_index %d is_add %d which %d ",
671 clib_host_to_net_u32 (mp->sw_if_index),
672 (int) mp->is_add, (int) mp->which);
676 #define vec_neg_search(v,E) \
679 while (_v(i) < vec_len(v) && v[_v(i)] == E) \
683 if (_v(i) == vec_len(v)) \
689 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
690 u8 record_l3, u8 record_l4,
691 u32 active_timer, u32 passive_timer)
693 flowprobe_record_t flags = 0;
695 if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
699 flags |= FLOW_RECORD_L2;
701 flags |= FLOW_RECORD_L3;
703 flags |= FLOW_RECORD_L4;
708 * Timers: ~0 is default, 0 is off
711 (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
713 (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
719 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
721 flowprobe_main_t *fm = &flowprobe_main;
722 vl_api_flowprobe_params_reply_t *rmp;
725 rv = flowprobe_params
727 mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
728 mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
729 mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
730 clib_net_to_host_u32 (mp->active_timer),
731 clib_net_to_host_u32 (mp->passive_timer));
733 REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
737 VLIB_PLUGIN_REGISTER () = {
738 .version = VPP_BUILD_VER,
739 .description = "Flow per Packet",
744 format_flowprobe_entry (u8 * s, va_list * args)
746 flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
747 s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
749 s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
750 format_ethernet_address, &e->key.dst_mac);
751 s = format (s, " %U -> %U",
752 format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
753 format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
754 s = format (s, " %d", e->key.protocol);
755 s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
756 clib_net_to_host_u16 (e->key.dst_port));
761 static clib_error_t *
762 flowprobe_show_table_fn (vlib_main_t * vm,
763 unformat_input_t * input, vlib_cli_command_t * cm)
765 flowprobe_main_t *fm = &flowprobe_main;
767 flowprobe_entry_t *e;
769 vlib_cli_output (vm, "Dumping IPFIX table");
771 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
774 pool_foreach (e, fm->pool_per_worker[i], (
776 vlib_cli_output (vm, "%U",
777 format_flowprobe_entry,
786 static clib_error_t *
787 flowprobe_show_stats_fn (vlib_main_t * vm,
788 unformat_input_t * input, vlib_cli_command_t * cm)
790 flowprobe_main_t *fm = &flowprobe_main;
793 vlib_cli_output (vm, "IPFIX table statistics");
794 vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
795 vlib_cli_output (vm, "Flow pool size per thread: %d\n",
796 0x1 << FLOWPROBE_LOG2_HASHSIZE);
798 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
799 vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
800 (100 * pool_elts (fm->pool_per_worker[i])) /
801 (0x1 << FLOWPROBE_LOG2_HASHSIZE));
805 static clib_error_t *
806 flowprobe_tx_interface_add_del_feature_command_fn (vlib_main_t * vm,
807 unformat_input_t * input,
808 vlib_cli_command_t * cmd)
810 flowprobe_main_t *fm = &flowprobe_main;
811 u32 sw_if_index = ~0;
813 u8 which = FLOW_VARIANT_IP4;
816 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
818 if (unformat (input, "disable"))
820 else if (unformat (input, "%U", unformat_vnet_sw_interface,
821 fm->vnet_main, &sw_if_index));
822 else if (unformat (input, "ip4"))
823 which = FLOW_VARIANT_IP4;
824 else if (unformat (input, "ip6"))
825 which = FLOW_VARIANT_IP6;
826 else if (unformat (input, "l2"))
827 which = FLOW_VARIANT_L2;
833 return clib_error_return (0,
834 "Please specify flowprobe params record first...");
836 if (sw_if_index == ~0)
837 return clib_error_return (0, "Please specify an interface...");
839 rv = validate_feature_on_interface (fm, sw_if_index, which);
843 return clib_error_return (0,
844 "Datapath is already enabled for given interface...");
847 return clib_error_return (0,
848 "Interface has enable different datapath ...");
851 flowprobe_tx_interface_add_del_feature (fm, sw_if_index, which, is_add);
857 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
858 return clib_error_return
859 (0, "Invalid interface, only works on physical ports");
862 case VNET_API_ERROR_UNIMPLEMENTED:
863 return clib_error_return (0, "ip6 not supported");
867 return clib_error_return (0, "flowprobe_enable_disable returned %d",
873 static clib_error_t *
874 flowprobe_params_command_fn (vlib_main_t * vm,
875 unformat_input_t * input,
876 vlib_cli_command_t * cmd)
878 flowprobe_main_t *fm = &flowprobe_main;
879 bool record_l2 = false, record_l3 = false, record_l4 = false;
880 u32 active_timer = ~0;
881 u32 passive_timer = ~0;
883 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
885 if (unformat (input, "active %d", &active_timer))
887 else if (unformat (input, "passive %d", &passive_timer))
889 else if (unformat (input, "record"))
890 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
892 if (unformat (input, "l2"))
894 else if (unformat (input, "l3"))
896 else if (unformat (input, "l4"))
905 if (passive_timer > 0 && active_timer > passive_timer)
906 return clib_error_return (0,
907 "Passive timer has to be greater than active one...");
909 if (flowprobe_params (fm, record_l2, record_l3, record_l4,
910 active_timer, passive_timer))
911 return clib_error_return (0,
912 "Couldn't change flowperpacket params when feature is enabled on some interface ...");
917 * '<em>flowprobe feature add-del</em>' commands to enable/disable
918 * per-packet IPFIX flow record generation on an interface
922 * To enable per-packet IPFIX flow-record generation on an interface:
923 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
925 * To disable per-packet IPFIX flow-record generation on an interface:
926 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
931 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
932 .path = "flowprobe feature add-del",
934 "flowprobe feature add-del <interface-name> <l2|ip4|ip6> disable",
935 .function = flowprobe_tx_interface_add_del_feature_command_fn,
937 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
938 .path = "flowprobe params",
940 "flowprobe params record <[l2] [l3] [l4]> [active <timer> passive <timer>]",
941 .function = flowprobe_params_command_fn,
943 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
944 .path = "show flowprobe table",
945 .short_help = "show flowprobe table",
946 .function = flowprobe_show_table_fn,
948 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
949 .path = "show flowprobe statistics",
950 .short_help = "show flowprobe statistics",
951 .function = flowprobe_show_stats_fn,
956 * Main-core process, sending an interrupt to the per worker input
957 * process that spins the per worker timer wheel.
960 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
962 uword *event_data = 0;
963 vlib_main_t **worker_vms = 0, *worker_vm;
964 flowprobe_main_t *fm = &flowprobe_main;
966 /* Wait for Godot... */
967 vlib_process_wait_for_event_or_clock (vm, 1e9);
968 uword event_type = vlib_process_get_events (vm, &event_data);
970 clib_warning ("bogus kickoff event received, %d", event_type);
971 vec_reset_length (event_data);
974 if (vec_len (vlib_mains) == 0)
975 vec_add1 (worker_vms, vm);
978 for (i = 0; i < vec_len (vlib_mains); i++)
980 worker_vm = vlib_mains[i];
982 vec_add1 (worker_vms, worker_vm);
985 f64 sleep_duration = 0.1;
989 /* Send an interrupt to each timer input node */
990 sleep_duration = 0.1;
991 for (i = 0; i < vec_len (worker_vms); i++)
993 worker_vm = worker_vms[i];
996 vlib_node_set_interrupt_pending (worker_vm,
997 flowprobe_walker_node.index);
999 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1002 vlib_process_suspend (vm, sleep_duration);
1004 return 0; /* or not */
1008 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1009 .function = timer_process,
1010 .name = "flowprobe-timer-process",
1011 .type = VLIB_NODE_TYPE_PROCESS,
1015 #include <flowprobe/flowprobe.api.c>
1018 * @brief Set up the API message handling tables
1019 * @param vm vlib_main_t * vlib main data structure pointer
1020 * @returns 0 to indicate all is well, or a clib_error_t
1022 static clib_error_t *
1023 flowprobe_init (vlib_main_t * vm)
1025 flowprobe_main_t *fm = &flowprobe_main;
1026 vlib_thread_main_t *tm = &vlib_thread_main;
1027 clib_error_t *error = 0;
1031 fm->vnet_main = vnet_get_main ();
1033 /* Ask for a correctly-sized block of API message decode slots */
1034 fm->msg_id_base = setup_message_id_table ();
1036 /* Set up time reference pair */
1037 fm->vlib_time_0 = vlib_time_now (vm);
1038 fm->nanosecond_time_0 = unix_time_now_nsec ();
1040 clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1041 clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1042 clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1044 /* Decide how many worker threads we have */
1045 num_threads = 1 /* main thread */ + tm->n_threads;
1047 /* Allocate per worker thread vectors per flavour */
1048 for (i = 0; i < FLOW_N_VARIANTS; i++)
1050 vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1051 vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1052 vec_validate (fm->context[i].next_record_offset_per_worker,
1056 fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1057 fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1062 VLIB_INIT_FUNCTION (flowprobe_init);
1065 * fd.io coding-style-patch-verification: ON
1068 * eval: (c-set-style "gnu")