7588dbc0aea4d59559f3d6525034362448848a14
[vpp.git] / src / examples / sample-plugin / sample / sample.c
1 /*
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  */
15 /*
16  *------------------------------------------------------------------
17  * sample.c - simple MAC-swap API / debug CLI handling
18  *------------------------------------------------------------------
19  */
20
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 #include <sample/sample.h>
24
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 #include <vlibsocket/api.h>
28
29 /* define message IDs */
30 #include <sample/sample_msg_enum.h>
31
32 /* define message structures */
33 #define vl_typedefs
34 #include <sample/sample_all_api_h.h> 
35 #undef vl_typedefs
36
37 /* define generated endian-swappers */
38 #define vl_endianfun
39 #include <sample/sample_all_api_h.h> 
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <sample/sample_all_api_h.h> 
46 #undef vl_printfun
47
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>
51 #undef vl_api_version
52
53 /* 
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
58  * rv - return value
59  */
60
61 #define REPLY_MACRO(t)                                          \
62 do {                                                            \
63     unix_shared_memory_queue_t * q =                            \
64     vl_api_client_index_to_input_queue (mp->client_index);      \
65     if (!q)                                                     \
66         return;                                                 \
67                                                                 \
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);                                    \
72                                                                 \
73     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
74 } while(0);
75
76
77 /* List of message types that this plugin understands */
78
79 #define foreach_sample_plugin_api_msg                           \
80 _(SAMPLE_MACSWAP_ENABLE_DISABLE, sample_macswap_enable_disable)
81
82 /* *INDENT-OFF* */
83 VLIB_PLUGIN_REGISTER () = {
84     .version = SAMPLE_PLUGIN_BUILD_VER,
85 };
86 /* *INDENT-ON* */
87
88 /* Action function shared between message handler and debug CLI */
89
90 int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
91                                    int enable_disable)
92 {
93   vnet_sw_interface_t * sw;
94   int rv = 0;
95
96   /* Utterly wrong? */
97   if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces, 
98                           sw_if_index))
99     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
100
101   /* Not a physical port? */
102   sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
103   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
104     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
105   
106   vnet_feature_enable_disable ("device-input", "sample",
107                                sw_if_index, enable_disable, 0, 0);
108
109   return rv;
110 }
111
112 static clib_error_t *
113 macswap_enable_disable_command_fn (vlib_main_t * vm,
114                                    unformat_input_t * input,
115                                    vlib_cli_command_t * cmd)
116 {
117   sample_main_t * sm = &sample_main;
118   u32 sw_if_index = ~0;
119   int enable_disable = 1;
120     
121   int rv;
122
123   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
124     if (unformat (input, "disable"))
125       enable_disable = 0;
126     else if (unformat (input, "%U", unformat_vnet_sw_interface,
127                        sm->vnet_main, &sw_if_index))
128       ;
129     else
130       break;
131   }
132
133   if (sw_if_index == ~0)
134     return clib_error_return (0, "Please specify an interface...");
135     
136   rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable);
137
138   switch(rv) {
139   case 0:
140     break;
141
142   case VNET_API_ERROR_INVALID_SW_IF_INDEX:
143     return clib_error_return 
144       (0, "Invalid interface, only works on physical ports");
145     break;
146
147   case VNET_API_ERROR_UNIMPLEMENTED:
148     return clib_error_return (0, "Device driver doesn't support redirection");
149     break;
150
151   default:
152     return clib_error_return (0, "sample_macswap_enable_disable returned %d",
153                               rv);
154   }
155   return 0;
156 }
157
158 VLIB_CLI_COMMAND (sr_content_command, static) = {
159     .path = "sample macswap",
160     .short_help = 
161     "sample macswap <interface-name> [disable]",
162     .function = macswap_enable_disable_command_fn,
163 };
164
165 /* API message handler */
166 static void vl_api_sample_macswap_enable_disable_t_handler
167 (vl_api_sample_macswap_enable_disable_t * mp)
168 {
169   vl_api_sample_macswap_enable_disable_reply_t * rmp;
170   sample_main_t * sm = &sample_main;
171   int rv;
172
173   rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index), 
174                                       (int) (mp->enable_disable));
175   
176   REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY);
177 }
178
179 /* Set up the API message handling tables */
180 static clib_error_t *
181 sample_plugin_api_hookup (vlib_main_t *vm)
182 {
183   sample_main_t * sm = &sample_main;
184 #define _(N,n)                                                  \
185     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
186                            #n,                                  \
187                            vl_api_##n##_t_handler,              \
188                            vl_noop_handler,                     \
189                            vl_api_##n##_t_endian,               \
190                            vl_api_##n##_t_print,                \
191                            sizeof(vl_api_##n##_t), 1); 
192     foreach_sample_plugin_api_msg;
193 #undef _
194
195     return 0;
196 }
197
198 #define vl_msg_name_crc_list
199 #include <sample/sample_all_api_h.h>
200 #undef vl_msg_name_crc_list
201
202 static void 
203 setup_message_id_table (sample_main_t * sm, api_main_t *am)
204 {
205 #define _(id,n,crc) \
206   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
207   foreach_vl_msg_name_crc_sample;
208 #undef _
209 }
210
211 static clib_error_t * sample_init (vlib_main_t * vm)
212 {
213   sample_main_t * sm = &sample_main;
214   clib_error_t * error = 0;
215   u8 * name;
216
217   sm->vnet_main =  vnet_get_main ();
218
219   name = format (0, "sample_%08x%c", api_version, 0);
220
221   /* Ask for a correctly-sized block of API message decode slots */
222   sm->msg_id_base = vl_msg_api_get_msg_ids 
223       ((char *) name, VL_MSG_FIRST_AVAILABLE);
224
225   error = sample_plugin_api_hookup (vm);
226
227   /* Add our API messages to the global name_crc hash table */
228   setup_message_id_table (sm, &api_main);
229
230   vec_free(name);
231
232   return error;
233 }
234
235 VLIB_INIT_FUNCTION (sample_init);
236
237 VNET_FEATURE_INIT (sample, static) = 
238 {
239   .arc_name = "device-input",
240   .node_name = "sample",
241   .runs_before = VNET_FEATURES ("ethernet-input"),
242 };