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)) |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 24 | '(setq capital-oh-en "ON") |
| 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> |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 49 | #include <vpp/app/version.h> |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 50 | |
| 51 | /* define message IDs */ |
| 52 | #include <" plugin-name "/" plugin-name "_msg_enum.h> |
| 53 | |
| 54 | /* define message structures */ |
| 55 | #define vl_typedefs |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 56 | #include <" plugin-name "/" plugin-name "_all_api_h.h> |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 57 | #undef vl_typedefs |
| 58 | |
| 59 | /* define generated endian-swappers */ |
| 60 | #define vl_endianfun |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 61 | #include <" plugin-name "/" plugin-name "_all_api_h.h> |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 62 | #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 Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 67 | #include <" plugin-name "/" plugin-name "_all_api_h.h> |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 68 | #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 Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 75 | /* |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 76 | * A handy macro to set up a message reply. |
| 77 | * Assumes that the following variables are available: |
| 78 | * mp - pointer to request message |
| 79 | * rmp - pointer to reply message type |
| 80 | * rv - return value |
| 81 | */ |
| 82 | |
| 83 | #define REPLY_MACRO(t) \\ |
| 84 | do { \\ |
| 85 | unix_shared_memory_queue_t * q = \\ |
| 86 | vl_api_client_index_to_input_queue (mp->client_index); \\ |
| 87 | if (!q) \\ |
| 88 | return; \\ |
| 89 | \\ |
| 90 | rmp = vl_msg_api_alloc (sizeof (*rmp)); \\ |
| 91 | rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \\ |
| 92 | rmp->context = mp->context; \\ |
| 93 | rmp->retval = ntohl(rv); \\ |
| 94 | \\ |
| 95 | vl_msg_api_send_shmem (q, (u8 *)&rmp); \\ |
| 96 | } while(0); |
| 97 | |
| 98 | |
| 99 | /* List of message types that this plugin understands */ |
| 100 | |
| 101 | #define foreach_" plugin-name "_plugin_api_msg \\ |
| 102 | _(" PLUGIN-NAME "_ENABLE_DISABLE, " plugin-name "_enable_disable) |
| 103 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 104 | /* Action function shared between message handler and debug CLI */ |
| 105 | |
| 106 | int " plugin-name "_enable_disable (" plugin-name "_main_t * sm, u32 sw_if_index, |
| 107 | int enable_disable) |
| 108 | { |
| 109 | vnet_sw_interface_t * sw; |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 110 | int rv = 0; |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 111 | |
| 112 | /* Utterly wrong? */ |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 113 | if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces, |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 114 | sw_if_index)) |
| 115 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 116 | |
| 117 | /* Not a physical port? */ |
| 118 | sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index); |
| 119 | if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 120 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 121 | |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 122 | vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\", |
| 123 | sw_if_index, enable_disable, 0, 0); |
| 124 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 125 | return rv; |
| 126 | } |
| 127 | |
| 128 | static clib_error_t * |
| 129 | " plugin-name "_enable_disable_command_fn (vlib_main_t * vm, |
| 130 | unformat_input_t * input, |
| 131 | vlib_cli_command_t * cmd) |
| 132 | { |
| 133 | " plugin-name "_main_t * sm = &" plugin-name "_main; |
| 134 | u32 sw_if_index = ~0; |
| 135 | int enable_disable = 1; |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 136 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 137 | int rv; |
| 138 | |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 139 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 140 | { |
| 141 | if (unformat (input, \"disable\")) |
| 142 | enable_disable = 0; |
| 143 | else if (unformat (input, \"%U\", unformat_vnet_sw_interface, |
| 144 | sm->vnet_main, &sw_if_index)) |
| 145 | ; |
| 146 | else |
| 147 | break; |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | if (sw_if_index == ~0) |
| 151 | return clib_error_return (0, \"Please specify an interface...\"); |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 152 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 153 | rv = " plugin-name "_enable_disable (sm, sw_if_index, enable_disable); |
| 154 | |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 155 | switch(rv) |
| 156 | { |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 157 | case 0: |
| 158 | break; |
| 159 | |
| 160 | case VNET_API_ERROR_INVALID_SW_IF_INDEX: |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 161 | return clib_error_return |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 162 | (0, \"Invalid interface, only works on physical ports\"); |
| 163 | break; |
| 164 | |
| 165 | case VNET_API_ERROR_UNIMPLEMENTED: |
| 166 | return clib_error_return (0, \"Device driver doesn't support redirection\"); |
| 167 | break; |
| 168 | |
| 169 | default: |
| 170 | return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\", |
| 171 | rv); |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 172 | } |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 176 | /* *INDENT-OFF* */ |
| 177 | VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) = |
| 178 | { |
| 179 | .path = \"" plugin-name " enable-disable\", |
| 180 | .short_help = |
| 181 | \"" plugin-name " enable-disable <interface-name> [disable]\", |
| 182 | .function = " plugin-name "_enable_disable_command_fn, |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 183 | }; |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 184 | /* *INDENT-ON* */ |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 185 | |
| 186 | /* API message handler */ |
| 187 | static void vl_api_" plugin-name "_enable_disable_t_handler |
| 188 | (vl_api_" plugin-name "_enable_disable_t * mp) |
| 189 | { |
| 190 | vl_api_" plugin-name "_enable_disable_reply_t * rmp; |
| 191 | " plugin-name "_main_t * sm = &" plugin-name "_main; |
| 192 | int rv; |
| 193 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 194 | rv = " plugin-name "_enable_disable (sm, ntohl(mp->sw_if_index), |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 195 | (int) (mp->enable_disable)); |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 196 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 197 | REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY); |
| 198 | } |
| 199 | |
| 200 | /* Set up the API message handling tables */ |
| 201 | static clib_error_t * |
| 202 | " plugin-name "_plugin_api_hookup (vlib_main_t *vm) |
| 203 | { |
| 204 | " plugin-name "_main_t * sm = &" plugin-name "_main; |
| 205 | #define _(N,n) \\ |
| 206 | vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \\ |
| 207 | #n, \\ |
| 208 | vl_api_##n##_t_handler, \\ |
| 209 | vl_noop_handler, \\ |
| 210 | vl_api_##n##_t_endian, \\ |
| 211 | vl_api_##n##_t_print, \\ |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 212 | sizeof(vl_api_##n##_t), 1); |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 213 | foreach_" plugin-name "_plugin_api_msg; |
| 214 | #undef _ |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 219 | #define vl_msg_name_crc_list |
| 220 | #include <" plugin-name "/" plugin-name "_all_api_h.h> |
| 221 | #undef vl_msg_name_crc_list |
| 222 | |
| 223 | static void |
| 224 | setup_message_id_table (" plugin-name "_main_t * sm, api_main_t * am) |
| 225 | { |
| 226 | #define _(id,n,crc) \ |
| 227 | vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base); |
| 228 | foreach_vl_msg_name_crc_" plugin-name" ; |
| 229 | #undef _ |
| 230 | } |
| 231 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 232 | static clib_error_t * " plugin-name "_init (vlib_main_t * vm) |
| 233 | { |
| 234 | " plugin-name "_main_t * sm = &" plugin-name "_main; |
| 235 | clib_error_t * error = 0; |
| 236 | u8 * name; |
| 237 | |
| 238 | name = format (0, \"" plugin-name "_%08x%c\", api_version, 0); |
| 239 | |
| 240 | /* Ask for a correctly-sized block of API message decode slots */ |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 241 | sm->msg_id_base = vl_msg_api_get_msg_ids |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 242 | ((char *) name, VL_MSG_FIRST_AVAILABLE); |
| 243 | |
| 244 | error = " plugin-name "_plugin_api_hookup (vm); |
| 245 | |
Dave Barach | 605c636 | 2017-01-02 12:22:48 -0500 | [diff] [blame] | 246 | /* Add our API messages to the global name_crc hash table */ |
| 247 | setup_message_id_table (sm, &api_main); |
| 248 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 249 | vec_free(name); |
| 250 | |
| 251 | return error; |
| 252 | } |
| 253 | |
| 254 | VLIB_INIT_FUNCTION (" plugin-name "_init); |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 255 | |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 256 | /* *INDENT-OFF* */ |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 257 | VNET_FEATURE_INIT (" plugin-name ", static) = |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 258 | { |
| 259 | .arc_name = \"device-input\", |
| 260 | .node_name = \"" plugin-name "\", |
| 261 | .runs_before = VNET_FEATURES (\"ethernet-input\"), |
| 262 | }; |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 263 | /* *INDENT-ON */ |
| 264 | |
| 265 | /* *INDENT-OFF* */ |
| 266 | VLIB_PLUGIN_REGISTER () = |
| 267 | { |
| 268 | .version = VPP_BUILD_VER, |
| 269 | }; |
| 270 | /* *INDENT-ON* */ |
| 271 | |
| 272 | /* |
| 273 | * fd.io coding-style-patch-verification: " capital-oh-en " |
| 274 | * |
| 275 | * Local Variables: |
| 276 | * eval: (c-set-style \"gnu\") |
| 277 | * End: |
| 278 | */ |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 279 | ") |
| 280 | |