blob: 632a1329c2d2c009d73b48734a8a5a06654ca829 [file] [log] [blame]
Dave Barachb852bfa2016-01-04 15:27:42 -05001;;; 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
Keith Burns (alagalah)ca46d8c2016-03-18 07:22:15 -070018(define-skeleton skel-plugin-main
Dave Barachb852bfa2016-01-04 15:27:42 -050019"Insert a plug-in 'main.c' skeleton "
20nil
21'(if (not (boundp 'plugin-name))
22 (setq plugin-name (read-string "Plugin name: ")))
23'(setq PLUGIN-NAME (upcase plugin-name))
Dave Barachfed79e82017-02-10 11:57:46 -050024'(setq capital-oh-en "ON")
Dave Barach82842442018-10-31 09:54:34 -040025'(setq main-p (concat (substring plugin-name 0 1) "mp"))
Dave Barachfed79e82017-02-10 11:57:46 -050026"/*
Dave Barach557d1282016-11-10 14:22:49 -050027 * " plugin-name ".c - skeleton vpp engine plug-in
Dave Barachb852bfa2016-01-04 15:27:42 -050028 *
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>
Dave Barachfed79e82017-02-10 11:57:46 -050049#include <vpp/app/version.h>
Dave Barach913f4c92019-05-29 09:59:51 -040050#include <stdbool.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050051
52/* define message IDs */
53#include <" plugin-name "/" plugin-name "_msg_enum.h>
54
55/* define message structures */
56#define vl_typedefs
Dave Barach557d1282016-11-10 14:22:49 -050057#include <" plugin-name "/" plugin-name "_all_api_h.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050058#undef vl_typedefs
59
60/* define generated endian-swappers */
61#define vl_endianfun
Dave Barach557d1282016-11-10 14:22:49 -050062#include <" plugin-name "/" plugin-name "_all_api_h.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050063#undef vl_endianfun
64
65/* instantiate all the print functions we know about */
66#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
67#define vl_printfun
Dave Barach557d1282016-11-10 14:22:49 -050068#include <" plugin-name "/" plugin-name "_all_api_h.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050069#undef vl_printfun
70
71/* Get the API version number */
72#define vl_api_version(n,v) static u32 api_version=(v);
73#include <" plugin-name "/" plugin-name "_all_api_h.h>
74#undef vl_api_version
75
Dave Barach82842442018-10-31 09:54:34 -040076#define REPLY_MSG_ID_BASE " main-p "->msg_id_base
Dave Barach10440432018-04-03 14:58:14 -040077#include <vlibapi/api_helper_macros.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050078
Dave Barach3f2e7752018-10-03 16:10:04 -040079" plugin-name "_main_t " plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -050080
81/* List of message types that this plugin understands */
82
83#define foreach_" plugin-name "_plugin_api_msg \\
84_(" PLUGIN-NAME "_ENABLE_DISABLE, " plugin-name "_enable_disable)
85
Dave Barachb852bfa2016-01-04 15:27:42 -050086/* Action function shared between message handler and debug CLI */
87
Dave Barach82842442018-10-31 09:54:34 -040088int " plugin-name "_enable_disable (" plugin-name "_main_t * " main-p ", u32 sw_if_index,
Dave Barachb852bfa2016-01-04 15:27:42 -050089 int enable_disable)
90{
91 vnet_sw_interface_t * sw;
Dave Barachb7e2f3d2016-11-08 16:47:34 -050092 int rv = 0;
Dave Barachb852bfa2016-01-04 15:27:42 -050093
94 /* Utterly wrong? */
Dave Barach82842442018-10-31 09:54:34 -040095 if (pool_is_free_index (" main-p "->vnet_main->interface_main.sw_interfaces,
Dave Barachb852bfa2016-01-04 15:27:42 -050096 sw_if_index))
97 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
98
99 /* Not a physical port? */
Dave Barach82842442018-10-31 09:54:34 -0400100 sw = vnet_get_sw_interface (" main-p "->vnet_main, sw_if_index);
Dave Barachb852bfa2016-01-04 15:27:42 -0500101 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
102 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barach557d1282016-11-10 14:22:49 -0500103
Dave Barach11965c72019-05-28 16:31:05 -0400104 " plugin-name "_create_periodic_process (" main-p ");
105
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500106 vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\",
107 sw_if_index, enable_disable, 0, 0);
108
Dave Barachf4addbd2018-04-30 13:03:46 -0400109 /* Send an event to enable/disable the periodic scanner process */
Dave Barach11965c72019-05-28 16:31:05 -0400110 vlib_process_signal_event (" main-p "->vlib_main,
111 " main-p"->periodic_node_index,
112 " PLUGIN-NAME"_EVENT_PERIODIC_ENABLE_DISABLE,
Dave Barachf4addbd2018-04-30 13:03:46 -0400113 (uword)enable_disable);
Dave Barachb852bfa2016-01-04 15:27:42 -0500114 return rv;
115}
116
117static clib_error_t *
118" plugin-name "_enable_disable_command_fn (vlib_main_t * vm,
119 unformat_input_t * input,
120 vlib_cli_command_t * cmd)
121{
Dave Barach82842442018-10-31 09:54:34 -0400122 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500123 u32 sw_if_index = ~0;
124 int enable_disable = 1;
Dave Barach557d1282016-11-10 14:22:49 -0500125
Dave Barachb852bfa2016-01-04 15:27:42 -0500126 int rv;
127
Dave Barach35465dc2019-05-18 14:52:54 -0400128 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Dave Barachfed79e82017-02-10 11:57:46 -0500129 {
130 if (unformat (input, \"disable\"))
131 enable_disable = 0;
132 else if (unformat (input, \"%U\", unformat_vnet_sw_interface,
Dave Barach82842442018-10-31 09:54:34 -0400133 " main-p "->vnet_main, &sw_if_index))
Dave Barachfed79e82017-02-10 11:57:46 -0500134 ;
135 else
136 break;
Dave Barachb852bfa2016-01-04 15:27:42 -0500137 }
138
139 if (sw_if_index == ~0)
140 return clib_error_return (0, \"Please specify an interface...\");
Dave Barach557d1282016-11-10 14:22:49 -0500141
Dave Barach82842442018-10-31 09:54:34 -0400142 rv = " plugin-name "_enable_disable (" main-p ", sw_if_index, enable_disable);
Dave Barachb852bfa2016-01-04 15:27:42 -0500143
Dave Barach35465dc2019-05-18 14:52:54 -0400144 switch(rv)
Dave Barachfed79e82017-02-10 11:57:46 -0500145 {
Dave Barachb852bfa2016-01-04 15:27:42 -0500146 case 0:
147 break;
148
149 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
Dave Barach557d1282016-11-10 14:22:49 -0500150 return clib_error_return
Dave Barachb852bfa2016-01-04 15:27:42 -0500151 (0, \"Invalid interface, only works on physical ports\");
152 break;
153
154 case VNET_API_ERROR_UNIMPLEMENTED:
155 return clib_error_return (0, \"Device driver doesn't support redirection\");
156 break;
157
158 default:
159 return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\",
160 rv);
Dave Barachfed79e82017-02-10 11:57:46 -0500161 }
Dave Barachb852bfa2016-01-04 15:27:42 -0500162 return 0;
163}
164
Dave Barachfed79e82017-02-10 11:57:46 -0500165/* *INDENT-OFF* */
Dave Barach35465dc2019-05-18 14:52:54 -0400166VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) =
Dave Barachfed79e82017-02-10 11:57:46 -0500167{
168 .path = \"" plugin-name " enable-disable\",
169 .short_help =
170 \"" plugin-name " enable-disable <interface-name> [disable]\",
171 .function = " plugin-name "_enable_disable_command_fn,
Dave Barachb852bfa2016-01-04 15:27:42 -0500172};
Dave Barachfed79e82017-02-10 11:57:46 -0500173/* *INDENT-ON* */
Dave Barachb852bfa2016-01-04 15:27:42 -0500174
175/* API message handler */
176static void vl_api_" plugin-name "_enable_disable_t_handler
177(vl_api_" plugin-name "_enable_disable_t * mp)
178{
179 vl_api_" plugin-name "_enable_disable_reply_t * rmp;
Dave Barach82842442018-10-31 09:54:34 -0400180 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500181 int rv;
182
Dave Barach82842442018-10-31 09:54:34 -0400183 rv = " plugin-name "_enable_disable (" main-p ", ntohl(mp->sw_if_index),
Dave Barachb852bfa2016-01-04 15:27:42 -0500184 (int) (mp->enable_disable));
Dave Barach557d1282016-11-10 14:22:49 -0500185
Dave Barachb852bfa2016-01-04 15:27:42 -0500186 REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY);
187}
188
189/* Set up the API message handling tables */
190static clib_error_t *
191" plugin-name "_plugin_api_hookup (vlib_main_t *vm)
192{
Dave Barach82842442018-10-31 09:54:34 -0400193 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500194#define _(N,n) \\
Dave Barach82842442018-10-31 09:54:34 -0400195 vl_msg_api_set_handlers((VL_API_##N + " main-p "->msg_id_base), \\
Dave Barachb852bfa2016-01-04 15:27:42 -0500196 #n, \\
197 vl_api_##n##_t_handler, \\
198 vl_noop_handler, \\
199 vl_api_##n##_t_endian, \\
200 vl_api_##n##_t_print, \\
Dave Barach557d1282016-11-10 14:22:49 -0500201 sizeof(vl_api_##n##_t), 1);
Dave Barachb852bfa2016-01-04 15:27:42 -0500202 foreach_" plugin-name "_plugin_api_msg;
203#undef _
204
205 return 0;
206}
207
Dave Barach557d1282016-11-10 14:22:49 -0500208#define vl_msg_name_crc_list
209#include <" plugin-name "/" plugin-name "_all_api_h.h>
210#undef vl_msg_name_crc_list
211
212static void
Dave Barach82842442018-10-31 09:54:34 -0400213setup_message_id_table (" plugin-name "_main_t * " main-p ", api_main_t * am)
Dave Barach557d1282016-11-10 14:22:49 -0500214{
215#define _(id,n,crc) \
Dave Barach35465dc2019-05-18 14:52:54 -0400216 vl_msg_api_add_msg_name_crc (am, #n \"_\" #crc, id + " main-p "->msg_id_base);
Dave Barach557d1282016-11-10 14:22:49 -0500217 foreach_vl_msg_name_crc_" plugin-name" ;
218#undef _
219}
220
Dave Barachb852bfa2016-01-04 15:27:42 -0500221static clib_error_t * " plugin-name "_init (vlib_main_t * vm)
222{
Dave Barach82842442018-10-31 09:54:34 -0400223 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500224 clib_error_t * error = 0;
225 u8 * name;
226
Dave Barach82842442018-10-31 09:54:34 -0400227 " main-p "->vlib_main = vm;
228 " main-p "->vnet_main = vnet_get_main();
Dave Barach10440432018-04-03 14:58:14 -0400229
Dave Barachb852bfa2016-01-04 15:27:42 -0500230 name = format (0, \"" plugin-name "_%08x%c\", api_version, 0);
231
232 /* Ask for a correctly-sized block of API message decode slots */
Dave Barach82842442018-10-31 09:54:34 -0400233 " main-p "->msg_id_base = vl_msg_api_get_msg_ids
Dave Barachb852bfa2016-01-04 15:27:42 -0500234 ((char *) name, VL_MSG_FIRST_AVAILABLE);
235
236 error = " plugin-name "_plugin_api_hookup (vm);
237
Dave Barach605c6362017-01-02 12:22:48 -0500238 /* Add our API messages to the global name_crc hash table */
Dave Barach82842442018-10-31 09:54:34 -0400239 setup_message_id_table (" main-p ", &api_main);
Dave Barach605c6362017-01-02 12:22:48 -0500240
Dave Barachb852bfa2016-01-04 15:27:42 -0500241 vec_free(name);
242
243 return error;
244}
245
246VLIB_INIT_FUNCTION (" plugin-name "_init);
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500247
Dave Barachfed79e82017-02-10 11:57:46 -0500248/* *INDENT-OFF* */
Dave Barach557d1282016-11-10 14:22:49 -0500249VNET_FEATURE_INIT (" plugin-name ", static) =
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500250{
251 .arc_name = \"device-input\",
252 .node_name = \"" plugin-name "\",
253 .runs_before = VNET_FEATURES (\"ethernet-input\"),
254};
Dave Barachfed79e82017-02-10 11:57:46 -0500255/* *INDENT-ON */
256
257/* *INDENT-OFF* */
Dave Barach35465dc2019-05-18 14:52:54 -0400258VLIB_PLUGIN_REGISTER () =
Dave Barachfed79e82017-02-10 11:57:46 -0500259{
260 .version = VPP_BUILD_VER,
Dave Barach10440432018-04-03 14:58:14 -0400261 .description = \"" plugin-name " plugin description goes here\",
Dave Barachfed79e82017-02-10 11:57:46 -0500262};
263/* *INDENT-ON* */
264
265/*
266 * fd.io coding-style-patch-verification: " capital-oh-en "
267 *
268 * Local Variables:
269 * eval: (c-set-style \"gnu\")
270 * End:
271 */
Dave Barachb852bfa2016-01-04 15:27:42 -0500272")