Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 1 | ;;; 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) | ca46d8c | 2016-03-18 07:22:15 -0700 | [diff] [blame] | 18 | (define-skeleton skel-plugin-main |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 19 | "Insert a plug-in 'main.c' skeleton " |
| 20 | nil |
| 21 | '(if (not (boundp 'plugin-name)) |
| 22 | (setq plugin-name (read-string "Plugin name: "))) |
| 23 | '(setq PLUGIN-NAME (upcase plugin-name)) |
| 24 | " |
| 25 | /* |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 26 | * " plugin-name ".c - skeleton vpp engine plug-in |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 27 | * |
| 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> |
| 48 | #include <vlibsocket/api.h> |
| 49 | |
| 50 | /* define message IDs */ |
| 51 | #include <" plugin-name "/" plugin-name "_msg_enum.h> |
| 52 | |
| 53 | /* define message structures */ |
| 54 | #define vl_typedefs |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 55 | #include <" plugin-name "/" plugin-name "_all_api_h.h> |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 56 | #undef vl_typedefs |
| 57 | |
| 58 | /* define generated endian-swappers */ |
| 59 | #define vl_endianfun |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 60 | #include <" plugin-name "/" plugin-name "_all_api_h.h> |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 61 | #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 Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 66 | #include <" plugin-name "/" plugin-name "_all_api_h.h> |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 67 | #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 Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 74 | /* |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 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 |
| 79 | * rv - return value |
| 80 | */ |
| 81 | |
| 82 | #define REPLY_MACRO(t) \\ |
| 83 | do { \\ |
| 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 Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 103 | /* |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 104 | * This routine exists to convince the vlib plugin framework that |
| 105 | * we haven't accidentally copied a random .dll into the plugin directory. |
| 106 | * |
| 107 | * Also collects global variable pointers passed from the vpp engine |
| 108 | */ |
| 109 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 110 | clib_error_t * |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 111 | vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h, |
| 112 | int from_early_init) |
| 113 | { |
| 114 | " plugin-name "_main_t * sm = &" plugin-name "_main; |
| 115 | clib_error_t * error = 0; |
| 116 | |
| 117 | sm->vlib_main = vm; |
| 118 | sm->vnet_main = h->vnet_main; |
| 119 | sm->ethernet_main = h->ethernet_main; |
| 120 | |
| 121 | return error; |
| 122 | } |
| 123 | |
| 124 | /* Action function shared between message handler and debug CLI */ |
| 125 | |
| 126 | int " plugin-name "_enable_disable (" plugin-name "_main_t * sm, u32 sw_if_index, |
| 127 | int enable_disable) |
| 128 | { |
| 129 | vnet_sw_interface_t * sw; |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 130 | int rv = 0; |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 131 | |
| 132 | /* Utterly wrong? */ |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 133 | if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces, |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 134 | sw_if_index)) |
| 135 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 136 | |
| 137 | /* Not a physical port? */ |
| 138 | sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index); |
| 139 | if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 140 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 141 | |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 142 | vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\", |
| 143 | sw_if_index, enable_disable, 0, 0); |
| 144 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 145 | return rv; |
| 146 | } |
| 147 | |
| 148 | static clib_error_t * |
| 149 | " plugin-name "_enable_disable_command_fn (vlib_main_t * vm, |
| 150 | unformat_input_t * input, |
| 151 | vlib_cli_command_t * cmd) |
| 152 | { |
| 153 | " plugin-name "_main_t * sm = &" plugin-name "_main; |
| 154 | u32 sw_if_index = ~0; |
| 155 | int enable_disable = 1; |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 156 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 157 | int rv; |
| 158 | |
| 159 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { |
| 160 | if (unformat (input, \"disable\")) |
| 161 | enable_disable = 0; |
| 162 | else if (unformat (input, \"%U\", unformat_vnet_sw_interface, |
| 163 | sm->vnet_main, &sw_if_index)) |
| 164 | ; |
| 165 | else |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | if (sw_if_index == ~0) |
| 170 | return clib_error_return (0, \"Please specify an interface...\"); |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 171 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 172 | rv = " plugin-name "_enable_disable (sm, sw_if_index, enable_disable); |
| 173 | |
| 174 | switch(rv) { |
| 175 | case 0: |
| 176 | break; |
| 177 | |
| 178 | case VNET_API_ERROR_INVALID_SW_IF_INDEX: |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 179 | return clib_error_return |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 180 | (0, \"Invalid interface, only works on physical ports\"); |
| 181 | break; |
| 182 | |
| 183 | case VNET_API_ERROR_UNIMPLEMENTED: |
| 184 | return clib_error_return (0, \"Device driver doesn't support redirection\"); |
| 185 | break; |
| 186 | |
| 187 | default: |
| 188 | return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\", |
| 189 | rv); |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) = { |
| 195 | .path = \"" plugin-name " enable-disable\", |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 196 | .short_help = |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 197 | \"" plugin-name " enable-disable <interface-name> [disable]\", |
| 198 | .function = " plugin-name "_enable_disable_command_fn, |
| 199 | }; |
| 200 | |
| 201 | /* API message handler */ |
| 202 | static void vl_api_" plugin-name "_enable_disable_t_handler |
| 203 | (vl_api_" plugin-name "_enable_disable_t * mp) |
| 204 | { |
| 205 | vl_api_" plugin-name "_enable_disable_reply_t * rmp; |
| 206 | " plugin-name "_main_t * sm = &" plugin-name "_main; |
| 207 | int rv; |
| 208 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 209 | rv = " plugin-name "_enable_disable (sm, ntohl(mp->sw_if_index), |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 210 | (int) (mp->enable_disable)); |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 211 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 212 | REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY); |
| 213 | } |
| 214 | |
| 215 | /* Set up the API message handling tables */ |
| 216 | static clib_error_t * |
| 217 | " plugin-name "_plugin_api_hookup (vlib_main_t *vm) |
| 218 | { |
| 219 | " plugin-name "_main_t * sm = &" plugin-name "_main; |
| 220 | #define _(N,n) \\ |
| 221 | vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \\ |
| 222 | #n, \\ |
| 223 | vl_api_##n##_t_handler, \\ |
| 224 | vl_noop_handler, \\ |
| 225 | vl_api_##n##_t_endian, \\ |
| 226 | vl_api_##n##_t_print, \\ |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 227 | sizeof(vl_api_##n##_t), 1); |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 228 | foreach_" plugin-name "_plugin_api_msg; |
| 229 | #undef _ |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 234 | #define vl_msg_name_crc_list |
| 235 | #include <" plugin-name "/" plugin-name "_all_api_h.h> |
| 236 | #undef vl_msg_name_crc_list |
| 237 | |
| 238 | static void |
| 239 | setup_message_id_table (" plugin-name "_main_t * sm, api_main_t * am) |
| 240 | { |
| 241 | #define _(id,n,crc) \ |
| 242 | vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base); |
| 243 | foreach_vl_msg_name_crc_" plugin-name" ; |
| 244 | #undef _ |
| 245 | } |
| 246 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 247 | static clib_error_t * " plugin-name "_init (vlib_main_t * vm) |
| 248 | { |
| 249 | " plugin-name "_main_t * sm = &" plugin-name "_main; |
| 250 | clib_error_t * error = 0; |
| 251 | u8 * name; |
| 252 | |
| 253 | name = format (0, \"" plugin-name "_%08x%c\", api_version, 0); |
| 254 | |
| 255 | /* Ask for a correctly-sized block of API message decode slots */ |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 256 | sm->msg_id_base = vl_msg_api_get_msg_ids |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 257 | ((char *) name, VL_MSG_FIRST_AVAILABLE); |
| 258 | |
| 259 | error = " plugin-name "_plugin_api_hookup (vm); |
| 260 | |
Dave Barach | 605c636 | 2017-01-02 12:22:48 -0500 | [diff] [blame] | 261 | /* Add our API messages to the global name_crc hash table */ |
| 262 | setup_message_id_table (sm, &api_main); |
| 263 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 264 | vec_free(name); |
| 265 | |
| 266 | return error; |
| 267 | } |
| 268 | |
| 269 | VLIB_INIT_FUNCTION (" plugin-name "_init); |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 270 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 271 | VNET_FEATURE_INIT (" plugin-name ", static) = |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 272 | { |
| 273 | .arc_name = \"device-input\", |
| 274 | .node_name = \"" plugin-name "\", |
| 275 | .runs_before = VNET_FEATURES (\"ethernet-input\"), |
| 276 | }; |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 277 | ") |
| 278 | |