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 */
48 VNET_FEATURE_INIT (flowprobe_input_ip4_unicast, static) = {
49 .arc_name = "ip4-unicast",
50 .node_name = "flowprobe-input-ip4",
51 .runs_before = VNET_FEATURES ("ip4-lookup"),
53 VNET_FEATURE_INIT (flowprobe_input_ip4_multicast, static) = {
54 .arc_name = "ip4-multicast",
55 .node_name = "flowprobe-input-ip4",
56 .runs_before = VNET_FEATURES ("ip4-mfib-forward-lookup"),
58 VNET_FEATURE_INIT (flowprobe_input_ip6_unicast, static) = {
59 .arc_name = "ip6-unicast",
60 .node_name = "flowprobe-input-ip6",
61 .runs_before = VNET_FEATURES ("ip6-lookup"),
63 VNET_FEATURE_INIT (flowprobe_input_ip6_multicast, static) = {
64 .arc_name = "ip6-multicast",
65 .node_name = "flowprobe-input-ip6",
66 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
68 VNET_FEATURE_INIT (flowprobe_input_l2, static) = {
69 .arc_name = "device-input",
70 .node_name = "flowprobe-input-l2",
71 .runs_before = VNET_FEATURES ("ethernet-input"),
73 VNET_FEATURE_INIT (flowprobe_output_ip4, static) = {
74 .arc_name = "ip4-output",
75 .node_name = "flowprobe-output-ip4",
76 .runs_before = VNET_FEATURES ("interface-output"),
79 VNET_FEATURE_INIT (flowprobe_output_ip6, static) = {
80 .arc_name = "ip6-output",
81 .node_name = "flowprobe-output-ip6",
82 .runs_before = VNET_FEATURES ("interface-output"),
85 VNET_FEATURE_INIT (flowprobe_output_l2, static) = {
86 .arc_name = "interface-output",
87 .node_name = "flowprobe-output-l2",
88 .runs_before = VNET_FEATURES ("interface-output-arc-end"),
93 vlib_cli_output (handle, (char *) s); \
97 static inline ipfix_field_specifier_t *
98 flowprobe_template_ip4_fields (ipfix_field_specifier_t * f)
100 #define flowprobe_template_ip4_field_count() 4
101 /* sourceIpv4Address, TLV type 8, u32 */
102 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
103 sourceIPv4Address, 4);
105 /* destinationIPv4Address, TLV type 12, u32 */
106 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
107 destinationIPv4Address, 4);
109 /* protocolIdentifier, TLV type 4, u8 */
110 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
111 protocolIdentifier, 1);
113 /* octetDeltaCount, TLV type 1, u64 */
114 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
120 static inline ipfix_field_specifier_t *
121 flowprobe_template_ip6_fields (ipfix_field_specifier_t * f)
123 #define flowprobe_template_ip6_field_count() 4
124 /* sourceIpv6Address, TLV type 27, 16 octets */
125 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
126 sourceIPv6Address, 16);
128 /* destinationIPv6Address, TLV type 28, 16 octets */
129 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
130 destinationIPv6Address, 16);
132 /* protocolIdentifier, TLV type 4, u8 */
133 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
134 protocolIdentifier, 1);
136 /* octetDeltaCount, TLV type 1, u64 */
137 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
143 static inline ipfix_field_specifier_t *
144 flowprobe_template_l2_fields (ipfix_field_specifier_t * f)
146 #define flowprobe_template_l2_field_count() 3
147 /* sourceMacAddress, TLV type 56, u8[6] we hope */
148 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
149 sourceMacAddress, 6);
151 /* destinationMacAddress, TLV type 80, u8[6] we hope */
152 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
153 destinationMacAddress, 6);
155 /* ethernetType, TLV type 256, u16 */
156 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
162 static inline ipfix_field_specifier_t *
163 flowprobe_template_common_fields (ipfix_field_specifier_t * f)
165 #define flowprobe_template_common_field_count() 6
166 /* ingressInterface, TLV type 10, u32 */
167 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
168 ingressInterface, 4);
171 /* egressInterface, TLV type 14, u32 */
172 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
176 /* flowDirection, TLV type 61, u8 */
177 f->e_id_length = ipfix_e_id_length (0 /* enterprise */, flowDirection, 1);
180 /* packetDeltaCount, TLV type 2, u64 */
181 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
182 packetDeltaCount, 8);
185 /* flowStartNanoseconds, TLV type 156, u64 */
186 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
187 flowStartNanoseconds, 8);
190 /* flowEndNanoseconds, TLV type 157, u64 */
191 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
192 flowEndNanoseconds, 8);
198 static inline ipfix_field_specifier_t *
199 flowprobe_template_l4_fields (ipfix_field_specifier_t * f)
201 #define flowprobe_template_l4_field_count() 3
202 /* sourceTransportPort, TLV type 7, u16 */
203 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
204 sourceTransportPort, 2);
206 /* destinationTransportPort, TLV type 11, u16 */
207 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
208 destinationTransportPort, 2);
210 /* tcpControlBits, TLV type 6, u16 */
211 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
219 * @brief Create an IPFIX template packet rewrite string
220 * @param frm flow_report_main_t *
221 * @param fr flow_report_t *
222 * @param collector_address ip4_address_t * the IPFIX collector address
223 * @param src_address ip4_address_t * the source address we should use
224 * @param collector_port u16 the collector port we should use, host byte order
225 * @returns u8 * vector containing the indicated IPFIX template packet
228 flowprobe_template_rewrite_inline (ipfix_exporter_t *exp, flow_report_t *fr,
230 flowprobe_variant_t which)
234 ipfix_message_header_t *h;
235 ipfix_set_header_t *s;
236 ipfix_template_header_t *t;
237 ipfix_field_specifier_t *f;
238 ipfix_field_specifier_t *first_field;
240 ip4_ipfix_template_packet_t *tp;
242 flow_report_stream_t *stream;
243 flowprobe_main_t *fm = &flowprobe_main;
244 flowprobe_record_t flags = fr->opaque.as_uword;
245 bool collect_ip4 = false, collect_ip6 = false;
246 bool collect_l4 = false;
248 stream = &exp->streams[fr->stream_index];
250 if (flags & FLOW_RECORD_L3)
252 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
253 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
254 if (which == FLOW_VARIANT_L2_IP4)
255 flags |= FLOW_RECORD_L2_IP4;
256 if (which == FLOW_VARIANT_L2_IP6)
257 flags |= FLOW_RECORD_L2_IP6;
259 if (flags & FLOW_RECORD_L4)
261 collect_l4 = (which != FLOW_VARIANT_L2);
264 field_count += flowprobe_template_common_field_count ();
265 if (flags & FLOW_RECORD_L2)
266 field_count += flowprobe_template_l2_field_count ();
268 field_count += flowprobe_template_ip4_field_count ();
270 field_count += flowprobe_template_ip6_field_count ();
272 field_count += flowprobe_template_l4_field_count ();
274 /* allocate rewrite space */
276 (rewrite, sizeof (ip4_ipfix_template_packet_t)
277 + field_count * sizeof (ipfix_field_specifier_t) - 1,
278 CLIB_CACHE_LINE_BYTES);
280 tp = (ip4_ipfix_template_packet_t *) rewrite;
281 ip = (ip4_header_t *) & tp->ip4;
282 udp = (udp_header_t *) (ip + 1);
283 h = (ipfix_message_header_t *) (udp + 1);
284 s = (ipfix_set_header_t *) (h + 1);
285 t = (ipfix_template_header_t *) (s + 1);
286 first_field = f = (ipfix_field_specifier_t *) (t + 1);
288 ip->ip_version_and_header_length = 0x45;
290 ip->protocol = IP_PROTOCOL_UDP;
291 ip->src_address.as_u32 = exp->src_address.ip.ip4.as_u32;
292 ip->dst_address.as_u32 = exp->ipfix_collector.ip.ip4.as_u32;
293 udp->src_port = clib_host_to_net_u16 (stream->src_port);
294 udp->dst_port = clib_host_to_net_u16 (collector_port);
295 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
297 /* FIXUP: message header export_time */
298 /* FIXUP: message header sequence_number */
299 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
301 /* Add TLVs to the template */
302 f = flowprobe_template_common_fields (f);
304 if (flags & FLOW_RECORD_L2)
305 f = flowprobe_template_l2_fields (f);
307 f = flowprobe_template_ip4_fields (f);
309 f = flowprobe_template_ip6_fields (f);
311 f = flowprobe_template_l4_fields (f);
313 /* Back to the template packet... */
314 ip = (ip4_header_t *) & tp->ip4;
315 udp = (udp_header_t *) (ip + 1);
317 ASSERT (f - first_field);
318 /* Field count in this template */
319 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
321 fm->template_size[flags] = (u8 *) f - (u8 *) s;
323 /* set length in octets */
325 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
327 /* message length in octets */
328 h->version_length = version_length ((u8 *) f - (u8 *) h);
330 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
331 ip->checksum = ip4_header_checksum (ip);
337 flowprobe_template_rewrite_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
339 ipfix_report_element_t *elts, u32 n_elts,
342 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
347 flowprobe_template_rewrite_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
349 ipfix_report_element_t *elts, u32 n_elts,
352 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
357 flowprobe_template_rewrite_l2 (ipfix_exporter_t *exp, flow_report_t *fr,
359 ipfix_report_element_t *elts, u32 n_elts,
362 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
367 flowprobe_template_rewrite_l2_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
369 ipfix_report_element_t *elts, u32 n_elts,
372 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
373 FLOW_VARIANT_L2_IP4);
377 flowprobe_template_rewrite_l2_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
379 ipfix_report_element_t *elts, u32 n_elts,
382 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
383 FLOW_VARIANT_L2_IP6);
387 * @brief Flush accumulated data
388 * @param frm flow_report_main_t *
389 * @param fr flow_report_t *
390 * @param f vlib_frame_t *
393 * This function must simply return the incoming frame, or no template packets
397 flowprobe_data_callback_ip4 (flow_report_main_t *frm, ipfix_exporter_t *exp,
398 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
401 flowprobe_flush_callback_ip4 ();
406 flowprobe_data_callback_ip6 (flow_report_main_t *frm, ipfix_exporter_t *exp,
407 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
410 flowprobe_flush_callback_ip6 ();
415 flowprobe_data_callback_l2 (flow_report_main_t *frm, ipfix_exporter_t *exp,
416 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
419 flowprobe_flush_callback_l2 ();
424 flowprobe_template_add_del (u32 domain_id, u16 src_port,
425 flowprobe_record_t flags,
426 vnet_flow_data_callback_t * flow_data_callback,
427 vnet_flow_rewrite_callback_t * rewrite_callback,
428 bool is_add, u16 * template_id)
430 ipfix_exporter_t *exp = &flow_report_main.exporters[0];
431 vnet_flow_report_add_del_args_t a = {
432 .rewrite_callback = rewrite_callback,
433 .flow_data_callback = flow_data_callback,
435 .domain_id = domain_id,
436 .src_port = src_port,
437 .opaque.as_uword = flags,
439 return vnet_flow_report_add_del (exp, &a, template_id);
443 flowprobe_expired_timer_callback (u32 * expired_timers)
445 vlib_main_t *vm = vlib_get_main ();
446 flowprobe_main_t *fm = &flowprobe_main;
447 u32 my_cpu_number = vm->thread_index;
451 for (i = 0; i < vec_len (expired_timers); i++)
453 poolindex = expired_timers[i] & 0x7FFFFFFF;
454 vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex);
458 static clib_error_t *
459 flowprobe_create_state_tables (u32 active_timer)
461 flowprobe_main_t *fm = &flowprobe_main;
462 vlib_thread_main_t *tm = &vlib_thread_main;
463 vlib_main_t *vm = vlib_get_main ();
464 clib_error_t *error = 0;
468 /* Decide how many worker threads we have */
469 num_threads = 1 /* main thread */ + tm->n_threads;
471 /* Hash table per worker */
472 fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE;
474 /* Init per worker flow state and timer wheels */
477 vec_validate (fm->timers_per_worker, num_threads - 1);
478 vec_validate (fm->expired_passive_per_worker, num_threads - 1);
479 vec_validate (fm->hash_per_worker, num_threads - 1);
480 vec_validate (fm->pool_per_worker, num_threads - 1);
482 for (i = 0; i < num_threads; i++)
485 pool_alloc (fm->pool_per_worker[i], 1 << fm->ht_log2len);
486 vec_resize (fm->hash_per_worker[i], 1 << fm->ht_log2len);
487 for (j = 0; j < (1 << fm->ht_log2len); j++)
488 fm->hash_per_worker[i][j] = ~0;
489 fm->timers_per_worker[i] =
490 clib_mem_alloc (sizeof (TWT (tw_timer_wheel)));
491 tw_timer_wheel_init_2t_1w_2048sl (fm->timers_per_worker[i],
492 flowprobe_expired_timer_callback,
499 f64 now = vlib_time_now (vm);
500 vec_validate (fm->stateless_entry, num_threads - 1);
501 for (i = 0; i < num_threads; i++)
502 fm->stateless_entry[i].last_exported = now;
503 fm->disabled = false;
505 fm->initialized = true;
509 static clib_error_t *
510 flowprobe_clear_state_if_index (u32 sw_if_index)
512 flowprobe_main_t *fm = &flowprobe_main;
513 clib_error_t *error = 0;
517 if (fm->active_timer > 0)
519 vec_foreach_index (worker_i, fm->pool_per_worker)
521 pool_foreach_index (entry_i, fm->pool_per_worker[worker_i])
523 flowprobe_entry_t *e =
524 pool_elt_at_index (fm->pool_per_worker[worker_i], entry_i);
525 if (e->key.rx_sw_if_index == sw_if_index ||
526 e->key.tx_sw_if_index == sw_if_index)
530 e->prot.tcp.flags = 0;
531 if (fm->passive_timer > 0)
533 tw_timer_stop_2t_1w_2048sl (
534 fm->timers_per_worker[worker_i],
535 e->passive_timer_handle);
537 flowprobe_delete_by_index (worker_i, entry_i);
547 validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index,
550 vec_validate_init_empty (fm->flow_per_interface, sw_if_index, ~0);
551 vec_validate_init_empty (fm->direction_per_interface, sw_if_index, ~0);
553 if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0)
555 else if (fm->flow_per_interface[sw_if_index] != which)
562 * @brief configure / deconfigure the IPFIX flow-per-packet
563 * @param fm flowprobe_main_t * fm
564 * @param sw_if_index u32 the desired interface
565 * @param which u8 the desired datapath
566 * @param direction u8 the desired direction
567 * @param is_add int 1 to enable the feature, 0 to disable it
568 * @returns 0 if successful, non-zero otherwise
572 flowprobe_interface_add_del_feature (flowprobe_main_t *fm, u32 sw_if_index,
573 u8 which, u8 direction, int is_add)
575 vlib_main_t *vm = vlib_get_main ();
578 flowprobe_record_t flags = fm->record;
580 fm->flow_per_interface[sw_if_index] = (is_add) ? which : (u8) ~ 0;
581 fm->direction_per_interface[sw_if_index] = (is_add) ? direction : (u8) ~0;
582 fm->template_per_flow[which] += (is_add) ? 1 : -1;
583 if (is_add && fm->template_per_flow[which] > 1)
584 template_id = fm->template_reports[flags];
586 if ((is_add && fm->template_per_flow[which] == 1) ||
587 (!is_add && fm->template_per_flow[which] == 0))
589 if (which == FLOW_VARIANT_L2)
593 flowprobe_flush_callback_l2 ();
595 if (fm->record & FLOW_RECORD_L2)
597 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
598 flowprobe_data_callback_l2,
599 flowprobe_template_rewrite_l2,
600 is_add, &template_id);
601 fm->template_reports[flags] = (is_add) ? template_id : 0;
603 if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
605 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
606 flowprobe_data_callback_l2,
607 flowprobe_template_rewrite_l2_ip4,
608 is_add, &template_id);
609 fm->template_reports[flags | FLOW_RECORD_L2_IP4] =
610 (is_add) ? template_id : 0;
612 flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
613 flowprobe_data_callback_l2,
614 flowprobe_template_rewrite_l2_ip6,
615 is_add, &template_id);
616 fm->template_reports[flags | FLOW_RECORD_L2_IP6] =
617 (is_add) ? template_id : 0;
619 /* Special case L2 */
620 fm->context[FLOW_VARIANT_L2_IP4].flags =
621 flags | FLOW_RECORD_L2_IP4;
622 fm->context[FLOW_VARIANT_L2_IP6].flags =
623 flags | FLOW_RECORD_L2_IP6;
626 else if (which == FLOW_VARIANT_IP4)
630 flowprobe_flush_callback_ip4 ();
632 rv = flowprobe_template_add_del (
633 1, UDP_DST_PORT_ipfix, flags, flowprobe_data_callback_ip4,
634 flowprobe_template_rewrite_ip4, is_add, &template_id);
635 fm->template_reports[flags] = (is_add) ? template_id : 0;
637 else if (which == FLOW_VARIANT_IP6)
641 flowprobe_flush_callback_ip6 ();
643 rv = flowprobe_template_add_del (
644 1, UDP_DST_PORT_ipfix, flags, flowprobe_data_callback_ip6,
645 flowprobe_template_rewrite_ip6, is_add, &template_id);
646 fm->template_reports[flags] = (is_add) ? template_id : 0;
649 if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
651 clib_warning ("vnet_flow_report_add_del returned %d", rv);
655 if (which != (u8) ~ 0)
657 fm->context[which].flags = fm->record;
660 if (direction == FLOW_DIRECTION_RX || direction == FLOW_DIRECTION_BOTH)
662 if (which == FLOW_VARIANT_IP4)
664 vnet_feature_enable_disable ("ip4-unicast", "flowprobe-input-ip4",
665 sw_if_index, is_add, 0, 0);
666 vnet_feature_enable_disable ("ip4-multicast", "flowprobe-input-ip4",
667 sw_if_index, is_add, 0, 0);
669 else if (which == FLOW_VARIANT_IP6)
671 vnet_feature_enable_disable ("ip6-unicast", "flowprobe-input-ip6",
672 sw_if_index, is_add, 0, 0);
673 vnet_feature_enable_disable ("ip6-multicast", "flowprobe-input-ip6",
674 sw_if_index, is_add, 0, 0);
676 else if (which == FLOW_VARIANT_L2)
677 vnet_feature_enable_disable ("device-input", "flowprobe-input-l2",
678 sw_if_index, is_add, 0, 0);
681 if (direction == FLOW_DIRECTION_TX || direction == FLOW_DIRECTION_BOTH)
683 if (which == FLOW_VARIANT_IP4)
684 vnet_feature_enable_disable ("ip4-output", "flowprobe-output-ip4",
685 sw_if_index, is_add, 0, 0);
686 else if (which == FLOW_VARIANT_IP6)
687 vnet_feature_enable_disable ("ip6-output", "flowprobe-output-ip6",
688 sw_if_index, is_add, 0, 0);
689 else if (which == FLOW_VARIANT_L2)
690 vnet_feature_enable_disable ("interface-output", "flowprobe-output-l2",
691 sw_if_index, is_add, 0, 0);
694 /* Stateful flow collection */
695 if (is_add && !fm->initialized)
697 flowprobe_create_state_tables (fm->active_timer);
698 if (fm->active_timer)
699 vlib_process_signal_event (vm, flowprobe_timer_node.index, 1, 0);
702 if (!is_add && fm->initialized)
704 flowprobe_clear_state_if_index (sw_if_index);
711 * @brief API message handler
712 * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
714 void vl_api_flowprobe_tx_interface_add_del_t_handler
715 (vl_api_flowprobe_tx_interface_add_del_t * mp)
717 flowprobe_main_t *fm = &flowprobe_main;
718 vl_api_flowprobe_tx_interface_add_del_reply_t *rmp;
719 u32 sw_if_index = ntohl (mp->sw_if_index);
722 VALIDATE_SW_IF_INDEX (mp);
726 clib_warning ("Please specify flowprobe params record first...");
727 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
731 rv = validate_feature_on_interface (fm, sw_if_index, mp->which);
732 if ((rv == 1 && mp->is_add == 1) || rv == 0)
734 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
738 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, mp->which,
739 FLOW_DIRECTION_TX, mp->is_add);
742 BAD_SW_IF_INDEX_LABEL;
744 REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY);
748 vl_api_flowprobe_interface_add_del_t_handler (
749 vl_api_flowprobe_interface_add_del_t *mp)
751 flowprobe_main_t *fm = &flowprobe_main;
752 vl_api_flowprobe_interface_add_del_reply_t *rmp;
759 VALIDATE_SW_IF_INDEX (mp);
761 sw_if_index = ntohl (mp->sw_if_index);
764 if (mp->which == FLOWPROBE_WHICH_IP4)
765 which = FLOW_VARIANT_IP4;
766 else if (mp->which == FLOWPROBE_WHICH_IP6)
767 which = FLOW_VARIANT_IP6;
768 else if (mp->which == FLOWPROBE_WHICH_L2)
769 which = FLOW_VARIANT_L2;
772 clib_warning ("Invalid value of which");
773 rv = VNET_API_ERROR_INVALID_VALUE;
777 if (mp->direction == FLOWPROBE_DIRECTION_RX)
778 direction = FLOW_DIRECTION_RX;
779 else if (mp->direction == FLOWPROBE_DIRECTION_TX)
780 direction = FLOW_DIRECTION_TX;
781 else if (mp->direction == FLOWPROBE_DIRECTION_BOTH)
782 direction = FLOW_DIRECTION_BOTH;
785 clib_warning ("Invalid value of direction");
786 rv = VNET_API_ERROR_INVALID_VALUE;
792 clib_warning ("Please specify flowprobe params record first");
793 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
797 rv = validate_feature_on_interface (fm, sw_if_index, which);
802 clib_warning ("Variant is already enabled for given interface");
803 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
809 clib_warning ("Interface has different variant enabled");
810 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
817 clib_warning ("Interface has no variant enabled");
818 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
823 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
827 BAD_SW_IF_INDEX_LABEL;
829 REPLY_MACRO (VL_API_FLOWPROBE_INTERFACE_ADD_DEL_REPLY);
833 send_flowprobe_interface_details (u32 sw_if_index, u8 which, u8 direction,
834 vl_api_registration_t *reg, u32 context)
836 flowprobe_main_t *fm = &flowprobe_main;
837 vl_api_flowprobe_interface_details_t *rmp = 0;
839 rmp = vl_msg_api_alloc (sizeof (*rmp));
842 clib_memset (rmp, 0, sizeof (*rmp));
844 ntohs (VL_API_FLOWPROBE_INTERFACE_DETAILS + REPLY_MSG_ID_BASE);
845 rmp->context = context;
847 rmp->sw_if_index = htonl (sw_if_index);
849 if (which == FLOW_VARIANT_IP4)
850 rmp->which = FLOWPROBE_WHICH_IP4;
851 else if (which == FLOW_VARIANT_IP6)
852 rmp->which = FLOWPROBE_WHICH_IP6;
853 else if (which == FLOW_VARIANT_L2)
854 rmp->which = FLOWPROBE_WHICH_L2;
858 if (direction == FLOW_DIRECTION_RX)
859 rmp->direction = FLOWPROBE_DIRECTION_RX;
860 else if (direction == FLOW_DIRECTION_TX)
861 rmp->direction = FLOWPROBE_DIRECTION_TX;
862 else if (direction == FLOW_DIRECTION_BOTH)
863 rmp->direction = FLOWPROBE_DIRECTION_BOTH;
867 vl_api_send_msg (reg, (u8 *) rmp);
871 vl_api_flowprobe_interface_dump_t_handler (
872 vl_api_flowprobe_interface_dump_t *mp)
874 flowprobe_main_t *fm = &flowprobe_main;
875 vl_api_registration_t *reg;
878 reg = vl_api_client_index_to_registration (mp->client_index);
882 sw_if_index = ntohl (mp->sw_if_index);
884 if (sw_if_index == ~0)
888 vec_foreach (which, fm->flow_per_interface)
890 if (*which == (u8) ~0)
893 sw_if_index = which - fm->flow_per_interface;
894 send_flowprobe_interface_details (
895 sw_if_index, *which, fm->direction_per_interface[sw_if_index], reg,
899 else if (vec_len (fm->flow_per_interface) > sw_if_index &&
900 fm->flow_per_interface[sw_if_index] != (u8) ~0)
902 send_flowprobe_interface_details (
903 sw_if_index, fm->flow_per_interface[sw_if_index],
904 fm->direction_per_interface[sw_if_index], reg, mp->context);
908 #define vec_neg_search(v,E) \
911 while (_v(i) < vec_len(v) && v[_v(i)] == E) \
915 if (_v(i) == vec_len(v)) \
921 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
922 u8 record_l3, u8 record_l4,
923 u32 active_timer, u32 passive_timer)
925 flowprobe_record_t flags = 0;
927 if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
928 return VNET_API_ERROR_UNSUPPORTED;
931 flags |= FLOW_RECORD_L2;
933 flags |= FLOW_RECORD_L3;
935 flags |= FLOW_RECORD_L4;
940 * Timers: ~0 is default, 0 is off
943 (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
945 (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
951 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
953 flowprobe_main_t *fm = &flowprobe_main;
954 vl_api_flowprobe_params_reply_t *rmp;
957 rv = flowprobe_params
959 mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
960 mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
961 mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
962 clib_net_to_host_u32 (mp->active_timer),
963 clib_net_to_host_u32 (mp->passive_timer));
965 REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
969 vl_api_flowprobe_set_params_t_handler (vl_api_flowprobe_set_params_t *mp)
971 flowprobe_main_t *fm = &flowprobe_main;
972 vl_api_flowprobe_set_params_reply_t *rmp;
973 bool record_l2, record_l3, record_l4;
978 record_l2 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L2);
979 record_l3 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L3);
980 record_l4 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L4);
982 active_timer = clib_net_to_host_u32 (mp->active_timer);
983 passive_timer = clib_net_to_host_u32 (mp->passive_timer);
985 if (passive_timer > 0 && active_timer > passive_timer)
987 clib_warning ("Passive timer must be greater than active timer");
988 rv = VNET_API_ERROR_INVALID_VALUE;
992 rv = flowprobe_params (fm, record_l2, record_l3, record_l4, active_timer,
994 if (rv == VNET_API_ERROR_UNSUPPORTED)
996 "Cannot change params when feature is enabled on some interfaces");
999 REPLY_MACRO (VL_API_FLOWPROBE_SET_PARAMS_REPLY);
1003 vl_api_flowprobe_get_params_t_handler (vl_api_flowprobe_get_params_t *mp)
1005 flowprobe_main_t *fm = &flowprobe_main;
1006 vl_api_flowprobe_get_params_reply_t *rmp;
1007 u8 record_flags = 0;
1010 if (fm->record & FLOW_RECORD_L2)
1011 record_flags |= FLOWPROBE_RECORD_FLAG_L2;
1012 if (fm->record & FLOW_RECORD_L3)
1013 record_flags |= FLOWPROBE_RECORD_FLAG_L3;
1014 if (fm->record & FLOW_RECORD_L4)
1015 record_flags |= FLOWPROBE_RECORD_FLAG_L4;
1018 REPLY_MACRO2 (VL_API_FLOWPROBE_GET_PARAMS_REPLY,
1020 rmp->record_flags = record_flags;
1021 rmp->active_timer = htonl (fm->active_timer);
1022 rmp->passive_timer = htonl (fm->passive_timer);
1027 VLIB_PLUGIN_REGISTER () = {
1028 .version = VPP_BUILD_VER,
1029 .description = "Flow per Packet",
1033 format_flowprobe_direction (u8 *s, va_list *args)
1035 u8 *direction = va_arg (*args, u8 *);
1036 if (*direction == FLOW_DIRECTION_RX)
1037 s = format (s, "rx");
1038 else if (*direction == FLOW_DIRECTION_TX)
1039 s = format (s, "tx");
1040 else if (*direction == FLOW_DIRECTION_BOTH)
1041 s = format (s, "rx tx");
1047 format_flowprobe_entry (u8 * s, va_list * args)
1049 flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
1050 s = format (s, " %U", format_flowprobe_direction, &e->key.direction);
1051 s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
1053 s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
1054 format_ethernet_address, &e->key.dst_mac);
1055 s = format (s, " %U -> %U",
1056 format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
1057 format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
1058 s = format (s, " %d", e->key.protocol);
1059 s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
1060 clib_net_to_host_u16 (e->key.dst_port));
1066 format_flowprobe_feature (u8 * s, va_list * args)
1068 u8 *which = va_arg (*args, u8 *);
1069 if (*which == FLOW_VARIANT_IP4)
1070 s = format (s, "ip4");
1071 else if (*which == FLOW_VARIANT_IP6)
1072 s = format (s, "ip6");
1073 else if (*which == FLOW_VARIANT_L2)
1074 s = format (s, "l2");
1080 format_flowprobe_params (u8 * s, va_list * args)
1082 flowprobe_record_t flags = va_arg (*args, flowprobe_record_t);
1083 u32 active_timer = va_arg (*args, u32);
1084 u32 passive_timer = va_arg (*args, u32);
1086 if (flags & FLOW_RECORD_L2)
1087 s = format (s, " l2");
1088 if (flags & FLOW_RECORD_L3)
1089 s = format (s, " l3");
1090 if (flags & FLOW_RECORD_L4)
1091 s = format (s, " l4");
1093 if (active_timer != (u32) ~ 0)
1094 s = format (s, " active: %d", active_timer);
1096 if (passive_timer != (u32) ~ 0)
1097 s = format (s, " passive: %d", passive_timer);
1102 static clib_error_t *
1103 flowprobe_show_table_fn (vlib_main_t * vm,
1104 unformat_input_t * input, vlib_cli_command_t * cm)
1106 flowprobe_main_t *fm = &flowprobe_main;
1108 flowprobe_entry_t *e;
1110 vlib_cli_output (vm, "Dumping IPFIX table");
1112 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
1114 pool_foreach (e, fm->pool_per_worker[i])
1116 vlib_cli_output (vm, "%U",
1117 format_flowprobe_entry,
1125 static clib_error_t *
1126 flowprobe_show_stats_fn (vlib_main_t * vm,
1127 unformat_input_t * input, vlib_cli_command_t * cm)
1129 flowprobe_main_t *fm = &flowprobe_main;
1132 vlib_cli_output (vm, "IPFIX table statistics");
1133 vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
1134 vlib_cli_output (vm, "Flow pool size per thread: %d\n",
1135 0x1 << FLOWPROBE_LOG2_HASHSIZE);
1137 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
1138 vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
1139 (100 * pool_elts (fm->pool_per_worker[i])) /
1140 (0x1 << FLOWPROBE_LOG2_HASHSIZE));
1144 static clib_error_t *
1145 flowprobe_interface_add_del_feature_command_fn (vlib_main_t *vm,
1146 unformat_input_t *input,
1147 vlib_cli_command_t *cmd)
1149 flowprobe_main_t *fm = &flowprobe_main;
1150 u32 sw_if_index = ~0;
1152 u8 which = FLOW_VARIANT_IP4;
1153 flowprobe_direction_t direction = FLOW_DIRECTION_TX;
1156 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1158 if (unformat (input, "disable"))
1160 else if (unformat (input, "%U", unformat_vnet_sw_interface,
1161 fm->vnet_main, &sw_if_index));
1162 else if (unformat (input, "ip4"))
1163 which = FLOW_VARIANT_IP4;
1164 else if (unformat (input, "ip6"))
1165 which = FLOW_VARIANT_IP6;
1166 else if (unformat (input, "l2"))
1167 which = FLOW_VARIANT_L2;
1168 else if (unformat (input, "rx"))
1169 direction = FLOW_DIRECTION_RX;
1170 else if (unformat (input, "tx"))
1171 direction = FLOW_DIRECTION_TX;
1172 else if (unformat (input, "both"))
1173 direction = FLOW_DIRECTION_BOTH;
1178 if (fm->record == 0)
1179 return clib_error_return (0,
1180 "Please specify flowprobe params record first...");
1182 if (sw_if_index == ~0)
1183 return clib_error_return (0, "Please specify an interface...");
1185 rv = validate_feature_on_interface (fm, sw_if_index, which);
1189 return clib_error_return (0,
1190 "Datapath is already enabled for given interface...");
1193 return clib_error_return (0,
1194 "Interface has enable different datapath ...");
1199 return clib_error_return (0, "Interface has no datapath enabled");
1203 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
1210 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
1211 return clib_error_return
1212 (0, "Invalid interface, only works on physical ports");
1215 case VNET_API_ERROR_UNIMPLEMENTED:
1216 return clib_error_return (0, "ip6 not supported");
1220 return clib_error_return (0, "flowprobe_enable_disable returned %d",
1226 static clib_error_t *
1227 flowprobe_show_feature_command_fn (vlib_main_t * vm,
1228 unformat_input_t * input,
1229 vlib_cli_command_t * cmd)
1231 flowprobe_main_t *fm = &flowprobe_main;
1235 vec_foreach (which, fm->flow_per_interface)
1237 if (*which == (u8) ~ 0)
1240 sw_if_index = which - fm->flow_per_interface;
1241 vlib_cli_output (vm, " %U %U %U", format_vnet_sw_if_index_name,
1242 vnet_get_main (), sw_if_index, format_flowprobe_feature,
1243 which, format_flowprobe_direction,
1244 &fm->direction_per_interface[sw_if_index]);
1249 static clib_error_t *
1250 flowprobe_params_command_fn (vlib_main_t * vm,
1251 unformat_input_t * input,
1252 vlib_cli_command_t * cmd)
1254 flowprobe_main_t *fm = &flowprobe_main;
1255 bool record_l2 = false, record_l3 = false, record_l4 = false;
1256 u32 active_timer = ~0;
1257 u32 passive_timer = ~0;
1259 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1261 if (unformat (input, "active %d", &active_timer))
1263 else if (unformat (input, "passive %d", &passive_timer))
1265 else if (unformat (input, "record"))
1266 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1268 if (unformat (input, "l2"))
1270 else if (unformat (input, "l3"))
1272 else if (unformat (input, "l4"))
1281 if (passive_timer > 0 && active_timer > passive_timer)
1282 return clib_error_return (0,
1283 "Passive timer has to be greater than active one...");
1285 if (flowprobe_params (fm, record_l2, record_l3, record_l4,
1286 active_timer, passive_timer))
1287 return clib_error_return (0,
1288 "Couldn't change flowperpacket params when feature is enabled on some interface ...");
1292 static clib_error_t *
1293 flowprobe_show_params_command_fn (vlib_main_t * vm,
1294 unformat_input_t * input,
1295 vlib_cli_command_t * cmd)
1297 flowprobe_main_t *fm = &flowprobe_main;
1298 flowprobe_record_t flags = fm->record;
1299 u32 active_timer = fm->active_timer;
1300 u32 passive_timer = fm->passive_timer;
1302 vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
1308 * '<em>flowprobe feature add-del</em>' commands to enable/disable
1309 * per-packet IPFIX flow record generation on an interface
1313 * To enable per-packet IPFIX flow-record generation on an interface:
1314 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
1316 * To disable per-packet IPFIX flow-record generation on an interface:
1317 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
1321 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
1322 .path = "flowprobe feature add-del",
1323 .short_help = "flowprobe feature add-del <interface-name> [(l2|ip4|ip6)] "
1324 "[(rx|tx|both)] [disable]",
1325 .function = flowprobe_interface_add_del_feature_command_fn,
1327 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
1328 .path = "flowprobe params",
1329 .short_help = "flowprobe params record [l2] [l3] [l4] [active <timer>] "
1330 "[passive <timer>]",
1331 .function = flowprobe_params_command_fn,
1334 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
1335 .path = "show flowprobe feature",
1337 "show flowprobe feature",
1338 .function = flowprobe_show_feature_command_fn,
1340 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
1341 .path = "show flowprobe params",
1343 "show flowprobe params",
1344 .function = flowprobe_show_params_command_fn,
1346 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
1347 .path = "show flowprobe table",
1348 .short_help = "show flowprobe table",
1349 .function = flowprobe_show_table_fn,
1351 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
1352 .path = "show flowprobe statistics",
1353 .short_help = "show flowprobe statistics",
1354 .function = flowprobe_show_stats_fn,
1358 * Main-core process, sending an interrupt to the per worker input
1359 * process that spins the per worker timer wheel.
1362 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1364 uword *event_data = 0;
1365 vlib_main_t **worker_vms = 0, *worker_vm;
1366 flowprobe_main_t *fm = &flowprobe_main;
1368 /* Wait for Godot... */
1369 vlib_process_wait_for_event_or_clock (vm, 1e9);
1370 uword event_type = vlib_process_get_events (vm, &event_data);
1371 if (event_type != 1)
1372 clib_warning ("bogus kickoff event received, %d", event_type);
1373 vec_reset_length (event_data);
1376 if (vlib_get_n_threads () == 0)
1377 vec_add1 (worker_vms, vm);
1380 for (i = 0; i < vlib_get_n_threads (); i++)
1382 worker_vm = vlib_get_main_by_index (i);
1384 vec_add1 (worker_vms, worker_vm);
1387 f64 sleep_duration = 0.1;
1391 /* Send an interrupt to each timer input node */
1392 sleep_duration = 0.1;
1393 for (i = 0; i < vec_len (worker_vms); i++)
1395 worker_vm = worker_vms[i];
1398 vlib_node_set_interrupt_pending (worker_vm,
1399 flowprobe_walker_node.index);
1401 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1404 vlib_process_suspend (vm, sleep_duration);
1406 return 0; /* or not */
1409 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1410 .function = timer_process,
1411 .name = "flowprobe-timer-process",
1412 .type = VLIB_NODE_TYPE_PROCESS,
1415 #include <flowprobe/flowprobe.api.c>
1418 * @brief Set up the API message handling tables
1419 * @param vm vlib_main_t * vlib main data structure pointer
1420 * @returns 0 to indicate all is well, or a clib_error_t
1422 static clib_error_t *
1423 flowprobe_init (vlib_main_t * vm)
1425 flowprobe_main_t *fm = &flowprobe_main;
1426 vlib_thread_main_t *tm = &vlib_thread_main;
1427 clib_error_t *error = 0;
1431 fm->vnet_main = vnet_get_main ();
1433 /* Ask for a correctly-sized block of API message decode slots */
1434 fm->msg_id_base = setup_message_id_table ();
1436 /* Set up time reference pair */
1437 fm->vlib_time_0 = vlib_time_now (vm);
1438 fm->nanosecond_time_0 = unix_time_now_nsec ();
1440 clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1441 clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1442 clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1444 /* Decide how many worker threads we have */
1445 num_threads = 1 /* main thread */ + tm->n_threads;
1447 /* Allocate per worker thread vectors per flavour */
1448 for (i = 0; i < FLOW_N_VARIANTS; i++)
1450 vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1451 vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1452 vec_validate (fm->context[i].next_record_offset_per_worker,
1456 fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1457 fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1462 VLIB_INIT_FUNCTION (flowprobe_init);
1465 * fd.io coding-style-patch-verification: ON
1468 * eval: (c-set-style "gnu")