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 #include <sample/sample.api_enum.h>
28 #include <sample/sample.api_types.h>
30 #define REPLY_MSG_ID_BASE sm->msg_id_base
31 #include <vlibapi/api_helper_macros.h>
34 VLIB_PLUGIN_REGISTER () = {
35 .version = SAMPLE_PLUGIN_BUILD_VER,
36 .description = "Sample of VPP Plugin",
40 sample_main_t sample_main;
43 * @brief Enable/disable the macswap plugin.
45 * Action function shared between message handler and debug CLI.
48 int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
51 vnet_sw_interface_t * sw;
55 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
57 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
59 /* Not a physical port? */
60 sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
61 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
62 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
64 vnet_feature_enable_disable ("device-input", "sample",
65 sw_if_index, enable_disable, 0, 0);
71 macswap_enable_disable_command_fn (vlib_main_t * vm,
72 unformat_input_t * input,
73 vlib_cli_command_t * cmd)
75 sample_main_t * sm = &sample_main;
77 int enable_disable = 1;
81 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
82 if (unformat (input, "disable"))
84 else if (unformat (input, "%U", unformat_vnet_sw_interface,
85 sm->vnet_main, &sw_if_index))
91 if (sw_if_index == ~0)
92 return clib_error_return (0, "Please specify an interface...");
94 rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable);
100 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
101 return clib_error_return
102 (0, "Invalid interface, only works on physical ports");
105 case VNET_API_ERROR_UNIMPLEMENTED:
106 return clib_error_return (0, "Device driver doesn't support redirection");
110 return clib_error_return (0, "sample_macswap_enable_disable returned %d",
117 * @brief CLI command to enable/disable the sample macswap plugin.
119 VLIB_CLI_COMMAND (sr_content_command, static) = {
120 .path = "sample macswap",
122 "sample macswap <interface-name> [disable]",
123 .function = macswap_enable_disable_command_fn,
127 * @brief Plugin API message handler.
129 static void vl_api_sample_macswap_enable_disable_t_handler
130 (vl_api_sample_macswap_enable_disable_t * mp)
132 vl_api_sample_macswap_enable_disable_reply_t * rmp;
133 sample_main_t * sm = &sample_main;
136 rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index),
137 (int) (mp->enable_disable));
139 REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY);
142 /* API definitions */
143 #include <sample/sample.api.c>
146 * @brief Initialize the sample plugin.
148 static clib_error_t * sample_init (vlib_main_t * vm)
150 sample_main_t * sm = &sample_main;
152 sm->vnet_main = vnet_get_main ();
154 /* Add our API messages to the global name_crc hash table */
155 sm->msg_id_base = setup_message_id_table ();
160 VLIB_INIT_FUNCTION (sample_init);
163 * @brief Hook the sample plugin into the VPP graph hierarchy.
165 VNET_FEATURE_INIT (sample, static) =
167 .arc_name = "device-input",
168 .node_name = "sample",
169 .runs_before = VNET_FEATURES ("ethernet-input"),