blob: 7dd525b169d41941cf3804911122ea30bb473d79 [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>
Damjan Marion91c6ef72017-12-01 13:34:24 +010025#include <vnet/ip/ip4_packet.h>
26#include <vnet/ip/ip6_packet.h>
27#include <vnet/ip/format.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020028#include <linux/virtio_net.h>
29#include <linux/vhost.h>
30#include <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010031#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020032
33static clib_error_t *
34tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
35 vlib_cli_command_t * cmd)
36{
37 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion8389fb92017-10-13 18:29:53 +020038 tap_create_if_args_t args = { 0 };
Damjan Marion91c6ef72017-12-01 13:34:24 +010039 int ip_addr_set = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +020040
Damjan Marion2df39092017-12-04 20:03:37 +010041 args.id = ~0;
Damjan Marion8389fb92017-10-13 18:29:53 +020042
Damjan Marion2df39092017-12-04 20:03:37 +010043 /* Get a line of input. */
44 if (unformat_user (input, unformat_line_input, line_input))
Damjan Marion8389fb92017-10-13 18:29:53 +020045 {
Damjan Marion2df39092017-12-04 20:03:37 +010046 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",
56 unformat_ethernet_address, args.host_mac_addr))
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",
61 unformat_ip4_address, &args.host_ip4_addr,
62 &args.host_ip4_prefix_len))
63 ip_addr_set = 1;
64 else if (unformat (line_input, "host-ip6-addr %U/%d",
65 unformat_ip6_address, &args.host_ip6_addr,
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",
73 unformat_ethernet_address, args.mac_addr))
74 args.mac_addr_set = 1;
75 else
Swarup Nayak76dc22c2017-12-05 09:46:17 +053076 {
77 unformat_free (line_input);
78 return clib_error_return (0, "unknown input `%U'",
79 format_unformat_error, input);
80 }
Damjan Marion2df39092017-12-04 20:03:37 +010081 }
82 unformat_free (line_input);
Damjan Marion8389fb92017-10-13 18:29:53 +020083 }
Damjan Marion8389fb92017-10-13 18:29:53 +020084
Damjan Marion91c6ef72017-12-01 13:34:24 +010085 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);
Damjan Marion8389fb92017-10-13 18:29:53 +020090
Damjan Marion2df39092017-12-04 20:03:37 +010091 vec_free (args.host_if_name);
Damjan Marion91c6ef72017-12-01 13:34:24 +010092 vec_free (args.host_namespace);
93 vec_free (args.host_bridge);
Damjan Marion8389fb92017-10-13 18:29:53 +020094
Damjan Marion91c6ef72017-12-01 13:34:24 +010095 return args.error;
Damjan Marion8389fb92017-10-13 18:29:53 +020096
Damjan Marion8389fb92017-10-13 18:29:53 +020097}
98
99/* *INDENT-OFF* */
100VLIB_CLI_COMMAND (tap_create_command, static) = {
101 .path = "create tap",
Damjan Marion2df39092017-12-04 20:03:37 +0100102 .short_help = "create tap {id <if-id>} [hw-addr <mac-address>] "
Damjan Marion91c6ef72017-12-01 13:34:24 +0100103 "[rx-ring-size <size>] [tx-ring-size <size>] [host-ns <netns>] "
104 "[host-bridge <bridge-name>] [host-ip4-addr <ip4addr/mask>] "
Damjan Marion2df39092017-12-04 20:03:37 +0100105 "[host-ip6-addr <ip6-addr] [host-if-name <name>]",
Damjan Marion8389fb92017-10-13 18:29:53 +0200106 .function = tap_create_command_fn,
107};
108/* *INDENT-ON* */
109
110static clib_error_t *
111tap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
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* */
150VLIB_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
158static clib_error_t *
159tap_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
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, },
179 foreach_virtio_net_features
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, },
187 foreach_virtio_if_flag
188#undef _
189 {.str = NULL}
190 };
191
192 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
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 {
218 vnet_hw_interface_t *hi =
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);
Damjan Marion2df39092017-12-04 20:03:37 +0100223 if (vif->host_if_name)
224 vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
Damjan Marion8389fb92017-10-13 18:29:53 +0200225 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, " qsz %d, last_used_idx %d, desc_in_use %d",
262 vring->size, vring->last_used_idx,
263 vring->desc_in_use);
264 vlib_cli_output (vm,
265 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
266 vring->avail->flags, vring->avail->idx,
267 vring->used->flags, vring->used->idx);
268 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
269 vring->call_fd);
270 if (show_descr)
271 {
272 vlib_cli_output (vm, "\n descriptor table:\n");
273 vlib_cli_output (vm,
274 " id addr len flags next user_addr\n");
275 vlib_cli_output (vm,
276 " ===== ================== ===== ====== ===== ==================\n");
277 vring = vif->vrings;
278 for (j = 0; j < vring->size; j++)
279 {
280 struct vring_desc *desc = &vring->desc[j];
281 vlib_cli_output (vm,
282 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
283 j, desc->addr,
284 desc->len,
285 desc->flags, desc->next, desc->addr);
286 }
287 }
288 }
289 }
290done:
291 vec_free (hw_if_indices);
292 return error;
293}
294
295/* *INDENT-OFF* */
296VLIB_CLI_COMMAND (tap_show_command, static) = {
297 .path = "show tap",
298 .short_help = "show tap {<interface>] [descriptors]",
299 .function = tap_show_command_fn,
300};
301/* *INDENT-ON* */
302
303clib_error_t *
304tap_cli_init (vlib_main_t * vm)
305{
306 return 0;
307}
308
309VLIB_INIT_FUNCTION (tap_cli_init);
310
311/*
312 * fd.io coding-style-patch-verification: ON
313 *
314 * Local Variables:
315 * eval: (c-set-style "gnu")
316 * End:
317 */