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.
17 * @brief Sample Plugin, plugin API / trace / CLI handling.
20 #include <vnet/vnet.h>
21 #include <vnet/plugin/plugin.h>
22 #include <sample/sample.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
27 /* define message IDs */
28 #include <sample/sample_msg_enum.h>
30 /* define message structures */
32 #include <sample/sample_all_api_h.h>
35 /* define generated endian-swappers */
37 #include <sample/sample_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 <sample/sample_all_api_h.h>
46 /* Get the API version number */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <sample/sample_all_api_h.h>
51 #define REPLY_MSG_ID_BASE sm->msg_id_base
52 #include <vlibapi/api_helper_macros.h>
54 /* List of message types that this plugin understands */
56 #define foreach_sample_plugin_api_msg \
57 _(SAMPLE_MACSWAP_ENABLE_DISABLE, sample_macswap_enable_disable)
60 VLIB_PLUGIN_REGISTER () = {
61 .version = SAMPLE_PLUGIN_BUILD_VER,
62 .description = "Sample of VPP Plugin",
66 sample_main_t sample_main;
69 * @brief Enable/disable the macswap plugin.
71 * Action function shared between message handler and debug CLI.
74 int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
77 vnet_sw_interface_t * sw;
81 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
83 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
85 /* Not a physical port? */
86 sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
87 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
88 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
90 vnet_feature_enable_disable ("device-input", "sample",
91 sw_if_index, enable_disable, 0, 0);
97 macswap_enable_disable_command_fn (vlib_main_t * vm,
98 unformat_input_t * input,
99 vlib_cli_command_t * cmd)
101 sample_main_t * sm = &sample_main;
102 u32 sw_if_index = ~0;
103 int enable_disable = 1;
107 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
108 if (unformat (input, "disable"))
110 else if (unformat (input, "%U", unformat_vnet_sw_interface,
111 sm->vnet_main, &sw_if_index))
117 if (sw_if_index == ~0)
118 return clib_error_return (0, "Please specify an interface...");
120 rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable);
126 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
127 return clib_error_return
128 (0, "Invalid interface, only works on physical ports");
131 case VNET_API_ERROR_UNIMPLEMENTED:
132 return clib_error_return (0, "Device driver doesn't support redirection");
136 return clib_error_return (0, "sample_macswap_enable_disable returned %d",
143 * @brief CLI command to enable/disable the sample macswap plugin.
145 VLIB_CLI_COMMAND (sr_content_command, static) = {
146 .path = "sample macswap",
148 "sample macswap <interface-name> [disable]",
149 .function = macswap_enable_disable_command_fn,
153 * @brief Plugin API message handler.
155 static void vl_api_sample_macswap_enable_disable_t_handler
156 (vl_api_sample_macswap_enable_disable_t * mp)
158 vl_api_sample_macswap_enable_disable_reply_t * rmp;
159 sample_main_t * sm = &sample_main;
162 rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index),
163 (int) (mp->enable_disable));
165 REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY);
169 * @brief Set up the API message handling tables.
171 static clib_error_t *
172 sample_plugin_api_hookup (vlib_main_t *vm)
174 sample_main_t * sm = &sample_main;
176 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
178 vl_api_##n##_t_handler, \
180 vl_api_##n##_t_endian, \
181 vl_api_##n##_t_print, \
182 sizeof(vl_api_##n##_t), 1);
183 foreach_sample_plugin_api_msg;
189 #define vl_msg_name_crc_list
190 #include <sample/sample_all_api_h.h>
191 #undef vl_msg_name_crc_list
194 setup_message_id_table (sample_main_t * sm, api_main_t *am)
196 #define _(id,n,crc) \
197 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
198 foreach_vl_msg_name_crc_sample;
203 * @brief Initialize the sample plugin.
205 static clib_error_t * sample_init (vlib_main_t * vm)
207 sample_main_t * sm = &sample_main;
208 clib_error_t * error = 0;
211 sm->vnet_main = vnet_get_main ();
213 name = format (0, "sample_%08x%c", api_version, 0);
215 /* Ask for a correctly-sized block of API message decode slots */
216 sm->msg_id_base = vl_msg_api_get_msg_ids
217 ((char *) name, VL_MSG_FIRST_AVAILABLE);
219 error = sample_plugin_api_hookup (vm);
221 /* Add our API messages to the global name_crc hash table */
222 setup_message_id_table (sm, &api_main);
229 VLIB_INIT_FUNCTION (sample_init);
232 * @brief Hook the sample plugin into the VPP graph hierarchy.
234 VNET_FEATURE_INIT (sample, static) =
236 .arc_name = "device-input",
237 .node_name = "sample",
238 .runs_before = VNET_FEATURES ("ethernet-input"),