blob: db1147e75e5035ba724f6c71836ce40475580824 [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")
25"/*
Dave Barach557d1282016-11-10 14:22:49 -050026 * " plugin-name ".c - skeleton vpp engine plug-in
Dave Barachb852bfa2016-01-04 15:27:42 -050027 *
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>
Dave Barachfed79e82017-02-10 11:57:46 -050048#include <vpp/app/version.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050049
50/* define message IDs */
51#include <" plugin-name "/" plugin-name "_msg_enum.h>
52
53/* define message structures */
54#define vl_typedefs
Dave Barach557d1282016-11-10 14:22:49 -050055#include <" plugin-name "/" plugin-name "_all_api_h.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050056#undef vl_typedefs
57
58/* define generated endian-swappers */
59#define vl_endianfun
Dave Barach557d1282016-11-10 14:22:49 -050060#include <" plugin-name "/" plugin-name "_all_api_h.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050061#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
Dave Barach557d1282016-11-10 14:22:49 -050066#include <" plugin-name "/" plugin-name "_all_api_h.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050067#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
Dave Barach557d1282016-11-10 14:22:49 -050074/*
Dave Barachb852bfa2016-01-04 15:27:42 -050075 * A handy macro to set up a message reply.
76 * Assumes that the following variables are available:
77 * mp - pointer to request message
78 * rmp - pointer to reply message type
79 * rv - return value
80 */
81
82#define REPLY_MACRO(t) \\
83do { \\
84 unix_shared_memory_queue_t * q = \\
85 vl_api_client_index_to_input_queue (mp->client_index); \\
86 if (!q) \\
87 return; \\
88 \\
89 rmp = vl_msg_api_alloc (sizeof (*rmp)); \\
90 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \\
91 rmp->context = mp->context; \\
92 rmp->retval = ntohl(rv); \\
93 \\
94 vl_msg_api_send_shmem (q, (u8 *)&rmp); \\
95} while(0);
96
97
98/* List of message types that this plugin understands */
99
100#define foreach_" plugin-name "_plugin_api_msg \\
101_(" PLUGIN-NAME "_ENABLE_DISABLE, " plugin-name "_enable_disable)
102
Dave Barachb852bfa2016-01-04 15:27:42 -0500103/* Action function shared between message handler and debug CLI */
104
105int " plugin-name "_enable_disable (" plugin-name "_main_t * sm, u32 sw_if_index,
106 int enable_disable)
107{
108 vnet_sw_interface_t * sw;
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500109 int rv = 0;
Dave Barachb852bfa2016-01-04 15:27:42 -0500110
111 /* Utterly wrong? */
Dave Barach557d1282016-11-10 14:22:49 -0500112 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
Dave Barachb852bfa2016-01-04 15:27:42 -0500113 sw_if_index))
114 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
115
116 /* Not a physical port? */
117 sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
118 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
119 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barach557d1282016-11-10 14:22:49 -0500120
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500121 vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\",
122 sw_if_index, enable_disable, 0, 0);
123
Dave Barachb852bfa2016-01-04 15:27:42 -0500124 return rv;
125}
126
127static clib_error_t *
128" plugin-name "_enable_disable_command_fn (vlib_main_t * vm,
129 unformat_input_t * input,
130 vlib_cli_command_t * cmd)
131{
132 " plugin-name "_main_t * sm = &" plugin-name "_main;
133 u32 sw_if_index = ~0;
134 int enable_disable = 1;
Dave Barach557d1282016-11-10 14:22:49 -0500135
Dave Barachb852bfa2016-01-04 15:27:42 -0500136 int rv;
137
Dave Barachfed79e82017-02-10 11:57:46 -0500138 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
139 {
140 if (unformat (input, \"disable\"))
141 enable_disable = 0;
142 else if (unformat (input, \"%U\", unformat_vnet_sw_interface,
143 sm->vnet_main, &sw_if_index))
144 ;
145 else
146 break;
Dave Barachb852bfa2016-01-04 15:27:42 -0500147 }
148
149 if (sw_if_index == ~0)
150 return clib_error_return (0, \"Please specify an interface...\");
Dave Barach557d1282016-11-10 14:22:49 -0500151
Dave Barachb852bfa2016-01-04 15:27:42 -0500152 rv = " plugin-name "_enable_disable (sm, sw_if_index, enable_disable);
153
Dave Barachfed79e82017-02-10 11:57:46 -0500154 switch(rv)
155 {
Dave Barachb852bfa2016-01-04 15:27:42 -0500156 case 0:
157 break;
158
159 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
Dave Barach557d1282016-11-10 14:22:49 -0500160 return clib_error_return
Dave Barachb852bfa2016-01-04 15:27:42 -0500161 (0, \"Invalid interface, only works on physical ports\");
162 break;
163
164 case VNET_API_ERROR_UNIMPLEMENTED:
165 return clib_error_return (0, \"Device driver doesn't support redirection\");
166 break;
167
168 default:
169 return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\",
170 rv);
Dave Barachfed79e82017-02-10 11:57:46 -0500171 }
Dave Barachb852bfa2016-01-04 15:27:42 -0500172 return 0;
173}
174
Dave Barachfed79e82017-02-10 11:57:46 -0500175/* *INDENT-OFF* */
176VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) =
177{
178 .path = \"" plugin-name " enable-disable\",
179 .short_help =
180 \"" plugin-name " enable-disable <interface-name> [disable]\",
181 .function = " plugin-name "_enable_disable_command_fn,
Dave Barachb852bfa2016-01-04 15:27:42 -0500182};
Dave Barachfed79e82017-02-10 11:57:46 -0500183/* *INDENT-ON* */
Dave Barachb852bfa2016-01-04 15:27:42 -0500184
185/* API message handler */
186static void vl_api_" plugin-name "_enable_disable_t_handler
187(vl_api_" plugin-name "_enable_disable_t * mp)
188{
189 vl_api_" plugin-name "_enable_disable_reply_t * rmp;
190 " plugin-name "_main_t * sm = &" plugin-name "_main;
191 int rv;
192
Dave Barach557d1282016-11-10 14:22:49 -0500193 rv = " plugin-name "_enable_disable (sm, ntohl(mp->sw_if_index),
Dave Barachb852bfa2016-01-04 15:27:42 -0500194 (int) (mp->enable_disable));
Dave Barach557d1282016-11-10 14:22:49 -0500195
Dave Barachb852bfa2016-01-04 15:27:42 -0500196 REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY);
197}
198
199/* Set up the API message handling tables */
200static clib_error_t *
201" plugin-name "_plugin_api_hookup (vlib_main_t *vm)
202{
203 " plugin-name "_main_t * sm = &" plugin-name "_main;
204#define _(N,n) \\
205 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \\
206 #n, \\
207 vl_api_##n##_t_handler, \\
208 vl_noop_handler, \\
209 vl_api_##n##_t_endian, \\
210 vl_api_##n##_t_print, \\
Dave Barach557d1282016-11-10 14:22:49 -0500211 sizeof(vl_api_##n##_t), 1);
Dave Barachb852bfa2016-01-04 15:27:42 -0500212 foreach_" plugin-name "_plugin_api_msg;
213#undef _
214
215 return 0;
216}
217
Dave Barach557d1282016-11-10 14:22:49 -0500218#define vl_msg_name_crc_list
219#include <" plugin-name "/" plugin-name "_all_api_h.h>
220#undef vl_msg_name_crc_list
221
222static void
223setup_message_id_table (" plugin-name "_main_t * sm, api_main_t * am)
224{
225#define _(id,n,crc) \
226 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
227 foreach_vl_msg_name_crc_" plugin-name" ;
228#undef _
229}
230
Dave Barachb852bfa2016-01-04 15:27:42 -0500231static clib_error_t * " plugin-name "_init (vlib_main_t * vm)
232{
233 " plugin-name "_main_t * sm = &" plugin-name "_main;
234 clib_error_t * error = 0;
235 u8 * name;
236
237 name = format (0, \"" plugin-name "_%08x%c\", api_version, 0);
238
239 /* Ask for a correctly-sized block of API message decode slots */
Dave Barach557d1282016-11-10 14:22:49 -0500240 sm->msg_id_base = vl_msg_api_get_msg_ids
Dave Barachb852bfa2016-01-04 15:27:42 -0500241 ((char *) name, VL_MSG_FIRST_AVAILABLE);
242
243 error = " plugin-name "_plugin_api_hookup (vm);
244
Dave Barach605c6362017-01-02 12:22:48 -0500245 /* Add our API messages to the global name_crc hash table */
246 setup_message_id_table (sm, &api_main);
247
Dave Barachb852bfa2016-01-04 15:27:42 -0500248 vec_free(name);
249
250 return error;
251}
252
253VLIB_INIT_FUNCTION (" plugin-name "_init);
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500254
Dave Barachfed79e82017-02-10 11:57:46 -0500255/* *INDENT-OFF* */
Dave Barach557d1282016-11-10 14:22:49 -0500256VNET_FEATURE_INIT (" plugin-name ", static) =
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500257{
258 .arc_name = \"device-input\",
259 .node_name = \"" plugin-name "\",
260 .runs_before = VNET_FEATURES (\"ethernet-input\"),
261};
Dave Barachfed79e82017-02-10 11:57:46 -0500262/* *INDENT-ON */
263
264/* *INDENT-OFF* */
265VLIB_PLUGIN_REGISTER () =
266{
267 .version = VPP_BUILD_VER,
268};
269/* *INDENT-ON* */
270
271/*
272 * fd.io coding-style-patch-verification: " capital-oh-en "
273 *
274 * Local Variables:
275 * eval: (c-set-style \"gnu\")
276 * End:
277 */
Dave Barachb852bfa2016-01-04 15:27:42 -0500278")
279