FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
cli.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 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 <stdint.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <inttypes.h>
21 
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/ip/ip4_packet.h>
26 #include <vnet/ip/ip6_packet.h>
27 #include <vnet/ip/format.h>
28 #include <linux/virtio_net.h>
29 #include <linux/vhost.h>
31 #include <vnet/devices/tap/tap.h>
32 
33 static clib_error_t *
35  vlib_cli_command_t * cmd)
36 {
37  unformat_input_t _line_input, *line_input = &_line_input;
38  tap_create_if_args_t args = { 0 };
39  int ip_addr_set = 0;
40 
41  args.id = ~0;
42 
43  /* Get a line of input. */
44  if (unformat_user (input, unformat_line_input, line_input))
45  {
46  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
47  {
48  if (unformat (line_input, "id %u", &args.id))
49  ;
50  else
51  if (unformat (line_input, "host-if-name %s", &args.host_if_name))
52  ;
53  else if (unformat (line_input, "host-ns %s", &args.host_namespace))
54  ;
55  else if (unformat (line_input, "host-mac-addr %U",
57  ;
58  else if (unformat (line_input, "host-bridge %s", &args.host_bridge))
59  ;
60  else if (unformat (line_input, "host-ip4-addr %U/%d",
62  &args.host_ip4_prefix_len))
63  ip_addr_set = 1;
64  else if (unformat (line_input, "host-ip6-addr %U/%d",
66  &args.host_ip6_prefix_len))
67  ip_addr_set = 1;
68  else if (unformat (line_input, "rx-ring-size %d", &args.rx_ring_sz))
69  ;
70  else if (unformat (line_input, "tx-ring-size %d", &args.tx_ring_sz))
71  ;
72  else if (unformat (line_input, "hw-addr %U",
74  args.mac_addr_set = 1;
75  else
76  {
77  unformat_free (line_input);
78  return clib_error_return (0, "unknown input `%U'",
79  format_unformat_error, input);
80  }
81  }
82  unformat_free (line_input);
83  }
84 
85  if (ip_addr_set && args.host_bridge)
86  return clib_error_return (0, "Please specify either host ip address or "
87  "host bridge");
88 
89  tap_create_if (vm, &args);
90 
91  vec_free (args.host_if_name);
92  vec_free (args.host_namespace);
93  vec_free (args.host_bridge);
94 
95  return args.error;
96 
97 }
98 
99 /* *INDENT-OFF* */
100 VLIB_CLI_COMMAND (tap_create_command, static) = {
101  .path = "create tap",
102  .short_help = "create tap {id <if-id>} [hw-addr <mac-address>] "
103  "[rx-ring-size <size>] [tx-ring-size <size>] [host-ns <netns>] "
104  "[host-bridge <bridge-name>] [host-ip4-addr <ip4addr/mask>] "
105  "[host-ip6-addr <ip6-addr] [host-if-name <name>]",
106  .function = tap_create_command_fn,
107 };
108 /* *INDENT-ON* */
109 
110 static clib_error_t *
112  vlib_cli_command_t * cmd)
113 {
114  unformat_input_t _line_input, *line_input = &_line_input;
115  u32 sw_if_index = ~0;
116  vnet_main_t *vnm = vnet_get_main ();
117  int rv;
118 
119  /* Get a line of input. */
120  if (!unformat_user (input, unformat_line_input, line_input))
121  return clib_error_return (0, "Missing <interface>");
122 
123  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
124  {
125  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
126  ;
127  else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
128  vnm, &sw_if_index))
129  ;
130  else
131  return clib_error_return (0, "unknown input `%U'",
132  format_unformat_error, input);
133  }
134  unformat_free (line_input);
135 
136  if (sw_if_index == ~0)
137  return clib_error_return (0,
138  "please specify interface name or sw_if_index");
139 
140  rv = tap_delete_if (vm, sw_if_index);
141  if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
142  return clib_error_return (0, "not a tap interface");
143  else if (rv != 0)
144  return clib_error_return (0, "error on deleting tap interface");
145 
146  return 0;
147 }
148 
149 /* *INDENT-OFF* */
150 VLIB_CLI_COMMAND (tap_delete__command, static) =
151 {
152  .path = "delete tap",
153  .short_help = "delete tap {<interface> | sw_if_index <sw_idx>}",
154  .function = tap_delete_command_fn,
155 };
156 /* *INDENT-ON* */
157 
158 static clib_error_t *
160  vlib_cli_command_t * cmd)
161 {
162  virtio_main_t *mm = &virtio_main;
163  virtio_if_t *vif;
164  vnet_main_t *vnm = vnet_get_main ();
165  int show_descr = 0;
166  clib_error_t *error = 0;
167  u32 hw_if_index, *hw_if_indices = 0;
168  virtio_vring_t *vring;
169  int i, j;
170  struct feat_struct
171  {
172  u8 bit;
173  char *str;
174  };
175  struct feat_struct *feat_entry;
176 
177  static struct feat_struct feat_array[] = {
178 #define _(s,b) { .str = #s, .bit = b, },
180 #undef _
181  {.str = NULL}
182  };
183 
184  struct feat_struct *flag_entry;
185  static struct feat_struct flags_array[] = {
186 #define _(b,e,s) { .bit = b, .str = s, },
188 #undef _
189  {.str = NULL}
190  };
191 
193  {
194  if (unformat
195  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
196  vec_add1 (hw_if_indices, hw_if_index);
197  else if (unformat (input, "descriptors"))
198  show_descr = 1;
199  else
200  {
201  error = clib_error_return (0, "unknown input `%U'",
202  format_unformat_error, input);
203  goto done;
204  }
205  }
206 
207  if (vec_len (hw_if_indices) == 0)
208  {
209  /* *INDENT-OFF* */
210  pool_foreach (vif, mm->interfaces,
211  vec_add1 (hw_if_indices, vif->hw_if_index);
212  );
213  /* *INDENT-ON* */
214  }
215 
216  for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
217  {
219  vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
220  vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
221  vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
222  vnm, vif->sw_if_index);
223  if (vif->host_if_name)
224  vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
225  if (vif->net_ns)
226  vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
227  vlib_cli_output (vm, " flags 0x%x", vif->flags);
228  flag_entry = (struct feat_struct *) &flags_array;
229  while (flag_entry->str)
230  {
231  if (vif->flags & (1ULL << flag_entry->bit))
232  vlib_cli_output (vm, " %s (%d)", flag_entry->str,
233  flag_entry->bit);
234  flag_entry++;
235  }
236  vlib_cli_output (vm, " fd %d", vif->fd);
237  vlib_cli_output (vm, " tap-fd %d", vif->tap_fd);
238  vlib_cli_output (vm, " features 0x%lx", vif->features);
239  feat_entry = (struct feat_struct *) &feat_array;
240  while (feat_entry->str)
241  {
242  if (vif->features & (1ULL << feat_entry->bit))
243  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
244  feat_entry->bit);
245  feat_entry++;
246  }
247  vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
248  feat_entry = (struct feat_struct *) &feat_array;
249  while (feat_entry->str)
250  {
251  if (vif->remote_features & (1ULL << feat_entry->bit))
252  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
253  feat_entry->bit);
254  feat_entry++;
255  }
256  vec_foreach_index (i, vif->vrings)
257  {
258  // RX = 0, TX = 1
259  vring = vec_elt_at_index (vif->vrings, i);
260  vlib_cli_output (vm, " Virtqueue (%s)", (i & 1) ? "TX" : "RX");
261  vlib_cli_output (vm,
262  " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
263  vring->size, vring->last_used_idx, vring->desc_next,
264  vring->desc_in_use);
265  vlib_cli_output (vm,
266  " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
267  vring->avail->flags, vring->avail->idx,
268  vring->used->flags, vring->used->idx);
269  vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
270  vring->call_fd);
271  if (show_descr)
272  {
273  vlib_cli_output (vm, "\n descriptor table:\n");
274  vlib_cli_output (vm,
275  " id addr len flags next user_addr\n");
276  vlib_cli_output (vm,
277  " ===== ================== ===== ====== ===== ==================\n");
278  vring = vif->vrings;
279  for (j = 0; j < vring->size; j++)
280  {
281  struct vring_desc *desc = &vring->desc[j];
282  vlib_cli_output (vm,
283  " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
284  j, desc->addr,
285  desc->len,
286  desc->flags, desc->next, desc->addr);
287  }
288  }
289  }
290  }
291 done:
292  vec_free (hw_if_indices);
293  return error;
294 }
295 
296 /* *INDENT-OFF* */
297 VLIB_CLI_COMMAND (tap_show_command, static) = {
298  .path = "show tap",
299  .short_help = "show tap {<interface>] [descriptors]",
300  .function = tap_show_command_fn,
301 };
302 /* *INDENT-ON* */
303 
304 clib_error_t *
306 {
307  return 0;
308 }
309 
311 
312 /*
313  * fd.io coding-style-patch-verification: ON
314  *
315  * Local Variables:
316  * eval: (c-set-style "gnu")
317  * End:
318  */
unformat_function_t unformat_vnet_hw_interface
struct vring_used * used
Definition: virtio.h:77
vmrglw vmrglh hi
#define vec_foreach_index(var, v)
Iterate over vector indices.
virtio_if_t * interfaces
Definition: virtio.h:124
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
ip4_address_t host_ip4_addr
Definition: tap.h:36
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static clib_error_t * tap_show_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:159
#define NULL
Definition: clib.h:55
#define foreach_virtio_net_features
Definition: virtio.h:21
int kick_fd
Definition: virtio.h:81
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u8 host_ip6_prefix_len
Definition: tap.h:39
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:518
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
unformat_function_t unformat_vnet_sw_interface
int call_fd
Definition: virtio.h:82
virtio_vring_t * vrings
Definition: virtio.h:103
format_function_t format_vnet_sw_if_index_name
clib_error_t * tap_cli_init(vlib_main_t *vm)
Definition: cli.c:305
static clib_error_t * tap_delete_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:111
struct vring_avail * avail
Definition: virtio.h:78
u64 features
Definition: virtio.h:105
u32 hw_if_index
Definition: virtio.h:98
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:438
unformat_function_t unformat_ip4_address
Definition: format.h:76
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
int tap_delete_if(vlib_main_t *vm, u32 sw_if_index)
Definition: tap.c:408
#define clib_error_return(e, args...)
Definition: error.h:99
u8 host_mac_addr[6]
Definition: tap.h:34
unformat_function_t unformat_line_input
Definition: format.h:281
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:459
int tap_fd
Definition: virtio.h:102
struct _unformat_input_t unformat_input_t
ip6_address_t host_ip6_addr
Definition: tap.h:38
u8 host_ip4_prefix_len
Definition: tap.h:37
u16 desc_next
Definition: virtio.h:80
unformat_function_t unformat_ip6_address
Definition: format.h:94
u16 last_used_idx
Definition: virtio.h:88
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vm
Definition: buffer.c:283
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
u8 * net_ns
Definition: virtio.h:111
u32 flags
Definition: virtio.h:93
static clib_error_t * tap_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:34
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
u8 * host_bridge
Definition: tap.h:35
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:227
unsigned int u32
Definition: types.h:88
u64 remote_features
Definition: virtio.h:105
u8 * host_if_name
Definition: tap.h:33
virtio_main_t virtio_main
Definition: virtio.c:35
void tap_create_if(vlib_main_t *vm, tap_create_if_args_t *args)
Definition: tap.c:80
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
int fd
Definition: virtio.h:101
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
u8 mac_addr[6]
Definition: tap.h:29
clib_error_t * error
Definition: tap.h:43
u32 sw_if_index
Definition: virtio.h:99
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
struct vring_desc * desc
Definition: virtio.h:76
u8 * host_namespace
Definition: tap.h:32
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:680
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
u16 desc_in_use
Definition: virtio.h:79
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
u8 * host_if_name
Definition: virtio.h:110