2 * Copyright (c) 2015 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 * sample.c - simple MAC-swap API / debug CLI handling
18 *------------------------------------------------------------------
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 #include <sample/sample.h>
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 #include <vlibsocket/api.h>
29 /* define message IDs */
30 #include <sample/sample_msg_enum.h>
32 /* define message structures */
34 #include <sample/sample_all_api_h.h>
37 /* define generated endian-swappers */
39 #include <sample/sample_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <sample/sample_all_api_h.h>
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <sample/sample_all_api_h.h>
54 * A handy macro to set up a message reply.
55 * Assumes that the following variables are available:
56 * mp - pointer to request message
57 * rmp - pointer to reply message type
61 #define REPLY_MACRO(t) \
63 unix_shared_memory_queue_t * q = \
64 vl_api_client_index_to_input_queue (mp->client_index); \
68 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
69 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
70 rmp->context = mp->context; \
71 rmp->retval = ntohl(rv); \
73 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
77 /* List of message types that this plugin understands */
79 #define foreach_sample_plugin_api_msg \
80 _(SAMPLE_MACSWAP_ENABLE_DISABLE, sample_macswap_enable_disable)
83 * This routine exists to convince the vlib plugin framework that
84 * we haven't accidentally copied a random .dll into the plugin directory.
86 * Also collects global variable pointers passed from the vpp engine
90 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
93 sample_main_t * sm = &sample_main;
94 clib_error_t * error = 0;
97 sm->vnet_main = h->vnet_main;
98 sm->ethernet_main = h->ethernet_main;
103 /* Action function shared between message handler and debug CLI */
105 int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
108 vnet_sw_interface_t * sw;
110 u32 node_index = enable_disable ? sample_node.index : ~0;
113 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
115 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
117 /* Not a physical port? */
118 sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
119 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
120 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
123 * Redirect pkts from the driver to the macswap node.
124 * Returns VNET_API_ERROR_UNIMPLEMENTED if the h/w driver
125 * doesn't implement the API.
127 * Node_index = ~0 => shut off redirection
129 rv = vnet_hw_interface_rx_redirect_to_node (sm->vnet_main, sw_if_index,
134 static clib_error_t *
135 macswap_enable_disable_command_fn (vlib_main_t * vm,
136 unformat_input_t * input,
137 vlib_cli_command_t * cmd)
139 sample_main_t * sm = &sample_main;
140 u32 sw_if_index = ~0;
141 int enable_disable = 1;
145 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
146 if (unformat (input, "disable"))
148 else if (unformat (input, "%U", unformat_vnet_sw_interface,
149 sm->vnet_main, &sw_if_index))
155 if (sw_if_index == ~0)
156 return clib_error_return (0, "Please specify an interface...");
158 rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable);
164 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
165 return clib_error_return
166 (0, "Invalid interface, only works on physical ports");
169 case VNET_API_ERROR_UNIMPLEMENTED:
170 return clib_error_return (0, "Device driver doesn't support redirection");
174 return clib_error_return (0, "sample_macswap_enable_disable returned %d",
180 VLIB_CLI_COMMAND (sr_content_command, static) = {
181 .path = "sample macswap",
183 "sample macswap <interface-name> [disable]",
184 .function = macswap_enable_disable_command_fn,
187 /* API message handler */
188 static void vl_api_sample_macswap_enable_disable_t_handler
189 (vl_api_sample_macswap_enable_disable_t * mp)
191 vl_api_sample_macswap_enable_disable_reply_t * rmp;
192 sample_main_t * sm = &sample_main;
195 rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index),
196 (int) (mp->enable_disable));
198 REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY);
201 /* Set up the API message handling tables */
202 static clib_error_t *
203 sample_plugin_api_hookup (vlib_main_t *vm)
205 sample_main_t * sm = &sample_main;
207 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
209 vl_api_##n##_t_handler, \
211 vl_api_##n##_t_endian, \
212 vl_api_##n##_t_print, \
213 sizeof(vl_api_##n##_t), 1);
214 foreach_sample_plugin_api_msg;
220 static clib_error_t * sample_init (vlib_main_t * vm)
222 sample_main_t * sm = &sample_main;
223 clib_error_t * error = 0;
226 name = format (0, "sample_%08x%c", api_version, 0);
228 /* Ask for a correctly-sized block of API message decode slots */
229 sm->msg_id_base = vl_msg_api_get_msg_ids
230 ((char *) name, VL_MSG_FIRST_AVAILABLE);
232 error = sample_plugin_api_hookup (vm);
239 VLIB_INIT_FUNCTION (sample_init);