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 | */ |
Ray Kinsella | 583dc8d | 2017-06-08 15:54:19 +0100 | [diff] [blame] | 15 | /** |
| 16 | * @file |
| 17 | * @brief Sample Plugin, plugin API / trace / CLI handling. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <vnet/vnet.h> |
| 21 | #include <vnet/plugin/plugin.h> |
| 22 | #include <sample/sample.h> |
| 23 | |
| 24 | #include <vlibapi/api.h> |
| 25 | #include <vlibmemory/api.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 27 | #include <sample/sample.api_enum.h> |
| 28 | #include <sample/sample.api_types.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | |
Eyal Bari | b614d08 | 2017-03-16 10:02:57 +0200 | [diff] [blame] | 30 | #define REPLY_MSG_ID_BASE sm->msg_id_base |
| 31 | #include <vlibapi/api_helper_macros.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | |
Anlu Yan | a4cb12b | 2017-02-14 18:07:40 -0800 | [diff] [blame] | 33 | /* *INDENT-OFF* */ |
| 34 | VLIB_PLUGIN_REGISTER () = { |
| 35 | .version = SAMPLE_PLUGIN_BUILD_VER, |
Damjan Marion | 1bfb0dd | 2017-03-22 11:08:39 +0100 | [diff] [blame] | 36 | .description = "Sample of VPP Plugin", |
Anlu Yan | a4cb12b | 2017-02-14 18:07:40 -0800 | [diff] [blame] | 37 | }; |
| 38 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | |
Damjan Marion | 3e13c09 | 2018-07-20 16:01:02 +0200 | [diff] [blame] | 40 | sample_main_t sample_main; |
| 41 | |
Ray Kinsella | 583dc8d | 2017-06-08 15:54:19 +0100 | [diff] [blame] | 42 | /** |
| 43 | * @brief Enable/disable the macswap plugin. |
| 44 | * |
| 45 | * Action function shared between message handler and debug CLI. |
| 46 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | |
| 48 | int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index, |
| 49 | int enable_disable) |
| 50 | { |
| 51 | vnet_sw_interface_t * sw; |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 52 | int rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
| 54 | /* Utterly wrong? */ |
| 55 | if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces, |
| 56 | sw_if_index)) |
| 57 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 58 | |
| 59 | /* Not a physical port? */ |
| 60 | sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index); |
| 61 | if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 62 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 63 | |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 64 | vnet_feature_enable_disable ("device-input", "sample", |
| 65 | sw_if_index, enable_disable, 0, 0); |
| 66 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | return rv; |
| 68 | } |
| 69 | |
| 70 | static clib_error_t * |
| 71 | macswap_enable_disable_command_fn (vlib_main_t * vm, |
| 72 | unformat_input_t * input, |
| 73 | vlib_cli_command_t * cmd) |
| 74 | { |
| 75 | sample_main_t * sm = &sample_main; |
| 76 | u32 sw_if_index = ~0; |
| 77 | int enable_disable = 1; |
| 78 | |
| 79 | int rv; |
| 80 | |
| 81 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { |
| 82 | if (unformat (input, "disable")) |
| 83 | enable_disable = 0; |
| 84 | else if (unformat (input, "%U", unformat_vnet_sw_interface, |
| 85 | sm->vnet_main, &sw_if_index)) |
| 86 | ; |
| 87 | else |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | if (sw_if_index == ~0) |
| 92 | return clib_error_return (0, "Please specify an interface..."); |
| 93 | |
| 94 | rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable); |
| 95 | |
| 96 | switch(rv) { |
| 97 | case 0: |
| 98 | break; |
| 99 | |
| 100 | case VNET_API_ERROR_INVALID_SW_IF_INDEX: |
| 101 | return clib_error_return |
| 102 | (0, "Invalid interface, only works on physical ports"); |
| 103 | break; |
| 104 | |
| 105 | case VNET_API_ERROR_UNIMPLEMENTED: |
| 106 | return clib_error_return (0, "Device driver doesn't support redirection"); |
| 107 | break; |
| 108 | |
| 109 | default: |
| 110 | return clib_error_return (0, "sample_macswap_enable_disable returned %d", |
| 111 | rv); |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
Ray Kinsella | 583dc8d | 2017-06-08 15:54:19 +0100 | [diff] [blame] | 116 | /** |
| 117 | * @brief CLI command to enable/disable the sample macswap plugin. |
| 118 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | VLIB_CLI_COMMAND (sr_content_command, static) = { |
| 120 | .path = "sample macswap", |
| 121 | .short_help = |
| 122 | "sample macswap <interface-name> [disable]", |
| 123 | .function = macswap_enable_disable_command_fn, |
| 124 | }; |
| 125 | |
Ray Kinsella | 583dc8d | 2017-06-08 15:54:19 +0100 | [diff] [blame] | 126 | /** |
| 127 | * @brief Plugin API message handler. |
| 128 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | static void vl_api_sample_macswap_enable_disable_t_handler |
| 130 | (vl_api_sample_macswap_enable_disable_t * mp) |
| 131 | { |
| 132 | vl_api_sample_macswap_enable_disable_reply_t * rmp; |
| 133 | sample_main_t * sm = &sample_main; |
| 134 | int rv; |
| 135 | |
| 136 | rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index), |
| 137 | (int) (mp->enable_disable)); |
| 138 | |
| 139 | REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY); |
| 140 | } |
| 141 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 142 | /* API definitions */ |
| 143 | #include <sample/sample.api.c> |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 144 | |
Ray Kinsella | 583dc8d | 2017-06-08 15:54:19 +0100 | [diff] [blame] | 145 | /** |
| 146 | * @brief Initialize the sample plugin. |
| 147 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | static clib_error_t * sample_init (vlib_main_t * vm) |
| 149 | { |
| 150 | sample_main_t * sm = &sample_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Anlu Yan | a4cb12b | 2017-02-14 18:07:40 -0800 | [diff] [blame] | 152 | sm->vnet_main = vnet_get_main (); |
| 153 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 154 | /* Add our API messages to the global name_crc hash table */ |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 155 | sm->msg_id_base = setup_message_id_table (); |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 156 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 157 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | VLIB_INIT_FUNCTION (sample_init); |
| 161 | |
Ray Kinsella | 583dc8d | 2017-06-08 15:54:19 +0100 | [diff] [blame] | 162 | /** |
| 163 | * @brief Hook the sample plugin into the VPP graph hierarchy. |
| 164 | */ |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 165 | VNET_FEATURE_INIT (sample, static) = |
| 166 | { |
| 167 | .arc_name = "device-input", |
| 168 | .node_name = "sample", |
| 169 | .runs_before = VNET_FEATURES ("ethernet-input"), |
| 170 | }; |