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