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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
22 /* Common prefix of tcp and udp headers
23 * containing only source and destination port fields */
26 u16 src_port, dst_port;
29 flow_report_classify_main_t flow_report_classify_main;
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,
36 ipfix_report_element_t *elts, u32 n_elts,
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;
46 tcpudp_header_t *tcpudp;
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;
54 ip4_ipfix_template_packet_t *tp;
57 flow_report_stream_t *stream;
59 u8 transport_protocol;
63 stream = &exp->streams[fr->stream_index];
65 ipfix_classify_table_t *table = &fcm->tables[flow_table_index];
67 ip_version = table->ip_version;
68 transport_protocol = table->transport_protocol;
70 tblp = pool_elt_at_index (vcm->tables, table->classify_table_index);
72 virt_mask = (u8 *) (tblp->mask - tblp->skip_n_vectors);
73 real_mask = (u8 *) (tblp->mask);
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)) \
82 fr->fields_to_send = clib_bitmap_set (fr->fields_to_send, \
89 /* Add packetTotalCount manually */
92 /* $$$ enterprise fields, at some later date */
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);
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);
108 ip->ip_version_and_header_length = 0x45;
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));
117 /* FIXUP: message header export_time */
118 /* FIXUP: message header sequence_number */
119 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
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)) \
126 f->e_id_length = ipfix_e_id_length (0 /* enterprise */, \
133 /* Add packetTotalCount manually */
135 ipfix_e_id_length (0 /* enterprise */ , packetTotalCount, 8);
138 /* Back to the template packet... */
139 ip = (ip4_header_t *) & tp->ip4;
140 udp = (udp_header_t *) (ip + 1);
142 ASSERT (f - first_field);
143 /* Field count in this template */
144 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
146 /* set length in octets */
148 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
150 /* message length in octets */
151 h->version_length = version_length ((u8 *) f - (u8 *) h);
153 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
154 ip->checksum = ip4_header_checksum (ip);
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,
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;
172 u32 record_offset = 0;
175 ip4_ipfix_template_packet_t *tp;
176 ipfix_message_header_t *h = 0;
177 ipfix_set_header_t *s = 0;
181 tcpudp_header_t *tcpudp;
184 u32 records_this_buffer;
187 vlib_main_t *vm = frm->vlib_main;
188 flow_report_stream_t *stream;
190 u8 transport_protocol;
193 stream = &exp->streams[fr->stream_index];
195 ipfix_classify_table_t *table = &fcm->tables[flow_table_index];
197 ip_version = table->ip_version;
198 transport_protocol = table->transport_protocol;
200 t = pool_elt_at_index (vcm->tables, table->classify_table_index);
202 clib_spinlock_lock (&t->writer_lock);
204 for (i = 0; i < t->nbuckets; i++)
210 save_v = vnet_classify_get_entry (t, b->offset);
211 for (j = 0; j < (1 << b->log2_pages); j++)
213 for (k = 0; k < t->entries_per_page; k++)
215 v = vnet_classify_entry_at_index
216 (t, save_v, j * t->entries_per_page + k);
218 if (vnet_classify_entry_is_free (v))
221 /* OK, we have something to send... */
222 if (PREDICT_FALSE (b0 == 0))
224 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
226 b0 = vlib_get_buffer (vm, bi0);
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;
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);
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);
249 /* FIXUP: message header sequence_number */
250 h->sequence_number = stream->sequence_number;
252 clib_host_to_net_u32 (h->sequence_number);
254 next_offset = (u32) (((u8 *) (s + 1)) - (u8 *) tp);
255 record_offset = next_offset;
256 records_this_buffer = 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)) \
265 clib_memcpy_fast (b0->data + next_offset, &field, \
267 next_offset += length; \
273 /* Add packetTotalCount manually */
275 u64 packets = clib_host_to_net_u64 (v->hits);
276 clib_memcpy_fast (b0->data + next_offset, &packets,
278 next_offset += sizeof (packets);
280 records_this_buffer++;
281 stream->sequence_number++;
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;
287 if (next_offset + next_record_size > exp->path_mtu)
289 s->set_id_length = ipfix_set_id_length (fr->template_id,
295 version_length (next_offset -
296 (sizeof (*ip) + sizeof (*udp)));
297 b0->current_length = next_offset;
298 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
300 tp = vlib_buffer_get_current (b0);
301 ip = (ip4_header_t *) & tp->ip4;
302 udp = (udp_header_t *) (ip + 1);
306 new_l0 = clib_host_to_net_u16 ((u16) next_offset);
308 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
309 length /* changed member */ );
311 ip->checksum = ip_csum_fold (sum0);
314 clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
316 if (exp->udp_checksum)
318 /* RFC 7011 section 10.3.2. */
320 ip4_tcp_udp_compute_checksum (vm, b0, ip);
321 if (udp->checksum == 0)
322 udp->checksum = 0xffff;
325 ASSERT (ip4_header_checksum_is_valid (ip));
331 if (f->n_vectors == VLIB_FRAME_SIZE)
333 vlib_put_frame_to_node (vm, node_index, f);
334 f = vlib_get_frame_to_node (vm, node_index);
336 to_next = vlib_frame_vector_args (f);
348 s->set_id_length = ipfix_set_id_length (fr->template_id,
350 (sizeof (*ip) + sizeof (*udp) +
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;
357 tp = vlib_buffer_get_current (b0);
358 ip = (ip4_header_t *) & tp->ip4;
359 udp = (udp_header_t *) (ip + 1);
363 new_l0 = clib_host_to_net_u16 ((u16) next_offset);
365 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
366 length /* changed member */ );
368 ip->checksum = ip_csum_fold (sum0);
370 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
372 if (exp->udp_checksum)
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;
380 ASSERT (ip4_header_checksum_is_valid (ip));
389 clib_spinlock_unlock (&t->writer_lock);
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)
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;
404 u32 classify_table_index = ~0;
406 u8 transport_protocol = 255;
407 clib_error_t *error = 0;
409 if (fcm->src_port == 0)
410 clib_error_return (0, "call 'set ipfix classify stream' first");
412 clib_memset (&args, 0, sizeof (args));
414 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
416 if (unformat (input, "add"))
418 else if (unformat (input, "del"))
420 else if (unformat (input, "%d", &classify_table_index))
422 else if (unformat (input, "ip4"))
424 else if (unformat (input, "ip6"))
426 else if (unformat (input, "tcp"))
427 transport_protocol = 6;
428 else if (unformat (input, "udp"))
429 transport_protocol = 17;
431 return clib_error_return (0, "unknown input `%U'",
432 format_unformat_error, input);
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");
440 return clib_error_return (0, "IP version not specified");
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)
448 table = &fcm->tables[i];
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;
463 return clib_error_return (0,
464 "Specified classifier table not registered");
467 table->ip_version = ip_version;
468 table->transport_protocol = transport_protocol;
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;
477 rv = vnet_flow_report_add_del (exp, &args, NULL);
479 error = flow_report_add_del_error_to_clib_error (rv);
481 /* If deleting, or add failed */
482 if (is_add == 0 || (rv && is_add))
483 ipfix_classify_delete_table (table - fcm->tables);
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,
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)
501 flow_report_classify_main_t *fcm = &flow_report_classify_main;
502 ipfix_exporter_t *exp = &flow_report_main.exporters[0];
504 u32 src_port = UDP_DST_PORT_ipfix;
506 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
508 if (unformat (input, "domain %d", &domain_id))
510 else if (unformat (input, "src-port %d", &src_port))
513 return clib_error_return (0, "unknown input `%U'",
514 format_unformat_error, input);
517 if (fcm->src_port != 0 &&
518 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
520 int rv = vnet_stream_change (exp, fcm->domain_id, fcm->src_port,
521 domain_id, (u16) src_port);
525 fcm->domain_id = domain_id;
526 fcm->src_port = (u16) src_port;
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,
540 static clib_error_t *
541 flow_report_classify_init (vlib_main_t * vm)
545 if ((error = vlib_call_init_function (vm, flow_report_init)))
551 VLIB_INIT_FUNCTION (flow_report_classify_init);
554 * fd.io coding-style-patch-verification: ON
557 * eval: (c-set-style "gnu")