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 (flowprobe_input_ip4_unicast, static) = {
50 .arc_name = "ip4-unicast",
51 .node_name = "flowprobe-input-ip4",
52 .runs_before = VNET_FEATURES ("ip4-lookup"),
54 VNET_FEATURE_INIT (flowprobe_input_ip4_multicast, static) = {
55 .arc_name = "ip4-multicast",
56 .node_name = "flowprobe-input-ip4",
57 .runs_before = VNET_FEATURES ("ip4-mfib-forward-lookup"),
59 VNET_FEATURE_INIT (flowprobe_input_ip6_unicast, static) = {
60 .arc_name = "ip6-unicast",
61 .node_name = "flowprobe-input-ip6",
62 .runs_before = VNET_FEATURES ("ip6-lookup"),
64 VNET_FEATURE_INIT (flowprobe_input_ip6_multicast, static) = {
65 .arc_name = "ip6-multicast",
66 .node_name = "flowprobe-input-ip6",
67 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
69 VNET_FEATURE_INIT (flowprobe_input_l2, static) = {
70 .arc_name = "device-input",
71 .node_name = "flowprobe-input-l2",
72 .runs_before = VNET_FEATURES ("ethernet-input"),
74 VNET_FEATURE_INIT (flowprobe_output_ip4, static) = {
75 .arc_name = "ip4-output",
76 .node_name = "flowprobe-output-ip4",
77 .runs_before = VNET_FEATURES ("interface-output"),
80 VNET_FEATURE_INIT (flowprobe_output_ip6, static) = {
81 .arc_name = "ip6-output",
82 .node_name = "flowprobe-output-ip6",
83 .runs_before = VNET_FEATURES ("interface-output"),
86 VNET_FEATURE_INIT (flowprobe_output_l2, static) = {
87 .arc_name = "interface-output",
88 .node_name = "flowprobe-output-l2",
89 .runs_before = VNET_FEATURES ("interface-output-arc-end"),
93 /* Macro to finish up custom dump fns */
94 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
97 vl_print (handle, (char *)s); \
101 static inline ipfix_field_specifier_t *
102 flowprobe_template_ip4_fields (ipfix_field_specifier_t * f)
104 #define flowprobe_template_ip4_field_count() 4
105 /* sourceIpv4Address, TLV type 8, u32 */
106 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
107 sourceIPv4Address, 4);
109 /* destinationIPv4Address, TLV type 12, u32 */
110 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
111 destinationIPv4Address, 4);
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_ip6_fields (ipfix_field_specifier_t * f)
127 #define flowprobe_template_ip6_field_count() 4
128 /* sourceIpv6Address, TLV type 27, 16 octets */
129 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
130 sourceIPv6Address, 16);
132 /* destinationIPv6Address, TLV type 28, 16 octets */
133 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
134 destinationIPv6Address, 16);
136 /* protocolIdentifier, TLV type 4, u8 */
137 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
138 protocolIdentifier, 1);
140 /* octetDeltaCount, TLV type 1, u64 */
141 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
147 static inline ipfix_field_specifier_t *
148 flowprobe_template_l2_fields (ipfix_field_specifier_t * f)
150 #define flowprobe_template_l2_field_count() 3
151 /* sourceMacAddress, TLV type 56, u8[6] we hope */
152 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
153 sourceMacAddress, 6);
155 /* destinationMacAddress, TLV type 80, u8[6] we hope */
156 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
157 destinationMacAddress, 6);
159 /* ethernetType, TLV type 256, u16 */
160 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
166 static inline ipfix_field_specifier_t *
167 flowprobe_template_common_fields (ipfix_field_specifier_t * f)
169 #define flowprobe_template_common_field_count() 6
170 /* ingressInterface, TLV type 10, u32 */
171 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
172 ingressInterface, 4);
175 /* egressInterface, TLV type 14, u32 */
176 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
180 /* flowDirection, TLV type 61, u8 */
181 f->e_id_length = ipfix_e_id_length (0 /* enterprise */, flowDirection, 1);
184 /* packetDeltaCount, TLV type 2, u64 */
185 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
186 packetDeltaCount, 8);
189 /* flowStartNanoseconds, TLV type 156, u64 */
190 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
191 flowStartNanoseconds, 8);
194 /* flowEndNanoseconds, TLV type 157, u64 */
195 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
196 flowEndNanoseconds, 8);
202 static inline ipfix_field_specifier_t *
203 flowprobe_template_l4_fields (ipfix_field_specifier_t * f)
205 #define flowprobe_template_l4_field_count() 3
206 /* sourceTransportPort, TLV type 7, u16 */
207 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
208 sourceTransportPort, 2);
210 /* destinationTransportPort, TLV type 11, u16 */
211 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
212 destinationTransportPort, 2);
214 /* tcpControlBits, TLV type 6, u16 */
215 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
223 * @brief Create an IPFIX template packet rewrite string
224 * @param frm flow_report_main_t *
225 * @param fr flow_report_t *
226 * @param collector_address ip4_address_t * the IPFIX collector address
227 * @param src_address ip4_address_t * the source address we should use
228 * @param collector_port u16 the collector port we should use, host byte order
229 * @returns u8 * vector containing the indicated IPFIX template packet
232 flowprobe_template_rewrite_inline (ipfix_exporter_t *exp, flow_report_t *fr,
234 flowprobe_variant_t which)
238 ipfix_message_header_t *h;
239 ipfix_set_header_t *s;
240 ipfix_template_header_t *t;
241 ipfix_field_specifier_t *f;
242 ipfix_field_specifier_t *first_field;
244 ip4_ipfix_template_packet_t *tp;
246 flow_report_stream_t *stream;
247 flowprobe_main_t *fm = &flowprobe_main;
248 flowprobe_record_t flags = fr->opaque.as_uword;
249 bool collect_ip4 = false, collect_ip6 = false;
251 stream = &exp->streams[fr->stream_index];
253 if (flags & FLOW_RECORD_L3)
255 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
256 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
257 if (which == FLOW_VARIANT_L2_IP4)
258 flags |= FLOW_RECORD_L2_IP4;
259 if (which == FLOW_VARIANT_L2_IP6)
260 flags |= FLOW_RECORD_L2_IP6;
263 field_count += flowprobe_template_common_field_count ();
264 if (flags & FLOW_RECORD_L2)
265 field_count += flowprobe_template_l2_field_count ();
267 field_count += flowprobe_template_ip4_field_count ();
269 field_count += flowprobe_template_ip6_field_count ();
270 if (flags & FLOW_RECORD_L4)
271 field_count += flowprobe_template_l4_field_count ();
273 /* allocate rewrite space */
275 (rewrite, sizeof (ip4_ipfix_template_packet_t)
276 + field_count * sizeof (ipfix_field_specifier_t) - 1,
277 CLIB_CACHE_LINE_BYTES);
279 tp = (ip4_ipfix_template_packet_t *) rewrite;
280 ip = (ip4_header_t *) & tp->ip4;
281 udp = (udp_header_t *) (ip + 1);
282 h = (ipfix_message_header_t *) (udp + 1);
283 s = (ipfix_set_header_t *) (h + 1);
284 t = (ipfix_template_header_t *) (s + 1);
285 first_field = f = (ipfix_field_specifier_t *) (t + 1);
287 ip->ip_version_and_header_length = 0x45;
289 ip->protocol = IP_PROTOCOL_UDP;
290 ip->src_address.as_u32 = exp->src_address.ip.ip4.as_u32;
291 ip->dst_address.as_u32 = exp->ipfix_collector.ip.ip4.as_u32;
292 udp->src_port = clib_host_to_net_u16 (stream->src_port);
293 udp->dst_port = clib_host_to_net_u16 (collector_port);
294 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
296 /* FIXUP: message header export_time */
297 /* FIXUP: message header sequence_number */
298 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
300 /* Add TLVs to the template */
301 f = flowprobe_template_common_fields (f);
303 if (flags & FLOW_RECORD_L2)
304 f = flowprobe_template_l2_fields (f);
306 f = flowprobe_template_ip4_fields (f);
308 f = flowprobe_template_ip6_fields (f);
309 if (flags & FLOW_RECORD_L4)
310 f = flowprobe_template_l4_fields (f);
312 /* Back to the template packet... */
313 ip = (ip4_header_t *) & tp->ip4;
314 udp = (udp_header_t *) (ip + 1);
316 ASSERT (f - first_field);
317 /* Field count in this template */
318 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
320 fm->template_size[flags] = (u8 *) f - (u8 *) s;
322 /* set length in octets */
324 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
326 /* message length in octets */
327 h->version_length = version_length ((u8 *) f - (u8 *) h);
329 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
330 ip->checksum = ip4_header_checksum (ip);
336 flowprobe_template_rewrite_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
338 ipfix_report_element_t *elts, u32 n_elts,
341 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
346 flowprobe_template_rewrite_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
348 ipfix_report_element_t *elts, u32 n_elts,
351 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
356 flowprobe_template_rewrite_l2 (ipfix_exporter_t *exp, flow_report_t *fr,
358 ipfix_report_element_t *elts, u32 n_elts,
361 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
366 flowprobe_template_rewrite_l2_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
368 ipfix_report_element_t *elts, u32 n_elts,
371 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
372 FLOW_VARIANT_L2_IP4);
376 flowprobe_template_rewrite_l2_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
378 ipfix_report_element_t *elts, u32 n_elts,
381 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
382 FLOW_VARIANT_L2_IP6);
386 * @brief Flush accumulated data
387 * @param frm flow_report_main_t *
388 * @param fr flow_report_t *
389 * @param f vlib_frame_t *
392 * This function must simply return the incoming frame, or no template packets
396 flowprobe_data_callback_ip4 (flow_report_main_t *frm, ipfix_exporter_t *exp,
397 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
400 flowprobe_flush_callback_ip4 ();
405 flowprobe_data_callback_ip6 (flow_report_main_t *frm, ipfix_exporter_t *exp,
406 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
409 flowprobe_flush_callback_ip6 ();
414 flowprobe_data_callback_l2 (flow_report_main_t *frm, ipfix_exporter_t *exp,
415 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
418 flowprobe_flush_callback_l2 ();
423 flowprobe_template_add_del (u32 domain_id, u16 src_port,
424 flowprobe_record_t flags,
425 vnet_flow_data_callback_t * flow_data_callback,
426 vnet_flow_rewrite_callback_t * rewrite_callback,
427 bool is_add, u16 * template_id)
429 ipfix_exporter_t *exp = &flow_report_main.exporters[0];
430 vnet_flow_report_add_del_args_t a = {
431 .rewrite_callback = rewrite_callback,
432 .flow_data_callback = flow_data_callback,
434 .domain_id = domain_id,
435 .src_port = src_port,
436 .opaque.as_uword = flags,
438 return vnet_flow_report_add_del (exp, &a, template_id);
442 flowprobe_expired_timer_callback (u32 * expired_timers)
444 vlib_main_t *vm = vlib_get_main ();
445 flowprobe_main_t *fm = &flowprobe_main;
446 u32 my_cpu_number = vm->thread_index;
450 for (i = 0; i < vec_len (expired_timers); i++)
452 poolindex = expired_timers[i] & 0x7FFFFFFF;
453 vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex);
457 static clib_error_t *
458 flowprobe_create_state_tables (u32 active_timer)
460 flowprobe_main_t *fm = &flowprobe_main;
461 vlib_thread_main_t *tm = &vlib_thread_main;
462 vlib_main_t *vm = vlib_get_main ();
463 clib_error_t *error = 0;
467 /* Decide how many worker threads we have */
468 num_threads = 1 /* main thread */ + tm->n_threads;
470 /* Hash table per worker */
471 fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE;
473 /* Init per worker flow state and timer wheels */
476 vec_validate (fm->timers_per_worker, num_threads - 1);
477 vec_validate (fm->expired_passive_per_worker, num_threads - 1);
478 vec_validate (fm->hash_per_worker, num_threads - 1);
479 vec_validate (fm->pool_per_worker, num_threads - 1);
481 for (i = 0; i < num_threads; i++)
484 pool_alloc (fm->pool_per_worker[i], 1 << fm->ht_log2len);
485 vec_resize (fm->hash_per_worker[i], 1 << fm->ht_log2len);
486 for (j = 0; j < (1 << fm->ht_log2len); j++)
487 fm->hash_per_worker[i][j] = ~0;
488 fm->timers_per_worker[i] =
489 clib_mem_alloc (sizeof (TWT (tw_timer_wheel)));
490 tw_timer_wheel_init_2t_1w_2048sl (fm->timers_per_worker[i],
491 flowprobe_expired_timer_callback,
498 f64 now = vlib_time_now (vm);
499 vec_validate (fm->stateless_entry, num_threads - 1);
500 for (i = 0; i < num_threads; i++)
501 fm->stateless_entry[i].last_exported = now;
502 fm->disabled = false;
504 fm->initialized = true;
509 validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index,
512 vec_validate_init_empty (fm->flow_per_interface, sw_if_index, ~0);
513 vec_validate_init_empty (fm->direction_per_interface, sw_if_index, ~0);
515 if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0)
517 else if (fm->flow_per_interface[sw_if_index] != which)
524 * @brief configure / deconfigure the IPFIX flow-per-packet
525 * @param fm flowprobe_main_t * fm
526 * @param sw_if_index u32 the desired interface
527 * @param which u8 the desired datapath
528 * @param direction u8 the desired direction
529 * @param is_add int 1 to enable the feature, 0 to disable it
530 * @returns 0 if successful, non-zero otherwise
534 flowprobe_interface_add_del_feature (flowprobe_main_t *fm, u32 sw_if_index,
535 u8 which, u8 direction, int is_add)
537 vlib_main_t *vm = vlib_get_main ();
540 flowprobe_record_t flags = fm->record;
542 fm->flow_per_interface[sw_if_index] = (is_add) ? which : (u8) ~ 0;
543 fm->direction_per_interface[sw_if_index] = (is_add) ? direction : (u8) ~0;
544 fm->template_per_flow[which] += (is_add) ? 1 : -1;
545 if (is_add && fm->template_per_flow[which] > 1)
546 template_id = fm->template_reports[flags];
548 if ((is_add && fm->template_per_flow[which] == 1) ||
549 (!is_add && fm->template_per_flow[which] == 0))
551 if (which == FLOW_VARIANT_L2)
553 if (fm->record & FLOW_RECORD_L2)
555 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
556 flowprobe_data_callback_l2,
557 flowprobe_template_rewrite_l2,
558 is_add, &template_id);
560 if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
562 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
563 flowprobe_data_callback_l2,
564 flowprobe_template_rewrite_l2_ip4,
565 is_add, &template_id);
566 fm->template_reports[flags | FLOW_RECORD_L2_IP4] =
567 (is_add) ? template_id : 0;
569 flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
570 flowprobe_data_callback_l2,
571 flowprobe_template_rewrite_l2_ip6,
572 is_add, &template_id);
573 fm->template_reports[flags | FLOW_RECORD_L2_IP6] =
574 (is_add) ? template_id : 0;
576 /* Special case L2 */
577 fm->context[FLOW_VARIANT_L2_IP4].flags =
578 flags | FLOW_RECORD_L2_IP4;
579 fm->context[FLOW_VARIANT_L2_IP6].flags =
580 flags | FLOW_RECORD_L2_IP6;
582 fm->template_reports[flags] = template_id;
585 else if (which == FLOW_VARIANT_IP4)
586 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
587 flowprobe_data_callback_ip4,
588 flowprobe_template_rewrite_ip4,
589 is_add, &template_id);
590 else if (which == FLOW_VARIANT_IP6)
591 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
592 flowprobe_data_callback_ip6,
593 flowprobe_template_rewrite_ip6,
594 is_add, &template_id);
596 if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
598 clib_warning ("vnet_flow_report_add_del returned %d", rv);
602 if (which != (u8) ~ 0)
604 fm->context[which].flags = fm->record;
605 fm->template_reports[flags] = (is_add) ? template_id : 0;
608 if (direction == FLOW_DIRECTION_RX || direction == FLOW_DIRECTION_BOTH)
610 if (which == FLOW_VARIANT_IP4)
612 vnet_feature_enable_disable ("ip4-unicast", "flowprobe-input-ip4",
613 sw_if_index, is_add, 0, 0);
614 vnet_feature_enable_disable ("ip4-multicast", "flowprobe-input-ip4",
615 sw_if_index, is_add, 0, 0);
617 else if (which == FLOW_VARIANT_IP6)
619 vnet_feature_enable_disable ("ip6-unicast", "flowprobe-input-ip6",
620 sw_if_index, is_add, 0, 0);
621 vnet_feature_enable_disable ("ip6-multicast", "flowprobe-input-ip6",
622 sw_if_index, is_add, 0, 0);
624 else if (which == FLOW_VARIANT_L2)
625 vnet_feature_enable_disable ("device-input", "flowprobe-input-l2",
626 sw_if_index, is_add, 0, 0);
629 if (direction == FLOW_DIRECTION_TX || direction == FLOW_DIRECTION_BOTH)
631 if (which == FLOW_VARIANT_IP4)
632 vnet_feature_enable_disable ("ip4-output", "flowprobe-output-ip4",
633 sw_if_index, is_add, 0, 0);
634 else if (which == FLOW_VARIANT_IP6)
635 vnet_feature_enable_disable ("ip6-output", "flowprobe-output-ip6",
636 sw_if_index, is_add, 0, 0);
637 else if (which == FLOW_VARIANT_L2)
638 vnet_feature_enable_disable ("interface-output", "flowprobe-output-l2",
639 sw_if_index, is_add, 0, 0);
642 /* Stateful flow collection */
643 if (is_add && !fm->initialized)
645 flowprobe_create_state_tables (fm->active_timer);
646 if (fm->active_timer)
647 vlib_process_signal_event (vm, flowprobe_timer_node.index, 1, 0);
654 * @brief API message handler
655 * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
657 void vl_api_flowprobe_tx_interface_add_del_t_handler
658 (vl_api_flowprobe_tx_interface_add_del_t * mp)
660 flowprobe_main_t *fm = &flowprobe_main;
661 vl_api_flowprobe_tx_interface_add_del_reply_t *rmp;
662 u32 sw_if_index = ntohl (mp->sw_if_index);
665 VALIDATE_SW_IF_INDEX (mp);
669 clib_warning ("Please specify flowprobe params record first...");
670 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
674 rv = validate_feature_on_interface (fm, sw_if_index, mp->which);
675 if ((rv == 1 && mp->is_add == 1) || rv == 0)
677 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
681 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, mp->which,
682 FLOW_DIRECTION_TX, mp->is_add);
685 BAD_SW_IF_INDEX_LABEL;
687 REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY);
691 vl_api_flowprobe_interface_add_del_t_handler (
692 vl_api_flowprobe_interface_add_del_t *mp)
694 flowprobe_main_t *fm = &flowprobe_main;
695 vl_api_flowprobe_interface_add_del_reply_t *rmp;
702 VALIDATE_SW_IF_INDEX (mp);
704 sw_if_index = ntohl (mp->sw_if_index);
707 if (mp->which == FLOWPROBE_WHICH_IP4)
708 which = FLOW_VARIANT_IP4;
709 else if (mp->which == FLOWPROBE_WHICH_IP6)
710 which = FLOW_VARIANT_IP6;
711 else if (mp->which == FLOWPROBE_WHICH_L2)
712 which = FLOW_VARIANT_L2;
715 clib_warning ("Invalid value of which");
716 rv = VNET_API_ERROR_INVALID_VALUE;
720 if (mp->direction == FLOWPROBE_DIRECTION_RX)
721 direction = FLOW_DIRECTION_RX;
722 else if (mp->direction == FLOWPROBE_DIRECTION_TX)
723 direction = FLOW_DIRECTION_TX;
724 else if (mp->direction == FLOWPROBE_DIRECTION_BOTH)
725 direction = FLOW_DIRECTION_BOTH;
728 clib_warning ("Invalid value of direction");
729 rv = VNET_API_ERROR_INVALID_VALUE;
735 clib_warning ("Please specify flowprobe params record first");
736 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
740 rv = validate_feature_on_interface (fm, sw_if_index, which);
745 clib_warning ("Variant is already enabled for given interface");
746 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
752 clib_warning ("Interface has different variant enabled");
753 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
760 clib_warning ("Interface has no variant enabled");
761 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
766 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
770 BAD_SW_IF_INDEX_LABEL;
772 REPLY_MACRO (VL_API_FLOWPROBE_INTERFACE_ADD_DEL_REPLY);
775 #define vec_neg_search(v,E) \
778 while (_v(i) < vec_len(v) && v[_v(i)] == E) \
782 if (_v(i) == vec_len(v)) \
788 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
789 u8 record_l3, u8 record_l4,
790 u32 active_timer, u32 passive_timer)
792 flowprobe_record_t flags = 0;
794 if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
798 flags |= FLOW_RECORD_L2;
800 flags |= FLOW_RECORD_L3;
802 flags |= FLOW_RECORD_L4;
807 * Timers: ~0 is default, 0 is off
810 (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
812 (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
818 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
820 flowprobe_main_t *fm = &flowprobe_main;
821 vl_api_flowprobe_params_reply_t *rmp;
824 rv = flowprobe_params
826 mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
827 mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
828 mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
829 clib_net_to_host_u32 (mp->active_timer),
830 clib_net_to_host_u32 (mp->passive_timer));
832 REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
836 VLIB_PLUGIN_REGISTER () = {
837 .version = VPP_BUILD_VER,
838 .description = "Flow per Packet",
843 format_flowprobe_direction (u8 *s, va_list *args)
845 u8 *direction = va_arg (*args, u8 *);
846 if (*direction == FLOW_DIRECTION_RX)
847 s = format (s, "rx");
848 else if (*direction == FLOW_DIRECTION_TX)
849 s = format (s, "tx");
850 else if (*direction == FLOW_DIRECTION_BOTH)
851 s = format (s, "rx tx");
857 format_flowprobe_entry (u8 * s, va_list * args)
859 flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
860 s = format (s, " %U", format_flowprobe_direction, &e->key.direction);
861 s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
863 s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
864 format_ethernet_address, &e->key.dst_mac);
865 s = format (s, " %U -> %U",
866 format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
867 format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
868 s = format (s, " %d", e->key.protocol);
869 s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
870 clib_net_to_host_u16 (e->key.dst_port));
876 format_flowprobe_feature (u8 * s, va_list * args)
878 u8 *which = va_arg (*args, u8 *);
879 if (*which == FLOW_VARIANT_IP4)
880 s = format (s, "ip4");
881 else if (*which == FLOW_VARIANT_IP6)
882 s = format (s, "ip6");
883 else if (*which == FLOW_VARIANT_L2)
884 s = format (s, "l2");
890 format_flowprobe_params (u8 * s, va_list * args)
892 flowprobe_record_t flags = va_arg (*args, flowprobe_record_t);
893 u32 active_timer = va_arg (*args, u32);
894 u32 passive_timer = va_arg (*args, u32);
896 if (flags & FLOW_RECORD_L2)
897 s = format (s, " l2");
898 if (flags & FLOW_RECORD_L3)
899 s = format (s, " l3");
900 if (flags & FLOW_RECORD_L4)
901 s = format (s, " l4");
903 if (active_timer != (u32) ~ 0)
904 s = format (s, " active: %d", active_timer);
906 if (passive_timer != (u32) ~ 0)
907 s = format (s, " passive: %d", passive_timer);
912 static clib_error_t *
913 flowprobe_show_table_fn (vlib_main_t * vm,
914 unformat_input_t * input, vlib_cli_command_t * cm)
916 flowprobe_main_t *fm = &flowprobe_main;
918 flowprobe_entry_t *e;
920 vlib_cli_output (vm, "Dumping IPFIX table");
922 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
925 pool_foreach (e, fm->pool_per_worker[i])
927 vlib_cli_output (vm, "%U",
928 format_flowprobe_entry,
937 static clib_error_t *
938 flowprobe_show_stats_fn (vlib_main_t * vm,
939 unformat_input_t * input, vlib_cli_command_t * cm)
941 flowprobe_main_t *fm = &flowprobe_main;
944 vlib_cli_output (vm, "IPFIX table statistics");
945 vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
946 vlib_cli_output (vm, "Flow pool size per thread: %d\n",
947 0x1 << FLOWPROBE_LOG2_HASHSIZE);
949 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
950 vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
951 (100 * pool_elts (fm->pool_per_worker[i])) /
952 (0x1 << FLOWPROBE_LOG2_HASHSIZE));
956 static clib_error_t *
957 flowprobe_interface_add_del_feature_command_fn (vlib_main_t *vm,
958 unformat_input_t *input,
959 vlib_cli_command_t *cmd)
961 flowprobe_main_t *fm = &flowprobe_main;
962 u32 sw_if_index = ~0;
964 u8 which = FLOW_VARIANT_IP4;
965 flowprobe_direction_t direction = FLOW_DIRECTION_TX;
968 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
970 if (unformat (input, "disable"))
972 else if (unformat (input, "%U", unformat_vnet_sw_interface,
973 fm->vnet_main, &sw_if_index));
974 else if (unformat (input, "ip4"))
975 which = FLOW_VARIANT_IP4;
976 else if (unformat (input, "ip6"))
977 which = FLOW_VARIANT_IP6;
978 else if (unformat (input, "l2"))
979 which = FLOW_VARIANT_L2;
980 else if (unformat (input, "rx"))
981 direction = FLOW_DIRECTION_RX;
982 else if (unformat (input, "tx"))
983 direction = FLOW_DIRECTION_TX;
984 else if (unformat (input, "both"))
985 direction = FLOW_DIRECTION_BOTH;
991 return clib_error_return (0,
992 "Please specify flowprobe params record first...");
994 if (sw_if_index == ~0)
995 return clib_error_return (0, "Please specify an interface...");
997 rv = validate_feature_on_interface (fm, sw_if_index, which);
1001 return clib_error_return (0,
1002 "Datapath is already enabled for given interface...");
1005 return clib_error_return (0,
1006 "Interface has enable different datapath ...");
1011 return clib_error_return (0, "Interface has no datapath enabled");
1015 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
1022 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
1023 return clib_error_return
1024 (0, "Invalid interface, only works on physical ports");
1027 case VNET_API_ERROR_UNIMPLEMENTED:
1028 return clib_error_return (0, "ip6 not supported");
1032 return clib_error_return (0, "flowprobe_enable_disable returned %d",
1038 static clib_error_t *
1039 flowprobe_show_feature_command_fn (vlib_main_t * vm,
1040 unformat_input_t * input,
1041 vlib_cli_command_t * cmd)
1043 flowprobe_main_t *fm = &flowprobe_main;
1047 vec_foreach (which, fm->flow_per_interface)
1049 if (*which == (u8) ~ 0)
1052 sw_if_index = which - fm->flow_per_interface;
1053 vlib_cli_output (vm, " %U %U %U", format_vnet_sw_if_index_name,
1054 vnet_get_main (), sw_if_index, format_flowprobe_feature,
1055 which, format_flowprobe_direction,
1056 &fm->direction_per_interface[sw_if_index]);
1061 static clib_error_t *
1062 flowprobe_params_command_fn (vlib_main_t * vm,
1063 unformat_input_t * input,
1064 vlib_cli_command_t * cmd)
1066 flowprobe_main_t *fm = &flowprobe_main;
1067 bool record_l2 = false, record_l3 = false, record_l4 = false;
1068 u32 active_timer = ~0;
1069 u32 passive_timer = ~0;
1071 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1073 if (unformat (input, "active %d", &active_timer))
1075 else if (unformat (input, "passive %d", &passive_timer))
1077 else if (unformat (input, "record"))
1078 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1080 if (unformat (input, "l2"))
1082 else if (unformat (input, "l3"))
1084 else if (unformat (input, "l4"))
1093 if (passive_timer > 0 && active_timer > passive_timer)
1094 return clib_error_return (0,
1095 "Passive timer has to be greater than active one...");
1097 if (flowprobe_params (fm, record_l2, record_l3, record_l4,
1098 active_timer, passive_timer))
1099 return clib_error_return (0,
1100 "Couldn't change flowperpacket params when feature is enabled on some interface ...");
1104 static clib_error_t *
1105 flowprobe_show_params_command_fn (vlib_main_t * vm,
1106 unformat_input_t * input,
1107 vlib_cli_command_t * cmd)
1109 flowprobe_main_t *fm = &flowprobe_main;
1110 flowprobe_record_t flags = fm->record;
1111 u32 active_timer = fm->active_timer;
1112 u32 passive_timer = fm->passive_timer;
1114 vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
1120 * '<em>flowprobe feature add-del</em>' commands to enable/disable
1121 * per-packet IPFIX flow record generation on an interface
1125 * To enable per-packet IPFIX flow-record generation on an interface:
1126 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
1128 * To disable per-packet IPFIX flow-record generation on an interface:
1129 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
1134 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
1135 .path = "flowprobe feature add-del",
1136 .short_help = "flowprobe feature add-del <interface-name> [(l2|ip4|ip6)] "
1137 "[(rx|tx|both)] [disable]",
1138 .function = flowprobe_interface_add_del_feature_command_fn,
1140 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
1141 .path = "flowprobe params",
1143 "flowprobe params record <[l2] [l3] [l4]> [active <timer> passive <timer>]",
1144 .function = flowprobe_params_command_fn,
1147 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
1148 .path = "show flowprobe feature",
1150 "show flowprobe feature",
1151 .function = flowprobe_show_feature_command_fn,
1153 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
1154 .path = "show flowprobe params",
1156 "show flowprobe params",
1157 .function = flowprobe_show_params_command_fn,
1159 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
1160 .path = "show flowprobe table",
1161 .short_help = "show flowprobe table",
1162 .function = flowprobe_show_table_fn,
1164 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
1165 .path = "show flowprobe statistics",
1166 .short_help = "show flowprobe statistics",
1167 .function = flowprobe_show_stats_fn,
1172 * Main-core process, sending an interrupt to the per worker input
1173 * process that spins the per worker timer wheel.
1176 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1178 uword *event_data = 0;
1179 vlib_main_t **worker_vms = 0, *worker_vm;
1180 flowprobe_main_t *fm = &flowprobe_main;
1182 /* Wait for Godot... */
1183 vlib_process_wait_for_event_or_clock (vm, 1e9);
1184 uword event_type = vlib_process_get_events (vm, &event_data);
1185 if (event_type != 1)
1186 clib_warning ("bogus kickoff event received, %d", event_type);
1187 vec_reset_length (event_data);
1190 if (vlib_get_n_threads () == 0)
1191 vec_add1 (worker_vms, vm);
1194 for (i = 0; i < vlib_get_n_threads (); i++)
1196 worker_vm = vlib_get_main_by_index (i);
1198 vec_add1 (worker_vms, worker_vm);
1201 f64 sleep_duration = 0.1;
1205 /* Send an interrupt to each timer input node */
1206 sleep_duration = 0.1;
1207 for (i = 0; i < vec_len (worker_vms); i++)
1209 worker_vm = worker_vms[i];
1212 vlib_node_set_interrupt_pending (worker_vm,
1213 flowprobe_walker_node.index);
1215 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1218 vlib_process_suspend (vm, sleep_duration);
1220 return 0; /* or not */
1224 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1225 .function = timer_process,
1226 .name = "flowprobe-timer-process",
1227 .type = VLIB_NODE_TYPE_PROCESS,
1231 #include <flowprobe/flowprobe.api.c>
1234 * @brief Set up the API message handling tables
1235 * @param vm vlib_main_t * vlib main data structure pointer
1236 * @returns 0 to indicate all is well, or a clib_error_t
1238 static clib_error_t *
1239 flowprobe_init (vlib_main_t * vm)
1241 flowprobe_main_t *fm = &flowprobe_main;
1242 vlib_thread_main_t *tm = &vlib_thread_main;
1243 clib_error_t *error = 0;
1247 fm->vnet_main = vnet_get_main ();
1249 /* Ask for a correctly-sized block of API message decode slots */
1250 fm->msg_id_base = setup_message_id_table ();
1252 /* Set up time reference pair */
1253 fm->vlib_time_0 = vlib_time_now (vm);
1254 fm->nanosecond_time_0 = unix_time_now_nsec ();
1256 clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1257 clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1258 clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1260 /* Decide how many worker threads we have */
1261 num_threads = 1 /* main thread */ + tm->n_threads;
1263 /* Allocate per worker thread vectors per flavour */
1264 for (i = 0; i < FLOW_N_VARIANTS; i++)
1266 vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1267 vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1268 vec_validate (fm->context[i].next_record_offset_per_worker,
1272 fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1273 fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1278 VLIB_INIT_FUNCTION (flowprobe_init);
1281 * fd.io coding-style-patch-verification: ON
1284 * eval: (c-set-style "gnu")