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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #include <sys/ioctl.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <linux/virtio_net.h>
26 #include <linux/vhost.h>
27 #include <vnet/devices/virtio/virtio.h>
28 #include <vnet/devices/virtio/tap.h>
31 tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
32 vlib_cli_command_t * cmd)
34 unformat_input_t _line_input, *line_input = &_line_input;
36 tap_create_if_args_t args = { 0 };
38 /* Get a line of input. */
39 if (!unformat_user (input, unformat_line_input, line_input))
40 return clib_error_return (0, "Missing name <interface>");
42 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
44 if (unformat (line_input, "name %s", &args.name))
46 else if (unformat (line_input, "host-ns %s", &args.net_ns))
48 else if (unformat (line_input, "rx-ring-size %d", &args.rx_ring_sz))
50 else if (unformat (line_input, "tx-ring-size %d", &args.tx_ring_sz))
52 else if (unformat (line_input, "hw-addr %U",
53 unformat_ethernet_address, args.hw_addr))
56 return clib_error_return (0, "unknown input `%U'",
57 format_unformat_error, input);
59 unformat_free (line_input);
61 rv = tap_create_if (vm, &args);
65 if (rv == VNET_API_ERROR_SYSCALL_ERROR_1)
66 return clib_error_return_unix (0, "open '/dev/vhost-net'");
67 else if (rv == VNET_API_ERROR_SYSCALL_ERROR_2)
68 return clib_error_return_unix (0, "open '/dev/net/tun'");
69 else if (rv == VNET_API_ERROR_UNSUPPORTED)
70 return clib_error_return (0, "vhost-net backend doesn't support needed"
72 else if (rv == VNET_API_ERROR_NAMESPACE_CREATE)
73 return clib_error_return (0, "failed to create netlink namespace");
74 else if (rv == VNET_API_ERROR_VIRTIO_INIT)
75 return clib_error_return (0, "failed to init virtio ring");
76 else if (rv == VNET_API_ERROR_INVALID_REGISTRATION)
77 return clib_error_return (0, "failed to register interface");
79 return clib_error_return (0, "error on creating tap interface");
85 VLIB_CLI_COMMAND (tap_create_command, static) = {
87 .short_help = "create tap {name <if-name>} [hw-addr <mac-address>]"
88 "[rx-ring-size <size>] [tx-ring-size <size>] [host-ns <netns>]",
89 .function = tap_create_command_fn,
94 tap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
95 vlib_cli_command_t * cmd)
97 unformat_input_t _line_input, *line_input = &_line_input;
99 vnet_main_t *vnm = vnet_get_main ();
102 /* Get a line of input. */
103 if (!unformat_user (input, unformat_line_input, line_input))
104 return clib_error_return (0, "Missing <interface>");
106 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
108 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
110 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
114 return clib_error_return (0, "unknown input `%U'",
115 format_unformat_error, input);
117 unformat_free (line_input);
119 if (sw_if_index == ~0)
120 return clib_error_return (0,
121 "please specify interface name or sw_if_index");
123 rv = tap_delete_if (vm, sw_if_index);
124 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
125 return clib_error_return (0, "not a tap interface");
127 return clib_error_return (0, "error on deleting tap interface");
133 VLIB_CLI_COMMAND (tap_delete__command, static) =
135 .path = "delete tap",
136 .short_help = "delete tap {<interface> | sw_if_index <sw_idx>}",
137 .function = tap_delete_command_fn,
141 static clib_error_t *
142 tap_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
143 vlib_cli_command_t * cmd)
145 virtio_main_t *mm = &virtio_main;
147 vnet_main_t *vnm = vnet_get_main ();
149 clib_error_t *error = 0;
150 u32 hw_if_index, *hw_if_indices = 0;
151 virtio_vring_t *vring;
158 struct feat_struct *feat_entry;
160 static struct feat_struct feat_array[] = {
161 #define _(s,b) { .str = #s, .bit = b, },
162 foreach_virtio_net_features
167 struct feat_struct *flag_entry;
168 static struct feat_struct flags_array[] = {
169 #define _(b,e,s) { .bit = b, .str = s, },
170 foreach_virtio_if_flag
175 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
178 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
179 vec_add1 (hw_if_indices, hw_if_index);
180 else if (unformat (input, "descriptors"))
184 error = clib_error_return (0, "unknown input `%U'",
185 format_unformat_error, input);
190 if (vec_len (hw_if_indices) == 0)
193 pool_foreach (vif, mm->interfaces,
194 vec_add1 (hw_if_indices, vif->hw_if_index);
199 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
201 vnet_hw_interface_t *hi =
202 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
203 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
204 vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
205 vnm, vif->sw_if_index);
207 vlib_cli_output (vm, " name \"%s\"", vif->name);
209 vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
210 vlib_cli_output (vm, " flags 0x%x", vif->flags);
211 flag_entry = (struct feat_struct *) &flags_array;
212 while (flag_entry->str)
214 if (vif->flags & (1ULL << flag_entry->bit))
215 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
219 vlib_cli_output (vm, " fd %d", vif->fd);
220 vlib_cli_output (vm, " tap-fd %d", vif->tap_fd);
221 vlib_cli_output (vm, " features 0x%lx", vif->features);
222 feat_entry = (struct feat_struct *) &feat_array;
223 while (feat_entry->str)
225 if (vif->features & (1ULL << feat_entry->bit))
226 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
230 vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
231 feat_entry = (struct feat_struct *) &feat_array;
232 while (feat_entry->str)
234 if (vif->remote_features & (1ULL << feat_entry->bit))
235 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
239 vec_foreach_index (i, vif->vrings)
242 vring = vec_elt_at_index (vif->vrings, i);
243 vlib_cli_output (vm, " Virtqueue (%s)", (i & 1) ? "TX" : "RX");
244 vlib_cli_output (vm, " qsz %d, last_used_idx %d, desc_in_use %d",
245 vring->size, vring->last_used_idx,
248 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
249 vring->avail->flags, vring->avail->idx,
250 vring->used->flags, vring->used->idx);
251 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
255 vlib_cli_output (vm, "\n descriptor table:\n");
257 " id addr len flags next user_addr\n");
259 " ===== ================== ===== ====== ===== ==================\n");
261 for (j = 0; j < vring->size; j++)
263 struct vring_desc *desc = &vring->desc[j];
265 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
268 desc->flags, desc->next, desc->addr);
274 vec_free (hw_if_indices);
279 VLIB_CLI_COMMAND (tap_show_command, static) = {
281 .short_help = "show tap {<interface>] [descriptors]",
282 .function = tap_show_command_fn,
287 tap_cli_init (vlib_main_t * vm)
292 VLIB_INIT_FUNCTION (tap_cli_init);
295 * fd.io coding-style-patch-verification: ON
298 * eval: (c-set-style "gnu")