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