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 (flow_report_main_t * frm,
34 ip4_address_t * collector_address,
35 ip4_address_t * src_address,
37 ipfix_report_element_t * elts,
38 u32 n_elts, u32 * stream_index)
40 flow_report_classify_main_t *fcm = &flow_report_classify_main;
41 vnet_classify_table_t *tblp;
42 vnet_classify_main_t *vcm = &vnet_classify_main;
43 u32 flow_table_index = fr->opaque.as_uword;
47 tcpudp_header_t *tcpudp;
49 ipfix_message_header_t *h;
50 ipfix_set_header_t *s;
51 ipfix_template_header_t *t;
52 ipfix_field_specifier_t *f;
53 ipfix_field_specifier_t *first_field;
55 ip4_ipfix_template_packet_t *tp;
58 flow_report_stream_t *stream;
60 u8 transport_protocol;
64 stream = &frm->streams[fr->stream_index];
66 ipfix_classify_table_t *table = &fcm->tables[flow_table_index];
68 ip_version = table->ip_version;
69 transport_protocol = table->transport_protocol;
71 tblp = pool_elt_at_index (vcm->tables, table->classify_table_index);
73 virt_mask = (u8 *) (tblp->mask - tblp->skip_n_vectors);
74 real_mask = (u8 *) (tblp->mask);
76 /* Determine field count */
77 ip_start = virt_mask + sizeof (ethernet_header_t);
78 #define _(field,mask,item,length) \
79 if (((u8 *)&field >= real_mask) && (memcmp(&field, &mask, length) == 0)) \
83 fr->fields_to_send = clib_bitmap_set (fr->fields_to_send, \
90 /* Add packetTotalCount manually */
93 /* $$$ enterprise fields, at some later date */
95 /* allocate rewrite space */
96 vec_validate_aligned (rewrite,
97 sizeof (ip4_ipfix_template_packet_t)
98 + field_count * sizeof (ipfix_field_specifier_t) - 1,
99 CLIB_CACHE_LINE_BYTES);
101 tp = (ip4_ipfix_template_packet_t *) rewrite;
102 ip = (ip4_header_t *) & tp->ip4;
103 udp = (udp_header_t *) (ip + 1);
104 h = (ipfix_message_header_t *) (udp + 1);
105 s = (ipfix_set_header_t *) (h + 1);
106 t = (ipfix_template_header_t *) (s + 1);
107 first_field = f = (ipfix_field_specifier_t *) (t + 1);
109 ip->ip_version_and_header_length = 0x45;
111 ip->protocol = IP_PROTOCOL_UDP;
112 ip->src_address.as_u32 = src_address->as_u32;
113 ip->dst_address.as_u32 = collector_address->as_u32;
114 udp->src_port = clib_host_to_net_u16 (stream->src_port);
115 udp->dst_port = clib_host_to_net_u16 (collector_port);
116 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
118 /* FIXUP: message header export_time */
119 /* FIXUP: message header sequence_number */
120 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
122 /* Take another trip through the mask and build the template */
123 ip_start = virt_mask + sizeof (ethernet_header_t);
124 #define _(field,mask,item,length) \
125 if (((u8 *)&field >= real_mask) && (memcmp(&field, &mask, length) == 0)) \
127 f->e_id_length = ipfix_e_id_length (0 /* enterprise */, \
134 /* Add packetTotalCount manually */
136 ipfix_e_id_length (0 /* enterprise */ , packetTotalCount, 8);
139 /* Back to the template packet... */
140 ip = (ip4_header_t *) & tp->ip4;
141 udp = (udp_header_t *) (ip + 1);
143 ASSERT (f - first_field);
144 /* Field count in this template */
145 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
147 /* set length in octets */
149 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
151 /* message length in octets */
152 h->version_length = version_length ((u8 *) f - (u8 *) h);
154 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
155 ip->checksum = ip4_header_checksum (ip);
161 ipfix_classify_send_flows (flow_report_main_t * frm,
163 vlib_frame_t * f, u32 * to_next, u32 node_index)
165 flow_report_classify_main_t *fcm = &flow_report_classify_main;
166 vnet_classify_main_t *vcm = &vnet_classify_main;
167 u32 flow_table_index = fr->opaque.as_uword;
168 vnet_classify_table_t *t;
169 vnet_classify_bucket_t *b;
170 vnet_classify_entry_t *v, *save_v;
171 vlib_buffer_t *b0 = 0;
173 u32 record_offset = 0;
176 ip4_ipfix_template_packet_t *tp;
177 ipfix_message_header_t *h = 0;
178 ipfix_set_header_t *s = 0;
182 tcpudp_header_t *tcpudp;
185 u32 records_this_buffer;
188 vlib_main_t *vm = frm->vlib_main;
189 flow_report_stream_t *stream;
191 u8 transport_protocol;
194 stream = &frm->streams[fr->stream_index];
196 ipfix_classify_table_t *table = &fcm->tables[flow_table_index];
198 ip_version = table->ip_version;
199 transport_protocol = table->transport_protocol;
201 t = pool_elt_at_index (vcm->tables, table->classify_table_index);
203 clib_spinlock_lock (&t->writer_lock);
205 for (i = 0; i < t->nbuckets; i++)
211 save_v = vnet_classify_get_entry (t, b->offset);
212 for (j = 0; j < (1 << b->log2_pages); j++)
214 for (k = 0; k < t->entries_per_page; k++)
216 v = vnet_classify_entry_at_index
217 (t, save_v, j * t->entries_per_page + k);
219 if (vnet_classify_entry_is_free (v))
222 /* OK, we have something to send... */
223 if (PREDICT_FALSE (b0 == 0))
225 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
227 b0 = vlib_get_buffer (vm, bi0);
229 u32 copy_len = sizeof (ip4_header_t) +
230 sizeof (udp_header_t) + sizeof (ipfix_message_header_t);
231 clib_memcpy_fast (b0->data, fr->rewrite, copy_len);
232 b0->current_data = 0;
233 b0->current_length = copy_len;
234 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
235 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
236 vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
238 tp = vlib_buffer_get_current (b0);
239 ip = (ip4_header_t *) & tp->ip4;
240 udp = (udp_header_t *) (ip + 1);
241 h = (ipfix_message_header_t *) (udp + 1);
242 s = (ipfix_set_header_t *) (h + 1);
244 /* FIXUP: message header export_time */
245 h->export_time = (u32)
246 (((f64) frm->unix_time_0) +
247 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
248 h->export_time = clib_host_to_net_u32 (h->export_time);
250 /* FIXUP: message header sequence_number */
251 h->sequence_number = stream->sequence_number;
253 clib_host_to_net_u32 (h->sequence_number);
255 next_offset = (u32) (((u8 *) (s + 1)) - (u8 *) tp);
256 record_offset = next_offset;
257 records_this_buffer = 0;
261 virt_key = (u8 *) (v->key - t->skip_n_vectors);
262 ip_start = virt_key + sizeof (ethernet_header_t);
263 #define _(field,mask,item,length) \
264 if (clib_bitmap_get (fr->fields_to_send, field_index)) \
266 clib_memcpy_fast (b0->data + next_offset, &field, \
268 next_offset += length; \
274 /* Add packetTotalCount manually */
276 u64 packets = clib_host_to_net_u64 (v->hits);
277 clib_memcpy_fast (b0->data + next_offset, &packets,
279 next_offset += sizeof (packets);
281 records_this_buffer++;
282 stream->sequence_number++;
284 /* Next record will have the same size as this record */
285 u32 next_record_size = next_offset - record_offset;
286 record_offset = next_offset;
288 if (next_offset + next_record_size > frm->path_mtu)
290 s->set_id_length = ipfix_set_id_length (fr->template_id,
296 version_length (next_offset -
297 (sizeof (*ip) + sizeof (*udp)));
298 b0->current_length = next_offset;
299 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
301 tp = vlib_buffer_get_current (b0);
302 ip = (ip4_header_t *) & tp->ip4;
303 udp = (udp_header_t *) (ip + 1);
307 new_l0 = clib_host_to_net_u16 ((u16) next_offset);
309 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
310 length /* changed member */ );
312 ip->checksum = ip_csum_fold (sum0);
315 clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
317 if (frm->udp_checksum)
319 /* RFC 7011 section 10.3.2. */
321 ip4_tcp_udp_compute_checksum (vm, b0, ip);
322 if (udp->checksum == 0)
323 udp->checksum = 0xffff;
326 ASSERT (ip4_header_checksum_is_valid (ip));
332 if (f->n_vectors == VLIB_FRAME_SIZE)
334 vlib_put_frame_to_node (vm, node_index, f);
335 f = vlib_get_frame_to_node (vm, node_index);
337 to_next = vlib_frame_vector_args (f);
349 s->set_id_length = ipfix_set_id_length (fr->template_id,
351 (sizeof (*ip) + sizeof (*udp) +
353 h->version_length = version_length (next_offset -
354 (sizeof (*ip) + sizeof (*udp)));
355 b0->current_length = next_offset;
356 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
358 tp = vlib_buffer_get_current (b0);
359 ip = (ip4_header_t *) & tp->ip4;
360 udp = (udp_header_t *) (ip + 1);
364 new_l0 = clib_host_to_net_u16 ((u16) next_offset);
366 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
367 length /* changed member */ );
369 ip->checksum = ip_csum_fold (sum0);
371 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
373 if (frm->udp_checksum)
375 /* RFC 7011 section 10.3.2. */
376 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
377 if (udp->checksum == 0)
378 udp->checksum = 0xffff;
381 ASSERT (ip4_header_checksum_is_valid (ip));
390 clib_spinlock_unlock (&t->writer_lock);
394 static clib_error_t *
395 ipfix_classify_table_add_del_command_fn (vlib_main_t * vm,
396 unformat_input_t * input,
397 vlib_cli_command_t * cmd)
399 flow_report_classify_main_t *fcm = &flow_report_classify_main;
400 flow_report_main_t *frm = &flow_report_main;
401 vnet_flow_report_add_del_args_t args;
402 ipfix_classify_table_t *table;
405 u32 classify_table_index = ~0;
407 u8 transport_protocol = 255;
408 clib_error_t *error = 0;
410 if (fcm->src_port == 0)
411 clib_error_return (0, "call 'set ipfix classify stream' first");
413 clib_memset (&args, 0, sizeof (args));
415 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
417 if (unformat (input, "add"))
419 else if (unformat (input, "del"))
421 else if (unformat (input, "%d", &classify_table_index))
423 else if (unformat (input, "ip4"))
425 else if (unformat (input, "ip6"))
427 else if (unformat (input, "tcp"))
428 transport_protocol = 6;
429 else if (unformat (input, "udp"))
430 transport_protocol = 17;
432 return clib_error_return (0, "unknown input `%U'",
433 format_unformat_error, input);
437 return clib_error_return (0, "expecting: add|del");
438 if (classify_table_index == ~0)
439 return clib_error_return (0, "classifier table not specified");
441 return clib_error_return (0, "IP version not specified");
445 for (i = 0; i < vec_len (fcm->tables); i++)
446 if (ipfix_classify_table_index_valid (i))
447 if (fcm->tables[i].classify_table_index == classify_table_index)
449 table = &fcm->tables[i];
456 return clib_error_return (0,
457 "Specified classifier table already used");
458 table = ipfix_classify_add_table ();
459 table->classify_table_index = classify_table_index;
464 return clib_error_return (0,
465 "Specified classifier table not registered");
468 table->ip_version = ip_version;
469 table->transport_protocol = transport_protocol;
471 args.opaque.as_uword = table - fcm->tables;
472 args.rewrite_callback = ipfix_classify_template_rewrite;
473 args.flow_data_callback = ipfix_classify_send_flows;
474 args.is_add = is_add;
475 args.domain_id = fcm->domain_id;
476 args.src_port = fcm->src_port;
478 rv = vnet_flow_report_add_del (frm, &args, NULL);
480 error = flow_report_add_del_error_to_clib_error (rv);
482 /* If deleting, or add failed */
483 if (is_add == 0 || (rv && is_add))
484 ipfix_classify_delete_table (table - fcm->tables);
490 VLIB_CLI_COMMAND (ipfix_classify_table_add_del_command, static) = {
491 .path = "ipfix classify table",
492 .short_help = "ipfix classify table add|del <table-index>",
493 .function = ipfix_classify_table_add_del_command_fn,
497 static clib_error_t *
498 set_ipfix_classify_stream_command_fn (vlib_main_t * vm,
499 unformat_input_t * input,
500 vlib_cli_command_t * cmd)
502 flow_report_classify_main_t *fcm = &flow_report_classify_main;
503 flow_report_main_t *frm = &flow_report_main;
505 u32 src_port = UDP_DST_PORT_ipfix;
507 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
509 if (unformat (input, "domain %d", &domain_id))
511 else if (unformat (input, "src-port %d", &src_port))
514 return clib_error_return (0, "unknown input `%U'",
515 format_unformat_error, input);
518 if (fcm->src_port != 0 &&
519 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
521 int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
522 domain_id, (u16) src_port);
526 fcm->domain_id = domain_id;
527 fcm->src_port = (u16) src_port;
533 VLIB_CLI_COMMAND (set_ipfix_classify_stream_command, static) = {
534 .path = "set ipfix classify stream",
535 .short_help = "set ipfix classify stream"
536 "[domain <domain-id>] [src-port <src-port>]",
537 .function = set_ipfix_classify_stream_command_fn,
541 static clib_error_t *
542 flow_report_classify_init (vlib_main_t * vm)
546 if ((error = vlib_call_init_function (vm, flow_report_init)))
552 VLIB_INIT_FUNCTION (flow_report_classify_init);
555 * fd.io coding-style-patch-verification: ON
558 * eval: (c-set-style "gnu")