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