2 *------------------------------------------------------------------
3 * flow_api.c - flow api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22 #include <vnet/ip/ip_types_api.h>
23 #include <vnet/udp/udp_local.h>
25 #include <vnet/interface.h>
26 #include <vnet/api_errno.h>
28 #include <vnet/fib/fib_table.h>
29 #include <vnet/ipfix-export/flow_report.h>
30 #include <vnet/ipfix-export/flow_report_classify.h>
32 #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>
36 #define REPLY_MSG_ID_BASE frm->msg_id_base
37 #include <vlibapi/api_helper_macros.h>
40 vnet_ipfix_exporter_lookup (ip4_address_t *ipfix_collector)
42 flow_report_main_t *frm = &flow_report_main;
43 ipfix_exporter_t *exp;
45 pool_foreach (exp, frm->exporters)
47 if (exp->ipfix_collector.as_u32 == ipfix_collector->as_u32)
55 * For backwards compatibility reasons index 0 in the set of exporters
56 * is alwyas used for the exporter created via the set_ipfix_exporter
59 #define USE_INDEX_0 true
60 #define USE_ANY_INDEX false
63 vl_api_set_ipfix_exporter_t_internal (
64 u32 client_index, vl_api_address_t *mp_collector_address,
65 u16 mp_collector_port, vl_api_address_t *mp_src_address, u32 mp_vrf_id,
66 u32 mp_path_mtu, u32 mp_template_interval, bool mp_udp_checksum,
67 bool use_index_0, bool is_create)
69 vlib_main_t *vm = vlib_get_main ();
70 flow_report_main_t *frm = &flow_report_main;
71 ipfix_exporter_t *exp;
72 vl_api_registration_t *reg;
73 ip4_address_t collector, src;
74 u16 collector_port = UDP_DST_PORT_ipfix;
76 u32 template_interval;
81 reg = vl_api_client_index_to_registration (client_index);
83 return VNET_API_ERROR_UNIMPLEMENTED;
85 /* Collector address is the key for the exporter lookup */
86 ip4_address_decode (mp_collector_address->un.ip4, &collector);
90 * In this case we update the existing exporter. There is no delete
93 exp = &frm->exporters[0];
98 exp = vnet_ipfix_exporter_lookup (&collector);
101 /* Create a new exporter instead of updating an existing one */
102 if (pool_elts (frm->exporters) >= IPFIX_EXPORTERS_MAX)
103 return VNET_API_ERROR_INVALID_VALUE;
104 pool_get (frm->exporters, exp);
106 return VNET_API_ERROR_INVALID_VALUE;
111 /* Delete the exporter */
112 exp = vnet_ipfix_exporter_lookup (&collector);
114 return VNET_API_ERROR_NO_SUCH_ENTRY;
116 pool_put (frm->exporters, exp);
121 if (mp_src_address->af == ADDRESS_IP6 ||
122 mp_collector_address->af == ADDRESS_IP6)
124 return VNET_API_ERROR_UNIMPLEMENTED;
127 collector_port = ntohs (mp_collector_port);
128 if (collector_port == (u16) ~ 0)
129 collector_port = UDP_DST_PORT_ipfix;
130 ip4_address_decode (mp_src_address->un.ip4, &src);
131 fib_id = ntohl (mp_vrf_id);
133 ip4_main_t *im = &ip4_main;
140 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
142 return VNET_API_ERROR_NO_SUCH_FIB;
146 path_mtu = ntohl (mp_path_mtu);
148 path_mtu = 512; // RFC 7011 section 10.3.3.
149 template_interval = ntohl (mp_template_interval);
150 if (template_interval == ~0)
151 template_interval = 20;
152 udp_checksum = mp_udp_checksum;
154 if (collector.as_u32 != 0 && src.as_u32 == 0)
155 return VNET_API_ERROR_INVALID_VALUE;
157 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
158 return VNET_API_ERROR_INVALID_VALUE;
161 return VNET_API_ERROR_INVALID_VALUE;
163 /* Reset report streams if we are reconfiguring IP addresses */
164 if (exp->ipfix_collector.as_u32 != collector.as_u32 ||
165 exp->src_address.as_u32 != src.as_u32 ||
166 exp->collector_port != collector_port)
167 vnet_flow_reports_reset (exp);
169 exp->ipfix_collector.as_u32 = collector.as_u32;
170 exp->collector_port = collector_port;
171 exp->src_address.as_u32 = src.as_u32;
172 exp->fib_index = fib_index;
173 exp->path_mtu = path_mtu;
174 exp->template_interval = template_interval;
175 exp->udp_checksum = udp_checksum;
177 /* Turn on the flow reporting process */
178 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
184 vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t *mp)
186 vl_api_set_ipfix_exporter_reply_t *rmp;
187 flow_report_main_t *frm = &flow_report_main;
188 int rv = vl_api_set_ipfix_exporter_t_internal (
189 mp->client_index, &mp->collector_address, mp->collector_port,
190 &mp->src_address, mp->vrf_id, mp->path_mtu, mp->template_interval,
191 mp->udp_checksum, USE_INDEX_0, 0);
193 REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
197 vl_api_ipfix_exporter_create_delete_t_handler (
198 vl_api_ipfix_exporter_create_delete_t *mp)
200 vl_api_ipfix_exporter_create_delete_reply_t *rmp;
201 flow_report_main_t *frm = &flow_report_main;
202 int rv = vl_api_set_ipfix_exporter_t_internal (
203 mp->client_index, &mp->collector_address, mp->collector_port,
204 &mp->src_address, mp->vrf_id, mp->path_mtu, mp->template_interval,
205 mp->udp_checksum, USE_ANY_INDEX, mp->is_create);
207 REPLY_MACRO (VL_API_IPFIX_EXPORTER_CREATE_DELETE_REPLY);
211 vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
213 flow_report_main_t *frm = &flow_report_main;
214 ipfix_exporter_t *exp = pool_elt_at_index (flow_report_main.exporters, 0);
215 vl_api_registration_t *reg;
216 vl_api_ipfix_exporter_details_t *rmp;
217 ip4_main_t *im = &ip4_main;
218 ip46_address_t collector = {.as_u64[0] = 0,.as_u64[1] = 0 };
219 ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 };
222 reg = vl_api_client_index_to_registration (mp->client_index);
226 rmp = vl_msg_api_alloc (sizeof (*rmp));
227 clib_memset (rmp, 0, sizeof (*rmp));
229 ntohs ((REPLY_MSG_ID_BASE) + VL_API_IPFIX_EXPORTER_DETAILS);
230 rmp->context = mp->context;
232 memcpy (&collector.ip4, &exp->ipfix_collector, sizeof (ip4_address_t));
233 ip_address_encode (&collector, IP46_TYPE_IP4, &rmp->collector_address);
235 rmp->collector_port = htons (exp->collector_port);
237 memcpy (&src.ip4, &exp->src_address, sizeof (ip4_address_t));
238 ip_address_encode (&src, IP46_TYPE_IP4, &rmp->src_address);
240 if (exp->fib_index == ~0)
243 vrf_id = im->fibs[exp->fib_index].ft_table_id;
244 rmp->vrf_id = htonl (vrf_id);
245 rmp->path_mtu = htonl (exp->path_mtu);
246 rmp->template_interval = htonl (exp->template_interval);
247 rmp->udp_checksum = (exp->udp_checksum != 0);
249 vl_api_send_msg (reg, (u8 *) rmp);
253 ipfix_all_fill_details (vl_api_ipfix_all_exporter_details_t *rmp,
254 ipfix_exporter_t *exp)
256 ip4_main_t *im = &ip4_main;
257 ip46_address_t collector = { .as_u64[0] = 0, .as_u64[1] = 0 };
258 ip46_address_t src = { .as_u64[0] = 0, .as_u64[1] = 0 };
261 memcpy (&collector.ip4, &exp->ipfix_collector, sizeof (ip4_address_t));
262 ip_address_encode (&collector, IP46_TYPE_IP4, &rmp->collector_address);
264 rmp->collector_port = htons (exp->collector_port);
266 memcpy (&src.ip4, &exp->src_address, sizeof (ip4_address_t));
267 ip_address_encode (&src, IP46_TYPE_IP4, &rmp->src_address);
269 if (exp->fib_index == ~0)
272 vrf_id = im->fibs[exp->fib_index].ft_table_id;
273 rmp->vrf_id = htonl (vrf_id);
274 rmp->path_mtu = htonl (exp->path_mtu);
275 rmp->template_interval = htonl (exp->template_interval);
276 rmp->udp_checksum = (exp->udp_checksum != 0);
280 ipfix_all_exporter_details (flow_report_main_t *frm, u32 index,
281 vl_api_registration_t *rp, u32 context)
283 ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, index);
285 vl_api_ipfix_all_exporter_details_t *rmp;
287 REPLY_MACRO_DETAILS4 (VL_API_IPFIX_ALL_EXPORTER_DETAILS, rp, context,
288 ({ ipfix_all_fill_details (rmp, exp); }));
292 vl_api_ipfix_all_exporter_get_t_handler (vl_api_ipfix_all_exporter_get_t *mp)
294 flow_report_main_t *frm = &flow_report_main;
295 vl_api_ipfix_all_exporter_get_reply_t *rmp;
298 REPLY_AND_DETAILS_MACRO (
299 VL_API_IPFIX_ALL_EXPORTER_GET_REPLY, frm->exporters,
300 ({ ipfix_all_exporter_details (frm, cursor, rp, mp->context); }));
304 vl_api_set_ipfix_classify_stream_t_handler
305 (vl_api_set_ipfix_classify_stream_t * mp)
307 vl_api_set_ipfix_classify_stream_reply_t *rmp;
308 flow_report_classify_main_t *fcm = &flow_report_classify_main;
309 flow_report_main_t *frm = &flow_report_main;
310 ipfix_exporter_t *exp = &frm->exporters[0];
312 u32 src_port = UDP_DST_PORT_ipfix;
315 domain_id = ntohl (mp->domain_id);
316 src_port = ntohs (mp->src_port);
318 if (fcm->src_port != 0 &&
319 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
321 int rv = vnet_stream_change (exp, fcm->domain_id, fcm->src_port,
322 domain_id, (u16) src_port);
326 fcm->domain_id = domain_id;
327 fcm->src_port = (u16) src_port;
329 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
333 vl_api_ipfix_classify_stream_dump_t_handler
334 (vl_api_ipfix_classify_stream_dump_t * mp)
336 flow_report_classify_main_t *fcm = &flow_report_classify_main;
337 vl_api_registration_t *reg;
338 vl_api_ipfix_classify_stream_details_t *rmp;
340 reg = vl_api_client_index_to_registration (mp->client_index);
344 rmp = vl_msg_api_alloc (sizeof (*rmp));
345 clib_memset (rmp, 0, sizeof (*rmp));
346 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
347 rmp->context = mp->context;
348 rmp->domain_id = htonl (fcm->domain_id);
349 rmp->src_port = htons (fcm->src_port);
351 vl_api_send_msg (reg, (u8 *) rmp);
355 vl_api_ipfix_classify_table_add_del_t_handler
356 (vl_api_ipfix_classify_table_add_del_t * mp)
358 vl_api_ipfix_classify_table_add_del_reply_t *rmp;
359 vl_api_registration_t *reg;
360 flow_report_classify_main_t *fcm = &flow_report_classify_main;
361 flow_report_main_t *frm = &flow_report_main;
362 ipfix_exporter_t *exp = &frm->exporters[0];
363 vnet_flow_report_add_del_args_t args;
364 ipfix_classify_table_t *table;
366 u32 classify_table_index;
368 u8 transport_protocol;
371 reg = vl_api_client_index_to_registration (mp->client_index);
375 classify_table_index = ntohl (mp->table_id);
376 ip_version = (mp->ip_version == ADDRESS_IP4) ? 4 : 6;
377 transport_protocol = mp->transport_protocol;
380 if (fcm->src_port == 0)
382 /* call set_ipfix_classify_stream first */
383 rv = VNET_API_ERROR_UNSPECIFIED;
387 clib_memset (&args, 0, sizeof (args));
391 for (i = 0; i < vec_len (fcm->tables); i++)
392 if (ipfix_classify_table_index_valid (i))
393 if (fcm->tables[i].classify_table_index == classify_table_index)
395 table = &fcm->tables[i];
403 rv = VNET_API_ERROR_VALUE_EXIST;
406 table = ipfix_classify_add_table ();
407 table->classify_table_index = classify_table_index;
413 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
418 table->ip_version = ip_version;
419 table->transport_protocol = transport_protocol;
421 args.opaque.as_uword = table - fcm->tables;
422 args.rewrite_callback = ipfix_classify_template_rewrite;
423 args.flow_data_callback = ipfix_classify_send_flows;
424 args.is_add = is_add;
425 args.domain_id = fcm->domain_id;
426 args.src_port = fcm->src_port;
428 rv = vnet_flow_report_add_del (exp, &args, NULL);
430 /* If deleting, or add failed */
431 if (is_add == 0 || (rv && is_add))
432 ipfix_classify_delete_table (table - fcm->tables);
435 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
439 send_ipfix_classify_table_details (u32 table_index,
440 vl_api_registration_t * reg, u32 context)
442 flow_report_classify_main_t *fcm = &flow_report_classify_main;
443 vl_api_ipfix_classify_table_details_t *mp;
445 ipfix_classify_table_t *table = &fcm->tables[table_index];
447 mp = vl_msg_api_alloc (sizeof (*mp));
448 clib_memset (mp, 0, sizeof (*mp));
449 mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
450 mp->context = context;
451 mp->table_id = htonl (table->classify_table_index);
452 mp->ip_version = (table->ip_version == 4) ? ADDRESS_IP4 : ADDRESS_IP6;
453 mp->transport_protocol = table->transport_protocol;
455 vl_api_send_msg (reg, (u8 *) mp);
459 vl_api_ipfix_classify_table_dump_t_handler
460 (vl_api_ipfix_classify_table_dump_t * mp)
462 flow_report_classify_main_t *fcm = &flow_report_classify_main;
463 vl_api_registration_t *reg;
466 reg = vl_api_client_index_to_registration (mp->client_index);
470 for (i = 0; i < vec_len (fcm->tables); i++)
471 if (ipfix_classify_table_index_valid (i))
472 send_ipfix_classify_table_details (i, reg, mp->context);
476 vl_api_ipfix_flush_t_handler (vl_api_ipfix_flush_t * mp)
478 flow_report_main_t *frm = &flow_report_main;
479 vl_api_ipfix_flush_reply_t *rmp;
480 vl_api_registration_t *reg;
481 vlib_main_t *vm = vlib_get_main ();
484 reg = vl_api_client_index_to_registration (mp->client_index);
488 /* poke the flow reporting process */
489 vlib_process_signal_event (vm, flow_report_process_node.index,
490 1 /* type_opaque */ , 0 /* data */ );
492 REPLY_MACRO (VL_API_IPFIX_FLUSH_REPLY);
495 #include <vnet/ipfix-export/ipfix_export.api.c>
496 static clib_error_t *
497 flow_api_hookup (vlib_main_t * vm)
499 flow_report_main_t *frm = &flow_report_main;
501 * Set up the (msg_name, crc, message-id) table
503 REPLY_MSG_ID_BASE = setup_message_id_table ();
508 VLIB_API_INIT_FUNCTION (flow_api_hookup);
511 * fd.io coding-style-patch-verification: ON
514 * eval: (c-set-style "gnu")