blob: 703e604201fc7c28fb419d7c587866ec48597cee [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
Ray Kinsella583dc8d2017-06-08 15:54:19 +010015/**
16 * @file
17 * @brief Sample Plugin, plugin API / trace / CLI handling.
Ed Warnickecb9cada2015-12-08 15:45:58 -070018 */
19
20#include <vnet/vnet.h>
21#include <vnet/plugin/plugin.h>
22#include <sample/sample.h>
23
24#include <vlibapi/api.h>
25#include <vlibmemory/api.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
27/* define message IDs */
28#include <sample/sample_msg_enum.h>
29
30/* define message structures */
31#define vl_typedefs
32#include <sample/sample_all_api_h.h>
33#undef vl_typedefs
34
35/* define generated endian-swappers */
36#define vl_endianfun
37#include <sample/sample_all_api_h.h>
38#undef vl_endianfun
39
40/* instantiate all the print functions we know about */
41#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42#define vl_printfun
43#include <sample/sample_all_api_h.h>
44#undef vl_printfun
45
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>
49#undef vl_api_version
50
Eyal Barib614d082017-03-16 10:02:57 +020051#define REPLY_MSG_ID_BASE sm->msg_id_base
52#include <vlibapi/api_helper_macros.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
54/* List of message types that this plugin understands */
55
56#define foreach_sample_plugin_api_msg \
57_(SAMPLE_MACSWAP_ENABLE_DISABLE, sample_macswap_enable_disable)
58
Anlu Yana4cb12b2017-02-14 18:07:40 -080059/* *INDENT-OFF* */
60VLIB_PLUGIN_REGISTER () = {
61 .version = SAMPLE_PLUGIN_BUILD_VER,
Damjan Marion1bfb0dd2017-03-22 11:08:39 +010062 .description = "Sample of VPP Plugin",
Anlu Yana4cb12b2017-02-14 18:07:40 -080063};
64/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
Ray Kinsella583dc8d2017-06-08 15:54:19 +010066/**
67 * @brief Enable/disable the macswap plugin.
68 *
69 * Action function shared between message handler and debug CLI.
70 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
72int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
73 int enable_disable)
74{
75 vnet_sw_interface_t * sw;
Dave Barachb7e2f3d2016-11-08 16:47:34 -050076 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
78 /* Utterly wrong? */
79 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
80 sw_if_index))
81 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
82
83 /* Not a physical port? */
84 sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
85 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
86 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
87
Dave Barachb7e2f3d2016-11-08 16:47:34 -050088 vnet_feature_enable_disable ("device-input", "sample",
89 sw_if_index, enable_disable, 0, 0);
90
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 return rv;
92}
93
94static clib_error_t *
95macswap_enable_disable_command_fn (vlib_main_t * vm,
96 unformat_input_t * input,
97 vlib_cli_command_t * cmd)
98{
99 sample_main_t * sm = &sample_main;
100 u32 sw_if_index = ~0;
101 int enable_disable = 1;
102
103 int rv;
104
105 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
106 if (unformat (input, "disable"))
107 enable_disable = 0;
108 else if (unformat (input, "%U", unformat_vnet_sw_interface,
109 sm->vnet_main, &sw_if_index))
110 ;
111 else
112 break;
113 }
114
115 if (sw_if_index == ~0)
116 return clib_error_return (0, "Please specify an interface...");
117
118 rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable);
119
120 switch(rv) {
121 case 0:
122 break;
123
124 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
125 return clib_error_return
126 (0, "Invalid interface, only works on physical ports");
127 break;
128
129 case VNET_API_ERROR_UNIMPLEMENTED:
130 return clib_error_return (0, "Device driver doesn't support redirection");
131 break;
132
133 default:
134 return clib_error_return (0, "sample_macswap_enable_disable returned %d",
135 rv);
136 }
137 return 0;
138}
139
Ray Kinsella583dc8d2017-06-08 15:54:19 +0100140/**
141 * @brief CLI command to enable/disable the sample macswap plugin.
142 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143VLIB_CLI_COMMAND (sr_content_command, static) = {
144 .path = "sample macswap",
145 .short_help =
146 "sample macswap <interface-name> [disable]",
147 .function = macswap_enable_disable_command_fn,
148};
149
Ray Kinsella583dc8d2017-06-08 15:54:19 +0100150/**
151 * @brief Plugin API message handler.
152 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153static void vl_api_sample_macswap_enable_disable_t_handler
154(vl_api_sample_macswap_enable_disable_t * mp)
155{
156 vl_api_sample_macswap_enable_disable_reply_t * rmp;
157 sample_main_t * sm = &sample_main;
158 int rv;
159
160 rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index),
161 (int) (mp->enable_disable));
162
163 REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY);
164}
165
Ray Kinsella583dc8d2017-06-08 15:54:19 +0100166/**
167 * @brief Set up the API message handling tables.
168 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169static clib_error_t *
170sample_plugin_api_hookup (vlib_main_t *vm)
171{
172 sample_main_t * sm = &sample_main;
173#define _(N,n) \
174 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
175 #n, \
176 vl_api_##n##_t_handler, \
177 vl_noop_handler, \
178 vl_api_##n##_t_endian, \
179 vl_api_##n##_t_print, \
180 sizeof(vl_api_##n##_t), 1);
181 foreach_sample_plugin_api_msg;
182#undef _
183
184 return 0;
185}
186
Dave Barach557d1282016-11-10 14:22:49 -0500187#define vl_msg_name_crc_list
188#include <sample/sample_all_api_h.h>
189#undef vl_msg_name_crc_list
190
191static void
192setup_message_id_table (sample_main_t * sm, api_main_t *am)
193{
194#define _(id,n,crc) \
195 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
196 foreach_vl_msg_name_crc_sample;
197#undef _
198}
199
Ray Kinsella583dc8d2017-06-08 15:54:19 +0100200/**
201 * @brief Initialize the sample plugin.
202 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203static clib_error_t * sample_init (vlib_main_t * vm)
204{
205 sample_main_t * sm = &sample_main;
206 clib_error_t * error = 0;
207 u8 * name;
208
Anlu Yana4cb12b2017-02-14 18:07:40 -0800209 sm->vnet_main = vnet_get_main ();
210
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211 name = format (0, "sample_%08x%c", api_version, 0);
212
213 /* Ask for a correctly-sized block of API message decode slots */
214 sm->msg_id_base = vl_msg_api_get_msg_ids
215 ((char *) name, VL_MSG_FIRST_AVAILABLE);
216
217 error = sample_plugin_api_hookup (vm);
218
Dave Barach557d1282016-11-10 14:22:49 -0500219 /* Add our API messages to the global name_crc hash table */
220 setup_message_id_table (sm, &api_main);
221
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 vec_free(name);
223
224 return error;
225}
226
227VLIB_INIT_FUNCTION (sample_init);
228
Ray Kinsella583dc8d2017-06-08 15:54:19 +0100229/**
230 * @brief Hook the sample plugin into the VPP graph hierarchy.
231 */
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500232VNET_FEATURE_INIT (sample, static) =
233{
234 .arc_name = "device-input",
235 .node_name = "sample",
236 .runs_before = VNET_FEATURES ("ethernet-input"),
237};