blob: 959671d375164e11cc1efed49b2c40a5eec237ec [file] [log] [blame]
Pavel Kotucekeb9e6662017-01-24 13:40:26 +01001/*
2 *------------------------------------------------------------------
3 * flow_api.c - flow api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25
26#include <vnet/fib/fib_table.h>
Ole Troana9855ef2018-05-02 12:45:10 +020027#include <vnet/ipfix-export/flow_report.h>
28#include <vnet/ipfix-export/flow_report_classify.h>
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010029
30#include <vnet/vnet_msg_enum.h>
31
32#define vl_typedefs /* define message structures */
33#include <vnet/vnet_all_api_h.h>
34#undef vl_typedefs
35
36#define vl_endianfun /* define message structures */
37#include <vnet/vnet_all_api_h.h>
38#undef vl_endianfun
39
40/* instantiate all the print functions we know about */
41#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42#define vl_printfun
43#include <vnet/vnet_all_api_h.h>
44#undef vl_printfun
45
46#include <vlibapi/api_helper_macros.h>
47
48#define foreach_vpe_api_msg \
49_(SET_IPFIX_EXPORTER, set_ipfix_exporter) \
50_(IPFIX_EXPORTER_DUMP, ipfix_exporter_dump) \
51_(SET_IPFIX_CLASSIFY_STREAM, set_ipfix_classify_stream) \
52_(IPFIX_CLASSIFY_STREAM_DUMP, ipfix_classify_stream_dump) \
53_(IPFIX_CLASSIFY_TABLE_ADD_DEL, ipfix_classify_table_add_del) \
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040054_(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump) \
55_(IPFIX_FLUSH, ipfix_flush)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010056
57static void
58vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
59{
60 vlib_main_t *vm = vlib_get_main ();
61 flow_report_main_t *frm = &flow_report_main;
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040062 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010063 vl_api_set_ipfix_exporter_reply_t *rmp;
64 ip4_address_t collector, src;
65 u16 collector_port = UDP_DST_PORT_ipfix;
66 u32 path_mtu;
67 u32 template_interval;
68 u8 udp_checksum;
69 u32 fib_id;
70 u32 fib_index = ~0;
71 int rv = 0;
72
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040073 reg = vl_api_client_index_to_registration (mp->client_index);
74 if (!reg)
75 return;
76
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010077 memcpy (collector.data, mp->collector_address, sizeof (collector.data));
78 collector_port = ntohs (mp->collector_port);
79 if (collector_port == (u16) ~ 0)
80 collector_port = UDP_DST_PORT_ipfix;
81 memcpy (src.data, mp->src_address, sizeof (src.data));
82 fib_id = ntohl (mp->vrf_id);
83
84 ip4_main_t *im = &ip4_main;
85 if (fib_id == ~0)
86 {
87 fib_index = ~0;
88 }
89 else
90 {
91 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
92 if (!p)
93 {
94 rv = VNET_API_ERROR_NO_SUCH_FIB;
95 goto out;
96 }
97 fib_index = p[0];
98 }
99
100 path_mtu = ntohl (mp->path_mtu);
101 if (path_mtu == ~0)
102 path_mtu = 512; // RFC 7011 section 10.3.3.
103 template_interval = ntohl (mp->template_interval);
104 if (template_interval == ~0)
105 template_interval = 20;
106 udp_checksum = mp->udp_checksum;
107
108 if (collector.as_u32 == 0)
109 {
110 rv = VNET_API_ERROR_INVALID_VALUE;
111 goto out;
112 }
113
114 if (src.as_u32 == 0)
115 {
116 rv = VNET_API_ERROR_INVALID_VALUE;
117 goto out;
118 }
119
120 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
121 {
122 rv = VNET_API_ERROR_INVALID_VALUE;
123 goto out;
124 }
125
126 if (path_mtu < 68)
127 {
128 rv = VNET_API_ERROR_INVALID_VALUE;
129 goto out;
130 }
131
132 /* Reset report streams if we are reconfiguring IP addresses */
133 if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
134 frm->src_address.as_u32 != src.as_u32 ||
135 frm->collector_port != collector_port)
136 vnet_flow_reports_reset (frm);
137
138 frm->ipfix_collector.as_u32 = collector.as_u32;
139 frm->collector_port = collector_port;
140 frm->src_address.as_u32 = src.as_u32;
141 frm->fib_index = fib_index;
142 frm->path_mtu = path_mtu;
143 frm->template_interval = template_interval;
144 frm->udp_checksum = udp_checksum;
145
146 /* Turn on the flow reporting process */
147 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
148
149out:
150 REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
151}
152
153static void
154vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
155{
156 flow_report_main_t *frm = &flow_report_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800157 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100158 vl_api_ipfix_exporter_details_t *rmp;
159 ip4_main_t *im = &ip4_main;
160 u32 vrf_id;
161
Florin Coras6c4dae22018-01-09 06:39:23 -0800162 reg = vl_api_client_index_to_registration (mp->client_index);
163 if (!reg)
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400164 return;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100165
166 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400167 clib_memset (rmp, 0, sizeof (*rmp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100168 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
169 rmp->context = mp->context;
170 memcpy (rmp->collector_address, frm->ipfix_collector.data,
171 sizeof (frm->ipfix_collector.data));
172 rmp->collector_port = htons (frm->collector_port);
173 memcpy (rmp->src_address, frm->src_address.data,
174 sizeof (frm->src_address.data));
175 if (frm->fib_index == ~0)
176 vrf_id = ~0;
177 else
178 vrf_id = im->fibs[frm->fib_index].ft_table_id;
179 rmp->vrf_id = htonl (vrf_id);
180 rmp->path_mtu = htonl (frm->path_mtu);
181 rmp->template_interval = htonl (frm->template_interval);
182 rmp->udp_checksum = (frm->udp_checksum != 0);
183
Florin Coras6c4dae22018-01-09 06:39:23 -0800184 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100185}
186
187static void
188 vl_api_set_ipfix_classify_stream_t_handler
189 (vl_api_set_ipfix_classify_stream_t * mp)
190{
191 vl_api_set_ipfix_classify_stream_reply_t *rmp;
192 flow_report_classify_main_t *fcm = &flow_report_classify_main;
193 flow_report_main_t *frm = &flow_report_main;
194 u32 domain_id = 0;
195 u32 src_port = UDP_DST_PORT_ipfix;
196 int rv = 0;
197
198 domain_id = ntohl (mp->domain_id);
199 src_port = ntohs (mp->src_port);
200
201 if (fcm->src_port != 0 &&
202 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
203 {
204 int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
205 domain_id, (u16) src_port);
206 ASSERT (rv == 0);
207 }
208
209 fcm->domain_id = domain_id;
210 fcm->src_port = (u16) src_port;
211
212 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
213}
214
215static void
216 vl_api_ipfix_classify_stream_dump_t_handler
217 (vl_api_ipfix_classify_stream_dump_t * mp)
218{
219 flow_report_classify_main_t *fcm = &flow_report_classify_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800220 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100221 vl_api_ipfix_classify_stream_details_t *rmp;
222
Florin Coras6c4dae22018-01-09 06:39:23 -0800223 reg = vl_api_client_index_to_registration (mp->client_index);
224 if (!reg)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100225 return;
226
227 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400228 clib_memset (rmp, 0, sizeof (*rmp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100229 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
230 rmp->context = mp->context;
231 rmp->domain_id = htonl (fcm->domain_id);
232 rmp->src_port = htons (fcm->src_port);
233
Florin Coras6c4dae22018-01-09 06:39:23 -0800234 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100235}
236
237static void
238 vl_api_ipfix_classify_table_add_del_t_handler
239 (vl_api_ipfix_classify_table_add_del_t * mp)
240{
241 vl_api_ipfix_classify_table_add_del_reply_t *rmp;
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400242 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100243 flow_report_classify_main_t *fcm = &flow_report_classify_main;
244 flow_report_main_t *frm = &flow_report_main;
245 vnet_flow_report_add_del_args_t args;
246 ipfix_classify_table_t *table;
247 int is_add;
248 u32 classify_table_index;
249 u8 ip_version;
250 u8 transport_protocol;
251 int rv = 0;
252
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400253 reg = vl_api_client_index_to_registration (mp->client_index);
254 if (!reg)
255 return;
256
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100257 classify_table_index = ntohl (mp->table_id);
258 ip_version = mp->ip_version;
259 transport_protocol = mp->transport_protocol;
260 is_add = mp->is_add;
261
262 if (fcm->src_port == 0)
263 {
264 /* call set_ipfix_classify_stream first */
265 rv = VNET_API_ERROR_UNSPECIFIED;
266 goto out;
267 }
268
Dave Barachb7b92992018-10-17 10:38:51 -0400269 clib_memset (&args, 0, sizeof (args));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100270
271 table = 0;
272 int i;
273 for (i = 0; i < vec_len (fcm->tables); i++)
274 if (ipfix_classify_table_index_valid (i))
275 if (fcm->tables[i].classify_table_index == classify_table_index)
276 {
277 table = &fcm->tables[i];
278 break;
279 }
280
281 if (is_add)
282 {
283 if (table)
284 {
285 rv = VNET_API_ERROR_VALUE_EXIST;
286 goto out;
287 }
288 table = ipfix_classify_add_table ();
289 table->classify_table_index = classify_table_index;
290 }
291 else
292 {
293 if (!table)
294 {
295 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
296 goto out;
297 }
298 }
299
300 table->ip_version = ip_version;
301 table->transport_protocol = transport_protocol;
302
303 args.opaque.as_uword = table - fcm->tables;
304 args.rewrite_callback = ipfix_classify_template_rewrite;
305 args.flow_data_callback = ipfix_classify_send_flows;
306 args.is_add = is_add;
307 args.domain_id = fcm->domain_id;
308 args.src_port = fcm->src_port;
309
Ole Troan5c749732017-03-13 13:39:52 +0100310 rv = vnet_flow_report_add_del (frm, &args, NULL);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100311
312 /* If deleting, or add failed */
313 if (is_add == 0 || (rv && is_add))
314 ipfix_classify_delete_table (table - fcm->tables);
315
316out:
317 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
318}
319
320static void
321send_ipfix_classify_table_details (u32 table_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800322 vl_api_registration_t * reg, u32 context)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100323{
324 flow_report_classify_main_t *fcm = &flow_report_classify_main;
325 vl_api_ipfix_classify_table_details_t *mp;
326
327 ipfix_classify_table_t *table = &fcm->tables[table_index];
328
329 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400330 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100331 mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
332 mp->context = context;
333 mp->table_id = htonl (table->classify_table_index);
334 mp->ip_version = table->ip_version;
335 mp->transport_protocol = table->transport_protocol;
336
Florin Coras6c4dae22018-01-09 06:39:23 -0800337 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100338}
339
340static void
341 vl_api_ipfix_classify_table_dump_t_handler
342 (vl_api_ipfix_classify_table_dump_t * mp)
343{
344 flow_report_classify_main_t *fcm = &flow_report_classify_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800345 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100346 u32 i;
347
Florin Coras6c4dae22018-01-09 06:39:23 -0800348 reg = vl_api_client_index_to_registration (mp->client_index);
349 if (!reg)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100350 return;
351
352 for (i = 0; i < vec_len (fcm->tables); i++)
353 if (ipfix_classify_table_index_valid (i))
Florin Coras6c4dae22018-01-09 06:39:23 -0800354 send_ipfix_classify_table_details (i, reg, mp->context);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100355}
356
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400357static void
358vl_api_ipfix_flush_t_handler (vl_api_ipfix_flush_t * mp)
359{
360 vl_api_ipfix_flush_reply_t *rmp;
361 vl_api_registration_t *reg;
362 vlib_main_t *vm = vlib_get_main ();
363 int rv = 0;
364
365 reg = vl_api_client_index_to_registration (mp->client_index);
366 if (!reg)
367 return;
368
369 /* poke the flow reporting process */
370 vlib_process_signal_event (vm, flow_report_process_node.index,
371 1 /* type_opaque */ , 0 /* data */ );
372
373 REPLY_MACRO (VL_API_IPFIX_FLUSH_REPLY);
374}
375
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100376/*
377 * flow_api_hookup
378 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500379 * vlib has already mapped shared memory and
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100380 * added the client registration handlers.
381 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
382 */
383#define vl_msg_name_crc_list
384#include <vnet/vnet_all_api_h.h>
385#undef vl_msg_name_crc_list
386
387static void
388setup_message_id_table (api_main_t * am)
389{
390#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
Ole Troana9855ef2018-05-02 12:45:10 +0200391 foreach_vl_msg_name_crc_ipfix_export;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100392#undef _
393}
394
395static clib_error_t *
396flow_api_hookup (vlib_main_t * vm)
397{
398 api_main_t *am = &api_main;
399
400#define _(N,n) \
401 vl_msg_api_set_handlers(VL_API_##N, #n, \
402 vl_api_##n##_t_handler, \
403 vl_noop_handler, \
404 vl_api_##n##_t_endian, \
405 vl_api_##n##_t_print, \
406 sizeof(vl_api_##n##_t), 1);
407 foreach_vpe_api_msg;
408#undef _
409
410 /*
411 * Set up the (msg_name, crc, message-id) table
412 */
413 setup_message_id_table (am);
414
415 return 0;
416}
417
418VLIB_API_INIT_FUNCTION (flow_api_hookup);
419
420/*
421 * fd.io coding-style-patch-verification: ON
422 *
423 * Local Variables:
424 * eval: (c-set-style "gnu")
425 * End:
426 */