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