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