Fix bugs in emacs plugin templates
[vpp.git] / extras / emacs / plugin-main-skel.el
1 ;;; plugin-main-skel.el - vpp engine plug-in "main.c" skeleton
2 ;;;
3 ;;; Copyright (c) 2016 Cisco and/or its affiliates.
4 ;;; Licensed under the Apache License, Version 2.0 (the "License");
5 ;;; you may not use this file except in compliance with the License.
6 ;;; You may obtain a copy of the License at:
7 ;;;
8 ;;;     http://www.apache.org/licenses/LICENSE-2.0
9 ;;;
10 ;;; Unless required by applicable law or agreed to in writing, software
11 ;;; distributed under the License is distributed on an "AS IS" BASIS,
12 ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ;;; See the License for the specific language governing permissions and
14 ;;; limitations under the License.
15
16 (require 'skeleton)
17
18 (define-skeleton skel-plugin-main
19 "Insert a plug-in 'main.c' skeleton "
20 nil
21 '(if (not (boundp 'plugin-name))
22      (setq plugin-name (read-string "Plugin name: ")))
23 '(setq PLUGIN-NAME (upcase plugin-name))
24 '(setq capital-oh-en "ON")
25 "/*
26  * " plugin-name ".c - skeleton vpp engine plug-in
27  *
28  * Copyright (c) <current-year> <your-organization>
29  * Licensed under the Apache License, Version 2.0 (the \"License\");
30  * you may not use this file except in compliance with the License.
31  * You may obtain a copy of the License at:
32  *
33  *     http://www.apache.org/licenses/LICENSE-2.0
34  *
35  * Unless required by applicable law or agreed to in writing, software
36  * distributed under the License is distributed on an \"AS IS\" BASIS,
37  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38  * See the License for the specific language governing permissions and
39  * limitations under the License.
40  */
41
42 #include <vnet/vnet.h>
43 #include <vnet/plugin/plugin.h>
44 #include <" plugin-name "/" plugin-name ".h>
45
46 #include <vlibapi/api.h>
47 #include <vlibmemory/api.h>
48 #include <vpp/app/version.h>
49
50 /* define message IDs */
51 #include <" plugin-name "/" plugin-name "_msg_enum.h>
52
53 /* define message structures */
54 #define vl_typedefs
55 #include <" plugin-name "/" plugin-name "_all_api_h.h>
56 #undef vl_typedefs
57
58 /* define generated endian-swappers */
59 #define vl_endianfun
60 #include <" plugin-name "/" plugin-name "_all_api_h.h>
61 #undef vl_endianfun
62
63 /* instantiate all the print functions we know about */
64 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
65 #define vl_printfun
66 #include <" plugin-name "/" plugin-name "_all_api_h.h>
67 #undef vl_printfun
68
69 /* Get the API version number */
70 #define vl_api_version(n,v) static u32 api_version=(v);
71 #include <" plugin-name "/" plugin-name "_all_api_h.h>
72 #undef vl_api_version
73
74 #define REPLY_MSG_ID_BASE sm->msg_id_base
75 #include <vlibapi/api_helper_macros.h>
76
77 extern " plugin-name "_main_t " plugin-name "_main;
78
79 /* List of message types that this plugin understands */
80
81 #define foreach_" plugin-name "_plugin_api_msg                           \\
82 _(" PLUGIN-NAME "_ENABLE_DISABLE, " plugin-name "_enable_disable)
83
84 /* Action function shared between message handler and debug CLI */
85
86 int " plugin-name "_enable_disable (" plugin-name "_main_t * sm, u32 sw_if_index,
87                                    int enable_disable)
88 {
89   vnet_sw_interface_t * sw;
90   int rv = 0;
91
92   /* Utterly wrong? */
93   if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
94                           sw_if_index))
95     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
96
97   /* Not a physical port? */
98   sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
99   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
100     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
101
102   vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\",
103                                sw_if_index, enable_disable, 0, 0);
104
105   return rv;
106 }
107
108 static clib_error_t *
109 " plugin-name "_enable_disable_command_fn (vlib_main_t * vm,
110                                    unformat_input_t * input,
111                                    vlib_cli_command_t * cmd)
112 {
113   " plugin-name "_main_t * sm = &" plugin-name "_main;
114   u32 sw_if_index = ~0;
115   int enable_disable = 1;
116
117   int rv;
118
119   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
120     {
121       if (unformat (input, \"disable\"))
122         enable_disable = 0;
123       else if (unformat (input, \"%U\", unformat_vnet_sw_interface,
124                          sm->vnet_main, &sw_if_index))
125         ;
126       else
127         break;
128   }
129
130   if (sw_if_index == ~0)
131     return clib_error_return (0, \"Please specify an interface...\");
132
133   rv = " plugin-name "_enable_disable (sm, sw_if_index, enable_disable);
134
135   switch(rv) 
136     {
137   case 0:
138     break;
139
140   case VNET_API_ERROR_INVALID_SW_IF_INDEX:
141     return clib_error_return
142       (0, \"Invalid interface, only works on physical ports\");
143     break;
144
145   case VNET_API_ERROR_UNIMPLEMENTED:
146     return clib_error_return (0, \"Device driver doesn't support redirection\");
147     break;
148
149   default:
150     return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\",
151                               rv);
152     }
153   return 0;
154 }
155
156 /* *INDENT-OFF* */
157 VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) = 
158 {
159   .path = \"" plugin-name " enable-disable\",
160   .short_help =
161   \"" plugin-name " enable-disable <interface-name> [disable]\",
162   .function = " plugin-name "_enable_disable_command_fn,
163 };
164 /* *INDENT-ON* */
165
166 /* API message handler */
167 static void vl_api_" plugin-name "_enable_disable_t_handler
168 (vl_api_" plugin-name "_enable_disable_t * mp)
169 {
170   vl_api_" plugin-name "_enable_disable_reply_t * rmp;
171   " plugin-name "_main_t * sm = &" plugin-name "_main;
172   int rv;
173
174   rv = " plugin-name "_enable_disable (sm, ntohl(mp->sw_if_index),
175                                       (int) (mp->enable_disable));
176
177   REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY);
178 }
179
180 /* Set up the API message handling tables */
181 static clib_error_t *
182 " plugin-name "_plugin_api_hookup (vlib_main_t *vm)
183 {
184   " plugin-name "_main_t * sm = &" plugin-name "_main;
185 #define _(N,n)                                                  \\
186     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \\
187                            #n,                                  \\
188                            vl_api_##n##_t_handler,              \\
189                            vl_noop_handler,                     \\
190                            vl_api_##n##_t_endian,               \\
191                            vl_api_##n##_t_print,                \\
192                            sizeof(vl_api_##n##_t), 1);
193     foreach_" plugin-name "_plugin_api_msg;
194 #undef _
195
196     return 0;
197 }
198
199 #define vl_msg_name_crc_list
200 #include <" plugin-name "/" plugin-name "_all_api_h.h>
201 #undef vl_msg_name_crc_list
202
203 static void
204 setup_message_id_table (" plugin-name "_main_t * sm, api_main_t * am)
205 {
206 #define _(id,n,crc) \
207   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
208   foreach_vl_msg_name_crc_" plugin-name" ;
209 #undef _
210 }
211
212 static clib_error_t * " plugin-name "_init (vlib_main_t * vm)
213 {
214   " plugin-name "_main_t * sm = &" plugin-name "_main;
215   clib_error_t * error = 0;
216   u8 * name;
217
218   sm->vlib_main = vm;
219   sm->vnet_main = vnet_get_main();
220
221   name = format (0, \"" plugin-name "_%08x%c\", api_version, 0);
222
223   /* Ask for a correctly-sized block of API message decode slots */
224   sm->msg_id_base = vl_msg_api_get_msg_ids
225       ((char *) name, VL_MSG_FIRST_AVAILABLE);
226
227   error = " plugin-name "_plugin_api_hookup (vm);
228
229   /* Add our API messages to the global name_crc hash table */
230   setup_message_id_table (sm, &api_main);
231
232   vec_free(name);
233
234   return error;
235 }
236
237 VLIB_INIT_FUNCTION (" plugin-name "_init);
238
239 /* *INDENT-OFF* */
240 VNET_FEATURE_INIT (" plugin-name ", static) =
241 {
242   .arc_name = \"device-input\",
243   .node_name = \"" plugin-name "\",
244   .runs_before = VNET_FEATURES (\"ethernet-input\"),
245 };
246 /* *INDENT-ON */
247
248 /* *INDENT-OFF* */
249 VLIB_PLUGIN_REGISTER () = 
250 {
251   .version = VPP_BUILD_VER,
252   .description = \"" plugin-name " plugin description goes here\",
253 };
254 /* *INDENT-ON* */
255
256 /*
257  * fd.io coding-style-patch-verification: " capital-oh-en "
258  *
259  * Local Variables:
260  * eval: (c-set-style \"gnu\")
261  * End:
262  */
263 ")
264