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"),
95 vlib_cli_output (handle, (char *) s); \
99 static inline ipfix_field_specifier_t *
100 flowprobe_template_ip4_fields (ipfix_field_specifier_t * f)
102 #define flowprobe_template_ip4_field_count() 4
103 /* sourceIpv4Address, TLV type 8, u32 */
104 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
105 sourceIPv4Address, 4);
107 /* destinationIPv4Address, TLV type 12, u32 */
108 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
109 destinationIPv4Address, 4);
111 /* protocolIdentifier, TLV type 4, u8 */
112 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
113 protocolIdentifier, 1);
115 /* octetDeltaCount, TLV type 1, u64 */
116 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
122 static inline ipfix_field_specifier_t *
123 flowprobe_template_ip6_fields (ipfix_field_specifier_t * f)
125 #define flowprobe_template_ip6_field_count() 4
126 /* sourceIpv6Address, TLV type 27, 16 octets */
127 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
128 sourceIPv6Address, 16);
130 /* destinationIPv6Address, TLV type 28, 16 octets */
131 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
132 destinationIPv6Address, 16);
134 /* protocolIdentifier, TLV type 4, u8 */
135 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
136 protocolIdentifier, 1);
138 /* octetDeltaCount, TLV type 1, u64 */
139 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
145 static inline ipfix_field_specifier_t *
146 flowprobe_template_l2_fields (ipfix_field_specifier_t * f)
148 #define flowprobe_template_l2_field_count() 3
149 /* sourceMacAddress, TLV type 56, u8[6] we hope */
150 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
151 sourceMacAddress, 6);
153 /* destinationMacAddress, TLV type 80, u8[6] we hope */
154 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
155 destinationMacAddress, 6);
157 /* ethernetType, TLV type 256, u16 */
158 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
164 static inline ipfix_field_specifier_t *
165 flowprobe_template_common_fields (ipfix_field_specifier_t * f)
167 #define flowprobe_template_common_field_count() 6
168 /* ingressInterface, TLV type 10, u32 */
169 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
170 ingressInterface, 4);
173 /* egressInterface, TLV type 14, u32 */
174 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
178 /* flowDirection, TLV type 61, u8 */
179 f->e_id_length = ipfix_e_id_length (0 /* enterprise */, flowDirection, 1);
182 /* packetDeltaCount, TLV type 2, u64 */
183 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
184 packetDeltaCount, 8);
187 /* flowStartNanoseconds, TLV type 156, u64 */
188 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
189 flowStartNanoseconds, 8);
192 /* flowEndNanoseconds, TLV type 157, u64 */
193 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
194 flowEndNanoseconds, 8);
200 static inline ipfix_field_specifier_t *
201 flowprobe_template_l4_fields (ipfix_field_specifier_t * f)
203 #define flowprobe_template_l4_field_count() 3
204 /* sourceTransportPort, TLV type 7, u16 */
205 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
206 sourceTransportPort, 2);
208 /* destinationTransportPort, TLV type 11, u16 */
209 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
210 destinationTransportPort, 2);
212 /* tcpControlBits, TLV type 6, u16 */
213 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
221 * @brief Create an IPFIX template packet rewrite string
222 * @param frm flow_report_main_t *
223 * @param fr flow_report_t *
224 * @param collector_address ip4_address_t * the IPFIX collector address
225 * @param src_address ip4_address_t * the source address we should use
226 * @param collector_port u16 the collector port we should use, host byte order
227 * @returns u8 * vector containing the indicated IPFIX template packet
230 flowprobe_template_rewrite_inline (ipfix_exporter_t *exp, flow_report_t *fr,
232 flowprobe_variant_t which)
236 ipfix_message_header_t *h;
237 ipfix_set_header_t *s;
238 ipfix_template_header_t *t;
239 ipfix_field_specifier_t *f;
240 ipfix_field_specifier_t *first_field;
242 ip4_ipfix_template_packet_t *tp;
244 flow_report_stream_t *stream;
245 flowprobe_main_t *fm = &flowprobe_main;
246 flowprobe_record_t flags = fr->opaque.as_uword;
247 bool collect_ip4 = false, collect_ip6 = false;
249 stream = &exp->streams[fr->stream_index];
251 if (flags & FLOW_RECORD_L3)
253 collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
254 collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
255 if (which == FLOW_VARIANT_L2_IP4)
256 flags |= FLOW_RECORD_L2_IP4;
257 if (which == FLOW_VARIANT_L2_IP6)
258 flags |= FLOW_RECORD_L2_IP6;
261 field_count += flowprobe_template_common_field_count ();
262 if (flags & FLOW_RECORD_L2)
263 field_count += flowprobe_template_l2_field_count ();
265 field_count += flowprobe_template_ip4_field_count ();
267 field_count += flowprobe_template_ip6_field_count ();
268 if (flags & FLOW_RECORD_L4)
269 field_count += flowprobe_template_l4_field_count ();
271 /* allocate rewrite space */
273 (rewrite, sizeof (ip4_ipfix_template_packet_t)
274 + field_count * sizeof (ipfix_field_specifier_t) - 1,
275 CLIB_CACHE_LINE_BYTES);
277 tp = (ip4_ipfix_template_packet_t *) rewrite;
278 ip = (ip4_header_t *) & tp->ip4;
279 udp = (udp_header_t *) (ip + 1);
280 h = (ipfix_message_header_t *) (udp + 1);
281 s = (ipfix_set_header_t *) (h + 1);
282 t = (ipfix_template_header_t *) (s + 1);
283 first_field = f = (ipfix_field_specifier_t *) (t + 1);
285 ip->ip_version_and_header_length = 0x45;
287 ip->protocol = IP_PROTOCOL_UDP;
288 ip->src_address.as_u32 = exp->src_address.ip.ip4.as_u32;
289 ip->dst_address.as_u32 = exp->ipfix_collector.ip.ip4.as_u32;
290 udp->src_port = clib_host_to_net_u16 (stream->src_port);
291 udp->dst_port = clib_host_to_net_u16 (collector_port);
292 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
294 /* FIXUP: message header export_time */
295 /* FIXUP: message header sequence_number */
296 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
298 /* Add TLVs to the template */
299 f = flowprobe_template_common_fields (f);
301 if (flags & FLOW_RECORD_L2)
302 f = flowprobe_template_l2_fields (f);
304 f = flowprobe_template_ip4_fields (f);
306 f = flowprobe_template_ip6_fields (f);
307 if (flags & FLOW_RECORD_L4)
308 f = flowprobe_template_l4_fields (f);
310 /* Back to the template packet... */
311 ip = (ip4_header_t *) & tp->ip4;
312 udp = (udp_header_t *) (ip + 1);
314 ASSERT (f - first_field);
315 /* Field count in this template */
316 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
318 fm->template_size[flags] = (u8 *) f - (u8 *) s;
320 /* set length in octets */
322 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
324 /* message length in octets */
325 h->version_length = version_length ((u8 *) f - (u8 *) h);
327 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
328 ip->checksum = ip4_header_checksum (ip);
334 flowprobe_template_rewrite_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
336 ipfix_report_element_t *elts, u32 n_elts,
339 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
344 flowprobe_template_rewrite_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
346 ipfix_report_element_t *elts, u32 n_elts,
349 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
354 flowprobe_template_rewrite_l2 (ipfix_exporter_t *exp, flow_report_t *fr,
356 ipfix_report_element_t *elts, u32 n_elts,
359 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
364 flowprobe_template_rewrite_l2_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
366 ipfix_report_element_t *elts, u32 n_elts,
369 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
370 FLOW_VARIANT_L2_IP4);
374 flowprobe_template_rewrite_l2_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
376 ipfix_report_element_t *elts, u32 n_elts,
379 return flowprobe_template_rewrite_inline (exp, fr, collector_port,
380 FLOW_VARIANT_L2_IP6);
384 * @brief Flush accumulated data
385 * @param frm flow_report_main_t *
386 * @param fr flow_report_t *
387 * @param f vlib_frame_t *
390 * This function must simply return the incoming frame, or no template packets
394 flowprobe_data_callback_ip4 (flow_report_main_t *frm, ipfix_exporter_t *exp,
395 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
398 flowprobe_flush_callback_ip4 ();
403 flowprobe_data_callback_ip6 (flow_report_main_t *frm, ipfix_exporter_t *exp,
404 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
407 flowprobe_flush_callback_ip6 ();
412 flowprobe_data_callback_l2 (flow_report_main_t *frm, ipfix_exporter_t *exp,
413 flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
416 flowprobe_flush_callback_l2 ();
421 flowprobe_template_add_del (u32 domain_id, u16 src_port,
422 flowprobe_record_t flags,
423 vnet_flow_data_callback_t * flow_data_callback,
424 vnet_flow_rewrite_callback_t * rewrite_callback,
425 bool is_add, u16 * template_id)
427 ipfix_exporter_t *exp = &flow_report_main.exporters[0];
428 vnet_flow_report_add_del_args_t a = {
429 .rewrite_callback = rewrite_callback,
430 .flow_data_callback = flow_data_callback,
432 .domain_id = domain_id,
433 .src_port = src_port,
434 .opaque.as_uword = flags,
436 return vnet_flow_report_add_del (exp, &a, template_id);
440 flowprobe_expired_timer_callback (u32 * expired_timers)
442 vlib_main_t *vm = vlib_get_main ();
443 flowprobe_main_t *fm = &flowprobe_main;
444 u32 my_cpu_number = vm->thread_index;
448 for (i = 0; i < vec_len (expired_timers); i++)
450 poolindex = expired_timers[i] & 0x7FFFFFFF;
451 vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex);
455 static clib_error_t *
456 flowprobe_create_state_tables (u32 active_timer)
458 flowprobe_main_t *fm = &flowprobe_main;
459 vlib_thread_main_t *tm = &vlib_thread_main;
460 vlib_main_t *vm = vlib_get_main ();
461 clib_error_t *error = 0;
465 /* Decide how many worker threads we have */
466 num_threads = 1 /* main thread */ + tm->n_threads;
468 /* Hash table per worker */
469 fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE;
471 /* Init per worker flow state and timer wheels */
474 vec_validate (fm->timers_per_worker, num_threads - 1);
475 vec_validate (fm->expired_passive_per_worker, num_threads - 1);
476 vec_validate (fm->hash_per_worker, num_threads - 1);
477 vec_validate (fm->pool_per_worker, num_threads - 1);
479 for (i = 0; i < num_threads; i++)
482 pool_alloc (fm->pool_per_worker[i], 1 << fm->ht_log2len);
483 vec_resize (fm->hash_per_worker[i], 1 << fm->ht_log2len);
484 for (j = 0; j < (1 << fm->ht_log2len); j++)
485 fm->hash_per_worker[i][j] = ~0;
486 fm->timers_per_worker[i] =
487 clib_mem_alloc (sizeof (TWT (tw_timer_wheel)));
488 tw_timer_wheel_init_2t_1w_2048sl (fm->timers_per_worker[i],
489 flowprobe_expired_timer_callback,
496 f64 now = vlib_time_now (vm);
497 vec_validate (fm->stateless_entry, num_threads - 1);
498 for (i = 0; i < num_threads; i++)
499 fm->stateless_entry[i].last_exported = now;
500 fm->disabled = false;
502 fm->initialized = true;
507 validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index,
510 vec_validate_init_empty (fm->flow_per_interface, sw_if_index, ~0);
511 vec_validate_init_empty (fm->direction_per_interface, sw_if_index, ~0);
513 if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0)
515 else if (fm->flow_per_interface[sw_if_index] != which)
522 * @brief configure / deconfigure the IPFIX flow-per-packet
523 * @param fm flowprobe_main_t * fm
524 * @param sw_if_index u32 the desired interface
525 * @param which u8 the desired datapath
526 * @param direction u8 the desired direction
527 * @param is_add int 1 to enable the feature, 0 to disable it
528 * @returns 0 if successful, non-zero otherwise
532 flowprobe_interface_add_del_feature (flowprobe_main_t *fm, u32 sw_if_index,
533 u8 which, u8 direction, int is_add)
535 vlib_main_t *vm = vlib_get_main ();
538 flowprobe_record_t flags = fm->record;
540 fm->flow_per_interface[sw_if_index] = (is_add) ? which : (u8) ~ 0;
541 fm->direction_per_interface[sw_if_index] = (is_add) ? direction : (u8) ~0;
542 fm->template_per_flow[which] += (is_add) ? 1 : -1;
543 if (is_add && fm->template_per_flow[which] > 1)
544 template_id = fm->template_reports[flags];
546 if ((is_add && fm->template_per_flow[which] == 1) ||
547 (!is_add && fm->template_per_flow[which] == 0))
549 if (which == FLOW_VARIANT_L2)
551 if (fm->record & FLOW_RECORD_L2)
553 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
554 flowprobe_data_callback_l2,
555 flowprobe_template_rewrite_l2,
556 is_add, &template_id);
558 if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
560 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
561 flowprobe_data_callback_l2,
562 flowprobe_template_rewrite_l2_ip4,
563 is_add, &template_id);
564 fm->template_reports[flags | FLOW_RECORD_L2_IP4] =
565 (is_add) ? template_id : 0;
567 flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
568 flowprobe_data_callback_l2,
569 flowprobe_template_rewrite_l2_ip6,
570 is_add, &template_id);
571 fm->template_reports[flags | FLOW_RECORD_L2_IP6] =
572 (is_add) ? template_id : 0;
574 /* Special case L2 */
575 fm->context[FLOW_VARIANT_L2_IP4].flags =
576 flags | FLOW_RECORD_L2_IP4;
577 fm->context[FLOW_VARIANT_L2_IP6].flags =
578 flags | FLOW_RECORD_L2_IP6;
580 fm->template_reports[flags] = template_id;
583 else if (which == FLOW_VARIANT_IP4)
584 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
585 flowprobe_data_callback_ip4,
586 flowprobe_template_rewrite_ip4,
587 is_add, &template_id);
588 else if (which == FLOW_VARIANT_IP6)
589 rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
590 flowprobe_data_callback_ip6,
591 flowprobe_template_rewrite_ip6,
592 is_add, &template_id);
594 if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
596 clib_warning ("vnet_flow_report_add_del returned %d", rv);
600 if (which != (u8) ~ 0)
602 fm->context[which].flags = fm->record;
603 fm->template_reports[flags] = (is_add) ? template_id : 0;
606 if (direction == FLOW_DIRECTION_RX || direction == FLOW_DIRECTION_BOTH)
608 if (which == FLOW_VARIANT_IP4)
610 vnet_feature_enable_disable ("ip4-unicast", "flowprobe-input-ip4",
611 sw_if_index, is_add, 0, 0);
612 vnet_feature_enable_disable ("ip4-multicast", "flowprobe-input-ip4",
613 sw_if_index, is_add, 0, 0);
615 else if (which == FLOW_VARIANT_IP6)
617 vnet_feature_enable_disable ("ip6-unicast", "flowprobe-input-ip6",
618 sw_if_index, is_add, 0, 0);
619 vnet_feature_enable_disable ("ip6-multicast", "flowprobe-input-ip6",
620 sw_if_index, is_add, 0, 0);
622 else if (which == FLOW_VARIANT_L2)
623 vnet_feature_enable_disable ("device-input", "flowprobe-input-l2",
624 sw_if_index, is_add, 0, 0);
627 if (direction == FLOW_DIRECTION_TX || direction == FLOW_DIRECTION_BOTH)
629 if (which == FLOW_VARIANT_IP4)
630 vnet_feature_enable_disable ("ip4-output", "flowprobe-output-ip4",
631 sw_if_index, is_add, 0, 0);
632 else if (which == FLOW_VARIANT_IP6)
633 vnet_feature_enable_disable ("ip6-output", "flowprobe-output-ip6",
634 sw_if_index, is_add, 0, 0);
635 else if (which == FLOW_VARIANT_L2)
636 vnet_feature_enable_disable ("interface-output", "flowprobe-output-l2",
637 sw_if_index, is_add, 0, 0);
640 /* Stateful flow collection */
641 if (is_add && !fm->initialized)
643 flowprobe_create_state_tables (fm->active_timer);
644 if (fm->active_timer)
645 vlib_process_signal_event (vm, flowprobe_timer_node.index, 1, 0);
652 * @brief API message handler
653 * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
655 void vl_api_flowprobe_tx_interface_add_del_t_handler
656 (vl_api_flowprobe_tx_interface_add_del_t * mp)
658 flowprobe_main_t *fm = &flowprobe_main;
659 vl_api_flowprobe_tx_interface_add_del_reply_t *rmp;
660 u32 sw_if_index = ntohl (mp->sw_if_index);
663 VALIDATE_SW_IF_INDEX (mp);
667 clib_warning ("Please specify flowprobe params record first...");
668 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
672 rv = validate_feature_on_interface (fm, sw_if_index, mp->which);
673 if ((rv == 1 && mp->is_add == 1) || rv == 0)
675 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
679 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, mp->which,
680 FLOW_DIRECTION_TX, mp->is_add);
683 BAD_SW_IF_INDEX_LABEL;
685 REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY);
689 vl_api_flowprobe_interface_add_del_t_handler (
690 vl_api_flowprobe_interface_add_del_t *mp)
692 flowprobe_main_t *fm = &flowprobe_main;
693 vl_api_flowprobe_interface_add_del_reply_t *rmp;
700 VALIDATE_SW_IF_INDEX (mp);
702 sw_if_index = ntohl (mp->sw_if_index);
705 if (mp->which == FLOWPROBE_WHICH_IP4)
706 which = FLOW_VARIANT_IP4;
707 else if (mp->which == FLOWPROBE_WHICH_IP6)
708 which = FLOW_VARIANT_IP6;
709 else if (mp->which == FLOWPROBE_WHICH_L2)
710 which = FLOW_VARIANT_L2;
713 clib_warning ("Invalid value of which");
714 rv = VNET_API_ERROR_INVALID_VALUE;
718 if (mp->direction == FLOWPROBE_DIRECTION_RX)
719 direction = FLOW_DIRECTION_RX;
720 else if (mp->direction == FLOWPROBE_DIRECTION_TX)
721 direction = FLOW_DIRECTION_TX;
722 else if (mp->direction == FLOWPROBE_DIRECTION_BOTH)
723 direction = FLOW_DIRECTION_BOTH;
726 clib_warning ("Invalid value of direction");
727 rv = VNET_API_ERROR_INVALID_VALUE;
733 clib_warning ("Please specify flowprobe params record first");
734 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
738 rv = validate_feature_on_interface (fm, sw_if_index, which);
743 clib_warning ("Variant is already enabled for given interface");
744 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
750 clib_warning ("Interface has different variant enabled");
751 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
758 clib_warning ("Interface has no variant enabled");
759 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
764 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
768 BAD_SW_IF_INDEX_LABEL;
770 REPLY_MACRO (VL_API_FLOWPROBE_INTERFACE_ADD_DEL_REPLY);
774 send_flowprobe_interface_details (u32 sw_if_index, u8 which, u8 direction,
775 vl_api_registration_t *reg, u32 context)
777 flowprobe_main_t *fm = &flowprobe_main;
778 vl_api_flowprobe_interface_details_t *rmp = 0;
780 rmp = vl_msg_api_alloc (sizeof (*rmp));
783 clib_memset (rmp, 0, sizeof (*rmp));
785 ntohs (VL_API_FLOWPROBE_INTERFACE_DETAILS + REPLY_MSG_ID_BASE);
786 rmp->context = context;
788 rmp->sw_if_index = htonl (sw_if_index);
790 if (which == FLOW_VARIANT_IP4)
791 rmp->which = FLOWPROBE_WHICH_IP4;
792 else if (which == FLOW_VARIANT_IP6)
793 rmp->which = FLOWPROBE_WHICH_IP6;
794 else if (which == FLOW_VARIANT_L2)
795 rmp->which = FLOWPROBE_WHICH_L2;
799 if (direction == FLOW_DIRECTION_RX)
800 rmp->direction = FLOWPROBE_DIRECTION_RX;
801 else if (direction == FLOW_DIRECTION_TX)
802 rmp->direction = FLOWPROBE_DIRECTION_TX;
803 else if (direction == FLOW_DIRECTION_BOTH)
804 rmp->direction = FLOWPROBE_DIRECTION_BOTH;
808 vl_api_send_msg (reg, (u8 *) rmp);
812 vl_api_flowprobe_interface_dump_t_handler (
813 vl_api_flowprobe_interface_dump_t *mp)
815 flowprobe_main_t *fm = &flowprobe_main;
816 vl_api_registration_t *reg;
819 reg = vl_api_client_index_to_registration (mp->client_index);
823 sw_if_index = ntohl (mp->sw_if_index);
825 if (sw_if_index == ~0)
829 vec_foreach (which, fm->flow_per_interface)
831 if (*which == (u8) ~0)
834 sw_if_index = which - fm->flow_per_interface;
835 send_flowprobe_interface_details (
836 sw_if_index, *which, fm->direction_per_interface[sw_if_index], reg,
840 else if (vec_len (fm->flow_per_interface) > sw_if_index &&
841 fm->flow_per_interface[sw_if_index] != (u8) ~0)
843 send_flowprobe_interface_details (
844 sw_if_index, fm->flow_per_interface[sw_if_index],
845 fm->direction_per_interface[sw_if_index], reg, mp->context);
849 #define vec_neg_search(v,E) \
852 while (_v(i) < vec_len(v) && v[_v(i)] == E) \
856 if (_v(i) == vec_len(v)) \
862 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
863 u8 record_l3, u8 record_l4,
864 u32 active_timer, u32 passive_timer)
866 flowprobe_record_t flags = 0;
868 if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
869 return VNET_API_ERROR_UNSUPPORTED;
872 flags |= FLOW_RECORD_L2;
874 flags |= FLOW_RECORD_L3;
876 flags |= FLOW_RECORD_L4;
881 * Timers: ~0 is default, 0 is off
884 (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
886 (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
892 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
894 flowprobe_main_t *fm = &flowprobe_main;
895 vl_api_flowprobe_params_reply_t *rmp;
898 rv = flowprobe_params
900 mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
901 mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
902 mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
903 clib_net_to_host_u32 (mp->active_timer),
904 clib_net_to_host_u32 (mp->passive_timer));
906 REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
910 vl_api_flowprobe_set_params_t_handler (vl_api_flowprobe_set_params_t *mp)
912 flowprobe_main_t *fm = &flowprobe_main;
913 vl_api_flowprobe_set_params_reply_t *rmp;
914 bool record_l2, record_l3, record_l4;
919 record_l2 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L2);
920 record_l3 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L3);
921 record_l4 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L4);
923 active_timer = clib_net_to_host_u32 (mp->active_timer);
924 passive_timer = clib_net_to_host_u32 (mp->passive_timer);
926 if (passive_timer > 0 && active_timer > passive_timer)
928 clib_warning ("Passive timer must be greater than active timer");
929 rv = VNET_API_ERROR_INVALID_VALUE;
933 rv = flowprobe_params (fm, record_l2, record_l3, record_l4, active_timer,
935 if (rv == VNET_API_ERROR_UNSUPPORTED)
937 "Cannot change params when feature is enabled on some interfaces");
940 REPLY_MACRO (VL_API_FLOWPROBE_SET_PARAMS_REPLY);
944 vl_api_flowprobe_get_params_t_handler (vl_api_flowprobe_get_params_t *mp)
946 flowprobe_main_t *fm = &flowprobe_main;
947 vl_api_flowprobe_get_params_reply_t *rmp;
951 if (fm->record & FLOW_RECORD_L2)
952 record_flags |= FLOWPROBE_RECORD_FLAG_L2;
953 if (fm->record & FLOW_RECORD_L3)
954 record_flags |= FLOWPROBE_RECORD_FLAG_L3;
955 if (fm->record & FLOW_RECORD_L4)
956 record_flags |= FLOWPROBE_RECORD_FLAG_L4;
959 REPLY_MACRO2 (VL_API_FLOWPROBE_GET_PARAMS_REPLY,
961 rmp->record_flags = record_flags;
962 rmp->active_timer = htonl (fm->active_timer);
963 rmp->passive_timer = htonl (fm->passive_timer);
969 VLIB_PLUGIN_REGISTER () = {
970 .version = VPP_BUILD_VER,
971 .description = "Flow per Packet",
976 format_flowprobe_direction (u8 *s, va_list *args)
978 u8 *direction = va_arg (*args, u8 *);
979 if (*direction == FLOW_DIRECTION_RX)
980 s = format (s, "rx");
981 else if (*direction == FLOW_DIRECTION_TX)
982 s = format (s, "tx");
983 else if (*direction == FLOW_DIRECTION_BOTH)
984 s = format (s, "rx tx");
990 format_flowprobe_entry (u8 * s, va_list * args)
992 flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
993 s = format (s, " %U", format_flowprobe_direction, &e->key.direction);
994 s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
996 s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
997 format_ethernet_address, &e->key.dst_mac);
998 s = format (s, " %U -> %U",
999 format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
1000 format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
1001 s = format (s, " %d", e->key.protocol);
1002 s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
1003 clib_net_to_host_u16 (e->key.dst_port));
1009 format_flowprobe_feature (u8 * s, va_list * args)
1011 u8 *which = va_arg (*args, u8 *);
1012 if (*which == FLOW_VARIANT_IP4)
1013 s = format (s, "ip4");
1014 else if (*which == FLOW_VARIANT_IP6)
1015 s = format (s, "ip6");
1016 else if (*which == FLOW_VARIANT_L2)
1017 s = format (s, "l2");
1023 format_flowprobe_params (u8 * s, va_list * args)
1025 flowprobe_record_t flags = va_arg (*args, flowprobe_record_t);
1026 u32 active_timer = va_arg (*args, u32);
1027 u32 passive_timer = va_arg (*args, u32);
1029 if (flags & FLOW_RECORD_L2)
1030 s = format (s, " l2");
1031 if (flags & FLOW_RECORD_L3)
1032 s = format (s, " l3");
1033 if (flags & FLOW_RECORD_L4)
1034 s = format (s, " l4");
1036 if (active_timer != (u32) ~ 0)
1037 s = format (s, " active: %d", active_timer);
1039 if (passive_timer != (u32) ~ 0)
1040 s = format (s, " passive: %d", passive_timer);
1045 static clib_error_t *
1046 flowprobe_show_table_fn (vlib_main_t * vm,
1047 unformat_input_t * input, vlib_cli_command_t * cm)
1049 flowprobe_main_t *fm = &flowprobe_main;
1051 flowprobe_entry_t *e;
1053 vlib_cli_output (vm, "Dumping IPFIX table");
1055 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
1058 pool_foreach (e, fm->pool_per_worker[i])
1060 vlib_cli_output (vm, "%U",
1061 format_flowprobe_entry,
1070 static clib_error_t *
1071 flowprobe_show_stats_fn (vlib_main_t * vm,
1072 unformat_input_t * input, vlib_cli_command_t * cm)
1074 flowprobe_main_t *fm = &flowprobe_main;
1077 vlib_cli_output (vm, "IPFIX table statistics");
1078 vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
1079 vlib_cli_output (vm, "Flow pool size per thread: %d\n",
1080 0x1 << FLOWPROBE_LOG2_HASHSIZE);
1082 for (i = 0; i < vec_len (fm->pool_per_worker); i++)
1083 vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
1084 (100 * pool_elts (fm->pool_per_worker[i])) /
1085 (0x1 << FLOWPROBE_LOG2_HASHSIZE));
1089 static clib_error_t *
1090 flowprobe_interface_add_del_feature_command_fn (vlib_main_t *vm,
1091 unformat_input_t *input,
1092 vlib_cli_command_t *cmd)
1094 flowprobe_main_t *fm = &flowprobe_main;
1095 u32 sw_if_index = ~0;
1097 u8 which = FLOW_VARIANT_IP4;
1098 flowprobe_direction_t direction = FLOW_DIRECTION_TX;
1101 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1103 if (unformat (input, "disable"))
1105 else if (unformat (input, "%U", unformat_vnet_sw_interface,
1106 fm->vnet_main, &sw_if_index));
1107 else if (unformat (input, "ip4"))
1108 which = FLOW_VARIANT_IP4;
1109 else if (unformat (input, "ip6"))
1110 which = FLOW_VARIANT_IP6;
1111 else if (unformat (input, "l2"))
1112 which = FLOW_VARIANT_L2;
1113 else if (unformat (input, "rx"))
1114 direction = FLOW_DIRECTION_RX;
1115 else if (unformat (input, "tx"))
1116 direction = FLOW_DIRECTION_TX;
1117 else if (unformat (input, "both"))
1118 direction = FLOW_DIRECTION_BOTH;
1123 if (fm->record == 0)
1124 return clib_error_return (0,
1125 "Please specify flowprobe params record first...");
1127 if (sw_if_index == ~0)
1128 return clib_error_return (0, "Please specify an interface...");
1130 rv = validate_feature_on_interface (fm, sw_if_index, which);
1134 return clib_error_return (0,
1135 "Datapath is already enabled for given interface...");
1138 return clib_error_return (0,
1139 "Interface has enable different datapath ...");
1144 return clib_error_return (0, "Interface has no datapath enabled");
1148 rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
1155 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
1156 return clib_error_return
1157 (0, "Invalid interface, only works on physical ports");
1160 case VNET_API_ERROR_UNIMPLEMENTED:
1161 return clib_error_return (0, "ip6 not supported");
1165 return clib_error_return (0, "flowprobe_enable_disable returned %d",
1171 static clib_error_t *
1172 flowprobe_show_feature_command_fn (vlib_main_t * vm,
1173 unformat_input_t * input,
1174 vlib_cli_command_t * cmd)
1176 flowprobe_main_t *fm = &flowprobe_main;
1180 vec_foreach (which, fm->flow_per_interface)
1182 if (*which == (u8) ~ 0)
1185 sw_if_index = which - fm->flow_per_interface;
1186 vlib_cli_output (vm, " %U %U %U", format_vnet_sw_if_index_name,
1187 vnet_get_main (), sw_if_index, format_flowprobe_feature,
1188 which, format_flowprobe_direction,
1189 &fm->direction_per_interface[sw_if_index]);
1194 static clib_error_t *
1195 flowprobe_params_command_fn (vlib_main_t * vm,
1196 unformat_input_t * input,
1197 vlib_cli_command_t * cmd)
1199 flowprobe_main_t *fm = &flowprobe_main;
1200 bool record_l2 = false, record_l3 = false, record_l4 = false;
1201 u32 active_timer = ~0;
1202 u32 passive_timer = ~0;
1204 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1206 if (unformat (input, "active %d", &active_timer))
1208 else if (unformat (input, "passive %d", &passive_timer))
1210 else if (unformat (input, "record"))
1211 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1213 if (unformat (input, "l2"))
1215 else if (unformat (input, "l3"))
1217 else if (unformat (input, "l4"))
1226 if (passive_timer > 0 && active_timer > passive_timer)
1227 return clib_error_return (0,
1228 "Passive timer has to be greater than active one...");
1230 if (flowprobe_params (fm, record_l2, record_l3, record_l4,
1231 active_timer, passive_timer))
1232 return clib_error_return (0,
1233 "Couldn't change flowperpacket params when feature is enabled on some interface ...");
1237 static clib_error_t *
1238 flowprobe_show_params_command_fn (vlib_main_t * vm,
1239 unformat_input_t * input,
1240 vlib_cli_command_t * cmd)
1242 flowprobe_main_t *fm = &flowprobe_main;
1243 flowprobe_record_t flags = fm->record;
1244 u32 active_timer = fm->active_timer;
1245 u32 passive_timer = fm->passive_timer;
1247 vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
1253 * '<em>flowprobe feature add-del</em>' commands to enable/disable
1254 * per-packet IPFIX flow record generation on an interface
1258 * To enable per-packet IPFIX flow-record generation on an interface:
1259 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
1261 * To disable per-packet IPFIX flow-record generation on an interface:
1262 * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
1267 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
1268 .path = "flowprobe feature add-del",
1269 .short_help = "flowprobe feature add-del <interface-name> [(l2|ip4|ip6)] "
1270 "[(rx|tx|both)] [disable]",
1271 .function = flowprobe_interface_add_del_feature_command_fn,
1273 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
1274 .path = "flowprobe params",
1275 .short_help = "flowprobe params record [l2] [l3] [l4] [active <timer>] "
1276 "[passive <timer>]",
1277 .function = flowprobe_params_command_fn,
1280 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
1281 .path = "show flowprobe feature",
1283 "show flowprobe feature",
1284 .function = flowprobe_show_feature_command_fn,
1286 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
1287 .path = "show flowprobe params",
1289 "show flowprobe params",
1290 .function = flowprobe_show_params_command_fn,
1292 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
1293 .path = "show flowprobe table",
1294 .short_help = "show flowprobe table",
1295 .function = flowprobe_show_table_fn,
1297 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
1298 .path = "show flowprobe statistics",
1299 .short_help = "show flowprobe statistics",
1300 .function = flowprobe_show_stats_fn,
1305 * Main-core process, sending an interrupt to the per worker input
1306 * process that spins the per worker timer wheel.
1309 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1311 uword *event_data = 0;
1312 vlib_main_t **worker_vms = 0, *worker_vm;
1313 flowprobe_main_t *fm = &flowprobe_main;
1315 /* Wait for Godot... */
1316 vlib_process_wait_for_event_or_clock (vm, 1e9);
1317 uword event_type = vlib_process_get_events (vm, &event_data);
1318 if (event_type != 1)
1319 clib_warning ("bogus kickoff event received, %d", event_type);
1320 vec_reset_length (event_data);
1323 if (vlib_get_n_threads () == 0)
1324 vec_add1 (worker_vms, vm);
1327 for (i = 0; i < vlib_get_n_threads (); i++)
1329 worker_vm = vlib_get_main_by_index (i);
1331 vec_add1 (worker_vms, worker_vm);
1334 f64 sleep_duration = 0.1;
1338 /* Send an interrupt to each timer input node */
1339 sleep_duration = 0.1;
1340 for (i = 0; i < vec_len (worker_vms); i++)
1342 worker_vm = worker_vms[i];
1345 vlib_node_set_interrupt_pending (worker_vm,
1346 flowprobe_walker_node.index);
1348 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1351 vlib_process_suspend (vm, sleep_duration);
1353 return 0; /* or not */
1357 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1358 .function = timer_process,
1359 .name = "flowprobe-timer-process",
1360 .type = VLIB_NODE_TYPE_PROCESS,
1364 #include <flowprobe/flowprobe.api.c>
1367 * @brief Set up the API message handling tables
1368 * @param vm vlib_main_t * vlib main data structure pointer
1369 * @returns 0 to indicate all is well, or a clib_error_t
1371 static clib_error_t *
1372 flowprobe_init (vlib_main_t * vm)
1374 flowprobe_main_t *fm = &flowprobe_main;
1375 vlib_thread_main_t *tm = &vlib_thread_main;
1376 clib_error_t *error = 0;
1380 fm->vnet_main = vnet_get_main ();
1382 /* Ask for a correctly-sized block of API message decode slots */
1383 fm->msg_id_base = setup_message_id_table ();
1385 /* Set up time reference pair */
1386 fm->vlib_time_0 = vlib_time_now (vm);
1387 fm->nanosecond_time_0 = unix_time_now_nsec ();
1389 clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1390 clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1391 clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1393 /* Decide how many worker threads we have */
1394 num_threads = 1 /* main thread */ + tm->n_threads;
1396 /* Allocate per worker thread vectors per flavour */
1397 for (i = 0; i < FLOW_N_VARIANTS; i++)
1399 vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1400 vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1401 vec_validate (fm->context[i].next_record_offset_per_worker,
1405 fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1406 fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1411 VLIB_INIT_FUNCTION (flowprobe_init);
1414 * fd.io coding-style-patch-verification: ON
1417 * eval: (c-set-style "gnu")