blob: 4aeb5358e9510ecadb931c1152208ab7e6f66c4b [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Kinsella583dc8d2017-06-08 15:54:19 +010015/**
16 * @file
17 * @brief Sample Plugin, plugin API / trace / CLI handling.
Ed Warnickecb9cada2015-12-08 15:45:58 -070018 */
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 Warnickecb9cada2015-12-08 15:45:58 -070026
Ole Troan2a1ca782019-09-19 01:08:30 +020027#include <sample/sample.api_enum.h>
28#include <sample/sample.api_types.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Eyal Barib614d082017-03-16 10:02:57 +020030#define REPLY_MSG_ID_BASE sm->msg_id_base
31#include <vlibapi/api_helper_macros.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070032
Anlu Yana4cb12b2017-02-14 18:07:40 -080033/* *INDENT-OFF* */
34VLIB_PLUGIN_REGISTER () = {
35 .version = SAMPLE_PLUGIN_BUILD_VER,
Damjan Marion1bfb0dd2017-03-22 11:08:39 +010036 .description = "Sample of VPP Plugin",
Anlu Yana4cb12b2017-02-14 18:07:40 -080037};
38/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070039
Damjan Marion3e13c092018-07-20 16:01:02 +020040sample_main_t sample_main;
41
Ray Kinsella583dc8d2017-06-08 15:54:19 +010042/**
43 * @brief Enable/disable the macswap plugin.
44 *
45 * Action function shared between message handler and debug CLI.
46 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
48int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
49 int enable_disable)
50{
51 vnet_sw_interface_t * sw;
Dave Barachb7e2f3d2016-11-08 16:47:34 -050052 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
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 Barachb7e2f3d2016-11-08 16:47:34 -050064 vnet_feature_enable_disable ("device-input", "sample",
65 sw_if_index, enable_disable, 0, 0);
66
Ed Warnickecb9cada2015-12-08 15:45:58 -070067 return rv;
68}
69
70static clib_error_t *
71macswap_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 Kinsella583dc8d2017-06-08 15:54:19 +0100116/**
117 * @brief CLI command to enable/disable the sample macswap plugin.
118 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119VLIB_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 Kinsella583dc8d2017-06-08 15:54:19 +0100126/**
127 * @brief Plugin API message handler.
128 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129static 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 Troan2a1ca782019-09-19 01:08:30 +0200142/* API definitions */
143#include <sample/sample.api.c>
Dave Barach557d1282016-11-10 14:22:49 -0500144
Ray Kinsella583dc8d2017-06-08 15:54:19 +0100145/**
146 * @brief Initialize the sample plugin.
147 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148static clib_error_t * sample_init (vlib_main_t * vm)
149{
150 sample_main_t * sm = &sample_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
Anlu Yana4cb12b2017-02-14 18:07:40 -0800152 sm->vnet_main = vnet_get_main ();
153
Dave Barach557d1282016-11-10 14:22:49 -0500154 /* Add our API messages to the global name_crc hash table */
Ole Troan2a1ca782019-09-19 01:08:30 +0200155 sm->msg_id_base = setup_message_id_table ();
Dave Barach557d1282016-11-10 14:22:49 -0500156
Ole Troan2a1ca782019-09-19 01:08:30 +0200157 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158}
159
160VLIB_INIT_FUNCTION (sample_init);
161
Ray Kinsella583dc8d2017-06-08 15:54:19 +0100162/**
163 * @brief Hook the sample plugin into the VPP graph hierarchy.
164 */
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500165VNET_FEATURE_INIT (sample, static) =
166{
167 .arc_name = "device-input",
168 .node_name = "sample",
169 .runs_before = VNET_FEATURES ("ethernet-input"),
170};