FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
sample.c
Go to the documentation of this file.
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  * @file
17  * @brief Sample Plugin, plugin API / trace / CLI handling.
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>
26 
27 #include <sample/sample.api_enum.h>
28 #include <sample/sample.api_types.h>
29 
30 #define REPLY_MSG_ID_BASE sm->msg_id_base
32 
33 /* *INDENT-OFF* */
35  .version = SAMPLE_PLUGIN_BUILD_VER,
36  .description = "Sample of VPP Plugin",
37 };
38 /* *INDENT-ON* */
39 
41 
42 /**
43  * @brief Enable/disable the macswap plugin.
44  *
45  * Action function shared between message handler and debug CLI.
46  */
47 
49  int enable_disable)
50 {
52  int rv = 0;
53 
54  /* Utterly wrong? */
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);
62  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
63 
64  vnet_feature_enable_disable ("device-input", "sample",
65  sw_if_index, enable_disable, 0, 0);
66 
67  return rv;
68 }
69 
70 static clib_error_t *
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 
116 /**
117  * @brief CLI command to enable/disable the sample macswap plugin.
118  */
119 VLIB_CLI_COMMAND (sr_content_command, static) = {
120  .path = "sample macswap",
121  .short_help =
122  "sample macswap <interface-name> [disable]",
124 };
125 
126 /**
127  * @brief Plugin API message handler.
128  */
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 
142 /* API definitions */
143 #include <sample/sample.api.c>
144 
145 /**
146  * @brief Initialize the sample plugin.
147  */
149 {
150  sample_main_t * sm = &sample_main;
151 
152  sm->vnet_main = vnet_get_main ();
153 
154  /* Add our API messages to the global name_crc hash table */
156 
157  return 0;
158 }
159 
161 
162 /**
163  * @brief Hook the sample plugin into the VPP graph hierarchy.
164  */
165 VNET_FEATURE_INIT (sample, static) =
166 {
167  .arc_name = "device-input",
168  .node_name = "sample",
169  .runs_before = VNET_FEATURES ("ethernet-input"),
170 };
sample_main_t sample_main
Definition: sample.c:40
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vnet_interface_main_t interface_main
Definition: vnet.h:56
vl_api_interface_index_t sw_if_index
Definition: sample.api:33
static void vl_api_sample_macswap_enable_disable_t_handler(vl_api_sample_macswap_enable_disable_t *mp)
Plugin API message handler.
Definition: sample.c:130
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
unformat_function_t unformat_vnet_sw_interface
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
#define SAMPLE_PLUGIN_BUILD_VER
Definition: sample.h:38
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
VLIB_PLUGIN_REGISTER()
struct _unformat_input_t unformat_input_t
#define REPLY_MACRO(t)
vlib_main_t * vm
Definition: in2out_ed.c:1810
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
int sample_macswap_enable_disable(sample_main_t *sm, u32 sw_if_index, int enable_disable)
Enable/disable the macswap plugin.
Definition: sample.c:48
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:284
vnet_main_t * vnet_main
Definition: sample.h:31
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
static clib_error_t * sample_init(vlib_main_t *vm)
Initialize the sample plugin.
Definition: sample.c:148
#define VNET_FEATURES(...)
Definition: feature.h:442
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:854
VNET_FEATURE_INIT(sample, static)
Hook the sample plugin into the VPP graph hierarchy.
vnet_sw_interface_type_t type
Definition: interface.h:718
static void setup_message_id_table(snat_main_t *sm, api_main_t *am)
Definition: nat_api.c:3410
static clib_error_t * macswap_enable_disable_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: sample.c:71
u16 msg_id_base
Definition: sample.h:28
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:304
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171