1 ;;; plugin-main-skel.el - vpp engine plug-in "main.c" skeleton
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:
8 ;;; http://www.apache.org/licenses/LICENSE-2.0
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.
18 (define-skeleton skel-plugin-main
19 "Insert a plug-in 'main.c' skeleton "
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"))
27 * " plugin-name ".c - skeleton vpp engine plug-in
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:
34 * http://www.apache.org/licenses/LICENSE-2.0
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.
43 #include <vnet/vnet.h>
44 #include <vnet/plugin/plugin.h>
45 #include <" plugin-name "/" plugin-name ".h>
47 #include <vlibapi/api.h>
48 #include <vlibmemory/api.h>
49 #include <vpp/app/version.h>
52 /* define message IDs */
53 #include <" plugin-name "/" plugin-name "_msg_enum.h>
55 /* define message structures */
57 #include <" plugin-name "/" plugin-name "_all_api_h.h>
60 /* define generated endian-swappers */
62 #include <" plugin-name "/" plugin-name "_all_api_h.h>
65 /* instantiate all the print functions we know about */
66 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
68 #include <" plugin-name "/" plugin-name "_all_api_h.h>
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>
76 #define REPLY_MSG_ID_BASE " main-p "->msg_id_base
77 #include <vlibapi/api_helper_macros.h>
79 " plugin-name "_main_t " plugin-name "_main;
81 /* List of message types that this plugin understands */
83 #define foreach_" plugin-name "_plugin_api_msg \\
84 _(" PLUGIN-NAME "_ENABLE_DISABLE, " plugin-name "_enable_disable)
86 /* Action function shared between message handler and debug CLI */
88 int " plugin-name "_enable_disable (" plugin-name "_main_t * " main-p ", u32 sw_if_index,
91 vnet_sw_interface_t * sw;
95 if (pool_is_free_index (" main-p "->vnet_main->interface_main.sw_interfaces,
97 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
99 /* Not a physical port? */
100 sw = vnet_get_sw_interface (" main-p "->vnet_main, sw_if_index);
101 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
102 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
104 " plugin-name "_create_periodic_process (" main-p ");
106 vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\",
107 sw_if_index, enable_disable, 0, 0);
109 /* Send an event to enable/disable the periodic scanner process */
110 vlib_process_signal_event (" main-p "->vlib_main,
111 " main-p"->periodic_node_index,
112 " PLUGIN-NAME"_EVENT_PERIODIC_ENABLE_DISABLE,
113 (uword)enable_disable);
117 static 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)
122 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
123 u32 sw_if_index = ~0;
124 int enable_disable = 1;
128 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
130 if (unformat (input, \"disable\"))
132 else if (unformat (input, \"%U\", unformat_vnet_sw_interface,
133 " main-p "->vnet_main, &sw_if_index))
139 if (sw_if_index == ~0)
140 return clib_error_return (0, \"Please specify an interface...\");
142 rv = " plugin-name "_enable_disable (" main-p ", sw_if_index, enable_disable);
149 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
150 return clib_error_return
151 (0, \"Invalid interface, only works on physical ports\");
154 case VNET_API_ERROR_UNIMPLEMENTED:
155 return clib_error_return (0, \"Device driver doesn't support redirection\");
159 return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\",
166 VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) =
168 .path = \"" plugin-name " enable-disable\",
170 \"" plugin-name " enable-disable <interface-name> [disable]\",
171 .function = " plugin-name "_enable_disable_command_fn,
175 /* API message handler */
176 static void vl_api_" plugin-name "_enable_disable_t_handler
177 (vl_api_" plugin-name "_enable_disable_t * mp)
179 vl_api_" plugin-name "_enable_disable_reply_t * rmp;
180 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
183 rv = " plugin-name "_enable_disable (" main-p ", ntohl(mp->sw_if_index),
184 (int) (mp->enable_disable));
186 REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY);
189 /* Set up the API message handling tables */
190 static clib_error_t *
191 " plugin-name "_plugin_api_hookup (vlib_main_t *vm)
193 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
195 vl_msg_api_set_handlers((VL_API_##N + " main-p "->msg_id_base), \\
197 vl_api_##n##_t_handler, \\
199 vl_api_##n##_t_endian, \\
200 vl_api_##n##_t_print, \\
201 sizeof(vl_api_##n##_t), 1);
202 foreach_" plugin-name "_plugin_api_msg;
208 #define vl_msg_name_crc_list
209 #include <" plugin-name "/" plugin-name "_all_api_h.h>
210 #undef vl_msg_name_crc_list
213 setup_message_id_table (" plugin-name "_main_t * " main-p ", api_main_t * am)
215 #define _(id,n,crc) \
216 vl_msg_api_add_msg_name_crc (am, #n \"_\" #crc, id + " main-p "->msg_id_base);
217 foreach_vl_msg_name_crc_" plugin-name" ;
221 static clib_error_t * " plugin-name "_init (vlib_main_t * vm)
223 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
224 clib_error_t * error = 0;
227 " main-p "->vlib_main = vm;
228 " main-p "->vnet_main = vnet_get_main();
230 name = format (0, \"" plugin-name "_%08x%c\", api_version, 0);
232 /* Ask for a correctly-sized block of API message decode slots */
233 " main-p "->msg_id_base = vl_msg_api_get_msg_ids
234 ((char *) name, VL_MSG_FIRST_AVAILABLE);
236 error = " plugin-name "_plugin_api_hookup (vm);
238 /* Add our API messages to the global name_crc hash table */
239 setup_message_id_table (" main-p ", &api_main);
246 VLIB_INIT_FUNCTION (" plugin-name "_init);
249 VNET_FEATURE_INIT (" plugin-name ", static) =
251 .arc_name = \"device-input\",
252 .node_name = \"" plugin-name "\",
253 .runs_before = VNET_FEATURES (\"ethernet-input\"),
258 VLIB_PLUGIN_REGISTER () =
260 .version = VPP_BUILD_VER,
261 .description = \"" plugin-name " plugin description goes here\",
266 * fd.io coding-style-patch-verification: " capital-oh-en "
269 * eval: (c-set-style \"gnu\")