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);
776 send_flowprobe_interface_details (u32 sw_if_index, u8 which, u8 direction,
777 vl_api_registration_t *reg, u32 context)
779 flowprobe_main_t *fm = &flowprobe_main;
780 vl_api_flowprobe_interface_details_t *rmp = 0;
782 rmp = vl_msg_api_alloc (sizeof (*rmp));
785 clib_memset (rmp, 0, sizeof (*rmp));
787 ntohs (VL_API_FLOWPROBE_INTERFACE_DETAILS + REPLY_MSG_ID_BASE);
788 rmp->context = context;
790 rmp->sw_if_index = htonl (sw_if_index);
792 if (which == FLOW_VARIANT_IP4)
793 rmp->which = FLOWPROBE_WHICH_IP4;
794 else if (which == FLOW_VARIANT_IP6)
795 rmp->which = FLOWPROBE_WHICH_IP6;
796 else if (which == FLOW_VARIANT_L2)
797 rmp->which = FLOWPROBE_WHICH_L2;
801 if (direction == FLOW_DIRECTION_RX)
802 rmp->direction = FLOWPROBE_DIRECTION_RX;
803 else if (direction == FLOW_DIRECTION_TX)
804 rmp->direction = FLOWPROBE_DIRECTION_TX;
805 else if (direction == FLOW_DIRECTION_BOTH)
806 rmp->direction = FLOWPROBE_DIRECTION_BOTH;
810 vl_api_send_msg (reg, (u8 *) rmp);
814 vl_api_flowprobe_interface_dump_t_handler (
815 vl_api_flowprobe_interface_dump_t *mp)
817 flowprobe_main_t *fm = &flowprobe_main;
818 vl_api_registration_t *reg;
821 reg = vl_api_client_index_to_registration (mp->client_index);
825 sw_if_index = ntohl (mp->sw_if_index);
827 if (sw_if_index == ~0)
831 vec_foreach (which, fm->flow_per_interface)
833 if (*which == (u8) ~0)
836 sw_if_index = which - fm->flow_per_interface;
837 send_flowprobe_interface_details (
838 sw_if_index, *which, fm->direction_per_interface[sw_if_index], reg,
842 else if (vec_len (fm->flow_per_interface) > sw_if_index &&
843 fm->flow_per_interface[sw_if_index] != (u8) ~0)
845 send_flowprobe_interface_details (
846 sw_if_index, fm->flow_per_interface[sw_if_index],
847 fm->direction_per_interface[sw_if_index], reg, mp->context);
851 #define vec_neg_search(v,E) \
854 while (_v(i) < vec_len(v) && v[_v(i)] == E) \
858 if (_v(i) == vec_len(v)) \
864 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
865 u8 record_l3, u8 record_l4,
866 u32 active_timer, u32 passive_timer)
868 flowprobe_record_t flags = 0;
870 if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
871 return VNET_API_ERROR_UNSUPPORTED;
874 flags |= FLOW_RECORD_L2;
876 flags |= FLOW_RECORD_L3;
878 flags |= FLOW_RECORD_L4;
883 * Timers: ~0 is default, 0 is off
886 (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
888 (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
894 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
896 flowprobe_main_t *fm = &flowprobe_main;
897 vl_api_flowprobe_params_reply_t *rmp;
900 rv = flowprobe_params
902 mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
903 mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
904 mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
905 clib_net_to_host_u32 (mp->active_timer),
906 clib_net_to_host_u32 (mp->passive_timer));
908 REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
912 vl_api_flowprobe_set_params_t_handler (vl_api_flowprobe_set_params_t *mp)
914 flowprobe_main_t *fm = &flowprobe_main;
915 vl_api_flowprobe_set_params_reply_t *rmp;
916 bool record_l2, record_l3, record_l4;
921 record_l2 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L2);
922 record_l3 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L3);
923 record_l4 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L4);
925 active_timer = clib_net_to_host_u32 (mp->active_timer);
926 passive_timer = clib_net_to_host_u32 (mp->passive_timer);
928 if (passive_timer > 0 && active_timer > passive_timer)
930 clib_warning ("Passive timer must be greater than active timer");
931 rv = VNET_API_ERROR_INVALID_VALUE;
935 rv = flowprobe_params (fm, record_l2, record_l3, record_l4, active_timer,
937 if (rv == VNET_API_ERROR_UNSUPPORTED)
939 "Cannot change params when feature is enabled on some interfaces");
942 REPLY_MACRO (VL_API_FLOWPROBE_SET_PARAMS_REPLY);
946 vl_api_flowprobe_get_params_t_handler (vl_api_flowprobe_get_params_t *mp)
948 flowprobe_main_t *fm = &flowprobe_main;
949 vl_api_flowprobe_get_params_reply_t *rmp;
953 if (fm->record & FLOW_RECORD_L2)
954 record_flags |= FLOWPROBE_RECORD_FLAG_L2;
955 if (fm->record & FLOW_RECORD_L3)
956 record_flags |= FLOWPROBE_RECORD_FLAG_L3;
957 if (fm->record & FLOW_RECORD_L4)
958 record_flags |= FLOWPROBE_RECORD_FLAG_L4;
961 REPLY_MACRO2 (VL_API_FLOWPROBE_GET_PARAMS_REPLY,
963 rmp->record_flags = record_flags;
964 rmp->active_timer = htonl (fm->active_timer);
965 rmp->passive_timer = htonl (fm->passive_timer);
971 VLIB_PLUGIN_REGISTER () = {
972 .version = VPP_BUILD_VER,
973 .description = "Flow per Packet",
978 format_flowprobe_direction (u8 *s, va_list *args)
980 u8 *direction = va_arg (*args, u8 *);
981 if (*direction == FLOW_DIRECTION_RX)
982 s = format (s, "rx");
983 else if (*direction == FLOW_DIRECTION_TX)
984 s = format (s, "tx");
985 else if (*direction == FLOW_DIRECTION_BOTH)
986 s = format (s, "rx tx");
992 format_flowprobe_entry (u8 * s, va_list * args)
994 flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
995 s = format (s, " %U", format_flowprobe_direction, &e->key.direction);
996 s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
998 s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
999 format_ethernet_address, &e->key.dst_mac);
1000 s = format (s, " %U -> %U",
1001 format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
1002 format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
1003 s = format (s, " %d", e->key.protocol);
1004 s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
1005 clib_net_to_host_u16 (e->key.dst_port));
1011 format_flowprobe_feature (u8 * s, va_list * args)
1013 u8 *which = va_arg (*args, u8 *);
1014 if (*which == FLOW_VARIANT_IP4)
1015 s = format (s, "ip4");
1016 else if (*which == FLOW_VARIANT_IP6)
1017 s = format (s, "ip6");
1018 else if (*which == FLOW_VARIANT_L2)
1019 s = format (s, "l2");
1025 format_flowprobe_params (u8 * s, va_list * args)
1027 flowprobe_record_t flags = va_arg (*args, flowprobe_record_t);
1028 u32 active_timer = va_arg (*args, u32);
1029 u32 passive_timer = va_arg (*args, u32);
1031 if (flags & FLOW_RECORD_L2)
1032 s = format (s, " l2");
1033 if (flags & FLOW_RECORD_L3)
1034 s = format (s, " l3");
1035 if (flags & FLOW_RECORD_L4)
1036 s = format (s, " l4");
1038 if (active_timer != (u32) ~ 0)
1039 s = format (s, " active: %d", active_timer);
1041 if (passive_timer != (u32) ~ 0)
1042 s = format (s, " passive: %d", passive_timer);
1047 static clib_error_t *
1048 flowprobe_show_table_fn (vlib_main_t * vm,
1049 unformat_input_t * input, vlib_cli_command_t * cm)
1051 flowprobe_main_t *fm = &flowprobe_main;
1053 flowprobe_entry_t *e;
1055 vlib_cli_output (vm, "Dumping IPFIX table");
1057 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
1060 pool_foreach (e, fm->pool_per_worker[i])
1062 vlib_cli_output (vm, "%U",
1063 format_flowprobe_entry,
1072 static clib_error_t *
1073 flowprobe_show_stats_fn (vlib_main_t * vm,
1074 unformat_input_t * input, vlib_cli_command_t * cm)
1076 flowprobe_main_t *fm = &flowprobe_main;
1079 vlib_cli_output (vm, "IPFIX table statistics");
1080 vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
1081 vlib_cli_output (vm, "Flow pool size per thread: %d\n",
1082 0x1 << FLOWPROBE_LOG2_HASHSIZE);
1084 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
1085 vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
1086 (100 * pool_elts (fm->pool_per_worker[i])) /
1087 (0x1 << FLOWPROBE_LOG2_HASHSIZE));
1091 static clib_error_t *
1092 flowprobe_interface_add_del_feature_command_fn (vlib_main_t *vm,
1093 unformat_input_t *input,
1094 vlib_cli_command_t *cmd)
1096 flowprobe_main_t *fm = &flowprobe_main;
1097 u32 sw_if_index = ~0;
1099 u8 which = FLOW_VARIANT_IP4;
1100 flowprobe_direction_t direction = FLOW_DIRECTION_TX;
1103 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1105 if (unformat (input, "disable"))
1107 else if (unformat (input, "%U", unformat_vnet_sw_interface,
1108 fm->vnet_main, &sw_if_index));
1109 else if (unformat (input, "ip4"))
1110 which = FLOW_VARIANT_IP4;
1111 else if (unformat (input, "ip6"))
1112 which = FLOW_VARIANT_IP6;
1113 else if (unformat (input, "l2"))
1114 which = FLOW_VARIANT_L2;
1115 else if (unformat (input, "rx"))
1116 direction = FLOW_DIRECTION_RX;
1117 else if (unformat (input, "tx"))
1118 direction = FLOW_DIRECTION_TX;
1119 else if (unformat (input, "both"))
1120 direction = FLOW_DIRECTION_BOTH;
1125 if (fm->record == 0)
1126 return clib_error_return (0,
1127 "Please specify flowprobe params record first...");
1129 if (sw_if_index == ~0)
1130 return clib_error_return (0, "Please specify an interface...");
1132 rv = validate_feature_on_interface (fm, sw_if_index, which);
1136 return clib_error_return (0,
1137 "Datapath is already enabled for given interface...");
1140 return clib_error_return (0,
1141 "Interface has enable different datapath ...");
1146 return clib_error_return (0, "Interface has no datapath enabled");
1150 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
1157 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
1158 return clib_error_return
1159 (0, "Invalid interface, only works on physical ports");
1162 case VNET_API_ERROR_UNIMPLEMENTED:
1163 return clib_error_return (0, "ip6 not supported");
1167 return clib_error_return (0, "flowprobe_enable_disable returned %d",
1173 static clib_error_t *
1174 flowprobe_show_feature_command_fn (vlib_main_t * vm,
1175 unformat_input_t * input,
1176 vlib_cli_command_t * cmd)
1178 flowprobe_main_t *fm = &flowprobe_main;
1182 vec_foreach (which, fm->flow_per_interface)
1184 if (*which == (u8) ~ 0)
1187 sw_if_index = which - fm->flow_per_interface;
1188 vlib_cli_output (vm, " %U %U %U", format_vnet_sw_if_index_name,
1189 vnet_get_main (), sw_if_index, format_flowprobe_feature,
1190 which, format_flowprobe_direction,
1191 &fm->direction_per_interface[sw_if_index]);
1196 static clib_error_t *
1197 flowprobe_params_command_fn (vlib_main_t * vm,
1198 unformat_input_t * input,
1199 vlib_cli_command_t * cmd)
1201 flowprobe_main_t *fm = &flowprobe_main;
1202 bool record_l2 = false, record_l3 = false, record_l4 = false;
1203 u32 active_timer = ~0;
1204 u32 passive_timer = ~0;
1206 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1208 if (unformat (input, "active %d", &active_timer))
1210 else if (unformat (input, "passive %d", &passive_timer))
1212 else if (unformat (input, "record"))
1213 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1215 if (unformat (input, "l2"))
1217 else if (unformat (input, "l3"))
1219 else if (unformat (input, "l4"))
1228 if (passive_timer > 0 && active_timer > passive_timer)
1229 return clib_error_return (0,
1230 "Passive timer has to be greater than active one...");
1232 if (flowprobe_params (fm, record_l2, record_l3, record_l4,
1233 active_timer, passive_timer))
1234 return clib_error_return (0,
1235 "Couldn't change flowperpacket params when feature is enabled on some interface ...");
1239 static clib_error_t *
1240 flowprobe_show_params_command_fn (vlib_main_t * vm,
1241 unformat_input_t * input,
1242 vlib_cli_command_t * cmd)
1244 flowprobe_main_t *fm = &flowprobe_main;
1245 flowprobe_record_t flags = fm->record;
1246 u32 active_timer = fm->active_timer;
1247 u32 passive_timer = fm->passive_timer;
1249 vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
1255 * '<em>flowprobe feature add-del</em>' commands to enable/disable
1256 * per-packet IPFIX flow record generation on an interface
1260 * To enable per-packet IPFIX flow-record generation on an interface:
1261 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
1263 * To disable per-packet IPFIX flow-record generation on an interface:
1264 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
1269 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
1270 .path = "flowprobe feature add-del",
1271 .short_help = "flowprobe feature add-del <interface-name> [(l2|ip4|ip6)] "
1272 "[(rx|tx|both)] [disable]",
1273 .function = flowprobe_interface_add_del_feature_command_fn,
1275 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
1276 .path = "flowprobe params",
1277 .short_help = "flowprobe params record [l2] [l3] [l4] [active <timer>] "
1278 "[passive <timer>]",
1279 .function = flowprobe_params_command_fn,
1282 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
1283 .path = "show flowprobe feature",
1285 "show flowprobe feature",
1286 .function = flowprobe_show_feature_command_fn,
1288 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
1289 .path = "show flowprobe params",
1291 "show flowprobe params",
1292 .function = flowprobe_show_params_command_fn,
1294 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
1295 .path = "show flowprobe table",
1296 .short_help = "show flowprobe table",
1297 .function = flowprobe_show_table_fn,
1299 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
1300 .path = "show flowprobe statistics",
1301 .short_help = "show flowprobe statistics",
1302 .function = flowprobe_show_stats_fn,
1307 * Main-core process, sending an interrupt to the per worker input
1308 * process that spins the per worker timer wheel.
1311 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1313 uword *event_data = 0;
1314 vlib_main_t **worker_vms = 0, *worker_vm;
1315 flowprobe_main_t *fm = &flowprobe_main;
1317 /* Wait for Godot... */
1318 vlib_process_wait_for_event_or_clock (vm, 1e9);
1319 uword event_type = vlib_process_get_events (vm, &event_data);
1320 if (event_type != 1)
1321 clib_warning ("bogus kickoff event received, %d", event_type);
1322 vec_reset_length (event_data);
1325 if (vlib_get_n_threads () == 0)
1326 vec_add1 (worker_vms, vm);
1329 for (i = 0; i < vlib_get_n_threads (); i++)
1331 worker_vm = vlib_get_main_by_index (i);
1333 vec_add1 (worker_vms, worker_vm);
1336 f64 sleep_duration = 0.1;
1340 /* Send an interrupt to each timer input node */
1341 sleep_duration = 0.1;
1342 for (i = 0; i < vec_len (worker_vms); i++)
1344 worker_vm = worker_vms[i];
1347 vlib_node_set_interrupt_pending (worker_vm,
1348 flowprobe_walker_node.index);
1350 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1353 vlib_process_suspend (vm, sleep_duration);
1355 return 0; /* or not */
1359 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1360 .function = timer_process,
1361 .name = "flowprobe-timer-process",
1362 .type = VLIB_NODE_TYPE_PROCESS,
1366 #include <flowprobe/flowprobe.api.c>
1369 * @brief Set up the API message handling tables
1370 * @param vm vlib_main_t * vlib main data structure pointer
1371 * @returns 0 to indicate all is well, or a clib_error_t
1373 static clib_error_t *
1374 flowprobe_init (vlib_main_t * vm)
1376 flowprobe_main_t *fm = &flowprobe_main;
1377 vlib_thread_main_t *tm = &vlib_thread_main;
1378 clib_error_t *error = 0;
1382 fm->vnet_main = vnet_get_main ();
1384 /* Ask for a correctly-sized block of API message decode slots */
1385 fm->msg_id_base = setup_message_id_table ();
1387 /* Set up time reference pair */
1388 fm->vlib_time_0 = vlib_time_now (vm);
1389 fm->nanosecond_time_0 = unix_time_now_nsec ();
1391 clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1392 clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1393 clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1395 /* Decide how many worker threads we have */
1396 num_threads = 1 /* main thread */ + tm->n_threads;
1398 /* Allocate per worker thread vectors per flavour */
1399 for (i = 0; i < FLOW_N_VARIANTS; i++)
1401 vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1402 vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1403 vec_validate (fm->context[i].next_record_offset_per_worker,
1407 fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1408 fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1413 VLIB_INIT_FUNCTION (flowprobe_init);
1416 * fd.io coding-style-patch-verification: ON
1419 * eval: (c-set-style "gnu")