Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | *------------------------------------------------------------------ |
| 17 | * sample.c - simple MAC-swap API / debug CLI handling |
| 18 | *------------------------------------------------------------------ |
| 19 | */ |
| 20 | |
| 21 | #include <vnet/vnet.h> |
| 22 | #include <vnet/plugin/plugin.h> |
| 23 | #include <sample/sample.h> |
| 24 | |
| 25 | #include <vlibapi/api.h> |
| 26 | #include <vlibmemory/api.h> |
| 27 | #include <vlibsocket/api.h> |
| 28 | |
| 29 | /* define message IDs */ |
| 30 | #include <sample/sample_msg_enum.h> |
| 31 | |
| 32 | /* define message structures */ |
| 33 | #define vl_typedefs |
| 34 | #include <sample/sample_all_api_h.h> |
| 35 | #undef vl_typedefs |
| 36 | |
| 37 | /* define generated endian-swappers */ |
| 38 | #define vl_endianfun |
| 39 | #include <sample/sample_all_api_h.h> |
| 40 | #undef vl_endianfun |
| 41 | |
| 42 | /* instantiate all the print functions we know about */ |
| 43 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 44 | #define vl_printfun |
| 45 | #include <sample/sample_all_api_h.h> |
| 46 | #undef vl_printfun |
| 47 | |
| 48 | /* Get the API version number */ |
| 49 | #define vl_api_version(n,v) static u32 api_version=(v); |
| 50 | #include <sample/sample_all_api_h.h> |
| 51 | #undef vl_api_version |
| 52 | |
Eyal Bari | b614d08 | 2017-03-16 10:02:57 +0200 | [diff] [blame] | 53 | #define REPLY_MSG_ID_BASE sm->msg_id_base |
| 54 | #include <vlibapi/api_helper_macros.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | |
| 56 | /* List of message types that this plugin understands */ |
| 57 | |
| 58 | #define foreach_sample_plugin_api_msg \ |
| 59 | _(SAMPLE_MACSWAP_ENABLE_DISABLE, sample_macswap_enable_disable) |
| 60 | |
Anlu Yan | a4cb12b | 2017-02-14 18:07:40 -0800 | [diff] [blame] | 61 | /* *INDENT-OFF* */ |
| 62 | VLIB_PLUGIN_REGISTER () = { |
| 63 | .version = SAMPLE_PLUGIN_BUILD_VER, |
Damjan Marion | 1bfb0dd | 2017-03-22 11:08:39 +0100 | [diff] [blame^] | 64 | .description = "Sample of VPP Plugin", |
Anlu Yan | a4cb12b | 2017-02-14 18:07:40 -0800 | [diff] [blame] | 65 | }; |
| 66 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
| 68 | /* Action function shared between message handler and debug CLI */ |
| 69 | |
| 70 | int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index, |
| 71 | int enable_disable) |
| 72 | { |
| 73 | vnet_sw_interface_t * sw; |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 74 | int rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | |
| 76 | /* Utterly wrong? */ |
| 77 | if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces, |
| 78 | sw_if_index)) |
| 79 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 80 | |
| 81 | /* Not a physical port? */ |
| 82 | sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index); |
| 83 | if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 84 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 85 | |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 86 | vnet_feature_enable_disable ("device-input", "sample", |
| 87 | sw_if_index, enable_disable, 0, 0); |
| 88 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | return rv; |
| 90 | } |
| 91 | |
| 92 | static clib_error_t * |
| 93 | macswap_enable_disable_command_fn (vlib_main_t * vm, |
| 94 | unformat_input_t * input, |
| 95 | vlib_cli_command_t * cmd) |
| 96 | { |
| 97 | sample_main_t * sm = &sample_main; |
| 98 | u32 sw_if_index = ~0; |
| 99 | int enable_disable = 1; |
| 100 | |
| 101 | int rv; |
| 102 | |
| 103 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { |
| 104 | if (unformat (input, "disable")) |
| 105 | enable_disable = 0; |
| 106 | else if (unformat (input, "%U", unformat_vnet_sw_interface, |
| 107 | sm->vnet_main, &sw_if_index)) |
| 108 | ; |
| 109 | else |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | if (sw_if_index == ~0) |
| 114 | return clib_error_return (0, "Please specify an interface..."); |
| 115 | |
| 116 | rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable); |
| 117 | |
| 118 | switch(rv) { |
| 119 | case 0: |
| 120 | break; |
| 121 | |
| 122 | case VNET_API_ERROR_INVALID_SW_IF_INDEX: |
| 123 | return clib_error_return |
| 124 | (0, "Invalid interface, only works on physical ports"); |
| 125 | break; |
| 126 | |
| 127 | case VNET_API_ERROR_UNIMPLEMENTED: |
| 128 | return clib_error_return (0, "Device driver doesn't support redirection"); |
| 129 | break; |
| 130 | |
| 131 | default: |
| 132 | return clib_error_return (0, "sample_macswap_enable_disable returned %d", |
| 133 | rv); |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | VLIB_CLI_COMMAND (sr_content_command, static) = { |
| 139 | .path = "sample macswap", |
| 140 | .short_help = |
| 141 | "sample macswap <interface-name> [disable]", |
| 142 | .function = macswap_enable_disable_command_fn, |
| 143 | }; |
| 144 | |
| 145 | /* API message handler */ |
| 146 | static void vl_api_sample_macswap_enable_disable_t_handler |
| 147 | (vl_api_sample_macswap_enable_disable_t * mp) |
| 148 | { |
| 149 | vl_api_sample_macswap_enable_disable_reply_t * rmp; |
| 150 | sample_main_t * sm = &sample_main; |
| 151 | int rv; |
| 152 | |
| 153 | rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index), |
| 154 | (int) (mp->enable_disable)); |
| 155 | |
| 156 | REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY); |
| 157 | } |
| 158 | |
| 159 | /* Set up the API message handling tables */ |
| 160 | static clib_error_t * |
| 161 | sample_plugin_api_hookup (vlib_main_t *vm) |
| 162 | { |
| 163 | sample_main_t * sm = &sample_main; |
| 164 | #define _(N,n) \ |
| 165 | vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \ |
| 166 | #n, \ |
| 167 | vl_api_##n##_t_handler, \ |
| 168 | vl_noop_handler, \ |
| 169 | vl_api_##n##_t_endian, \ |
| 170 | vl_api_##n##_t_print, \ |
| 171 | sizeof(vl_api_##n##_t), 1); |
| 172 | foreach_sample_plugin_api_msg; |
| 173 | #undef _ |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 178 | #define vl_msg_name_crc_list |
| 179 | #include <sample/sample_all_api_h.h> |
| 180 | #undef vl_msg_name_crc_list |
| 181 | |
| 182 | static void |
| 183 | setup_message_id_table (sample_main_t * sm, api_main_t *am) |
| 184 | { |
| 185 | #define _(id,n,crc) \ |
| 186 | vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base); |
| 187 | foreach_vl_msg_name_crc_sample; |
| 188 | #undef _ |
| 189 | } |
| 190 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | static clib_error_t * sample_init (vlib_main_t * vm) |
| 192 | { |
| 193 | sample_main_t * sm = &sample_main; |
| 194 | clib_error_t * error = 0; |
| 195 | u8 * name; |
| 196 | |
Anlu Yan | a4cb12b | 2017-02-14 18:07:40 -0800 | [diff] [blame] | 197 | sm->vnet_main = vnet_get_main (); |
| 198 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | name = format (0, "sample_%08x%c", api_version, 0); |
| 200 | |
| 201 | /* Ask for a correctly-sized block of API message decode slots */ |
| 202 | sm->msg_id_base = vl_msg_api_get_msg_ids |
| 203 | ((char *) name, VL_MSG_FIRST_AVAILABLE); |
| 204 | |
| 205 | error = sample_plugin_api_hookup (vm); |
| 206 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 207 | /* Add our API messages to the global name_crc hash table */ |
| 208 | setup_message_id_table (sm, &api_main); |
| 209 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | vec_free(name); |
| 211 | |
| 212 | return error; |
| 213 | } |
| 214 | |
| 215 | VLIB_INIT_FUNCTION (sample_init); |
| 216 | |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 217 | VNET_FEATURE_INIT (sample, static) = |
| 218 | { |
| 219 | .arc_name = "device-input", |
| 220 | .node_name = "sample", |
| 221 | .runs_before = VNET_FEATURES ("ethernet-input"), |
| 222 | }; |