blob: 226db2686ac00994b6e9578eee4ad2938d66af60 [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>
Florin Corasb040f982020-10-20 14:59:43 -070023#include <vnet/udp/udp_local.h>
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010024
25#include <vnet/interface.h>
26#include <vnet/api_errno.h>
27
28#include <vnet/fib/fib_table.h>
Ole Troana9855ef2018-05-02 12:45:10 +020029#include <vnet/ipfix-export/flow_report.h>
30#include <vnet/ipfix-export/flow_report_classify.h>
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010031
32#include <vnet/vnet_msg_enum.h>
33
34#define vl_typedefs /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_typedefs
37
38#define vl_endianfun /* define message structures */
39#include <vnet/vnet_all_api_h.h>
40#undef vl_endianfun
41
42/* instantiate all the print functions we know about */
43#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44#define vl_printfun
45#include <vnet/vnet_all_api_h.h>
46#undef vl_printfun
47
48#include <vlibapi/api_helper_macros.h>
49
50#define foreach_vpe_api_msg \
51_(SET_IPFIX_EXPORTER, set_ipfix_exporter) \
52_(IPFIX_EXPORTER_DUMP, ipfix_exporter_dump) \
53_(SET_IPFIX_CLASSIFY_STREAM, set_ipfix_classify_stream) \
54_(IPFIX_CLASSIFY_STREAM_DUMP, ipfix_classify_stream_dump) \
55_(IPFIX_CLASSIFY_TABLE_ADD_DEL, ipfix_classify_table_add_del) \
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040056_(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump) \
57_(IPFIX_FLUSH, ipfix_flush)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010058
59static void
60vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
61{
62 vlib_main_t *vm = vlib_get_main ();
63 flow_report_main_t *frm = &flow_report_main;
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040064 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010065 vl_api_set_ipfix_exporter_reply_t *rmp;
66 ip4_address_t collector, src;
67 u16 collector_port = UDP_DST_PORT_ipfix;
68 u32 path_mtu;
69 u32 template_interval;
70 u8 udp_checksum;
71 u32 fib_id;
72 u32 fib_index = ~0;
73 int rv = 0;
74
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040075 reg = vl_api_client_index_to_registration (mp->client_index);
76 if (!reg)
77 return;
78
Jakub Grajciar2f71a882019-10-10 14:21:22 +020079 if (mp->src_address.af == ADDRESS_IP6
80 || mp->collector_address.af == ADDRESS_IP6)
81 {
82 rv = VNET_API_ERROR_UNIMPLEMENTED;
83 goto out;
84 }
85
86 ip4_address_decode (mp->collector_address.un.ip4, &collector);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010087 collector_port = ntohs (mp->collector_port);
88 if (collector_port == (u16) ~ 0)
89 collector_port = UDP_DST_PORT_ipfix;
Jakub Grajciar2f71a882019-10-10 14:21:22 +020090 ip4_address_decode (mp->src_address.un.ip4, &src);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010091 fib_id = ntohl (mp->vrf_id);
92
93 ip4_main_t *im = &ip4_main;
94 if (fib_id == ~0)
95 {
96 fib_index = ~0;
97 }
98 else
99 {
100 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
101 if (!p)
102 {
103 rv = VNET_API_ERROR_NO_SUCH_FIB;
104 goto out;
105 }
106 fib_index = p[0];
107 }
108
109 path_mtu = ntohl (mp->path_mtu);
110 if (path_mtu == ~0)
111 path_mtu = 512; // RFC 7011 section 10.3.3.
112 template_interval = ntohl (mp->template_interval);
113 if (template_interval == ~0)
114 template_interval = 20;
115 udp_checksum = mp->udp_checksum;
116
Alexander Chernavin67ec7522020-10-01 08:57:59 -0400117 if (collector.as_u32 != 0 && src.as_u32 == 0)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100118 {
119 rv = VNET_API_ERROR_INVALID_VALUE;
120 goto out;
121 }
122
123 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
124 {
125 rv = VNET_API_ERROR_INVALID_VALUE;
126 goto out;
127 }
128
129 if (path_mtu < 68)
130 {
131 rv = VNET_API_ERROR_INVALID_VALUE;
132 goto out;
133 }
134
135 /* Reset report streams if we are reconfiguring IP addresses */
136 if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
137 frm->src_address.as_u32 != src.as_u32 ||
138 frm->collector_port != collector_port)
139 vnet_flow_reports_reset (frm);
140
141 frm->ipfix_collector.as_u32 = collector.as_u32;
142 frm->collector_port = collector_port;
143 frm->src_address.as_u32 = src.as_u32;
144 frm->fib_index = fib_index;
145 frm->path_mtu = path_mtu;
146 frm->template_interval = template_interval;
147 frm->udp_checksum = udp_checksum;
148
149 /* Turn on the flow reporting process */
150 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
151
152out:
153 REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
154}
155
156static void
157vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
158{
159 flow_report_main_t *frm = &flow_report_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800160 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100161 vl_api_ipfix_exporter_details_t *rmp;
162 ip4_main_t *im = &ip4_main;
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200163 ip46_address_t collector = {.as_u64[0] = 0,.as_u64[1] = 0 };
164 ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 };
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100165 u32 vrf_id;
166
Florin Coras6c4dae22018-01-09 06:39:23 -0800167 reg = vl_api_client_index_to_registration (mp->client_index);
168 if (!reg)
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400169 return;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100170
171 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400172 clib_memset (rmp, 0, sizeof (*rmp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100173 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
174 rmp->context = mp->context;
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200175
176 memcpy (&collector.ip4, &frm->ipfix_collector, sizeof (ip4_address_t));
177 ip_address_encode (&collector, IP46_TYPE_IP4, &rmp->collector_address);
178
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100179 rmp->collector_port = htons (frm->collector_port);
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200180
181 memcpy (&src.ip4, &frm->src_address, sizeof (ip4_address_t));
182 ip_address_encode (&src, IP46_TYPE_IP4, &rmp->src_address);
183
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100184 if (frm->fib_index == ~0)
185 vrf_id = ~0;
186 else
187 vrf_id = im->fibs[frm->fib_index].ft_table_id;
188 rmp->vrf_id = htonl (vrf_id);
189 rmp->path_mtu = htonl (frm->path_mtu);
190 rmp->template_interval = htonl (frm->template_interval);
191 rmp->udp_checksum = (frm->udp_checksum != 0);
192
Florin Coras6c4dae22018-01-09 06:39:23 -0800193 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100194}
195
196static void
197 vl_api_set_ipfix_classify_stream_t_handler
198 (vl_api_set_ipfix_classify_stream_t * mp)
199{
200 vl_api_set_ipfix_classify_stream_reply_t *rmp;
201 flow_report_classify_main_t *fcm = &flow_report_classify_main;
202 flow_report_main_t *frm = &flow_report_main;
203 u32 domain_id = 0;
204 u32 src_port = UDP_DST_PORT_ipfix;
205 int rv = 0;
206
207 domain_id = ntohl (mp->domain_id);
208 src_port = ntohs (mp->src_port);
209
210 if (fcm->src_port != 0 &&
211 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
212 {
213 int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
214 domain_id, (u16) src_port);
215 ASSERT (rv == 0);
216 }
217
218 fcm->domain_id = domain_id;
219 fcm->src_port = (u16) src_port;
220
221 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
222}
223
224static void
225 vl_api_ipfix_classify_stream_dump_t_handler
226 (vl_api_ipfix_classify_stream_dump_t * mp)
227{
228 flow_report_classify_main_t *fcm = &flow_report_classify_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800229 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100230 vl_api_ipfix_classify_stream_details_t *rmp;
231
Florin Coras6c4dae22018-01-09 06:39:23 -0800232 reg = vl_api_client_index_to_registration (mp->client_index);
233 if (!reg)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100234 return;
235
236 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400237 clib_memset (rmp, 0, sizeof (*rmp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100238 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
239 rmp->context = mp->context;
240 rmp->domain_id = htonl (fcm->domain_id);
241 rmp->src_port = htons (fcm->src_port);
242
Florin Coras6c4dae22018-01-09 06:39:23 -0800243 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100244}
245
246static void
247 vl_api_ipfix_classify_table_add_del_t_handler
248 (vl_api_ipfix_classify_table_add_del_t * mp)
249{
250 vl_api_ipfix_classify_table_add_del_reply_t *rmp;
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400251 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100252 flow_report_classify_main_t *fcm = &flow_report_classify_main;
253 flow_report_main_t *frm = &flow_report_main;
254 vnet_flow_report_add_del_args_t args;
255 ipfix_classify_table_t *table;
256 int is_add;
257 u32 classify_table_index;
258 u8 ip_version;
259 u8 transport_protocol;
260 int rv = 0;
261
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400262 reg = vl_api_client_index_to_registration (mp->client_index);
263 if (!reg)
264 return;
265
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100266 classify_table_index = ntohl (mp->table_id);
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200267 ip_version = ntohl (mp->ip_version);
268 transport_protocol = ntohl (mp->transport_protocol);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100269 is_add = mp->is_add;
270
271 if (fcm->src_port == 0)
272 {
273 /* call set_ipfix_classify_stream first */
274 rv = VNET_API_ERROR_UNSPECIFIED;
275 goto out;
276 }
277
Dave Barachb7b92992018-10-17 10:38:51 -0400278 clib_memset (&args, 0, sizeof (args));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100279
280 table = 0;
281 int i;
282 for (i = 0; i < vec_len (fcm->tables); i++)
283 if (ipfix_classify_table_index_valid (i))
284 if (fcm->tables[i].classify_table_index == classify_table_index)
285 {
286 table = &fcm->tables[i];
287 break;
288 }
289
290 if (is_add)
291 {
292 if (table)
293 {
294 rv = VNET_API_ERROR_VALUE_EXIST;
295 goto out;
296 }
297 table = ipfix_classify_add_table ();
298 table->classify_table_index = classify_table_index;
299 }
300 else
301 {
302 if (!table)
303 {
304 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
305 goto out;
306 }
307 }
308
309 table->ip_version = ip_version;
310 table->transport_protocol = transport_protocol;
311
312 args.opaque.as_uword = table - fcm->tables;
313 args.rewrite_callback = ipfix_classify_template_rewrite;
314 args.flow_data_callback = ipfix_classify_send_flows;
315 args.is_add = is_add;
316 args.domain_id = fcm->domain_id;
317 args.src_port = fcm->src_port;
318
Ole Troan5c749732017-03-13 13:39:52 +0100319 rv = vnet_flow_report_add_del (frm, &args, NULL);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100320
321 /* If deleting, or add failed */
322 if (is_add == 0 || (rv && is_add))
323 ipfix_classify_delete_table (table - fcm->tables);
324
325out:
326 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
327}
328
329static void
330send_ipfix_classify_table_details (u32 table_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800331 vl_api_registration_t * reg, u32 context)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100332{
333 flow_report_classify_main_t *fcm = &flow_report_classify_main;
334 vl_api_ipfix_classify_table_details_t *mp;
335
336 ipfix_classify_table_t *table = &fcm->tables[table_index];
337
338 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400339 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100340 mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
341 mp->context = context;
342 mp->table_id = htonl (table->classify_table_index);
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200343 mp->ip_version = htonl (table->ip_version);
344 mp->transport_protocol = htonl (table->transport_protocol);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100345
Florin Coras6c4dae22018-01-09 06:39:23 -0800346 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100347}
348
349static void
350 vl_api_ipfix_classify_table_dump_t_handler
351 (vl_api_ipfix_classify_table_dump_t * mp)
352{
353 flow_report_classify_main_t *fcm = &flow_report_classify_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800354 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100355 u32 i;
356
Florin Coras6c4dae22018-01-09 06:39:23 -0800357 reg = vl_api_client_index_to_registration (mp->client_index);
358 if (!reg)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100359 return;
360
361 for (i = 0; i < vec_len (fcm->tables); i++)
362 if (ipfix_classify_table_index_valid (i))
Florin Coras6c4dae22018-01-09 06:39:23 -0800363 send_ipfix_classify_table_details (i, reg, mp->context);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100364}
365
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400366static void
367vl_api_ipfix_flush_t_handler (vl_api_ipfix_flush_t * mp)
368{
369 vl_api_ipfix_flush_reply_t *rmp;
370 vl_api_registration_t *reg;
371 vlib_main_t *vm = vlib_get_main ();
372 int rv = 0;
373
374 reg = vl_api_client_index_to_registration (mp->client_index);
375 if (!reg)
376 return;
377
378 /* poke the flow reporting process */
379 vlib_process_signal_event (vm, flow_report_process_node.index,
380 1 /* type_opaque */ , 0 /* data */ );
381
382 REPLY_MACRO (VL_API_IPFIX_FLUSH_REPLY);
383}
384
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100385/*
386 * flow_api_hookup
387 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500388 * vlib has already mapped shared memory and
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100389 * added the client registration handlers.
390 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
391 */
392#define vl_msg_name_crc_list
393#include <vnet/vnet_all_api_h.h>
394#undef vl_msg_name_crc_list
395
396static void
397setup_message_id_table (api_main_t * am)
398{
399#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
Ole Troana9855ef2018-05-02 12:45:10 +0200400 foreach_vl_msg_name_crc_ipfix_export;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100401#undef _
402}
403
404static clib_error_t *
405flow_api_hookup (vlib_main_t * vm)
406{
Dave Barach39d69112019-11-27 11:42:13 -0500407 api_main_t *am = vlibapi_get_main ();
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100408
409#define _(N,n) \
410 vl_msg_api_set_handlers(VL_API_##N, #n, \
411 vl_api_##n##_t_handler, \
412 vl_noop_handler, \
413 vl_api_##n##_t_endian, \
414 vl_api_##n##_t_print, \
415 sizeof(vl_api_##n##_t), 1);
416 foreach_vpe_api_msg;
417#undef _
418
419 /*
420 * Set up the (msg_name, crc, message-id) table
421 */
422 setup_message_id_table (am);
423
424 return 0;
425}
426
427VLIB_API_INIT_FUNCTION (flow_api_hookup);
428
429/*
430 * fd.io coding-style-patch-verification: ON
431 *
432 * Local Variables:
433 * eval: (c-set-style "gnu")
434 * End:
435 */