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 | */ |
Ole Troan | a9855ef | 2018-05-02 12:45:10 +0200 | [diff] [blame] | 18 | #include <vnet/ipfix-export/flow_report.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 19 | #include <vnet/api_errno.h> |
| 20 | |
Juraj Sloboda | 837fbb1 | 2016-07-06 23:11:47 -0700 | [diff] [blame] | 21 | flow_report_main_t flow_report_main; |
| 22 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 23 | static_always_inline u8 |
| 24 | stream_index_valid (u32 index) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 25 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 26 | flow_report_main_t *frm = &flow_report_main; |
| 27 | return index < vec_len (frm->streams) && |
| 28 | frm->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 * |
| 32 | add_stream (void) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 33 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 34 | flow_report_main_t *frm = &flow_report_main; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 35 | u32 i; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 36 | for (i = 0; i < vec_len (frm->streams); i++) |
| 37 | if (!stream_index_valid (i)) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 38 | return &frm->streams[i]; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 39 | u32 index = vec_len (frm->streams); |
| 40 | vec_validate (frm->streams, index); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 41 | return &frm->streams[index]; |
| 42 | } |
| 43 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 44 | static_always_inline void |
| 45 | delete_stream (u32 index) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 46 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 47 | flow_report_main_t *frm = &flow_report_main; |
| 48 | ASSERT (index < vec_len (frm->streams)); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 49 | ASSERT (frm->streams[index].domain_id != ~0); |
| 50 | frm->streams[index].domain_id = ~0; |
| 51 | } |
| 52 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 53 | static i32 |
| 54 | find_stream (u32 domain_id, u16 src_port) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 55 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 56 | flow_report_main_t *frm = &flow_report_main; |
| 57 | flow_report_stream_t *stream; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 58 | u32 i; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 59 | for (i = 0; i < vec_len (frm->streams); i++) |
| 60 | if (stream_index_valid (i)) |
| 61 | { |
| 62 | stream = &frm->streams[i]; |
| 63 | if (domain_id == stream->domain_id) |
| 64 | { |
| 65 | if (src_port != stream->src_port) |
| 66 | return -2; |
| 67 | return i; |
| 68 | } |
| 69 | else if (src_port == stream->src_port) |
| 70 | { |
| 71 | return -2; |
| 72 | } |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 73 | } |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 77 | int |
| 78 | send_template_packet (flow_report_main_t * frm, |
| 79 | flow_report_t * fr, u32 * buffer_indexp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | { |
| 81 | u32 bi0; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 82 | vlib_buffer_t *b0; |
| 83 | ip4_ipfix_template_packet_t *tp; |
| 84 | ipfix_message_header_t *h; |
| 85 | ip4_header_t *ip; |
| 86 | udp_header_t *udp; |
| 87 | vlib_main_t *vm = frm->vlib_main; |
| 88 | flow_report_stream_t *stream; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | |
| 90 | ASSERT (buffer_indexp); |
| 91 | |
| 92 | if (fr->update_rewrite || fr->rewrite == 0) |
| 93 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 94 | if (frm->ipfix_collector.as_u32 == 0 || frm->src_address.as_u32 == 0) |
| 95 | { |
| 96 | vlib_node_set_state (frm->vlib_main, flow_report_process_node.index, |
| 97 | VLIB_NODE_STATE_DISABLED); |
| 98 | return -1; |
| 99 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | vec_free (fr->rewrite); |
| 101 | fr->update_rewrite = 1; |
| 102 | } |
| 103 | |
| 104 | if (fr->update_rewrite) |
| 105 | { |
| 106 | fr->rewrite = fr->rewrite_callback (frm, fr, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 107 | &frm->ipfix_collector, |
| 108 | &frm->src_address, |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 109 | frm->collector_port, |
| 110 | fr->report_elements, |
| 111 | fr->n_report_elements, |
| 112 | fr->stream_indexp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | fr->update_rewrite = 0; |
| 114 | } |
| 115 | |
| 116 | if (vlib_buffer_alloc (vm, &bi0, 1) != 1) |
| 117 | return -1; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 118 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | b0 = vlib_get_buffer (vm, bi0); |
| 120 | |
Dave Barach | 393252a | 2016-11-02 18:37:56 -0400 | [diff] [blame] | 121 | /* Initialize the buffer */ |
Dave Barach | 393252a | 2016-11-02 18:37:56 -0400 | [diff] [blame] | 122 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 123 | |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 124 | ASSERT (vec_len (fr->rewrite) < vlib_buffer_get_default_data_size (vm)); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 125 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 126 | clib_memcpy_fast (b0->data, fr->rewrite, vec_len (fr->rewrite)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | b0->current_data = 0; |
| 128 | b0->current_length = vec_len (fr->rewrite); |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 129 | b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0; |
Juraj Sloboda | 86634f0 | 2016-07-01 06:12:58 -0700 | [diff] [blame] | 131 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | |
| 133 | tp = vlib_buffer_get_current (b0); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 134 | ip = (ip4_header_t *) & tp->ip4; |
| 135 | udp = (udp_header_t *) (ip + 1); |
| 136 | h = (ipfix_message_header_t *) (udp + 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 138 | /* FIXUP: message header export_time */ |
| 139 | h->export_time = (u32) |
| 140 | (((f64) frm->unix_time_0) + |
| 141 | (vlib_time_now (frm->vlib_main) - frm->vlib_time_0)); |
| 142 | h->export_time = clib_host_to_net_u32 (h->export_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 144 | stream = &frm->streams[fr->stream_index]; |
| 145 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | /* FIXUP: message header sequence_number. Templates do not increase it */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 147 | h->sequence_number = clib_host_to_net_u32 (stream->sequence_number); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | |
| 149 | /* FIXUP: udp length */ |
| 150 | udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip)); |
| 151 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 152 | if (frm->udp_checksum) |
| 153 | { |
| 154 | /* RFC 7011 section 10.3.2. */ |
| 155 | udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip); |
| 156 | if (udp->checksum == 0) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 157 | udp->checksum = 0xffff; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | *buffer_indexp = bi0; |
Juraj Sloboda | 0d2a8e7 | 2016-07-07 02:59:28 -0700 | [diff] [blame] | 161 | |
| 162 | fr->last_template_sent = vlib_time_now (vm); |
| 163 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 167 | u8 * |
| 168 | vnet_flow_rewrite_generic_callback (flow_report_main_t * frm, |
| 169 | flow_report_t * fr, |
| 170 | ip4_address_t * collector_address, |
| 171 | ip4_address_t * src_address, |
| 172 | u16 collector_port, |
| 173 | ipfix_report_element_t * report_elts, |
| 174 | u32 n_elts, u32 * stream_indexp) |
| 175 | { |
| 176 | ip4_header_t *ip; |
| 177 | udp_header_t *udp; |
| 178 | ipfix_message_header_t *h; |
| 179 | ipfix_set_header_t *s; |
| 180 | ipfix_template_header_t *t; |
| 181 | ipfix_field_specifier_t *f; |
| 182 | ipfix_field_specifier_t *first_field; |
| 183 | u8 *rewrite = 0; |
| 184 | ip4_ipfix_template_packet_t *tp; |
| 185 | flow_report_stream_t *stream; |
| 186 | int i; |
| 187 | ipfix_report_element_t *ep; |
| 188 | |
| 189 | ASSERT (stream_indexp); |
| 190 | ASSERT (n_elts); |
| 191 | ASSERT (report_elts); |
| 192 | |
| 193 | stream = &frm->streams[fr->stream_index]; |
| 194 | *stream_indexp = fr->stream_index; |
| 195 | |
| 196 | /* allocate rewrite space */ |
| 197 | vec_validate_aligned (rewrite, |
| 198 | sizeof (ip4_ipfix_template_packet_t) |
| 199 | + n_elts * sizeof (ipfix_field_specifier_t) - 1, |
| 200 | CLIB_CACHE_LINE_BYTES); |
| 201 | |
| 202 | /* create the packet rewrite string */ |
| 203 | tp = (ip4_ipfix_template_packet_t *) rewrite; |
| 204 | ip = (ip4_header_t *) & tp->ip4; |
| 205 | udp = (udp_header_t *) (ip + 1); |
| 206 | h = (ipfix_message_header_t *) (udp + 1); |
| 207 | s = (ipfix_set_header_t *) (h + 1); |
| 208 | t = (ipfix_template_header_t *) (s + 1); |
| 209 | first_field = f = (ipfix_field_specifier_t *) (t + 1); |
| 210 | |
| 211 | ip->ip_version_and_header_length = 0x45; |
| 212 | ip->ttl = 254; |
| 213 | ip->protocol = IP_PROTOCOL_UDP; |
| 214 | ip->src_address.as_u32 = src_address->as_u32; |
| 215 | ip->dst_address.as_u32 = collector_address->as_u32; |
| 216 | udp->src_port = clib_host_to_net_u16 (stream->src_port); |
| 217 | udp->dst_port = clib_host_to_net_u16 (collector_port); |
| 218 | udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip)); |
| 219 | |
| 220 | /* FIXUP LATER: message header export_time */ |
| 221 | h->domain_id = clib_host_to_net_u32 (stream->domain_id); |
| 222 | |
| 223 | ep = report_elts; |
| 224 | |
| 225 | for (i = 0; i < n_elts; i++) |
| 226 | { |
| 227 | f->e_id_length = ipfix_e_id_length (0, ep->info_element, ep->size); |
| 228 | f++; |
| 229 | ep++; |
| 230 | } |
| 231 | |
| 232 | /* Back to the template packet... */ |
| 233 | ip = (ip4_header_t *) & tp->ip4; |
| 234 | udp = (udp_header_t *) (ip + 1); |
| 235 | |
| 236 | ASSERT (f - first_field); |
| 237 | /* Field count in this template */ |
| 238 | t->id_count = ipfix_id_count (fr->template_id, f - first_field); |
| 239 | |
| 240 | /* set length in octets */ |
| 241 | s->set_id_length = |
| 242 | ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s); |
| 243 | |
| 244 | /* message length in octets */ |
| 245 | h->version_length = version_length ((u8 *) f - (u8 *) h); |
| 246 | |
| 247 | ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip); |
| 248 | ip->checksum = ip4_header_checksum (ip); |
| 249 | |
| 250 | return rewrite; |
| 251 | } |
| 252 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | static uword |
| 254 | flow_report_process (vlib_main_t * vm, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 255 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 257 | flow_report_main_t *frm = &flow_report_main; |
| 258 | flow_report_t *fr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | u32 ip4_lookup_node_index; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 260 | vlib_node_t *ip4_lookup_node; |
| 261 | vlib_frame_t *nf = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | u32 template_bi; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 263 | u32 *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | int send_template; |
| 265 | f64 now; |
| 266 | int rv; |
| 267 | uword event_type; |
| 268 | uword *event_data = 0; |
| 269 | |
| 270 | /* Wait for Godot... */ |
| 271 | vlib_process_wait_for_event_or_clock (vm, 1e9); |
| 272 | event_type = vlib_process_get_events (vm, &event_data); |
| 273 | if (event_type != 1) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 274 | clib_warning ("bogus kickoff event received, %d", event_type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | vec_reset_length (event_data); |
| 276 | |
| 277 | /* Enqueue pkts to ip4-lookup */ |
| 278 | ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup"); |
| 279 | ip4_lookup_node_index = ip4_lookup_node->index; |
| 280 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 281 | while (1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | { |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 283 | vlib_process_wait_for_event_or_clock (vm, 5.0); |
| 284 | event_type = vlib_process_get_events (vm, &event_data); |
| 285 | vec_reset_length (event_data); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 286 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | vec_foreach (fr, frm->reports) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 288 | { |
| 289 | now = vlib_time_now (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 291 | /* Need to send a template packet? */ |
| 292 | send_template = |
| 293 | now > (fr->last_template_sent + frm->template_interval); |
| 294 | send_template += fr->last_template_sent == 0; |
| 295 | template_bi = ~0; |
| 296 | rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 297 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 298 | if (send_template) |
| 299 | rv = send_template_packet (frm, fr, &template_bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 300 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 301 | if (rv < 0) |
| 302 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 304 | nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index); |
| 305 | nf->n_vectors = 0; |
| 306 | to_next = vlib_frame_vector_args (nf); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 308 | if (template_bi != ~0) |
| 309 | { |
| 310 | to_next[0] = template_bi; |
| 311 | to_next++; |
| 312 | nf->n_vectors++; |
| 313 | } |
| 314 | |
| 315 | nf = fr->flow_data_callback (frm, fr, |
| 316 | nf, to_next, ip4_lookup_node_index); |
| 317 | if (nf) |
| 318 | vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf); |
| 319 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 322 | return 0; /* not so much */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 325 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 326 | VLIB_REGISTER_NODE (flow_report_process_node) = { |
| 327 | .function = flow_report_process, |
| 328 | .type = VLIB_NODE_TYPE_PROCESS, |
| 329 | .name = "flow-report-process", |
| 330 | }; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 331 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 333 | int |
| 334 | vnet_flow_report_add_del (flow_report_main_t * frm, |
| 335 | vnet_flow_report_add_del_args_t * a, |
| 336 | u16 * template_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | { |
| 338 | int i; |
| 339 | int found_index = ~0; |
| 340 | flow_report_t *fr; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 341 | flow_report_stream_t *stream; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 342 | u32 si; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 343 | |
| 344 | si = find_stream (a->domain_id, a->src_port); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 345 | if (si == -2) |
| 346 | return VNET_API_ERROR_INVALID_VALUE; |
| 347 | if (si == -1 && a->is_add == 0) |
| 348 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 349 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 350 | for (i = 0; i < vec_len (frm->reports); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 351 | { |
| 352 | fr = vec_elt_at_index (frm->reports, i); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 353 | if (fr->opaque.as_uword == a->opaque.as_uword |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 354 | && fr->rewrite_callback == a->rewrite_callback |
| 355 | && fr->flow_data_callback == a->flow_data_callback) |
| 356 | { |
| 357 | found_index = i; |
| 358 | if (template_id) |
| 359 | *template_id = fr->template_id; |
| 360 | break; |
| 361 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | if (a->is_add == 0) |
| 365 | { |
| 366 | if (found_index != ~0) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 367 | { |
| 368 | vec_delete (frm->reports, 1, found_index); |
| 369 | stream = &frm->streams[si]; |
| 370 | stream->n_reports--; |
| 371 | if (stream->n_reports == 0) |
| 372 | delete_stream (si); |
| 373 | return 0; |
| 374 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 376 | } |
| 377 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 378 | if (found_index != ~0) |
| 379 | return VNET_API_ERROR_VALUE_EXIST; |
| 380 | |
| 381 | if (si == -1) |
| 382 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 383 | stream = add_stream (); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 384 | stream->domain_id = a->domain_id; |
| 385 | stream->src_port = a->src_port; |
| 386 | stream->sequence_number = 0; |
| 387 | stream->n_reports = 0; |
| 388 | si = stream - frm->streams; |
| 389 | } |
| 390 | else |
| 391 | stream = &frm->streams[si]; |
| 392 | |
| 393 | stream->n_reports++; |
| 394 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | vec_add2 (frm->reports, fr, 1); |
| 396 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 397 | fr->stream_index = si; |
| 398 | fr->template_id = 256 + stream->next_template_no; |
| 399 | stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | fr->update_rewrite = 1; |
| 401 | fr->opaque = a->opaque; |
| 402 | fr->rewrite_callback = a->rewrite_callback; |
| 403 | fr->flow_data_callback = a->flow_data_callback; |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 404 | fr->report_elements = a->report_elements; |
| 405 | fr->n_report_elements = a->n_report_elements; |
| 406 | fr->stream_indexp = a->stream_indexp; |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 407 | if (template_id) |
| 408 | *template_id = fr->template_id; |
| 409 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 413 | clib_error_t * |
| 414 | flow_report_add_del_error_to_clib_error (int error) |
Juraj Sloboda | 24648ad | 2016-09-06 04:43:52 -0700 | [diff] [blame] | 415 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 416 | switch (error) |
| 417 | { |
| 418 | case 0: |
| 419 | return 0; |
| 420 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 421 | return clib_error_return (0, "Flow report not found"); |
| 422 | case VNET_API_ERROR_VALUE_EXIST: |
| 423 | return clib_error_return (0, "Flow report already exists"); |
| 424 | case VNET_API_ERROR_INVALID_VALUE: |
| 425 | return clib_error_return (0, "Expecting either still unused values " |
| 426 | "for both domain_id and src_port " |
| 427 | "or already used values for both fields"); |
| 428 | default: |
| 429 | return clib_error_return (0, "vnet_flow_report_add_del returned %d", |
| 430 | error); |
| 431 | } |
Juraj Sloboda | 24648ad | 2016-09-06 04:43:52 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 434 | void |
| 435 | vnet_flow_reports_reset (flow_report_main_t * frm) |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 436 | { |
| 437 | flow_report_t *fr; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 438 | u32 i; |
| 439 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 440 | for (i = 0; i < vec_len (frm->streams); i++) |
| 441 | if (stream_index_valid (i)) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 442 | frm->streams[i].sequence_number = 0; |
| 443 | |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 444 | vec_foreach (fr, frm->reports) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 445 | { |
| 446 | fr->update_rewrite = 1; |
| 447 | fr->last_template_sent = 0; |
| 448 | } |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 451 | void |
| 452 | vnet_stream_reset (flow_report_main_t * frm, u32 stream_index) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 453 | { |
| 454 | flow_report_t *fr; |
| 455 | |
| 456 | frm->streams[stream_index].sequence_number = 0; |
| 457 | |
| 458 | vec_foreach (fr, frm->reports) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 459 | if (frm->reports->stream_index == stream_index) |
| 460 | { |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 461 | fr->update_rewrite = 1; |
| 462 | fr->last_template_sent = 0; |
| 463 | } |
| 464 | } |
| 465 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 466 | int |
| 467 | vnet_stream_change (flow_report_main_t * frm, |
| 468 | u32 old_domain_id, u16 old_src_port, |
| 469 | u32 new_domain_id, u16 new_src_port) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 470 | { |
| 471 | i32 stream_index = find_stream (old_domain_id, old_src_port); |
| 472 | if (stream_index < 0) |
| 473 | return 1; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 474 | flow_report_stream_t *stream = &frm->streams[stream_index]; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 475 | stream->domain_id = new_domain_id; |
| 476 | stream->src_port = new_src_port; |
| 477 | if (old_domain_id != new_domain_id || old_src_port != new_src_port) |
| 478 | vnet_stream_reset (frm, stream_index); |
| 479 | return 0; |
| 480 | } |
| 481 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 482 | static clib_error_t * |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 483 | set_ipfix_exporter_command_fn (vlib_main_t * vm, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 484 | unformat_input_t * input, |
| 485 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 486 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 487 | flow_report_main_t *frm = &flow_report_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | ip4_address_t collector, src; |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 489 | u16 collector_port = UDP_DST_PORT_ipfix; |
Juraj Sloboda | 86634f0 | 2016-07-01 06:12:58 -0700 | [diff] [blame] | 490 | u32 fib_id; |
| 491 | u32 fib_index = ~0; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 492 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | collector.as_u32 = 0; |
| 494 | src.as_u32 = 0; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 495 | u32 path_mtu = 512; // RFC 7011 section 10.3.3. |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 496 | u32 template_interval = 20; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 497 | u8 udp_checksum = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 498 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 499 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 500 | { |
| 501 | if (unformat (input, "collector %U", unformat_ip4_address, &collector)) |
| 502 | ; |
| 503 | else if (unformat (input, "port %u", &collector_port)) |
| 504 | ; |
| 505 | else if (unformat (input, "src %U", unformat_ip4_address, &src)) |
| 506 | ; |
| 507 | else if (unformat (input, "fib-id %u", &fib_id)) |
| 508 | { |
| 509 | ip4_main_t *im = &ip4_main; |
| 510 | uword *p = hash_get (im->fib_index_by_table_id, fib_id); |
| 511 | if (!p) |
| 512 | return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id); |
| 513 | fib_index = p[0]; |
| 514 | } |
| 515 | else if (unformat (input, "path-mtu %u", &path_mtu)) |
| 516 | ; |
| 517 | else if (unformat (input, "template-interval %u", &template_interval)) |
| 518 | ; |
| 519 | else if (unformat (input, "udp-checksum")) |
| 520 | udp_checksum = 1; |
| 521 | else |
| 522 | break; |
| 523 | } |
| 524 | |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 525 | if (collector.as_u32 != 0 && src.as_u32 == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | return clib_error_return (0, "src address required"); |
| 527 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 528 | if (path_mtu > 1450 /* vpp does not support fragmentation */ ) |
| 529 | 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] | 530 | |
| 531 | if (path_mtu < 68) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 532 | 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] | 533 | |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 534 | /* Reset report streams if we are reconfiguring IP addresses */ |
| 535 | if (frm->ipfix_collector.as_u32 != collector.as_u32 || |
| 536 | frm->src_address.as_u32 != src.as_u32 || |
| 537 | frm->collector_port != collector_port) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 538 | vnet_flow_reports_reset (frm); |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 539 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | frm->ipfix_collector.as_u32 = collector.as_u32; |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 541 | frm->collector_port = collector_port; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | frm->src_address.as_u32 = src.as_u32; |
Juraj Sloboda | 86634f0 | 2016-07-01 06:12:58 -0700 | [diff] [blame] | 543 | frm->fib_index = fib_index; |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 544 | frm->path_mtu = path_mtu; |
| 545 | frm->template_interval = template_interval; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 546 | frm->udp_checksum = udp_checksum; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 547 | |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 548 | if (collector.as_u32) |
| 549 | vlib_cli_output (vm, "Collector %U, src address %U, " |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 550 | "fib index %d, path MTU %u, " |
| 551 | "template resend interval %us, " |
| 552 | "udp checksum %s", |
| 553 | format_ip4_address, &frm->ipfix_collector, |
| 554 | format_ip4_address, &frm->src_address, |
| 555 | fib_index, path_mtu, template_interval, |
| 556 | udp_checksum ? "enabled" : "disabled"); |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 557 | else |
| 558 | vlib_cli_output (vm, "IPFIX Collector is disabled"); |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 559 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 560 | /* Turn on the flow reporting process */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 561 | vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 565 | /* *INDENT-OFF* */ |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 566 | VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = { |
| 567 | .path = "set ipfix exporter", |
| 568 | .short_help = "set ipfix exporter " |
| 569 | "collector <ip4-address> [port <port>] " |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 570 | "src <ip4-address> [fib-id <fib-id>] " |
| 571 | "[path-mtu <path-mtu>] " |
| 572 | "[template-interval <template-interval>]", |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 573 | "[udp-checksum]", |
| 574 | .function = set_ipfix_exporter_command_fn, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | }; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 576 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 577 | |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 578 | |
| 579 | static clib_error_t * |
| 580 | ipfix_flush_command_fn (vlib_main_t * vm, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 581 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 582 | { |
| 583 | /* poke the flow reporting process */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 584 | vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0); |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 585 | return 0; |
| 586 | } |
| 587 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 588 | /* *INDENT-OFF* */ |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 589 | VLIB_CLI_COMMAND (ipfix_flush_command, static) = { |
| 590 | .path = "ipfix flush", |
| 591 | .short_help = "flush the current ipfix data [for make test]", |
| 592 | .function = ipfix_flush_command_fn, |
| 593 | }; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 594 | /* *INDENT-ON* */ |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 595 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 596 | static clib_error_t * |
| 597 | flow_report_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 599 | flow_report_main_t *frm = &flow_report_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 600 | |
| 601 | frm->vlib_main = vm; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 602 | frm->vnet_main = vnet_get_main (); |
| 603 | frm->unix_time_0 = time (0); |
| 604 | frm->vlib_time_0 = vlib_time_now (frm->vlib_main); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 605 | frm->fib_index = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | VLIB_INIT_FUNCTION (flow_report_init) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 611 | /* |
| 612 | * fd.io coding-style-patch-verification: ON |
| 613 | * |
| 614 | * Local Variables: |
| 615 | * eval: (c-set-style "gnu") |
| 616 | * End: |
| 617 | */ |