blob: 0c1b75f8c4e388694e983f557e47571d3366869b [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
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 <linux/virtio_net.h>
26#include <linux/vhost.h>
27#include <vnet/devices/virtio/virtio.h>
28#include <vnet/devices/virtio/tap.h>
29
30static clib_error_t *
31tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
32 vlib_cli_command_t * cmd)
33{
34 unformat_input_t _line_input, *line_input = &_line_input;
35 int rv;
36 tap_create_if_args_t args = { 0 };
37
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>");
41
42 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
43 {
44 if (unformat (line_input, "name %s", &args.name))
45 ;
46 else if (unformat (line_input, "host-ns %s", &args.net_ns))
47 ;
48 else if (unformat (line_input, "rx-ring-size %d", &args.rx_ring_sz))
49 ;
50 else if (unformat (line_input, "tx-ring-size %d", &args.tx_ring_sz))
51 ;
52 else if (unformat (line_input, "hw-addr %U",
53 unformat_ethernet_address, args.hw_addr))
54 args.hw_addr_set = 1;
55 else
56 return clib_error_return (0, "unknown input `%U'",
57 format_unformat_error, input);
58 }
59 unformat_free (line_input);
60
61 rv = tap_create_if (vm, &args);
62
63 vec_free (args.name);
64
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"
71 " features");
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");
78 else if (rv != 0)
79 return clib_error_return (0, "error on creating tap interface");
80
81 return 0;
82}
83
84/* *INDENT-OFF* */
85VLIB_CLI_COMMAND (tap_create_command, static) = {
86 .path = "create tap",
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,
90};
91/* *INDENT-ON* */
92
93static clib_error_t *
94tap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
95 vlib_cli_command_t * cmd)
96{
97 unformat_input_t _line_input, *line_input = &_line_input;
98 u32 sw_if_index = ~0;
99 vnet_main_t *vnm = vnet_get_main ();
100 int rv;
101
102 /* Get a line of input. */
103 if (!unformat_user (input, unformat_line_input, line_input))
104 return clib_error_return (0, "Missing <interface>");
105
106 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
107 {
108 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
109 ;
110 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
111 vnm, &sw_if_index))
112 ;
113 else
114 return clib_error_return (0, "unknown input `%U'",
115 format_unformat_error, input);
116 }
117 unformat_free (line_input);
118
119 if (sw_if_index == ~0)
120 return clib_error_return (0,
121 "please specify interface name or sw_if_index");
122
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");
126 else if (rv != 0)
127 return clib_error_return (0, "error on deleting tap interface");
128
129 return 0;
130}
131
132/* *INDENT-OFF* */
133VLIB_CLI_COMMAND (tap_delete__command, static) =
134{
135 .path = "delete tap",
136 .short_help = "delete tap {<interface> | sw_if_index <sw_idx>}",
137 .function = tap_delete_command_fn,
138};
139/* *INDENT-ON* */
140
141static clib_error_t *
142tap_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
143 vlib_cli_command_t * cmd)
144{
145 virtio_main_t *mm = &virtio_main;
146 virtio_if_t *vif;
147 vnet_main_t *vnm = vnet_get_main ();
148 int show_descr = 0;
149 clib_error_t *error = 0;
150 u32 hw_if_index, *hw_if_indices = 0;
151 virtio_vring_t *vring;
152 int i, j;
153 struct feat_struct
154 {
155 u8 bit;
156 char *str;
157 };
158 struct feat_struct *feat_entry;
159
160 static struct feat_struct feat_array[] = {
161#define _(s,b) { .str = #s, .bit = b, },
162 foreach_virtio_net_features
163#undef _
164 {.str = NULL}
165 };
166
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
171#undef _
172 {.str = NULL}
173 };
174
175 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
176 {
177 if (unformat
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"))
181 show_descr = 1;
182 else
183 {
184 error = clib_error_return (0, "unknown input `%U'",
185 format_unformat_error, input);
186 goto done;
187 }
188 }
189
190 if (vec_len (hw_if_indices) == 0)
191 {
192 /* *INDENT-OFF* */
193 pool_foreach (vif, mm->interfaces,
194 vec_add1 (hw_if_indices, vif->hw_if_index);
195 );
196 /* *INDENT-ON* */
197 }
198
199 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
200 {
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);
206 if (vif->name)
207 vlib_cli_output (vm, " name \"%s\"", vif->name);
208 if (vif->net_ns)
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)
213 {
214 if (vif->flags & (1ULL << flag_entry->bit))
215 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
216 flag_entry->bit);
217 flag_entry++;
218 }
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)
224 {
225 if (vif->features & (1ULL << feat_entry->bit))
226 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
227 feat_entry->bit);
228 feat_entry++;
229 }
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)
233 {
234 if (vif->remote_features & (1ULL << feat_entry->bit))
235 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
236 feat_entry->bit);
237 feat_entry++;
238 }
239 vec_foreach_index (i, vif->vrings)
240 {
241 // RX = 0, TX = 1
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,
246 vring->desc_in_use);
247 vlib_cli_output (vm,
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,
252 vring->call_fd);
253 if (show_descr)
254 {
255 vlib_cli_output (vm, "\n descriptor table:\n");
256 vlib_cli_output (vm,
257 " id addr len flags next user_addr\n");
258 vlib_cli_output (vm,
259 " ===== ================== ===== ====== ===== ==================\n");
260 vring = vif->vrings;
261 for (j = 0; j < vring->size; j++)
262 {
263 struct vring_desc *desc = &vring->desc[j];
264 vlib_cli_output (vm,
265 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
266 j, desc->addr,
267 desc->len,
268 desc->flags, desc->next, desc->addr);
269 }
270 }
271 }
272 }
273done:
274 vec_free (hw_if_indices);
275 return error;
276}
277
278/* *INDENT-OFF* */
279VLIB_CLI_COMMAND (tap_show_command, static) = {
280 .path = "show tap",
281 .short_help = "show tap {<interface>] [descriptors]",
282 .function = tap_show_command_fn,
283};
284/* *INDENT-ON* */
285
286clib_error_t *
287tap_cli_init (vlib_main_t * vm)
288{
289 return 0;
290}
291
292VLIB_INIT_FUNCTION (tap_cli_init);
293
294/*
295 * fd.io coding-style-patch-verification: ON
296 *
297 * Local Variables:
298 * eval: (c-set-style "gnu")
299 * End:
300 */