2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 *------------------------------------------------------------------
17 * ioam_export.c - ioam export API / debug CLI handling
18 *------------------------------------------------------------------
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 #include <ioam/export-common/ioam_export.h>
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 #include <vlibsocket/api.h>
28 #include <vnet/ip/ip6_hop_by_hop.h>
31 /* define message IDs */
32 #include <ioam/export/ioam_export_msg_enum.h>
34 /* define message structures */
36 #include <ioam/export/ioam_export_all_api_h.h>
39 /* define generated endian-swappers */
41 #include <ioam/export/ioam_export_all_api_h.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #include <ioam/export/ioam_export_all_api_h.h>
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <ioam/export/ioam_export_all_api_h.h>
56 * A handy macro to set up a message reply.
57 * Assumes that the following variables are available:
58 * mp - pointer to request message
59 * rmp - pointer to reply message type
63 #define REPLY_MACRO(t) \
65 unix_shared_memory_queue_t * q = \
66 vl_api_client_index_to_input_queue (mp->client_index); \
70 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
71 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
72 rmp->context = mp->context; \
73 rmp->retval = ntohl(rv); \
75 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
79 /* List of message types that this plugin understands */
81 #define foreach_ioam_export_plugin_api_msg \
82 _(IOAM_EXPORT_IP6_ENABLE_DISABLE, ioam_export_ip6_enable_disable)
84 ioam_export_main_t ioam_export_main;
86 extern vlib_node_registration_t export_node;
88 /* Action function shared between message handler and debug CLI */
91 ioam_export_ip6_enable_disable (ioam_export_main_t * em,
93 ip4_address_t * collector_address,
94 ip4_address_t * src_address)
96 vlib_main_t *vm = em->vlib_main;
100 if (1 == ioam_export_header_create (em, collector_address, src_address))
102 ioam_export_thread_buffer_init (em, vm);
103 ip6_hbh_set_next_override (em->my_hbh_slot);
104 /* Turn on the export buffer check process */
105 vlib_process_signal_event (vm, em->export_process_node_index, 1, 0);
115 ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
116 ioam_export_header_cleanup (em, collector_address, src_address);
117 ioam_export_thread_buffer_free (em);
118 /* Turn off the export buffer check process */
119 vlib_process_signal_event (vm, em->export_process_node_index, 2, 0);
126 /* API message handler */
127 static void vl_api_ioam_export_ip6_enable_disable_t_handler
128 (vl_api_ioam_export_ip6_enable_disable_t * mp)
130 vl_api_ioam_export_ip6_enable_disable_reply_t *rmp;
131 ioam_export_main_t *sm = &ioam_export_main;
134 rv = ioam_export_ip6_enable_disable (sm, (int) (mp->is_disable),
136 mp->collector_address,
137 (ip4_address_t *) mp->src_address);
139 REPLY_MACRO (VL_API_IOAM_EXPORT_IP6_ENABLE_DISABLE_REPLY);
142 /* Set up the API message handling tables */
143 static clib_error_t *
144 ioam_export_plugin_api_hookup (vlib_main_t * vm)
146 ioam_export_main_t *sm = &ioam_export_main;
148 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
150 vl_api_##n##_t_handler, \
152 vl_api_##n##_t_endian, \
153 vl_api_##n##_t_print, \
154 sizeof(vl_api_##n##_t), 1);
155 foreach_ioam_export_plugin_api_msg;
161 #define vl_msg_name_crc_list
162 #include <ioam/export/ioam_export_all_api_h.h>
163 #undef vl_msg_name_crc_list
166 setup_message_id_table (ioam_export_main_t * sm, api_main_t * am)
168 #define _(id,n,crc) \
169 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
170 foreach_vl_msg_name_crc_ioam_export;
174 static clib_error_t *
175 set_ioam_export_ipfix_command_fn (vlib_main_t * vm,
176 unformat_input_t * input,
177 vlib_cli_command_t * cmd)
179 ioam_export_main_t *em = &ioam_export_main;
180 ip4_address_t collector, src;
183 collector.as_u32 = 0;
186 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
188 if (unformat (input, "collector %U", unformat_ip4_address, &collector))
190 else if (unformat (input, "src %U", unformat_ip4_address, &src))
192 else if (unformat (input, "disable"))
198 if (collector.as_u32 == 0)
199 return clib_error_return (0, "collector address required");
202 return clib_error_return (0, "src address required");
204 em->ipfix_collector.as_u32 = collector.as_u32;
205 em->src_address.as_u32 = src.as_u32;
207 vlib_cli_output (vm, "Collector %U, src address %U",
208 format_ip4_address, &em->ipfix_collector,
209 format_ip4_address, &em->src_address);
211 /* Turn on the export timer process */
212 // vlib_process_signal_event (vm, flow_report_process_node.index,
214 ioam_export_ip6_enable_disable (em, is_disable, &collector, &src);
220 VLIB_CLI_COMMAND (set_ipfix_command, static) =
222 .path = "set ioam export ipfix",.short_help =
223 "set ioam export ipfix collector <ip4-address> src <ip4-address>",.
224 function = set_ioam_export_ipfix_command_fn,};
228 static clib_error_t *
229 ioam_export_init (vlib_main_t * vm)
231 ioam_export_main_t *em = &ioam_export_main;
232 clib_error_t *error = 0;
234 u32 node_index = export_node.index;
235 vlib_node_t *ip6_hbyh_node = NULL;
238 em->vnet_main = vnet_get_main ();
239 em->set_id = IPFIX_IOAM_EXPORT_ID;
240 ioam_export_reset_next_node (em);
242 name = format (0, "ioam_export_%08x%c", api_version, 0);
244 /* Ask for a correctly-sized block of API message decode slots */
245 em->msg_id_base = vl_msg_api_get_msg_ids
246 ((char *) name, VL_MSG_FIRST_AVAILABLE);
247 em->unix_time_0 = (u32) time (0); /* Store starting time */
248 em->vlib_time_0 = vlib_time_now (vm);
250 error = ioam_export_plugin_api_hookup (vm);
252 /* Add our API messages to the global name_crc hash table */
253 setup_message_id_table (em, &api_main);
255 /* Hook this export node to ip6-hop-by-hop */
256 ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
257 em->my_hbh_slot = vlib_node_add_next (vm, ip6_hbyh_node->index, node_index);
263 VLIB_INIT_FUNCTION (ioam_export_init);
266 * fd.io coding-style-patch-verification: ON
269 * eval: (c-set-style "gnu")