Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | /* |
| 16 | * flow_report.c |
| 17 | */ |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 18 | #include <vppinfra/atomics.h> |
Ole Troan | a9855ef | 2018-05-02 12:45:10 +0200 | [diff] [blame] | 19 | #include <vnet/ipfix-export/flow_report.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | #include <vnet/api_errno.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 21 | #include <vnet/udp/udp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | |
Juraj Sloboda | 837fbb1 | 2016-07-06 23:11:47 -0700 | [diff] [blame] | 23 | flow_report_main_t flow_report_main; |
| 24 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 25 | static_always_inline u8 |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 26 | stream_index_valid (ipfix_exporter_t *exp, u32 index) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 27 | { |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 28 | return index < vec_len (exp->streams) && exp->streams[index].domain_id != ~0; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 31 | static_always_inline flow_report_stream_t * |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 32 | add_stream (ipfix_exporter_t *exp) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 33 | { |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 34 | u32 i; |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 35 | for (i = 0; i < vec_len (exp->streams); i++) |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 36 | if (!stream_index_valid (exp, i)) |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 37 | return &exp->streams[i]; |
| 38 | u32 index = vec_len (exp->streams); |
| 39 | vec_validate (exp->streams, index); |
| 40 | return &exp->streams[index]; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 43 | static_always_inline void |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 44 | delete_stream (ipfix_exporter_t *exp, u32 index) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 45 | { |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 46 | ASSERT (index < vec_len (exp->streams)); |
| 47 | ASSERT (exp->streams[index].domain_id != ~0); |
| 48 | exp->streams[index].domain_id = ~0; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 51 | static i32 |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 52 | find_stream (ipfix_exporter_t *exp, u32 domain_id, u16 src_port) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 53 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 54 | flow_report_stream_t *stream; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 55 | u32 i; |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 56 | for (i = 0; i < vec_len (exp->streams); i++) |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 57 | if (stream_index_valid (exp, i)) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 58 | { |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 59 | stream = &exp->streams[i]; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 60 | if (domain_id == stream->domain_id) |
| 61 | { |
| 62 | if (src_port != stream->src_port) |
| 63 | return -2; |
| 64 | return i; |
| 65 | } |
| 66 | else if (src_port == stream->src_port) |
| 67 | { |
| 68 | return -2; |
| 69 | } |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 70 | } |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 71 | return -1; |
| 72 | } |
| 73 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 74 | int |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 75 | send_template_packet (flow_report_main_t *frm, ipfix_exporter_t *exp, |
| 76 | flow_report_t *fr, u32 *buffer_indexp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | { |
| 78 | u32 bi0; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 79 | vlib_buffer_t *b0; |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 80 | ip4_ipfix_template_packet_t *tp4; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 81 | ipfix_message_header_t *h; |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 82 | ip4_header_t *ip4; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 83 | udp_header_t *udp; |
| 84 | vlib_main_t *vm = frm->vlib_main; |
| 85 | flow_report_stream_t *stream; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
| 87 | ASSERT (buffer_indexp); |
| 88 | |
| 89 | if (fr->update_rewrite || fr->rewrite == 0) |
| 90 | { |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 91 | if (exp->ipfix_collector.as_u32 == 0 || exp->src_address.as_u32 == 0) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 92 | { |
| 93 | vlib_node_set_state (frm->vlib_main, flow_report_process_node.index, |
| 94 | VLIB_NODE_STATE_DISABLED); |
| 95 | return -1; |
| 96 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | vec_free (fr->rewrite); |
| 98 | fr->update_rewrite = 1; |
| 99 | } |
| 100 | |
| 101 | if (fr->update_rewrite) |
| 102 | { |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 103 | fr->rewrite = fr->rewrite_callback ( |
Paul Atkins | c2d476b | 2021-10-04 16:29:52 +0100 | [diff] [blame] | 104 | exp, fr, exp->collector_port, fr->report_elements, |
| 105 | fr->n_report_elements, fr->stream_indexp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | fr->update_rewrite = 0; |
| 107 | } |
| 108 | |
| 109 | if (vlib_buffer_alloc (vm, &bi0, 1) != 1) |
| 110 | return -1; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 111 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | b0 = vlib_get_buffer (vm, bi0); |
| 113 | |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 114 | ASSERT (vec_len (fr->rewrite) < vlib_buffer_get_default_data_size (vm)); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 115 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 116 | clib_memcpy_fast (b0->data, fr->rewrite, vec_len (fr->rewrite)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | b0->current_data = 0; |
| 118 | b0->current_length = vec_len (fr->rewrite); |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 119 | b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0; |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 121 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 123 | tp4 = vlib_buffer_get_current (b0); |
| 124 | ip4 = (ip4_header_t *) &tp4->ip4; |
| 125 | udp = (udp_header_t *) (ip4 + 1); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 126 | h = (ipfix_message_header_t *) (udp + 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 128 | /* FIXUP: message header export_time */ |
| 129 | h->export_time = (u32) |
| 130 | (((f64) frm->unix_time_0) + |
| 131 | (vlib_time_now (frm->vlib_main) - frm->vlib_time_0)); |
| 132 | h->export_time = clib_host_to_net_u32 (h->export_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 134 | stream = &exp->streams[fr->stream_index]; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 135 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | /* FIXUP: message header sequence_number. Templates do not increase it */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 137 | h->sequence_number = clib_host_to_net_u32 (stream->sequence_number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | |
| 139 | /* FIXUP: udp length */ |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 140 | udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip4)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 142 | if (exp->udp_checksum) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 143 | { |
| 144 | /* RFC 7011 section 10.3.2. */ |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 145 | udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip4); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 146 | if (udp->checksum == 0) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 147 | udp->checksum = 0xffff; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | *buffer_indexp = bi0; |
Juraj Sloboda | 0d2a8e7 | 2016-07-07 02:59:28 -0700 | [diff] [blame] | 151 | |
| 152 | fr->last_template_sent = vlib_time_now (vm); |
| 153 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 157 | u8 * |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 158 | vnet_flow_rewrite_generic_callback (ipfix_exporter_t *exp, flow_report_t *fr, |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 159 | u16 collector_port, |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 160 | ipfix_report_element_t *report_elts, |
| 161 | u32 n_elts, u32 *stream_indexp) |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 162 | { |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 163 | ip4_header_t *ip4; |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 164 | udp_header_t *udp; |
| 165 | ipfix_message_header_t *h; |
| 166 | ipfix_set_header_t *s; |
| 167 | ipfix_template_header_t *t; |
| 168 | ipfix_field_specifier_t *f; |
| 169 | ipfix_field_specifier_t *first_field; |
| 170 | u8 *rewrite = 0; |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 171 | ip4_ipfix_template_packet_t *tp4; |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 172 | flow_report_stream_t *stream; |
| 173 | int i; |
| 174 | ipfix_report_element_t *ep; |
| 175 | |
| 176 | ASSERT (stream_indexp); |
| 177 | ASSERT (n_elts); |
| 178 | ASSERT (report_elts); |
| 179 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 180 | stream = &exp->streams[fr->stream_index]; |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 181 | *stream_indexp = fr->stream_index; |
| 182 | |
| 183 | /* allocate rewrite space */ |
| 184 | vec_validate_aligned (rewrite, |
| 185 | sizeof (ip4_ipfix_template_packet_t) |
| 186 | + n_elts * sizeof (ipfix_field_specifier_t) - 1, |
| 187 | CLIB_CACHE_LINE_BYTES); |
| 188 | |
| 189 | /* create the packet rewrite string */ |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 190 | tp4 = (ip4_ipfix_template_packet_t *) rewrite; |
| 191 | ip4 = (ip4_header_t *) &tp4->ip4; |
| 192 | udp = (udp_header_t *) (ip4 + 1); |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 193 | h = (ipfix_message_header_t *) (udp + 1); |
| 194 | s = (ipfix_set_header_t *) (h + 1); |
| 195 | t = (ipfix_template_header_t *) (s + 1); |
| 196 | first_field = f = (ipfix_field_specifier_t *) (t + 1); |
| 197 | |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 198 | ip4->ip_version_and_header_length = 0x45; |
| 199 | ip4->ttl = 254; |
| 200 | ip4->protocol = IP_PROTOCOL_UDP; |
| 201 | ip4->src_address.as_u32 = exp->src_address.as_u32; |
| 202 | ip4->dst_address.as_u32 = exp->ipfix_collector.as_u32; |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 203 | udp->src_port = clib_host_to_net_u16 (stream->src_port); |
| 204 | udp->dst_port = clib_host_to_net_u16 (collector_port); |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 205 | udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip4)); |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 206 | |
| 207 | /* FIXUP LATER: message header export_time */ |
| 208 | h->domain_id = clib_host_to_net_u32 (stream->domain_id); |
| 209 | |
| 210 | ep = report_elts; |
| 211 | |
| 212 | for (i = 0; i < n_elts; i++) |
| 213 | { |
| 214 | f->e_id_length = ipfix_e_id_length (0, ep->info_element, ep->size); |
| 215 | f++; |
| 216 | ep++; |
| 217 | } |
| 218 | |
| 219 | /* Back to the template packet... */ |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 220 | ip4 = (ip4_header_t *) &tp4->ip4; |
| 221 | udp = (udp_header_t *) (ip4 + 1); |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 222 | |
| 223 | ASSERT (f - first_field); |
| 224 | /* Field count in this template */ |
| 225 | t->id_count = ipfix_id_count (fr->template_id, f - first_field); |
| 226 | |
| 227 | /* set length in octets */ |
| 228 | s->set_id_length = |
| 229 | ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s); |
| 230 | |
| 231 | /* message length in octets */ |
| 232 | h->version_length = version_length ((u8 *) f - (u8 *) h); |
| 233 | |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 234 | ip4->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip4); |
| 235 | ip4->checksum = ip4_header_checksum (ip4); |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 236 | |
| 237 | return rewrite; |
| 238 | } |
| 239 | |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 240 | vlib_buffer_t * |
| 241 | vnet_ipfix_exp_get_buffer (vlib_main_t *vm, ipfix_exporter_t *exp, |
| 242 | flow_report_t *fr, u32 thread_index) |
| 243 | { |
| 244 | u32 bi0; |
| 245 | vlib_buffer_t *b0; |
| 246 | |
| 247 | if (fr->per_thread_data[thread_index].buffer) |
| 248 | return fr->per_thread_data[thread_index].buffer; |
| 249 | |
| 250 | if (vlib_buffer_alloc (vm, &bi0, 1) != 1) |
| 251 | return NULL; |
| 252 | |
| 253 | /* Initialize the buffer */ |
| 254 | b0 = fr->per_thread_data[thread_index].buffer = vlib_get_buffer (vm, bi0); |
| 255 | |
| 256 | b0->current_data = 0; |
| 257 | b0->current_length = exp->all_headers_size; |
| 258 | b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT); |
| 259 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0; |
| 260 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index; |
| 261 | fr->per_thread_data[thread_index].next_data_offset = b0->current_length; |
| 262 | |
| 263 | return b0; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Send a buffer that is mostly populated. Has flow records but needs some |
| 268 | * header fields updated. |
| 269 | */ |
| 270 | void |
| 271 | vnet_ipfix_exp_send_buffer (vlib_main_t *vm, ipfix_exporter_t *exp, |
| 272 | flow_report_t *fr, flow_report_stream_t *stream, |
| 273 | u32 thread_index, vlib_buffer_t *b0) |
| 274 | { |
| 275 | flow_report_main_t *frm = &flow_report_main; |
| 276 | vlib_frame_t *f; |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 277 | ip4_ipfix_template_packet_t *tp4; |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 278 | ipfix_set_header_t *s; |
| 279 | ipfix_message_header_t *h; |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 280 | ip4_header_t *ip4; |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 281 | udp_header_t *udp; |
| 282 | |
| 283 | /* nothing to send */ |
| 284 | if (fr->per_thread_data[thread_index].next_data_offset <= |
| 285 | exp->all_headers_size) |
| 286 | return; |
| 287 | |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 288 | tp4 = vlib_buffer_get_current (b0); |
| 289 | ip4 = (ip4_header_t *) &tp4->ip4; |
| 290 | udp = (udp_header_t *) (ip4 + 1); |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 291 | h = (ipfix_message_header_t *) (udp + 1); |
| 292 | s = (ipfix_set_header_t *) (h + 1); |
| 293 | |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 294 | ip4->ip_version_and_header_length = 0x45; |
| 295 | ip4->ttl = 254; |
| 296 | ip4->protocol = IP_PROTOCOL_UDP; |
| 297 | ip4->flags_and_fragment_offset = 0; |
| 298 | ip4->src_address.as_u32 = exp->src_address.as_u32; |
| 299 | ip4->dst_address.as_u32 = exp->ipfix_collector.as_u32; |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 300 | udp->src_port = clib_host_to_net_u16 (stream->src_port); |
| 301 | udp->dst_port = clib_host_to_net_u16 (exp->collector_port); |
| 302 | udp->checksum = 0; |
| 303 | |
| 304 | /* FIXUP: message header export_time */ |
| 305 | h->export_time = |
| 306 | (u32) (((f64) frm->unix_time_0) + (vlib_time_now (vm) - frm->vlib_time_0)); |
| 307 | h->export_time = clib_host_to_net_u32 (h->export_time); |
| 308 | h->domain_id = clib_host_to_net_u32 (stream->domain_id); |
| 309 | |
| 310 | /* |
| 311 | * RFC 7011: Section 3.2 |
| 312 | * |
| 313 | * Incremental sequence counter modulo 2^32 of all IPFIX Data Records |
| 314 | * sent in the current stream from the current Observation Domain by |
| 315 | * the Exporting Process |
| 316 | */ |
| 317 | h->sequence_number = |
| 318 | clib_atomic_fetch_add (&stream->sequence_number, |
| 319 | fr->per_thread_data[thread_index].n_data_records); |
| 320 | h->sequence_number = clib_host_to_net_u32 (h->sequence_number); |
| 321 | |
| 322 | /* |
| 323 | * For data records we use the template ID as the set ID. |
| 324 | * RFC 7011: 3.4.3 |
| 325 | */ |
| 326 | s->set_id_length = ipfix_set_id_length ( |
| 327 | fr->template_id, |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 328 | b0->current_length - (sizeof (*ip4) + sizeof (*udp) + sizeof (*h))); |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 329 | h->version_length = |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 330 | version_length (b0->current_length - (sizeof (*ip4) + sizeof (*udp))); |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 331 | |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 332 | ip4->length = clib_host_to_net_u16 (b0->current_length); |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 333 | |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 334 | ip4->checksum = ip4_header_checksum (ip4); |
| 335 | udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip4)); |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 336 | |
| 337 | if (exp->udp_checksum) |
| 338 | { |
| 339 | /* RFC 7011 section 10.3.2. */ |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 340 | udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip4); |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 341 | if (udp->checksum == 0) |
| 342 | udp->checksum = 0xffff; |
| 343 | } |
| 344 | |
Paul Atkins | 0ea1485 | 2021-10-04 17:29:54 +0100 | [diff] [blame^] | 345 | ASSERT (ip4_header_checksum_is_valid (ip4)); |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 346 | |
| 347 | /* Find or allocate a frame */ |
| 348 | f = fr->per_thread_data[thread_index].frame; |
| 349 | if (PREDICT_FALSE (f == 0)) |
| 350 | { |
| 351 | u32 *to_next; |
| 352 | f = vlib_get_frame_to_node (vm, ip4_lookup_node.index); |
| 353 | fr->per_thread_data[thread_index].frame = f; |
| 354 | u32 bi0 = vlib_get_buffer_index (vm, b0); |
| 355 | |
| 356 | /* Enqueue the buffer */ |
| 357 | to_next = vlib_frame_vector_args (f); |
| 358 | to_next[0] = bi0; |
| 359 | f->n_vectors = 1; |
| 360 | } |
| 361 | |
| 362 | vlib_put_frame_to_node (vm, ip4_lookup_node.index, f); |
| 363 | |
| 364 | fr->per_thread_data[thread_index].frame = NULL; |
| 365 | fr->per_thread_data[thread_index].buffer = NULL; |
| 366 | fr->per_thread_data[thread_index].next_data_offset = 0; |
| 367 | } |
| 368 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | static uword |
| 370 | flow_report_process (vlib_main_t * vm, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 371 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 373 | flow_report_main_t *frm = &flow_report_main; |
| 374 | flow_report_t *fr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | u32 ip4_lookup_node_index; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 376 | vlib_node_t *ip4_lookup_node; |
| 377 | vlib_frame_t *nf = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | u32 template_bi; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 379 | u32 *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | int send_template; |
Matthew Smith | baa1870 | 2021-04-28 11:48:39 -0500 | [diff] [blame] | 381 | f64 now, wait_time; |
| 382 | f64 def_wait_time = 5.0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | int rv; |
| 384 | uword event_type; |
| 385 | uword *event_data = 0; |
| 386 | |
| 387 | /* Wait for Godot... */ |
| 388 | vlib_process_wait_for_event_or_clock (vm, 1e9); |
| 389 | event_type = vlib_process_get_events (vm, &event_data); |
| 390 | if (event_type != 1) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 391 | clib_warning ("bogus kickoff event received, %d", event_type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 392 | vec_reset_length (event_data); |
| 393 | |
| 394 | /* Enqueue pkts to ip4-lookup */ |
| 395 | ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup"); |
| 396 | ip4_lookup_node_index = ip4_lookup_node->index; |
| 397 | |
Matthew Smith | baa1870 | 2021-04-28 11:48:39 -0500 | [diff] [blame] | 398 | wait_time = def_wait_time; |
| 399 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 400 | while (1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | { |
Matthew Smith | baa1870 | 2021-04-28 11:48:39 -0500 | [diff] [blame] | 402 | vlib_process_wait_for_event_or_clock (vm, wait_time); |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 403 | event_type = vlib_process_get_events (vm, &event_data); |
| 404 | vec_reset_length (event_data); |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 405 | ipfix_exporter_t *exp; |
| 406 | pool_foreach (exp, frm->exporters) |
Paul Atkins | 292992e | 2021-09-21 21:08:14 +0100 | [diff] [blame] | 407 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 409 | /* 5s delay by default, possibly reduced by template intervals */ |
| 410 | wait_time = def_wait_time; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 412 | vec_foreach (fr, exp->reports) |
Paul Atkins | 292992e | 2021-09-21 21:08:14 +0100 | [diff] [blame] | 413 | { |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 414 | f64 next_template; |
| 415 | now = vlib_time_now (vm); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 416 | |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 417 | /* Need to send a template packet? */ |
| 418 | send_template = |
| 419 | now > (fr->last_template_sent + exp->template_interval); |
| 420 | send_template += fr->last_template_sent == 0; |
| 421 | template_bi = ~0; |
| 422 | rv = 0; |
| 423 | |
| 424 | if (send_template) |
| 425 | rv = send_template_packet (frm, exp, fr, &template_bi); |
| 426 | |
| 427 | if (rv < 0) |
| 428 | continue; |
| 429 | |
| 430 | /* |
| 431 | * decide if template should be sent sooner than current wait |
| 432 | * time |
| 433 | */ |
| 434 | next_template = |
| 435 | (fr->last_template_sent + exp->template_interval) - now; |
| 436 | wait_time = clib_min (wait_time, next_template); |
| 437 | |
| 438 | nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index); |
| 439 | nf->n_vectors = 0; |
| 440 | to_next = vlib_frame_vector_args (nf); |
| 441 | |
| 442 | if (template_bi != ~0) |
| 443 | { |
| 444 | to_next[0] = template_bi; |
| 445 | to_next++; |
| 446 | nf->n_vectors++; |
| 447 | } |
| 448 | |
| 449 | nf = fr->flow_data_callback (frm, exp, fr, nf, to_next, |
| 450 | ip4_lookup_node_index); |
| 451 | if (nf) |
| 452 | vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf); |
| 453 | } |
Paul Atkins | 292992e | 2021-09-21 21:08:14 +0100 | [diff] [blame] | 454 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 457 | return 0; /* not so much */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 460 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | VLIB_REGISTER_NODE (flow_report_process_node) = { |
| 462 | .function = flow_report_process, |
| 463 | .type = VLIB_NODE_TYPE_PROCESS, |
| 464 | .name = "flow-report-process", |
| 465 | }; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 466 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 467 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 468 | int |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 469 | vnet_flow_report_add_del (ipfix_exporter_t *exp, |
| 470 | vnet_flow_report_add_del_args_t *a, u16 *template_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | { |
| 472 | int i; |
| 473 | int found_index = ~0; |
| 474 | flow_report_t *fr; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 475 | flow_report_stream_t *stream; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 476 | u32 si; |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 477 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 478 | flow_report_main_t *frm = &flow_report_main; |
| 479 | vlib_main_t *vm = frm->vlib_main; |
| 480 | int size; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 481 | |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 482 | si = find_stream (exp, a->domain_id, a->src_port); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 483 | if (si == -2) |
| 484 | return VNET_API_ERROR_INVALID_VALUE; |
| 485 | if (si == -1 && a->is_add == 0) |
| 486 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 487 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 488 | for (i = 0; i < vec_len (exp->reports); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | { |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 490 | fr = vec_elt_at_index (exp->reports, i); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 491 | if (fr->opaque.as_uword == a->opaque.as_uword |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 492 | && fr->rewrite_callback == a->rewrite_callback |
| 493 | && fr->flow_data_callback == a->flow_data_callback) |
| 494 | { |
| 495 | found_index = i; |
| 496 | if (template_id) |
| 497 | *template_id = fr->template_id; |
| 498 | break; |
| 499 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | if (a->is_add == 0) |
| 503 | { |
| 504 | if (found_index != ~0) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 505 | { |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 506 | for (int i = 0; |
| 507 | i < vec_len (exp->reports[found_index].per_thread_data); i++) |
| 508 | { |
| 509 | u32 bi; |
| 510 | if (exp->reports[found_index].per_thread_data[i].buffer) |
| 511 | { |
| 512 | bi = vlib_get_buffer_index ( |
| 513 | vm, exp->reports[found_index].per_thread_data[i].buffer); |
| 514 | vlib_buffer_free (vm, &bi, 1); |
| 515 | } |
| 516 | } |
| 517 | vec_free (exp->reports[found_index].per_thread_data); |
| 518 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 519 | vec_delete (exp->reports, 1, found_index); |
| 520 | stream = &exp->streams[si]; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 521 | stream->n_reports--; |
| 522 | if (stream->n_reports == 0) |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 523 | delete_stream (exp, si); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 524 | return 0; |
| 525 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 527 | } |
| 528 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 529 | if (found_index != ~0) |
| 530 | return VNET_API_ERROR_VALUE_EXIST; |
| 531 | |
| 532 | if (si == -1) |
| 533 | { |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 534 | stream = add_stream (exp); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 535 | stream->domain_id = a->domain_id; |
| 536 | stream->src_port = a->src_port; |
| 537 | stream->sequence_number = 0; |
| 538 | stream->n_reports = 0; |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 539 | si = stream - exp->streams; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 540 | } |
| 541 | else |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 542 | stream = &exp->streams[si]; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 543 | |
| 544 | stream->n_reports++; |
| 545 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 546 | vec_add2 (exp->reports, fr, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 548 | fr->stream_index = si; |
| 549 | fr->template_id = 256 + stream->next_template_no; |
| 550 | stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 551 | fr->update_rewrite = 1; |
| 552 | fr->opaque = a->opaque; |
| 553 | fr->rewrite_callback = a->rewrite_callback; |
| 554 | fr->flow_data_callback = a->flow_data_callback; |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 555 | fr->report_elements = a->report_elements; |
| 556 | fr->n_report_elements = a->n_report_elements; |
| 557 | fr->stream_indexp = a->stream_indexp; |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 558 | vec_validate (fr->per_thread_data, tm->n_threads); |
| 559 | /* Store the flow_report index back in the args struct */ |
| 560 | a->flow_report_index = fr - exp->reports; |
| 561 | |
| 562 | size = 0; |
| 563 | for (int i = 0; i < fr->n_report_elements; i++) |
| 564 | size += fr->report_elements[i].size; |
| 565 | fr->data_record_size = size; |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 566 | if (template_id) |
| 567 | *template_id = fr->template_id; |
| 568 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 572 | clib_error_t * |
| 573 | flow_report_add_del_error_to_clib_error (int error) |
Juraj Sloboda | 24648ad | 2016-09-06 04:43:52 -0700 | [diff] [blame] | 574 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 575 | switch (error) |
| 576 | { |
| 577 | case 0: |
| 578 | return 0; |
| 579 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 580 | return clib_error_return (0, "Flow report not found"); |
| 581 | case VNET_API_ERROR_VALUE_EXIST: |
| 582 | return clib_error_return (0, "Flow report already exists"); |
| 583 | case VNET_API_ERROR_INVALID_VALUE: |
| 584 | return clib_error_return (0, "Expecting either still unused values " |
| 585 | "for both domain_id and src_port " |
| 586 | "or already used values for both fields"); |
| 587 | default: |
| 588 | return clib_error_return (0, "vnet_flow_report_add_del returned %d", |
| 589 | error); |
| 590 | } |
Juraj Sloboda | 24648ad | 2016-09-06 04:43:52 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 593 | void |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 594 | vnet_flow_reports_reset (ipfix_exporter_t *exp) |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 595 | { |
| 596 | flow_report_t *fr; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 597 | u32 i; |
| 598 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 599 | for (i = 0; i < vec_len (exp->streams); i++) |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 600 | if (stream_index_valid (exp, i)) |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 601 | exp->streams[i].sequence_number = 0; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 602 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 603 | vec_foreach (fr, exp->reports) |
Paul Atkins | 292992e | 2021-09-21 21:08:14 +0100 | [diff] [blame] | 604 | { |
| 605 | fr->update_rewrite = 1; |
| 606 | fr->last_template_sent = 0; |
| 607 | } |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 610 | void |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 611 | vnet_stream_reset (ipfix_exporter_t *exp, u32 stream_index) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 612 | { |
| 613 | flow_report_t *fr; |
| 614 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 615 | exp->streams[stream_index].sequence_number = 0; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 616 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 617 | vec_foreach (fr, exp->reports) |
| 618 | if (exp->reports->stream_index == stream_index) |
Paul Atkins | 292992e | 2021-09-21 21:08:14 +0100 | [diff] [blame] | 619 | { |
| 620 | fr->update_rewrite = 1; |
| 621 | fr->last_template_sent = 0; |
| 622 | } |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 625 | int |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 626 | vnet_stream_change (ipfix_exporter_t *exp, u32 old_domain_id, u16 old_src_port, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 627 | u32 new_domain_id, u16 new_src_port) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 628 | { |
Paul Atkins | 9e82781 | 2021-09-22 08:15:03 +0100 | [diff] [blame] | 629 | i32 stream_index = find_stream (exp, old_domain_id, old_src_port); |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 630 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 631 | if (stream_index < 0) |
| 632 | return 1; |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 633 | flow_report_stream_t *stream = &exp->streams[stream_index]; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 634 | stream->domain_id = new_domain_id; |
| 635 | stream->src_port = new_src_port; |
| 636 | if (old_domain_id != new_domain_id || old_src_port != new_src_port) |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 637 | vnet_stream_reset (exp, stream_index); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 638 | return 0; |
| 639 | } |
| 640 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 641 | static clib_error_t * |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 642 | set_ipfix_exporter_command_fn (vlib_main_t * vm, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 643 | unformat_input_t * input, |
| 644 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 646 | flow_report_main_t *frm = &flow_report_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 647 | ip4_address_t collector, src; |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 648 | u16 collector_port = UDP_DST_PORT_ipfix; |
Juraj Sloboda | 86634f0 | 2016-07-01 06:12:58 -0700 | [diff] [blame] | 649 | u32 fib_id; |
| 650 | u32 fib_index = ~0; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 651 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | collector.as_u32 = 0; |
| 653 | src.as_u32 = 0; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 654 | u32 path_mtu = 512; // RFC 7011 section 10.3.3. |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 655 | u32 template_interval = 20; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 656 | u8 udp_checksum = 0; |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 657 | ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 658 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 659 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 660 | { |
| 661 | if (unformat (input, "collector %U", unformat_ip4_address, &collector)) |
| 662 | ; |
Elias Rudberg | 2dca180 | 2020-05-27 01:03:46 +0200 | [diff] [blame] | 663 | else if (unformat (input, "port %U", unformat_udp_port, |
| 664 | &collector_port)) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 665 | ; |
| 666 | else if (unformat (input, "src %U", unformat_ip4_address, &src)) |
| 667 | ; |
| 668 | else if (unformat (input, "fib-id %u", &fib_id)) |
| 669 | { |
| 670 | ip4_main_t *im = &ip4_main; |
| 671 | uword *p = hash_get (im->fib_index_by_table_id, fib_id); |
| 672 | if (!p) |
| 673 | return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id); |
| 674 | fib_index = p[0]; |
| 675 | } |
| 676 | else if (unformat (input, "path-mtu %u", &path_mtu)) |
| 677 | ; |
| 678 | else if (unformat (input, "template-interval %u", &template_interval)) |
| 679 | ; |
| 680 | else if (unformat (input, "udp-checksum")) |
| 681 | udp_checksum = 1; |
| 682 | else |
| 683 | break; |
| 684 | } |
| 685 | |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 686 | if (collector.as_u32 != 0 && src.as_u32 == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 687 | return clib_error_return (0, "src address required"); |
| 688 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 689 | if (path_mtu > 1450 /* vpp does not support fragmentation */ ) |
| 690 | return clib_error_return (0, "too big path-mtu value, maximum is 1450"); |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 691 | |
| 692 | if (path_mtu < 68) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 693 | return clib_error_return (0, "too small path-mtu value, minimum is 68"); |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 694 | |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 695 | /* Calculate how much header data we need. */ |
| 696 | exp->all_headers_size = sizeof (ip4_header_t) + sizeof (udp_header_t) + |
| 697 | sizeof (ipfix_message_header_t) + |
| 698 | sizeof (ipfix_set_header_t); |
| 699 | |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 700 | /* Reset report streams if we are reconfiguring IP addresses */ |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 701 | if (exp->ipfix_collector.as_u32 != collector.as_u32 || |
| 702 | exp->src_address.as_u32 != src.as_u32 || |
| 703 | exp->collector_port != collector_port) |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 704 | vnet_flow_reports_reset (exp); |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 705 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 706 | exp->ipfix_collector.as_u32 = collector.as_u32; |
| 707 | exp->collector_port = collector_port; |
| 708 | exp->src_address.as_u32 = src.as_u32; |
| 709 | exp->fib_index = fib_index; |
| 710 | exp->path_mtu = path_mtu; |
| 711 | exp->template_interval = template_interval; |
| 712 | exp->udp_checksum = udp_checksum; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 713 | |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 714 | if (collector.as_u32) |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 715 | vlib_cli_output (vm, |
| 716 | "Collector %U, src address %U, " |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 717 | "fib index %d, path MTU %u, " |
| 718 | "template resend interval %us, " |
| 719 | "udp checksum %s", |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 720 | format_ip4_address, exp->ipfix_collector, |
| 721 | format_ip4_address, exp->src_address, fib_index, path_mtu, |
| 722 | template_interval, udp_checksum ? "enabled" : "disabled"); |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 723 | else |
| 724 | vlib_cli_output (vm, "IPFIX Collector is disabled"); |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 725 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 726 | /* Turn on the flow reporting process */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 727 | vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 728 | return 0; |
| 729 | } |
| 730 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 731 | /* *INDENT-OFF* */ |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 732 | VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = { |
| 733 | .path = "set ipfix exporter", |
| 734 | .short_help = "set ipfix exporter " |
| 735 | "collector <ip4-address> [port <port>] " |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 736 | "src <ip4-address> [fib-id <fib-id>] " |
| 737 | "[path-mtu <path-mtu>] " |
Ignas Bacius | f3a522f | 2020-02-18 12:33:09 +0200 | [diff] [blame] | 738 | "[template-interval <template-interval>] " |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 739 | "[udp-checksum]", |
| 740 | .function = set_ipfix_exporter_command_fn, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 741 | }; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 742 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 743 | |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 744 | |
| 745 | static clib_error_t * |
| 746 | ipfix_flush_command_fn (vlib_main_t * vm, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 747 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 748 | { |
| 749 | /* poke the flow reporting process */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 750 | vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0); |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 754 | /* *INDENT-OFF* */ |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 755 | VLIB_CLI_COMMAND (ipfix_flush_command, static) = { |
| 756 | .path = "ipfix flush", |
| 757 | .short_help = "flush the current ipfix data [for make test]", |
| 758 | .function = ipfix_flush_command_fn, |
| 759 | }; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 760 | /* *INDENT-ON* */ |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 761 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 762 | static clib_error_t * |
| 763 | flow_report_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 764 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 765 | flow_report_main_t *frm = &flow_report_main; |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 766 | ipfix_exporter_t *exp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 767 | |
| 768 | frm->vlib_main = vm; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 769 | frm->vnet_main = vnet_get_main (); |
| 770 | frm->unix_time_0 = time (0); |
| 771 | frm->vlib_time_0 = vlib_time_now (frm->vlib_main); |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 772 | /* |
| 773 | * Make sure that we can always access the first exporter for |
| 774 | * backwards compatibility reasons. |
| 775 | */ |
| 776 | pool_alloc (frm->exporters, IPFIX_EXPORTERS_MAX); |
| 777 | pool_get (frm->exporters, exp); |
| 778 | /* Verify that this is at index 0 */ |
| 779 | ASSERT (frm->exporters == exp); |
| 780 | exp->fib_index = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 784 | VLIB_INIT_FUNCTION (flow_report_init); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 785 | /* |
| 786 | * fd.io coding-style-patch-verification: ON |
| 787 | * |
| 788 | * Local Variables: |
| 789 | * eval: (c-set-style "gnu") |
| 790 | * End: |
| 791 | */ |