blob: dc163fe1e0ae020a1b40e979ff7b95a1cc5e673f [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
Filip Tehlar53dea272021-06-21 10:57:49 +000032#include <vnet/format_fns.h>
33#include <vnet/ipfix-export/ipfix_export.api_enum.h>
34#include <vnet/ipfix-export/ipfix_export.api_types.h>
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010035
Filip Tehlar53dea272021-06-21 10:57:49 +000036#define REPLY_MSG_ID_BASE frm->msg_id_base
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010037#include <vlibapi/api_helper_macros.h>
38
Paul Atkinsa6e131e2021-09-22 14:18:45 +010039static int
40vl_api_set_ipfix_exporter_t_internal (
41 u32 client_index, vl_api_address_t *mp_collector_address,
42 u16 mp_collector_port, vl_api_address_t *mp_src_address, u32 mp_vrf_id,
43 u32 mp_path_mtu, u32 mp_template_interval, bool mp_udp_checksum)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010044{
45 vlib_main_t *vm = vlib_get_main ();
46 flow_report_main_t *frm = &flow_report_main;
Paul Atkins9ec64492021-09-21 20:49:12 +010047 ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, 0);
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040048 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010049 ip4_address_t collector, src;
50 u16 collector_port = UDP_DST_PORT_ipfix;
51 u32 path_mtu;
52 u32 template_interval;
53 u8 udp_checksum;
54 u32 fib_id;
55 u32 fib_index = ~0;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010056
Paul Atkinsa6e131e2021-09-22 14:18:45 +010057 reg = vl_api_client_index_to_registration (client_index);
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040058 if (!reg)
Paul Atkinsa6e131e2021-09-22 14:18:45 +010059 return VNET_API_ERROR_UNIMPLEMENTED;
Paul Vinciguerra21b83e92019-06-24 09:55:46 -040060
Paul Atkinsa6e131e2021-09-22 14:18:45 +010061 if (mp_src_address->af == ADDRESS_IP6 ||
62 mp_collector_address->af == ADDRESS_IP6)
Jakub Grajciar2f71a882019-10-10 14:21:22 +020063 {
Paul Atkinsa6e131e2021-09-22 14:18:45 +010064 return VNET_API_ERROR_UNIMPLEMENTED;
Jakub Grajciar2f71a882019-10-10 14:21:22 +020065 }
66
Paul Atkinsa6e131e2021-09-22 14:18:45 +010067 ip4_address_decode (mp_collector_address->un.ip4, &collector);
68 collector_port = ntohs (mp_collector_port);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010069 if (collector_port == (u16) ~ 0)
70 collector_port = UDP_DST_PORT_ipfix;
Paul Atkinsa6e131e2021-09-22 14:18:45 +010071 ip4_address_decode (mp_src_address->un.ip4, &src);
72 fib_id = ntohl (mp_vrf_id);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010073
74 ip4_main_t *im = &ip4_main;
75 if (fib_id == ~0)
76 {
77 fib_index = ~0;
78 }
79 else
80 {
81 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
82 if (!p)
Paul Atkinsa6e131e2021-09-22 14:18:45 +010083 return VNET_API_ERROR_NO_SUCH_FIB;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010084 fib_index = p[0];
85 }
86
Paul Atkinsa6e131e2021-09-22 14:18:45 +010087 path_mtu = ntohl (mp_path_mtu);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010088 if (path_mtu == ~0)
89 path_mtu = 512; // RFC 7011 section 10.3.3.
Paul Atkinsa6e131e2021-09-22 14:18:45 +010090 template_interval = ntohl (mp_template_interval);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010091 if (template_interval == ~0)
92 template_interval = 20;
Paul Atkinsa6e131e2021-09-22 14:18:45 +010093 udp_checksum = mp_udp_checksum;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010094
Alexander Chernavin67ec7522020-10-01 08:57:59 -040095 if (collector.as_u32 != 0 && src.as_u32 == 0)
Paul Atkinsa6e131e2021-09-22 14:18:45 +010096 return VNET_API_ERROR_INVALID_VALUE;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +010097
98 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
Paul Atkinsa6e131e2021-09-22 14:18:45 +010099 return VNET_API_ERROR_INVALID_VALUE;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100100
101 if (path_mtu < 68)
Paul Atkinsa6e131e2021-09-22 14:18:45 +0100102 return VNET_API_ERROR_INVALID_VALUE;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100103
104 /* Reset report streams if we are reconfiguring IP addresses */
Paul Atkins9ec64492021-09-21 20:49:12 +0100105 if (exp->ipfix_collector.as_u32 != collector.as_u32 ||
106 exp->src_address.as_u32 != src.as_u32 ||
107 exp->collector_port != collector_port)
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100108 vnet_flow_reports_reset (exp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100109
Paul Atkins9ec64492021-09-21 20:49:12 +0100110 exp->ipfix_collector.as_u32 = collector.as_u32;
111 exp->collector_port = collector_port;
112 exp->src_address.as_u32 = src.as_u32;
113 exp->fib_index = fib_index;
114 exp->path_mtu = path_mtu;
115 exp->template_interval = template_interval;
116 exp->udp_checksum = udp_checksum;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100117
118 /* Turn on the flow reporting process */
119 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
120
Paul Atkinsa6e131e2021-09-22 14:18:45 +0100121 return 0;
122}
123
124static void
125vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t *mp)
126{
127 vl_api_set_ipfix_exporter_reply_t *rmp;
128 flow_report_main_t *frm = &flow_report_main;
129 int rv = vl_api_set_ipfix_exporter_t_internal (
130 mp->client_index, &mp->collector_address, mp->collector_port,
131 &mp->src_address, mp->vrf_id, mp->path_mtu, mp->template_interval,
132 mp->udp_checksum);
133
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100134 REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
135}
136
137static void
138vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
139{
140 flow_report_main_t *frm = &flow_report_main;
Paul Atkins9ec64492021-09-21 20:49:12 +0100141 ipfix_exporter_t *exp = pool_elt_at_index (flow_report_main.exporters, 0);
Florin Coras6c4dae22018-01-09 06:39:23 -0800142 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100143 vl_api_ipfix_exporter_details_t *rmp;
144 ip4_main_t *im = &ip4_main;
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200145 ip46_address_t collector = {.as_u64[0] = 0,.as_u64[1] = 0 };
146 ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 };
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100147 u32 vrf_id;
148
Florin Coras6c4dae22018-01-09 06:39:23 -0800149 reg = vl_api_client_index_to_registration (mp->client_index);
150 if (!reg)
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400151 return;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100152
153 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400154 clib_memset (rmp, 0, sizeof (*rmp));
Paul Atkins5df0b342021-09-23 10:55:25 +0100155 rmp->_vl_msg_id =
156 ntohs ((REPLY_MSG_ID_BASE) + VL_API_IPFIX_EXPORTER_DETAILS);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100157 rmp->context = mp->context;
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200158
Paul Atkins9ec64492021-09-21 20:49:12 +0100159 memcpy (&collector.ip4, &exp->ipfix_collector, sizeof (ip4_address_t));
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200160 ip_address_encode (&collector, IP46_TYPE_IP4, &rmp->collector_address);
161
Paul Atkins9ec64492021-09-21 20:49:12 +0100162 rmp->collector_port = htons (exp->collector_port);
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200163
Paul Atkins9ec64492021-09-21 20:49:12 +0100164 memcpy (&src.ip4, &exp->src_address, sizeof (ip4_address_t));
Jakub Grajciar2f71a882019-10-10 14:21:22 +0200165 ip_address_encode (&src, IP46_TYPE_IP4, &rmp->src_address);
166
Paul Atkins9ec64492021-09-21 20:49:12 +0100167 if (exp->fib_index == ~0)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100168 vrf_id = ~0;
169 else
Paul Atkins9ec64492021-09-21 20:49:12 +0100170 vrf_id = im->fibs[exp->fib_index].ft_table_id;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100171 rmp->vrf_id = htonl (vrf_id);
Paul Atkins9ec64492021-09-21 20:49:12 +0100172 rmp->path_mtu = htonl (exp->path_mtu);
173 rmp->template_interval = htonl (exp->template_interval);
174 rmp->udp_checksum = (exp->udp_checksum != 0);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100175
Florin Coras6c4dae22018-01-09 06:39:23 -0800176 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100177}
178
179static void
180 vl_api_set_ipfix_classify_stream_t_handler
181 (vl_api_set_ipfix_classify_stream_t * mp)
182{
183 vl_api_set_ipfix_classify_stream_reply_t *rmp;
184 flow_report_classify_main_t *fcm = &flow_report_classify_main;
185 flow_report_main_t *frm = &flow_report_main;
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100186 ipfix_exporter_t *exp = &frm->exporters[0];
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100187 u32 domain_id = 0;
188 u32 src_port = UDP_DST_PORT_ipfix;
189 int rv = 0;
190
191 domain_id = ntohl (mp->domain_id);
192 src_port = ntohs (mp->src_port);
193
194 if (fcm->src_port != 0 &&
195 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
196 {
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100197 int rv = vnet_stream_change (exp, fcm->domain_id, fcm->src_port,
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100198 domain_id, (u16) src_port);
199 ASSERT (rv == 0);
200 }
201
202 fcm->domain_id = domain_id;
203 fcm->src_port = (u16) src_port;
204
205 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
206}
207
208static void
209 vl_api_ipfix_classify_stream_dump_t_handler
210 (vl_api_ipfix_classify_stream_dump_t * mp)
211{
212 flow_report_classify_main_t *fcm = &flow_report_classify_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800213 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100214 vl_api_ipfix_classify_stream_details_t *rmp;
215
Florin Coras6c4dae22018-01-09 06:39:23 -0800216 reg = vl_api_client_index_to_registration (mp->client_index);
217 if (!reg)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100218 return;
219
220 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400221 clib_memset (rmp, 0, sizeof (*rmp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100222 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
223 rmp->context = mp->context;
224 rmp->domain_id = htonl (fcm->domain_id);
225 rmp->src_port = htons (fcm->src_port);
226
Florin Coras6c4dae22018-01-09 06:39:23 -0800227 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100228}
229
230static void
231 vl_api_ipfix_classify_table_add_del_t_handler
232 (vl_api_ipfix_classify_table_add_del_t * mp)
233{
234 vl_api_ipfix_classify_table_add_del_reply_t *rmp;
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400235 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100236 flow_report_classify_main_t *fcm = &flow_report_classify_main;
237 flow_report_main_t *frm = &flow_report_main;
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100238 ipfix_exporter_t *exp = &frm->exporters[0];
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100239 vnet_flow_report_add_del_args_t args;
240 ipfix_classify_table_t *table;
241 int is_add;
242 u32 classify_table_index;
243 u8 ip_version;
244 u8 transport_protocol;
245 int rv = 0;
246
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400247 reg = vl_api_client_index_to_registration (mp->client_index);
248 if (!reg)
249 return;
250
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100251 classify_table_index = ntohl (mp->table_id);
Alexander Chernavinf6cf57c2020-09-30 10:36:10 -0400252 ip_version = (mp->ip_version == ADDRESS_IP4) ? 4 : 6;
253 transport_protocol = mp->transport_protocol;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100254 is_add = mp->is_add;
255
256 if (fcm->src_port == 0)
257 {
258 /* call set_ipfix_classify_stream first */
259 rv = VNET_API_ERROR_UNSPECIFIED;
260 goto out;
261 }
262
Dave Barachb7b92992018-10-17 10:38:51 -0400263 clib_memset (&args, 0, sizeof (args));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100264
265 table = 0;
266 int i;
267 for (i = 0; i < vec_len (fcm->tables); i++)
268 if (ipfix_classify_table_index_valid (i))
269 if (fcm->tables[i].classify_table_index == classify_table_index)
270 {
271 table = &fcm->tables[i];
272 break;
273 }
274
275 if (is_add)
276 {
277 if (table)
278 {
279 rv = VNET_API_ERROR_VALUE_EXIST;
280 goto out;
281 }
282 table = ipfix_classify_add_table ();
283 table->classify_table_index = classify_table_index;
284 }
285 else
286 {
287 if (!table)
288 {
289 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
290 goto out;
291 }
292 }
293
294 table->ip_version = ip_version;
295 table->transport_protocol = transport_protocol;
296
297 args.opaque.as_uword = table - fcm->tables;
298 args.rewrite_callback = ipfix_classify_template_rewrite;
299 args.flow_data_callback = ipfix_classify_send_flows;
300 args.is_add = is_add;
301 args.domain_id = fcm->domain_id;
302 args.src_port = fcm->src_port;
303
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100304 rv = vnet_flow_report_add_del (exp, &args, NULL);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100305
306 /* If deleting, or add failed */
307 if (is_add == 0 || (rv && is_add))
308 ipfix_classify_delete_table (table - fcm->tables);
309
310out:
311 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
312}
313
314static void
315send_ipfix_classify_table_details (u32 table_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800316 vl_api_registration_t * reg, u32 context)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100317{
318 flow_report_classify_main_t *fcm = &flow_report_classify_main;
319 vl_api_ipfix_classify_table_details_t *mp;
320
321 ipfix_classify_table_t *table = &fcm->tables[table_index];
322
323 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400324 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100325 mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
326 mp->context = context;
327 mp->table_id = htonl (table->classify_table_index);
Alexander Chernavinf6cf57c2020-09-30 10:36:10 -0400328 mp->ip_version = (table->ip_version == 4) ? ADDRESS_IP4 : ADDRESS_IP6;
329 mp->transport_protocol = table->transport_protocol;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100330
Florin Coras6c4dae22018-01-09 06:39:23 -0800331 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100332}
333
334static void
335 vl_api_ipfix_classify_table_dump_t_handler
336 (vl_api_ipfix_classify_table_dump_t * mp)
337{
338 flow_report_classify_main_t *fcm = &flow_report_classify_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800339 vl_api_registration_t *reg;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100340 u32 i;
341
Florin Coras6c4dae22018-01-09 06:39:23 -0800342 reg = vl_api_client_index_to_registration (mp->client_index);
343 if (!reg)
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100344 return;
345
346 for (i = 0; i < vec_len (fcm->tables); i++)
347 if (ipfix_classify_table_index_valid (i))
Florin Coras6c4dae22018-01-09 06:39:23 -0800348 send_ipfix_classify_table_details (i, reg, mp->context);
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100349}
350
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400351static void
352vl_api_ipfix_flush_t_handler (vl_api_ipfix_flush_t * mp)
353{
Filip Tehlar53dea272021-06-21 10:57:49 +0000354 flow_report_main_t *frm = &flow_report_main;
Paul Vinciguerra21b83e92019-06-24 09:55:46 -0400355 vl_api_ipfix_flush_reply_t *rmp;
356 vl_api_registration_t *reg;
357 vlib_main_t *vm = vlib_get_main ();
358 int rv = 0;
359
360 reg = vl_api_client_index_to_registration (mp->client_index);
361 if (!reg)
362 return;
363
364 /* poke the flow reporting process */
365 vlib_process_signal_event (vm, flow_report_process_node.index,
366 1 /* type_opaque */ , 0 /* data */ );
367
368 REPLY_MACRO (VL_API_IPFIX_FLUSH_REPLY);
369}
370
Filip Tehlar53dea272021-06-21 10:57:49 +0000371#include <vnet/ipfix-export/ipfix_export.api.c>
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100372static clib_error_t *
373flow_api_hookup (vlib_main_t * vm)
374{
Filip Tehlar53dea272021-06-21 10:57:49 +0000375 flow_report_main_t *frm = &flow_report_main;
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100376 /*
377 * Set up the (msg_name, crc, message-id) table
378 */
Filip Tehlar53dea272021-06-21 10:57:49 +0000379 REPLY_MSG_ID_BASE = setup_message_id_table ();
Pavel Kotucekeb9e6662017-01-24 13:40:26 +0100380
381 return 0;
382}
383
384VLIB_API_INIT_FUNCTION (flow_api_hookup);
385
386/*
387 * fd.io coding-style-patch-verification: ON
388 *
389 * Local Variables:
390 * eval: (c-set-style "gnu")
391 * End:
392 */