FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
cli.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 #include <vlib/vlib.h>
18 #include <vlib/unix/unix.h>
19 #include <vlib/pci/pci.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ip/ip4_packet.h>
22 #include <vnet/ip/ip6_packet.h>
25 
26 static clib_error_t *
28  vlib_cli_command_t * cmd)
29 {
30  unformat_input_t _line_input, *line_input = &_line_input;
32  u64 feature_mask = (u64) ~ (0ULL);
33 
34  /* Get a line of input. */
35  if (!unformat_user (input, unformat_line_input, line_input))
36  return 0;
37 
38  memset (&args, 0, sizeof (args));
39  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
40  {
41  if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
42  ;
43  else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
44  args.features = feature_mask;
45  else if (unformat (line_input, "gso-enabled"))
46  args.gso_enabled = 1;
47  else if (unformat (line_input, "csum-enabled"))
48  args.checksum_offload_enabled = 1;
49  else
50  return clib_error_return (0, "unknown input `%U'",
51  format_unformat_error, input);
52  }
53  unformat_free (line_input);
54 
55  virtio_pci_create_if (vm, &args);
56 
57  return args.error;
58 }
59 
60 /* *INDENT-OFF* */
61 VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
62  .path = "create interface virtio",
63  .short_help = "create interface virtio <pci-address> "
64  "[feature-mask <hex-mask>] [gso-enabled] [csum-enabled]",
65  .function = virtio_pci_create_command_fn,
66 };
67 /* *INDENT-ON* */
68 
69 static clib_error_t *
71  vlib_cli_command_t * cmd)
72 {
73  unformat_input_t _line_input, *line_input = &_line_input;
74  u32 sw_if_index = ~0;
76  virtio_main_t *vim = &virtio_main;
77  virtio_if_t *vif;
78  vnet_main_t *vnm = vnet_get_main ();
79 
80  /* Get a line of input. */
81  if (!unformat_user (input, unformat_line_input, line_input))
82  return 0;
83 
84  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
85  {
86  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
87  ;
88  else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
89  vnm, &sw_if_index))
90  ;
91  else
92  return clib_error_return (0, "unknown input `%U'",
93  format_unformat_error, input);
94  }
95  unformat_free (line_input);
96 
97  if (sw_if_index == ~0)
98  return clib_error_return (0,
99  "please specify interface name or sw_if_index");
100 
101  hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
102  if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
103  return clib_error_return (0, "not a virtio interface");
104 
105  vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
106 
107  if (virtio_pci_delete_if (vm, vif) < 0)
108  return clib_error_return (0, "not a virtio pci interface");
109 
110  return 0;
111 }
112 
113 /* *INDENT-OFF* */
114 VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
115  .path = "delete interface virtio",
116  .short_help = "delete interface virtio "
117  "{<interface> | sw_if_index <sw_idx>}",
118  .function = virtio_pci_delete_command_fn,
119 };
120 /* *INDENT-ON* */
121 
122 static clib_error_t *
124  vlib_cli_command_t * cmd)
125 {
126  unformat_input_t _line_input, *line_input = &_line_input;
127  u32 sw_if_index = ~0;
129  virtio_main_t *vim = &virtio_main;
130  virtio_if_t *vif;
131  vnet_main_t *vnm = vnet_get_main ();
132  int gso_enabled = 0, checksum_offload_enabled = 0;
133  int offloads_disabled = 0;
134 
135  /* Get a line of input. */
136  if (!unformat_user (input, unformat_line_input, line_input))
137  return 0;
138 
139  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
140  {
141  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
142  ;
143  else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
144  vnm, &sw_if_index))
145  ;
146  else if (unformat (line_input, "gso-enabled"))
147  gso_enabled = 1;
148  else if (unformat (line_input, "csum-offload-enabled"))
149  checksum_offload_enabled = 1;
150  else if (unformat (line_input, "offloads-disabled"))
151  offloads_disabled = 1;
152  else
153  return clib_error_return (0, "unknown input `%U'",
154  format_unformat_error, input);
155  }
156  unformat_free (line_input);
157 
158  if (sw_if_index == ~0)
159  return clib_error_return (0,
160  "please specify interface name or sw_if_index");
161 
162  hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
163  if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
164  return clib_error_return (0, "not a virtio interface");
165 
166  vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
167 
169  (vm, vif, gso_enabled, checksum_offload_enabled, offloads_disabled) < 0)
170  return clib_error_return (0, "not able to enable/disable offloads");
171 
172  return 0;
173 }
174 
175 /* *INDENT-OFF* */
176 VLIB_CLI_COMMAND (virtio_pci_enable_command, static) = {
177  .path = "set virtio pci",
178  .short_help = "set virtio pci {<interface> | sw_if_index <sw_idx>}"
179  " [gso-enabled | csum-offload-enabled | offloads-disabled]",
180  .function = virtio_pci_enable_command_fn,
181 };
182 /* *INDENT-ON* */
183 
184 static clib_error_t *
186  vlib_cli_command_t * cmd)
187 {
188  virtio_main_t *vim = &virtio_main;
189  vnet_main_t *vnm = &vnet_main;
190  virtio_if_t *vif;
191  clib_error_t *error = 0;
192  u32 hw_if_index, *hw_if_indices = 0;
194  u8 show_descr = 0, show_device_config = 0;
195 
197  {
198  if (unformat
199  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
200  {
201  hi = vnet_get_hw_interface (vnm, hw_if_index);
202  if (virtio_device_class.index != hi->dev_class_index)
203  {
204  error = clib_error_return (0, "unknown input `%U'",
205  format_unformat_error, input);
206  goto done;
207  }
208  vec_add1 (hw_if_indices, hw_if_index);
209  }
210  else if (unformat (input, "descriptors") || unformat (input, "desc"))
211  show_descr = 1;
212  else if (unformat (input, "debug-device"))
213  show_device_config = 1;
214  else
215  {
216  error = clib_error_return (0, "unknown input `%U'",
217  format_unformat_error, input);
218  goto done;
219  }
220  }
221 
222  if (vec_len (hw_if_indices) == 0)
223  {
224  pool_foreach (vif, vim->interfaces,
225  vec_add1 (hw_if_indices, vif->hw_if_index);
226  );
227  }
228  else if (show_device_config)
229  {
230  vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
231  if (vif->type == VIRTIO_IF_TYPE_PCI)
232  vif->virtio_pci_func->device_debug_config_space (vm, vif);
233  }
234 
235  virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
236 
237 done:
238  vec_free (hw_if_indices);
239  return error;
240 }
241 
242 /* *INDENT-OFF* */
243 VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
244  .path = "show virtio pci",
245  .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
246  .function = show_virtio_pci_fn,
247 };
248 /* *INDENT-ON* */
249 
250 clib_error_t *
252 {
253  return 0;
254 }
255 
257 
258 /*
259  * fd.io coding-style-patch-verification: ON
260  *
261  * Local Variables:
262  * eval: (c-set-style "gnu")
263  * End:
264  */
unformat_function_t unformat_vnet_hw_interface
static clib_error_t * virtio_pci_delete_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:70
virtio_if_t * interfaces
Definition: virtio.h:192
void virtio_show(vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr, u32 type)
Definition: virtio.c:265
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
unsigned long u64
Definition: types.h:89
const virtio_pci_func_t * virtio_pci_func
Definition: virtio.h:184
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
vnet_device_class_t virtio_device_class
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
vlib_main_t * vm
Definition: in2out_ed.c:1582
unformat_function_t unformat_vnet_sw_interface
unsigned char u8
Definition: types.h:56
void virtio_pci_create_if(vlib_main_t *vm, virtio_pci_create_if_args_t *args)
Definition: pci.c:1009
static clib_error_t * virtio_pci_enable_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:123
u32 hw_if_index
Definition: virtio.h:128
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
#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
unformat_function_t unformat_line_input
Definition: format.h:283
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
struct _unformat_input_t unformat_input_t
unformat_function_t unformat_vlib_pci_addr
Definition: pci.h:323
static clib_error_t * virtio_pci_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:27
vnet_main_t vnet_main
Definition: misc.c:43
int virtio_pci_enable_disable_offloads(vlib_main_t *vm, virtio_if_t *vif, int gso_enabled, int checksum_offload_enabled, int offloads_disabled)
Definition: pci.c:1294
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
static clib_error_t * show_virtio_pci_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:185
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
int virtio_pci_delete_if(vlib_main_t *vm, virtio_if_t *vif)
Definition: pci.c:1210
virtio_if_type_t type
Definition: virtio.h:126
clib_error_t * error
Definition: pci.h:241
virtio_main_t virtio_main
Definition: virtio.c:35
vl_api_ip4_address_t hi
Definition: arp.api:37
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
clib_error_t * virtio_pci_cli_init(vlib_main_t *vm)
Definition: cli.c:251
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171