blob: 217cf31d6914bb2b251dc2b3c0cb3a3cba171a12 [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>
Jakub Grajciar2f71a882019-10-10 14:21:22 +020022#include <vnet/ip/ip_types_api.h>
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010023
24#include <vnet/interface.h>
25#include <vnet/api_errno.h>
26
27#include <vnet/fib/fib_table.h>
Ole Troana9855ef2018-05-02 12:45:10 +020028#include <vnet/ipfix-export/flow_report.h>
29#include <vnet/ipfix-export/flow_report_classify.h>
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010030
31#include <vnet/vnet_msg_enum.h>
32
33#define vl_typedefs /* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_typedefs
36
37#define vl_endianfun /* define message structures */
38#include <vnet/vnet_all_api_h.h>
39#undef vl_endianfun
40
41/* instantiate all the print functions we know about */
42#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43#define vl_printfun
44#include <vnet/vnet_all_api_h.h>
45#undef vl_printfun
46
47#include <vlibapi/api_helper_macros.h>
48
49#define foreach_vpe_api_msg \
50_(SET_IPFIX_EXPORTER, set_ipfix_exporter) \
51_(IPFIX_EXPORTER_DUMP, ipfix_exporter_dump) \
52_(SET_IPFIX_CLASSIFY_STREAM, set_ipfix_classify_stream) \
53_(IPFIX_CLASSIFY_STREAM_DUMP, ipfix_classify_stream_dump) \
54_(IPFIX_CLASSIFY_TABLE_ADD_DEL, ipfix_classify_table_add_del) \
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040055_(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump) \
56_(IPFIX_FLUSH, ipfix_flush)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010057
58static void
59vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
60{
61 vlib_main_t *vm = vlib_get_main ();
62 flow_report_main_t *frm = &flow_report_main;
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040063 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010064 vl_api_set_ipfix_exporter_reply_t *rmp;
65 ip4_address_t collector, src;
66 u16 collector_port = UDP_DST_PORT_ipfix;
67 u32 path_mtu;
68 u32 template_interval;
69 u8 udp_checksum;
70 u32 fib_id;
71 u32 fib_index = ~0;
72 int rv = 0;
73
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040074 reg = vl_api_client_index_to_registration (mp->client_index);
75 if (!reg)
76 return;
77
Jakub Grajciar2f71a882019-10-10 14:21:22 +020078 if (mp->src_address.af == ADDRESS_IP6
79 || mp->collector_address.af == ADDRESS_IP6)
80 {
81 rv = VNET_API_ERROR_UNIMPLEMENTED;
82 goto out;
83 }
84
85 ip4_address_decode (mp->collector_address.un.ip4, &collector);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010086 collector_port = ntohs (mp->collector_port);
87 if (collector_port == (u16) ~ 0)
88 collector_port = UDP_DST_PORT_ipfix;
Jakub Grajciar2f71a882019-10-10 14:21:22 +020089 ip4_address_decode (mp->src_address.un.ip4, &src);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010090 fib_id = ntohl (mp->vrf_id);
91
92 ip4_main_t *im = &ip4_main;
93 if (fib_id == ~0)
94 {
95 fib_index = ~0;
96 }
97 else
98 {
99 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
100 if (!p)
101 {
102 rv = VNET_API_ERROR_NO_SUCH_FIB;
103 goto out;
104 }
105 fib_index = p[0];
106 }
107
108 path_mtu = ntohl (mp->path_mtu);
109 if (path_mtu == ~0)
110 path_mtu = 512; // RFC 7011 section 10.3.3.
111 template_interval = ntohl (mp->template_interval);
112 if (template_interval == ~0)
113 template_interval = 20;
114 udp_checksum = mp->udp_checksum;
115
Alexander Chernavin67ec7522020-10-01 08:57:59 -0400116 if (collector.as_u32 != 0 && src.as_u32 == 0)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100117 {
118 rv = VNET_API_ERROR_INVALID_VALUE;
119 goto out;
120 }
121
122 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
123 {
124 rv = VNET_API_ERROR_INVALID_VALUE;
125 goto out;
126 }
127
128 if (path_mtu < 68)
129 {
130 rv = VNET_API_ERROR_INVALID_VALUE;
131 goto out;
132 }
133
134 /* Reset report streams if we are reconfiguring IP addresses */
135 if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
136 frm->src_address.as_u32 != src.as_u32 ||
137 frm->collector_port != collector_port)
138 vnet_flow_reports_reset (frm);
139
140 frm->ipfix_collector.as_u32 = collector.as_u32;
141 frm->collector_port = collector_port;
142 frm->src_address.as_u32 = src.as_u32;
143 frm->fib_index = fib_index;
144 frm->path_mtu = path_mtu;
145 frm->template_interval = template_interval;
146 frm->udp_checksum = udp_checksum;
147
148 /* Turn on the flow reporting process */
149 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
150
151out:
152 REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
153}
154
155static void
156vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
157{
158 flow_report_main_t *frm = &flow_report_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800159 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100160 vl_api_ipfix_exporter_details_t *rmp;
161 ip4_main_t *im = &ip4_main;
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200162 ip46_address_t collector = {.as_u64[0] = 0,.as_u64[1] = 0 };
163 ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 };
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100164 u32 vrf_id;
165
Florin Coras6c4dae22018-01-09 06:39:23 -0800166 reg = vl_api_client_index_to_registration (mp->client_index);
167 if (!reg)
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400168 return;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100169
170 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400171 clib_memset (rmp, 0, sizeof (*rmp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100172 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
173 rmp->context = mp->context;
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200174
175 memcpy (&collector.ip4, &frm->ipfix_collector, sizeof (ip4_address_t));
176 ip_address_encode (&collector, IP46_TYPE_IP4, &rmp->collector_address);
177
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100178 rmp->collector_port = htons (frm->collector_port);
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200179
180 memcpy (&src.ip4, &frm->src_address, sizeof (ip4_address_t));
181 ip_address_encode (&src, IP46_TYPE_IP4, &rmp->src_address);
182
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100183 if (frm->fib_index == ~0)
184 vrf_id = ~0;
185 else
186 vrf_id = im->fibs[frm->fib_index].ft_table_id;
187 rmp->vrf_id = htonl (vrf_id);
188 rmp->path_mtu = htonl (frm->path_mtu);
189 rmp->template_interval = htonl (frm->template_interval);
190 rmp->udp_checksum = (frm->udp_checksum != 0);
191
Florin Coras6c4dae22018-01-09 06:39:23 -0800192 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100193}
194
195static void
196 vl_api_set_ipfix_classify_stream_t_handler
197 (vl_api_set_ipfix_classify_stream_t * mp)
198{
199 vl_api_set_ipfix_classify_stream_reply_t *rmp;
200 flow_report_classify_main_t *fcm = &flow_report_classify_main;
201 flow_report_main_t *frm = &flow_report_main;
202 u32 domain_id = 0;
203 u32 src_port = UDP_DST_PORT_ipfix;
204 int rv = 0;
205
206 domain_id = ntohl (mp->domain_id);
207 src_port = ntohs (mp->src_port);
208
209 if (fcm->src_port != 0 &&
210 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
211 {
212 int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
213 domain_id, (u16) src_port);
214 ASSERT (rv == 0);
215 }
216
217 fcm->domain_id = domain_id;
218 fcm->src_port = (u16) src_port;
219
220 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
221}
222
223static void
224 vl_api_ipfix_classify_stream_dump_t_handler
225 (vl_api_ipfix_classify_stream_dump_t * mp)
226{
227 flow_report_classify_main_t *fcm = &flow_report_classify_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800228 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100229 vl_api_ipfix_classify_stream_details_t *rmp;
230
Florin Coras6c4dae22018-01-09 06:39:23 -0800231 reg = vl_api_client_index_to_registration (mp->client_index);
232 if (!reg)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100233 return;
234
235 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400236 clib_memset (rmp, 0, sizeof (*rmp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100237 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
238 rmp->context = mp->context;
239 rmp->domain_id = htonl (fcm->domain_id);
240 rmp->src_port = htons (fcm->src_port);
241
Florin Coras6c4dae22018-01-09 06:39:23 -0800242 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100243}
244
245static void
246 vl_api_ipfix_classify_table_add_del_t_handler
247 (vl_api_ipfix_classify_table_add_del_t * mp)
248{
249 vl_api_ipfix_classify_table_add_del_reply_t *rmp;
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400250 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100251 flow_report_classify_main_t *fcm = &flow_report_classify_main;
252 flow_report_main_t *frm = &flow_report_main;
253 vnet_flow_report_add_del_args_t args;
254 ipfix_classify_table_t *table;
255 int is_add;
256 u32 classify_table_index;
257 u8 ip_version;
258 u8 transport_protocol;
259 int rv = 0;
260
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400261 reg = vl_api_client_index_to_registration (mp->client_index);
262 if (!reg)
263 return;
264
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100265 classify_table_index = ntohl (mp->table_id);
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200266 ip_version = ntohl (mp->ip_version);
267 transport_protocol = ntohl (mp->transport_protocol);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100268 is_add = mp->is_add;
269
270 if (fcm->src_port == 0)
271 {
272 /* call set_ipfix_classify_stream first */
273 rv = VNET_API_ERROR_UNSPECIFIED;
274 goto out;
275 }
276
Dave Barachb7b92992018-10-17 10:38:51 -0400277 clib_memset (&args, 0, sizeof (args));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100278
279 table = 0;
280 int i;
281 for (i = 0; i < vec_len (fcm->tables); i++)
282 if (ipfix_classify_table_index_valid (i))
283 if (fcm->tables[i].classify_table_index == classify_table_index)
284 {
285 table = &fcm->tables[i];
286 break;
287 }
288
289 if (is_add)
290 {
291 if (table)
292 {
293 rv = VNET_API_ERROR_VALUE_EXIST;
294 goto out;
295 }
296 table = ipfix_classify_add_table ();
297 table->classify_table_index = classify_table_index;
298 }
299 else
300 {
301 if (!table)
302 {
303 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
304 goto out;
305 }
306 }
307
308 table->ip_version = ip_version;
309 table->transport_protocol = transport_protocol;
310
311 args.opaque.as_uword = table - fcm->tables;
312 args.rewrite_callback = ipfix_classify_template_rewrite;
313 args.flow_data_callback = ipfix_classify_send_flows;
314 args.is_add = is_add;
315 args.domain_id = fcm->domain_id;
316 args.src_port = fcm->src_port;
317
Ole Troan5c749732017-03-13 13:39:52 +0100318 rv = vnet_flow_report_add_del (frm, &args, NULL);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100319
320 /* If deleting, or add failed */
321 if (is_add == 0 || (rv && is_add))
322 ipfix_classify_delete_table (table - fcm->tables);
323
324out:
325 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
326}
327
328static void
329send_ipfix_classify_table_details (u32 table_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800330 vl_api_registration_t * reg, u32 context)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100331{
332 flow_report_classify_main_t *fcm = &flow_report_classify_main;
333 vl_api_ipfix_classify_table_details_t *mp;
334
335 ipfix_classify_table_t *table = &fcm->tables[table_index];
336
337 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400338 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100339 mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
340 mp->context = context;
341 mp->table_id = htonl (table->classify_table_index);
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200342 mp->ip_version = htonl (table->ip_version);
343 mp->transport_protocol = htonl (table->transport_protocol);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100344
Florin Coras6c4dae22018-01-09 06:39:23 -0800345 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100346}
347
348static void
349 vl_api_ipfix_classify_table_dump_t_handler
350 (vl_api_ipfix_classify_table_dump_t * mp)
351{
352 flow_report_classify_main_t *fcm = &flow_report_classify_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800353 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100354 u32 i;
355
Florin Coras6c4dae22018-01-09 06:39:23 -0800356 reg = vl_api_client_index_to_registration (mp->client_index);
357 if (!reg)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100358 return;
359
360 for (i = 0; i < vec_len (fcm->tables); i++)
361 if (ipfix_classify_table_index_valid (i))
Florin Coras6c4dae22018-01-09 06:39:23 -0800362 send_ipfix_classify_table_details (i, reg, mp->context);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100363}
364
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400365static void
366vl_api_ipfix_flush_t_handler (vl_api_ipfix_flush_t * mp)
367{
368 vl_api_ipfix_flush_reply_t *rmp;
369 vl_api_registration_t *reg;
370 vlib_main_t *vm = vlib_get_main ();
371 int rv = 0;
372
373 reg = vl_api_client_index_to_registration (mp->client_index);
374 if (!reg)
375 return;
376
377 /* poke the flow reporting process */
378 vlib_process_signal_event (vm, flow_report_process_node.index,
379 1 /* type_opaque */ , 0 /* data */ );
380
381 REPLY_MACRO (VL_API_IPFIX_FLUSH_REPLY);
382}
383
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100384/*
385 * flow_api_hookup
386 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500387 * vlib has already mapped shared memory and
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100388 * added the client registration handlers.
389 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
390 */
391#define vl_msg_name_crc_list
392#include <vnet/vnet_all_api_h.h>
393#undef vl_msg_name_crc_list
394
395static void
396setup_message_id_table (api_main_t * am)
397{
398#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
Ole Troana9855ef2018-05-02 12:45:10 +0200399 foreach_vl_msg_name_crc_ipfix_export;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100400#undef _
401}
402
403static clib_error_t *
404flow_api_hookup (vlib_main_t * vm)
405{
Dave Barach39d69112019-11-27 11:42:13 -0500406 api_main_t *am = vlibapi_get_main ();
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100407
408#define _(N,n) \
409 vl_msg_api_set_handlers(VL_API_##N, #n, \
410 vl_api_##n##_t_handler, \
411 vl_noop_handler, \
412 vl_api_##n##_t_endian, \
413 vl_api_##n##_t_print, \
414 sizeof(vl_api_##n##_t), 1);
415 foreach_vpe_api_msg;
416#undef _
417
418 /*
419 * Set up the (msg_name, crc, message-id) table
420 */
421 setup_message_id_table (am);
422
423 return 0;
424}
425
426VLIB_API_INIT_FUNCTION (flow_api_hookup);
427
428/*
429 * fd.io coding-style-patch-verification: ON
430 *
431 * Local Variables:
432 * eval: (c-set-style "gnu")
433 * End:
434 */