blob: d829aaffaf9aa20eb6fcc21d6a0cd8c81f987501 [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 -080033VLIB_PLUGIN_REGISTER () = {
34 .version = SAMPLE_PLUGIN_BUILD_VER,
Damjan Marion1bfb0dd2017-03-22 11:08:39 +010035 .description = "Sample of VPP Plugin",
Anlu Yana4cb12b2017-02-14 18:07:40 -080036};
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
Damjan Marion3e13c092018-07-20 16:01:02 +020038sample_main_t sample_main;
39
Ray Kinsella583dc8d2017-06-08 15:54:19 +010040/**
41 * @brief Enable/disable the macswap plugin.
42 *
43 * Action function shared between message handler and debug CLI.
44 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
46int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
47 int enable_disable)
48{
49 vnet_sw_interface_t * sw;
Dave Barachb7e2f3d2016-11-08 16:47:34 -050050 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
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 Barachb7e2f3d2016-11-08 16:47:34 -050062 vnet_feature_enable_disable ("device-input", "sample",
63 sw_if_index, enable_disable, 0, 0);
64
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 return rv;
66}
67
68static clib_error_t *
69macswap_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 Kinsella583dc8d2017-06-08 15:54:19 +0100114/**
115 * @brief CLI command to enable/disable the sample macswap plugin.
116 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117VLIB_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 Kinsella583dc8d2017-06-08 15:54:19 +0100124/**
125 * @brief Plugin API message handler.
126 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127static 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 Troan2a1ca782019-09-19 01:08:30 +0200140/* API definitions */
141#include <sample/sample.api.c>
Dave Barach557d1282016-11-10 14:22:49 -0500142
Ray Kinsella583dc8d2017-06-08 15:54:19 +0100143/**
144 * @brief Initialize the sample plugin.
145 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146static clib_error_t * sample_init (vlib_main_t * vm)
147{
148 sample_main_t * sm = &sample_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Anlu Yana4cb12b2017-02-14 18:07:40 -0800150 sm->vnet_main = vnet_get_main ();
151
Dave Barach557d1282016-11-10 14:22:49 -0500152 /* Add our API messages to the global name_crc hash table */
Ole Troan2a1ca782019-09-19 01:08:30 +0200153 sm->msg_id_base = setup_message_id_table ();
Dave Barach557d1282016-11-10 14:22:49 -0500154
Ole Troan2a1ca782019-09-19 01:08:30 +0200155 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156}
157
158VLIB_INIT_FUNCTION (sample_init);
159
Ray Kinsella583dc8d2017-06-08 15:54:19 +0100160/**
161 * @brief Hook the sample plugin into the VPP graph hierarchy.
162 */
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500163VNET_FEATURE_INIT (sample, static) =
164{
165 .arc_name = "device-input",
166 .node_name = "sample",
167 .runs_before = VNET_FEATURES ("ethernet-input"),
168};