ipfix-export: refactor params to the callback fns
[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 (flow_perpacket_ip4, static) =
50 {
51   .arc_name = "ip4-output",
52   .node_name = "flowprobe-ip4",
53   .runs_before = VNET_FEATURES ("interface-output"),
54 };
55
56 VNET_FEATURE_INIT (flow_perpacket_ip6, static) =
57 {
58   .arc_name = "ip6-output",
59   .node_name = "flowprobe-ip6",
60   .runs_before = VNET_FEATURES ("interface-output"),
61 };
62
63 VNET_FEATURE_INIT (flow_perpacket_l2, static) = {
64   .arc_name = "interface-output",
65   .node_name = "flowprobe-l2",
66   .runs_before = VNET_FEATURES ("interface-output-arc-end"),
67 };
68 /* *INDENT-ON* */
69
70 /* Macro to finish up custom dump fns */
71 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
72 #define FINISH                                  \
73     vec_add1 (s, 0);                            \
74     vl_print (handle, (char *)s);               \
75     vec_free (s);                               \
76     return handle;
77
78 static inline ipfix_field_specifier_t *
79 flowprobe_template_ip4_fields (ipfix_field_specifier_t * f)
80 {
81 #define flowprobe_template_ip4_field_count() 4
82   /* sourceIpv4Address, TLV type 8, u32 */
83   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
84                                       sourceIPv4Address, 4);
85   f++;
86   /* destinationIPv4Address, TLV type 12, u32 */
87   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
88                                       destinationIPv4Address, 4);
89   f++;
90   /* protocolIdentifier, TLV type 4, u8 */
91   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
92                                       protocolIdentifier, 1);
93   f++;
94   /* octetDeltaCount, TLV type 1, u64 */
95   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
96                                       octetDeltaCount, 8);
97   f++;
98   return f;
99 }
100
101 static inline ipfix_field_specifier_t *
102 flowprobe_template_ip6_fields (ipfix_field_specifier_t * f)
103 {
104 #define flowprobe_template_ip6_field_count() 4
105   /* sourceIpv6Address, TLV type 27, 16 octets */
106   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
107                                       sourceIPv6Address, 16);
108   f++;
109   /* destinationIPv6Address, TLV type 28, 16 octets */
110   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
111                                       destinationIPv6Address, 16);
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_l2_fields (ipfix_field_specifier_t * f)
126 {
127 #define flowprobe_template_l2_field_count() 3
128   /* sourceMacAddress, TLV type 56, u8[6] we hope */
129   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
130                                       sourceMacAddress, 6);
131   f++;
132   /* destinationMacAddress, TLV type 80, u8[6] we hope */
133   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
134                                       destinationMacAddress, 6);
135   f++;
136   /* ethernetType, TLV type 256, u16 */
137   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
138                                       ethernetType, 2);
139   f++;
140   return f;
141 }
142
143 static inline ipfix_field_specifier_t *
144 flowprobe_template_common_fields (ipfix_field_specifier_t * f)
145 {
146 #define flowprobe_template_common_field_count() 5
147   /* ingressInterface, TLV type 10, u32 */
148   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
149                                       ingressInterface, 4);
150   f++;
151
152   /* egressInterface, TLV type 14, u32 */
153   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
154                                       egressInterface, 4);
155   f++;
156
157   /* packetDeltaCount, TLV type 2, u64 */
158   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
159                                       packetDeltaCount, 8);
160   f++;
161
162   /* flowStartNanoseconds, TLV type 156, u64 */
163   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
164                                       flowStartNanoseconds, 8);
165   f++;
166
167   /* flowEndNanoseconds, TLV type 157, u64 */
168   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
169                                       flowEndNanoseconds, 8);
170   f++;
171
172   return f;
173 }
174
175 static inline ipfix_field_specifier_t *
176 flowprobe_template_l4_fields (ipfix_field_specifier_t * f)
177 {
178 #define flowprobe_template_l4_field_count() 3
179   /* sourceTransportPort, TLV type 7, u16 */
180   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
181                                       sourceTransportPort, 2);
182   f++;
183   /* destinationTransportPort, TLV type 11, u16 */
184   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
185                                       destinationTransportPort, 2);
186   f++;
187   /* tcpControlBits, TLV type 6, u16 */
188   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
189                                       tcpControlBits, 2);
190   f++;
191
192   return f;
193 }
194
195 /**
196  * @brief Create an IPFIX template packet rewrite string
197  * @param frm flow_report_main_t *
198  * @param fr flow_report_t *
199  * @param collector_address ip4_address_t * the IPFIX collector address
200  * @param src_address ip4_address_t * the source address we should use
201  * @param collector_port u16 the collector port we should use, host byte order
202  * @returns u8 * vector containing the indicated IPFIX template packet
203  */
204 static inline u8 *
205 flowprobe_template_rewrite_inline (ipfix_exporter_t *exp, flow_report_t *fr,
206                                    u16 collector_port,
207                                    flowprobe_variant_t which)
208 {
209   ip4_header_t *ip;
210   udp_header_t *udp;
211   ipfix_message_header_t *h;
212   ipfix_set_header_t *s;
213   ipfix_template_header_t *t;
214   ipfix_field_specifier_t *f;
215   ipfix_field_specifier_t *first_field;
216   u8 *rewrite = 0;
217   ip4_ipfix_template_packet_t *tp;
218   u32 field_count = 0;
219   flow_report_stream_t *stream;
220   flowprobe_main_t *fm = &flowprobe_main;
221   flowprobe_record_t flags = fr->opaque.as_uword;
222   bool collect_ip4 = false, collect_ip6 = false;
223
224   stream = &exp->streams[fr->stream_index];
225
226   if (flags & FLOW_RECORD_L3)
227     {
228       collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
229       collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
230       if (which == FLOW_VARIANT_L2_IP4)
231         flags |= FLOW_RECORD_L2_IP4;
232       if (which == FLOW_VARIANT_L2_IP6)
233         flags |= FLOW_RECORD_L2_IP6;
234     }
235
236   field_count += flowprobe_template_common_field_count ();
237   if (flags & FLOW_RECORD_L2)
238     field_count += flowprobe_template_l2_field_count ();
239   if (collect_ip4)
240     field_count += flowprobe_template_ip4_field_count ();
241   if (collect_ip6)
242     field_count += flowprobe_template_ip6_field_count ();
243   if (flags & FLOW_RECORD_L4)
244     field_count += flowprobe_template_l4_field_count ();
245
246   /* allocate rewrite space */
247   vec_validate_aligned
248     (rewrite, sizeof (ip4_ipfix_template_packet_t)
249      + field_count * sizeof (ipfix_field_specifier_t) - 1,
250      CLIB_CACHE_LINE_BYTES);
251
252   tp = (ip4_ipfix_template_packet_t *) rewrite;
253   ip = (ip4_header_t *) & tp->ip4;
254   udp = (udp_header_t *) (ip + 1);
255   h = (ipfix_message_header_t *) (udp + 1);
256   s = (ipfix_set_header_t *) (h + 1);
257   t = (ipfix_template_header_t *) (s + 1);
258   first_field = f = (ipfix_field_specifier_t *) (t + 1);
259
260   ip->ip_version_and_header_length = 0x45;
261   ip->ttl = 254;
262   ip->protocol = IP_PROTOCOL_UDP;
263   ip->src_address.as_u32 = exp->src_address.as_u32;
264   ip->dst_address.as_u32 = exp->ipfix_collector.as_u32;
265   udp->src_port = clib_host_to_net_u16 (stream->src_port);
266   udp->dst_port = clib_host_to_net_u16 (collector_port);
267   udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
268
269   /* FIXUP: message header export_time */
270   /* FIXUP: message header sequence_number */
271   h->domain_id = clib_host_to_net_u32 (stream->domain_id);
272
273   /* Add TLVs to the template */
274   f = flowprobe_template_common_fields (f);
275
276   if (flags & FLOW_RECORD_L2)
277     f = flowprobe_template_l2_fields (f);
278   if (collect_ip4)
279     f = flowprobe_template_ip4_fields (f);
280   if (collect_ip6)
281     f = flowprobe_template_ip6_fields (f);
282   if (flags & FLOW_RECORD_L4)
283     f = flowprobe_template_l4_fields (f);
284
285   /* Back to the template packet... */
286   ip = (ip4_header_t *) & tp->ip4;
287   udp = (udp_header_t *) (ip + 1);
288
289   ASSERT (f - first_field);
290   /* Field count in this template */
291   t->id_count = ipfix_id_count (fr->template_id, f - first_field);
292
293   fm->template_size[flags] = (u8 *) f - (u8 *) s;
294
295   /* set length in octets */
296   s->set_id_length =
297     ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
298
299   /* message length in octets */
300   h->version_length = version_length ((u8 *) f - (u8 *) h);
301
302   ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
303   ip->checksum = ip4_header_checksum (ip);
304
305   return rewrite;
306 }
307
308 static u8 *
309 flowprobe_template_rewrite_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
310                                 u16 collector_port,
311                                 ipfix_report_element_t *elts, u32 n_elts,
312                                 u32 *stream_index)
313 {
314   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
315                                             FLOW_VARIANT_IP6);
316 }
317
318 static u8 *
319 flowprobe_template_rewrite_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
320                                 u16 collector_port,
321                                 ipfix_report_element_t *elts, u32 n_elts,
322                                 u32 *stream_index)
323 {
324   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
325                                             FLOW_VARIANT_IP4);
326 }
327
328 static u8 *
329 flowprobe_template_rewrite_l2 (ipfix_exporter_t *exp, flow_report_t *fr,
330                                u16 collector_port,
331                                ipfix_report_element_t *elts, u32 n_elts,
332                                u32 *stream_index)
333 {
334   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
335                                             FLOW_VARIANT_L2);
336 }
337
338 static u8 *
339 flowprobe_template_rewrite_l2_ip4 (ipfix_exporter_t *exp, flow_report_t *fr,
340                                    u16 collector_port,
341                                    ipfix_report_element_t *elts, u32 n_elts,
342                                    u32 *stream_index)
343 {
344   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
345                                             FLOW_VARIANT_L2_IP4);
346 }
347
348 static u8 *
349 flowprobe_template_rewrite_l2_ip6 (ipfix_exporter_t *exp, flow_report_t *fr,
350                                    u16 collector_port,
351                                    ipfix_report_element_t *elts, u32 n_elts,
352                                    u32 *stream_index)
353 {
354   return flowprobe_template_rewrite_inline (exp, fr, collector_port,
355                                             FLOW_VARIANT_L2_IP6);
356 }
357
358 /**
359  * @brief Flush accumulated data
360  * @param frm flow_report_main_t *
361  * @param fr flow_report_t *
362  * @param f vlib_frame_t *
363  *
364  * <em>Notes:</em>
365  * This function must simply return the incoming frame, or no template packets
366  * will be sent.
367  */
368 vlib_frame_t *
369 flowprobe_data_callback_ip4 (flow_report_main_t *frm, ipfix_exporter_t *exp,
370                              flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
371                              u32 node_index)
372 {
373   flowprobe_flush_callback_ip4 ();
374   return f;
375 }
376
377 vlib_frame_t *
378 flowprobe_data_callback_ip6 (flow_report_main_t *frm, ipfix_exporter_t *exp,
379                              flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
380                              u32 node_index)
381 {
382   flowprobe_flush_callback_ip6 ();
383   return f;
384 }
385
386 vlib_frame_t *
387 flowprobe_data_callback_l2 (flow_report_main_t *frm, ipfix_exporter_t *exp,
388                             flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
389                             u32 node_index)
390 {
391   flowprobe_flush_callback_l2 ();
392   return f;
393 }
394
395 static int
396 flowprobe_template_add_del (u32 domain_id, u16 src_port,
397                             flowprobe_record_t flags,
398                             vnet_flow_data_callback_t * flow_data_callback,
399                             vnet_flow_rewrite_callback_t * rewrite_callback,
400                             bool is_add, u16 * template_id)
401 {
402   ipfix_exporter_t *exp = &flow_report_main.exporters[0];
403   vnet_flow_report_add_del_args_t a = {
404     .rewrite_callback = rewrite_callback,
405     .flow_data_callback = flow_data_callback,
406     .is_add = is_add,
407     .domain_id = domain_id,
408     .src_port = src_port,
409     .opaque.as_uword = flags,
410   };
411   return vnet_flow_report_add_del (exp, &a, template_id);
412 }
413
414 static void
415 flowprobe_expired_timer_callback (u32 * expired_timers)
416 {
417   vlib_main_t *vm = vlib_get_main ();
418   flowprobe_main_t *fm = &flowprobe_main;
419   u32 my_cpu_number = vm->thread_index;
420   int i;
421   u32 poolindex;
422
423   for (i = 0; i < vec_len (expired_timers); i++)
424     {
425       poolindex = expired_timers[i] & 0x7FFFFFFF;
426       vec_add1 (fm->expired_passive_per_worker[my_cpu_number], poolindex);
427     }
428 }
429
430 static clib_error_t *
431 flowprobe_create_state_tables (u32 active_timer)
432 {
433   flowprobe_main_t *fm = &flowprobe_main;
434   vlib_thread_main_t *tm = &vlib_thread_main;
435   vlib_main_t *vm = vlib_get_main ();
436   clib_error_t *error = 0;
437   u32 num_threads;
438   int i;
439
440   /* Decide how many worker threads we have */
441   num_threads = 1 /* main thread */  + tm->n_threads;
442
443   /* Hash table per worker */
444   fm->ht_log2len = FLOWPROBE_LOG2_HASHSIZE;
445
446   /* Init per worker flow state and timer wheels */
447   if (active_timer)
448     {
449       vec_validate (fm->timers_per_worker, num_threads - 1);
450       vec_validate (fm->expired_passive_per_worker, num_threads - 1);
451       vec_validate (fm->hash_per_worker, num_threads - 1);
452       vec_validate (fm->pool_per_worker, num_threads - 1);
453
454       for (i = 0; i < num_threads; i++)
455         {
456           int j;
457           pool_alloc (fm->pool_per_worker[i], 1 << fm->ht_log2len);
458           vec_resize (fm->hash_per_worker[i], 1 << fm->ht_log2len);
459           for (j = 0; j < (1 << fm->ht_log2len); j++)
460             fm->hash_per_worker[i][j] = ~0;
461           fm->timers_per_worker[i] =
462             clib_mem_alloc (sizeof (TWT (tw_timer_wheel)));
463           tw_timer_wheel_init_2t_1w_2048sl (fm->timers_per_worker[i],
464                                             flowprobe_expired_timer_callback,
465                                             1.0, 1024);
466         }
467       fm->disabled = true;
468     }
469   else
470     {
471       f64 now = vlib_time_now (vm);
472       vec_validate (fm->stateless_entry, num_threads - 1);
473       for (i = 0; i < num_threads; i++)
474         fm->stateless_entry[i].last_exported = now;
475       fm->disabled = false;
476     }
477   fm->initialized = true;
478   return error;
479 }
480
481 static int
482 validate_feature_on_interface (flowprobe_main_t * fm, u32 sw_if_index,
483                                u8 which)
484 {
485   vec_validate_init_empty (fm->flow_per_interface, sw_if_index, ~0);
486
487   if (fm->flow_per_interface[sw_if_index] == (u8) ~ 0)
488     return -1;
489   else if (fm->flow_per_interface[sw_if_index] != which)
490     return 0;
491   else
492     return 1;
493 }
494
495 /**
496  * @brief configure / deconfigure the IPFIX flow-per-packet
497  * @param fm flowprobe_main_t * fm
498  * @param sw_if_index u32 the desired interface
499  * @param is_add int 1 to enable the feature, 0 to disable it
500  * @returns 0 if successful, non-zero otherwise
501  */
502
503 static int
504 flowprobe_tx_interface_add_del_feature (flowprobe_main_t * fm,
505                                         u32 sw_if_index, u8 which, int is_add)
506 {
507   vlib_main_t *vm = vlib_get_main ();
508   int rv = 0;
509   u16 template_id = 0;
510   flowprobe_record_t flags = fm->record;
511
512   fm->flow_per_interface[sw_if_index] = (is_add) ? which : (u8) ~ 0;
513   fm->template_per_flow[which] += (is_add) ? 1 : -1;
514   if (is_add && fm->template_per_flow[which] > 1)
515     template_id = fm->template_reports[flags];
516
517   if ((is_add && fm->template_per_flow[which] == 1) ||
518       (!is_add && fm->template_per_flow[which] == 0))
519     {
520       if (which == FLOW_VARIANT_L2)
521         {
522           if (fm->record & FLOW_RECORD_L2)
523             {
524               rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
525                                                flowprobe_data_callback_l2,
526                                                flowprobe_template_rewrite_l2,
527                                                is_add, &template_id);
528             }
529           if (fm->record & FLOW_RECORD_L3 || fm->record & FLOW_RECORD_L4)
530             {
531               rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
532                                                flowprobe_data_callback_l2,
533                                                flowprobe_template_rewrite_l2_ip4,
534                                                is_add, &template_id);
535               fm->template_reports[flags | FLOW_RECORD_L2_IP4] =
536                 (is_add) ? template_id : 0;
537               rv =
538                 flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
539                                             flowprobe_data_callback_l2,
540                                             flowprobe_template_rewrite_l2_ip6,
541                                             is_add, &template_id);
542               fm->template_reports[flags | FLOW_RECORD_L2_IP6] =
543                 (is_add) ? template_id : 0;
544
545               /* Special case L2 */
546               fm->context[FLOW_VARIANT_L2_IP4].flags =
547                 flags | FLOW_RECORD_L2_IP4;
548               fm->context[FLOW_VARIANT_L2_IP6].flags =
549                 flags | FLOW_RECORD_L2_IP6;
550
551               fm->template_reports[flags] = template_id;
552             }
553         }
554       else if (which == FLOW_VARIANT_IP4)
555         rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
556                                          flowprobe_data_callback_ip4,
557                                          flowprobe_template_rewrite_ip4,
558                                          is_add, &template_id);
559       else if (which == FLOW_VARIANT_IP6)
560         rv = flowprobe_template_add_del (1, UDP_DST_PORT_ipfix, flags,
561                                          flowprobe_data_callback_ip6,
562                                          flowprobe_template_rewrite_ip6,
563                                          is_add, &template_id);
564     }
565   if (rv && rv != VNET_API_ERROR_VALUE_EXIST)
566     {
567       clib_warning ("vnet_flow_report_add_del returned %d", rv);
568       return -1;
569     }
570
571   if (which != (u8) ~ 0)
572     {
573       fm->context[which].flags = fm->record;
574       fm->template_reports[flags] = (is_add) ? template_id : 0;
575     }
576
577   if (which == FLOW_VARIANT_IP4)
578     vnet_feature_enable_disable ("ip4-output", "flowprobe-ip4",
579                                  sw_if_index, is_add, 0, 0);
580   else if (which == FLOW_VARIANT_IP6)
581     vnet_feature_enable_disable ("ip6-output", "flowprobe-ip6",
582                                  sw_if_index, is_add, 0, 0);
583   else if (which == FLOW_VARIANT_L2)
584     vnet_feature_enable_disable ("interface-output", "flowprobe-l2",
585                                  sw_if_index, is_add, 0, 0);
586
587   /* Stateful flow collection */
588   if (is_add && !fm->initialized)
589     {
590       flowprobe_create_state_tables (fm->active_timer);
591       if (fm->active_timer)
592         vlib_process_signal_event (vm, flowprobe_timer_node.index, 1, 0);
593     }
594
595   return 0;
596 }
597
598 /**
599  * @brief API message handler
600  * @param mp vl_api_flowprobe_tx_interface_add_del_t * mp the api message
601  */
602 void vl_api_flowprobe_tx_interface_add_del_t_handler
603   (vl_api_flowprobe_tx_interface_add_del_t * mp)
604 {
605   flowprobe_main_t *fm = &flowprobe_main;
606   vl_api_flowprobe_tx_interface_add_del_reply_t *rmp;
607   u32 sw_if_index = ntohl (mp->sw_if_index);
608   int rv = 0;
609
610   VALIDATE_SW_IF_INDEX (mp);
611
612   if (fm->record == 0)
613     {
614       clib_warning ("Please specify flowprobe params record first...");
615       rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
616       goto out;
617     }
618
619   rv = validate_feature_on_interface (fm, sw_if_index, mp->which);
620   if ((rv == 1 && mp->is_add == 1) || rv == 0)
621     {
622       rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
623       goto out;
624     }
625
626   rv = flowprobe_tx_interface_add_del_feature
627     (fm, sw_if_index, mp->which, mp->is_add);
628
629 out:
630   BAD_SW_IF_INDEX_LABEL;
631
632   REPLY_MACRO (VL_API_FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY);
633 }
634
635 #define vec_neg_search(v,E)         \
636 ({              \
637   word _v(i) = 0;         \
638   while (_v(i) < vec_len(v) && v[_v(i)] == E)        \
639   {             \
640     _v(i)++;            \
641   }             \
642   if (_v(i) == vec_len(v))        \
643     _v(i) = ~0;                 \
644   _v(i);            \
645 })
646
647 static int
648 flowprobe_params (flowprobe_main_t * fm, u8 record_l2,
649                   u8 record_l3, u8 record_l4,
650                   u32 active_timer, u32 passive_timer)
651 {
652   flowprobe_record_t flags = 0;
653
654   if (vec_neg_search (fm->flow_per_interface, (u8) ~ 0) != ~0)
655     return ~0;
656
657   if (record_l2)
658     flags |= FLOW_RECORD_L2;
659   if (record_l3)
660     flags |= FLOW_RECORD_L3;
661   if (record_l4)
662     flags |= FLOW_RECORD_L4;
663
664   fm->record = flags;
665
666   /*
667    * Timers: ~0 is default, 0 is off
668    */
669   fm->active_timer =
670     (active_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_ACTIVE : active_timer);
671   fm->passive_timer =
672     (passive_timer == (u32) ~ 0 ? FLOWPROBE_TIMER_PASSIVE : passive_timer);
673
674   return 0;
675 }
676
677 void
678 vl_api_flowprobe_params_t_handler (vl_api_flowprobe_params_t * mp)
679 {
680   flowprobe_main_t *fm = &flowprobe_main;
681   vl_api_flowprobe_params_reply_t *rmp;
682   int rv = 0;
683
684   rv = flowprobe_params
685     (fm,
686      mp->record_flags & FLOWPROBE_RECORD_FLAG_L2,
687      mp->record_flags & FLOWPROBE_RECORD_FLAG_L3,
688      mp->record_flags & FLOWPROBE_RECORD_FLAG_L4,
689      clib_net_to_host_u32 (mp->active_timer),
690      clib_net_to_host_u32 (mp->passive_timer));
691
692   REPLY_MACRO (VL_API_FLOWPROBE_PARAMS_REPLY);
693 }
694
695 /* *INDENT-OFF* */
696 VLIB_PLUGIN_REGISTER () = {
697     .version = VPP_BUILD_VER,
698     .description = "Flow per Packet",
699 };
700 /* *INDENT-ON* */
701
702 u8 *
703 format_flowprobe_entry (u8 * s, va_list * args)
704 {
705   flowprobe_entry_t *e = va_arg (*args, flowprobe_entry_t *);
706   s = format (s, " %d/%d", e->key.rx_sw_if_index, e->key.tx_sw_if_index);
707
708   s = format (s, " %U %U", format_ethernet_address, &e->key.src_mac,
709               format_ethernet_address, &e->key.dst_mac);
710   s = format (s, " %U -> %U",
711               format_ip46_address, &e->key.src_address, IP46_TYPE_ANY,
712               format_ip46_address, &e->key.dst_address, IP46_TYPE_ANY);
713   s = format (s, " %d", e->key.protocol);
714   s = format (s, " %d %d\n", clib_net_to_host_u16 (e->key.src_port),
715               clib_net_to_host_u16 (e->key.dst_port));
716
717   return s;
718 }
719
720 u8 *
721 format_flowprobe_feature (u8 * s, va_list * args)
722 {
723   u8 *which = va_arg (*args, u8 *);
724   if (*which == FLOW_VARIANT_IP4)
725     s = format (s, "ip4");
726   else if (*which == FLOW_VARIANT_IP6)
727     s = format (s, "ip6");
728   else if (*which == FLOW_VARIANT_L2)
729     s = format (s, "l2");
730
731   return s;
732 }
733
734 u8 *
735 format_flowprobe_params (u8 * s, va_list * args)
736 {
737   flowprobe_record_t flags = va_arg (*args, flowprobe_record_t);
738   u32 active_timer = va_arg (*args, u32);
739   u32 passive_timer = va_arg (*args, u32);
740
741   if (flags & FLOW_RECORD_L2)
742     s = format (s, " l2");
743   if (flags & FLOW_RECORD_L3)
744     s = format (s, " l3");
745   if (flags & FLOW_RECORD_L4)
746     s = format (s, " l4");
747
748   if (active_timer != (u32) ~ 0)
749     s = format (s, " active: %d", active_timer);
750
751   if (passive_timer != (u32) ~ 0)
752     s = format (s, " passive: %d", passive_timer);
753
754   return s;
755 }
756
757 static clib_error_t *
758 flowprobe_show_table_fn (vlib_main_t * vm,
759                          unformat_input_t * input, vlib_cli_command_t * cm)
760 {
761   flowprobe_main_t *fm = &flowprobe_main;
762   int i;
763   flowprobe_entry_t *e;
764
765   vlib_cli_output (vm, "Dumping IPFIX table");
766
767   for (i = 0; i < vec_len (fm->pool_per_worker); i++)
768     {
769       /* *INDENT-OFF* */
770       pool_foreach (e, fm->pool_per_worker[i])
771         {
772           vlib_cli_output (vm, "%U",
773                            format_flowprobe_entry,
774                            e);
775         }
776       /* *INDENT-ON* */
777
778     }
779   return 0;
780 }
781
782 static clib_error_t *
783 flowprobe_show_stats_fn (vlib_main_t * vm,
784                          unformat_input_t * input, vlib_cli_command_t * cm)
785 {
786   flowprobe_main_t *fm = &flowprobe_main;
787   int i;
788
789   vlib_cli_output (vm, "IPFIX table statistics");
790   vlib_cli_output (vm, "Flow entry size: %d\n", sizeof (flowprobe_entry_t));
791   vlib_cli_output (vm, "Flow pool size per thread: %d\n",
792                    0x1 << FLOWPROBE_LOG2_HASHSIZE);
793
794   for (i = 0; i < vec_len (fm->pool_per_worker); i++)
795     vlib_cli_output (vm, "Pool utilisation thread %d is %d%%\n", i,
796                      (100 * pool_elts (fm->pool_per_worker[i])) /
797                      (0x1 << FLOWPROBE_LOG2_HASHSIZE));
798   return 0;
799 }
800
801 static clib_error_t *
802 flowprobe_tx_interface_add_del_feature_command_fn (vlib_main_t * vm,
803                                                    unformat_input_t * input,
804                                                    vlib_cli_command_t * cmd)
805 {
806   flowprobe_main_t *fm = &flowprobe_main;
807   u32 sw_if_index = ~0;
808   int is_add = 1;
809   u8 which = FLOW_VARIANT_IP4;
810   int rv;
811
812   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
813     {
814       if (unformat (input, "disable"))
815         is_add = 0;
816       else if (unformat (input, "%U", unformat_vnet_sw_interface,
817                          fm->vnet_main, &sw_if_index));
818       else if (unformat (input, "ip4"))
819         which = FLOW_VARIANT_IP4;
820       else if (unformat (input, "ip6"))
821         which = FLOW_VARIANT_IP6;
822       else if (unformat (input, "l2"))
823         which = FLOW_VARIANT_L2;
824       else
825         break;
826     }
827
828   if (fm->record == 0)
829     return clib_error_return (0,
830                               "Please specify flowprobe params record first...");
831
832   if (sw_if_index == ~0)
833     return clib_error_return (0, "Please specify an interface...");
834
835   rv = validate_feature_on_interface (fm, sw_if_index, which);
836   if (rv == 1)
837     {
838       if (is_add)
839         return clib_error_return (0,
840                                   "Datapath is already enabled for given interface...");
841     }
842   else if (rv == 0)
843     return clib_error_return (0,
844                               "Interface has enable different datapath ...");
845
846   rv =
847     flowprobe_tx_interface_add_del_feature (fm, sw_if_index, which, is_add);
848   switch (rv)
849     {
850     case 0:
851       break;
852
853     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
854       return clib_error_return
855         (0, "Invalid interface, only works on physical ports");
856       break;
857
858     case VNET_API_ERROR_UNIMPLEMENTED:
859       return clib_error_return (0, "ip6 not supported");
860       break;
861
862     default:
863       return clib_error_return (0, "flowprobe_enable_disable returned %d",
864                                 rv);
865     }
866   return 0;
867 }
868
869 static clib_error_t *
870 flowprobe_show_feature_command_fn (vlib_main_t * vm,
871                                    unformat_input_t * input,
872                                    vlib_cli_command_t * cmd)
873 {
874   flowprobe_main_t *fm = &flowprobe_main;
875   u8 *which;
876   u32 sw_if_index;
877
878   vec_foreach (which, fm->flow_per_interface)
879   {
880     if (*which == (u8) ~ 0)
881       continue;
882
883     sw_if_index = which - fm->flow_per_interface;
884     vlib_cli_output (vm, " %U %U", format_vnet_sw_if_index_name,
885                      vnet_get_main (), sw_if_index, format_flowprobe_feature,
886                      which);
887   }
888   return 0;
889 }
890
891 static clib_error_t *
892 flowprobe_params_command_fn (vlib_main_t * vm,
893                              unformat_input_t * input,
894                              vlib_cli_command_t * cmd)
895 {
896   flowprobe_main_t *fm = &flowprobe_main;
897   bool record_l2 = false, record_l3 = false, record_l4 = false;
898   u32 active_timer = ~0;
899   u32 passive_timer = ~0;
900
901   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
902     {
903       if (unformat (input, "active %d", &active_timer))
904         ;
905       else if (unformat (input, "passive %d", &passive_timer))
906         ;
907       else if (unformat (input, "record"))
908         while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
909           {
910             if (unformat (input, "l2"))
911               record_l2 = true;
912             else if (unformat (input, "l3"))
913               record_l3 = true;
914             else if (unformat (input, "l4"))
915               record_l4 = true;
916             else
917               break;
918           }
919       else
920         break;
921     }
922
923   if (passive_timer > 0 && active_timer > passive_timer)
924     return clib_error_return (0,
925                               "Passive timer has to be greater than active one...");
926
927   if (flowprobe_params (fm, record_l2, record_l3, record_l4,
928                         active_timer, passive_timer))
929     return clib_error_return (0,
930                               "Couldn't change flowperpacket params when feature is enabled on some interface ...");
931   return 0;
932 }
933
934 static clib_error_t *
935 flowprobe_show_params_command_fn (vlib_main_t * vm,
936                                   unformat_input_t * input,
937                                   vlib_cli_command_t * cmd)
938 {
939   flowprobe_main_t *fm = &flowprobe_main;
940   flowprobe_record_t flags = fm->record;
941   u32 active_timer = fm->active_timer;
942   u32 passive_timer = fm->passive_timer;
943
944   vlib_cli_output (vm, "%U", format_flowprobe_params, flags, active_timer,
945                    passive_timer);
946   return 0;
947 }
948
949 /*?
950  * '<em>flowprobe feature add-del</em>' commands to enable/disable
951  * per-packet IPFIX flow record generation on an interface
952  *
953  * @cliexpar
954  * @parblock
955  * To enable per-packet IPFIX flow-record generation on an interface:
956  * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0}
957  *
958  * To disable per-packet IPFIX flow-record generation on an interface:
959  * @cliexcmd{flowprobe feature add-del GigabitEthernet2/0/0 disable}
960  * @cliexend
961  * @endparblock
962 ?*/
963 /* *INDENT-OFF* */
964 VLIB_CLI_COMMAND (flowprobe_enable_disable_command, static) = {
965     .path = "flowprobe feature add-del",
966     .short_help =
967     "flowprobe feature add-del <interface-name> <l2|ip4|ip6> disable",
968     .function = flowprobe_tx_interface_add_del_feature_command_fn,
969 };
970 VLIB_CLI_COMMAND (flowprobe_params_command, static) = {
971     .path = "flowprobe params",
972     .short_help =
973     "flowprobe params record <[l2] [l3] [l4]> [active <timer> passive <timer>]",
974     .function = flowprobe_params_command_fn,
975 };
976
977 VLIB_CLI_COMMAND (flowprobe_show_feature_command, static) = {
978     .path = "show flowprobe feature",
979     .short_help =
980     "show flowprobe feature",
981     .function = flowprobe_show_feature_command_fn,
982 };
983 VLIB_CLI_COMMAND (flowprobe_show_params_command, static) = {
984     .path = "show flowprobe params",
985     .short_help =
986     "show flowprobe params",
987     .function = flowprobe_show_params_command_fn,
988 };
989 VLIB_CLI_COMMAND (flowprobe_show_table_command, static) = {
990     .path = "show flowprobe table",
991     .short_help = "show flowprobe table",
992     .function = flowprobe_show_table_fn,
993 };
994 VLIB_CLI_COMMAND (flowprobe_show_stats_command, static) = {
995     .path = "show flowprobe statistics",
996     .short_help = "show flowprobe statistics",
997     .function = flowprobe_show_stats_fn,
998 };
999 /* *INDENT-ON* */
1000
1001 /*
1002  * Main-core process, sending an interrupt to the per worker input
1003  * process that spins the per worker timer wheel.
1004  */
1005 static uword
1006 timer_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1007 {
1008   uword *event_data = 0;
1009   vlib_main_t **worker_vms = 0, *worker_vm;
1010   flowprobe_main_t *fm = &flowprobe_main;
1011
1012   /* Wait for Godot... */
1013   vlib_process_wait_for_event_or_clock (vm, 1e9);
1014   uword event_type = vlib_process_get_events (vm, &event_data);
1015   if (event_type != 1)
1016     clib_warning ("bogus kickoff event received, %d", event_type);
1017   vec_reset_length (event_data);
1018
1019   int i;
1020   if (vlib_get_n_threads () == 0)
1021     vec_add1 (worker_vms, vm);
1022   else
1023     {
1024       for (i = 0; i < vlib_get_n_threads (); i++)
1025         {
1026           worker_vm = vlib_get_main_by_index (i);
1027           if (worker_vm)
1028             vec_add1 (worker_vms, worker_vm);
1029         }
1030     }
1031   f64 sleep_duration = 0.1;
1032
1033   while (1)
1034     {
1035       /* Send an interrupt to each timer input node */
1036       sleep_duration = 0.1;
1037       for (i = 0; i < vec_len (worker_vms); i++)
1038         {
1039           worker_vm = worker_vms[i];
1040           if (worker_vm)
1041             {
1042               vlib_node_set_interrupt_pending (worker_vm,
1043                                                flowprobe_walker_node.index);
1044               sleep_duration =
1045                 (fm->expired_passive_per_worker[i] > 0) ? 1e-4 : 0.1;
1046             }
1047         }
1048       vlib_process_suspend (vm, sleep_duration);
1049     }
1050   return 0;                     /* or not */
1051 }
1052
1053 /* *INDENT-OFF* */
1054 VLIB_REGISTER_NODE (flowprobe_timer_node,static) = {
1055   .function = timer_process,
1056   .name = "flowprobe-timer-process",
1057   .type = VLIB_NODE_TYPE_PROCESS,
1058 };
1059 /* *INDENT-ON* */
1060
1061 #include <flowprobe/flowprobe.api.c>
1062
1063 /**
1064  * @brief Set up the API message handling tables
1065  * @param vm vlib_main_t * vlib main data structure pointer
1066  * @returns 0 to indicate all is well, or a clib_error_t
1067  */
1068 static clib_error_t *
1069 flowprobe_init (vlib_main_t * vm)
1070 {
1071   flowprobe_main_t *fm = &flowprobe_main;
1072   vlib_thread_main_t *tm = &vlib_thread_main;
1073   clib_error_t *error = 0;
1074   u32 num_threads;
1075   int i;
1076
1077   fm->vnet_main = vnet_get_main ();
1078
1079   /* Ask for a correctly-sized block of API message decode slots */
1080   fm->msg_id_base = setup_message_id_table ();
1081
1082   /* Set up time reference pair */
1083   fm->vlib_time_0 = vlib_time_now (vm);
1084   fm->nanosecond_time_0 = unix_time_now_nsec ();
1085
1086   clib_memset (fm->template_reports, 0, sizeof (fm->template_reports));
1087   clib_memset (fm->template_size, 0, sizeof (fm->template_size));
1088   clib_memset (fm->template_per_flow, 0, sizeof (fm->template_per_flow));
1089
1090   /* Decide how many worker threads we have */
1091   num_threads = 1 /* main thread */  + tm->n_threads;
1092
1093   /* Allocate per worker thread vectors per flavour */
1094   for (i = 0; i < FLOW_N_VARIANTS; i++)
1095     {
1096       vec_validate (fm->context[i].buffers_per_worker, num_threads - 1);
1097       vec_validate (fm->context[i].frames_per_worker, num_threads - 1);
1098       vec_validate (fm->context[i].next_record_offset_per_worker,
1099                     num_threads - 1);
1100     }
1101
1102   fm->active_timer = FLOWPROBE_TIMER_ACTIVE;
1103   fm->passive_timer = FLOWPROBE_TIMER_PASSIVE;
1104
1105   return error;
1106 }
1107
1108 VLIB_INIT_FUNCTION (flowprobe_init);
1109
1110 /*
1111  * fd.io coding-style-patch-verification: ON
1112  *
1113  * Local Variables:
1114  * eval: (c-set-style "gnu")
1115  * End:
1116  */