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 | */ |
| 18 | #include <vnet/flow/flow_report.h> |
| 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; |
Dave Barach | 393252a | 2016-11-02 18:37:56 -0400 | [diff] [blame] | 89 | vlib_buffer_free_list_t *fl; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | |
| 91 | ASSERT (buffer_indexp); |
| 92 | |
| 93 | if (fr->update_rewrite || fr->rewrite == 0) |
| 94 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 95 | if (frm->ipfix_collector.as_u32 == 0 || frm->src_address.as_u32 == 0) |
| 96 | { |
| 97 | vlib_node_set_state (frm->vlib_main, flow_report_process_node.index, |
| 98 | VLIB_NODE_STATE_DISABLED); |
| 99 | return -1; |
| 100 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | vec_free (fr->rewrite); |
| 102 | fr->update_rewrite = 1; |
| 103 | } |
| 104 | |
| 105 | if (fr->update_rewrite) |
| 106 | { |
| 107 | fr->rewrite = fr->rewrite_callback (frm, fr, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 108 | &frm->ipfix_collector, |
| 109 | &frm->src_address, |
| 110 | frm->collector_port); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | fr->update_rewrite = 0; |
| 112 | } |
| 113 | |
| 114 | if (vlib_buffer_alloc (vm, &bi0, 1) != 1) |
| 115 | return -1; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 116 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | b0 = vlib_get_buffer (vm, bi0); |
| 118 | |
Dave Barach | 393252a | 2016-11-02 18:37:56 -0400 | [diff] [blame] | 119 | /* Initialize the buffer */ |
| 120 | fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
| 121 | vlib_buffer_init_for_free_list (b0, fl); |
| 122 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 123 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | ASSERT (vec_len (fr->rewrite) < VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 125 | |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 126 | clib_memcpy (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 | |
| 167 | static uword |
| 168 | flow_report_process (vlib_main_t * vm, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 169 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 171 | flow_report_main_t *frm = &flow_report_main; |
| 172 | flow_report_t *fr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | u32 ip4_lookup_node_index; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 174 | vlib_node_t *ip4_lookup_node; |
| 175 | vlib_frame_t *nf = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | u32 template_bi; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 177 | u32 *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | int send_template; |
| 179 | f64 now; |
| 180 | int rv; |
| 181 | uword event_type; |
| 182 | uword *event_data = 0; |
| 183 | |
| 184 | /* Wait for Godot... */ |
| 185 | vlib_process_wait_for_event_or_clock (vm, 1e9); |
| 186 | event_type = vlib_process_get_events (vm, &event_data); |
| 187 | if (event_type != 1) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 188 | clib_warning ("bogus kickoff event received, %d", event_type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | vec_reset_length (event_data); |
| 190 | |
| 191 | /* Enqueue pkts to ip4-lookup */ |
| 192 | ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup"); |
| 193 | ip4_lookup_node_index = ip4_lookup_node->index; |
| 194 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 195 | while (1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | { |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 197 | vlib_process_wait_for_event_or_clock (vm, 5.0); |
| 198 | event_type = vlib_process_get_events (vm, &event_data); |
| 199 | vec_reset_length (event_data); |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 200 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | vec_foreach (fr, frm->reports) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 202 | { |
| 203 | now = vlib_time_now (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 205 | /* Need to send a template packet? */ |
| 206 | send_template = |
| 207 | now > (fr->last_template_sent + frm->template_interval); |
| 208 | send_template += fr->last_template_sent == 0; |
| 209 | template_bi = ~0; |
| 210 | rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 212 | if (send_template) |
| 213 | rv = send_template_packet (frm, fr, &template_bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 215 | if (rv < 0) |
| 216 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 218 | nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index); |
| 219 | nf->n_vectors = 0; |
| 220 | to_next = vlib_frame_vector_args (nf); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 222 | if (template_bi != ~0) |
| 223 | { |
| 224 | to_next[0] = template_bi; |
| 225 | to_next++; |
| 226 | nf->n_vectors++; |
| 227 | } |
| 228 | |
| 229 | nf = fr->flow_data_callback (frm, fr, |
| 230 | nf, to_next, ip4_lookup_node_index); |
| 231 | if (nf) |
| 232 | vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf); |
| 233 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 236 | return 0; /* not so much */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 239 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | VLIB_REGISTER_NODE (flow_report_process_node) = { |
| 241 | .function = flow_report_process, |
| 242 | .type = VLIB_NODE_TYPE_PROCESS, |
| 243 | .name = "flow-report-process", |
| 244 | }; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 245 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 247 | int |
| 248 | vnet_flow_report_add_del (flow_report_main_t * frm, |
| 249 | vnet_flow_report_add_del_args_t * a, |
| 250 | u16 * template_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | { |
| 252 | int i; |
| 253 | int found_index = ~0; |
| 254 | flow_report_t *fr; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 255 | flow_report_stream_t *stream; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 256 | u32 si; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 257 | |
| 258 | si = find_stream (a->domain_id, a->src_port); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 259 | if (si == -2) |
| 260 | return VNET_API_ERROR_INVALID_VALUE; |
| 261 | if (si == -1 && a->is_add == 0) |
| 262 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 263 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 264 | for (i = 0; i < vec_len (frm->reports); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | { |
| 266 | fr = vec_elt_at_index (frm->reports, i); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 267 | if (fr->opaque.as_uword == a->opaque.as_uword |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 268 | && fr->rewrite_callback == a->rewrite_callback |
| 269 | && fr->flow_data_callback == a->flow_data_callback) |
| 270 | { |
| 271 | found_index = i; |
| 272 | if (template_id) |
| 273 | *template_id = fr->template_id; |
| 274 | break; |
| 275 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (a->is_add == 0) |
| 279 | { |
| 280 | if (found_index != ~0) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 281 | { |
| 282 | vec_delete (frm->reports, 1, found_index); |
| 283 | stream = &frm->streams[si]; |
| 284 | stream->n_reports--; |
| 285 | if (stream->n_reports == 0) |
| 286 | delete_stream (si); |
| 287 | return 0; |
| 288 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 290 | } |
| 291 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 292 | if (found_index != ~0) |
| 293 | return VNET_API_ERROR_VALUE_EXIST; |
| 294 | |
| 295 | if (si == -1) |
| 296 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 297 | stream = add_stream (); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 298 | stream->domain_id = a->domain_id; |
| 299 | stream->src_port = a->src_port; |
| 300 | stream->sequence_number = 0; |
| 301 | stream->n_reports = 0; |
| 302 | si = stream - frm->streams; |
| 303 | } |
| 304 | else |
| 305 | stream = &frm->streams[si]; |
| 306 | |
| 307 | stream->n_reports++; |
| 308 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | vec_add2 (frm->reports, fr, 1); |
| 310 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 311 | fr->stream_index = si; |
| 312 | fr->template_id = 256 + stream->next_template_no; |
| 313 | stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 314 | fr->update_rewrite = 1; |
| 315 | fr->opaque = a->opaque; |
| 316 | fr->rewrite_callback = a->rewrite_callback; |
| 317 | fr->flow_data_callback = a->flow_data_callback; |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 318 | |
| 319 | if (template_id) |
| 320 | *template_id = fr->template_id; |
| 321 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 325 | clib_error_t * |
| 326 | flow_report_add_del_error_to_clib_error (int error) |
Juraj Sloboda | 24648ad | 2016-09-06 04:43:52 -0700 | [diff] [blame] | 327 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 328 | switch (error) |
| 329 | { |
| 330 | case 0: |
| 331 | return 0; |
| 332 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 333 | return clib_error_return (0, "Flow report not found"); |
| 334 | case VNET_API_ERROR_VALUE_EXIST: |
| 335 | return clib_error_return (0, "Flow report already exists"); |
| 336 | case VNET_API_ERROR_INVALID_VALUE: |
| 337 | return clib_error_return (0, "Expecting either still unused values " |
| 338 | "for both domain_id and src_port " |
| 339 | "or already used values for both fields"); |
| 340 | default: |
| 341 | return clib_error_return (0, "vnet_flow_report_add_del returned %d", |
| 342 | error); |
| 343 | } |
Juraj Sloboda | 24648ad | 2016-09-06 04:43:52 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 346 | void |
| 347 | vnet_flow_reports_reset (flow_report_main_t * frm) |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 348 | { |
| 349 | flow_report_t *fr; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 350 | u32 i; |
| 351 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 352 | for (i = 0; i < vec_len (frm->streams); i++) |
| 353 | if (stream_index_valid (i)) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 354 | frm->streams[i].sequence_number = 0; |
| 355 | |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 356 | vec_foreach (fr, frm->reports) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 357 | { |
| 358 | fr->update_rewrite = 1; |
| 359 | fr->last_template_sent = 0; |
| 360 | } |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 363 | void |
| 364 | vnet_stream_reset (flow_report_main_t * frm, u32 stream_index) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 365 | { |
| 366 | flow_report_t *fr; |
| 367 | |
| 368 | frm->streams[stream_index].sequence_number = 0; |
| 369 | |
| 370 | vec_foreach (fr, frm->reports) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 371 | if (frm->reports->stream_index == stream_index) |
| 372 | { |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 373 | fr->update_rewrite = 1; |
| 374 | fr->last_template_sent = 0; |
| 375 | } |
| 376 | } |
| 377 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 378 | int |
| 379 | vnet_stream_change (flow_report_main_t * frm, |
| 380 | u32 old_domain_id, u16 old_src_port, |
| 381 | u32 new_domain_id, u16 new_src_port) |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 382 | { |
| 383 | i32 stream_index = find_stream (old_domain_id, old_src_port); |
| 384 | if (stream_index < 0) |
| 385 | return 1; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 386 | flow_report_stream_t *stream = &frm->streams[stream_index]; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 387 | stream->domain_id = new_domain_id; |
| 388 | stream->src_port = new_src_port; |
| 389 | if (old_domain_id != new_domain_id || old_src_port != new_src_port) |
| 390 | vnet_stream_reset (frm, stream_index); |
| 391 | return 0; |
| 392 | } |
| 393 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | static clib_error_t * |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 395 | set_ipfix_exporter_command_fn (vlib_main_t * vm, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 396 | unformat_input_t * input, |
| 397 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 399 | flow_report_main_t *frm = &flow_report_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | ip4_address_t collector, src; |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 401 | u16 collector_port = UDP_DST_PORT_ipfix; |
Juraj Sloboda | 86634f0 | 2016-07-01 06:12:58 -0700 | [diff] [blame] | 402 | u32 fib_id; |
| 403 | u32 fib_index = ~0; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 404 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | collector.as_u32 = 0; |
| 406 | src.as_u32 = 0; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 407 | u32 path_mtu = 512; // RFC 7011 section 10.3.3. |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 408 | u32 template_interval = 20; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 409 | u8 udp_checksum = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 411 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 412 | { |
| 413 | if (unformat (input, "collector %U", unformat_ip4_address, &collector)) |
| 414 | ; |
| 415 | else if (unformat (input, "port %u", &collector_port)) |
| 416 | ; |
| 417 | else if (unformat (input, "src %U", unformat_ip4_address, &src)) |
| 418 | ; |
| 419 | else if (unformat (input, "fib-id %u", &fib_id)) |
| 420 | { |
| 421 | ip4_main_t *im = &ip4_main; |
| 422 | uword *p = hash_get (im->fib_index_by_table_id, fib_id); |
| 423 | if (!p) |
| 424 | return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id); |
| 425 | fib_index = p[0]; |
| 426 | } |
| 427 | else if (unformat (input, "path-mtu %u", &path_mtu)) |
| 428 | ; |
| 429 | else if (unformat (input, "template-interval %u", &template_interval)) |
| 430 | ; |
| 431 | else if (unformat (input, "udp-checksum")) |
| 432 | udp_checksum = 1; |
| 433 | else |
| 434 | break; |
| 435 | } |
| 436 | |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 437 | if (collector.as_u32 != 0 && src.as_u32 == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | return clib_error_return (0, "src address required"); |
| 439 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 440 | if (path_mtu > 1450 /* vpp does not support fragmentation */ ) |
| 441 | 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] | 442 | |
| 443 | if (path_mtu < 68) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 444 | 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] | 445 | |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 446 | /* Reset report streams if we are reconfiguring IP addresses */ |
| 447 | if (frm->ipfix_collector.as_u32 != collector.as_u32 || |
| 448 | frm->src_address.as_u32 != src.as_u32 || |
| 449 | frm->collector_port != collector_port) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 450 | vnet_flow_reports_reset (frm); |
Juraj Sloboda | 618ab08 | 2016-07-06 06:11:00 -0700 | [diff] [blame] | 451 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | frm->ipfix_collector.as_u32 = collector.as_u32; |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 453 | frm->collector_port = collector_port; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | frm->src_address.as_u32 = src.as_u32; |
Juraj Sloboda | 86634f0 | 2016-07-01 06:12:58 -0700 | [diff] [blame] | 455 | frm->fib_index = fib_index; |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 456 | frm->path_mtu = path_mtu; |
| 457 | frm->template_interval = template_interval; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 458 | frm->udp_checksum = udp_checksum; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 459 | |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 460 | if (collector.as_u32) |
| 461 | vlib_cli_output (vm, "Collector %U, src address %U, " |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 462 | "fib index %d, path MTU %u, " |
| 463 | "template resend interval %us, " |
| 464 | "udp checksum %s", |
| 465 | format_ip4_address, &frm->ipfix_collector, |
| 466 | format_ip4_address, &frm->src_address, |
| 467 | fib_index, path_mtu, template_interval, |
| 468 | udp_checksum ? "enabled" : "disabled"); |
Ole Troan | 5c74973 | 2017-03-13 13:39:52 +0100 | [diff] [blame] | 469 | else |
| 470 | vlib_cli_output (vm, "IPFIX Collector is disabled"); |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 471 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | /* Turn on the flow reporting process */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 473 | vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 477 | /* *INDENT-OFF* */ |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 478 | VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = { |
| 479 | .path = "set ipfix exporter", |
| 480 | .short_help = "set ipfix exporter " |
| 481 | "collector <ip4-address> [port <port>] " |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 482 | "src <ip4-address> [fib-id <fib-id>] " |
| 483 | "[path-mtu <path-mtu>] " |
| 484 | "[template-interval <template-interval>]", |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 485 | "[udp-checksum]", |
| 486 | .function = set_ipfix_exporter_command_fn, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | }; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 488 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 490 | |
| 491 | static clib_error_t * |
| 492 | ipfix_flush_command_fn (vlib_main_t * vm, |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 493 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 494 | { |
| 495 | /* poke the flow reporting process */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 496 | vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0); |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 500 | /* *INDENT-OFF* */ |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 501 | VLIB_CLI_COMMAND (ipfix_flush_command, static) = { |
| 502 | .path = "ipfix flush", |
| 503 | .short_help = "flush the current ipfix data [for make test]", |
| 504 | .function = ipfix_flush_command_fn, |
| 505 | }; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 506 | /* *INDENT-ON* */ |
Dave Barach | 0f3b680 | 2016-12-23 15:15:48 -0500 | [diff] [blame] | 507 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 508 | static clib_error_t * |
| 509 | flow_report_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | { |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 511 | flow_report_main_t *frm = &flow_report_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | |
| 513 | frm->vlib_main = vm; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 514 | frm->vnet_main = vnet_get_main (); |
| 515 | frm->unix_time_0 = time (0); |
| 516 | frm->vlib_time_0 = vlib_time_now (frm->vlib_main); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 517 | frm->fib_index = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | VLIB_INIT_FUNCTION (flow_report_init) |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 523 | /* |
| 524 | * fd.io coding-style-patch-verification: ON |
| 525 | * |
| 526 | * Local Variables: |
| 527 | * eval: (c-set-style "gnu") |
| 528 | * End: |
| 529 | */ |