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>
19 /* Common prefix of tcp and udp headers
20 * containing only source and destination port fields */
23 u16 src_port, dst_port;
26 flow_report_classify_main_t flow_report_classify_main;
29 ipfix_classify_template_rewrite (flow_report_main_t * frm,
31 ip4_address_t * collector_address,
32 ip4_address_t * src_address,
34 ipfix_report_element_t * elts,
35 u32 n_elts, u32 * stream_index)
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;
44 tcpudp_header_t *tcpudp;
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;
52 ip4_ipfix_template_packet_t *tp;
55 flow_report_stream_t *stream;
57 u8 transport_protocol;
61 stream = &frm->streams[fr->stream_index];
63 ipfix_classify_table_t *table = &fcm->tables[flow_table_index];
65 ip_version = table->ip_version;
66 transport_protocol = table->transport_protocol;
68 tblp = pool_elt_at_index (vcm->tables, table->classify_table_index);
70 virt_mask = (u8 *) (tblp->mask - tblp->skip_n_vectors);
71 real_mask = (u8 *) (tblp->mask);
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)) \
80 fr->fields_to_send = clib_bitmap_set (fr->fields_to_send, \
87 /* Add packetTotalCount manually */
90 /* $$$ enterprise fields, at some later date */
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);
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);
106 ip->ip_version_and_header_length = 0x45;
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));
115 /* FIXUP: message header export_time */
116 /* FIXUP: message header sequence_number */
117 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
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)) \
124 f->e_id_length = ipfix_e_id_length (0 /* enterprise */, \
131 /* Add packetTotalCount manually */
133 ipfix_e_id_length (0 /* enterprise */ , packetTotalCount, 8);
136 /* Back to the template packet... */
137 ip = (ip4_header_t *) & tp->ip4;
138 udp = (udp_header_t *) (ip + 1);
140 ASSERT (f - first_field);
141 /* Field count in this template */
142 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
144 /* set length in octets */
146 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
148 /* message length in octets */
149 h->version_length = version_length ((u8 *) f - (u8 *) h);
151 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
152 ip->checksum = ip4_header_checksum (ip);
158 ipfix_classify_send_flows (flow_report_main_t * frm,
160 vlib_frame_t * f, u32 * to_next, u32 node_index)
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;
170 u32 record_offset = 0;
173 ip4_ipfix_template_packet_t *tp;
174 ipfix_message_header_t *h = 0;
175 ipfix_set_header_t *s = 0;
179 tcpudp_header_t *tcpudp;
182 u32 records_this_buffer;
185 vlib_main_t *vm = frm->vlib_main;
186 flow_report_stream_t *stream;
188 u8 transport_protocol;
191 stream = &frm->streams[fr->stream_index];
193 ipfix_classify_table_t *table = &fcm->tables[flow_table_index];
195 ip_version = table->ip_version;
196 transport_protocol = table->transport_protocol;
198 t = pool_elt_at_index (vcm->tables, table->classify_table_index);
200 while (clib_atomic_test_and_set (t->writer_lock))
203 for (i = 0; i < t->nbuckets; i++)
209 save_v = vnet_classify_get_entry (t, b->offset);
210 for (j = 0; j < (1 << b->log2_pages); j++)
212 for (k = 0; k < t->entries_per_page; k++)
214 v = vnet_classify_entry_at_index
215 (t, save_v, j * t->entries_per_page + k);
217 if (vnet_classify_entry_is_free (v))
220 /* OK, we have something to send... */
221 if (PREDICT_FALSE (b0 == 0))
223 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
225 b0 = vlib_get_buffer (vm, bi0);
227 u32 copy_len = sizeof (ip4_header_t) +
228 sizeof (udp_header_t) + sizeof (ipfix_message_header_t);
229 clib_memcpy (b0->data, fr->rewrite, copy_len);
230 b0->current_data = 0;
231 b0->current_length = copy_len;
232 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
233 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
234 vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
236 tp = vlib_buffer_get_current (b0);
237 ip = (ip4_header_t *) & tp->ip4;
238 udp = (udp_header_t *) (ip + 1);
239 h = (ipfix_message_header_t *) (udp + 1);
240 s = (ipfix_set_header_t *) (h + 1);
242 /* FIXUP: message header export_time */
243 h->export_time = (u32)
244 (((f64) frm->unix_time_0) +
245 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
246 h->export_time = clib_host_to_net_u32 (h->export_time);
248 /* FIXUP: message header sequence_number */
249 h->sequence_number = stream->sequence_number;
251 clib_host_to_net_u32 (h->sequence_number);
253 next_offset = (u32) (((u8 *) (s + 1)) - (u8 *) tp);
254 record_offset = next_offset;
255 records_this_buffer = 0;
259 virt_key = (u8 *) (v->key - t->skip_n_vectors);
260 ip_start = virt_key + sizeof (ethernet_header_t);
261 #define _(field,mask,item,length) \
262 if (clib_bitmap_get (fr->fields_to_send, field_index)) \
264 clib_memcpy (b0->data + next_offset, &field, \
266 next_offset += length; \
272 /* Add packetTotalCount manually */
274 u64 packets = clib_host_to_net_u64 (v->hits);
275 clib_memcpy (b0->data + next_offset, &packets,
277 next_offset += sizeof (packets);
279 records_this_buffer++;
280 stream->sequence_number++;
282 /* Next record will have the same size as this record */
283 u32 next_record_size = next_offset - record_offset;
284 record_offset = next_offset;
286 if (next_offset + next_record_size > frm->path_mtu)
288 s->set_id_length = ipfix_set_id_length (fr->template_id,
294 version_length (next_offset -
295 (sizeof (*ip) + sizeof (*udp)));
296 b0->current_length = next_offset;
297 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
299 tp = vlib_buffer_get_current (b0);
300 ip = (ip4_header_t *) & tp->ip4;
301 udp = (udp_header_t *) (ip + 1);
305 new_l0 = clib_host_to_net_u16 ((u16) next_offset);
307 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
308 length /* changed member */ );
310 ip->checksum = ip_csum_fold (sum0);
313 clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
315 if (frm->udp_checksum)
317 /* RFC 7011 section 10.3.2. */
319 ip4_tcp_udp_compute_checksum (vm, b0, ip);
320 if (udp->checksum == 0)
321 udp->checksum = 0xffff;
324 ASSERT (ip->checksum == ip4_header_checksum (ip));
330 if (f->n_vectors == VLIB_FRAME_SIZE)
332 vlib_put_frame_to_node (vm, node_index, f);
333 f = vlib_get_frame_to_node (vm, node_index);
335 to_next = vlib_frame_vector_args (f);
347 s->set_id_length = ipfix_set_id_length (fr->template_id,
349 (sizeof (*ip) + sizeof (*udp) +
351 h->version_length = version_length (next_offset -
352 (sizeof (*ip) + sizeof (*udp)));
353 b0->current_length = next_offset;
354 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
356 tp = vlib_buffer_get_current (b0);
357 ip = (ip4_header_t *) & tp->ip4;
358 udp = (udp_header_t *) (ip + 1);
362 new_l0 = clib_host_to_net_u16 ((u16) next_offset);
364 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
365 length /* changed member */ );
367 ip->checksum = ip_csum_fold (sum0);
369 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
371 if (frm->udp_checksum)
373 /* RFC 7011 section 10.3.2. */
374 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
375 if (udp->checksum == 0)
376 udp->checksum = 0xffff;
379 ASSERT (ip->checksum == ip4_header_checksum (ip));
388 clib_atomic_release (t->writer_lock);
392 static clib_error_t *
393 ipfix_classify_table_add_del_command_fn (vlib_main_t * vm,
394 unformat_input_t * input,
395 vlib_cli_command_t * cmd)
397 flow_report_classify_main_t *fcm = &flow_report_classify_main;
398 flow_report_main_t *frm = &flow_report_main;
399 vnet_flow_report_add_del_args_t args;
400 ipfix_classify_table_t *table;
403 u32 classify_table_index = ~0;
405 u8 transport_protocol = 255;
406 clib_error_t *error = 0;
408 if (fcm->src_port == 0)
409 clib_error_return (0, "call 'set ipfix classify stream' first");
411 clib_memset (&args, 0, sizeof (args));
413 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
415 if (unformat (input, "add"))
417 else if (unformat (input, "del"))
419 else if (unformat (input, "%d", &classify_table_index))
421 else if (unformat (input, "ip4"))
423 else if (unformat (input, "ip6"))
425 else if (unformat (input, "tcp"))
426 transport_protocol = 6;
427 else if (unformat (input, "udp"))
428 transport_protocol = 17;
430 return clib_error_return (0, "unknown input `%U'",
431 format_unformat_error, input);
435 return clib_error_return (0, "expecting: add|del");
436 if (classify_table_index == ~0)
437 return clib_error_return (0, "classifier table not specified");
439 return clib_error_return (0, "IP version not specified");
443 for (i = 0; i < vec_len (fcm->tables); i++)
444 if (ipfix_classify_table_index_valid (i))
445 if (fcm->tables[i].classify_table_index == classify_table_index)
447 table = &fcm->tables[i];
454 return clib_error_return (0,
455 "Specified classifier table already used");
456 table = ipfix_classify_add_table ();
457 table->classify_table_index = classify_table_index;
462 return clib_error_return (0,
463 "Specified classifier table not registered");
466 table->ip_version = ip_version;
467 table->transport_protocol = transport_protocol;
469 args.opaque.as_uword = table - fcm->tables;
470 args.rewrite_callback = ipfix_classify_template_rewrite;
471 args.flow_data_callback = ipfix_classify_send_flows;
472 args.is_add = is_add;
473 args.domain_id = fcm->domain_id;
474 args.src_port = fcm->src_port;
476 rv = vnet_flow_report_add_del (frm, &args, NULL);
478 error = flow_report_add_del_error_to_clib_error (rv);
480 /* If deleting, or add failed */
481 if (is_add == 0 || (rv && is_add))
482 ipfix_classify_delete_table (table - fcm->tables);
488 VLIB_CLI_COMMAND (ipfix_classify_table_add_del_command, static) = {
489 .path = "ipfix classify table",
490 .short_help = "ipfix classify table add|del <table-index>",
491 .function = ipfix_classify_table_add_del_command_fn,
495 static clib_error_t *
496 set_ipfix_classify_stream_command_fn (vlib_main_t * vm,
497 unformat_input_t * input,
498 vlib_cli_command_t * cmd)
500 flow_report_classify_main_t *fcm = &flow_report_classify_main;
501 flow_report_main_t *frm = &flow_report_main;
503 u32 src_port = UDP_DST_PORT_ipfix;
505 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
507 if (unformat (input, "domain %d", &domain_id))
509 else if (unformat (input, "src-port %d", &src_port))
512 return clib_error_return (0, "unknown input `%U'",
513 format_unformat_error, input);
516 if (fcm->src_port != 0 &&
517 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
519 int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
520 domain_id, (u16) src_port);
524 fcm->domain_id = domain_id;
525 fcm->src_port = (u16) src_port;
531 VLIB_CLI_COMMAND (set_ipfix_classify_stream_command, static) = {
532 .path = "set ipfix classify stream",
533 .short_help = "set ipfix classify stream"
534 "[domain <domain-id>] [src-port <src-port>]",
535 .function = set_ipfix_classify_stream_command_fn,
539 static clib_error_t *
540 flow_report_classify_init (vlib_main_t * vm)
544 if ((error = vlib_call_init_function (vm, flow_report_init)))
550 VLIB_INIT_FUNCTION (flow_report_classify_init);
553 * fd.io coding-style-patch-verification: ON
556 * eval: (c-set-style "gnu")