blob: 38c2454faefdf1063d69d17d3ded3d83a3470e2a [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Troana9855ef2018-05-02 12:45:10 +020018#include <vnet/ipfix-export/flow_report.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070019#include <vnet/api_errno.h>
Florin Corasb040f982020-10-20 14:59:43 -070020#include <vnet/udp/udp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
Juraj Sloboda837fbb12016-07-06 23:11:47 -070022flow_report_main_t flow_report_main;
23
Swarup Nayak6bcac062017-11-26 23:11:40 +053024static_always_inline u8
Paul Atkins9e827812021-09-22 08:15:03 +010025stream_index_valid (ipfix_exporter_t *exp, u32 index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070026{
Paul Atkins9ec64492021-09-21 20:49:12 +010027 return index < vec_len (exp->streams) && exp->streams[index].domain_id != ~0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070028}
29
Swarup Nayak6bcac062017-11-26 23:11:40 +053030static_always_inline flow_report_stream_t *
Paul Atkins9e827812021-09-22 08:15:03 +010031add_stream (ipfix_exporter_t *exp)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070032{
Juraj Slobodaffa652a2016-08-07 23:43:42 -070033 u32 i;
Paul Atkins9ec64492021-09-21 20:49:12 +010034 for (i = 0; i < vec_len (exp->streams); i++)
Paul Atkins9e827812021-09-22 08:15:03 +010035 if (!stream_index_valid (exp, i))
Paul Atkins9ec64492021-09-21 20:49:12 +010036 return &exp->streams[i];
37 u32 index = vec_len (exp->streams);
38 vec_validate (exp->streams, index);
39 return &exp->streams[index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -070040}
41
Swarup Nayak6bcac062017-11-26 23:11:40 +053042static_always_inline void
Paul Atkins9e827812021-09-22 08:15:03 +010043delete_stream (ipfix_exporter_t *exp, u32 index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070044{
Paul Atkins9ec64492021-09-21 20:49:12 +010045 ASSERT (index < vec_len (exp->streams));
46 ASSERT (exp->streams[index].domain_id != ~0);
47 exp->streams[index].domain_id = ~0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070048}
49
Swarup Nayak6bcac062017-11-26 23:11:40 +053050static i32
Paul Atkins9e827812021-09-22 08:15:03 +010051find_stream (ipfix_exporter_t *exp, u32 domain_id, u16 src_port)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070052{
Swarup Nayak6bcac062017-11-26 23:11:40 +053053 flow_report_stream_t *stream;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070054 u32 i;
Paul Atkins9ec64492021-09-21 20:49:12 +010055 for (i = 0; i < vec_len (exp->streams); i++)
Paul Atkins9e827812021-09-22 08:15:03 +010056 if (stream_index_valid (exp, i))
Swarup Nayak6bcac062017-11-26 23:11:40 +053057 {
Paul Atkins9ec64492021-09-21 20:49:12 +010058 stream = &exp->streams[i];
Swarup Nayak6bcac062017-11-26 23:11:40 +053059 if (domain_id == stream->domain_id)
60 {
61 if (src_port != stream->src_port)
62 return -2;
63 return i;
64 }
65 else if (src_port == stream->src_port)
66 {
67 return -2;
68 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -070069 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -070070 return -1;
71}
72
Swarup Nayak6bcac062017-11-26 23:11:40 +053073int
Paul Atkinsd747dd92021-09-22 14:56:17 +010074send_template_packet (flow_report_main_t *frm, ipfix_exporter_t *exp,
75 flow_report_t *fr, u32 *buffer_indexp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076{
77 u32 bi0;
Swarup Nayak6bcac062017-11-26 23:11:40 +053078 vlib_buffer_t *b0;
79 ip4_ipfix_template_packet_t *tp;
80 ipfix_message_header_t *h;
81 ip4_header_t *ip;
82 udp_header_t *udp;
83 vlib_main_t *vm = frm->vlib_main;
84 flow_report_stream_t *stream;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
86 ASSERT (buffer_indexp);
87
88 if (fr->update_rewrite || fr->rewrite == 0)
89 {
Paul Atkins9ec64492021-09-21 20:49:12 +010090 if (exp->ipfix_collector.as_u32 == 0 || exp->src_address.as_u32 == 0)
Swarup Nayak6bcac062017-11-26 23:11:40 +053091 {
92 vlib_node_set_state (frm->vlib_main, flow_report_process_node.index,
93 VLIB_NODE_STATE_DISABLED);
94 return -1;
95 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 vec_free (fr->rewrite);
97 fr->update_rewrite = 1;
98 }
99
100 if (fr->update_rewrite)
101 {
Paul Atkins9ec64492021-09-21 20:49:12 +0100102 fr->rewrite = fr->rewrite_callback (
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100103 exp, fr, &exp->ipfix_collector, &exp->src_address, exp->collector_port,
Paul Atkins9ec64492021-09-21 20:49:12 +0100104 fr->report_elements, fr->n_report_elements, fr->stream_indexp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 fr->update_rewrite = 0;
106 }
107
108 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
109 return -1;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530110
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 b0 = vlib_get_buffer (vm, bi0);
112
Damjan Marion8934a042019-02-09 23:29:26 +0100113 ASSERT (vec_len (fr->rewrite) < vlib_buffer_get_default_data_size (vm));
Swarup Nayak6bcac062017-11-26 23:11:40 +0530114
Dave Barach178cf492018-11-13 16:34:13 -0500115 clib_memcpy_fast (b0->data, fr->rewrite, vec_len (fr->rewrite));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 b0->current_data = 0;
117 b0->current_length = vec_len (fr->rewrite);
Damjan Mariondac03522018-02-01 15:30:13 +0100118 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
Paul Atkins9ec64492021-09-21 20:49:12 +0100120 vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
122 tp = vlib_buffer_get_current (b0);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530123 ip = (ip4_header_t *) & tp->ip4;
124 udp = (udp_header_t *) (ip + 1);
125 h = (ipfix_message_header_t *) (udp + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Swarup Nayak6bcac062017-11-26 23:11:40 +0530127 /* FIXUP: message header export_time */
128 h->export_time = (u32)
129 (((f64) frm->unix_time_0) +
130 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
131 h->export_time = clib_host_to_net_u32 (h->export_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
Paul Atkins9ec64492021-09-21 20:49:12 +0100133 stream = &exp->streams[fr->stream_index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700134
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 /* FIXUP: message header sequence_number. Templates do not increase it */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530136 h->sequence_number = clib_host_to_net_u32 (stream->sequence_number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
138 /* FIXUP: udp length */
139 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
140
Paul Atkins9ec64492021-09-21 20:49:12 +0100141 if (exp->udp_checksum)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700142 {
143 /* RFC 7011 section 10.3.2. */
144 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
145 if (udp->checksum == 0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530146 udp->checksum = 0xffff;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700147 }
148
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 *buffer_indexp = bi0;
Juraj Sloboda0d2a8e72016-07-07 02:59:28 -0700150
151 fr->last_template_sent = vlib_time_now (vm);
152
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 return 0;
154}
155
Dave Barach2be45812018-05-13 08:50:25 -0400156u8 *
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100157vnet_flow_rewrite_generic_callback (ipfix_exporter_t *exp, flow_report_t *fr,
158 ip4_address_t *collector_address,
159 ip4_address_t *src_address,
Dave Barach2be45812018-05-13 08:50:25 -0400160 u16 collector_port,
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100161 ipfix_report_element_t *report_elts,
162 u32 n_elts, u32 *stream_indexp)
Dave Barach2be45812018-05-13 08:50:25 -0400163{
164 ip4_header_t *ip;
165 udp_header_t *udp;
166 ipfix_message_header_t *h;
167 ipfix_set_header_t *s;
168 ipfix_template_header_t *t;
169 ipfix_field_specifier_t *f;
170 ipfix_field_specifier_t *first_field;
171 u8 *rewrite = 0;
172 ip4_ipfix_template_packet_t *tp;
173 flow_report_stream_t *stream;
174 int i;
175 ipfix_report_element_t *ep;
176
177 ASSERT (stream_indexp);
178 ASSERT (n_elts);
179 ASSERT (report_elts);
180
Paul Atkins9ec64492021-09-21 20:49:12 +0100181 stream = &exp->streams[fr->stream_index];
Dave Barach2be45812018-05-13 08:50:25 -0400182 *stream_indexp = fr->stream_index;
183
184 /* allocate rewrite space */
185 vec_validate_aligned (rewrite,
186 sizeof (ip4_ipfix_template_packet_t)
187 + n_elts * sizeof (ipfix_field_specifier_t) - 1,
188 CLIB_CACHE_LINE_BYTES);
189
190 /* create the packet rewrite string */
191 tp = (ip4_ipfix_template_packet_t *) rewrite;
192 ip = (ip4_header_t *) & tp->ip4;
193 udp = (udp_header_t *) (ip + 1);
194 h = (ipfix_message_header_t *) (udp + 1);
195 s = (ipfix_set_header_t *) (h + 1);
196 t = (ipfix_template_header_t *) (s + 1);
197 first_field = f = (ipfix_field_specifier_t *) (t + 1);
198
199 ip->ip_version_and_header_length = 0x45;
200 ip->ttl = 254;
201 ip->protocol = IP_PROTOCOL_UDP;
202 ip->src_address.as_u32 = src_address->as_u32;
203 ip->dst_address.as_u32 = collector_address->as_u32;
204 udp->src_port = clib_host_to_net_u16 (stream->src_port);
205 udp->dst_port = clib_host_to_net_u16 (collector_port);
206 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
207
208 /* FIXUP LATER: message header export_time */
209 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
210
211 ep = report_elts;
212
213 for (i = 0; i < n_elts; i++)
214 {
215 f->e_id_length = ipfix_e_id_length (0, ep->info_element, ep->size);
216 f++;
217 ep++;
218 }
219
220 /* Back to the template packet... */
221 ip = (ip4_header_t *) & tp->ip4;
222 udp = (udp_header_t *) (ip + 1);
223
224 ASSERT (f - first_field);
225 /* Field count in this template */
226 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
227
228 /* set length in octets */
229 s->set_id_length =
230 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
231
232 /* message length in octets */
233 h->version_length = version_length ((u8 *) f - (u8 *) h);
234
235 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
236 ip->checksum = ip4_header_checksum (ip);
237
238 return rewrite;
239}
240
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241static uword
242flow_report_process (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530243 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530245 flow_report_main_t *frm = &flow_report_main;
246 flow_report_t *fr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247 u32 ip4_lookup_node_index;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530248 vlib_node_t *ip4_lookup_node;
249 vlib_frame_t *nf = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250 u32 template_bi;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530251 u32 *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 int send_template;
Matthew Smithbaa18702021-04-28 11:48:39 -0500253 f64 now, wait_time;
254 f64 def_wait_time = 5.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 int rv;
256 uword event_type;
257 uword *event_data = 0;
258
259 /* Wait for Godot... */
260 vlib_process_wait_for_event_or_clock (vm, 1e9);
261 event_type = vlib_process_get_events (vm, &event_data);
262 if (event_type != 1)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530263 clib_warning ("bogus kickoff event received, %d", event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 vec_reset_length (event_data);
265
266 /* Enqueue pkts to ip4-lookup */
267 ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
268 ip4_lookup_node_index = ip4_lookup_node->index;
269
Matthew Smithbaa18702021-04-28 11:48:39 -0500270 wait_time = def_wait_time;
271
Swarup Nayak6bcac062017-11-26 23:11:40 +0530272 while (1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 {
Matthew Smithbaa18702021-04-28 11:48:39 -0500274 vlib_process_wait_for_event_or_clock (vm, wait_time);
Dave Barach0f3b6802016-12-23 15:15:48 -0500275 event_type = vlib_process_get_events (vm, &event_data);
276 vec_reset_length (event_data);
Paul Atkinsd747dd92021-09-22 14:56:17 +0100277 ipfix_exporter_t *exp;
278 pool_foreach (exp, frm->exporters)
Paul Atkins292992e2021-09-21 21:08:14 +0100279 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
Paul Atkinsd747dd92021-09-22 14:56:17 +0100281 /* 5s delay by default, possibly reduced by template intervals */
282 wait_time = def_wait_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
Paul Atkinsd747dd92021-09-22 14:56:17 +0100284 vec_foreach (fr, exp->reports)
Paul Atkins292992e2021-09-21 21:08:14 +0100285 {
Paul Atkinsd747dd92021-09-22 14:56:17 +0100286 f64 next_template;
287 now = vlib_time_now (vm);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530288
Paul Atkinsd747dd92021-09-22 14:56:17 +0100289 /* Need to send a template packet? */
290 send_template =
291 now > (fr->last_template_sent + exp->template_interval);
292 send_template += fr->last_template_sent == 0;
293 template_bi = ~0;
294 rv = 0;
295
296 if (send_template)
297 rv = send_template_packet (frm, exp, fr, &template_bi);
298
299 if (rv < 0)
300 continue;
301
302 /*
303 * decide if template should be sent sooner than current wait
304 * time
305 */
306 next_template =
307 (fr->last_template_sent + exp->template_interval) - now;
308 wait_time = clib_min (wait_time, next_template);
309
310 nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
311 nf->n_vectors = 0;
312 to_next = vlib_frame_vector_args (nf);
313
314 if (template_bi != ~0)
315 {
316 to_next[0] = template_bi;
317 to_next++;
318 nf->n_vectors++;
319 }
320
321 nf = fr->flow_data_callback (frm, exp, fr, nf, to_next,
322 ip4_lookup_node_index);
323 if (nf)
324 vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
325 }
Paul Atkins292992e2021-09-21 21:08:14 +0100326 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 }
328
Swarup Nayak6bcac062017-11-26 23:11:40 +0530329 return 0; /* not so much */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330}
331
Swarup Nayak6bcac062017-11-26 23:11:40 +0530332/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333VLIB_REGISTER_NODE (flow_report_process_node) = {
334 .function = flow_report_process,
335 .type = VLIB_NODE_TYPE_PROCESS,
336 .name = "flow-report-process",
337};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530338/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339
Swarup Nayak6bcac062017-11-26 23:11:40 +0530340int
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100341vnet_flow_report_add_del (ipfix_exporter_t *exp,
342 vnet_flow_report_add_del_args_t *a, u16 *template_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343{
344 int i;
345 int found_index = ~0;
346 flow_report_t *fr;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530347 flow_report_stream_t *stream;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700348 u32 si;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530349
Paul Atkins9e827812021-09-22 08:15:03 +0100350 si = find_stream (exp, a->domain_id, a->src_port);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700351 if (si == -2)
352 return VNET_API_ERROR_INVALID_VALUE;
353 if (si == -1 && a->is_add == 0)
354 return VNET_API_ERROR_NO_SUCH_ENTRY;
355
Paul Atkins9ec64492021-09-21 20:49:12 +0100356 for (i = 0; i < vec_len (exp->reports); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357 {
Paul Atkins9ec64492021-09-21 20:49:12 +0100358 fr = vec_elt_at_index (exp->reports, i);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700359 if (fr->opaque.as_uword == a->opaque.as_uword
Swarup Nayak6bcac062017-11-26 23:11:40 +0530360 && fr->rewrite_callback == a->rewrite_callback
361 && fr->flow_data_callback == a->flow_data_callback)
362 {
363 found_index = i;
364 if (template_id)
365 *template_id = fr->template_id;
366 break;
367 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 }
369
370 if (a->is_add == 0)
371 {
372 if (found_index != ~0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530373 {
Paul Atkins9ec64492021-09-21 20:49:12 +0100374 vec_delete (exp->reports, 1, found_index);
375 stream = &exp->streams[si];
Swarup Nayak6bcac062017-11-26 23:11:40 +0530376 stream->n_reports--;
377 if (stream->n_reports == 0)
Paul Atkins9e827812021-09-22 08:15:03 +0100378 delete_stream (exp, si);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530379 return 0;
380 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 return VNET_API_ERROR_NO_SUCH_ENTRY;
382 }
383
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700384 if (found_index != ~0)
385 return VNET_API_ERROR_VALUE_EXIST;
386
387 if (si == -1)
388 {
Paul Atkins9e827812021-09-22 08:15:03 +0100389 stream = add_stream (exp);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700390 stream->domain_id = a->domain_id;
391 stream->src_port = a->src_port;
392 stream->sequence_number = 0;
393 stream->n_reports = 0;
Paul Atkins9ec64492021-09-21 20:49:12 +0100394 si = stream - exp->streams;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700395 }
396 else
Paul Atkins9ec64492021-09-21 20:49:12 +0100397 stream = &exp->streams[si];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700398
399 stream->n_reports++;
400
Paul Atkins9ec64492021-09-21 20:49:12 +0100401 vec_add2 (exp->reports, fr, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700403 fr->stream_index = si;
404 fr->template_id = 256 + stream->next_template_no;
405 stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 fr->update_rewrite = 1;
407 fr->opaque = a->opaque;
408 fr->rewrite_callback = a->rewrite_callback;
409 fr->flow_data_callback = a->flow_data_callback;
Dave Barach2be45812018-05-13 08:50:25 -0400410 fr->report_elements = a->report_elements;
411 fr->n_report_elements = a->n_report_elements;
412 fr->stream_indexp = a->stream_indexp;
Ole Troan5c749732017-03-13 13:39:52 +0100413 if (template_id)
414 *template_id = fr->template_id;
415
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 return 0;
417}
418
Swarup Nayak6bcac062017-11-26 23:11:40 +0530419clib_error_t *
420flow_report_add_del_error_to_clib_error (int error)
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700421{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530422 switch (error)
423 {
424 case 0:
425 return 0;
426 case VNET_API_ERROR_NO_SUCH_ENTRY:
427 return clib_error_return (0, "Flow report not found");
428 case VNET_API_ERROR_VALUE_EXIST:
429 return clib_error_return (0, "Flow report already exists");
430 case VNET_API_ERROR_INVALID_VALUE:
431 return clib_error_return (0, "Expecting either still unused values "
432 "for both domain_id and src_port "
433 "or already used values for both fields");
434 default:
435 return clib_error_return (0, "vnet_flow_report_add_del returned %d",
436 error);
437 }
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700438}
439
Swarup Nayak6bcac062017-11-26 23:11:40 +0530440void
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100441vnet_flow_reports_reset (ipfix_exporter_t *exp)
Juraj Sloboda618ab082016-07-06 06:11:00 -0700442{
443 flow_report_t *fr;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700444 u32 i;
445
Paul Atkins9ec64492021-09-21 20:49:12 +0100446 for (i = 0; i < vec_len (exp->streams); i++)
Paul Atkins9e827812021-09-22 08:15:03 +0100447 if (stream_index_valid (exp, i))
Paul Atkins9ec64492021-09-21 20:49:12 +0100448 exp->streams[i].sequence_number = 0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700449
Paul Atkins9ec64492021-09-21 20:49:12 +0100450 vec_foreach (fr, exp->reports)
Paul Atkins292992e2021-09-21 21:08:14 +0100451 {
452 fr->update_rewrite = 1;
453 fr->last_template_sent = 0;
454 }
Juraj Sloboda618ab082016-07-06 06:11:00 -0700455}
456
Swarup Nayak6bcac062017-11-26 23:11:40 +0530457void
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100458vnet_stream_reset (ipfix_exporter_t *exp, u32 stream_index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700459{
460 flow_report_t *fr;
461
Paul Atkins9ec64492021-09-21 20:49:12 +0100462 exp->streams[stream_index].sequence_number = 0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700463
Paul Atkins9ec64492021-09-21 20:49:12 +0100464 vec_foreach (fr, exp->reports)
465 if (exp->reports->stream_index == stream_index)
Paul Atkins292992e2021-09-21 21:08:14 +0100466 {
467 fr->update_rewrite = 1;
468 fr->last_template_sent = 0;
469 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700470}
471
Swarup Nayak6bcac062017-11-26 23:11:40 +0530472int
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100473vnet_stream_change (ipfix_exporter_t *exp, u32 old_domain_id, u16 old_src_port,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530474 u32 new_domain_id, u16 new_src_port)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700475{
Paul Atkins9e827812021-09-22 08:15:03 +0100476 i32 stream_index = find_stream (exp, old_domain_id, old_src_port);
Paul Atkins9ec64492021-09-21 20:49:12 +0100477
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700478 if (stream_index < 0)
479 return 1;
Paul Atkins9ec64492021-09-21 20:49:12 +0100480 flow_report_stream_t *stream = &exp->streams[stream_index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700481 stream->domain_id = new_domain_id;
482 stream->src_port = new_src_port;
483 if (old_domain_id != new_domain_id || old_src_port != new_src_port)
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100484 vnet_stream_reset (exp, stream_index);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700485 return 0;
486}
487
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488static clib_error_t *
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700489set_ipfix_exporter_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530490 unformat_input_t * input,
491 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530493 flow_report_main_t *frm = &flow_report_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494 ip4_address_t collector, src;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700495 u16 collector_port = UDP_DST_PORT_ipfix;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700496 u32 fib_id;
497 u32 fib_index = ~0;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530498
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499 collector.as_u32 = 0;
500 src.as_u32 = 0;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530501 u32 path_mtu = 512; // RFC 7011 section 10.3.3.
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700502 u32 template_interval = 20;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700503 u8 udp_checksum = 0;
Paul Atkins9ec64492021-09-21 20:49:12 +0100504 ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
Swarup Nayak6bcac062017-11-26 23:11:40 +0530506 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
507 {
508 if (unformat (input, "collector %U", unformat_ip4_address, &collector))
509 ;
Elias Rudberg2dca1802020-05-27 01:03:46 +0200510 else if (unformat (input, "port %U", unformat_udp_port,
511 &collector_port))
Swarup Nayak6bcac062017-11-26 23:11:40 +0530512 ;
513 else if (unformat (input, "src %U", unformat_ip4_address, &src))
514 ;
515 else if (unformat (input, "fib-id %u", &fib_id))
516 {
517 ip4_main_t *im = &ip4_main;
518 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
519 if (!p)
520 return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
521 fib_index = p[0];
522 }
523 else if (unformat (input, "path-mtu %u", &path_mtu))
524 ;
525 else if (unformat (input, "template-interval %u", &template_interval))
526 ;
527 else if (unformat (input, "udp-checksum"))
528 udp_checksum = 1;
529 else
530 break;
531 }
532
Ole Troan5c749732017-03-13 13:39:52 +0100533 if (collector.as_u32 != 0 && src.as_u32 == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 return clib_error_return (0, "src address required");
535
Swarup Nayak6bcac062017-11-26 23:11:40 +0530536 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
537 return clib_error_return (0, "too big path-mtu value, maximum is 1450");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700538
539 if (path_mtu < 68)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530540 return clib_error_return (0, "too small path-mtu value, minimum is 68");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700541
Juraj Sloboda618ab082016-07-06 06:11:00 -0700542 /* Reset report streams if we are reconfiguring IP addresses */
Paul Atkins9ec64492021-09-21 20:49:12 +0100543 if (exp->ipfix_collector.as_u32 != collector.as_u32 ||
544 exp->src_address.as_u32 != src.as_u32 ||
545 exp->collector_port != collector_port)
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100546 vnet_flow_reports_reset (exp);
Juraj Sloboda618ab082016-07-06 06:11:00 -0700547
Paul Atkins9ec64492021-09-21 20:49:12 +0100548 exp->ipfix_collector.as_u32 = collector.as_u32;
549 exp->collector_port = collector_port;
550 exp->src_address.as_u32 = src.as_u32;
551 exp->fib_index = fib_index;
552 exp->path_mtu = path_mtu;
553 exp->template_interval = template_interval;
554 exp->udp_checksum = udp_checksum;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530555
Ole Troan5c749732017-03-13 13:39:52 +0100556 if (collector.as_u32)
Paul Atkins9ec64492021-09-21 20:49:12 +0100557 vlib_cli_output (vm,
558 "Collector %U, src address %U, "
Swarup Nayak6bcac062017-11-26 23:11:40 +0530559 "fib index %d, path MTU %u, "
560 "template resend interval %us, "
561 "udp checksum %s",
Paul Atkins9ec64492021-09-21 20:49:12 +0100562 format_ip4_address, exp->ipfix_collector,
563 format_ip4_address, exp->src_address, fib_index, path_mtu,
564 template_interval, udp_checksum ? "enabled" : "disabled");
Ole Troan5c749732017-03-13 13:39:52 +0100565 else
566 vlib_cli_output (vm, "IPFIX Collector is disabled");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700567
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568 /* Turn on the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530569 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 return 0;
571}
572
Swarup Nayak6bcac062017-11-26 23:11:40 +0530573/* *INDENT-OFF* */
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700574VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = {
575 .path = "set ipfix exporter",
576 .short_help = "set ipfix exporter "
577 "collector <ip4-address> [port <port>] "
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700578 "src <ip4-address> [fib-id <fib-id>] "
579 "[path-mtu <path-mtu>] "
Ignas Baciusf3a522f2020-02-18 12:33:09 +0200580 "[template-interval <template-interval>] "
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700581 "[udp-checksum]",
582 .function = set_ipfix_exporter_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530584/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Dave Barach0f3b6802016-12-23 15:15:48 -0500586
587static clib_error_t *
588ipfix_flush_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530589 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach0f3b6802016-12-23 15:15:48 -0500590{
591 /* poke the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530592 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Dave Barach0f3b6802016-12-23 15:15:48 -0500593 return 0;
594}
595
Swarup Nayak6bcac062017-11-26 23:11:40 +0530596/* *INDENT-OFF* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500597VLIB_CLI_COMMAND (ipfix_flush_command, static) = {
598 .path = "ipfix flush",
599 .short_help = "flush the current ipfix data [for make test]",
600 .function = ipfix_flush_command_fn,
601};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530602/* *INDENT-ON* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500603
Swarup Nayak6bcac062017-11-26 23:11:40 +0530604static clib_error_t *
605flow_report_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530607 flow_report_main_t *frm = &flow_report_main;
Paul Atkins9ec64492021-09-21 20:49:12 +0100608 ipfix_exporter_t *exp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609
610 frm->vlib_main = vm;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530611 frm->vnet_main = vnet_get_main ();
612 frm->unix_time_0 = time (0);
613 frm->vlib_time_0 = vlib_time_now (frm->vlib_main);
Paul Atkins9ec64492021-09-21 20:49:12 +0100614 /*
615 * Make sure that we can always access the first exporter for
616 * backwards compatibility reasons.
617 */
618 pool_alloc (frm->exporters, IPFIX_EXPORTERS_MAX);
619 pool_get (frm->exporters, exp);
620 /* Verify that this is at index 0 */
621 ASSERT (frm->exporters == exp);
622 exp->fib_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 return 0;
624}
625
Dave Barachf8d50682019-05-14 18:01:44 -0400626VLIB_INIT_FUNCTION (flow_report_init);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530627/*
628 * fd.io coding-style-patch-verification: ON
629 *
630 * Local Variables:
631 * eval: (c-set-style "gnu")
632 * End:
633 */