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