blob: 096a2c46970188bf9c04c9195bdcc8bed69258ad [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 <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010029#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020030
31static clib_error_t *
32tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
33 vlib_cli_command_t * cmd)
34{
35 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion8389fb92017-10-13 18:29:53 +020036 tap_create_if_args_t args = { 0 };
Damjan Marion91c6ef72017-12-01 13:34:24 +010037 int ip_addr_set = 0;
Damjan Marion0ba86cb2019-11-08 15:15:11 +010038 u32 tmp;
Damjan Marion8389fb92017-10-13 18:29:53 +020039
Damjan Marion2df39092017-12-04 20:03:37 +010040 args.id = ~0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020041 args.tap_flags = 0;
Mohsin Kazmic5d53272019-07-01 10:26:43 +020042 args.rv = -1;
Damjan Marion7c6102b2019-11-08 17:59:56 +010043 args.num_rx_queues = 1;
Nathan Skrzypczak40edaf62021-12-15 18:45:59 +010044 args.num_tx_queues = 1;
Damjan Marion8389fb92017-10-13 18:29:53 +020045
Damjan Marion2df39092017-12-04 20:03:37 +010046 /* Get a line of input. */
47 if (unformat_user (input, unformat_line_input, line_input))
Damjan Marion8389fb92017-10-13 18:29:53 +020048 {
Damjan Marion2df39092017-12-04 20:03:37 +010049 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
50 {
51 if (unformat (line_input, "id %u", &args.id))
52 ;
53 else
54 if (unformat (line_input, "host-if-name %s", &args.host_if_name))
55 ;
56 else if (unformat (line_input, "host-ns %s", &args.host_namespace))
57 ;
58 else if (unformat (line_input, "host-mac-addr %U",
Mohsin Kazmi30397532020-01-30 13:36:02 +010059 unformat_ethernet_address,
60 args.host_mac_addr.bytes))
Damjan Marion2df39092017-12-04 20:03:37 +010061 ;
62 else if (unformat (line_input, "host-bridge %s", &args.host_bridge))
63 ;
64 else if (unformat (line_input, "host-ip4-addr %U/%d",
65 unformat_ip4_address, &args.host_ip4_addr,
66 &args.host_ip4_prefix_len))
67 ip_addr_set = 1;
Damjan Marion7866c452018-01-18 13:35:11 +010068 else if (unformat (line_input, "host-ip4-gw %U",
69 unformat_ip4_address, &args.host_ip4_gw))
70 args.host_ip4_gw_set = 1;
Damjan Marion2df39092017-12-04 20:03:37 +010071 else if (unformat (line_input, "host-ip6-addr %U/%d",
72 unformat_ip6_address, &args.host_ip6_addr,
73 &args.host_ip6_prefix_len))
74 ip_addr_set = 1;
Damjan Marion7866c452018-01-18 13:35:11 +010075 else if (unformat (line_input, "host-ip6-gw %U",
76 unformat_ip6_address, &args.host_ip6_gw))
77 args.host_ip6_gw_set = 1;
Damjan Marion7c6102b2019-11-08 17:59:56 +010078 else if (unformat (line_input, "num-rx-queues %d", &tmp))
79 args.num_rx_queues = tmp;
Nathan Skrzypczak40edaf62021-12-15 18:45:59 +010080 else if (unformat (line_input, "num-tx-queues %d", &tmp))
81 args.num_tx_queues = tmp;
Damjan Marion0ba86cb2019-11-08 15:15:11 +010082 else if (unformat (line_input, "rx-ring-size %d", &tmp))
83 args.rx_ring_sz = tmp;
84 else if (unformat (line_input, "tx-ring-size %d", &tmp))
85 args.tx_ring_sz = tmp;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +020086 else
87 if (unformat
88 (line_input, "host-mtu-size %d", &args.host_mtu_size))
89 args.host_mtu_set = 1;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020090 else if (unformat (line_input, "no-gso"))
91 args.tap_flags &= ~TAP_FLAG_GSO;
92 else if (unformat (line_input, "gso"))
93 args.tap_flags |= TAP_FLAG_GSO;
Mohsin Kazmid88fc0f2020-04-30 19:05:56 +020094 else if (unformat (line_input, "gro-coalesce"))
95 args.tap_flags |= TAP_FLAG_GRO_COALESCE;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +010096 else if (unformat (line_input, "csum-offload"))
97 args.tap_flags |= TAP_FLAG_CSUM_OFFLOAD;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +000098 else if (unformat (line_input, "persist"))
99 args.tap_flags |= TAP_FLAG_PERSIST;
100 else if (unformat (line_input, "attach"))
101 args.tap_flags |= TAP_FLAG_ATTACH;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200102 else if (unformat (line_input, "tun"))
103 args.tap_flags |= TAP_FLAG_TUN;
Mohsin Kazmi50bd1652020-08-26 11:07:48 +0200104 else if (unformat (line_input, "packed"))
105 args.tap_flags |= TAP_FLAG_PACKED;
106 else if (unformat (line_input, "in-order"))
107 args.tap_flags |= TAP_FLAG_IN_ORDER;
Damjan Marion2df39092017-12-04 20:03:37 +0100108 else if (unformat (line_input, "hw-addr %U",
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200109 unformat_ethernet_address, args.mac_addr.bytes))
Damjan Marion2df39092017-12-04 20:03:37 +0100110 args.mac_addr_set = 1;
111 else
Swarup Nayak76dc22c2017-12-05 09:46:17 +0530112 {
113 unformat_free (line_input);
114 return clib_error_return (0, "unknown input `%U'",
115 format_unformat_error, input);
116 }
Damjan Marion2df39092017-12-04 20:03:37 +0100117 }
118 unformat_free (line_input);
Damjan Marion8389fb92017-10-13 18:29:53 +0200119 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200120
Damjan Marion91c6ef72017-12-01 13:34:24 +0100121 if (ip_addr_set && args.host_bridge)
122 return clib_error_return (0, "Please specify either host ip address or "
123 "host bridge");
124
125 tap_create_if (vm, &args);
Damjan Marion8389fb92017-10-13 18:29:53 +0200126
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200127 if (!args.rv)
128 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
129 vnet_get_main (), args.sw_if_index);
130
Damjan Marion2df39092017-12-04 20:03:37 +0100131 vec_free (args.host_if_name);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100132 vec_free (args.host_namespace);
133 vec_free (args.host_bridge);
Damjan Marion8389fb92017-10-13 18:29:53 +0200134
Damjan Marion91c6ef72017-12-01 13:34:24 +0100135 return args.error;
Damjan Marion8389fb92017-10-13 18:29:53 +0200136
Damjan Marion8389fb92017-10-13 18:29:53 +0200137}
138
139/* *INDENT-OFF* */
140VLIB_CLI_COMMAND (tap_create_command, static) = {
141 .path = "create tap",
Nathan Skrzypczak40edaf62021-12-15 18:45:59 +0100142 .short_help =
143 "create tap {id <if-id>} [hw-addr <mac-address>] "
144 "[num-rx-queues <n>] [num-tx-queues <n>] [rx-ring-size <size>] "
145 "[tx-ring-size <size>] [host-ns <netns>] [host-bridge <bridge-name>] "
Mohsin Kazmi28adbb32020-04-29 13:10:08 +0200146 "[host-ip4-addr <ip4addr/mask>] [host-ip6-addr <ip6-addr>] "
147 "[host-ip4-gw <ip4-addr>] [host-ip6-gw <ip6-addr>] "
148 "[host-mac-addr <host-mac-address>] [host-if-name <name>] "
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +0200149 "[host-mtu-size <size>] [no-gso|gso [gro-coalesce]|csum-offload] "
Mohsin Kazmi50bd1652020-08-26 11:07:48 +0200150 "[persist] [attach] [tun] [packed] [in-order]",
Damjan Marion8389fb92017-10-13 18:29:53 +0200151 .function = tap_create_command_fn,
152};
153/* *INDENT-ON* */
154
155static clib_error_t *
156tap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
157 vlib_cli_command_t * cmd)
158{
159 unformat_input_t _line_input, *line_input = &_line_input;
160 u32 sw_if_index = ~0;
161 vnet_main_t *vnm = vnet_get_main ();
162 int rv;
163
164 /* Get a line of input. */
165 if (!unformat_user (input, unformat_line_input, line_input))
166 return clib_error_return (0, "Missing <interface>");
167
168 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
169 {
170 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
171 ;
172 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
173 vnm, &sw_if_index))
174 ;
175 else
176 return clib_error_return (0, "unknown input `%U'",
177 format_unformat_error, input);
178 }
179 unformat_free (line_input);
180
181 if (sw_if_index == ~0)
182 return clib_error_return (0,
183 "please specify interface name or sw_if_index");
184
185 rv = tap_delete_if (vm, sw_if_index);
186 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
187 return clib_error_return (0, "not a tap interface");
188 else if (rv != 0)
189 return clib_error_return (0, "error on deleting tap interface");
190
191 return 0;
192}
193
194/* *INDENT-OFF* */
195VLIB_CLI_COMMAND (tap_delete__command, static) =
196{
197 .path = "delete tap",
198 .short_help = "delete tap {<interface> | sw_if_index <sw_idx>}",
199 .function = tap_delete_command_fn,
200};
201/* *INDENT-ON* */
202
203static clib_error_t *
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100204tap_offload_command_fn (vlib_main_t * vm, unformat_input_t * input,
205 vlib_cli_command_t * cmd)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200206{
207 unformat_input_t _line_input, *line_input = &_line_input;
208 u32 sw_if_index = ~0;
209 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200210 int gso_enable = 0, gso_disable = 0, is_gro_coalesce = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100211 int csum_offload_enable = 0, csum_offload_disable = 0;
212 int rv = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200213
214 /* Get a line of input. */
215 if (!unformat_user (input, unformat_line_input, line_input))
216 return clib_error_return (0, "Missing <interface>");
217
218 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
219 {
220 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
221 ;
222 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
223 vnm, &sw_if_index))
224 ;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100225 else if (unformat (line_input, "gso-enable"))
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200226 {
227 gso_enable = 1;
228 if (unformat (line_input, "gro-coalesce"))
229 is_gro_coalesce = 1;
230 }
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100231 else if (unformat (line_input, "gso-disable"))
232 gso_disable = 1;
233 else if (unformat (line_input, "csum-offload-enable"))
234 csum_offload_enable = 1;
235 else if (unformat (line_input, "csum-offload-disable"))
236 csum_offload_disable = 1;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200237 else
238 return clib_error_return (0, "unknown input `%U'",
239 format_unformat_error, input);
240 }
241 unformat_free (line_input);
242
243 if (sw_if_index == ~0)
244 return clib_error_return (0,
245 "please specify interface name or sw_if_index");
246
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100247 if (gso_enable)
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200248 rv = tap_gso_enable_disable (vm, sw_if_index, 1, is_gro_coalesce);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100249 else if (csum_offload_enable)
250 rv = tap_csum_offload_enable_disable (vm, sw_if_index, 1);
251 else if (gso_disable)
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200252 rv = tap_gso_enable_disable (vm, sw_if_index, 0, 0);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100253 else if (csum_offload_disable)
254 rv = tap_csum_offload_enable_disable (vm, sw_if_index, 0);
255
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200256 if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
257 return clib_error_return (0, "not a tap interface");
258 else if (rv != 0)
259 return clib_error_return (0, "error on configuring GSO on tap interface");
260
261 return 0;
262}
263
264/* *INDENT-OFF* */
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100265VLIB_CLI_COMMAND (tap_offload_command, static) =
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200266{
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100267 .path = "set tap offload",
268 .short_help = "set tap offload {<interface> | sw_if_index <sw_idx>}"
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200269 " <gso-enable [gro-coalesce] | gso-disable | csum-offload-enable |"
270 "csum-offload-disable>",
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100271 .function = tap_offload_command_fn,
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200272};
273/* *INDENT-ON* */
274
275static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +0200276tap_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
277 vlib_cli_command_t * cmd)
278{
279 virtio_main_t *mm = &virtio_main;
280 virtio_if_t *vif;
281 vnet_main_t *vnm = vnet_get_main ();
282 int show_descr = 0;
283 clib_error_t *error = 0;
284 u32 hw_if_index, *hw_if_indices = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200285
286 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
287 {
288 if (unformat
289 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
290 vec_add1 (hw_if_indices, hw_if_index);
291 else if (unformat (input, "descriptors"))
292 show_descr = 1;
293 else
294 {
295 error = clib_error_return (0, "unknown input `%U'",
296 format_unformat_error, input);
297 goto done;
298 }
299 }
300
301 if (vec_len (hw_if_indices) == 0)
302 {
303 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100304 pool_foreach (vif, mm->interfaces)
Damjan Marion8389fb92017-10-13 18:29:53 +0200305 vec_add1 (hw_if_indices, vif->hw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200306 /* *INDENT-ON* */
307 }
308
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200309 virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_TAP);
310
Damjan Marion8389fb92017-10-13 18:29:53 +0200311done:
312 vec_free (hw_if_indices);
313 return error;
314}
315
316/* *INDENT-OFF* */
317VLIB_CLI_COMMAND (tap_show_command, static) = {
318 .path = "show tap",
319 .short_help = "show tap {<interface>] [descriptors]",
320 .function = tap_show_command_fn,
321};
322/* *INDENT-ON* */
323
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200324static clib_error_t *
325tun_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
326 vlib_cli_command_t * cmd)
327{
328 virtio_main_t *mm = &virtio_main;
329 virtio_if_t *vif;
330 vnet_main_t *vnm = vnet_get_main ();
331 int show_descr = 0;
332 clib_error_t *error = 0;
333 u32 hw_if_index, *hw_if_indices = 0;
334
335 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
336 {
337 if (unformat
338 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
339 vec_add1 (hw_if_indices, hw_if_index);
340 else if (unformat (input, "descriptors"))
341 show_descr = 1;
342 else
343 {
344 error = clib_error_return (0, "unknown input `%U'",
345 format_unformat_error, input);
346 goto done;
347 }
348 }
349
350 if (vec_len (hw_if_indices) == 0)
351 {
352 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100353 pool_foreach (vif, mm->interfaces)
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200354 vec_add1 (hw_if_indices, vif->hw_if_index);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200355 /* *INDENT-ON* */
356 }
357
358 virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_TUN);
359
360done:
361 vec_free (hw_if_indices);
362 return error;
363}
364
365/* *INDENT-OFF* */
366VLIB_CLI_COMMAND (tun_show_command, static) = {
367 .path = "show tun",
368 .short_help = "show tun {<interface>] [descriptors]",
369 .function = tun_show_command_fn,
370};
371/* *INDENT-ON* */
372
Damjan Marion8389fb92017-10-13 18:29:53 +0200373clib_error_t *
374tap_cli_init (vlib_main_t * vm)
375{
376 return 0;
377}
378
379VLIB_INIT_FUNCTION (tap_cli_init);
380
381/*
382 * fd.io coding-style-patch-verification: ON
383 *
384 * Local Variables:
385 * eval: (c-set-style "gnu")
386 * End:
387 */