80bfa9b584f50544dbe9a9a21306646bad8e79aa
[vpp.git] / src / plugins / flowprobe / node.c
1 /*
2  * node.c - ipfix probe graph node
3  *
4  * Copyright (c) 2017 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 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vppinfra/crc32.h>
21 #include <vppinfra/error.h>
22 #include <flowprobe/flowprobe.h>
23 #include <vnet/ip/ip6_packet.h>
24
25 static void flowprobe_export_entry (vlib_main_t * vm, flowprobe_entry_t * e);
26
27 /**
28  * @file flow record generator graph node
29  */
30
31 typedef struct
32 {
33   /** interface handle */
34   u32 rx_sw_if_index;
35   u32 tx_sw_if_index;
36   /** packet timestamp */
37   u64 timestamp;
38   /** size of the buffer */
39   u16 buffer_size;
40
41   /** L2 information */
42   u8 src_mac[6];
43   u8 dst_mac[6];
44   /** Ethertype */
45   u16 ethertype;
46
47   /** L3 information */
48   ip46_address_t src_address;
49   ip46_address_t dst_address;
50   u8 protocol;
51   u8 tos;
52
53   /** L4 information */
54   u16 src_port;
55   u16 dst_port;
56
57   flowprobe_variant_t which;
58 } flowprobe_trace_t;
59
60 static char *flowprobe_variant_strings[] = {
61   [FLOW_VARIANT_IP4] = "IP4",
62   [FLOW_VARIANT_IP6] = "IP6",
63   [FLOW_VARIANT_L2] = "L2",
64   [FLOW_VARIANT_L2_IP4] = "L2-IP4",
65   [FLOW_VARIANT_L2_IP6] = "L2-IP6",
66 };
67
68 /* packet trace format function */
69 static u8 *
70 format_flowprobe_trace (u8 * s, va_list * args)
71 {
72   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74   flowprobe_trace_t *t = va_arg (*args, flowprobe_trace_t *);
75   uword indent = format_get_indent (s);
76
77   s = format (s,
78               "FLOWPROBE[%s]: rx_sw_if_index %d, tx_sw_if_index %d, "
79               "timestamp %lld, size %d", flowprobe_variant_strings[t->which],
80               t->rx_sw_if_index, t->tx_sw_if_index,
81               t->timestamp, t->buffer_size);
82
83   if (t->which == FLOW_VARIANT_L2)
84     s = format (s, "\n%U -> %U", format_white_space, indent,
85                 format_ethernet_address, &t->src_mac,
86                 format_ethernet_address, &t->dst_mac);
87
88   if (t->protocol > 0
89       && (t->which == FLOW_VARIANT_L2_IP4 || t->which == FLOW_VARIANT_IP4
90           || t->which == FLOW_VARIANT_L2_IP6 || t->which == FLOW_VARIANT_IP6))
91     s =
92       format (s, "\n%U%U: %U -> %U", format_white_space, indent,
93               format_ip_protocol, t->protocol, format_ip46_address,
94               &t->src_address, IP46_TYPE_ANY, format_ip46_address,
95               &t->dst_address, IP46_TYPE_ANY);
96   return s;
97 }
98
99 vlib_node_registration_t flowprobe_ip4_node;
100 vlib_node_registration_t flowprobe_ip6_node;
101 vlib_node_registration_t flowprobe_l2_node;
102
103 /* No counters at the moment */
104 #define foreach_flowprobe_error                 \
105 _(COLLISION, "Hash table collisions")           \
106 _(BUFFER, "Buffer allocation error")            \
107 _(EXPORTED_PACKETS, "Exported packets")         \
108 _(INPATH, "Exported packets in path")
109
110 typedef enum
111 {
112 #define _(sym,str) FLOWPROBE_ERROR_##sym,
113   foreach_flowprobe_error
114 #undef _
115     FLOWPROBE_N_ERROR,
116 } flowprobe_error_t;
117
118 static char *flowprobe_error_strings[] = {
119 #define _(sym,string) string,
120   foreach_flowprobe_error
121 #undef _
122 };
123
124 typedef enum
125 {
126   FLOWPROBE_NEXT_DROP,
127   FLOWPROBE_NEXT_IP4_LOOKUP,
128   FLOWPROBE_N_NEXT,
129 } flowprobe_next_t;
130
131 #define FLOWPROBE_NEXT_NODES {                                  \
132     [FLOWPROBE_NEXT_DROP] = "error-drop",                       \
133     [FLOWPROBE_NEXT_IP4_LOOKUP] = "ip4-lookup",         \
134 }
135
136 static inline flowprobe_variant_t
137 flowprobe_get_variant (flowprobe_variant_t which,
138                        flowprobe_record_t flags, u16 ethertype)
139 {
140   if (which == FLOW_VARIANT_L2
141       && (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4))
142     return ethertype == ETHERNET_TYPE_IP6 ? FLOW_VARIANT_L2_IP6 : ethertype ==
143       ETHERNET_TYPE_IP4 ? FLOW_VARIANT_L2_IP4 : FLOW_VARIANT_L2;
144   return which;
145 }
146
147 static inline u32
148 flowprobe_common_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
149 {
150   u16 start = offset;
151
152   /* Ingress interface */
153   u32 rx_if = clib_host_to_net_u32 (e->key.rx_sw_if_index);
154   clib_memcpy (to_b->data + offset, &rx_if, sizeof (rx_if));
155   offset += sizeof (rx_if);
156
157   /* Egress interface */
158   u32 tx_if = clib_host_to_net_u32 (e->key.tx_sw_if_index);
159   clib_memcpy (to_b->data + offset, &tx_if, sizeof (tx_if));
160   offset += sizeof (tx_if);
161
162   /* packet delta count */
163   u64 packetdelta = clib_host_to_net_u64 (e->packetcount);
164   clib_memcpy (to_b->data + offset, &packetdelta, sizeof (u64));
165   offset += sizeof (u64);
166
167   return offset - start;
168 }
169
170 static inline u32
171 flowprobe_l2_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
172 {
173   u16 start = offset;
174
175   /* src mac address */
176   clib_memcpy (to_b->data + offset, &e->key.src_mac, 6);
177   offset += 6;
178
179   /* dst mac address */
180   clib_memcpy (to_b->data + offset, &e->key.dst_mac, 6);
181   offset += 6;
182
183   /* ethertype */
184   clib_memcpy (to_b->data + offset, &e->key.ethertype, 2);
185   offset += 2;
186
187   return offset - start;
188 }
189
190 static inline u32
191 flowprobe_l3_ip6_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
192 {
193   u16 start = offset;
194
195   /* ip6 src address */
196   clib_memcpy (to_b->data + offset, &e->key.src_address,
197                sizeof (ip6_address_t));
198   offset += sizeof (ip6_address_t);
199
200   /* ip6 dst address */
201   clib_memcpy (to_b->data + offset, &e->key.dst_address,
202                sizeof (ip6_address_t));
203   offset += sizeof (ip6_address_t);
204
205   /* Protocol */
206   to_b->data[offset++] = e->key.protocol;
207
208   /* octetDeltaCount */
209   u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
210   clib_memcpy (to_b->data + offset, &octetdelta, sizeof (u64));
211   offset += sizeof (u64);
212
213   return offset - start;
214 }
215
216 static inline u32
217 flowprobe_l3_ip4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
218 {
219   u16 start = offset;
220
221   /* ip4 src address */
222   clib_memcpy (to_b->data + offset, &e->key.src_address.ip4,
223                sizeof (ip4_address_t));
224   offset += sizeof (ip4_address_t);
225
226   /* ip4 dst address */
227   clib_memcpy (to_b->data + offset, &e->key.dst_address.ip4,
228                sizeof (ip4_address_t));
229   offset += sizeof (ip4_address_t);
230
231   /* Protocol */
232   to_b->data[offset++] = e->key.protocol;
233
234   /* octetDeltaCount */
235   u64 octetdelta = clib_host_to_net_u64 (e->octetcount);
236   clib_memcpy (to_b->data + offset, &octetdelta, sizeof (u64));
237   offset += sizeof (u64);
238
239   return offset - start;
240 }
241
242 static inline u32
243 flowprobe_l4_add (vlib_buffer_t * to_b, flowprobe_entry_t * e, u16 offset)
244 {
245   u16 start = offset;
246
247   /* src port */
248   clib_memcpy (to_b->data + offset, &e->key.src_port, 2);
249   offset += 2;
250
251   /* dst port */
252   clib_memcpy (to_b->data + offset, &e->key.dst_port, 2);
253   offset += 2;
254
255   return offset - start;
256 }
257
258 static inline u32
259 flowprobe_hash (flowprobe_key_t * k)
260 {
261   flowprobe_main_t *fm = &flowprobe_main;
262   u32 h = 0;
263
264 #ifdef clib_crc32c_uses_intrinsics
265   h = clib_crc32c ((u8 *) k->as_u32, FLOWPROBE_KEY_IN_U32);
266 #else
267   u64 tmp =
268     k->as_u32[0] ^ k->as_u32[1] ^ k->as_u32[2] ^ k->as_u32[3] ^ k->as_u32[4];
269   h = clib_xxhash (tmp);
270 #endif
271
272   return h >> (32 - fm->ht_log2len);
273 }
274
275 flowprobe_entry_t *
276 flowprobe_lookup (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex,
277                   bool * collision)
278 {
279   flowprobe_main_t *fm = &flowprobe_main;
280   flowprobe_entry_t *e;
281   u32 h;
282
283   h = (fm->active_timer) ? flowprobe_hash (k) : 0;
284
285   /* Lookup in the flow state pool */
286   *poolindex = fm->hash_per_worker[my_cpu_number][h];
287   if (*poolindex != ~0)
288     {
289       e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], *poolindex);
290       if (e)
291         {
292           /* Verify key or report collision */
293           if (memcmp (k, &e->key, sizeof (flowprobe_key_t)))
294             *collision = true;
295           return e;
296         }
297     }
298
299   return 0;
300 }
301
302 flowprobe_entry_t *
303 flowprobe_create (u32 my_cpu_number, flowprobe_key_t * k, u32 * poolindex)
304 {
305   flowprobe_main_t *fm = &flowprobe_main;
306   u32 h;
307
308   flowprobe_entry_t *e;
309
310   /* Get my index */
311   h = (fm->active_timer) ? flowprobe_hash (k) : 0;
312
313   pool_get (fm->pool_per_worker[my_cpu_number], e);
314   *poolindex = e - fm->pool_per_worker[my_cpu_number];
315   fm->hash_per_worker[my_cpu_number][h] = *poolindex;
316
317   e->key = *k;
318
319   if (fm->passive_timer > 0)
320     {
321       e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
322         (fm->timers_per_worker[my_cpu_number], *poolindex, 0,
323          fm->passive_timer);
324     }
325   return e;
326 }
327
328 static inline void
329 add_to_flow_record_state (vlib_main_t * vm, vlib_node_runtime_t * node,
330                           flowprobe_main_t * fm, vlib_buffer_t * b,
331                           u64 timestamp, u16 length,
332                           flowprobe_variant_t which, flowprobe_trace_t * t)
333 {
334   if (fm->disabled)
335     return;
336
337   u32 my_cpu_number = vm->thread_index;
338   u16 octets = 0;
339
340   flowprobe_record_t flags = fm->context[which].flags;
341   bool collect_ip4 = false, collect_ip6 = false;
342   ASSERT (b);
343   ethernet_header_t *eth = vlib_buffer_get_current (b);
344   u16 ethertype = clib_net_to_host_u16 (eth->type);
345   /* *INDENT-OFF* */
346   flowprobe_key_t k = { {0} };
347   /* *INDENT-ON* */
348   ip4_header_t *ip4 = 0;
349   ip6_header_t *ip6 = 0;
350   udp_header_t *udp = 0;
351
352   if (flags & FLOW_RECORD_L3 || flags & FLOW_RECORD_L4)
353     {
354       collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
355       collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
356     }
357
358   k.rx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
359   k.tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
360
361   k.which = which;
362
363   if (flags & FLOW_RECORD_L2)
364     {
365       clib_memcpy (k.src_mac, eth->src_address, 6);
366       clib_memcpy (k.dst_mac, eth->dst_address, 6);
367       k.ethertype = ethertype;
368     }
369   if (collect_ip6 && ethertype == ETHERNET_TYPE_IP6)
370     {
371       ip6 = (ip6_header_t *) (eth + 1);
372       udp = (udp_header_t *) (ip6 + 1);
373       if (flags & FLOW_RECORD_L3)
374         {
375           k.src_address.as_u64[0] = ip6->src_address.as_u64[0];
376           k.src_address.as_u64[1] = ip6->src_address.as_u64[1];
377           k.dst_address.as_u64[0] = ip6->dst_address.as_u64[0];
378           k.dst_address.as_u64[1] = ip6->dst_address.as_u64[1];
379         }
380       k.protocol = ip6->protocol;
381       octets = clib_net_to_host_u16 (ip6->payload_length)
382         + sizeof (ip6_header_t);
383     }
384   if (collect_ip4 && ethertype == ETHERNET_TYPE_IP4)
385     {
386       ip4 = (ip4_header_t *) (eth + 1);
387       udp = (udp_header_t *) (ip4 + 1);
388       if (flags & FLOW_RECORD_L3)
389         {
390           k.src_address.ip4.as_u32 = ip4->src_address.as_u32;
391           k.dst_address.ip4.as_u32 = ip4->dst_address.as_u32;
392         }
393       k.protocol = ip4->protocol;
394       octets = clib_net_to_host_u16 (ip4->length);
395     }
396   if ((flags & FLOW_RECORD_L4) && udp &&
397       (k.protocol == IP_PROTOCOL_TCP || k.protocol == IP_PROTOCOL_UDP))
398     {
399       k.src_port = udp->src_port;
400       k.dst_port = udp->dst_port;
401     }
402
403   if (t)
404     {
405       t->rx_sw_if_index = k.rx_sw_if_index;
406       t->tx_sw_if_index = k.tx_sw_if_index;
407       clib_memcpy (t->src_mac, k.src_mac, 6);
408       clib_memcpy (t->dst_mac, k.dst_mac, 6);
409       t->ethertype = k.ethertype;
410       t->src_address.ip4.as_u32 = k.src_address.ip4.as_u32;
411       t->dst_address.ip4.as_u32 = k.dst_address.ip4.as_u32;
412       t->protocol = k.protocol;
413       t->src_port = k.src_port;
414       t->dst_port = k.dst_port;
415       t->which = k.which;
416     }
417
418   flowprobe_entry_t *e = 0;
419   f64 now = vlib_time_now (vm);
420   if (fm->active_timer > 0)
421     {
422       u32 poolindex = ~0;
423       bool collision = false;
424
425       e = flowprobe_lookup (my_cpu_number, &k, &poolindex, &collision);
426       if (collision)
427         {
428           /* Flush data and clean up entry for reuse. */
429           if (e->packetcount)
430             flowprobe_export_entry (vm, e);
431           e->key = k;
432           vlib_node_increment_counter (vm, node->node_index,
433                                        FLOWPROBE_ERROR_COLLISION, 1);
434         }
435       if (!e)                   /* Create new entry */
436         {
437           e = flowprobe_create (my_cpu_number, &k, &poolindex);
438           e->last_exported = now;
439         }
440     }
441   else
442     {
443       e = &fm->stateless_entry[my_cpu_number];
444       e->key = k;
445     }
446
447   if (e)
448     {
449       /* Updating entry */
450       e->packetcount++;
451       e->octetcount += octets;
452       e->last_updated = now;
453
454       if (fm->active_timer == 0
455           || (now > e->last_exported + fm->active_timer))
456         flowprobe_export_entry (vm, e);
457     }
458 }
459
460 static u16
461 flowprobe_get_headersize (void)
462 {
463   return sizeof (ip4_header_t) + sizeof (udp_header_t) +
464     sizeof (ipfix_message_header_t) + sizeof (ipfix_set_header_t);
465 }
466
467 static void
468 flowprobe_export_send (vlib_main_t * vm, vlib_buffer_t * b0,
469                        flowprobe_variant_t which)
470 {
471   flowprobe_main_t *fm = &flowprobe_main;
472   flow_report_main_t *frm = &flow_report_main;
473   vlib_frame_t *f;
474   ip4_ipfix_template_packet_t *tp;
475   ipfix_set_header_t *s;
476   ipfix_message_header_t *h;
477   ip4_header_t *ip;
478   udp_header_t *udp;
479   flowprobe_record_t flags = fm->context[which].flags;
480   u32 my_cpu_number = vm->thread_index;
481
482   /* Fill in header */
483   flow_report_stream_t *stream;
484
485   /* Nothing to send */
486   if (fm->context[which].next_record_offset_per_worker[my_cpu_number] <=
487       flowprobe_get_headersize ())
488     return;
489
490   u32 i, index = vec_len (frm->streams);
491   for (i = 0; i < index; i++)
492     if (frm->streams[i].domain_id == 1)
493       {
494         index = i;
495         break;
496       }
497   if (i == vec_len (frm->streams))
498     {
499       vec_validate (frm->streams, index);
500       frm->streams[index].domain_id = 1;
501     }
502   stream = &frm->streams[index];
503
504   tp = vlib_buffer_get_current (b0);
505   ip = (ip4_header_t *) & tp->ip4;
506   udp = (udp_header_t *) (ip + 1);
507   h = (ipfix_message_header_t *) (udp + 1);
508   s = (ipfix_set_header_t *) (h + 1);
509
510   ip->ip_version_and_header_length = 0x45;
511   ip->ttl = 254;
512   ip->protocol = IP_PROTOCOL_UDP;
513   ip->flags_and_fragment_offset = 0;
514   ip->src_address.as_u32 = frm->src_address.as_u32;
515   ip->dst_address.as_u32 = frm->ipfix_collector.as_u32;
516   udp->src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipfix);
517   udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipfix);
518   udp->checksum = 0;
519
520   /* FIXUP: message header export_time */
521   h->export_time = (u32)
522     (((f64) frm->unix_time_0) +
523      (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
524   h->export_time = clib_host_to_net_u32 (h->export_time);
525   h->domain_id = clib_host_to_net_u32 (stream->domain_id);
526
527   /* FIXUP: message header sequence_number */
528   h->sequence_number = stream->sequence_number++;
529   h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
530
531   s->set_id_length = ipfix_set_id_length (fm->template_reports[flags],
532                                           b0->current_length -
533                                           (sizeof (*ip) + sizeof (*udp) +
534                                            sizeof (*h)));
535   h->version_length = version_length (b0->current_length -
536                                       (sizeof (*ip) + sizeof (*udp)));
537
538   ip->length = clib_host_to_net_u16 (b0->current_length);
539
540   ip->checksum = ip4_header_checksum (ip);
541   udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
542
543   if (frm->udp_checksum)
544     {
545       /* RFC 7011 section 10.3.2. */
546       udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
547       if (udp->checksum == 0)
548         udp->checksum = 0xffff;
549     }
550
551   ASSERT (ip->checksum == ip4_header_checksum (ip));
552
553   /* Find or allocate a frame */
554   f = fm->context[which].frames_per_worker[my_cpu_number];
555   if (PREDICT_FALSE (f == 0))
556     {
557       u32 *to_next;
558       f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
559       fm->context[which].frames_per_worker[my_cpu_number] = f;
560       u32 bi0 = vlib_get_buffer_index (vm, b0);
561
562       /* Enqueue the buffer */
563       to_next = vlib_frame_vector_args (f);
564       to_next[0] = bi0;
565       f->n_vectors = 1;
566     }
567
568   vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
569   vlib_node_increment_counter (vm, flowprobe_l2_node.index,
570                                FLOWPROBE_ERROR_EXPORTED_PACKETS, 1);
571
572   fm->context[which].frames_per_worker[my_cpu_number] = 0;
573   fm->context[which].buffers_per_worker[my_cpu_number] = 0;
574   fm->context[which].next_record_offset_per_worker[my_cpu_number] =
575     flowprobe_get_headersize ();
576 }
577
578 static vlib_buffer_t *
579 flowprobe_get_buffer (vlib_main_t * vm, flowprobe_variant_t which)
580 {
581   flowprobe_main_t *fm = &flowprobe_main;
582   flow_report_main_t *frm = &flow_report_main;
583   vlib_buffer_t *b0;
584   u32 bi0;
585   vlib_buffer_free_list_t *fl;
586   u32 my_cpu_number = vm->thread_index;
587
588   /* Find or allocate a buffer */
589   b0 = fm->context[which].buffers_per_worker[my_cpu_number];
590
591   /* Need to allocate a buffer? */
592   if (PREDICT_FALSE (b0 == 0))
593     {
594       if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
595         {
596           vlib_node_increment_counter (vm, flowprobe_l2_node.index,
597                                        FLOWPROBE_ERROR_BUFFER, 1);
598           return 0;
599         }
600
601       /* Initialize the buffer */
602       b0 = fm->context[which].buffers_per_worker[my_cpu_number] =
603         vlib_get_buffer (vm, bi0);
604       fl =
605         vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
606       vlib_buffer_init_for_free_list (b0, fl);
607       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
608
609       b0->current_data = 0;
610       b0->current_length = flowprobe_get_headersize ();
611       b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_FLOW_REPORT);
612       vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
613       vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
614       fm->context[which].next_record_offset_per_worker[my_cpu_number] =
615         b0->current_length;
616     }
617
618   return b0;
619 }
620
621 static void
622 flowprobe_export_entry (vlib_main_t * vm, flowprobe_entry_t * e)
623 {
624   u32 my_cpu_number = vm->thread_index;
625   flowprobe_main_t *fm = &flowprobe_main;
626   flow_report_main_t *frm = &flow_report_main;
627   vlib_buffer_t *b0;
628   bool collect_ip4 = false, collect_ip6 = false;
629   flowprobe_variant_t which = e->key.which;
630   flowprobe_record_t flags = fm->context[which].flags;
631   u16 offset =
632     fm->context[which].next_record_offset_per_worker[my_cpu_number];
633
634   if (offset < flowprobe_get_headersize ())
635     offset = flowprobe_get_headersize ();
636
637   b0 = flowprobe_get_buffer (vm, which);
638   /* No available buffer, what to do... */
639   if (b0 == 0)
640     return;
641
642   if (flags & FLOW_RECORD_L3)
643     {
644       collect_ip4 = which == FLOW_VARIANT_L2_IP4 || which == FLOW_VARIANT_IP4;
645       collect_ip6 = which == FLOW_VARIANT_L2_IP6 || which == FLOW_VARIANT_IP6;
646     }
647
648   offset += flowprobe_common_add (b0, e, offset);
649
650   if (flags & FLOW_RECORD_L2)
651     offset += flowprobe_l2_add (b0, e, offset);
652   if (collect_ip6)
653     offset += flowprobe_l3_ip6_add (b0, e, offset);
654   if (collect_ip4)
655     offset += flowprobe_l3_ip4_add (b0, e, offset);
656   if (flags & FLOW_RECORD_L4)
657     offset += flowprobe_l4_add (b0, e, offset);
658
659   /* Reset per flow-export counters */
660   e->packetcount = 0;
661   e->octetcount = 0;
662   e->last_exported = vlib_time_now (vm);
663
664   b0->current_length = offset;
665
666   fm->context[which].next_record_offset_per_worker[my_cpu_number] = offset;
667   /* Time to flush the buffer? */
668   if (offset + fm->template_size[flags] > frm->path_mtu)
669     flowprobe_export_send (vm, b0, which);
670 }
671
672 uword
673 flowprobe_node_fn (vlib_main_t * vm,
674                    vlib_node_runtime_t * node, vlib_frame_t * frame,
675                    flowprobe_variant_t which)
676 {
677   u32 n_left_from, *from, *to_next;
678   flowprobe_next_t next_index;
679   flowprobe_main_t *fm = &flowprobe_main;
680   u64 now;
681
682   now = (u64) ((vlib_time_now (vm) - fm->vlib_time_0) * 1e9);
683   now += fm->nanosecond_time_0;
684
685   from = vlib_frame_vector_args (frame);
686   n_left_from = frame->n_vectors;
687   next_index = node->cached_next_index;
688
689   while (n_left_from > 0)
690     {
691       u32 n_left_to_next;
692
693       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
694
695       while (n_left_from >= 4 && n_left_to_next >= 2)
696         {
697           u32 next0 = FLOWPROBE_NEXT_DROP;
698           u32 next1 = FLOWPROBE_NEXT_DROP;
699           u16 len0, len1;
700           u32 bi0, bi1;
701           vlib_buffer_t *b0, *b1;
702
703           /* Prefetch next iteration. */
704           {
705             vlib_buffer_t *p2, *p3;
706
707             p2 = vlib_get_buffer (vm, from[2]);
708             p3 = vlib_get_buffer (vm, from[3]);
709
710             vlib_prefetch_buffer_header (p2, LOAD);
711             vlib_prefetch_buffer_header (p3, LOAD);
712
713             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
714             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
715           }
716
717           /* speculatively enqueue b0 and b1 to the current next frame */
718           to_next[0] = bi0 = from[0];
719           to_next[1] = bi1 = from[1];
720           from += 2;
721           to_next += 2;
722           n_left_from -= 2;
723           n_left_to_next -= 2;
724
725           b0 = vlib_get_buffer (vm, bi0);
726           b1 = vlib_get_buffer (vm, bi1);
727
728           vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_TX],
729                              &next0, b0);
730           vnet_feature_next (vnet_buffer (b1)->sw_if_index[VLIB_TX],
731                              &next1, b1);
732
733           len0 = vlib_buffer_length_in_chain (vm, b0);
734           ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
735           u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
736
737           if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_FLOW_REPORT) == 0))
738             add_to_flow_record_state (vm, node, fm, b0, now, len0,
739                                       flowprobe_get_variant
740                                       (which, fm->context[which].flags,
741                                        ethertype0), 0);
742
743           len1 = vlib_buffer_length_in_chain (vm, b1);
744           ethernet_header_t *eh1 = vlib_buffer_get_current (b1);
745           u16 ethertype1 = clib_net_to_host_u16 (eh1->type);
746
747           if (PREDICT_TRUE ((b1->flags & VLIB_BUFFER_FLOW_REPORT) == 0))
748             add_to_flow_record_state (vm, node, fm, b1, now, len1,
749                                       flowprobe_get_variant
750                                       (which, fm->context[which].flags,
751                                        ethertype1), 0);
752
753           /* verify speculative enqueues, maybe switch current next frame */
754           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
755                                            to_next, n_left_to_next,
756                                            bi0, bi1, next0, next1);
757         }
758
759       while (n_left_from > 0 && n_left_to_next > 0)
760         {
761           u32 bi0;
762           vlib_buffer_t *b0;
763           u32 next0 = FLOWPROBE_NEXT_DROP;
764           u16 len0;
765
766           /* speculatively enqueue b0 to the current next frame */
767           bi0 = from[0];
768           to_next[0] = bi0;
769           from += 1;
770           to_next += 1;
771           n_left_from -= 1;
772           n_left_to_next -= 1;
773
774           b0 = vlib_get_buffer (vm, bi0);
775
776           vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_TX],
777                              &next0, b0);
778
779           len0 = vlib_buffer_length_in_chain (vm, b0);
780           ethernet_header_t *eh0 = vlib_buffer_get_current (b0);
781           u16 ethertype0 = clib_net_to_host_u16 (eh0->type);
782
783           if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_FLOW_REPORT) == 0))
784             {
785               flowprobe_trace_t *t = 0;
786               if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
787                                  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
788                 t = vlib_add_trace (vm, node, b0, sizeof (*t));
789
790               add_to_flow_record_state (vm, node, fm, b0, now, len0,
791                                         flowprobe_get_variant
792                                         (which, fm->context[which].flags,
793                                          ethertype0), t);
794             }
795
796           /* verify speculative enqueue, maybe switch current next frame */
797           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
798                                            to_next, n_left_to_next,
799                                            bi0, next0);
800         }
801
802       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
803     }
804   return frame->n_vectors;
805 }
806
807 static uword
808 flowprobe_ip4_node_fn (vlib_main_t * vm,
809                        vlib_node_runtime_t * node, vlib_frame_t * frame)
810 {
811   return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP4);
812 }
813
814 static uword
815 flowprobe_ip6_node_fn (vlib_main_t * vm,
816                        vlib_node_runtime_t * node, vlib_frame_t * frame)
817 {
818   return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_IP6);
819 }
820
821 static uword
822 flowprobe_l2_node_fn (vlib_main_t * vm,
823                       vlib_node_runtime_t * node, vlib_frame_t * frame)
824 {
825   return flowprobe_node_fn (vm, node, frame, FLOW_VARIANT_L2);
826 }
827
828 static inline void
829 flush_record (flowprobe_variant_t which)
830 {
831   vlib_main_t *vm = vlib_get_main ();
832   vlib_buffer_t *b = flowprobe_get_buffer (vm, which);
833   if (b)
834     flowprobe_export_send (vm, b, which);
835 }
836
837 void
838 flowprobe_flush_callback_ip4 (void)
839 {
840   flush_record (FLOW_VARIANT_IP4);
841 }
842
843 void
844 flowprobe_flush_callback_ip6 (void)
845 {
846   flush_record (FLOW_VARIANT_IP6);
847 }
848
849 void
850 flowprobe_flush_callback_l2 (void)
851 {
852   flush_record (FLOW_VARIANT_L2);
853   flush_record (FLOW_VARIANT_L2_IP4);
854   flush_record (FLOW_VARIANT_L2_IP6);
855 }
856
857
858 static void
859 flowprobe_delete_by_index (u32 my_cpu_number, u32 poolindex)
860 {
861   flowprobe_main_t *fm = &flowprobe_main;
862   flowprobe_entry_t *e;
863   u32 h;
864
865   e = pool_elt_at_index (fm->pool_per_worker[my_cpu_number], poolindex);
866
867   /* Get my index */
868   h = flowprobe_hash (&e->key);
869
870   /* Reset hash */
871   fm->hash_per_worker[my_cpu_number][h] = ~0;
872
873   pool_put_index (fm->pool_per_worker[my_cpu_number], poolindex);
874 }
875
876
877 /* Per worker process processing the active/passive expired entries */
878 static uword
879 flowprobe_walker_process (vlib_main_t * vm,
880                           vlib_node_runtime_t * rt, vlib_frame_t * f)
881 {
882   flowprobe_main_t *fm = &flowprobe_main;
883   flow_report_main_t *frm = &flow_report_main;
884   flowprobe_entry_t *e;
885
886   /*
887    * $$$$ Remove this check from here and track FRM status and disable
888    * this process if required.
889    */
890   if (frm->ipfix_collector.as_u32 == 0 || frm->src_address.as_u32 == 0)
891     {
892       fm->disabled = true;
893       return 0;
894     }
895   fm->disabled = false;
896
897   u32 cpu_index = os_get_thread_index ();
898   u32 *to_be_removed = 0, *i;
899
900   /*
901    * Tick the timer when required and process the vector of expired
902    * timers
903    */
904   f64 start_time = vlib_time_now (vm);
905   u32 count = 0;
906
907   tw_timer_expire_timers_2t_1w_2048sl (fm->timers_per_worker[cpu_index],
908                                        start_time);
909
910   vec_foreach (i, fm->expired_passive_per_worker[cpu_index])
911   {
912     u32 exported = 0;
913     f64 now = vlib_time_now (vm);
914     if (now > start_time + 100e-6
915         || exported > FLOW_MAXIMUM_EXPORT_ENTRIES - 1)
916       break;
917
918     if (pool_is_free_index (fm->pool_per_worker[cpu_index], *i))
919       {
920         clib_warning ("Element is %d is freed already\n", *i);
921         continue;
922       }
923     else
924       e = pool_elt_at_index (fm->pool_per_worker[cpu_index], *i);
925
926     /* Check last update timestamp. If it is longer than passive time nuke
927      * entry. Otherwise restart timer with what's left
928      * Premature passive timer by more than 10%
929      */
930     if ((now - e->last_updated) < (fm->passive_timer * 0.9))
931       {
932         f64 delta = fm->passive_timer - (now - e->last_updated);
933         e->passive_timer_handle = tw_timer_start_2t_1w_2048sl
934           (fm->timers_per_worker[cpu_index], *i, 0, delta);
935       }
936     else                        /* Nuke entry */
937       {
938         vec_add1 (to_be_removed, *i);
939       }
940     /* If anything to report send it to the exporter */
941     if (e->packetcount && now > e->last_exported + fm->active_timer)
942       {
943         exported++;
944         flowprobe_export_entry (vm, e);
945       }
946     count++;
947   }
948   if (count)
949     vec_delete (fm->expired_passive_per_worker[cpu_index], count, 0);
950
951   vec_foreach (i, to_be_removed) flowprobe_delete_by_index (cpu_index, *i);
952   vec_free (to_be_removed);
953
954   return 0;
955 }
956
957 /* *INDENT-OFF* */
958 VLIB_REGISTER_NODE (flowprobe_ip4_node) = {
959   .function = flowprobe_ip4_node_fn,
960   .name = "flowprobe-ip4",
961   .vector_size = sizeof (u32),
962   .format_trace = format_flowprobe_trace,
963   .type = VLIB_NODE_TYPE_INTERNAL,
964   .n_errors = ARRAY_LEN(flowprobe_error_strings),
965   .error_strings = flowprobe_error_strings,
966   .n_next_nodes = FLOWPROBE_N_NEXT,
967   .next_nodes = FLOWPROBE_NEXT_NODES,
968 };
969 VLIB_REGISTER_NODE (flowprobe_ip6_node) = {
970   .function = flowprobe_ip6_node_fn,
971   .name = "flowprobe-ip6",
972   .vector_size = sizeof (u32),
973   .format_trace = format_flowprobe_trace,
974   .type = VLIB_NODE_TYPE_INTERNAL,
975   .n_errors = ARRAY_LEN(flowprobe_error_strings),
976   .error_strings = flowprobe_error_strings,
977   .n_next_nodes = FLOWPROBE_N_NEXT,
978   .next_nodes = FLOWPROBE_NEXT_NODES,
979 };
980 VLIB_REGISTER_NODE (flowprobe_l2_node) = {
981   .function = flowprobe_l2_node_fn,
982   .name = "flowprobe-l2",
983   .vector_size = sizeof (u32),
984   .format_trace = format_flowprobe_trace,
985   .type = VLIB_NODE_TYPE_INTERNAL,
986   .n_errors = ARRAY_LEN(flowprobe_error_strings),
987   .error_strings = flowprobe_error_strings,
988   .n_next_nodes = FLOWPROBE_N_NEXT,
989   .next_nodes = FLOWPROBE_NEXT_NODES,
990 };
991 VLIB_REGISTER_NODE (flowprobe_walker_node) = {
992   .function = flowprobe_walker_process,
993   .name = "flowprobe-walker",
994   .type = VLIB_NODE_TYPE_INPUT,
995   .state = VLIB_NODE_STATE_INTERRUPT,
996 };
997 /* *INDENT-ON* */
998
999 /*
1000  * fd.io coding-style-patch-verification: ON
1001  *
1002  * Local Variables:
1003  * eval: (c-set-style "gnu")
1004  * End:
1005  */