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>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/ipfix-export/flow_report.h>
28 #include <vnet/ipfix-export/flow_report_classify.h>
30 #include <vnet/vnet_msg_enum.h>
32 #define vl_typedefs /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
36 #define vl_endianfun /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #include <vnet/vnet_all_api_h.h>
46 #include <vlibapi/api_helper_macros.h>
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) \
54 _(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump) \
55 _(IPFIX_FLUSH, ipfix_flush)
58 vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
60 vlib_main_t *vm = vlib_get_main ();
61 flow_report_main_t *frm = &flow_report_main;
62 vl_api_registration_t *reg;
63 vl_api_set_ipfix_exporter_reply_t *rmp;
64 ip4_address_t collector, src;
65 u16 collector_port = UDP_DST_PORT_ipfix;
67 u32 template_interval;
73 reg = vl_api_client_index_to_registration (mp->client_index);
77 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);
84 ip4_main_t *im = &ip4_main;
91 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
94 rv = VNET_API_ERROR_NO_SUCH_FIB;
100 path_mtu = ntohl (mp->path_mtu);
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;
108 if (collector.as_u32 == 0)
110 rv = VNET_API_ERROR_INVALID_VALUE;
116 rv = VNET_API_ERROR_INVALID_VALUE;
120 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
122 rv = VNET_API_ERROR_INVALID_VALUE;
128 rv = VNET_API_ERROR_INVALID_VALUE;
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);
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;
146 /* Turn on the flow reporting process */
147 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
150 REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
154 vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
156 flow_report_main_t *frm = &flow_report_main;
157 vl_api_registration_t *reg;
158 vl_api_ipfix_exporter_details_t *rmp;
159 ip4_main_t *im = &ip4_main;
162 reg = vl_api_client_index_to_registration (mp->client_index);
166 rmp = vl_msg_api_alloc (sizeof (*rmp));
167 clib_memset (rmp, 0, sizeof (*rmp));
168 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)
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);
184 vl_api_send_msg (reg, (u8 *) rmp);
188 vl_api_set_ipfix_classify_stream_t_handler
189 (vl_api_set_ipfix_classify_stream_t * mp)
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;
195 u32 src_port = UDP_DST_PORT_ipfix;
198 domain_id = ntohl (mp->domain_id);
199 src_port = ntohs (mp->src_port);
201 if (fcm->src_port != 0 &&
202 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
204 int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
205 domain_id, (u16) src_port);
209 fcm->domain_id = domain_id;
210 fcm->src_port = (u16) src_port;
212 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
216 vl_api_ipfix_classify_stream_dump_t_handler
217 (vl_api_ipfix_classify_stream_dump_t * mp)
219 flow_report_classify_main_t *fcm = &flow_report_classify_main;
220 vl_api_registration_t *reg;
221 vl_api_ipfix_classify_stream_details_t *rmp;
223 reg = vl_api_client_index_to_registration (mp->client_index);
227 rmp = vl_msg_api_alloc (sizeof (*rmp));
228 clib_memset (rmp, 0, sizeof (*rmp));
229 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);
234 vl_api_send_msg (reg, (u8 *) rmp);
238 vl_api_ipfix_classify_table_add_del_t_handler
239 (vl_api_ipfix_classify_table_add_del_t * mp)
241 vl_api_ipfix_classify_table_add_del_reply_t *rmp;
242 vl_api_registration_t *reg;
243 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;
248 u32 classify_table_index;
250 u8 transport_protocol;
253 reg = vl_api_client_index_to_registration (mp->client_index);
257 classify_table_index = ntohl (mp->table_id);
258 ip_version = mp->ip_version;
259 transport_protocol = mp->transport_protocol;
262 if (fcm->src_port == 0)
264 /* call set_ipfix_classify_stream first */
265 rv = VNET_API_ERROR_UNSPECIFIED;
269 clib_memset (&args, 0, sizeof (args));
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)
277 table = &fcm->tables[i];
285 rv = VNET_API_ERROR_VALUE_EXIST;
288 table = ipfix_classify_add_table ();
289 table->classify_table_index = classify_table_index;
295 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
300 table->ip_version = ip_version;
301 table->transport_protocol = transport_protocol;
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;
310 rv = vnet_flow_report_add_del (frm, &args, NULL);
312 /* If deleting, or add failed */
313 if (is_add == 0 || (rv && is_add))
314 ipfix_classify_delete_table (table - fcm->tables);
317 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
321 send_ipfix_classify_table_details (u32 table_index,
322 vl_api_registration_t * reg, u32 context)
324 flow_report_classify_main_t *fcm = &flow_report_classify_main;
325 vl_api_ipfix_classify_table_details_t *mp;
327 ipfix_classify_table_t *table = &fcm->tables[table_index];
329 mp = vl_msg_api_alloc (sizeof (*mp));
330 clib_memset (mp, 0, sizeof (*mp));
331 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;
337 vl_api_send_msg (reg, (u8 *) mp);
341 vl_api_ipfix_classify_table_dump_t_handler
342 (vl_api_ipfix_classify_table_dump_t * mp)
344 flow_report_classify_main_t *fcm = &flow_report_classify_main;
345 vl_api_registration_t *reg;
348 reg = vl_api_client_index_to_registration (mp->client_index);
352 for (i = 0; i < vec_len (fcm->tables); i++)
353 if (ipfix_classify_table_index_valid (i))
354 send_ipfix_classify_table_details (i, reg, mp->context);
358 vl_api_ipfix_flush_t_handler (vl_api_ipfix_flush_t * mp)
360 vl_api_ipfix_flush_reply_t *rmp;
361 vl_api_registration_t *reg;
362 vlib_main_t *vm = vlib_get_main ();
365 reg = vl_api_client_index_to_registration (mp->client_index);
369 /* poke the flow reporting process */
370 vlib_process_signal_event (vm, flow_report_process_node.index,
371 1 /* type_opaque */ , 0 /* data */ );
373 REPLY_MACRO (VL_API_IPFIX_FLUSH_REPLY);
378 * Add vpe's API message handlers to the table.
379 * vlib has already mapped shared memory and
380 * added the client registration handlers.
381 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
383 #define vl_msg_name_crc_list
384 #include <vnet/vnet_all_api_h.h>
385 #undef vl_msg_name_crc_list
388 setup_message_id_table (api_main_t * am)
390 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
391 foreach_vl_msg_name_crc_ipfix_export;
395 static clib_error_t *
396 flow_api_hookup (vlib_main_t * vm)
398 api_main_t *am = &api_main;
401 vl_msg_api_set_handlers(VL_API_##N, #n, \
402 vl_api_##n##_t_handler, \
404 vl_api_##n##_t_endian, \
405 vl_api_##n##_t_print, \
406 sizeof(vl_api_##n##_t), 1);
411 * Set up the (msg_name, crc, message-id) table
413 setup_message_id_table (am);
418 VLIB_API_INIT_FUNCTION (flow_api_hookup);
421 * fd.io coding-style-patch-verification: ON
424 * eval: (c-set-style "gnu")