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))
26 * " plugin-name ".c - skeleton vpp engine plug-in
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:
33 * http://www.apache.org/licenses/LICENSE-2.0
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.
42 #include <vnet/vnet.h>
43 #include <vnet/plugin/plugin.h>
44 #include <" plugin-name "/" plugin-name ".h>
46 #include <vlibapi/api.h>
47 #include <vlibmemory/api.h>
48 #include <vlibsocket/api.h>
50 /* define message IDs */
51 #include <" plugin-name "/" plugin-name "_msg_enum.h>
53 /* define message structures */
55 #include <" plugin-name "/" plugin-name "_all_api_h.h>
58 /* define generated endian-swappers */
60 #include <" plugin-name "/" plugin-name "_all_api_h.h>
63 /* instantiate all the print functions we know about */
64 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
66 #include <" plugin-name "/" plugin-name "_all_api_h.h>
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>
75 * 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
82 #define REPLY_MACRO(t) \\
84 unix_shared_memory_queue_t * q = \\
85 vl_api_client_index_to_input_queue (mp->client_index); \\
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); \\
94 vl_msg_api_send_shmem (q, (u8 *)&rmp); \\
98 /* List of message types that this plugin understands */
100 #define foreach_" plugin-name "_plugin_api_msg \\
101 _(" PLUGIN-NAME "_ENABLE_DISABLE, " plugin-name "_enable_disable)
104 * This routine exists to convince the vlib plugin framework that
105 * we haven't accidentally copied a random .dll into the plugin directory.
107 * Also collects global variable pointers passed from the vpp engine
111 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
114 " plugin-name "_main_t * sm = &" plugin-name "_main;
115 clib_error_t * error = 0;
118 sm->vnet_main = h->vnet_main;
119 sm->ethernet_main = h->ethernet_main;
124 /* Action function shared between message handler and debug CLI */
126 int " plugin-name "_enable_disable (" plugin-name "_main_t * sm, u32 sw_if_index,
129 vnet_sw_interface_t * sw;
131 u32 node_index = enable_disable ? " plugin-name "_node.index : ~0;
134 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
136 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
138 /* Not a physical port? */
139 sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
140 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
141 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
144 * Redirect pkts from the driver to the macswap node.
145 * Returns VNET_API_ERROR_UNIMPLEMENTED if the h/w driver
146 * doesn't implement the API.
148 * Node_index = ~0 => shut off redirection
150 rv = vnet_hw_interface_rx_redirect_to_node (sm->vnet_main, sw_if_index,
155 static clib_error_t *
156 " plugin-name "_enable_disable_command_fn (vlib_main_t * vm,
157 unformat_input_t * input,
158 vlib_cli_command_t * cmd)
160 " plugin-name "_main_t * sm = &" plugin-name "_main;
161 u32 sw_if_index = ~0;
162 int enable_disable = 1;
166 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
167 if (unformat (input, \"disable\"))
169 else if (unformat (input, \"%U\", unformat_vnet_sw_interface,
170 sm->vnet_main, &sw_if_index))
176 if (sw_if_index == ~0)
177 return clib_error_return (0, \"Please specify an interface...\");
179 rv = " plugin-name "_enable_disable (sm, sw_if_index, enable_disable);
185 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
186 return clib_error_return
187 (0, \"Invalid interface, only works on physical ports\");
190 case VNET_API_ERROR_UNIMPLEMENTED:
191 return clib_error_return (0, \"Device driver doesn't support redirection\");
195 return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\",
201 VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) = {
202 .path = \"" plugin-name " enable-disable\",
204 \"" plugin-name " enable-disable <interface-name> [disable]\",
205 .function = " plugin-name "_enable_disable_command_fn,
208 /* API message handler */
209 static void vl_api_" plugin-name "_enable_disable_t_handler
210 (vl_api_" plugin-name "_enable_disable_t * mp)
212 vl_api_" plugin-name "_enable_disable_reply_t * rmp;
213 " plugin-name "_main_t * sm = &" plugin-name "_main;
216 rv = " plugin-name "_enable_disable (sm, ntohl(mp->sw_if_index),
217 (int) (mp->enable_disable));
219 REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY);
222 /* Set up the API message handling tables */
223 static clib_error_t *
224 " plugin-name "_plugin_api_hookup (vlib_main_t *vm)
226 " plugin-name "_main_t * sm = &" plugin-name "_main;
228 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \\
230 vl_api_##n##_t_handler, \\
232 vl_api_##n##_t_endian, \\
233 vl_api_##n##_t_print, \\
234 sizeof(vl_api_##n##_t), 1);
235 foreach_" plugin-name "_plugin_api_msg;
241 static clib_error_t * " plugin-name "_init (vlib_main_t * vm)
243 " plugin-name "_main_t * sm = &" plugin-name "_main;
244 clib_error_t * error = 0;
247 name = format (0, \"" plugin-name "_%08x%c\", api_version, 0);
249 /* Ask for a correctly-sized block of API message decode slots */
250 sm->msg_id_base = vl_msg_api_get_msg_ids
251 ((char *) name, VL_MSG_FIRST_AVAILABLE);
253 error = " plugin-name "_plugin_api_hookup (vm);
260 VLIB_INIT_FUNCTION (" plugin-name "_init);