flowprobe: add support for reporting on inbound packets
[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 #define vec_neg_search(v,E)         \
776 ({              \
777   word _v(i) = 0;         \
778   while (_v(i) < vec_len(v) && v[_v(i)] == E)        \
779   {             \
780     _v(i)++;            \
781   }             \
782   if (_v(i) == vec_len(v))        \
783     _v(i) = ~0;                 \
784   _v(i);            \
785 })
786
787 static int
788 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
789                   u8 record_l3, u8 record_l4,
790                   u32 active_timer, u32 passive_timer)
791 {
792   flowprobe_record_t flags = 0;
793
794   if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
795     return ~0;
796
797   if (record_l2)
798     flags |= FLOW_RECORD_L2;
799   if (record_l3)
800     flags |= FLOW_RECORD_L3;
801   if (record_l4)
802     flags |= FLOW_RECORD_L4;
803
804   fm->record = flags;
805
806   /*
807    * Timers: ~0 is default, 0 is off
808    */
809   fm->active_timer =
810     (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
811   fm->passive_timer =
812     (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
813
814   return 0;
815 }
816
817 void
818 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
819 {
820   flowprobe_main_t *fm = &flowprobe_main;
821   vl_api_flowprobe_params_reply_t *rmp;
822   int rv = 0;
823
824   rv = flowprobe_params
825     (fm,
826      mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
827      mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
828      mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
829      clib_net_to_host_u32 (mp->active_timer),
830      clib_net_to_host_u32 (mp->passive_timer));
831
832   REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
833 }
834
835 /* *INDENT-OFF* */
836 VLIB_PLUGIN_REGISTER () = {
837     .version = VPP_BUILD_VER,
838     .description = "Flow per Packet",
839 };
840 /* *INDENT-ON* */
841
842 u8 *
843 format_flowprobe_direction (u8 *s, va_list *args)
844 {
845   u8 *direction = va_arg (*args, u8 *);
846   if (*direction == FLOW_DIRECTION_RX)
847     s = format (s, "rx");
848   else if (*direction == FLOW_DIRECTION_TX)
849     s = format (s, "tx");
850   else if (*direction == FLOW_DIRECTION_BOTH)
851     s = format (s, "rx tx");
852
853   return s;
854 }
855
856 u8 *
857 format_flowprobe_entry (u8 * s, va_list * args)
858 {
859   flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
860   s = format (s, " %U", format_flowprobe_direction, &e->key.direction);
861   s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
862
863   s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
864               format_ethernet_address, &e->key.dst_mac);
865   s = format (s, " %U -> %U",
866               format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
867               format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
868   s = format (s, " %d", e->key.protocol);
869   s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
870               clib_net_to_host_u16 (e->key.dst_port));
871
872   return s;
873 }
874
875 u8 *
876 format_flowprobe_feature (u8 * s, va_list * args)
877 {
878   u8 *which = va_arg (*args, u8 *);
879   if (*which == FLOW_VARIANT_IP4)
880     s = format (s, "ip4");
881   else if (*which == FLOW_VARIANT_IP6)
882     s = format (s, "ip6");
883   else if (*which == FLOW_VARIANT_L2)
884     s = format (s, "l2");
885
886   return s;
887 }
888
889 u8 *
890 format_flowprobe_params (u8 * s, va_list * args)
891 {
892   flowprobe_record_t flags = va_arg (*args, flowprobe_record_t);
893   u32 active_timer = va_arg (*args, u32);
894   u32 passive_timer = va_arg (*args, u32);
895
896   if (flags & FLOW_RECORD_L2)
897     s = format (s, " l2");
898   if (flags & FLOW_RECORD_L3)
899     s = format (s, " l3");
900   if (flags & FLOW_RECORD_L4)
901     s = format (s, " l4");
902
903   if (active_timer != (u32) ~ 0)
904     s = format (s, " active: %d", active_timer);
905
906   if (passive_timer != (u32) ~ 0)
907     s = format (s, " passive: %d", passive_timer);
908
909   return s;
910 }
911
912 static clib_error_t *
913 flowprobe_show_table_fn (vlib_main_t * vm,
914                          unformat_input_t * input, vlib_cli_command_t * cm)
915 {
916   flowprobe_main_t *fm = &flowprobe_main;
917   int i;
918   flowprobe_entry_t *e;
919
920   vlib_cli_output (vm, "Dumping IPFIX table");
921
922   for (i = 0; i < vec_len (fm->pool_per_worker); i++)
923     {
924       /* *INDENT-OFF* */
925       pool_foreach (e, fm->pool_per_worker[i])
926         {
927           vlib_cli_output (vm, "%U",
928                            format_flowprobe_entry,
929                            e);
930         }
931       /* *INDENT-ON* */
932
933     }
934   return 0;
935 }
936
937 static clib_error_t *
938 flowprobe_show_stats_fn (vlib_main_t * vm,
939                          unformat_input_t * input, vlib_cli_command_t * cm)
940 {
941   flowprobe_main_t *fm = &flowprobe_main;
942   int i;
943
944   vlib_cli_output (vm, "IPFIX table statistics");
945   vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
946   vlib_cli_output (vm, "Flow pool size per thread: %d\n",
947                    0x1 << FLOWPROBE_LOG2_HASHSIZE);
948
949   for (i = 0; i < vec_len (fm->pool_per_worker); i++)
950     vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
951                      (100 * pool_elts (fm->pool_per_worker[i])) /
952                      (0x1 << FLOWPROBE_LOG2_HASHSIZE));
953   return 0;
954 }
955
956 static clib_error_t *
957 flowprobe_interface_add_del_feature_command_fn (vlib_main_t *vm,
958                                                 unformat_input_t *input,
959                                                 vlib_cli_command_t *cmd)
960 {
961   flowprobe_main_t *fm = &flowprobe_main;
962   u32 sw_if_index = ~0;
963   int is_add = 1;
964   u8 which = FLOW_VARIANT_IP4;
965   flowprobe_direction_t direction = FLOW_DIRECTION_TX;
966   int rv;
967
968   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
969     {
970       if (unformat (input, "disable"))
971         is_add = 0;
972       else if (unformat (input, "%U", unformat_vnet_sw_interface,
973                          fm->vnet_main, &sw_if_index));
974       else if (unformat (input, "ip4"))
975         which = FLOW_VARIANT_IP4;
976       else if (unformat (input, "ip6"))
977         which = FLOW_VARIANT_IP6;
978       else if (unformat (input, "l2"))
979         which = FLOW_VARIANT_L2;
980       else if (unformat (input, "rx"))
981         direction = FLOW_DIRECTION_RX;
982       else if (unformat (input, "tx"))
983         direction = FLOW_DIRECTION_TX;
984       else if (unformat (input, "both"))
985         direction = FLOW_DIRECTION_BOTH;
986       else
987         break;
988     }
989
990   if (fm->record == 0)
991     return clib_error_return (0,
992                               "Please specify flowprobe params record first...");
993
994   if (sw_if_index == ~0)
995     return clib_error_return (0, "Please specify an interface...");
996
997   rv = validate_feature_on_interface (fm, sw_if_index, which);
998   if (rv == 1)
999     {
1000       if (is_add)
1001         return clib_error_return (0,
1002                                   "Datapath is already enabled for given interface...");
1003     }
1004   else if (rv == 0)
1005     return clib_error_return (0,
1006                               "Interface has enable different datapath ...");
1007   else if (rv == -1)
1008     {
1009       if (!is_add)
1010         {
1011           return clib_error_return (0, "Interface has no datapath enabled");
1012         }
1013     }
1014
1015   rv = flowprobe_interface_add_del_feature (fm, sw_if_index, which, direction,
1016                                             is_add);
1017   switch (rv)
1018     {
1019     case 0:
1020       break;
1021
1022     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
1023       return clib_error_return
1024         (0, "Invalid interface, only works on physical ports");
1025       break;
1026
1027     case VNET_API_ERROR_UNIMPLEMENTED:
1028       return clib_error_return (0, "ip6 not supported");
1029       break;
1030
1031     default:
1032       return clib_error_return (0, "flowprobe_enable_disable returned %d",
1033                                 rv);
1034     }
1035   return 0;
1036 }
1037
1038 static clib_error_t *
1039 flowprobe_show_feature_command_fn (vlib_main_t * vm,
1040                                    unformat_input_t * input,
1041                                    vlib_cli_command_t * cmd)
1042 {
1043   flowprobe_main_t *fm = &flowprobe_main;
1044   u8 *which;
1045   u32 sw_if_index;
1046
1047   vec_foreach (which, fm->flow_per_interface)
1048   {
1049     if (*which == (u8) ~ 0)
1050       continue;
1051
1052     sw_if_index = which - fm->flow_per_interface;
1053     vlib_cli_output (vm, " %U %U %U", format_vnet_sw_if_index_name,
1054                      vnet_get_main (), sw_if_index, format_flowprobe_feature,
1055                      which, format_flowprobe_direction,
1056                      &fm->direction_per_interface[sw_if_index]);
1057   }
1058   return 0;
1059 }
1060
1061 static clib_error_t *
1062 flowprobe_params_command_fn (vlib_main_t * vm,
1063                              unformat_input_t * input,
1064                              vlib_cli_command_t * cmd)
1065 {
1066   flowprobe_main_t *fm = &flowprobe_main;
1067   bool record_l2 = false, record_l3 = false, record_l4 = false;
1068   u32 active_timer = ~0;
1069   u32 passive_timer = ~0;
1070
1071   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1072     {
1073       if (unformat (input, "active %d", &active_timer))
1074         ;
1075       else if (unformat (input, "passive %d", &passive_timer))
1076         ;
1077       else if (unformat (input, "record"))
1078         while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1079           {
1080             if (unformat (input, "l2"))
1081               record_l2 = true;
1082             else if (unformat (input, "l3"))
1083               record_l3 = true;
1084             else if (unformat (input, "l4"))
1085               record_l4 = true;
1086             else
1087               break;
1088           }
1089       else
1090         break;
1091     }
1092
1093   if (passive_timer > 0 && active_timer > passive_timer)
1094     return clib_error_return (0,
1095                               "Passive timer has to be greater than active one...");
1096
1097   if (flowprobe_params (fm, record_l2, record_l3, record_l4,
1098                         active_timer, passive_timer))
1099     return clib_error_return (0,
1100                               "Couldn't change flowperpacket params when feature is enabled on some interface ...");
1101   return 0;
1102 }
1103
1104 static clib_error_t *
1105 flowprobe_show_params_command_fn (vlib_main_t * vm,
1106                                   unformat_input_t * input,
1107                                   vlib_cli_command_t * cmd)
1108 {
1109   flowprobe_main_t *fm = &flowprobe_main;
1110   flowprobe_record_t flags = fm->record;
1111   u32 active_timer = fm->active_timer;
1112   u32 passive_timer = fm->passive_timer;
1113
1114   vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
1115                    passive_timer);
1116   return 0;
1117 }
1118
1119 /*?
1120  * '<em>flowprobe feature add-del</em>' commands to enable/disable
1121  * per-packet IPFIX flow record generation on an interface
1122  *
1123  * @cliexpar
1124  * @parblock
1125  * To enable per-packet IPFIX flow-record generation on an interface:
1126  * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
1127  *
1128  * To disable per-packet IPFIX flow-record generation on an interface:
1129  * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
1130  * @cliexend
1131  * @endparblock
1132 ?*/
1133 /* *INDENT-OFF* */
1134 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
1135   .path = "flowprobe feature add-del",
1136   .short_help = "flowprobe feature add-del <interface-name> [(l2|ip4|ip6)] "
1137                 "[(rx|tx|both)] [disable]",
1138   .function = flowprobe_interface_add_del_feature_command_fn,
1139 };
1140 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
1141     .path = "flowprobe params",
1142     .short_help =
1143     "flowprobe params record <[l2] [l3] [l4]> [active <timer> passive <timer>]",
1144     .function = flowprobe_params_command_fn,
1145 };
1146
1147 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
1148     .path = "show flowprobe feature",
1149     .short_help =
1150     "show flowprobe feature",
1151     .function = flowprobe_show_feature_command_fn,
1152 };
1153 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
1154     .path = "show flowprobe params",
1155     .short_help =
1156     "show flowprobe params",
1157     .function = flowprobe_show_params_command_fn,
1158 };
1159 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
1160     .path = "show flowprobe table",
1161     .short_help = "show flowprobe table",
1162     .function = flowprobe_show_table_fn,
1163 };
1164 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
1165     .path = "show flowprobe statistics",
1166     .short_help = "show flowprobe statistics",
1167     .function = flowprobe_show_stats_fn,
1168 };
1169 /* *INDENT-ON* */
1170
1171 /*
1172  * Main-core process, sending an interrupt to the per worker input
1173  * process that spins the per worker timer wheel.
1174  */
1175 static uword
1176 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1177 {
1178   uword *event_data = 0;
1179   vlib_main_t **worker_vms = 0, *worker_vm;
1180   flowprobe_main_t *fm = &flowprobe_main;
1181
1182   /* Wait for Godot... */
1183   vlib_process_wait_for_event_or_clock (vm, 1e9);
1184   uword event_type = vlib_process_get_events (vm, &event_data);
1185   if (event_type != 1)
1186     clib_warning ("bogus kickoff event received, %d", event_type);
1187   vec_reset_length (event_data);
1188
1189   int i;
1190   if (vlib_get_n_threads () == 0)
1191     vec_add1 (worker_vms, vm);
1192   else
1193     {
1194       for (i = 0; i < vlib_get_n_threads (); i++)
1195         {
1196           worker_vm = vlib_get_main_by_index (i);
1197           if (worker_vm)
1198             vec_add1 (worker_vms, worker_vm);
1199         }
1200     }
1201   f64 sleep_duration = 0.1;
1202
1203   while (1)
1204     {
1205       /* Send an interrupt to each timer input node */
1206       sleep_duration = 0.1;
1207       for (i = 0; i < vec_len (worker_vms); i++)
1208         {
1209           worker_vm = worker_vms[i];
1210           if (worker_vm)
1211             {
1212               vlib_node_set_interrupt_pending (worker_vm,
1213                                                flowprobe_walker_node.index);
1214               sleep_duration =
1215                 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1216             }
1217         }
1218       vlib_process_suspend (vm, sleep_duration);
1219     }
1220   return 0;                     /* or not */
1221 }
1222
1223 /* *INDENT-OFF* */
1224 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1225   .function = timer_process,
1226   .name = "flowprobe-timer-process",
1227   .type = VLIB_NODE_TYPE_PROCESS,
1228 };
1229 /* *INDENT-ON* */
1230
1231 #include <flowprobe/flowprobe.api.c>
1232
1233 /**
1234  * @brief Set up the API message handling tables
1235  * @param vm vlib_main_t * vlib main data structure pointer
1236  * @returns 0 to indicate all is well, or a clib_error_t
1237  */
1238 static clib_error_t *
1239 flowprobe_init (vlib_main_t * vm)
1240 {
1241   flowprobe_main_t *fm = &flowprobe_main;
1242   vlib_thread_main_t *tm = &vlib_thread_main;
1243   clib_error_t *error = 0;
1244   u32 num_threads;
1245   int i;
1246
1247   fm->vnet_main = vnet_get_main ();
1248
1249   /* Ask for a correctly-sized block of API message decode slots */
1250   fm->msg_id_base = setup_message_id_table ();
1251
1252   /* Set up time reference pair */
1253   fm->vlib_time_0 = vlib_time_now (vm);
1254   fm->nanosecond_time_0 = unix_time_now_nsec ();
1255
1256   clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1257   clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1258   clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1259
1260   /* Decide how many worker threads we have */
1261   num_threads = 1 /* main thread */  + tm->n_threads;
1262
1263   /* Allocate per worker thread vectors per flavour */
1264   for (i = 0; i < FLOW_N_VARIANTS; i++)
1265     {
1266       vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1267       vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1268       vec_validate (fm->context[i].next_record_offset_per_worker,
1269                     num_threads - 1);
1270     }
1271
1272   fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1273   fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1274
1275   return error;
1276 }
1277
1278 VLIB_INIT_FUNCTION (flowprobe_init);
1279
1280 /*
1281  * fd.io coding-style-patch-verification: ON
1282  *
1283  * Local Variables:
1284  * eval: (c-set-style "gnu")
1285  * End:
1286  */