flowprobe: add api messages to obtain current state
[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 /* *INDENT-OFF* */
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"),
53 };
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"),
58 };
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"),
63 };
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"),
68 };
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"),
73 };
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"),
78 };
79
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"),
84 };
85
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"),
90 };
91 /* *INDENT-ON* */
92
93 /* Macro to finish up custom dump fns */
94 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
95 #define FINISH                                  \
96     vec_add1 (s, 0);                            \
97     vl_print (handle, (char *)s);               \
98     vec_free (s);                               \
99     return handle;
100
101 static inline ipfix_field_specifier_t *
102 flowprobe_template_ip4_fields (ipfix_field_specifier_t * f)
103 {
104 #define flowprobe_template_ip4_field_count() 4
105   /* sourceIpv4Address, TLV type 8, u32 */
106   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
107                                       sourceIPv4Address, 4);
108   f++;
109   /* destinationIPv4Address, TLV type 12, u32 */
110   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
111                                       destinationIPv4Address, 4);
112   f++;
113   /* protocolIdentifier, TLV type 4, u8 */
114   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
115                                       protocolIdentifier, 1);
116   f++;
117   /* octetDeltaCount, TLV type 1, u64 */
118   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
119                                       octetDeltaCount, 8);
120   f++;
121   return f;
122 }
123
124 static inline ipfix_field_specifier_t *
125 flowprobe_template_ip6_fields (ipfix_field_specifier_t * f)
126 {
127 #define flowprobe_template_ip6_field_count() 4
128   /* sourceIpv6Address, TLV type 27, 16 octets */
129   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
130                                       sourceIPv6Address, 16);
131   f++;
132   /* destinationIPv6Address, TLV type 28, 16 octets */
133   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
134                                       destinationIPv6Address, 16);
135   f++;
136   /* protocolIdentifier, TLV type 4, u8 */
137   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
138                                       protocolIdentifier, 1);
139   f++;
140   /* octetDeltaCount, TLV type 1, u64 */
141   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
142                                       octetDeltaCount, 8);
143   f++;
144   return f;
145 }
146
147 static inline ipfix_field_specifier_t *
148 flowprobe_template_l2_fields (ipfix_field_specifier_t * f)
149 {
150 #define flowprobe_template_l2_field_count() 3
151   /* sourceMacAddress, TLV type 56, u8[6] we hope */
152   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
153                                       sourceMacAddress, 6);
154   f++;
155   /* destinationMacAddress, TLV type 80, u8[6] we hope */
156   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
157                                       destinationMacAddress, 6);
158   f++;
159   /* ethernetType, TLV type 256, u16 */
160   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
161                                       ethernetType, 2);
162   f++;
163   return f;
164 }
165
166 static inline ipfix_field_specifier_t *
167 flowprobe_template_common_fields (ipfix_field_specifier_t * f)
168 {
169 #define flowprobe_template_common_field_count() 6
170   /* ingressInterface, TLV type 10, u32 */
171   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
172                                       ingressInterface, 4);
173   f++;
174
175   /* egressInterface, TLV type 14, u32 */
176   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
177                                       egressInterface, 4);
178   f++;
179
180   /* flowDirection, TLV type 61, u8 */
181   f->e_id_length = ipfix_e_id_length (0 /* enterprise */, flowDirection, 1);
182   f++;
183
184   /* packetDeltaCount, TLV type 2, u64 */
185   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
186                                       packetDeltaCount, 8);
187   f++;
188
189   /* flowStartNanoseconds, TLV type 156, u64 */
190   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
191                                       flowStartNanoseconds, 8);
192   f++;
193
194   /* flowEndNanoseconds, TLV type 157, u64 */
195   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
196                                       flowEndNanoseconds, 8);
197   f++;
198
199   return f;
200 }
201
202 static inline ipfix_field_specifier_t *
203 flowprobe_template_l4_fields (ipfix_field_specifier_t * f)
204 {
205 #define flowprobe_template_l4_field_count() 3
206   /* sourceTransportPort, TLV type 7, u16 */
207   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
208                                       sourceTransportPort, 2);
209   f++;
210   /* destinationTransportPort, TLV type 11, u16 */
211   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
212                                       destinationTransportPort, 2);
213   f++;
214   /* tcpControlBits, TLV type 6, u16 */
215   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
216                                       tcpControlBits, 2);
217   f++;
218
219   return f;
220 }
221
222 /**
223  * @brief Create an IPFIX template packet rewrite string
224  * @param frm flow_report_main_t *
225  * @param fr flow_report_t *
226  * @param collector_address ip4_address_t * the IPFIX collector address
227  * @param src_address ip4_address_t * the source address we should use
228  * @param collector_port u16 the collector port we should use, host byte order
229  * @returns u8 * vector containing the indicated IPFIX template packet
230  */
231 static inline u8 *
232 flowprobe_template_rewrite_inline (ipfix_exporter_t *exp, flow_report_t *fr,
233                                    u16 collector_port,
234                                    flowprobe_variant_t which)
235 {
236   ip4_header_t *ip;
237   udp_header_t *udp;
238   ipfix_message_header_t *h;
239   ipfix_set_header_t *s;
240   ipfix_template_header_t *t;
241   ipfix_field_specifier_t *f;
242   ipfix_field_specifier_t *first_field;
243   u8 *rewrite = 0;
244   ip4_ipfix_template_packet_t *tp;
245   u32 field_count = 0;
246   flow_report_stream_t *stream;
247   flowprobe_main_t *fm = &flowprobe_main;
248   flowprobe_record_t flags = fr->opaque.as_uword;
249   bool collect_ip4 = false, collect_ip6 = false;
250
251   stream = &exp->streams[fr->stream_index];
252
253   if (flags & FLOW_RECORD_L3)
254     {
255       collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
256       collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
257       if (which == FLOW_VARIANT_L2_IP4)
258         flags |= FLOW_RECORD_L2_IP4;
259       if (which == FLOW_VARIANT_L2_IP6)
260         flags |= FLOW_RECORD_L2_IP6;
261     }
262
263   field_count += flowprobe_template_common_field_count ();
264   if (flags & FLOW_RECORD_L2)
265     field_count += flowprobe_template_l2_field_count ();
266   if (collect_ip4)
267     field_count += flowprobe_template_ip4_field_count ();
268   if (collect_ip6)
269     field_count += flowprobe_template_ip6_field_count ();
270   if (flags & FLOW_RECORD_L4)
271     field_count += flowprobe_template_l4_field_count ();
272
273   /* allocate rewrite space */
274   vec_validate_aligned
275     (rewrite, sizeof (ip4_ipfix_template_packet_t)
276      + field_count * sizeof (ipfix_field_specifier_t) - 1,
277      CLIB_CACHE_LINE_BYTES);
278
279   tp = (ip4_ipfix_template_packet_t *) rewrite;
280   ip = (ip4_header_t *) & tp->ip4;
281   udp = (udp_header_t *) (ip + 1);
282   h = (ipfix_message_header_t *) (udp + 1);
283   s = (ipfix_set_header_t *) (h + 1);
284   t = (ipfix_template_header_t *) (s + 1);
285   first_field = f = (ipfix_field_specifier_t *) (t + 1);
286
287   ip->ip_version_and_header_length = 0x45;
288   ip->ttl = 254;
289   ip->protocol = IP_PROTOCOL_UDP;
290   ip->src_address.as_u32 = exp->src_address.ip.ip4.as_u32;
291   ip->dst_address.as_u32 = exp->ipfix_collector.ip.ip4.as_u32;
292   udp->src_port = clib_host_to_net_u16 (stream->src_port);
293   udp->dst_port = clib_host_to_net_u16 (collector_port);
294   udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
295
296   /* FIXUP: message header export_time */
297   /* FIXUP: message header sequence_number */
298   h->domain_id = clib_host_to_net_u32 (stream->domain_id);
299
300   /* Add TLVs to the template */
301   f = flowprobe_template_common_fields (f);
302
303   if (flags & FLOW_RECORD_L2)
304     f = flowprobe_template_l2_fields (f);
305   if (collect_ip4)
306     f = flowprobe_template_ip4_fields (f);
307   if (collect_ip6)
308     f = flowprobe_template_ip6_fields (f);
309   if (flags & FLOW_RECORD_L4)
310     f = flowprobe_template_l4_fields (f);
311
312   /* Back to the template packet... */
313   ip = (ip4_header_t *) & tp->ip4;
314   udp = (udp_header_t *) (ip + 1);
315
316   ASSERT (f - first_field);
317   /* Field count in this template */
318   t->id_count = ipfix_id_count (fr->template_id, f - first_field);
319
320   fm->template_size[flags] = (u8 *) f - (u8 *) s;
321
322   /* set length in octets */
323   s->set_id_length =
324     ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
325
326   /* message length in octets */
327   h->version_length = version_length ((u8 *) f - (u8 *) h);
328
329   ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
330   ip->checksum = ip4_header_checksum (ip);
331
332   return rewrite;
333 }
334
335 static u8 *
336 flowprobe_template_rewrite_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
337                                 u16 collector_port,
338                                 ipfix_report_element_t *elts, u32 n_elts,
339                                 u32 *stream_index)
340 {
341   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
342                                             FLOW_VARIANT_IP6);
343 }
344
345 static u8 *
346 flowprobe_template_rewrite_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
347                                 u16 collector_port,
348                                 ipfix_report_element_t *elts, u32 n_elts,
349                                 u32 *stream_index)
350 {
351   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
352                                             FLOW_VARIANT_IP4);
353 }
354
355 static u8 *
356 flowprobe_template_rewrite_l2 (ipfix_exporter_t *exp, flow_report_t *fr,
357                                u16 collector_port,
358                                ipfix_report_element_t *elts, u32 n_elts,
359                                u32 *stream_index)
360 {
361   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
362                                             FLOW_VARIANT_L2);
363 }
364
365 static u8 *
366 flowprobe_template_rewrite_l2_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
367                                    u16 collector_port,
368                                    ipfix_report_element_t *elts, u32 n_elts,
369                                    u32 *stream_index)
370 {
371   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
372                                             FLOW_VARIANT_L2_IP4);
373 }
374
375 static u8 *
376 flowprobe_template_rewrite_l2_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
377                                    u16 collector_port,
378                                    ipfix_report_element_t *elts, u32 n_elts,
379                                    u32 *stream_index)
380 {
381   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
382                                             FLOW_VARIANT_L2_IP6);
383 }
384
385 /**
386  * @brief Flush accumulated data
387  * @param frm flow_report_main_t *
388  * @param fr flow_report_t *
389  * @param f vlib_frame_t *
390  *
391  * <em>Notes:</em>
392  * This function must simply return the incoming frame, or no template packets
393  * will be sent.
394  */
395 vlib_frame_t *
396 flowprobe_data_callback_ip4 (flow_report_main_t *frm, ipfix_exporter_t *exp,
397                              flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
398                              u32 node_index)
399 {
400   flowprobe_flush_callback_ip4 ();
401   return f;
402 }
403
404 vlib_frame_t *
405 flowprobe_data_callback_ip6 (flow_report_main_t *frm, ipfix_exporter_t *exp,
406                              flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
407                              u32 node_index)
408 {
409   flowprobe_flush_callback_ip6 ();
410   return f;
411 }
412
413 vlib_frame_t *
414 flowprobe_data_callback_l2 (flow_report_main_t *frm, ipfix_exporter_t *exp,
415                             flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
416                             u32 node_index)
417 {
418   flowprobe_flush_callback_l2 ();
419   return f;
420 }
421
422 static int
423 flowprobe_template_add_del (u32 domain_id, u16 src_port,
424                             flowprobe_record_t flags,
425                             vnet_flow_data_callback_t * flow_data_callback,
426                             vnet_flow_rewrite_callback_t * rewrite_callback,
427                             bool is_add, u16 * template_id)
428 {
429   ipfix_exporter_t *exp = &flow_report_main.exporters[0];
430   vnet_flow_report_add_del_args_t a = {
431     .rewrite_callback = rewrite_callback,
432     .flow_data_callback = flow_data_callback,
433     .is_add = is_add,
434     .domain_id = domain_id,
435     .src_port = src_port,
436     .opaque.as_uword = flags,
437   };
438   return vnet_flow_report_add_del (exp, &a, template_id);
439 }
440
441 static void
442 flowprobe_expired_timer_callback (u32 * expired_timers)
443 {
444   vlib_main_t *vm = vlib_get_main ();
445   flowprobe_main_t *fm = &flowprobe_main;
446   u32 my_cpu_number = vm->thread_index;
447   int i;
448   u32 poolindex;
449
450   for (i = 0; i < vec_len (expired_timers); i++)
451     {
452       poolindex = expired_timers[i] & 0x7FFFFFFF;
453       vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex);
454     }
455 }
456
457 static clib_error_t *
458 flowprobe_create_state_tables (u32 active_timer)
459 {
460   flowprobe_main_t *fm = &flowprobe_main;
461   vlib_thread_main_t *tm = &vlib_thread_main;
462   vlib_main_t *vm = vlib_get_main ();
463   clib_error_t *error = 0;
464   u32 num_threads;
465   int i;
466
467   /* Decide how many worker threads we have */
468   num_threads = 1 /* main thread */  + tm->n_threads;
469
470   /* Hash table per worker */
471   fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE;
472
473   /* Init per worker flow state and timer wheels */
474   if (active_timer)
475     {
476       vec_validate (fm->timers_per_worker, num_threads - 1);
477       vec_validate (fm->expired_passive_per_worker, num_threads - 1);
478       vec_validate (fm->hash_per_worker, num_threads - 1);
479       vec_validate (fm->pool_per_worker, num_threads - 1);
480
481       for (i = 0; i < num_threads; i++)
482         {
483           int j;
484           pool_alloc (fm->pool_per_worker[i], 1 << fm->ht_log2len);
485           vec_resize (fm->hash_per_worker[i], 1 << fm->ht_log2len);
486           for (j = 0; j < (1 << fm->ht_log2len); j++)
487             fm->hash_per_worker[i][j] = ~0;
488           fm->timers_per_worker[i] =
489             clib_mem_alloc (sizeof (TWT (tw_timer_wheel)));
490           tw_timer_wheel_init_2t_1w_2048sl (fm->timers_per_worker[i],
491                                             flowprobe_expired_timer_callback,
492                                             1.0, 1024);
493         }
494       fm->disabled = true;
495     }
496   else
497     {
498       f64 now = vlib_time_now (vm);
499       vec_validate (fm->stateless_entry, num_threads - 1);
500       for (i = 0; i < num_threads; i++)
501         fm->stateless_entry[i].last_exported = now;
502       fm->disabled = false;
503     }
504   fm->initialized = true;
505   return error;
506 }
507
508 static int
509 validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index,
510                                u8 which)
511 {
512   vec_validate_init_empty (fm->flow_per_interface, sw_if_index, ~0);
513   vec_validate_init_empty (fm->direction_per_interface, sw_if_index, ~0);
514
515   if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0)
516     return -1;
517   else if (fm->flow_per_interface[sw_if_index] != which)
518     return 0;
519   else
520     return 1;
521 }
522
523 /**
524  * @brief configure / deconfigure the IPFIX flow-per-packet
525  * @param fm flowprobe_main_t * fm
526  * @param sw_if_index u32 the desired interface
527  * @param which u8 the desired datapath
528  * @param direction u8 the desired direction
529  * @param is_add int 1 to enable the feature, 0 to disable it
530  * @returns 0 if successful, non-zero otherwise
531  */
532
533 static int
534 flowprobe_interface_add_del_feature (flowprobe_main_t *fm, u32 sw_if_index,
535                                      u8 which, u8 direction, int is_add)
536 {
537   vlib_main_t *vm = vlib_get_main ();
538   int rv = 0;
539   u16 template_id = 0;
540   flowprobe_record_t flags = fm->record;
541
542   fm->flow_per_interface[sw_if_index] = (is_add) ? which : (u8) ~ 0;
543   fm->direction_per_interface[sw_if_index] = (is_add) ? direction : (u8) ~0;
544   fm->template_per_flow[which] += (is_add) ? 1 : -1;
545   if (is_add && fm->template_per_flow[which] > 1)
546     template_id = fm->template_reports[flags];
547
548   if ((is_add && fm->template_per_flow[which] == 1) ||
549       (!is_add && fm->template_per_flow[which] == 0))
550     {
551       if (which == FLOW_VARIANT_L2)
552         {
553           if (fm->record & FLOW_RECORD_L2)
554             {
555               rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
556                                                flowprobe_data_callback_l2,
557                                                flowprobe_template_rewrite_l2,
558                                                is_add, &template_id);
559             }
560           if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
561             {
562               rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
563                                                flowprobe_data_callback_l2,
564                                                flowprobe_template_rewrite_l2_ip4,
565                                                is_add, &template_id);
566               fm->template_reports[flags | FLOW_RECORD_L2_IP4] =
567                 (is_add) ? template_id : 0;
568               rv =
569                 flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
570                                             flowprobe_data_callback_l2,
571                                             flowprobe_template_rewrite_l2_ip6,
572                                             is_add, &template_id);
573               fm->template_reports[flags | FLOW_RECORD_L2_IP6] =
574                 (is_add) ? template_id : 0;
575
576               /* Special case L2 */
577               fm->context[FLOW_VARIANT_L2_IP4].flags =
578                 flags | FLOW_RECORD_L2_IP4;
579               fm->context[FLOW_VARIANT_L2_IP6].flags =
580                 flags | FLOW_RECORD_L2_IP6;
581
582               fm->template_reports[flags] = template_id;
583             }
584         }
585       else if (which == FLOW_VARIANT_IP4)
586         rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
587                                          flowprobe_data_callback_ip4,
588                                          flowprobe_template_rewrite_ip4,
589                                          is_add, &template_id);
590       else if (which == FLOW_VARIANT_IP6)
591         rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
592                                          flowprobe_data_callback_ip6,
593                                          flowprobe_template_rewrite_ip6,
594                                          is_add, &template_id);
595     }
596   if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
597     {
598       clib_warning ("vnet_flow_report_add_del returned %d", rv);
599       return -1;
600     }
601
602   if (which != (u8) ~ 0)
603     {
604       fm->context[which].flags = fm->record;
605       fm->template_reports[flags] = (is_add) ? template_id : 0;
606     }
607
608   if (direction == FLOW_DIRECTION_RX || direction == FLOW_DIRECTION_BOTH)
609     {
610       if (which == FLOW_VARIANT_IP4)
611         {
612           vnet_feature_enable_disable ("ip4-unicast", "flowprobe-input-ip4",
613                                        sw_if_index, is_add, 0, 0);
614           vnet_feature_enable_disable ("ip4-multicast", "flowprobe-input-ip4",
615                                        sw_if_index, is_add, 0, 0);
616         }
617       else if (which == FLOW_VARIANT_IP6)
618         {
619           vnet_feature_enable_disable ("ip6-unicast", "flowprobe-input-ip6",
620                                        sw_if_index, is_add, 0, 0);
621           vnet_feature_enable_disable ("ip6-multicast", "flowprobe-input-ip6",
622                                        sw_if_index, is_add, 0, 0);
623         }
624       else if (which == FLOW_VARIANT_L2)
625         vnet_feature_enable_disable ("device-input", "flowprobe-input-l2",
626                                      sw_if_index, is_add, 0, 0);
627     }
628
629   if (direction == FLOW_DIRECTION_TX || direction == FLOW_DIRECTION_BOTH)
630     {
631       if (which == FLOW_VARIANT_IP4)
632         vnet_feature_enable_disable ("ip4-output", "flowprobe-output-ip4",
633                                      sw_if_index, is_add, 0, 0);
634       else if (which == FLOW_VARIANT_IP6)
635         vnet_feature_enable_disable ("ip6-output", "flowprobe-output-ip6",
636                                      sw_if_index, is_add, 0, 0);
637       else if (which == FLOW_VARIANT_L2)
638         vnet_feature_enable_disable ("interface-output", "flowprobe-output-l2",
639                                      sw_if_index, is_add, 0, 0);
640     }
641
642   /* Stateful flow collection */
643   if (is_add && !fm->initialized)
644     {
645       flowprobe_create_state_tables (fm->active_timer);
646       if (fm->active_timer)
647         vlib_process_signal_event (vm, flowprobe_timer_node.index, 1, 0);
648     }
649
650   return 0;
651 }
652
653 /**
654  * @brief API message handler
655  * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
656  */
657 void vl_api_flowprobe_tx_interface_add_del_t_handler
658   (vl_api_flowprobe_tx_interface_add_del_t * mp)
659 {
660   flowprobe_main_t *fm = &flowprobe_main;
661   vl_api_flowprobe_tx_interface_add_del_reply_t *rmp;
662   u32 sw_if_index = ntohl (mp->sw_if_index);
663   int rv = 0;
664
665   VALIDATE_SW_IF_INDEX (mp);
666
667   if (fm->record == 0)
668     {
669       clib_warning ("Please specify flowprobe params record first...");
670       rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
671       goto out;
672     }
673
674   rv = validate_feature_on_interface (fm, sw_if_index, mp->which);
675   if ((rv == 1 && mp->is_add == 1) || rv == 0)
676     {
677       rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
678       goto out;
679     }
680
681   rv = flowprobe_interface_add_del_feature (fm, sw_if_index, mp->which,
682                                             FLOW_DIRECTION_TX, mp->is_add);
683
684 out:
685   BAD_SW_IF_INDEX_LABEL;
686
687   REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY);
688 }
689
690 void
691 vl_api_flowprobe_interface_add_del_t_handler (
692   vl_api_flowprobe_interface_add_del_t *mp)
693 {
694   flowprobe_main_t *fm = &flowprobe_main;
695   vl_api_flowprobe_interface_add_del_reply_t *rmp;
696   u32 sw_if_index;
697   u8 which;
698   u8 direction;
699   bool is_add;
700   int rv = 0;
701
702   VALIDATE_SW_IF_INDEX (mp);
703
704   sw_if_index = ntohl (mp->sw_if_index);
705   is_add = mp->is_add;
706
707   if (mp->which == FLOWPROBE_WHICH_IP4)
708     which = FLOW_VARIANT_IP4;
709   else if (mp->which == FLOWPROBE_WHICH_IP6)
710     which = FLOW_VARIANT_IP6;
711   else if (mp->which == FLOWPROBE_WHICH_L2)
712     which = FLOW_VARIANT_L2;
713   else
714     {
715       clib_warning ("Invalid value of which");
716       rv = VNET_API_ERROR_INVALID_VALUE;
717       goto out;
718     }
719
720   if (mp->direction == FLOWPROBE_DIRECTION_RX)
721     direction = FLOW_DIRECTION_RX;
722   else if (mp->direction == FLOWPROBE_DIRECTION_TX)
723     direction = FLOW_DIRECTION_TX;
724   else if (mp->direction == FLOWPROBE_DIRECTION_BOTH)
725     direction = FLOW_DIRECTION_BOTH;
726   else
727     {
728       clib_warning ("Invalid value of direction");
729       rv = VNET_API_ERROR_INVALID_VALUE;
730       goto out;
731     }
732
733   if (fm->record == 0)
734     {
735       clib_warning ("Please specify flowprobe params record first");
736       rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
737       goto out;
738     }
739
740   rv = validate_feature_on_interface (fm, sw_if_index, which);
741   if (rv == 1)
742     {
743       if (is_add)
744         {
745           clib_warning ("Variant is already enabled for given interface");
746           rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
747           goto out;
748         }
749     }
750   else if (rv == 0)
751     {
752       clib_warning ("Interface has different variant enabled");
753       rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
754       goto out;
755     }
756   else if (rv == -1)
757     {
758       if (!is_add)
759         {
760           clib_warning ("Interface has no variant enabled");
761           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
762           goto out;
763         }
764     }
765
766   rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
767                                             is_add);
768
769 out:
770   BAD_SW_IF_INDEX_LABEL;
771
772   REPLY_MACRO (VL_API_FLOWPROBE_INTERFACE_ADD_DEL_REPLY);
773 }
774
775 static void
776 send_flowprobe_interface_details (u32 sw_if_index, u8 which, u8 direction,
777                                   vl_api_registration_t *reg, u32 context)
778 {
779   flowprobe_main_t *fm = &flowprobe_main;
780   vl_api_flowprobe_interface_details_t *rmp = 0;
781
782   rmp = vl_msg_api_alloc (sizeof (*rmp));
783   if (!rmp)
784     return;
785   clib_memset (rmp, 0, sizeof (*rmp));
786   rmp->_vl_msg_id =
787     ntohs (VL_API_FLOWPROBE_INTERFACE_DETAILS + REPLY_MSG_ID_BASE);
788   rmp->context = context;
789
790   rmp->sw_if_index = htonl (sw_if_index);
791
792   if (which == FLOW_VARIANT_IP4)
793     rmp->which = FLOWPROBE_WHICH_IP4;
794   else if (which == FLOW_VARIANT_IP6)
795     rmp->which = FLOWPROBE_WHICH_IP6;
796   else if (which == FLOW_VARIANT_L2)
797     rmp->which = FLOWPROBE_WHICH_L2;
798   else
799     ASSERT (0);
800
801   if (direction == FLOW_DIRECTION_RX)
802     rmp->direction = FLOWPROBE_DIRECTION_RX;
803   else if (direction == FLOW_DIRECTION_TX)
804     rmp->direction = FLOWPROBE_DIRECTION_TX;
805   else if (direction == FLOW_DIRECTION_BOTH)
806     rmp->direction = FLOWPROBE_DIRECTION_BOTH;
807   else
808     ASSERT (0);
809
810   vl_api_send_msg (reg, (u8 *) rmp);
811 }
812
813 static void
814 vl_api_flowprobe_interface_dump_t_handler (
815   vl_api_flowprobe_interface_dump_t *mp)
816 {
817   flowprobe_main_t *fm = &flowprobe_main;
818   vl_api_registration_t *reg;
819   u32 sw_if_index;
820
821   reg = vl_api_client_index_to_registration (mp->client_index);
822   if (!reg)
823     return;
824
825   sw_if_index = ntohl (mp->sw_if_index);
826
827   if (sw_if_index == ~0)
828     {
829       u8 *which;
830
831       vec_foreach (which, fm->flow_per_interface)
832         {
833           if (*which == (u8) ~0)
834             continue;
835
836           sw_if_index = which - fm->flow_per_interface;
837           send_flowprobe_interface_details (
838             sw_if_index, *which, fm->direction_per_interface[sw_if_index], reg,
839             mp->context);
840         }
841     }
842   else if (vec_len (fm->flow_per_interface) > sw_if_index &&
843            fm->flow_per_interface[sw_if_index] != (u8) ~0)
844     {
845       send_flowprobe_interface_details (
846         sw_if_index, fm->flow_per_interface[sw_if_index],
847         fm->direction_per_interface[sw_if_index], reg, mp->context);
848     }
849 }
850
851 #define vec_neg_search(v,E)         \
852 ({              \
853   word _v(i) = 0;         \
854   while (_v(i) < vec_len(v) && v[_v(i)] == E)        \
855   {             \
856     _v(i)++;            \
857   }             \
858   if (_v(i) == vec_len(v))        \
859     _v(i) = ~0;                 \
860   _v(i);            \
861 })
862
863 static int
864 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
865                   u8 record_l3, u8 record_l4,
866                   u32 active_timer, u32 passive_timer)
867 {
868   flowprobe_record_t flags = 0;
869
870   if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
871     return VNET_API_ERROR_UNSUPPORTED;
872
873   if (record_l2)
874     flags |= FLOW_RECORD_L2;
875   if (record_l3)
876     flags |= FLOW_RECORD_L3;
877   if (record_l4)
878     flags |= FLOW_RECORD_L4;
879
880   fm->record = flags;
881
882   /*
883    * Timers: ~0 is default, 0 is off
884    */
885   fm->active_timer =
886     (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
887   fm->passive_timer =
888     (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
889
890   return 0;
891 }
892
893 void
894 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
895 {
896   flowprobe_main_t *fm = &flowprobe_main;
897   vl_api_flowprobe_params_reply_t *rmp;
898   int rv = 0;
899
900   rv = flowprobe_params
901     (fm,
902      mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
903      mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
904      mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
905      clib_net_to_host_u32 (mp->active_timer),
906      clib_net_to_host_u32 (mp->passive_timer));
907
908   REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
909 }
910
911 void
912 vl_api_flowprobe_set_params_t_handler (vl_api_flowprobe_set_params_t *mp)
913 {
914   flowprobe_main_t *fm = &flowprobe_main;
915   vl_api_flowprobe_set_params_reply_t *rmp;
916   bool record_l2, record_l3, record_l4;
917   u32 active_timer;
918   u32 passive_timer;
919   int rv = 0;
920
921   record_l2 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L2);
922   record_l3 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L3);
923   record_l4 = (mp->record_flags & FLOWPROBE_RECORD_FLAG_L4);
924
925   active_timer = clib_net_to_host_u32 (mp->active_timer);
926   passive_timer = clib_net_to_host_u32 (mp->passive_timer);
927
928   if (passive_timer > 0 && active_timer > passive_timer)
929     {
930       clib_warning ("Passive timer must be greater than active timer");
931       rv = VNET_API_ERROR_INVALID_VALUE;
932       goto out;
933     }
934
935   rv = flowprobe_params (fm, record_l2, record_l3, record_l4, active_timer,
936                          passive_timer);
937   if (rv == VNET_API_ERROR_UNSUPPORTED)
938     clib_warning (
939       "Cannot change params when feature is enabled on some interfaces");
940
941 out:
942   REPLY_MACRO (VL_API_FLOWPROBE_SET_PARAMS_REPLY);
943 }
944
945 void
946 vl_api_flowprobe_get_params_t_handler (vl_api_flowprobe_get_params_t *mp)
947 {
948   flowprobe_main_t *fm = &flowprobe_main;
949   vl_api_flowprobe_get_params_reply_t *rmp;
950   u8 record_flags = 0;
951   int rv = 0;
952
953   if (fm->record & FLOW_RECORD_L2)
954     record_flags |= FLOWPROBE_RECORD_FLAG_L2;
955   if (fm->record & FLOW_RECORD_L3)
956     record_flags |= FLOWPROBE_RECORD_FLAG_L3;
957   if (fm->record & FLOW_RECORD_L4)
958     record_flags |= FLOWPROBE_RECORD_FLAG_L4;
959
960   // clang-format off
961   REPLY_MACRO2 (VL_API_FLOWPROBE_GET_PARAMS_REPLY,
962   ({
963     rmp->record_flags = record_flags;
964     rmp->active_timer = htonl (fm->active_timer);
965     rmp->passive_timer = htonl (fm->passive_timer);
966   }));
967   // clang-format on
968 }
969
970 /* *INDENT-OFF* */
971 VLIB_PLUGIN_REGISTER () = {
972     .version = VPP_BUILD_VER,
973     .description = "Flow per Packet",
974 };
975 /* *INDENT-ON* */
976
977 u8 *
978 format_flowprobe_direction (u8 *s, va_list *args)
979 {
980   u8 *direction = va_arg (*args, u8 *);
981   if (*direction == FLOW_DIRECTION_RX)
982     s = format (s, "rx");
983   else if (*direction == FLOW_DIRECTION_TX)
984     s = format (s, "tx");
985   else if (*direction == FLOW_DIRECTION_BOTH)
986     s = format (s, "rx tx");
987
988   return s;
989 }
990
991 u8 *
992 format_flowprobe_entry (u8 * s, va_list * args)
993 {
994   flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
995   s = format (s, " %U", format_flowprobe_direction, &e->key.direction);
996   s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
997
998   s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
999               format_ethernet_address, &e->key.dst_mac);
1000   s = format (s, " %U -> %U",
1001               format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
1002               format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
1003   s = format (s, " %d", e->key.protocol);
1004   s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
1005               clib_net_to_host_u16 (e->key.dst_port));
1006
1007   return s;
1008 }
1009
1010 u8 *
1011 format_flowprobe_feature (u8 * s, va_list * args)
1012 {
1013   u8 *which = va_arg (*args, u8 *);
1014   if (*which == FLOW_VARIANT_IP4)
1015     s = format (s, "ip4");
1016   else if (*which == FLOW_VARIANT_IP6)
1017     s = format (s, "ip6");
1018   else if (*which == FLOW_VARIANT_L2)
1019     s = format (s, "l2");
1020
1021   return s;
1022 }
1023
1024 u8 *
1025 format_flowprobe_params (u8 * s, va_list * args)
1026 {
1027   flowprobe_record_t flags = va_arg (*args, flowprobe_record_t);
1028   u32 active_timer = va_arg (*args, u32);
1029   u32 passive_timer = va_arg (*args, u32);
1030
1031   if (flags & FLOW_RECORD_L2)
1032     s = format (s, " l2");
1033   if (flags & FLOW_RECORD_L3)
1034     s = format (s, " l3");
1035   if (flags & FLOW_RECORD_L4)
1036     s = format (s, " l4");
1037
1038   if (active_timer != (u32) ~ 0)
1039     s = format (s, " active: %d", active_timer);
1040
1041   if (passive_timer != (u32) ~ 0)
1042     s = format (s, " passive: %d", passive_timer);
1043
1044   return s;
1045 }
1046
1047 static clib_error_t *
1048 flowprobe_show_table_fn (vlib_main_t * vm,
1049                          unformat_input_t * input, vlib_cli_command_t * cm)
1050 {
1051   flowprobe_main_t *fm = &flowprobe_main;
1052   int i;
1053   flowprobe_entry_t *e;
1054
1055   vlib_cli_output (vm, "Dumping IPFIX table");
1056
1057   for (i = 0; i < vec_len (fm->pool_per_worker); i++)
1058     {
1059       /* *INDENT-OFF* */
1060       pool_foreach (e, fm->pool_per_worker[i])
1061         {
1062           vlib_cli_output (vm, "%U",
1063                            format_flowprobe_entry,
1064                            e);
1065         }
1066       /* *INDENT-ON* */
1067
1068     }
1069   return 0;
1070 }
1071
1072 static clib_error_t *
1073 flowprobe_show_stats_fn (vlib_main_t * vm,
1074                          unformat_input_t * input, vlib_cli_command_t * cm)
1075 {
1076   flowprobe_main_t *fm = &flowprobe_main;
1077   int i;
1078
1079   vlib_cli_output (vm, "IPFIX table statistics");
1080   vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
1081   vlib_cli_output (vm, "Flow pool size per thread: %d\n",
1082                    0x1 << FLOWPROBE_LOG2_HASHSIZE);
1083
1084   for (i = 0; i < vec_len (fm->pool_per_worker); i++)
1085     vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
1086                      (100 * pool_elts (fm->pool_per_worker[i])) /
1087                      (0x1 << FLOWPROBE_LOG2_HASHSIZE));
1088   return 0;
1089 }
1090
1091 static clib_error_t *
1092 flowprobe_interface_add_del_feature_command_fn (vlib_main_t *vm,
1093                                                 unformat_input_t *input,
1094                                                 vlib_cli_command_t *cmd)
1095 {
1096   flowprobe_main_t *fm = &flowprobe_main;
1097   u32 sw_if_index = ~0;
1098   int is_add = 1;
1099   u8 which = FLOW_VARIANT_IP4;
1100   flowprobe_direction_t direction = FLOW_DIRECTION_TX;
1101   int rv;
1102
1103   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1104     {
1105       if (unformat (input, "disable"))
1106         is_add = 0;
1107       else if (unformat (input, "%U", unformat_vnet_sw_interface,
1108                          fm->vnet_main, &sw_if_index));
1109       else if (unformat (input, "ip4"))
1110         which = FLOW_VARIANT_IP4;
1111       else if (unformat (input, "ip6"))
1112         which = FLOW_VARIANT_IP6;
1113       else if (unformat (input, "l2"))
1114         which = FLOW_VARIANT_L2;
1115       else if (unformat (input, "rx"))
1116         direction = FLOW_DIRECTION_RX;
1117       else if (unformat (input, "tx"))
1118         direction = FLOW_DIRECTION_TX;
1119       else if (unformat (input, "both"))
1120         direction = FLOW_DIRECTION_BOTH;
1121       else
1122         break;
1123     }
1124
1125   if (fm->record == 0)
1126     return clib_error_return (0,
1127                               "Please specify flowprobe params record first...");
1128
1129   if (sw_if_index == ~0)
1130     return clib_error_return (0, "Please specify an interface...");
1131
1132   rv = validate_feature_on_interface (fm, sw_if_index, which);
1133   if (rv == 1)
1134     {
1135       if (is_add)
1136         return clib_error_return (0,
1137                                   "Datapath is already enabled for given interface...");
1138     }
1139   else if (rv == 0)
1140     return clib_error_return (0,
1141                               "Interface has enable different datapath ...");
1142   else if (rv == -1)
1143     {
1144       if (!is_add)
1145         {
1146           return clib_error_return (0, "Interface has no datapath enabled");
1147         }
1148     }
1149
1150   rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
1151                                             is_add);
1152   switch (rv)
1153     {
1154     case 0:
1155       break;
1156
1157     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
1158       return clib_error_return
1159         (0, "Invalid interface, only works on physical ports");
1160       break;
1161
1162     case VNET_API_ERROR_UNIMPLEMENTED:
1163       return clib_error_return (0, "ip6 not supported");
1164       break;
1165
1166     default:
1167       return clib_error_return (0, "flowprobe_enable_disable returned %d",
1168                                 rv);
1169     }
1170   return 0;
1171 }
1172
1173 static clib_error_t *
1174 flowprobe_show_feature_command_fn (vlib_main_t * vm,
1175                                    unformat_input_t * input,
1176                                    vlib_cli_command_t * cmd)
1177 {
1178   flowprobe_main_t *fm = &flowprobe_main;
1179   u8 *which;
1180   u32 sw_if_index;
1181
1182   vec_foreach (which, fm->flow_per_interface)
1183   {
1184     if (*which == (u8) ~ 0)
1185       continue;
1186
1187     sw_if_index = which - fm->flow_per_interface;
1188     vlib_cli_output (vm, " %U %U %U", format_vnet_sw_if_index_name,
1189                      vnet_get_main (), sw_if_index, format_flowprobe_feature,
1190                      which, format_flowprobe_direction,
1191                      &fm->direction_per_interface[sw_if_index]);
1192   }
1193   return 0;
1194 }
1195
1196 static clib_error_t *
1197 flowprobe_params_command_fn (vlib_main_t * vm,
1198                              unformat_input_t * input,
1199                              vlib_cli_command_t * cmd)
1200 {
1201   flowprobe_main_t *fm = &flowprobe_main;
1202   bool record_l2 = false, record_l3 = false, record_l4 = false;
1203   u32 active_timer = ~0;
1204   u32 passive_timer = ~0;
1205
1206   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1207     {
1208       if (unformat (input, "active %d", &active_timer))
1209         ;
1210       else if (unformat (input, "passive %d", &passive_timer))
1211         ;
1212       else if (unformat (input, "record"))
1213         while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1214           {
1215             if (unformat (input, "l2"))
1216               record_l2 = true;
1217             else if (unformat (input, "l3"))
1218               record_l3 = true;
1219             else if (unformat (input, "l4"))
1220               record_l4 = true;
1221             else
1222               break;
1223           }
1224       else
1225         break;
1226     }
1227
1228   if (passive_timer > 0 && active_timer > passive_timer)
1229     return clib_error_return (0,
1230                               "Passive timer has to be greater than active one...");
1231
1232   if (flowprobe_params (fm, record_l2, record_l3, record_l4,
1233                         active_timer, passive_timer))
1234     return clib_error_return (0,
1235                               "Couldn't change flowperpacket params when feature is enabled on some interface ...");
1236   return 0;
1237 }
1238
1239 static clib_error_t *
1240 flowprobe_show_params_command_fn (vlib_main_t * vm,
1241                                   unformat_input_t * input,
1242                                   vlib_cli_command_t * cmd)
1243 {
1244   flowprobe_main_t *fm = &flowprobe_main;
1245   flowprobe_record_t flags = fm->record;
1246   u32 active_timer = fm->active_timer;
1247   u32 passive_timer = fm->passive_timer;
1248
1249   vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
1250                    passive_timer);
1251   return 0;
1252 }
1253
1254 /*?
1255  * '<em>flowprobe feature add-del</em>' commands to enable/disable
1256  * per-packet IPFIX flow record generation on an interface
1257  *
1258  * @cliexpar
1259  * @parblock
1260  * To enable per-packet IPFIX flow-record generation on an interface:
1261  * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
1262  *
1263  * To disable per-packet IPFIX flow-record generation on an interface:
1264  * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
1265  * @cliexend
1266  * @endparblock
1267 ?*/
1268 /* *INDENT-OFF* */
1269 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
1270   .path = "flowprobe feature add-del",
1271   .short_help = "flowprobe feature add-del <interface-name> [(l2|ip4|ip6)] "
1272                 "[(rx|tx|both)] [disable]",
1273   .function = flowprobe_interface_add_del_feature_command_fn,
1274 };
1275 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
1276   .path = "flowprobe params",
1277   .short_help = "flowprobe params record [l2] [l3] [l4] [active <timer>] "
1278                 "[passive <timer>]",
1279   .function = flowprobe_params_command_fn,
1280 };
1281
1282 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
1283     .path = "show flowprobe feature",
1284     .short_help =
1285     "show flowprobe feature",
1286     .function = flowprobe_show_feature_command_fn,
1287 };
1288 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
1289     .path = "show flowprobe params",
1290     .short_help =
1291     "show flowprobe params",
1292     .function = flowprobe_show_params_command_fn,
1293 };
1294 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
1295     .path = "show flowprobe table",
1296     .short_help = "show flowprobe table",
1297     .function = flowprobe_show_table_fn,
1298 };
1299 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
1300     .path = "show flowprobe statistics",
1301     .short_help = "show flowprobe statistics",
1302     .function = flowprobe_show_stats_fn,
1303 };
1304 /* *INDENT-ON* */
1305
1306 /*
1307  * Main-core process, sending an interrupt to the per worker input
1308  * process that spins the per worker timer wheel.
1309  */
1310 static uword
1311 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1312 {
1313   uword *event_data = 0;
1314   vlib_main_t **worker_vms = 0, *worker_vm;
1315   flowprobe_main_t *fm = &flowprobe_main;
1316
1317   /* Wait for Godot... */
1318   vlib_process_wait_for_event_or_clock (vm, 1e9);
1319   uword event_type = vlib_process_get_events (vm, &event_data);
1320   if (event_type != 1)
1321     clib_warning ("bogus kickoff event received, %d", event_type);
1322   vec_reset_length (event_data);
1323
1324   int i;
1325   if (vlib_get_n_threads () == 0)
1326     vec_add1 (worker_vms, vm);
1327   else
1328     {
1329       for (i = 0; i < vlib_get_n_threads (); i++)
1330         {
1331           worker_vm = vlib_get_main_by_index (i);
1332           if (worker_vm)
1333             vec_add1 (worker_vms, worker_vm);
1334         }
1335     }
1336   f64 sleep_duration = 0.1;
1337
1338   while (1)
1339     {
1340       /* Send an interrupt to each timer input node */
1341       sleep_duration = 0.1;
1342       for (i = 0; i < vec_len (worker_vms); i++)
1343         {
1344           worker_vm = worker_vms[i];
1345           if (worker_vm)
1346             {
1347               vlib_node_set_interrupt_pending (worker_vm,
1348                                                flowprobe_walker_node.index);
1349               sleep_duration =
1350                 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1351             }
1352         }
1353       vlib_process_suspend (vm, sleep_duration);
1354     }
1355   return 0;                     /* or not */
1356 }
1357
1358 /* *INDENT-OFF* */
1359 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1360   .function = timer_process,
1361   .name = "flowprobe-timer-process",
1362   .type = VLIB_NODE_TYPE_PROCESS,
1363 };
1364 /* *INDENT-ON* */
1365
1366 #include <flowprobe/flowprobe.api.c>
1367
1368 /**
1369  * @brief Set up the API message handling tables
1370  * @param vm vlib_main_t * vlib main data structure pointer
1371  * @returns 0 to indicate all is well, or a clib_error_t
1372  */
1373 static clib_error_t *
1374 flowprobe_init (vlib_main_t * vm)
1375 {
1376   flowprobe_main_t *fm = &flowprobe_main;
1377   vlib_thread_main_t *tm = &vlib_thread_main;
1378   clib_error_t *error = 0;
1379   u32 num_threads;
1380   int i;
1381
1382   fm->vnet_main = vnet_get_main ();
1383
1384   /* Ask for a correctly-sized block of API message decode slots */
1385   fm->msg_id_base = setup_message_id_table ();
1386
1387   /* Set up time reference pair */
1388   fm->vlib_time_0 = vlib_time_now (vm);
1389   fm->nanosecond_time_0 = unix_time_now_nsec ();
1390
1391   clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1392   clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1393   clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1394
1395   /* Decide how many worker threads we have */
1396   num_threads = 1 /* main thread */  + tm->n_threads;
1397
1398   /* Allocate per worker thread vectors per flavour */
1399   for (i = 0; i < FLOW_N_VARIANTS; i++)
1400     {
1401       vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1402       vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1403       vec_validate (fm->context[i].next_record_offset_per_worker,
1404                     num_threads - 1);
1405     }
1406
1407   fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1408   fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1409
1410   return error;
1411 }
1412
1413 VLIB_INIT_FUNCTION (flowprobe_init);
1414
1415 /*
1416  * fd.io coding-style-patch-verification: ON
1417  *
1418  * Local Variables:
1419  * eval: (c-set-style "gnu")
1420  * End:
1421  */