hs-test: more debug output in http3 test
[vpp.git] / src / plugins / flowprobe / flowprobe.c
1 /*
2  * flowprobe.c - ipfix probe plugin
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  * @file
20  * @brief Per-packet IPFIX flow record generator plugin
21  *
22  * This file implements vpp plugin registration mechanics,
23  * debug CLI, and binary API handling.
24  */
25
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>
31
32 #include <vlibapi/api.h>
33 #include <vlibmemory/api.h>
34
35 /* define message IDs */
36 #include <flowprobe/flowprobe.api_enum.h>
37 #include <flowprobe/flowprobe.api_types.h>
38
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,
42                                 vlib_frame_t * f);
43
44 #define REPLY_MSG_ID_BASE fm->msg_id_base
45 #include <vlibapi/api_helper_macros.h>
46
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"),
52 };
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"),
57 };
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"),
62 };
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"),
67 };
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"),
72 };
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"),
77 };
78
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"),
83 };
84
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"),
89 };
90
91 #define FINISH                                                                \
92   vec_add1 (s, 0);                                                            \
93   vlib_cli_output (handle, (char *) s);                                       \
94   vec_free (s);                                                               \
95   return handle;
96
97 static inline ipfix_field_specifier_t *
98 flowprobe_template_ip4_fields (ipfix_field_specifier_t * f)
99 {
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);
104   f++;
105   /* destinationIPv4Address, TLV type 12, u32 */
106   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
107                                       destinationIPv4Address, 4);
108   f++;
109   /* protocolIdentifier, TLV type 4, u8 */
110   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
111                                       protocolIdentifier, 1);
112   f++;
113   /* octetDeltaCount, TLV type 1, u64 */
114   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
115                                       octetDeltaCount, 8);
116   f++;
117   return f;
118 }
119
120 static inline ipfix_field_specifier_t *
121 flowprobe_template_ip6_fields (ipfix_field_specifier_t * f)
122 {
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);
127   f++;
128   /* destinationIPv6Address, TLV type 28, 16 octets */
129   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
130                                       destinationIPv6Address, 16);
131   f++;
132   /* protocolIdentifier, TLV type 4, u8 */
133   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
134                                       protocolIdentifier, 1);
135   f++;
136   /* octetDeltaCount, TLV type 1, u64 */
137   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
138                                       octetDeltaCount, 8);
139   f++;
140   return f;
141 }
142
143 static inline ipfix_field_specifier_t *
144 flowprobe_template_l2_fields (ipfix_field_specifier_t * f)
145 {
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);
150   f++;
151   /* destinationMacAddress, TLV type 80, u8[6] we hope */
152   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
153                                       destinationMacAddress, 6);
154   f++;
155   /* ethernetType, TLV type 256, u16 */
156   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
157                                       ethernetType, 2);
158   f++;
159   return f;
160 }
161
162 static inline ipfix_field_specifier_t *
163 flowprobe_template_common_fields (ipfix_field_specifier_t * f)
164 {
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);
169   f++;
170
171   /* egressInterface, TLV type 14, u32 */
172   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
173                                       egressInterface, 4);
174   f++;
175
176   /* flowDirection, TLV type 61, u8 */
177   f->e_id_length = ipfix_e_id_length (0 /* enterprise */, flowDirection, 1);
178   f++;
179
180   /* packetDeltaCount, TLV type 2, u64 */
181   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
182                                       packetDeltaCount, 8);
183   f++;
184
185   /* flowStartNanoseconds, TLV type 156, u64 */
186   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
187                                       flowStartNanoseconds, 8);
188   f++;
189
190   /* flowEndNanoseconds, TLV type 157, u64 */
191   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
192                                       flowEndNanoseconds, 8);
193   f++;
194
195   return f;
196 }
197
198 static inline ipfix_field_specifier_t *
199 flowprobe_template_l4_fields (ipfix_field_specifier_t * f)
200 {
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);
205   f++;
206   /* destinationTransportPort, TLV type 11, u16 */
207   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
208                                       destinationTransportPort, 2);
209   f++;
210   /* tcpControlBits, TLV type 6, u16 */
211   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
212                                       tcpControlBits, 2);
213   f++;
214
215   return f;
216 }
217
218 /**
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
226  */
227 static inline u8 *
228 flowprobe_template_rewrite_inline (ipfix_exporter_t *exp, flow_report_t *fr,
229                                    u16 collector_port,
230                                    flowprobe_variant_t which)
231 {
232   ip4_header_t *ip;
233   udp_header_t *udp;
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;
239   u8 *rewrite = 0;
240   ip4_ipfix_template_packet_t *tp;
241   u32 field_count = 0;
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;
247
248   stream = &exp->streams[fr->stream_index];
249
250   if (flags & FLOW_RECORD_L3)
251     {
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;
258     }
259   if (flags & FLOW_RECORD_L4)
260     {
261       collect_l4 = (which != FLOW_VARIANT_L2);
262     }
263
264   field_count += flowprobe_template_common_field_count ();
265   if (flags & FLOW_RECORD_L2)
266     field_count += flowprobe_template_l2_field_count ();
267   if (collect_ip4)
268     field_count += flowprobe_template_ip4_field_count ();
269   if (collect_ip6)
270     field_count += flowprobe_template_ip6_field_count ();
271   if (collect_l4)
272     field_count += flowprobe_template_l4_field_count ();
273
274   /* allocate rewrite space */
275   vec_validate_aligned
276     (rewrite, sizeof (ip4_ipfix_template_packet_t)
277      + field_count * sizeof (ipfix_field_specifier_t) - 1,
278      CLIB_CACHE_LINE_BYTES);
279
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);
287
288   ip->ip_version_and_header_length = 0x45;
289   ip->ttl = 254;
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));
296
297   /* FIXUP: message header export_time */
298   /* FIXUP: message header sequence_number */
299   h->domain_id = clib_host_to_net_u32 (stream->domain_id);
300
301   /* Add TLVs to the template */
302   f = flowprobe_template_common_fields (f);
303
304   if (flags & FLOW_RECORD_L2)
305     f = flowprobe_template_l2_fields (f);
306   if (collect_ip4)
307     f = flowprobe_template_ip4_fields (f);
308   if (collect_ip6)
309     f = flowprobe_template_ip6_fields (f);
310   if (collect_l4)
311     f = flowprobe_template_l4_fields (f);
312
313   /* Back to the template packet... */
314   ip = (ip4_header_t *) & tp->ip4;
315   udp = (udp_header_t *) (ip + 1);
316
317   ASSERT (f - first_field);
318   /* Field count in this template */
319   t->id_count = ipfix_id_count (fr->template_id, f - first_field);
320
321   fm->template_size[flags] = (u8 *) f - (u8 *) s;
322
323   /* set length in octets */
324   s->set_id_length =
325     ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
326
327   /* message length in octets */
328   h->version_length = version_length ((u8 *) f - (u8 *) h);
329
330   ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
331   ip->checksum = ip4_header_checksum (ip);
332
333   return rewrite;
334 }
335
336 static u8 *
337 flowprobe_template_rewrite_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
338                                 u16 collector_port,
339                                 ipfix_report_element_t *elts, u32 n_elts,
340                                 u32 *stream_index)
341 {
342   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
343                                             FLOW_VARIANT_IP6);
344 }
345
346 static u8 *
347 flowprobe_template_rewrite_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
348                                 u16 collector_port,
349                                 ipfix_report_element_t *elts, u32 n_elts,
350                                 u32 *stream_index)
351 {
352   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
353                                             FLOW_VARIANT_IP4);
354 }
355
356 static u8 *
357 flowprobe_template_rewrite_l2 (ipfix_exporter_t *exp, flow_report_t *fr,
358                                u16 collector_port,
359                                ipfix_report_element_t *elts, u32 n_elts,
360                                u32 *stream_index)
361 {
362   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
363                                             FLOW_VARIANT_L2);
364 }
365
366 static u8 *
367 flowprobe_template_rewrite_l2_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
368                                    u16 collector_port,
369                                    ipfix_report_element_t *elts, u32 n_elts,
370                                    u32 *stream_index)
371 {
372   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
373                                             FLOW_VARIANT_L2_IP4);
374 }
375
376 static u8 *
377 flowprobe_template_rewrite_l2_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
378                                    u16 collector_port,
379                                    ipfix_report_element_t *elts, u32 n_elts,
380                                    u32 *stream_index)
381 {
382   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
383                                             FLOW_VARIANT_L2_IP6);
384 }
385
386 /**
387  * @brief Flush accumulated data
388  * @param frm flow_report_main_t *
389  * @param fr flow_report_t *
390  * @param f vlib_frame_t *
391  *
392  * <em>Notes:</em>
393  * This function must simply return the incoming frame, or no template packets
394  * will be sent.
395  */
396 vlib_frame_t *
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,
399                              u32 node_index)
400 {
401   flowprobe_flush_callback_ip4 ();
402   return f;
403 }
404
405 vlib_frame_t *
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,
408                              u32 node_index)
409 {
410   flowprobe_flush_callback_ip6 ();
411   return f;
412 }
413
414 vlib_frame_t *
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,
417                             u32 node_index)
418 {
419   flowprobe_flush_callback_l2 ();
420   return f;
421 }
422
423 static int
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)
429 {
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,
434     .is_add = is_add,
435     .domain_id = domain_id,
436     .src_port = src_port,
437     .opaque.as_uword = flags,
438   };
439   return vnet_flow_report_add_del (exp, &a, template_id);
440 }
441
442 static void
443 flowprobe_expired_timer_callback (u32 * expired_timers)
444 {
445   vlib_main_t *vm = vlib_get_main ();
446   flowprobe_main_t *fm = &flowprobe_main;
447   u32 my_cpu_number = vm->thread_index;
448   int i;
449   u32 poolindex;
450
451   for (i = 0; i < vec_len (expired_timers); i++)
452     {
453       poolindex = expired_timers[i] & 0x7FFFFFFF;
454       vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex);
455     }
456 }
457
458 static clib_error_t *
459 flowprobe_create_state_tables (u32 active_timer)
460 {
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;
465   u32 num_threads;
466   int i;
467
468   /* Decide how many worker threads we have */
469   num_threads = 1 /* main thread */  + tm->n_threads;
470
471   /* Hash table per worker */
472   fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE;
473
474   /* Init per worker flow state and timer wheels */
475   if (active_timer)
476     {
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);
481
482       for (i = 0; i < num_threads; i++)
483         {
484           int j;
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,
493                                             1.0, 1024);
494         }
495       fm->disabled = true;
496     }
497   else
498     {
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;
504     }
505   fm->initialized = true;
506   return error;
507 }
508
509 static clib_error_t *
510 flowprobe_clear_state_if_index (u32 sw_if_index)
511 {
512   flowprobe_main_t *fm = &flowprobe_main;
513   clib_error_t *error = 0;
514   u32 worker_i;
515   u32 entry_i;
516
517   if (fm->active_timer > 0)
518     {
519       vec_foreach_index (worker_i, fm->pool_per_worker)
520         {
521           pool_foreach_index (entry_i, fm->pool_per_worker[worker_i])
522             {
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)
527                 {
528                   e->packetcount = 0;
529                   e->octetcount = 0;
530                   e->prot.tcp.flags = 0;
531                   if (fm->passive_timer > 0)
532                     {
533                       tw_timer_stop_2t_1w_2048sl (
534                         fm->timers_per_worker[worker_i],
535                         e->passive_timer_handle);
536                     }
537                   flowprobe_delete_by_index (worker_i, entry_i);
538                 }
539             }
540         }
541     }
542
543   return error;
544 }
545
546 static int
547 validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index,
548                                u8 which)
549 {
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);
552
553   if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0)
554     return -1;
555   else if (fm->flow_per_interface[sw_if_index] != which)
556     return 0;
557   else
558     return 1;
559 }
560
561 /**
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
569  */
570
571 static int
572 flowprobe_interface_add_del_feature (flowprobe_main_t *fm, u32 sw_if_index,
573                                      u8 which, u8 direction, int is_add)
574 {
575   vlib_main_t *vm = vlib_get_main ();
576   int rv = 0;
577   u16 template_id = 0;
578   flowprobe_record_t flags = fm->record;
579
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];
585
586   if ((is_add && fm->template_per_flow[which] == 1) ||
587       (!is_add && fm->template_per_flow[which] == 0))
588     {
589       if (which == FLOW_VARIANT_L2)
590         {
591           if (!is_add)
592             {
593               flowprobe_flush_callback_l2 ();
594             }
595           if (fm->record & FLOW_RECORD_L2)
596             {
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;
602             }
603           if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
604             {
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;
611               rv =
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;
618
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;
624             }
625         }
626       else if (which == FLOW_VARIANT_IP4)
627         {
628           if (!is_add)
629             {
630               flowprobe_flush_callback_ip4 ();
631             }
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;
636         }
637       else if (which == FLOW_VARIANT_IP6)
638         {
639           if (!is_add)
640             {
641               flowprobe_flush_callback_ip6 ();
642             }
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;
647         }
648     }
649   if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
650     {
651       clib_warning ("vnet_flow_report_add_del returned %d", rv);
652       return -1;
653     }
654
655   if (which != (u8) ~ 0)
656     {
657       fm->context[which].flags = fm->record;
658     }
659
660   if (direction == FLOW_DIRECTION_RX || direction == FLOW_DIRECTION_BOTH)
661     {
662       if (which == FLOW_VARIANT_IP4)
663         {
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);
668         }
669       else if (which == FLOW_VARIANT_IP6)
670         {
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);
675         }
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);
679     }
680
681   if (direction == FLOW_DIRECTION_TX || direction == FLOW_DIRECTION_BOTH)
682     {
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);
692     }
693
694   /* Stateful flow collection */
695   if (is_add && !fm->initialized)
696     {
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);
700     }
701
702   if (!is_add && fm->initialized)
703     {
704       flowprobe_clear_state_if_index (sw_if_index);
705     }
706
707   return 0;
708 }
709
710 /**
711  * @brief API message handler
712  * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
713  */
714 void vl_api_flowprobe_tx_interface_add_del_t_handler
715   (vl_api_flowprobe_tx_interface_add_del_t * mp)
716 {
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);
720   int rv = 0;
721
722   VALIDATE_SW_IF_INDEX (mp);
723
724   if (fm->record == 0)
725     {
726       clib_warning ("Please specify flowprobe params record first...");
727       rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
728       goto out;
729     }
730
731   rv = validate_feature_on_interface (fm, sw_if_index, mp->which);
732   if ((rv == 1 && mp->is_add == 1) || rv == 0)
733     {
734       rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
735       goto out;
736     }
737
738   rv = flowprobe_interface_add_del_feature (fm, sw_if_index, mp->which,
739                                             FLOW_DIRECTION_TX, mp->is_add);
740
741 out:
742   BAD_SW_IF_INDEX_LABEL;
743
744   REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY);
745 }
746
747 void
748 vl_api_flowprobe_interface_add_del_t_handler (
749   vl_api_flowprobe_interface_add_del_t *mp)
750 {
751   flowprobe_main_t *fm = &flowprobe_main;
752   vl_api_flowprobe_interface_add_del_reply_t *rmp;
753   u32 sw_if_index;
754   u8 which;
755   u8 direction;
756   bool is_add;
757   int rv = 0;
758
759   VALIDATE_SW_IF_INDEX (mp);
760
761   sw_if_index = ntohl (mp->sw_if_index);
762   is_add = mp->is_add;
763
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;
770   else
771     {
772       clib_warning ("Invalid value of which");
773       rv = VNET_API_ERROR_INVALID_VALUE;
774       goto out;
775     }
776
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;
783   else
784     {
785       clib_warning ("Invalid value of direction");
786       rv = VNET_API_ERROR_INVALID_VALUE;
787       goto out;
788     }
789
790   if (fm->record == 0)
791     {
792       clib_warning ("Please specify flowprobe params record first");
793       rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
794       goto out;
795     }
796
797   rv = validate_feature_on_interface (fm, sw_if_index, which);
798   if (rv == 1)
799     {
800       if (is_add)
801         {
802           clib_warning ("Variant is already enabled for given interface");
803           rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
804           goto out;
805         }
806     }
807   else if (rv == 0)
808     {
809       clib_warning ("Interface has different variant enabled");
810       rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
811       goto out;
812     }
813   else if (rv == -1)
814     {
815       if (!is_add)
816         {
817           clib_warning ("Interface has no variant enabled");
818           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
819           goto out;
820         }
821     }
822
823   rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
824                                             is_add);
825
826 out:
827   BAD_SW_IF_INDEX_LABEL;
828
829   REPLY_MACRO (VL_API_FLOWPROBE_INTERFACE_ADD_DEL_REPLY);
830 }
831
832 static void
833 send_flowprobe_interface_details (u32 sw_if_index, u8 which, u8 direction,
834                                   vl_api_registration_t *reg, u32 context)
835 {
836   flowprobe_main_t *fm = &flowprobe_main;
837   vl_api_flowprobe_interface_details_t *rmp = 0;
838
839   rmp = vl_msg_api_alloc (sizeof (*rmp));
840   if (!rmp)
841     return;
842   clib_memset (rmp, 0, sizeof (*rmp));
843   rmp->_vl_msg_id =
844     ntohs (VL_API_FLOWPROBE_INTERFACE_DETAILS + REPLY_MSG_ID_BASE);
845   rmp->context = context;
846
847   rmp->sw_if_index = htonl (sw_if_index);
848
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;
855   else
856     ASSERT (0);
857
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;
864   else
865     ASSERT (0);
866
867   vl_api_send_msg (reg, (u8 *) rmp);
868 }
869
870 static void
871 vl_api_flowprobe_interface_dump_t_handler (
872   vl_api_flowprobe_interface_dump_t *mp)
873 {
874   flowprobe_main_t *fm = &flowprobe_main;
875   vl_api_registration_t *reg;
876   u32 sw_if_index;
877
878   reg = vl_api_client_index_to_registration (mp->client_index);
879   if (!reg)
880     return;
881
882   sw_if_index = ntohl (mp->sw_if_index);
883
884   if (sw_if_index == ~0)
885     {
886       u8 *which;
887
888       vec_foreach (which, fm->flow_per_interface)
889         {
890           if (*which == (u8) ~0)
891             continue;
892
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,
896             mp->context);
897         }
898     }
899   else if (vec_len (fm->flow_per_interface) > sw_if_index &&
900            fm->flow_per_interface[sw_if_index] != (u8) ~0)
901     {
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);
905     }
906 }
907
908 #define vec_neg_search(v,E)         \
909 ({              \
910   word _v(i) = 0;         \
911   while (_v(i) < vec_len(v) && v[_v(i)] == E)        \
912   {             \
913     _v(i)++;            \
914   }             \
915   if (_v(i) == vec_len(v))        \
916     _v(i) = ~0;                 \
917   _v(i);            \
918 })
919
920 static int
921 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
922                   u8 record_l3, u8 record_l4,
923                   u32 active_timer, u32 passive_timer)
924 {
925   flowprobe_record_t flags = 0;
926
927   if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
928     return VNET_API_ERROR_UNSUPPORTED;
929
930   if (record_l2)
931     flags |= FLOW_RECORD_L2;
932   if (record_l3)
933     flags |= FLOW_RECORD_L3;
934   if (record_l4)
935     flags |= FLOW_RECORD_L4;
936
937   fm->record = flags;
938
939   /*
940    * Timers: ~0 is default, 0 is off
941    */
942   fm->active_timer =
943     (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
944   fm->passive_timer =
945     (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
946
947   return 0;
948 }
949
950 void
951 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
952 {
953   flowprobe_main_t *fm = &flowprobe_main;
954   vl_api_flowprobe_params_reply_t *rmp;
955   int rv = 0;
956
957   rv = flowprobe_params
958     (fm,
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));
964
965   REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
966 }
967
968 void
969 vl_api_flowprobe_set_params_t_handler (vl_api_flowprobe_set_params_t *mp)
970 {
971   flowprobe_main_t *fm = &flowprobe_main;
972   vl_api_flowprobe_set_params_reply_t *rmp;
973   bool record_l2, record_l3, record_l4;
974   u32 active_timer;
975   u32 passive_timer;
976   int rv = 0;
977
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);
981
982   active_timer = clib_net_to_host_u32 (mp->active_timer);
983   passive_timer = clib_net_to_host_u32 (mp->passive_timer);
984
985   if (passive_timer > 0 && active_timer > passive_timer)
986     {
987       clib_warning ("Passive timer must be greater than active timer");
988       rv = VNET_API_ERROR_INVALID_VALUE;
989       goto out;
990     }
991
992   rv = flowprobe_params (fm, record_l2, record_l3, record_l4, active_timer,
993                          passive_timer);
994   if (rv == VNET_API_ERROR_UNSUPPORTED)
995     clib_warning (
996       "Cannot change params when feature is enabled on some interfaces");
997
998 out:
999   REPLY_MACRO (VL_API_FLOWPROBE_SET_PARAMS_REPLY);
1000 }
1001
1002 void
1003 vl_api_flowprobe_get_params_t_handler (vl_api_flowprobe_get_params_t *mp)
1004 {
1005   flowprobe_main_t *fm = &flowprobe_main;
1006   vl_api_flowprobe_get_params_reply_t *rmp;
1007   u8 record_flags = 0;
1008   int rv = 0;
1009
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;
1016
1017   // clang-format off
1018   REPLY_MACRO2 (VL_API_FLOWPROBE_GET_PARAMS_REPLY,
1019   ({
1020     rmp->record_flags = record_flags;
1021     rmp->active_timer = htonl (fm->active_timer);
1022     rmp->passive_timer = htonl (fm->passive_timer);
1023   }));
1024   // clang-format on
1025 }
1026
1027 VLIB_PLUGIN_REGISTER () = {
1028     .version = VPP_BUILD_VER,
1029     .description = "Flow per Packet",
1030 };
1031
1032 u8 *
1033 format_flowprobe_direction (u8 *s, va_list *args)
1034 {
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");
1042
1043   return s;
1044 }
1045
1046 u8 *
1047 format_flowprobe_entry (u8 * s, va_list * args)
1048 {
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);
1052
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));
1061
1062   return s;
1063 }
1064
1065 u8 *
1066 format_flowprobe_feature (u8 * s, va_list * args)
1067 {
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");
1075
1076   return s;
1077 }
1078
1079 u8 *
1080 format_flowprobe_params (u8 * s, va_list * args)
1081 {
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);
1085
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");
1092
1093   if (active_timer != (u32) ~ 0)
1094     s = format (s, " active: %d", active_timer);
1095
1096   if (passive_timer != (u32) ~ 0)
1097     s = format (s, " passive: %d", passive_timer);
1098
1099   return s;
1100 }
1101
1102 static clib_error_t *
1103 flowprobe_show_table_fn (vlib_main_t * vm,
1104                          unformat_input_t * input, vlib_cli_command_t * cm)
1105 {
1106   flowprobe_main_t *fm = &flowprobe_main;
1107   int i;
1108   flowprobe_entry_t *e;
1109
1110   vlib_cli_output (vm, "Dumping IPFIX table");
1111
1112   for (i = 0; i < vec_len (fm->pool_per_worker); i++)
1113     {
1114       pool_foreach (e, fm->pool_per_worker[i])
1115         {
1116           vlib_cli_output (vm, "%U",
1117                            format_flowprobe_entry,
1118                            e);
1119         }
1120
1121     }
1122   return 0;
1123 }
1124
1125 static clib_error_t *
1126 flowprobe_show_stats_fn (vlib_main_t * vm,
1127                          unformat_input_t * input, vlib_cli_command_t * cm)
1128 {
1129   flowprobe_main_t *fm = &flowprobe_main;
1130   int i;
1131
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);
1136
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));
1141   return 0;
1142 }
1143
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)
1148 {
1149   flowprobe_main_t *fm = &flowprobe_main;
1150   u32 sw_if_index = ~0;
1151   int is_add = 1;
1152   u8 which = FLOW_VARIANT_IP4;
1153   flowprobe_direction_t direction = FLOW_DIRECTION_TX;
1154   int rv;
1155
1156   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1157     {
1158       if (unformat (input, "disable"))
1159         is_add = 0;
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;
1174       else
1175         break;
1176     }
1177
1178   if (fm->record == 0)
1179     return clib_error_return (0,
1180                               "Please specify flowprobe params record first...");
1181
1182   if (sw_if_index == ~0)
1183     return clib_error_return (0, "Please specify an interface...");
1184
1185   rv = validate_feature_on_interface (fm, sw_if_index, which);
1186   if (rv == 1)
1187     {
1188       if (is_add)
1189         return clib_error_return (0,
1190                                   "Datapath is already enabled for given interface...");
1191     }
1192   else if (rv == 0)
1193     return clib_error_return (0,
1194                               "Interface has enable different datapath ...");
1195   else if (rv == -1)
1196     {
1197       if (!is_add)
1198         {
1199           return clib_error_return (0, "Interface has no datapath enabled");
1200         }
1201     }
1202
1203   rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
1204                                             is_add);
1205   switch (rv)
1206     {
1207     case 0:
1208       break;
1209
1210     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
1211       return clib_error_return
1212         (0, "Invalid interface, only works on physical ports");
1213       break;
1214
1215     case VNET_API_ERROR_UNIMPLEMENTED:
1216       return clib_error_return (0, "ip6 not supported");
1217       break;
1218
1219     default:
1220       return clib_error_return (0, "flowprobe_enable_disable returned %d",
1221                                 rv);
1222     }
1223   return 0;
1224 }
1225
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)
1230 {
1231   flowprobe_main_t *fm = &flowprobe_main;
1232   u8 *which;
1233   u32 sw_if_index;
1234
1235   vec_foreach (which, fm->flow_per_interface)
1236   {
1237     if (*which == (u8) ~ 0)
1238       continue;
1239
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]);
1245   }
1246   return 0;
1247 }
1248
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)
1253 {
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;
1258
1259   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1260     {
1261       if (unformat (input, "active %d", &active_timer))
1262         ;
1263       else if (unformat (input, "passive %d", &passive_timer))
1264         ;
1265       else if (unformat (input, "record"))
1266         while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1267           {
1268             if (unformat (input, "l2"))
1269               record_l2 = true;
1270             else if (unformat (input, "l3"))
1271               record_l3 = true;
1272             else if (unformat (input, "l4"))
1273               record_l4 = true;
1274             else
1275               break;
1276           }
1277       else
1278         break;
1279     }
1280
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...");
1284
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 ...");
1289   return 0;
1290 }
1291
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)
1296 {
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;
1301
1302   vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
1303                    passive_timer);
1304   return 0;
1305 }
1306
1307 /*?
1308  * '<em>flowprobe feature add-del</em>' commands to enable/disable
1309  * per-packet IPFIX flow record generation on an interface
1310  *
1311  * @cliexpar
1312  * @parblock
1313  * To enable per-packet IPFIX flow-record generation on an interface:
1314  * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
1315  *
1316  * To disable per-packet IPFIX flow-record generation on an interface:
1317  * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
1318  * @cliexend
1319  * @endparblock
1320 ?*/
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,
1326 };
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,
1332 };
1333
1334 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
1335     .path = "show flowprobe feature",
1336     .short_help =
1337     "show flowprobe feature",
1338     .function = flowprobe_show_feature_command_fn,
1339 };
1340 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
1341     .path = "show flowprobe params",
1342     .short_help =
1343     "show flowprobe params",
1344     .function = flowprobe_show_params_command_fn,
1345 };
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,
1350 };
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,
1355 };
1356
1357 /*
1358  * Main-core process, sending an interrupt to the per worker input
1359  * process that spins the per worker timer wheel.
1360  */
1361 static uword
1362 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1363 {
1364   uword *event_data = 0;
1365   vlib_main_t **worker_vms = 0, *worker_vm;
1366   flowprobe_main_t *fm = &flowprobe_main;
1367
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);
1374
1375   int i;
1376   if (vlib_get_n_threads () == 0)
1377     vec_add1 (worker_vms, vm);
1378   else
1379     {
1380       for (i = 0; i < vlib_get_n_threads (); i++)
1381         {
1382           worker_vm = vlib_get_main_by_index (i);
1383           if (worker_vm)
1384             vec_add1 (worker_vms, worker_vm);
1385         }
1386     }
1387   f64 sleep_duration = 0.1;
1388
1389   while (1)
1390     {
1391       /* Send an interrupt to each timer input node */
1392       sleep_duration = 0.1;
1393       for (i = 0; i < vec_len (worker_vms); i++)
1394         {
1395           worker_vm = worker_vms[i];
1396           if (worker_vm)
1397             {
1398               vlib_node_set_interrupt_pending (worker_vm,
1399                                                flowprobe_walker_node.index);
1400               sleep_duration =
1401                 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1402             }
1403         }
1404       vlib_process_suspend (vm, sleep_duration);
1405     }
1406   return 0;                     /* or not */
1407 }
1408
1409 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1410   .function = timer_process,
1411   .name = "flowprobe-timer-process",
1412   .type = VLIB_NODE_TYPE_PROCESS,
1413 };
1414
1415 #include <flowprobe/flowprobe.api.c>
1416
1417 /**
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
1421  */
1422 static clib_error_t *
1423 flowprobe_init (vlib_main_t * vm)
1424 {
1425   flowprobe_main_t *fm = &flowprobe_main;
1426   vlib_thread_main_t *tm = &vlib_thread_main;
1427   clib_error_t *error = 0;
1428   u32 num_threads;
1429   int i;
1430
1431   fm->vnet_main = vnet_get_main ();
1432
1433   /* Ask for a correctly-sized block of API message decode slots */
1434   fm->msg_id_base = setup_message_id_table ();
1435
1436   /* Set up time reference pair */
1437   fm->vlib_time_0 = vlib_time_now (vm);
1438   fm->nanosecond_time_0 = unix_time_now_nsec ();
1439
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));
1443
1444   /* Decide how many worker threads we have */
1445   num_threads = 1 /* main thread */  + tm->n_threads;
1446
1447   /* Allocate per worker thread vectors per flavour */
1448   for (i = 0; i < FLOW_N_VARIANTS; i++)
1449     {
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,
1453                     num_threads - 1);
1454     }
1455
1456   fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1457   fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1458
1459   return error;
1460 }
1461
1462 VLIB_INIT_FUNCTION (flowprobe_init);
1463
1464 /*
1465  * fd.io coding-style-patch-verification: ON
1466  *
1467  * Local Variables:
1468  * eval: (c-set-style "gnu")
1469  * End:
1470  */