2 * flowprobe.c - ipfix probe plugin
4 * Copyright (c) 2016 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * @brief Per-packet IPFIX flow record generator plugin
22 * This file implements vpp plugin registration mechanics,
23 * debug CLI, and binary API handling.
26 #include <vnet/vnet.h>
27 #include <vpp/app/version.h>
28 #include <vnet/plugin/plugin.h>
29 #include <vnet/udp/udp_local.h>
30 #include <flowprobe/flowprobe.h>
32 #include <vlibapi/api.h>
33 #include <vlibmemory/api.h>
35 /* define message IDs */
36 #include <flowprobe/flowprobe.api_enum.h>
37 #include <flowprobe/flowprobe.api_types.h>
39 flowprobe_main_t flowprobe_main;
40 static vlib_node_registration_t flowprobe_timer_node;
41 uword flowprobe_walker_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
44 #define REPLY_MSG_ID_BASE fm->msg_id_base
45 #include <vlibapi/api_helper_macros.h>
47 /* Define the per-interface configurable features */
49 VNET_FEATURE_INIT (flow_perpacket_ip4, static) =
51 .arc_name = "ip4-output",
52 .node_name = "flowprobe-ip4",
53 .runs_before = VNET_FEATURES ("interface-output"),
56 VNET_FEATURE_INIT (flow_perpacket_ip6, static) =
58 .arc_name = "ip6-output",
59 .node_name = "flowprobe-ip6",
60 .runs_before = VNET_FEATURES ("interface-output"),
63 VNET_FEATURE_INIT (flow_perpacket_l2, static) = {
64 .arc_name = "interface-output",
65 .node_name = "flowprobe-l2",
66 .runs_before = VNET_FEATURES ("interface-output-arc-end"),
70 /* Macro to finish up custom dump fns */
71 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
74 vl_print (handle, (char *)s); \
78 static inline ipfix_field_specifier_t *
79 flowprobe_template_ip4_fields (ipfix_field_specifier_t * f)
81 #define flowprobe_template_ip4_field_count() 4
82 /* sourceIpv4Address, TLV type 8, u32 */
83 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
84 sourceIPv4Address, 4);
86 /* destinationIPv4Address, TLV type 12, u32 */
87 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
88 destinationIPv4Address, 4);
90 /* protocolIdentifier, TLV type 4, u8 */
91 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
92 protocolIdentifier, 1);
94 /* octetDeltaCount, TLV type 1, u64 */
95 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
101 static inline ipfix_field_specifier_t *
102 flowprobe_template_ip6_fields (ipfix_field_specifier_t * f)
104 #define flowprobe_template_ip6_field_count() 4
105 /* sourceIpv6Address, TLV type 27, 16 octets */
106 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
107 sourceIPv6Address, 16);
109 /* destinationIPv6Address, TLV type 28, 16 octets */
110 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
111 destinationIPv6Address, 16);
113 /* protocolIdentifier, TLV type 4, u8 */
114 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
115 protocolIdentifier, 1);
117 /* octetDeltaCount, TLV type 1, u64 */
118 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
124 static inline ipfix_field_specifier_t *
125 flowprobe_template_l2_fields (ipfix_field_specifier_t * f)
127 #define flowprobe_template_l2_field_count() 3
128 /* sourceMacAddress, TLV type 56, u8[6] we hope */
129 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
130 sourceMacAddress, 6);
132 /* destinationMacAddress, TLV type 80, u8[6] we hope */
133 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
134 destinationMacAddress, 6);
136 /* ethernetType, TLV type 256, u16 */
137 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
143 static inline ipfix_field_specifier_t *
144 flowprobe_template_common_fields (ipfix_field_specifier_t * f)
146 #define flowprobe_template_common_field_count() 5
147 /* ingressInterface, TLV type 10, u32 */
148 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
149 ingressInterface, 4);
152 /* egressInterface, TLV type 14, u32 */
153 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
157 /* packetDeltaCount, TLV type 2, u64 */
158 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
159 packetDeltaCount, 8);
162 /* flowStartNanoseconds, TLV type 156, u64 */
163 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
164 flowStartNanoseconds, 8);
167 /* flowEndNanoseconds, TLV type 157, u64 */
168 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
169 flowEndNanoseconds, 8);
175 static inline ipfix_field_specifier_t *
176 flowprobe_template_l4_fields (ipfix_field_specifier_t * f)
178 #define flowprobe_template_l4_field_count() 3
179 /* sourceTransportPort, TLV type 7, u16 */
180 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
181 sourceTransportPort, 2);
183 /* destinationTransportPort, TLV type 11, u16 */
184 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
185 destinationTransportPort, 2);
187 /* tcpControlBits, TLV type 6, u16 */
188 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
196 * @brief Create an IPFIX template packet rewrite string
197 * @param frm flow_report_main_t *
198 * @param fr flow_report_t *
199 * @param collector_address ip4_address_t * the IPFIX collector address
200 * @param src_address ip4_address_t * the source address we should use
201 * @param collector_port u16 the collector port we should use, host byte order
202 * @returns u8 * vector containing the indicated IPFIX template packet
205 flowprobe_template_rewrite_inline (ipfix_exporter_t *exp, flow_report_t *fr,
206 ip4_address_t *collector_address,
207 ip4_address_t *src_address,
209 flowprobe_variant_t which)
213 ipfix_message_header_t *h;
214 ipfix_set_header_t *s;
215 ipfix_template_header_t *t;
216 ipfix_field_specifier_t *f;
217 ipfix_field_specifier_t *first_field;
219 ip4_ipfix_template_packet_t *tp;
221 flow_report_stream_t *stream;
222 flowprobe_main_t *fm = &flowprobe_main;
223 flowprobe_record_t flags = fr->opaque.as_uword;
224 bool collect_ip4 = false, collect_ip6 = false;
226 stream = &exp->streams[fr->stream_index];
228 if (flags & FLOW_RECORD_L3)
230 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
231 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
232 if (which == FLOW_VARIANT_L2_IP4)
233 flags |= FLOW_RECORD_L2_IP4;
234 if (which == FLOW_VARIANT_L2_IP6)
235 flags |= FLOW_RECORD_L2_IP6;
238 field_count += flowprobe_template_common_field_count ();
239 if (flags & FLOW_RECORD_L2)
240 field_count += flowprobe_template_l2_field_count ();
242 field_count += flowprobe_template_ip4_field_count ();
244 field_count += flowprobe_template_ip6_field_count ();
245 if (flags & FLOW_RECORD_L4)
246 field_count += flowprobe_template_l4_field_count ();
248 /* allocate rewrite space */
250 (rewrite, sizeof (ip4_ipfix_template_packet_t)
251 + field_count * sizeof (ipfix_field_specifier_t) - 1,
252 CLIB_CACHE_LINE_BYTES);
254 tp = (ip4_ipfix_template_packet_t *) rewrite;
255 ip = (ip4_header_t *) & tp->ip4;
256 udp = (udp_header_t *) (ip + 1);
257 h = (ipfix_message_header_t *) (udp + 1);
258 s = (ipfix_set_header_t *) (h + 1);
259 t = (ipfix_template_header_t *) (s + 1);
260 first_field = f = (ipfix_field_specifier_t *) (t + 1);
262 ip->ip_version_and_header_length = 0x45;
264 ip->protocol = IP_PROTOCOL_UDP;
265 ip->src_address.as_u32 = src_address->as_u32;
266 ip->dst_address.as_u32 = collector_address->as_u32;
267 udp->src_port = clib_host_to_net_u16 (stream->src_port);
268 udp->dst_port = clib_host_to_net_u16 (collector_port);
269 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
271 /* FIXUP: message header export_time */
272 /* FIXUP: message header sequence_number */
273 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
275 /* Add TLVs to the template */
276 f = flowprobe_template_common_fields (f);
278 if (flags & FLOW_RECORD_L2)
279 f = flowprobe_template_l2_fields (f);
281 f = flowprobe_template_ip4_fields (f);
283 f = flowprobe_template_ip6_fields (f);
284 if (flags & FLOW_RECORD_L4)
285 f = flowprobe_template_l4_fields (f);
287 /* Back to the template packet... */
288 ip = (ip4_header_t *) & tp->ip4;
289 udp = (udp_header_t *) (ip + 1);
291 ASSERT (f - first_field);
292 /* Field count in this template */
293 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
295 fm->template_size[flags] = (u8 *) f - (u8 *) s;
297 /* set length in octets */
299 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
301 /* message length in octets */
302 h->version_length = version_length ((u8 *) f - (u8 *) h);
304 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
305 ip->checksum = ip4_header_checksum (ip);
311 flowprobe_template_rewrite_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
312 ip4_address_t *collector_address,
313 ip4_address_t *src_address, u16 collector_port,
314 ipfix_report_element_t *elts, u32 n_elts,
317 return flowprobe_template_rewrite_inline (
318 exp, fr, collector_address, src_address, collector_port, FLOW_VARIANT_IP6);
322 flowprobe_template_rewrite_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
323 ip4_address_t *collector_address,
324 ip4_address_t *src_address, u16 collector_port,
325 ipfix_report_element_t *elts, u32 n_elts,
328 return flowprobe_template_rewrite_inline (
329 exp, fr, collector_address, src_address, collector_port, FLOW_VARIANT_IP4);
333 flowprobe_template_rewrite_l2 (ipfix_exporter_t *exp, flow_report_t *fr,
334 ip4_address_t *collector_address,
335 ip4_address_t *src_address, u16 collector_port,
336 ipfix_report_element_t *elts, u32 n_elts,
339 return flowprobe_template_rewrite_inline (
340 exp, fr, collector_address, src_address, collector_port, FLOW_VARIANT_L2);
344 flowprobe_template_rewrite_l2_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
345 ip4_address_t *collector_address,
346 ip4_address_t *src_address,
348 ipfix_report_element_t *elts, u32 n_elts,
351 return flowprobe_template_rewrite_inline (exp, fr, collector_address,
352 src_address, collector_port,
353 FLOW_VARIANT_L2_IP4);
357 flowprobe_template_rewrite_l2_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
358 ip4_address_t *collector_address,
359 ip4_address_t *src_address,
361 ipfix_report_element_t *elts, u32 n_elts,
364 return flowprobe_template_rewrite_inline (exp, fr, collector_address,
365 src_address, collector_port,
366 FLOW_VARIANT_L2_IP6);
370 * @brief Flush accumulated data
371 * @param frm flow_report_main_t *
372 * @param fr flow_report_t *
373 * @param f vlib_frame_t *
376 * This function must simply return the incoming frame, or no template packets
380 flowprobe_data_callback_ip4 (flow_report_main_t *frm, ipfix_exporter_t *exp,
381 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
384 flowprobe_flush_callback_ip4 ();
389 flowprobe_data_callback_ip6 (flow_report_main_t *frm, ipfix_exporter_t *exp,
390 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
393 flowprobe_flush_callback_ip6 ();
398 flowprobe_data_callback_l2 (flow_report_main_t *frm, ipfix_exporter_t *exp,
399 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
402 flowprobe_flush_callback_l2 ();
407 flowprobe_template_add_del (u32 domain_id, u16 src_port,
408 flowprobe_record_t flags,
409 vnet_flow_data_callback_t * flow_data_callback,
410 vnet_flow_rewrite_callback_t * rewrite_callback,
411 bool is_add, u16 * template_id)
413 ipfix_exporter_t *exp = &flow_report_main.exporters[0];
414 vnet_flow_report_add_del_args_t a = {
415 .rewrite_callback = rewrite_callback,
416 .flow_data_callback = flow_data_callback,
418 .domain_id = domain_id,
419 .src_port = src_port,
420 .opaque.as_uword = flags,
422 return vnet_flow_report_add_del (exp, &a, template_id);
426 flowprobe_expired_timer_callback (u32 * expired_timers)
428 vlib_main_t *vm = vlib_get_main ();
429 flowprobe_main_t *fm = &flowprobe_main;
430 u32 my_cpu_number = vm->thread_index;
434 for (i = 0; i < vec_len (expired_timers); i++)
436 poolindex = expired_timers[i] & 0x7FFFFFFF;
437 vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex);
441 static clib_error_t *
442 flowprobe_create_state_tables (u32 active_timer)
444 flowprobe_main_t *fm = &flowprobe_main;
445 vlib_thread_main_t *tm = &vlib_thread_main;
446 vlib_main_t *vm = vlib_get_main ();
447 clib_error_t *error = 0;
451 /* Decide how many worker threads we have */
452 num_threads = 1 /* main thread */ + tm->n_threads;
454 /* Hash table per worker */
455 fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE;
457 /* Init per worker flow state and timer wheels */
460 vec_validate (fm->timers_per_worker, num_threads - 1);
461 vec_validate (fm->expired_passive_per_worker, num_threads - 1);
462 vec_validate (fm->hash_per_worker, num_threads - 1);
463 vec_validate (fm->pool_per_worker, num_threads - 1);
465 for (i = 0; i < num_threads; i++)
468 pool_alloc (fm->pool_per_worker[i], 1 << fm->ht_log2len);
469 vec_resize (fm->hash_per_worker[i], 1 << fm->ht_log2len);
470 for (j = 0; j < (1 << fm->ht_log2len); j++)
471 fm->hash_per_worker[i][j] = ~0;
472 fm->timers_per_worker[i] =
473 clib_mem_alloc (sizeof (TWT (tw_timer_wheel)));
474 tw_timer_wheel_init_2t_1w_2048sl (fm->timers_per_worker[i],
475 flowprobe_expired_timer_callback,
482 f64 now = vlib_time_now (vm);
483 vec_validate (fm->stateless_entry, num_threads - 1);
484 for (i = 0; i < num_threads; i++)
485 fm->stateless_entry[i].last_exported = now;
486 fm->disabled = false;
488 fm->initialized = true;
493 validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index,
496 vec_validate_init_empty (fm->flow_per_interface, sw_if_index, ~0);
498 if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0)
500 else if (fm->flow_per_interface[sw_if_index] != which)
507 * @brief configure / deconfigure the IPFIX flow-per-packet
508 * @param fm flowprobe_main_t * fm
509 * @param sw_if_index u32 the desired interface
510 * @param is_add int 1 to enable the feature, 0 to disable it
511 * @returns 0 if successful, non-zero otherwise
515 flowprobe_tx_interface_add_del_feature (flowprobe_main_t * fm,
516 u32 sw_if_index, u8 which, int is_add)
518 vlib_main_t *vm = vlib_get_main ();
521 flowprobe_record_t flags = fm->record;
523 fm->flow_per_interface[sw_if_index] = (is_add) ? which : (u8) ~ 0;
524 fm->template_per_flow[which] += (is_add) ? 1 : -1;
525 if (is_add && fm->template_per_flow[which] > 1)
526 template_id = fm->template_reports[flags];
528 if ((is_add && fm->template_per_flow[which] == 1) ||
529 (!is_add && fm->template_per_flow[which] == 0))
531 if (which == FLOW_VARIANT_L2)
533 if (fm->record & FLOW_RECORD_L2)
535 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
536 flowprobe_data_callback_l2,
537 flowprobe_template_rewrite_l2,
538 is_add, &template_id);
540 if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
542 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
543 flowprobe_data_callback_l2,
544 flowprobe_template_rewrite_l2_ip4,
545 is_add, &template_id);
546 fm->template_reports[flags | FLOW_RECORD_L2_IP4] =
547 (is_add) ? template_id : 0;
549 flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
550 flowprobe_data_callback_l2,
551 flowprobe_template_rewrite_l2_ip6,
552 is_add, &template_id);
553 fm->template_reports[flags | FLOW_RECORD_L2_IP6] =
554 (is_add) ? template_id : 0;
556 /* Special case L2 */
557 fm->context[FLOW_VARIANT_L2_IP4].flags =
558 flags | FLOW_RECORD_L2_IP4;
559 fm->context[FLOW_VARIANT_L2_IP6].flags =
560 flags | FLOW_RECORD_L2_IP6;
562 fm->template_reports[flags] = template_id;
565 else if (which == FLOW_VARIANT_IP4)
566 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
567 flowprobe_data_callback_ip4,
568 flowprobe_template_rewrite_ip4,
569 is_add, &template_id);
570 else if (which == FLOW_VARIANT_IP6)
571 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
572 flowprobe_data_callback_ip6,
573 flowprobe_template_rewrite_ip6,
574 is_add, &template_id);
576 if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
578 clib_warning ("vnet_flow_report_add_del returned %d", rv);
582 if (which != (u8) ~ 0)
584 fm->context[which].flags = fm->record;
585 fm->template_reports[flags] = (is_add) ? template_id : 0;
588 if (which == FLOW_VARIANT_IP4)
589 vnet_feature_enable_disable ("ip4-output", "flowprobe-ip4",
590 sw_if_index, is_add, 0, 0);
591 else if (which == FLOW_VARIANT_IP6)
592 vnet_feature_enable_disable ("ip6-output", "flowprobe-ip6",
593 sw_if_index, is_add, 0, 0);
594 else if (which == FLOW_VARIANT_L2)
595 vnet_feature_enable_disable ("interface-output", "flowprobe-l2",
596 sw_if_index, is_add, 0, 0);
598 /* Stateful flow collection */
599 if (is_add && !fm->initialized)
601 flowprobe_create_state_tables (fm->active_timer);
602 if (fm->active_timer)
603 vlib_process_signal_event (vm, flowprobe_timer_node.index, 1, 0);
610 * @brief API message handler
611 * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
613 void vl_api_flowprobe_tx_interface_add_del_t_handler
614 (vl_api_flowprobe_tx_interface_add_del_t * mp)
616 flowprobe_main_t *fm = &flowprobe_main;
617 vl_api_flowprobe_tx_interface_add_del_reply_t *rmp;
618 u32 sw_if_index = ntohl (mp->sw_if_index);
621 VALIDATE_SW_IF_INDEX (mp);
625 clib_warning ("Please specify flowprobe params record first...");
626 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
630 rv = validate_feature_on_interface (fm, sw_if_index, mp->which);
631 if ((rv == 1 && mp->is_add == 1) || rv == 0)
633 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
637 rv = flowprobe_tx_interface_add_del_feature
638 (fm, sw_if_index, mp->which, mp->is_add);
641 BAD_SW_IF_INDEX_LABEL;
643 REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY);
646 #define vec_neg_search(v,E) \
649 while (_v(i) < vec_len(v) && v[_v(i)] == E) \
653 if (_v(i) == vec_len(v)) \
659 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
660 u8 record_l3, u8 record_l4,
661 u32 active_timer, u32 passive_timer)
663 flowprobe_record_t flags = 0;
665 if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
669 flags |= FLOW_RECORD_L2;
671 flags |= FLOW_RECORD_L3;
673 flags |= FLOW_RECORD_L4;
678 * Timers: ~0 is default, 0 is off
681 (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
683 (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
689 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
691 flowprobe_main_t *fm = &flowprobe_main;
692 vl_api_flowprobe_params_reply_t *rmp;
695 rv = flowprobe_params
697 mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
698 mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
699 mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
700 clib_net_to_host_u32 (mp->active_timer),
701 clib_net_to_host_u32 (mp->passive_timer));
703 REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
707 VLIB_PLUGIN_REGISTER () = {
708 .version = VPP_BUILD_VER,
709 .description = "Flow per Packet",
714 format_flowprobe_entry (u8 * s, va_list * args)
716 flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
717 s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
719 s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
720 format_ethernet_address, &e->key.dst_mac);
721 s = format (s, " %U -> %U",
722 format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
723 format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
724 s = format (s, " %d", e->key.protocol);
725 s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
726 clib_net_to_host_u16 (e->key.dst_port));
732 format_flowprobe_feature (u8 * s, va_list * args)
734 u8 *which = va_arg (*args, u8 *);
735 if (*which == FLOW_VARIANT_IP4)
736 s = format (s, "ip4");
737 else if (*which == FLOW_VARIANT_IP6)
738 s = format (s, "ip6");
739 else if (*which == FLOW_VARIANT_L2)
740 s = format (s, "l2");
746 format_flowprobe_params (u8 * s, va_list * args)
748 flowprobe_record_t flags = va_arg (*args, flowprobe_record_t);
749 u32 active_timer = va_arg (*args, u32);
750 u32 passive_timer = va_arg (*args, u32);
752 if (flags & FLOW_RECORD_L2)
753 s = format (s, " l2");
754 if (flags & FLOW_RECORD_L3)
755 s = format (s, " l3");
756 if (flags & FLOW_RECORD_L4)
757 s = format (s, " l4");
759 if (active_timer != (u32) ~ 0)
760 s = format (s, " active: %d", active_timer);
762 if (passive_timer != (u32) ~ 0)
763 s = format (s, " passive: %d", passive_timer);
768 static clib_error_t *
769 flowprobe_show_table_fn (vlib_main_t * vm,
770 unformat_input_t * input, vlib_cli_command_t * cm)
772 flowprobe_main_t *fm = &flowprobe_main;
774 flowprobe_entry_t *e;
776 vlib_cli_output (vm, "Dumping IPFIX table");
778 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
781 pool_foreach (e, fm->pool_per_worker[i])
783 vlib_cli_output (vm, "%U",
784 format_flowprobe_entry,
793 static clib_error_t *
794 flowprobe_show_stats_fn (vlib_main_t * vm,
795 unformat_input_t * input, vlib_cli_command_t * cm)
797 flowprobe_main_t *fm = &flowprobe_main;
800 vlib_cli_output (vm, "IPFIX table statistics");
801 vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
802 vlib_cli_output (vm, "Flow pool size per thread: %d\n",
803 0x1 << FLOWPROBE_LOG2_HASHSIZE);
805 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
806 vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
807 (100 * pool_elts (fm->pool_per_worker[i])) /
808 (0x1 << FLOWPROBE_LOG2_HASHSIZE));
812 static clib_error_t *
813 flowprobe_tx_interface_add_del_feature_command_fn (vlib_main_t * vm,
814 unformat_input_t * input,
815 vlib_cli_command_t * cmd)
817 flowprobe_main_t *fm = &flowprobe_main;
818 u32 sw_if_index = ~0;
820 u8 which = FLOW_VARIANT_IP4;
823 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
825 if (unformat (input, "disable"))
827 else if (unformat (input, "%U", unformat_vnet_sw_interface,
828 fm->vnet_main, &sw_if_index));
829 else if (unformat (input, "ip4"))
830 which = FLOW_VARIANT_IP4;
831 else if (unformat (input, "ip6"))
832 which = FLOW_VARIANT_IP6;
833 else if (unformat (input, "l2"))
834 which = FLOW_VARIANT_L2;
840 return clib_error_return (0,
841 "Please specify flowprobe params record first...");
843 if (sw_if_index == ~0)
844 return clib_error_return (0, "Please specify an interface...");
846 rv = validate_feature_on_interface (fm, sw_if_index, which);
850 return clib_error_return (0,
851 "Datapath is already enabled for given interface...");
854 return clib_error_return (0,
855 "Interface has enable different datapath ...");
858 flowprobe_tx_interface_add_del_feature (fm, sw_if_index, which, is_add);
864 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
865 return clib_error_return
866 (0, "Invalid interface, only works on physical ports");
869 case VNET_API_ERROR_UNIMPLEMENTED:
870 return clib_error_return (0, "ip6 not supported");
874 return clib_error_return (0, "flowprobe_enable_disable returned %d",
880 static clib_error_t *
881 flowprobe_show_feature_command_fn (vlib_main_t * vm,
882 unformat_input_t * input,
883 vlib_cli_command_t * cmd)
885 flowprobe_main_t *fm = &flowprobe_main;
889 vec_foreach (which, fm->flow_per_interface)
891 if (*which == (u8) ~ 0)
894 sw_if_index = which - fm->flow_per_interface;
895 vlib_cli_output (vm, " %U %U", format_vnet_sw_if_index_name,
896 vnet_get_main (), sw_if_index, format_flowprobe_feature,
902 static clib_error_t *
903 flowprobe_params_command_fn (vlib_main_t * vm,
904 unformat_input_t * input,
905 vlib_cli_command_t * cmd)
907 flowprobe_main_t *fm = &flowprobe_main;
908 bool record_l2 = false, record_l3 = false, record_l4 = false;
909 u32 active_timer = ~0;
910 u32 passive_timer = ~0;
912 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
914 if (unformat (input, "active %d", &active_timer))
916 else if (unformat (input, "passive %d", &passive_timer))
918 else if (unformat (input, "record"))
919 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
921 if (unformat (input, "l2"))
923 else if (unformat (input, "l3"))
925 else if (unformat (input, "l4"))
934 if (passive_timer > 0 && active_timer > passive_timer)
935 return clib_error_return (0,
936 "Passive timer has to be greater than active one...");
938 if (flowprobe_params (fm, record_l2, record_l3, record_l4,
939 active_timer, passive_timer))
940 return clib_error_return (0,
941 "Couldn't change flowperpacket params when feature is enabled on some interface ...");
945 static clib_error_t *
946 flowprobe_show_params_command_fn (vlib_main_t * vm,
947 unformat_input_t * input,
948 vlib_cli_command_t * cmd)
950 flowprobe_main_t *fm = &flowprobe_main;
951 flowprobe_record_t flags = fm->record;
952 u32 active_timer = fm->active_timer;
953 u32 passive_timer = fm->passive_timer;
955 vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
961 * '<em>flowprobe feature add-del</em>' commands to enable/disable
962 * per-packet IPFIX flow record generation on an interface
966 * To enable per-packet IPFIX flow-record generation on an interface:
967 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
969 * To disable per-packet IPFIX flow-record generation on an interface:
970 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
975 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
976 .path = "flowprobe feature add-del",
978 "flowprobe feature add-del <interface-name> <l2|ip4|ip6> disable",
979 .function = flowprobe_tx_interface_add_del_feature_command_fn,
981 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
982 .path = "flowprobe params",
984 "flowprobe params record <[l2] [l3] [l4]> [active <timer> passive <timer>]",
985 .function = flowprobe_params_command_fn,
988 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
989 .path = "show flowprobe feature",
991 "show flowprobe feature",
992 .function = flowprobe_show_feature_command_fn,
994 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
995 .path = "show flowprobe params",
997 "show flowprobe params",
998 .function = flowprobe_show_params_command_fn,
1000 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
1001 .path = "show flowprobe table",
1002 .short_help = "show flowprobe table",
1003 .function = flowprobe_show_table_fn,
1005 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
1006 .path = "show flowprobe statistics",
1007 .short_help = "show flowprobe statistics",
1008 .function = flowprobe_show_stats_fn,
1013 * Main-core process, sending an interrupt to the per worker input
1014 * process that spins the per worker timer wheel.
1017 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1019 uword *event_data = 0;
1020 vlib_main_t **worker_vms = 0, *worker_vm;
1021 flowprobe_main_t *fm = &flowprobe_main;
1023 /* Wait for Godot... */
1024 vlib_process_wait_for_event_or_clock (vm, 1e9);
1025 uword event_type = vlib_process_get_events (vm, &event_data);
1026 if (event_type != 1)
1027 clib_warning ("bogus kickoff event received, %d", event_type);
1028 vec_reset_length (event_data);
1031 if (vlib_get_n_threads () == 0)
1032 vec_add1 (worker_vms, vm);
1035 for (i = 0; i < vlib_get_n_threads (); i++)
1037 worker_vm = vlib_get_main_by_index (i);
1039 vec_add1 (worker_vms, worker_vm);
1042 f64 sleep_duration = 0.1;
1046 /* Send an interrupt to each timer input node */
1047 sleep_duration = 0.1;
1048 for (i = 0; i < vec_len (worker_vms); i++)
1050 worker_vm = worker_vms[i];
1053 vlib_node_set_interrupt_pending (worker_vm,
1054 flowprobe_walker_node.index);
1056 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1059 vlib_process_suspend (vm, sleep_duration);
1061 return 0; /* or not */
1065 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1066 .function = timer_process,
1067 .name = "flowprobe-timer-process",
1068 .type = VLIB_NODE_TYPE_PROCESS,
1072 #include <flowprobe/flowprobe.api.c>
1075 * @brief Set up the API message handling tables
1076 * @param vm vlib_main_t * vlib main data structure pointer
1077 * @returns 0 to indicate all is well, or a clib_error_t
1079 static clib_error_t *
1080 flowprobe_init (vlib_main_t * vm)
1082 flowprobe_main_t *fm = &flowprobe_main;
1083 vlib_thread_main_t *tm = &vlib_thread_main;
1084 clib_error_t *error = 0;
1088 fm->vnet_main = vnet_get_main ();
1090 /* Ask for a correctly-sized block of API message decode slots */
1091 fm->msg_id_base = setup_message_id_table ();
1093 /* Set up time reference pair */
1094 fm->vlib_time_0 = vlib_time_now (vm);
1095 fm->nanosecond_time_0 = unix_time_now_nsec ();
1097 clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1098 clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1099 clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1101 /* Decide how many worker threads we have */
1102 num_threads = 1 /* main thread */ + tm->n_threads;
1104 /* Allocate per worker thread vectors per flavour */
1105 for (i = 0; i < FLOW_N_VARIANTS; i++)
1107 vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1108 vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1109 vec_validate (fm->context[i].next_record_offset_per_worker,
1113 fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1114 fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1119 VLIB_INIT_FUNCTION (flowprobe_init);
1122 * fd.io coding-style-patch-verification: ON
1125 * eval: (c-set-style "gnu")