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>
26 #include <vlibsocket/api.h>
28 /* define message IDs */
29 #include <sample/sample_msg_enum.h>
31 /* define message structures */
33 #include <sample/sample_all_api_h.h>
36 /* define generated endian-swappers */
38 #include <sample/sample_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 <sample/sample_all_api_h.h>
47 /* Get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <sample/sample_all_api_h.h>
52 #define REPLY_MSG_ID_BASE sm->msg_id_base
53 #include <vlibapi/api_helper_macros.h>
55 /* List of message types that this plugin understands */
57 #define foreach_sample_plugin_api_msg \
58 _(SAMPLE_MACSWAP_ENABLE_DISABLE, sample_macswap_enable_disable)
61 VLIB_PLUGIN_REGISTER () = {
62 .version = SAMPLE_PLUGIN_BUILD_VER,
63 .description = "Sample of VPP Plugin",
68 * @brief Enable/disable the macswap plugin.
70 * Action function shared between message handler and debug CLI.
73 int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
76 vnet_sw_interface_t * sw;
80 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
82 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
84 /* Not a physical port? */
85 sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
86 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
87 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
89 vnet_feature_enable_disable ("device-input", "sample",
90 sw_if_index, enable_disable, 0, 0);
96 macswap_enable_disable_command_fn (vlib_main_t * vm,
97 unformat_input_t * input,
98 vlib_cli_command_t * cmd)
100 sample_main_t * sm = &sample_main;
101 u32 sw_if_index = ~0;
102 int enable_disable = 1;
106 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
107 if (unformat (input, "disable"))
109 else if (unformat (input, "%U", unformat_vnet_sw_interface,
110 sm->vnet_main, &sw_if_index))
116 if (sw_if_index == ~0)
117 return clib_error_return (0, "Please specify an interface...");
119 rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable);
125 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
126 return clib_error_return
127 (0, "Invalid interface, only works on physical ports");
130 case VNET_API_ERROR_UNIMPLEMENTED:
131 return clib_error_return (0, "Device driver doesn't support redirection");
135 return clib_error_return (0, "sample_macswap_enable_disable returned %d",
142 * @brief CLI command to enable/disable the sample macswap plugin.
144 VLIB_CLI_COMMAND (sr_content_command, static) = {
145 .path = "sample macswap",
147 "sample macswap <interface-name> [disable]",
148 .function = macswap_enable_disable_command_fn,
152 * @brief Plugin API message handler.
154 static void vl_api_sample_macswap_enable_disable_t_handler
155 (vl_api_sample_macswap_enable_disable_t * mp)
157 vl_api_sample_macswap_enable_disable_reply_t * rmp;
158 sample_main_t * sm = &sample_main;
161 rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index),
162 (int) (mp->enable_disable));
164 REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY);
168 * @brief Set up the API message handling tables.
170 static clib_error_t *
171 sample_plugin_api_hookup (vlib_main_t *vm)
173 sample_main_t * sm = &sample_main;
175 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
177 vl_api_##n##_t_handler, \
179 vl_api_##n##_t_endian, \
180 vl_api_##n##_t_print, \
181 sizeof(vl_api_##n##_t), 1);
182 foreach_sample_plugin_api_msg;
188 #define vl_msg_name_crc_list
189 #include <sample/sample_all_api_h.h>
190 #undef vl_msg_name_crc_list
193 setup_message_id_table (sample_main_t * sm, api_main_t *am)
195 #define _(id,n,crc) \
196 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
197 foreach_vl_msg_name_crc_sample;
202 * @brief Initialize the sample plugin.
204 static clib_error_t * sample_init (vlib_main_t * vm)
206 sample_main_t * sm = &sample_main;
207 clib_error_t * error = 0;
210 sm->vnet_main = vnet_get_main ();
212 name = format (0, "sample_%08x%c", api_version, 0);
214 /* Ask for a correctly-sized block of API message decode slots */
215 sm->msg_id_base = vl_msg_api_get_msg_ids
216 ((char *) name, VL_MSG_FIRST_AVAILABLE);
218 error = sample_plugin_api_hookup (vm);
220 /* Add our API messages to the global name_crc hash table */
221 setup_message_id_table (sm, &api_main);
228 VLIB_INIT_FUNCTION (sample_init);
231 * @brief Hook the sample plugin into the VPP graph hierarchy.
233 VNET_FEATURE_INIT (sample, static) =
235 .arc_name = "device-input",
236 .node_name = "sample",
237 .runs_before = VNET_FEATURES ("ethernet-input"),