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>
24 #include <vnet/interface.h>
25 #include <vnet/api_errno.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/ipfix-export/flow_report.h>
29 #include <vnet/ipfix-export/flow_report_classify.h>
31 #include <vnet/vnet_msg_enum.h>
33 #define vl_typedefs /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
37 #define vl_endianfun /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #include <vnet/vnet_all_api_h.h>
47 #include <vlibapi/api_helper_macros.h>
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) \
55 _(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump) \
56 _(IPFIX_FLUSH, ipfix_flush)
59 vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
61 vlib_main_t *vm = vlib_get_main ();
62 flow_report_main_t *frm = &flow_report_main;
63 vl_api_registration_t *reg;
64 vl_api_set_ipfix_exporter_reply_t *rmp;
65 ip4_address_t collector, src;
66 u16 collector_port = UDP_DST_PORT_ipfix;
68 u32 template_interval;
74 reg = vl_api_client_index_to_registration (mp->client_index);
78 if (mp->src_address.af == ADDRESS_IP6
79 || mp->collector_address.af == ADDRESS_IP6)
81 rv = VNET_API_ERROR_UNIMPLEMENTED;
85 ip4_address_decode (mp->collector_address.un.ip4, &collector);
86 collector_port = ntohs (mp->collector_port);
87 if (collector_port == (u16) ~ 0)
88 collector_port = UDP_DST_PORT_ipfix;
89 ip4_address_decode (mp->src_address.un.ip4, &src);
90 fib_id = ntohl (mp->vrf_id);
92 ip4_main_t *im = &ip4_main;
99 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
102 rv = VNET_API_ERROR_NO_SUCH_FIB;
108 path_mtu = ntohl (mp->path_mtu);
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;
116 if (collector.as_u32 == 0)
118 rv = VNET_API_ERROR_INVALID_VALUE;
124 rv = VNET_API_ERROR_INVALID_VALUE;
128 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
130 rv = VNET_API_ERROR_INVALID_VALUE;
136 rv = VNET_API_ERROR_INVALID_VALUE;
140 /* Reset report streams if we are reconfiguring IP addresses */
141 if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
142 frm->src_address.as_u32 != src.as_u32 ||
143 frm->collector_port != collector_port)
144 vnet_flow_reports_reset (frm);
146 frm->ipfix_collector.as_u32 = collector.as_u32;
147 frm->collector_port = collector_port;
148 frm->src_address.as_u32 = src.as_u32;
149 frm->fib_index = fib_index;
150 frm->path_mtu = path_mtu;
151 frm->template_interval = template_interval;
152 frm->udp_checksum = udp_checksum;
154 /* Turn on the flow reporting process */
155 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
158 REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
162 vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
164 flow_report_main_t *frm = &flow_report_main;
165 vl_api_registration_t *reg;
166 vl_api_ipfix_exporter_details_t *rmp;
167 ip4_main_t *im = &ip4_main;
168 ip46_address_t collector = {.as_u64[0] = 0,.as_u64[1] = 0 };
169 ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 };
172 reg = vl_api_client_index_to_registration (mp->client_index);
176 rmp = vl_msg_api_alloc (sizeof (*rmp));
177 clib_memset (rmp, 0, sizeof (*rmp));
178 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
179 rmp->context = mp->context;
181 memcpy (&collector.ip4, &frm->ipfix_collector, sizeof (ip4_address_t));
182 ip_address_encode (&collector, IP46_TYPE_IP4, &rmp->collector_address);
184 rmp->collector_port = htons (frm->collector_port);
186 memcpy (&src.ip4, &frm->src_address, sizeof (ip4_address_t));
187 ip_address_encode (&src, IP46_TYPE_IP4, &rmp->src_address);
189 if (frm->fib_index == ~0)
192 vrf_id = im->fibs[frm->fib_index].ft_table_id;
193 rmp->vrf_id = htonl (vrf_id);
194 rmp->path_mtu = htonl (frm->path_mtu);
195 rmp->template_interval = htonl (frm->template_interval);
196 rmp->udp_checksum = (frm->udp_checksum != 0);
198 vl_api_send_msg (reg, (u8 *) rmp);
202 vl_api_set_ipfix_classify_stream_t_handler
203 (vl_api_set_ipfix_classify_stream_t * mp)
205 vl_api_set_ipfix_classify_stream_reply_t *rmp;
206 flow_report_classify_main_t *fcm = &flow_report_classify_main;
207 flow_report_main_t *frm = &flow_report_main;
209 u32 src_port = UDP_DST_PORT_ipfix;
212 domain_id = ntohl (mp->domain_id);
213 src_port = ntohs (mp->src_port);
215 if (fcm->src_port != 0 &&
216 (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
218 int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
219 domain_id, (u16) src_port);
223 fcm->domain_id = domain_id;
224 fcm->src_port = (u16) src_port;
226 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
230 vl_api_ipfix_classify_stream_dump_t_handler
231 (vl_api_ipfix_classify_stream_dump_t * mp)
233 flow_report_classify_main_t *fcm = &flow_report_classify_main;
234 vl_api_registration_t *reg;
235 vl_api_ipfix_classify_stream_details_t *rmp;
237 reg = vl_api_client_index_to_registration (mp->client_index);
241 rmp = vl_msg_api_alloc (sizeof (*rmp));
242 clib_memset (rmp, 0, sizeof (*rmp));
243 rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
244 rmp->context = mp->context;
245 rmp->domain_id = htonl (fcm->domain_id);
246 rmp->src_port = htons (fcm->src_port);
248 vl_api_send_msg (reg, (u8 *) rmp);
252 vl_api_ipfix_classify_table_add_del_t_handler
253 (vl_api_ipfix_classify_table_add_del_t * mp)
255 vl_api_ipfix_classify_table_add_del_reply_t *rmp;
256 vl_api_registration_t *reg;
257 flow_report_classify_main_t *fcm = &flow_report_classify_main;
258 flow_report_main_t *frm = &flow_report_main;
259 vnet_flow_report_add_del_args_t args;
260 ipfix_classify_table_t *table;
262 u32 classify_table_index;
264 u8 transport_protocol;
267 reg = vl_api_client_index_to_registration (mp->client_index);
271 classify_table_index = ntohl (mp->table_id);
272 ip_version = ntohl (mp->ip_version);
273 transport_protocol = ntohl (mp->transport_protocol);
276 if (fcm->src_port == 0)
278 /* call set_ipfix_classify_stream first */
279 rv = VNET_API_ERROR_UNSPECIFIED;
283 clib_memset (&args, 0, sizeof (args));
287 for (i = 0; i < vec_len (fcm->tables); i++)
288 if (ipfix_classify_table_index_valid (i))
289 if (fcm->tables[i].classify_table_index == classify_table_index)
291 table = &fcm->tables[i];
299 rv = VNET_API_ERROR_VALUE_EXIST;
302 table = ipfix_classify_add_table ();
303 table->classify_table_index = classify_table_index;
309 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
314 table->ip_version = ip_version;
315 table->transport_protocol = transport_protocol;
317 args.opaque.as_uword = table - fcm->tables;
318 args.rewrite_callback = ipfix_classify_template_rewrite;
319 args.flow_data_callback = ipfix_classify_send_flows;
320 args.is_add = is_add;
321 args.domain_id = fcm->domain_id;
322 args.src_port = fcm->src_port;
324 rv = vnet_flow_report_add_del (frm, &args, NULL);
326 /* If deleting, or add failed */
327 if (is_add == 0 || (rv && is_add))
328 ipfix_classify_delete_table (table - fcm->tables);
331 REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
335 send_ipfix_classify_table_details (u32 table_index,
336 vl_api_registration_t * reg, u32 context)
338 flow_report_classify_main_t *fcm = &flow_report_classify_main;
339 vl_api_ipfix_classify_table_details_t *mp;
341 ipfix_classify_table_t *table = &fcm->tables[table_index];
343 mp = vl_msg_api_alloc (sizeof (*mp));
344 clib_memset (mp, 0, sizeof (*mp));
345 mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
346 mp->context = context;
347 mp->table_id = htonl (table->classify_table_index);
348 mp->ip_version = htonl (table->ip_version);
349 mp->transport_protocol = htonl (table->transport_protocol);
351 vl_api_send_msg (reg, (u8 *) mp);
355 vl_api_ipfix_classify_table_dump_t_handler
356 (vl_api_ipfix_classify_table_dump_t * mp)
358 flow_report_classify_main_t *fcm = &flow_report_classify_main;
359 vl_api_registration_t *reg;
362 reg = vl_api_client_index_to_registration (mp->client_index);
366 for (i = 0; i < vec_len (fcm->tables); i++)
367 if (ipfix_classify_table_index_valid (i))
368 send_ipfix_classify_table_details (i, reg, mp->context);
372 vl_api_ipfix_flush_t_handler (vl_api_ipfix_flush_t * mp)
374 vl_api_ipfix_flush_reply_t *rmp;
375 vl_api_registration_t *reg;
376 vlib_main_t *vm = vlib_get_main ();
379 reg = vl_api_client_index_to_registration (mp->client_index);
383 /* poke the flow reporting process */
384 vlib_process_signal_event (vm, flow_report_process_node.index,
385 1 /* type_opaque */ , 0 /* data */ );
387 REPLY_MACRO (VL_API_IPFIX_FLUSH_REPLY);
392 * Add vpe's API message handlers to the table.
393 * vlib has already mapped shared memory and
394 * added the client registration handlers.
395 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
397 #define vl_msg_name_crc_list
398 #include <vnet/vnet_all_api_h.h>
399 #undef vl_msg_name_crc_list
402 setup_message_id_table (api_main_t * am)
404 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
405 foreach_vl_msg_name_crc_ipfix_export;
409 static clib_error_t *
410 flow_api_hookup (vlib_main_t * vm)
412 api_main_t *am = vlibapi_get_main ();
415 vl_msg_api_set_handlers(VL_API_##N, #n, \
416 vl_api_##n##_t_handler, \
418 vl_api_##n##_t_endian, \
419 vl_api_##n##_t_print, \
420 sizeof(vl_api_##n##_t), 1);
425 * Set up the (msg_name, crc, message-id) table
427 setup_message_id_table (am);
432 VLIB_API_INIT_FUNCTION (flow_api_hookup);
435 * fd.io coding-style-patch-verification: ON
438 * eval: (c-set-style "gnu")