ipfix-export: support creating multiple exporters
[vpp.git] / src / vnet / ipfix-export / flow_report_classify.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/ipfix-export/flow_report.h>
16 #include <vnet/ipfix-export/flow_report_classify.h>
17 #include <vnet/api_errno.h>
18 #include <vnet/classify/vnet_classify.h>
19 #include <vnet/ip/ip4.h>
20 #include <vnet/udp/udp_local.h>
21
22 /* Common prefix of tcp and udp headers
23  * containing only source and destination port fields */
24 typedef struct
25 {
26   u16 src_port, dst_port;
27 } tcpudp_header_t;
28
29 flow_report_classify_main_t flow_report_classify_main;
30
31 u8 *
32 ipfix_classify_template_rewrite (ipfix_exporter_t *exp, flow_report_t *fr,
33                                  ip4_address_t *collector_address,
34                                  ip4_address_t *src_address,
35                                  u16 collector_port,
36                                  ipfix_report_element_t *elts, u32 n_elts,
37                                  u32 *stream_index)
38 {
39   flow_report_classify_main_t *fcm = &flow_report_classify_main;
40   vnet_classify_table_t *tblp;
41   vnet_classify_main_t *vcm = &vnet_classify_main;
42   u32 flow_table_index = fr->opaque.as_uword;
43   u8 *ip_start;
44   ip4_header_t *ip;
45   ip6_header_t *ip6;
46   tcpudp_header_t *tcpudp;
47   udp_header_t *udp;
48   ipfix_message_header_t *h;
49   ipfix_set_header_t *s;
50   ipfix_template_header_t *t;
51   ipfix_field_specifier_t *f;
52   ipfix_field_specifier_t *first_field;
53   u8 *rewrite = 0;
54   ip4_ipfix_template_packet_t *tp;
55   u32 field_count = 0;
56   u32 field_index = 0;
57   flow_report_stream_t *stream;
58   u8 ip_version;
59   u8 transport_protocol;
60   u8 *virt_mask;
61   u8 *real_mask;
62
63   stream = &exp->streams[fr->stream_index];
64
65   ipfix_classify_table_t *table = &fcm->tables[flow_table_index];
66
67   ip_version = table->ip_version;
68   transport_protocol = table->transport_protocol;
69
70   tblp = pool_elt_at_index (vcm->tables, table->classify_table_index);
71
72   virt_mask = (u8 *) (tblp->mask - tblp->skip_n_vectors);
73   real_mask = (u8 *) (tblp->mask);
74
75   /* Determine field count */
76   ip_start = virt_mask + sizeof (ethernet_header_t);
77 #define _(field,mask,item,length)                                             \
78   if (((u8 *)&field >= real_mask) && (memcmp(&field, &mask, length) == 0))    \
79     {                                                                         \
80       field_count++;                                                          \
81                                                                               \
82       fr->fields_to_send = clib_bitmap_set (fr->fields_to_send,               \
83                                             field_index, 1);                  \
84     }                                                                         \
85   field_index++;
86   foreach_ipfix_field;
87 #undef _
88
89   /* Add packetTotalCount manually */
90   field_count += 1;
91
92   /* $$$ enterprise fields, at some later date */
93
94   /* allocate rewrite space */
95   vec_validate_aligned (rewrite,
96                         sizeof (ip4_ipfix_template_packet_t)
97                         + field_count * sizeof (ipfix_field_specifier_t) - 1,
98                         CLIB_CACHE_LINE_BYTES);
99
100   tp = (ip4_ipfix_template_packet_t *) rewrite;
101   ip = (ip4_header_t *) & tp->ip4;
102   udp = (udp_header_t *) (ip + 1);
103   h = (ipfix_message_header_t *) (udp + 1);
104   s = (ipfix_set_header_t *) (h + 1);
105   t = (ipfix_template_header_t *) (s + 1);
106   first_field = f = (ipfix_field_specifier_t *) (t + 1);
107
108   ip->ip_version_and_header_length = 0x45;
109   ip->ttl = 254;
110   ip->protocol = IP_PROTOCOL_UDP;
111   ip->src_address.as_u32 = src_address->as_u32;
112   ip->dst_address.as_u32 = collector_address->as_u32;
113   udp->src_port = clib_host_to_net_u16 (stream->src_port);
114   udp->dst_port = clib_host_to_net_u16 (collector_port);
115   udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
116
117   /* FIXUP: message header export_time */
118   /* FIXUP: message header sequence_number */
119   h->domain_id = clib_host_to_net_u32 (stream->domain_id);
120
121   /* Take another trip through the mask and build the template */
122   ip_start = virt_mask + sizeof (ethernet_header_t);
123 #define _(field,mask,item,length)                                             \
124   if (((u8 *)&field >= real_mask) && (memcmp(&field, &mask, length) == 0))    \
125     {                                                                         \
126       f->e_id_length = ipfix_e_id_length (0 /* enterprise */,                 \
127                                           item, length);                      \
128       f++;                                                                    \
129     }
130   foreach_ipfix_field;
131 #undef _
132
133   /* Add packetTotalCount manually */
134   f->e_id_length =
135     ipfix_e_id_length (0 /* enterprise */ , packetTotalCount, 8);
136   f++;
137
138   /* Back to the template packet... */
139   ip = (ip4_header_t *) & tp->ip4;
140   udp = (udp_header_t *) (ip + 1);
141
142   ASSERT (f - first_field);
143   /* Field count in this template */
144   t->id_count = ipfix_id_count (fr->template_id, f - first_field);
145
146   /* set length in octets */
147   s->set_id_length =
148     ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
149
150   /* message length in octets */
151   h->version_length = version_length ((u8 *) f - (u8 *) h);
152
153   ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
154   ip->checksum = ip4_header_checksum (ip);
155
156   return rewrite;
157 }
158
159 vlib_frame_t *
160 ipfix_classify_send_flows (flow_report_main_t *frm, ipfix_exporter_t *exp,
161                            flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
162                            u32 node_index)
163 {
164   flow_report_classify_main_t *fcm = &flow_report_classify_main;
165   vnet_classify_main_t *vcm = &vnet_classify_main;
166   u32 flow_table_index = fr->opaque.as_uword;
167   vnet_classify_table_t *t;
168   vnet_classify_bucket_t *b;
169   vnet_classify_entry_t *v, *save_v;
170   vlib_buffer_t *b0 = 0;
171   u32 next_offset = 0;
172   u32 record_offset = 0;
173   u32 bi0 = ~0;
174   int i, j, k;
175   ip4_ipfix_template_packet_t *tp;
176   ipfix_message_header_t *h = 0;
177   ipfix_set_header_t *s = 0;
178   u8 *ip_start;
179   ip4_header_t *ip;
180   ip6_header_t *ip6;
181   tcpudp_header_t *tcpudp;
182   udp_header_t *udp;
183   int field_index;
184   u32 records_this_buffer;
185   u16 new_l0, old_l0;
186   ip_csum_t sum0;
187   vlib_main_t *vm = frm->vlib_main;
188   flow_report_stream_t *stream;
189   u8 ip_version;
190   u8 transport_protocol;
191   u8 *virt_key;
192
193   stream = &exp->streams[fr->stream_index];
194
195   ipfix_classify_table_t *table = &fcm->tables[flow_table_index];
196
197   ip_version = table->ip_version;
198   transport_protocol = table->transport_protocol;
199
200   t = pool_elt_at_index (vcm->tables, table->classify_table_index);
201
202   clib_spinlock_lock (&t->writer_lock);
203
204   for (i = 0; i < t->nbuckets; i++)
205     {
206       b = &t->buckets[i];
207       if (b->offset == 0)
208         continue;
209
210       save_v = vnet_classify_get_entry (t, b->offset);
211       for (j = 0; j < (1 << b->log2_pages); j++)
212         {
213           for (k = 0; k < t->entries_per_page; k++)
214             {
215               v = vnet_classify_entry_at_index
216                 (t, save_v, j * t->entries_per_page + k);
217
218               if (vnet_classify_entry_is_free (v))
219                 continue;
220
221               /* OK, we have something to send... */
222               if (PREDICT_FALSE (b0 == 0))
223                 {
224                   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
225                     goto flush;
226                   b0 = vlib_get_buffer (vm, bi0);
227
228                   u32 copy_len = sizeof (ip4_header_t) +
229                     sizeof (udp_header_t) + sizeof (ipfix_message_header_t);
230                   clib_memcpy_fast (b0->data, fr->rewrite, copy_len);
231                   b0->current_data = 0;
232                   b0->current_length = copy_len;
233                   b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
234                   vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
235                   vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index;
236
237                   tp = vlib_buffer_get_current (b0);
238                   ip = (ip4_header_t *) & tp->ip4;
239                   udp = (udp_header_t *) (ip + 1);
240                   h = (ipfix_message_header_t *) (udp + 1);
241                   s = (ipfix_set_header_t *) (h + 1);
242
243                   /* FIXUP: message header export_time */
244                   h->export_time = (u32)
245                     (((f64) frm->unix_time_0) +
246                      (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
247                   h->export_time = clib_host_to_net_u32 (h->export_time);
248
249                   /* FIXUP: message header sequence_number */
250                   h->sequence_number = stream->sequence_number;
251                   h->sequence_number =
252                     clib_host_to_net_u32 (h->sequence_number);
253
254                   next_offset = (u32) (((u8 *) (s + 1)) - (u8 *) tp);
255                   record_offset = next_offset;
256                   records_this_buffer = 0;
257                 }
258
259               field_index = 0;
260               virt_key = (u8 *) (v->key - t->skip_n_vectors);
261               ip_start = virt_key + sizeof (ethernet_header_t);
262 #define _(field,mask,item,length)                                       \
263               if (clib_bitmap_get (fr->fields_to_send, field_index))    \
264                 {                                                       \
265                   clib_memcpy_fast (b0->data + next_offset, &field,          \
266                           length);                                      \
267                   next_offset += length;                                \
268                 }                                                       \
269               field_index++;
270               foreach_ipfix_field;
271 #undef _
272
273               /* Add packetTotalCount manually */
274               {
275                 u64 packets = clib_host_to_net_u64 (v->hits);
276                 clib_memcpy_fast (b0->data + next_offset, &packets,
277                                   sizeof (packets));
278                 next_offset += sizeof (packets);
279               }
280               records_this_buffer++;
281               stream->sequence_number++;
282
283               /* Next record will have the same size as this record */
284               u32 next_record_size = next_offset - record_offset;
285               record_offset = next_offset;
286
287               if (next_offset + next_record_size > exp->path_mtu)
288                 {
289                   s->set_id_length = ipfix_set_id_length (fr->template_id,
290                                                           next_offset -
291                                                           (sizeof (*ip) +
292                                                            sizeof (*udp) +
293                                                            sizeof (*h)));
294                   h->version_length =
295                     version_length (next_offset -
296                                     (sizeof (*ip) + sizeof (*udp)));
297                   b0->current_length = next_offset;
298                   b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
299
300                   tp = vlib_buffer_get_current (b0);
301                   ip = (ip4_header_t *) & tp->ip4;
302                   udp = (udp_header_t *) (ip + 1);
303
304                   sum0 = ip->checksum;
305                   old_l0 = ip->length;
306                   new_l0 = clib_host_to_net_u16 ((u16) next_offset);
307
308                   sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
309                                          length /* changed member */ );
310
311                   ip->checksum = ip_csum_fold (sum0);
312                   ip->length = new_l0;
313                   udp->length =
314                     clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
315
316                   if (exp->udp_checksum)
317                     {
318                       /* RFC 7011 section 10.3.2. */
319                       udp->checksum =
320                         ip4_tcp_udp_compute_checksum (vm, b0, ip);
321                       if (udp->checksum == 0)
322                         udp->checksum = 0xffff;
323                     }
324
325                   ASSERT (ip4_header_checksum_is_valid (ip));
326
327                   to_next[0] = bi0;
328                   f->n_vectors++;
329                   to_next++;
330
331                   if (f->n_vectors == VLIB_FRAME_SIZE)
332                     {
333                       vlib_put_frame_to_node (vm, node_index, f);
334                       f = vlib_get_frame_to_node (vm, node_index);
335                       f->n_vectors = 0;
336                       to_next = vlib_frame_vector_args (f);
337                     }
338                   b0 = 0;
339                   bi0 = ~0;
340                 }
341             }
342         }
343     }
344
345 flush:
346   if (b0)
347     {
348       s->set_id_length = ipfix_set_id_length (fr->template_id,
349                                               next_offset -
350                                               (sizeof (*ip) + sizeof (*udp) +
351                                                sizeof (*h)));
352       h->version_length = version_length (next_offset -
353                                           (sizeof (*ip) + sizeof (*udp)));
354       b0->current_length = next_offset;
355       b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
356
357       tp = vlib_buffer_get_current (b0);
358       ip = (ip4_header_t *) & tp->ip4;
359       udp = (udp_header_t *) (ip + 1);
360
361       sum0 = ip->checksum;
362       old_l0 = ip->length;
363       new_l0 = clib_host_to_net_u16 ((u16) next_offset);
364
365       sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
366                              length /* changed member */ );
367
368       ip->checksum = ip_csum_fold (sum0);
369       ip->length = new_l0;
370       udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
371
372       if (exp->udp_checksum)
373         {
374           /* RFC 7011 section 10.3.2. */
375           udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
376           if (udp->checksum == 0)
377             udp->checksum = 0xffff;
378         }
379
380       ASSERT (ip4_header_checksum_is_valid (ip));
381
382       to_next[0] = bi0;
383       f->n_vectors++;
384
385       b0 = 0;
386       bi0 = ~0;
387     }
388
389   clib_spinlock_unlock (&t->writer_lock);
390   return f;
391 }
392
393 static clib_error_t *
394 ipfix_classify_table_add_del_command_fn (vlib_main_t * vm,
395                                          unformat_input_t * input,
396                                          vlib_cli_command_t * cmd)
397 {
398   flow_report_classify_main_t *fcm = &flow_report_classify_main;
399   ipfix_exporter_t *exp = &flow_report_main.exporters[0];
400   vnet_flow_report_add_del_args_t args;
401   ipfix_classify_table_t *table;
402   int rv;
403   int is_add = -1;
404   u32 classify_table_index = ~0;
405   u8 ip_version = 0;
406   u8 transport_protocol = 255;
407   clib_error_t *error = 0;
408
409   if (fcm->src_port == 0)
410     clib_error_return (0, "call 'set ipfix classify stream' first");
411
412   clib_memset (&args, 0, sizeof (args));
413
414   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
415     {
416       if (unformat (input, "add"))
417         is_add = 1;
418       else if (unformat (input, "del"))
419         is_add = 0;
420       else if (unformat (input, "%d", &classify_table_index))
421         ;
422       else if (unformat (input, "ip4"))
423         ip_version = 4;
424       else if (unformat (input, "ip6"))
425         ip_version = 6;
426       else if (unformat (input, "tcp"))
427         transport_protocol = 6;
428       else if (unformat (input, "udp"))
429         transport_protocol = 17;
430       else
431         return clib_error_return (0, "unknown input `%U'",
432                                   format_unformat_error, input);
433     }
434
435   if (is_add == -1)
436     return clib_error_return (0, "expecting: add|del");
437   if (classify_table_index == ~0)
438     return clib_error_return (0, "classifier table not specified");
439   if (ip_version == 0)
440     return clib_error_return (0, "IP version not specified");
441
442   table = 0;
443   int i;
444   for (i = 0; i < vec_len (fcm->tables); i++)
445     if (ipfix_classify_table_index_valid (i))
446       if (fcm->tables[i].classify_table_index == classify_table_index)
447         {
448           table = &fcm->tables[i];
449           break;
450         }
451
452   if (is_add)
453     {
454       if (table)
455         return clib_error_return (0,
456                                   "Specified classifier table already used");
457       table = ipfix_classify_add_table ();
458       table->classify_table_index = classify_table_index;
459     }
460   else
461     {
462       if (!table)
463         return clib_error_return (0,
464                                   "Specified classifier table not registered");
465     }
466
467   table->ip_version = ip_version;
468   table->transport_protocol = transport_protocol;
469
470   args.opaque.as_uword = table - fcm->tables;
471   args.rewrite_callback = ipfix_classify_template_rewrite;
472   args.flow_data_callback = ipfix_classify_send_flows;
473   args.is_add = is_add;
474   args.domain_id = fcm->domain_id;
475   args.src_port = fcm->src_port;
476
477   rv = vnet_flow_report_add_del (exp, &args, NULL);
478
479   error = flow_report_add_del_error_to_clib_error (rv);
480
481   /* If deleting, or add failed */
482   if (is_add == 0 || (rv && is_add))
483     ipfix_classify_delete_table (table - fcm->tables);
484
485   return error;
486 }
487
488 /* *INDENT-OFF* */
489 VLIB_CLI_COMMAND (ipfix_classify_table_add_del_command, static) = {
490   .path = "ipfix classify table",
491   .short_help = "ipfix classify table add|del <table-index>",
492   .function = ipfix_classify_table_add_del_command_fn,
493 };
494 /* *INDENT-ON* */
495
496 static clib_error_t *
497 set_ipfix_classify_stream_command_fn (vlib_main_t * vm,
498                                       unformat_input_t * input,
499                                       vlib_cli_command_t * cmd)
500 {
501   flow_report_classify_main_t *fcm = &flow_report_classify_main;
502   ipfix_exporter_t *exp = &flow_report_main.exporters[0];
503   u32 domain_id = 1;
504   u32 src_port = UDP_DST_PORT_ipfix;
505
506   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
507     {
508       if (unformat (input, "domain %d", &domain_id))
509         ;
510       else if (unformat (input, "src-port %d", &src_port))
511         ;
512       else
513         return clib_error_return (0, "unknown input `%U'",
514                                   format_unformat_error, input);
515     }
516
517   if (fcm->src_port != 0 &&
518       (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
519     {
520       int rv = vnet_stream_change (exp, fcm->domain_id, fcm->src_port,
521                                    domain_id, (u16) src_port);
522       ASSERT (rv == 0);
523     }
524
525   fcm->domain_id = domain_id;
526   fcm->src_port = (u16) src_port;
527
528   return 0;
529 }
530
531 /* *INDENT-OFF* */
532 VLIB_CLI_COMMAND (set_ipfix_classify_stream_command, static) = {
533   .path = "set ipfix classify stream",
534   .short_help = "set ipfix classify stream"
535                 "[domain <domain-id>] [src-port <src-port>]",
536   .function = set_ipfix_classify_stream_command_fn,
537 };
538 /* *INDENT-ON* */
539
540 static clib_error_t *
541 flow_report_classify_init (vlib_main_t * vm)
542 {
543   clib_error_t *error;
544
545   if ((error = vlib_call_init_function (vm, flow_report_init)))
546     return error;
547
548   return 0;
549 }
550
551 VLIB_INIT_FUNCTION (flow_report_classify_init);
552
553 /*
554  * fd.io coding-style-patch-verification: ON
555  *
556  * Local Variables:
557  * eval: (c-set-style "gnu")
558  * End:
559  */