blob: 14606c639c56e20d0a4240cecb4060d74e11fd39 [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 Barachb852bfa2016-01-04 15:27:42 -050050
51/* define message IDs */
52#include <" plugin-name "/" plugin-name "_msg_enum.h>
53
54/* define message structures */
55#define vl_typedefs
Dave Barach557d1282016-11-10 14:22:49 -050056#include <" plugin-name "/" plugin-name "_all_api_h.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050057#undef vl_typedefs
58
59/* define generated endian-swappers */
60#define vl_endianfun
Dave Barach557d1282016-11-10 14:22:49 -050061#include <" plugin-name "/" plugin-name "_all_api_h.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050062#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
Dave Barach557d1282016-11-10 14:22:49 -050067#include <" plugin-name "/" plugin-name "_all_api_h.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050068#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
Dave Barach82842442018-10-31 09:54:34 -040075#define REPLY_MSG_ID_BASE " main-p "->msg_id_base
Dave Barach10440432018-04-03 14:58:14 -040076#include <vlibapi/api_helper_macros.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050077
Dave Barach3f2e7752018-10-03 16:10:04 -040078" plugin-name "_main_t " plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -050079
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
Dave Barachb852bfa2016-01-04 15:27:42 -050085/* Action function shared between message handler and debug CLI */
86
Dave Barach82842442018-10-31 09:54:34 -040087int " plugin-name "_enable_disable (" plugin-name "_main_t * " main-p ", u32 sw_if_index,
Dave Barachb852bfa2016-01-04 15:27:42 -050088 int enable_disable)
89{
90 vnet_sw_interface_t * sw;
Dave Barachb7e2f3d2016-11-08 16:47:34 -050091 int rv = 0;
Dave Barachb852bfa2016-01-04 15:27:42 -050092
93 /* Utterly wrong? */
Dave Barach82842442018-10-31 09:54:34 -040094 if (pool_is_free_index (" main-p "->vnet_main->interface_main.sw_interfaces,
Dave Barachb852bfa2016-01-04 15:27:42 -050095 sw_if_index))
96 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
97
98 /* Not a physical port? */
Dave Barach82842442018-10-31 09:54:34 -040099 sw = vnet_get_sw_interface (" main-p "->vnet_main, sw_if_index);
Dave Barachb852bfa2016-01-04 15:27:42 -0500100 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
101 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barach557d1282016-11-10 14:22:49 -0500102
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500103 vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\",
104 sw_if_index, enable_disable, 0, 0);
105
Dave Barachf4addbd2018-04-30 13:03:46 -0400106 /* Send an event to enable/disable the periodic scanner process */
Dave Barach82842442018-10-31 09:54:34 -0400107 vlib_process_signal_event (" main-p "->vlib_main, " plugin-name"_periodic_node.index,
Dave Barachf4addbd2018-04-30 13:03:46 -0400108 " PLUGIN-NAME"_EVENT_PERIODIC_ENABLE_DISABLE,
109 (uword)enable_disable);
110
Dave Barachb852bfa2016-01-04 15:27:42 -0500111 return rv;
112}
113
114static clib_error_t *
115" plugin-name "_enable_disable_command_fn (vlib_main_t * vm,
116 unformat_input_t * input,
117 vlib_cli_command_t * cmd)
118{
Dave Barach82842442018-10-31 09:54:34 -0400119 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500120 u32 sw_if_index = ~0;
121 int enable_disable = 1;
Dave Barach557d1282016-11-10 14:22:49 -0500122
Dave Barachb852bfa2016-01-04 15:27:42 -0500123 int rv;
124
Dave Barachfed79e82017-02-10 11:57:46 -0500125 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
126 {
127 if (unformat (input, \"disable\"))
128 enable_disable = 0;
129 else if (unformat (input, \"%U\", unformat_vnet_sw_interface,
Dave Barach82842442018-10-31 09:54:34 -0400130 " main-p "->vnet_main, &sw_if_index))
Dave Barachfed79e82017-02-10 11:57:46 -0500131 ;
132 else
133 break;
Dave Barachb852bfa2016-01-04 15:27:42 -0500134 }
135
136 if (sw_if_index == ~0)
137 return clib_error_return (0, \"Please specify an interface...\");
Dave Barach557d1282016-11-10 14:22:49 -0500138
Dave Barach82842442018-10-31 09:54:34 -0400139 rv = " plugin-name "_enable_disable (" main-p ", sw_if_index, enable_disable);
Dave Barachb852bfa2016-01-04 15:27:42 -0500140
Dave Barachfed79e82017-02-10 11:57:46 -0500141 switch(rv)
142 {
Dave Barachb852bfa2016-01-04 15:27:42 -0500143 case 0:
144 break;
145
146 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
Dave Barach557d1282016-11-10 14:22:49 -0500147 return clib_error_return
Dave Barachb852bfa2016-01-04 15:27:42 -0500148 (0, \"Invalid interface, only works on physical ports\");
149 break;
150
151 case VNET_API_ERROR_UNIMPLEMENTED:
152 return clib_error_return (0, \"Device driver doesn't support redirection\");
153 break;
154
155 default:
156 return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\",
157 rv);
Dave Barachfed79e82017-02-10 11:57:46 -0500158 }
Dave Barachb852bfa2016-01-04 15:27:42 -0500159 return 0;
160}
161
Dave Barachfed79e82017-02-10 11:57:46 -0500162/* *INDENT-OFF* */
163VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) =
164{
165 .path = \"" plugin-name " enable-disable\",
166 .short_help =
167 \"" plugin-name " enable-disable <interface-name> [disable]\",
168 .function = " plugin-name "_enable_disable_command_fn,
Dave Barachb852bfa2016-01-04 15:27:42 -0500169};
Dave Barachfed79e82017-02-10 11:57:46 -0500170/* *INDENT-ON* */
Dave Barachb852bfa2016-01-04 15:27:42 -0500171
172/* API message handler */
173static void vl_api_" plugin-name "_enable_disable_t_handler
174(vl_api_" plugin-name "_enable_disable_t * mp)
175{
176 vl_api_" plugin-name "_enable_disable_reply_t * rmp;
Dave Barach82842442018-10-31 09:54:34 -0400177 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500178 int rv;
179
Dave Barach82842442018-10-31 09:54:34 -0400180 rv = " plugin-name "_enable_disable (" main-p ", ntohl(mp->sw_if_index),
Dave Barachb852bfa2016-01-04 15:27:42 -0500181 (int) (mp->enable_disable));
Dave Barach557d1282016-11-10 14:22:49 -0500182
Dave Barachb852bfa2016-01-04 15:27:42 -0500183 REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY);
184}
185
186/* Set up the API message handling tables */
187static clib_error_t *
188" plugin-name "_plugin_api_hookup (vlib_main_t *vm)
189{
Dave Barach82842442018-10-31 09:54:34 -0400190 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500191#define _(N,n) \\
Dave Barach82842442018-10-31 09:54:34 -0400192 vl_msg_api_set_handlers((VL_API_##N + " main-p "->msg_id_base), \\
Dave Barachb852bfa2016-01-04 15:27:42 -0500193 #n, \\
194 vl_api_##n##_t_handler, \\
195 vl_noop_handler, \\
196 vl_api_##n##_t_endian, \\
197 vl_api_##n##_t_print, \\
Dave Barach557d1282016-11-10 14:22:49 -0500198 sizeof(vl_api_##n##_t), 1);
Dave Barachb852bfa2016-01-04 15:27:42 -0500199 foreach_" plugin-name "_plugin_api_msg;
200#undef _
201
202 return 0;
203}
204
Dave Barach557d1282016-11-10 14:22:49 -0500205#define vl_msg_name_crc_list
206#include <" plugin-name "/" plugin-name "_all_api_h.h>
207#undef vl_msg_name_crc_list
208
209static void
Dave Barach82842442018-10-31 09:54:34 -0400210setup_message_id_table (" plugin-name "_main_t * " main-p ", api_main_t * am)
Dave Barach557d1282016-11-10 14:22:49 -0500211{
212#define _(id,n,crc) \
Dave Barach82842442018-10-31 09:54:34 -0400213 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + " main-p "->msg_id_base);
Dave Barach557d1282016-11-10 14:22:49 -0500214 foreach_vl_msg_name_crc_" plugin-name" ;
215#undef _
216}
217
Dave Barachb852bfa2016-01-04 15:27:42 -0500218static clib_error_t * " plugin-name "_init (vlib_main_t * vm)
219{
Dave Barach82842442018-10-31 09:54:34 -0400220 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500221 clib_error_t * error = 0;
222 u8 * name;
223
Dave Barach82842442018-10-31 09:54:34 -0400224 " main-p "->vlib_main = vm;
225 " main-p "->vnet_main = vnet_get_main();
Dave Barach10440432018-04-03 14:58:14 -0400226
Dave Barachb852bfa2016-01-04 15:27:42 -0500227 name = format (0, \"" plugin-name "_%08x%c\", api_version, 0);
228
229 /* Ask for a correctly-sized block of API message decode slots */
Dave Barach82842442018-10-31 09:54:34 -0400230 " main-p "->msg_id_base = vl_msg_api_get_msg_ids
Dave Barachb852bfa2016-01-04 15:27:42 -0500231 ((char *) name, VL_MSG_FIRST_AVAILABLE);
232
233 error = " plugin-name "_plugin_api_hookup (vm);
234
Dave Barach605c6362017-01-02 12:22:48 -0500235 /* Add our API messages to the global name_crc hash table */
Dave Barach82842442018-10-31 09:54:34 -0400236 setup_message_id_table (" main-p ", &api_main);
Dave Barach605c6362017-01-02 12:22:48 -0500237
Dave Barachb852bfa2016-01-04 15:27:42 -0500238 vec_free(name);
239
240 return error;
241}
242
243VLIB_INIT_FUNCTION (" plugin-name "_init);
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500244
Dave Barachfed79e82017-02-10 11:57:46 -0500245/* *INDENT-OFF* */
Dave Barach557d1282016-11-10 14:22:49 -0500246VNET_FEATURE_INIT (" plugin-name ", static) =
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500247{
248 .arc_name = \"device-input\",
249 .node_name = \"" plugin-name "\",
250 .runs_before = VNET_FEATURES (\"ethernet-input\"),
251};
Dave Barachfed79e82017-02-10 11:57:46 -0500252/* *INDENT-ON */
253
254/* *INDENT-OFF* */
255VLIB_PLUGIN_REGISTER () =
256{
257 .version = VPP_BUILD_VER,
Dave Barach10440432018-04-03 14:58:14 -0400258 .description = \"" plugin-name " plugin description goes here\",
Dave Barachfed79e82017-02-10 11:57:46 -0500259};
260/* *INDENT-ON* */
261
262/*
263 * fd.io coding-style-patch-verification: " capital-oh-en "
264 *
265 * Local Variables:
266 * eval: (c-set-style \"gnu\")
267 * End:
268 */
Dave Barachb852bfa2016-01-04 15:27:42 -0500269")
270