FD.io VPP  v19.04.2-12-g66b1689
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  u32 tmp;
33  u64 feature_mask = (u64) ~ (0ULL);
34 
35  /* Get a line of input. */
36  if (!unformat_user (input, unformat_line_input, line_input))
37  return 0;
38 
39  memset (&args, 0, sizeof (args));
40  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
41  {
42  if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
43  ;
44  else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
45  args.features = feature_mask;
46  else if (unformat (line_input, "rx-queue-size %u", &tmp))
47  args.rxq_size = tmp;
48  else if (unformat (line_input, "tx-queue-size %u", &tmp))
49  args.txq_size = tmp;
50  else
51  return clib_error_return (0, "unknown input `%U'",
52  format_unformat_error, input);
53  }
54  unformat_free (line_input);
55 
56  virtio_pci_create_if (vm, &args);
57 
58  return args.error;
59 }
60 
61 /* *INDENT-OFF* */
62 VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
63  .path = "create interface virtio",
64  .short_help = "create interface virtio <pci-address> "
65  "[feature-mask <hex-mask>] [rx-queue-size <size>] [tx-queue-size <size>]",
66  .function = virtio_pci_create_command_fn,
67 };
68 /* *INDENT-ON* */
69 
70 static clib_error_t *
72  vlib_cli_command_t * cmd)
73 {
74  unformat_input_t _line_input, *line_input = &_line_input;
75  u32 sw_if_index = ~0;
77  virtio_main_t *vim = &virtio_main;
78  virtio_if_t *vif;
79  vnet_main_t *vnm = vnet_get_main ();
80 
81  /* Get a line of input. */
82  if (!unformat_user (input, unformat_line_input, line_input))
83  return 0;
84 
85  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
86  {
87  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
88  ;
89  else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
90  vnm, &sw_if_index))
91  ;
92  else
93  return clib_error_return (0, "unknown input `%U'",
94  format_unformat_error, input);
95  }
96  unformat_free (line_input);
97 
98  if (sw_if_index == ~0)
99  return clib_error_return (0,
100  "please specify interface name or sw_if_index");
101 
102  hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
103  if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
104  return clib_error_return (0, "not a virtio interface");
105 
106  vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
107 
108  if (virtio_pci_delete_if (vm, vif) < 0)
109  return clib_error_return (0, "not a virtio pci interface");
110 
111  return 0;
112 }
113 
114 /* *INDENT-OFF* */
115 VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
116  .path = "delete interface virtio",
117  .short_help = "delete interface virtio "
118  "{<interface> | sw_if_index <sw_idx>}",
119  .function = virtio_pci_delete_command_fn,
120 };
121 /* *INDENT-ON* */
122 
123 static clib_error_t *
125  vlib_cli_command_t * cmd)
126 {
127  virtio_main_t *vim = &virtio_main;
128  vnet_main_t *vnm = &vnet_main;
129  virtio_if_t *vif;
130  clib_error_t *error = 0;
131  u32 hw_if_index, *hw_if_indices = 0;
133  u8 show_descr = 0, show_device_config = 0;
134 
136  {
137  if (unformat
138  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
139  {
140  hi = vnet_get_hw_interface (vnm, hw_if_index);
141  if (virtio_device_class.index != hi->dev_class_index)
142  {
143  error = clib_error_return (0, "unknown input `%U'",
144  format_unformat_error, input);
145  goto done;
146  }
147  vec_add1 (hw_if_indices, hw_if_index);
148  }
149  else if (unformat (input, "descriptors") || unformat (input, "desc"))
150  show_descr = 1;
151  else if (unformat (input, "debug-device"))
152  show_device_config = 1;
153  else
154  {
155  error = clib_error_return (0, "unknown input `%U'",
156  format_unformat_error, input);
157  goto done;
158  }
159  }
160 
161  if (vec_len (hw_if_indices) == 0)
162  {
163  pool_foreach (vif, vim->interfaces,
164  vec_add1 (hw_if_indices, vif->hw_if_index);
165  );
166  }
167  else if (show_device_config)
168  {
169  vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
170  if (vif->type == VIRTIO_IF_TYPE_PCI)
171  debug_device_config_space (vm, vif);
172  }
173 
174  virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
175 
176 done:
177  vec_free (hw_if_indices);
178  return error;
179 }
180 
181 /* *INDENT-OFF* */
182 VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
183  .path = "show virtio pci",
184  .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
185  .function = show_virtio_pci_fn,
186 };
187 /* *INDENT-ON* */
188 
189 clib_error_t *
191 {
192  virtio_main_t *vim = &virtio_main;
193  vim->log_default = vlib_log_register_class ("virtio-pci", 0);
194  return 0;
195 }
196 
198 
199 /*
200  * fd.io coding-style-patch-verification: ON
201  *
202  * Local Variables:
203  * eval: (c-set-style "gnu")
204  * End:
205  */
unformat_function_t unformat_vnet_hw_interface
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:227
u32 sw_if_index
Definition: ipsec_gre.api:37
vmrglw vmrglh hi
virtio_if_t * interfaces
Definition: virtio.h:191
void virtio_show(vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr, u32 type)
Definition: virtio.c:267
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
unsigned long u64
Definition: types.h:89
#define NULL
Definition: clib.h:58
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:522
static clib_error_t * show_virtio_pci_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:124
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
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:989
u32 hw_if_index
Definition: virtio.h:140
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
#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:282
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
struct _unformat_input_t unformat_input_t
unformat_function_t unformat_vlib_pci_addr
Definition: pci.h:323
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:71
vnet_main_t vnet_main
Definition: misc.c:43
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
vlib_main_t * vm
Definition: buffer.c:312
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
void debug_device_config_space(vlib_main_t *vm, virtio_if_t *vif)
Definition: pci.c:385
vlib_log_class_t log_default
Definition: virtio.h:189
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
int virtio_pci_delete_if(vlib_main_t *vm, virtio_if_t *vif)
Definition: pci.c:1186
virtio_if_type_t type
Definition: virtio.h:144
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
clib_error_t * error
Definition: pci.h:238
virtio_main_t virtio_main
Definition: virtio.c: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:162
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:190
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170