blob: 33d6e3bc84a0d06012b73735d58863913d81ed18 [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 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
Damjan Marion2df39092017-12-04 20:03:37 +010018#define _GNU_SOURCE
Damjan Marion8389fb92017-10-13 18:29:53 +020019#include <sys/types.h>
20#include <sys/stat.h>
Alexander Chernavina6c34a12020-09-04 09:24:20 -040021#include <sys/socket.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020022#include <fcntl.h>
23#include <net/if.h>
24#include <linux/if_tun.h>
25#include <sys/ioctl.h>
Alexander Chernavina6c34a12020-09-04 09:24:20 -040026#include <linux/ethtool.h>
27#include <linux/sockios.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020028#include <sys/eventfd.h>
Damjan Marion7c6102b2019-11-08 17:59:56 +010029#include <net/if_arp.h>
Damjan Marion7c6102b2019-11-08 17:59:56 +010030#include <limits.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020031
32#include <linux/netlink.h>
33#include <linux/rtnetlink.h>
34
35#include <vlib/vlib.h>
Lijian.Zhangba0da572019-08-21 17:51:16 +080036#include <vlib/physmem.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020037#include <vlib/unix/unix.h>
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +020038#include <vppinfra/linux/netns.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020039#include <vnet/ethernet/ethernet.h>
Damjan Marion91c6ef72017-12-01 13:34:24 +010040#include <vnet/ip/ip4_packet.h>
41#include <vnet/ip/ip6_packet.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010042#include <vnet/devices/netlink.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020043#include <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010044#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020045
Damjan Marion2df39092017-12-04 20:03:37 +010046tap_main_t tap_main;
47
Damjan Marion7c6102b2019-11-08 17:59:56 +010048#define tap_log_err(dev, f, ...) \
49 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
50#define tap_log_dbg(dev, f, ...) \
51 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
52
Damjan Marion8389fb92017-10-13 18:29:53 +020053#define _IOCTL(fd,a,...) \
54 if (ioctl (fd, a, __VA_ARGS__) < 0) \
55 { \
56 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
Damjan Marion7c6102b2019-11-08 17:59:56 +010057 tap_log_err (vif, "%U", format_clib_error, err); \
Damjan Marion8389fb92017-10-13 18:29:53 +020058 goto error; \
59 }
60
Mohsin Kazmi206acf82020-04-06 14:19:54 +020061 /* *INDENT-OFF* */
62VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) =
63{
64 .name = "tun-device",
65 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
66};
67 /* *INDENT-ON* */
68
Neale Ranns0cc23b72021-05-20 16:09:40 +000069#define TUN_MAX_PACKET_BYTES 65355
70#define TUN_MIN_PACKET_BYTES 64
71#define TUN_DEFAULT_PACKET_BYTES 1500
72
Damjan Marion8389fb92017-10-13 18:29:53 +020073static u32
74virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
75 u32 flags)
76{
77 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010078 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020079 return 0;
80}
81
Neale Rannscbe8d652018-04-27 04:42:47 -070082#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010083
Damjan Marion7c6102b2019-11-08 17:59:56 +010084static void
85tap_free (vlib_main_t * vm, virtio_if_t * vif)
86{
87 virtio_main_t *mm = &virtio_main;
88 tap_main_t *tm = &tap_main;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +000089 clib_error_t *err = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +010090 int i;
91
92 /* *INDENT-OFF* */
93 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
94 close (vif->vhost_fds[i]);
95 vec_foreach_index (i, vif->rxq_vrings)
96 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
97 vec_foreach_index (i, vif->txq_vrings)
98 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
99 /* *INDENT-ON* */
100
Mohsin Kazmi108b15b2020-10-28 19:35:53 +0100101 if (vif->tap_fds)
102 {
103 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
104 tap_log_dbg (vif, "TUNSETPERSIST: unset");
105 }
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000106error:
Aloys Augustin2857e782020-03-16 17:29:52 +0100107 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100108
109 vec_free (vif->vhost_fds);
110 vec_free (vif->rxq_vrings);
111 vec_free (vif->txq_vrings);
112 vec_free (vif->host_if_name);
113 vec_free (vif->net_ns);
114 vec_free (vif->host_bridge);
115 clib_error_free (vif->error);
116
117 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
118 clib_memset (vif, 0, sizeof (*vif));
119 pool_put (mm->interfaces, vif);
120}
121
Damjan Marion91c6ef72017-12-01 13:34:24 +0100122void
Damjan Marion8389fb92017-10-13 18:29:53 +0200123tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
124{
Damjan Marion7c6102b2019-11-08 17:59:56 +0100125 vlib_thread_main_t *thm = vlib_get_thread_main ();
Lijian.Zhangba0da572019-08-21 17:51:16 +0800126 vlib_physmem_main_t *vpm = &vm->physmem_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200127 vnet_main_t *vnm = vnet_get_main ();
128 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100129 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200130 vnet_sw_interface_t *sw;
131 vnet_hw_interface_t *hw;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000132 int i, num_vhost_queues;
Damjan Marion2df39092017-12-04 20:03:37 +0100133 int old_netns_fd = -1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200134 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000135 struct ifreq get_ifr = {.ifr_flags = 0 };
Damjan Marion8389fb92017-10-13 18:29:53 +0200136 size_t hdrsz;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200137 vhost_memory_t *vhost_mem = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200138 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100139 clib_error_t *err = 0;
Damjan Marion39807d02019-11-08 13:52:28 +0100140 unsigned int tap_features;
Aloys Augustin2857e782020-03-16 17:29:52 +0100141 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200142 char *host_if_name = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100143 unsigned int offload = 0;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000144 int sndbuf = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100145
146 if (args->id != ~0)
147 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700148 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100149 {
150 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
151 args->error = clib_error_return (0, "interface already exists");
152 return;
153 }
154 }
155 else
156 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700157 args->id = clib_bitmap_first_clear (tm->tap_ids);
158 }
Damjan Marion2df39092017-12-04 20:03:37 +0100159
Neale Rannscbe8d652018-04-27 04:42:47 -0700160 if (args->id > TAP_MAX_INSTANCE)
161 {
162 args->rv = VNET_API_ERROR_UNSPECIFIED;
163 args->error = clib_error_return (0, "cannot find free interface id");
164 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100165 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200166
Damjan Marion7c6102b2019-11-08 17:59:56 +0100167 pool_get_zero (vim->interfaces, vif);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200168
169 if (args->tap_flags & TAP_FLAG_TUN)
170 {
171 vif->type = VIRTIO_IF_TYPE_TUN;
172 ifr.ifr_flags |= IFF_TUN;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000173
174 /*
175 * From kernel 4.20, xdp support has been added in tun_sendmsg.
176 * If sndbuf == INT_MAX, vhost batches the packet and processes
177 * them using xdp data path for tun driver. It assumes packets
178 * are ethernet frames (It needs to be fixed).
179 * To avoid xdp data path in tun driver, sndbuf value should
180 * be < INT_MAX.
181 */
182 sndbuf = INT_MAX - 1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200183 }
184 else
185 {
186 vif->type = VIRTIO_IF_TYPE_TAP;
187 ifr.ifr_flags |= IFF_TAP;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000188 sndbuf = INT_MAX;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200189 }
190
Damjan Marion8389fb92017-10-13 18:29:53 +0200191 vif->dev_instance = vif - vim->interfaces;
Damjan Marion2df39092017-12-04 20:03:37 +0100192 vif->id = args->id;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100193 vif->num_txqs = thm->n_vlib_mains;
Aloys Augustin2857e782020-03-16 17:29:52 +0100194 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
Damjan Marion2df39092017-12-04 20:03:37 +0100195
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000196 if (args->tap_flags & TAP_FLAG_ATTACH)
197 {
198 if (args->host_if_name != NULL)
199 {
200 host_if_name = (char *) args->host_if_name;
201 clib_memcpy (ifr.ifr_name, host_if_name,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200202 clib_min (IFNAMSIZ, vec_len (host_if_name)));
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000203 }
204 else
205 {
206 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
207 err = clib_error_return (0, "host_if_name is not provided");
208 goto error;
209 }
210 if (args->host_namespace)
211 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200212 old_netns_fd = clib_netns_open (NULL /* self */);
213 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000214 {
215 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200216 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000217 args->host_namespace);
218 goto error;
219 }
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200220 if (clib_setns (nfd) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000221 {
222 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
223 args->error = clib_error_return_unix (0, "setns '%s'",
224 args->host_namespace);
225 goto error;
226 }
227 }
228 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100229
230 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200231 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100232 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
233 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
234 goto error;
235 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100236 vec_add1 (vif->tap_fds, tfd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100237 tap_log_dbg (vif, "open tap fd %d", tfd);
238
239 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
240 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
241 if ((tap_features & IFF_VNET_HDR) == 0)
242 {
243 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
244 args->error = clib_error_return (0, "vhost-net backend not available");
Damjan Marion8389fb92017-10-13 18:29:53 +0200245 goto error;
246 }
247
Damjan Marion7c6102b2019-11-08 17:59:56 +0100248 if ((tap_features & IFF_MULTI_QUEUE) == 0)
249 {
Aloys Augustin2857e782020-03-16 17:29:52 +0100250 if (vif->num_rxqs > 1)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100251 {
252 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
253 args->error = clib_error_return (0, "multiqueue not supported");
254 goto error;
255 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100256 vif->num_rxqs = vif->num_txqs = 1;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100257 }
258 else
259 ifr.ifr_flags |= IFF_MULTI_QUEUE;
260
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200261 hdrsz = sizeof (virtio_net_hdr_v1_t);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100262 if (args->tap_flags & TAP_FLAG_GSO)
263 {
264 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
265 vif->gso_enabled = 1;
266 }
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100267 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
268 {
269 offload = TUN_F_CSUM;
270 vif->csum_offload_enabled = 1;
271 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100272
273 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
274 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
275 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
276
277 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
278 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
279
280 if (!args->host_if_name)
281 host_if_name = ifr.ifr_ifrn.ifrn_name;
282 else
283 host_if_name = (char *) args->host_if_name;
284
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000285 /*
286 * unset the persistence when attaching to existing
287 * interface
288 */
289 if (args->tap_flags & TAP_FLAG_ATTACH)
290 {
291 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
292 tap_log_dbg (vif, "TUNSETPERSIST: unset");
293 }
294
295 /* set the persistence */
296 if (args->tap_flags & TAP_FLAG_PERSIST)
297 {
298 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
299 tap_log_dbg (vif, "TUNSETPERSIST: set");
300
301 /* verify persistence is set, read the flags */
302 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
303 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
304 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
305 {
306 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
307 args->error = clib_error_return (0, "persistence not supported");
308 goto error;
309 }
310 }
311
Aloys Augustin2857e782020-03-16 17:29:52 +0100312 /* create additional queues on the linux side.
313 * we create as many linux queue pairs as we have rx queues
314 */
315 for (i = 1; i < vif->num_rxqs; i++)
316 {
317 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
318 {
319 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
320 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
321 goto error;
322 }
323 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
324 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
325 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
326 vec_add1 (vif->tap_fds, qfd);
327 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100328
Aloys Augustin2857e782020-03-16 17:29:52 +0100329 for (i = 0; i < vif->num_rxqs; i++)
330 {
331 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
332 vif->tap_fds[i], hdrsz);
333 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100334
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000335 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
336 sndbuf);
337 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100338
Aloys Augustin2857e782020-03-16 17:29:52 +0100339 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
340 offload);
341 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
342
343 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
344 {
345 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
346 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
347 goto error;
348 }
349 }
350
351 /* open as many vhost-net fds as required and set ownership */
352 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
353 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100354 {
355 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
356 {
357 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
358 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
359 goto error;
360 }
361 vec_add1 (vif->vhost_fds, vfd);
362 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
363 _IOCTL (vfd, VHOST_SET_OWNER, 0);
364 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
365 }
366
367 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
368 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
369 vif->remote_features);
Damjan Marion8389fb92017-10-13 18:29:53 +0200370
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200371 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200372 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100373 args->rv = VNET_API_ERROR_UNSUPPORTED;
374 args->error = clib_error_return (0, "vhost-net backend doesn't support "
375 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200376 goto error;
377 }
378
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200379 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
380 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200381 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100382 args->rv = VNET_API_ERROR_UNSUPPORTED;
383 args->error = clib_error_return (0, "vhost-net backend doesn't support "
384 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200385 goto error;
386 }
387
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200388 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200389 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100390 args->rv = VNET_API_ERROR_UNSUPPORTED;
391 args->error = clib_error_return (0, "vhost-net backend doesn't support "
392 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200393 goto error;
394 }
395
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200396 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
397 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
398 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
399
400 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200401
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000402 if (!(args->tap_flags & TAP_FLAG_ATTACH))
Damjan Marion8389fb92017-10-13 18:29:53 +0200403 {
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000404 /* if namespace is specified, all further netlink messages should be executed
405 after we change our net namespace */
406 if (args->host_namespace)
Damjan Marion2df39092017-12-04 20:03:37 +0100407 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200408 old_netns_fd = clib_netns_open (NULL /* self */);
409 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000410 {
411 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200412 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000413 args->host_namespace);
414 goto error;
415 }
416 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
417 host_if_name);
418 if (args->error)
419 {
420 args->rv = VNET_API_ERROR_NETLINK_ERROR;
421 goto error;
422 }
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200423 if (clib_setns (nfd) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000424 {
425 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
426 args->error = clib_error_return_unix (0, "setns '%s'",
427 args->host_namespace);
428 goto error;
429 }
430 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
431 {
432 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
433 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200434 host_if_name);
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000435 goto error;
436 }
437 }
438 else if (host_if_name)
439 {
440 args->error =
441 vnet_netlink_set_link_name (vif->ifindex, host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100442 if (args->error)
443 {
444 args->rv = VNET_API_ERROR_NETLINK_ERROR;
445 goto error;
446 }
447 }
448 }
449
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200450 if (vif->type == VIRTIO_IF_TYPE_TAP)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200451 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200452 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
453 ethernet_mac_address_generate (args->host_mac_addr.bytes);
454 args->error = vnet_netlink_set_link_addr (vif->ifindex,
455 args->host_mac_addr.bytes);
456 if (args->error)
457 {
458 args->rv = VNET_API_ERROR_NETLINK_ERROR;
459 goto error;
460 }
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200461
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000462 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100463 {
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000464 args->error = vnet_netlink_set_link_master (vif->ifindex,
465 (char *)
466 args->host_bridge);
467 if (args->error)
468 {
469 args->rv = VNET_API_ERROR_NETLINK_ERROR;
470 goto error;
471 }
Damjan Marion91c6ef72017-12-01 13:34:24 +0100472 }
473 }
474
475 if (args->host_ip4_prefix_len)
476 {
477 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
478 &args->host_ip4_addr,
479 args->host_ip4_prefix_len);
480 if (args->error)
481 {
482 args->rv = VNET_API_ERROR_NETLINK_ERROR;
483 goto error;
484 }
485 }
486
487 if (args->host_ip6_prefix_len)
488 {
489 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
490 &args->host_ip6_addr,
491 args->host_ip6_prefix_len);
492 if (args->error)
493 {
494 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200495 goto error;
496 }
497 }
498
Damjan Marion2df39092017-12-04 20:03:37 +0100499 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
500 if (args->error)
501 {
502 args->rv = VNET_API_ERROR_NETLINK_ERROR;
503 goto error;
504 }
505
Damjan Marion7866c452018-01-18 13:35:11 +0100506 if (args->host_ip4_gw_set)
507 {
508 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
509 if (args->error)
510 {
511 args->rv = VNET_API_ERROR_NETLINK_ERROR;
512 goto error;
513 }
514 }
515
516 if (args->host_ip6_gw_set)
517 {
518 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
519 if (args->error)
520 {
521 args->rv = VNET_API_ERROR_NETLINK_ERROR;
522 goto error;
523 }
524 }
525
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200526 if (args->host_mtu_set)
527 {
528 args->error =
529 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
530 if (args->error)
531 {
532 args->rv = VNET_API_ERROR_NETLINK_ERROR;
533 goto error;
534 }
535 }
536 else if (tm->host_mtu_size != 0)
537 {
538 args->error =
539 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
540 if (args->error)
541 {
542 args->rv = VNET_API_ERROR_NETLINK_ERROR;
543 goto error;
544 }
545 args->host_mtu_set = 1;
546 args->host_mtu_size = tm->host_mtu_size;
547 }
548
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100549 /* switch back to old net namespace */
550 if (args->host_namespace)
551 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200552 if (clib_setns (old_netns_fd) == -1)
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100553 {
554 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
555 args->error = clib_error_return_unix (0, "setns '%s'",
556 args->host_namespace);
557 goto error;
558 }
559 }
560
Aloys Augustin2857e782020-03-16 17:29:52 +0100561 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100562 {
563 if (i < vif->num_rxqs && (args->error =
564 virtio_vring_init (vm, vif, RX_QUEUE (i),
565 args->rx_ring_sz)))
566 {
567 args->rv = VNET_API_ERROR_INIT_FAILED;
568 goto error;
569 }
570
571 if (i < vif->num_txqs && (args->error =
572 virtio_vring_init (vm, vif, TX_QUEUE (i),
573 args->tx_ring_sz)))
574 {
575 args->rv = VNET_API_ERROR_INIT_FAILED;
576 goto error;
577 }
578 }
579
580 /* setup features and memtable */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200581 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200582 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400583 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200584 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800585 vhost_mem->regions[0].memory_size = vpm->max_size;
586 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
587 vhost_mem->regions[0].userspace_addr =
588 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200589
Damjan Marion7c6102b2019-11-08 17:59:56 +0100590 for (i = 0; i < vhost_mem->nregions; i++)
591 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
592 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
593 vhost_mem->regions[0].memory_size,
594 vhost_mem->regions[0].guest_phys_addr,
595 vhost_mem->regions[0].userspace_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200596
Damjan Marion7c6102b2019-11-08 17:59:56 +0100597
Aloys Augustin2857e782020-03-16 17:29:52 +0100598 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion8389fb92017-10-13 18:29:53 +0200599 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100600 int fd = vif->vhost_fds[i];
601 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
602 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
603 fd, vif->features);
604 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
605 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200606 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100607
608 /* finish initializing queue pair */
Aloys Augustin2857e782020-03-16 17:29:52 +0100609 for (i = 0; i < num_vhost_queues * 2; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100610 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200611 vhost_vring_addr_t addr = { 0 };
612 vhost_vring_state_t state = { 0 };
613 vhost_vring_file_t file = { 0 };
Damjan Marion7c6102b2019-11-08 17:59:56 +0100614 virtio_vring_t *vring;
615 u16 qp = i >> 1;
616 int fd = vif->vhost_fds[qp];
617
618 if (i & 1)
619 {
620 if (qp >= vif->num_txqs)
621 continue;
622 vring = vec_elt_at_index (vif->txq_vrings, qp);
623 }
624 else
625 {
626 if (qp >= vif->num_rxqs)
627 continue;
628 vring = vec_elt_at_index (vif->rxq_vrings, qp);
629 }
630
631 addr.index = state.index = file.index = vring->queue_id & 1;
632 state.num = vring->size;
633 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
634 state.index, state.num);
635 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
636
637 addr.flags = 0;
638 addr.desc_user_addr = pointer_to_uword (vring->desc);
639 addr.avail_user_addr = pointer_to_uword (vring->avail);
640 addr.used_user_addr = pointer_to_uword (vring->used);
641
642 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
643 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
644 "used_user_addr 0x%lx", fd, addr.index,
645 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
646 addr.used_user_addr);
647 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
648
649 file.fd = vring->call_fd;
650 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
651 fd, file.index, file.fd);
652 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
653
654 file.fd = vring->kick_fd;
655 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
656 fd, file.index, file.fd);
657 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
658
Aloys Augustin2857e782020-03-16 17:29:52 +0100659 file.fd = vif->tap_fds[qp % vif->num_rxqs];
Damjan Marion7c6102b2019-11-08 17:59:56 +0100660 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
661 fd, file.index, file.fd);
662 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
663 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200664
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200665 if (vif->type == VIRTIO_IF_TYPE_TAP)
666 {
667 if (!args->mac_addr_set)
668 ethernet_mac_address_generate (args->mac_addr.bytes);
Damjan Marion8389fb92017-10-13 18:29:53 +0200669
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200670 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000671 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200672 }
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200673 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
Benoît Gannec30d87e2019-07-15 17:16:49 +0200674 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200675 vif->host_mtu_size = args->host_mtu_size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200676 vif->tap_flags = args->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200677 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100678 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
679 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
680 if (args->host_ip4_prefix_len)
681 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
682 if (args->host_ip6_prefix_len)
683 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
684
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200685 if (vif->type != VIRTIO_IF_TYPE_TUN)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100686 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200687 args->error =
688 ethernet_register_interface (vnm, virtio_device_class.index,
689 vif->dev_instance, vif->mac_addr,
690 &vif->hw_if_index,
691 virtio_eth_flag_change);
692 if (args->error)
693 {
694 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
695 goto error;
696 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200697
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200698 }
699 else
700 {
701 vif->hw_if_index = vnet_register_interface
702 (vnm, virtio_device_class.index,
703 vif->dev_instance /* device instance */ ,
704 tun_device_hw_interface_class.index, vif->dev_instance);
705
706 }
Neale Rannscbe8d652018-04-27 04:42:47 -0700707 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200708 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
709 vif->sw_if_index = sw->sw_if_index;
710 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200711 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200712 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100713 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200714 if (args->tap_flags & TAP_FLAG_GSO)
715 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100716 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
717 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
718 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100719 }
720 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
721 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100722 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
723 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200724 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200725 if ((args->tap_flags & TAP_FLAG_GSO)
726 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
727 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200728 virtio_set_packet_coalesce (vif);
729 }
Neale Ranns0cc23b72021-05-20 16:09:40 +0000730 if (vif->type == VIRTIO_IF_TYPE_TUN)
731 {
732 hw->max_supported_packet_bytes = TUN_MAX_PACKET_BYTES;
733 hw->min_packet_bytes = hw->min_supported_packet_bytes =
734 TUN_MIN_PACKET_BYTES;
735 hw->max_packet_bytes =
736 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES;
737 vnet_sw_interface_set_mtu (vnm, hw->sw_if_index, hw->max_packet_bytes);
738 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100739
Damjan Marion94100532020-11-06 23:25:57 +0100740 virtio_vring_set_rx_queues (vm, vif);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100741
Damjan Marion8389fb92017-10-13 18:29:53 +0200742 vif->per_interface_next_index = ~0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200743 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
744 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
745 VNET_HW_INTERFACE_FLAG_LINK_UP);
Matthew Smithbd50ed12020-07-24 13:38:03 -0500746 /*
747 * Host tun/tap driver link carrier state is "up" at creation. The
748 * driver never changes this unless the backend (VPP) changes it using
749 * TUNSETCARRIER ioctl(). See tap_set_carrier().
750 */
751 vif->host_carrier_up = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000752 vif->cxq_vring = NULL;
753
Damjan Marion8389fb92017-10-13 18:29:53 +0200754 goto done;
755
756error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100757 if (err)
758 {
759 ASSERT (args->error == 0);
760 args->error = err;
761 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
762 }
Benoît Gannec30d87e2019-07-15 17:16:49 +0200763
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100764 tap_log_err (vif, "%U", format_clib_error, args->error);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100765 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200766done:
767 if (vhost_mem)
768 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100769 if (old_netns_fd != -1)
770 close (old_netns_fd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100771 if (nfd != -1)
772 close (nfd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200773}
774
775int
776tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
777{
778 vnet_main_t *vnm = vnet_get_main ();
779 virtio_main_t *mm = &virtio_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200780 virtio_if_t *vif;
781 vnet_hw_interface_t *hw;
782
Dave Barach3940de32019-07-23 16:28:36 -0400783 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200784 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
785 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
786
787 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
788
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500789 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200790 return VNET_API_ERROR_INVALID_INTERFACE;
791
Damjan Marion8389fb92017-10-13 18:29:53 +0200792 /* bring down the interface */
793 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
794 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
795
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500796 if (vif->type == VIRTIO_IF_TYPE_TAP)
797 ethernet_delete_interface (vnm, vif->hw_if_index);
798 else /* VIRTIO_IF_TYPE_TUN */
799 vnet_delete_hw_interface (vnm, vif->hw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200800 vif->hw_if_index = ~0;
801
Damjan Marion7c6102b2019-11-08 17:59:56 +0100802 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200803
804 return 0;
805}
806
807int
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100808tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
809 int enable_disable)
810{
811 vnet_main_t *vnm = vnet_get_main ();
812 virtio_main_t *mm = &virtio_main;
813 virtio_if_t *vif;
814 vnet_hw_interface_t *hw;
815 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100816 int i = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100817
818 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
819
820 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
821 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
822
823 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
824
825 const unsigned int csum_offload_on = TUN_F_CSUM;
826 const unsigned int csum_offload_off = 0;
827 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100828 vec_foreach_index (i, vif->tap_fds)
829 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100830 vif->gso_enabled = 0;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200831 vif->packet_coalesce = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100832 vif->csum_offload_enabled = enable_disable ? 1 : 0;
833
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100834 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100835 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100836 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100837 }
838
839 if (enable_disable)
840 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100841 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100842 }
843 else
844 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100845 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100846 }
847
848error:
849 if (err)
850 {
851 clib_warning ("Error %s checksum offload on sw_if_index %d",
852 enable_disable ? "enabling" : "disabling", sw_if_index);
853 return VNET_API_ERROR_SYSCALL_ERROR_3;
854 }
855 return 0;
856}
857
858int
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200859tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
860 int is_packet_coalesce)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200861{
862 vnet_main_t *vnm = vnet_get_main ();
863 virtio_main_t *mm = &virtio_main;
864 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400865 vnet_hw_interface_t *hw;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200866 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100867 int i = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200868
Dave Barach3940de32019-07-23 16:28:36 -0400869 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
870
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200871 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
872 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
873
874 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
875
876 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
877 const unsigned int gso_off = 0;
878 unsigned int offload = enable_disable ? gso_on : gso_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100879 vec_foreach_index (i, vif->tap_fds)
880 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200881 vif->gso_enabled = enable_disable ? 1 : 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100882 vif->csum_offload_enabled = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200883 if (enable_disable)
884 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100885 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) == 0)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200886 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100887 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
888 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200889 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200890 if (is_packet_coalesce)
891 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200892 virtio_set_packet_coalesce (vif);
893 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200894 }
895 else
896 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100897 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200898 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100899 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
900 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200901 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200902 vif->packet_coalesce = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200903 }
904
905error:
906 if (err)
907 {
908 clib_warning ("Error %s gso on sw_if_index %d",
909 enable_disable ? "enabling" : "disabling", sw_if_index);
910 return VNET_API_ERROR_SYSCALL_ERROR_3;
911 }
912 return 0;
913}
914
915int
Damjan Marion8389fb92017-10-13 18:29:53 +0200916tap_dump_ifs (tap_interface_details_t ** out_tapids)
917{
918 vnet_main_t *vnm = vnet_get_main ();
919 virtio_main_t *mm = &virtio_main;
920 virtio_if_t *vif;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000921 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200922 vnet_hw_interface_t *hi;
923 tap_interface_details_t *r_tapids = NULL;
924 tap_interface_details_t *tapid = NULL;
925
926 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100927 pool_foreach (vif, mm->interfaces) {
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200928 if ((vif->type != VIRTIO_IF_TYPE_TAP)
929 && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200930 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200931 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400932 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100933 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200934 tapid->sw_if_index = vif->sw_if_index;
935 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
936 clib_memcpy(tapid->dev_name, hi->name,
Vladimir Isaev84f3d9f2020-09-18 14:43:29 +0300937 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000938 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
939 tapid->rx_ring_sz = vring->size;
940 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
941 tapid->tx_ring_sz = vring->size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200942 tapid->tap_flags = vif->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200943 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100944 if (vif->host_if_name)
945 {
946 clib_memcpy(tapid->host_if_name, vif->host_if_name,
947 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200948 vec_len (vif->host_if_name)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100949 }
950 if (vif->net_ns)
951 {
952 clib_memcpy(tapid->host_namespace, vif->net_ns,
953 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200954 vec_len (vif->net_ns)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100955 }
956 if (vif->host_bridge)
957 {
958 clib_memcpy(tapid->host_bridge, vif->host_bridge,
959 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200960 vec_len (vif->host_bridge)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100961 }
962 if (vif->host_ip4_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200963 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
Milan Lenco73e7f422017-12-14 10:04:25 +0100964 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
965 if (vif->host_ip6_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200966 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
Milan Lenco73e7f422017-12-14 10:04:25 +0100967 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200968 tapid->host_mtu_size = vif->host_mtu_size;
Damjan Marionb2c31b62020-12-13 21:47:40 +0100969 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200970 /* *INDENT-ON* */
971
972 *out_tapids = r_tapids;
973
974 return 0;
975}
976
Matthew Smithbd50ed12020-07-24 13:38:03 -0500977/*
978 * Set host tap/tun interface carrier state so it will appear to host
979 * applications that the interface's link state changed.
980 *
981 * If the kernel we're building against does not have support for the
982 * TUNSETCARRIER ioctl command, do nothing.
983 */
984int
985tap_set_carrier (u32 hw_if_index, u32 carrier_up)
986{
987 int ret = 0;
988#ifdef TUNSETCARRIER
989 vnet_main_t *vnm = vnet_get_main ();
990 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
991 virtio_main_t *mm = &virtio_main;
992 virtio_if_t *vif;
993 int *fd;
994
995 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
996 vec_foreach (fd, vif->tap_fds)
997 {
998 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
999 if (ret < 0)
1000 {
1001 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
1002 break;
1003 }
1004 }
1005 if (!ret)
1006 vif->host_carrier_up = (carrier_up != 0);
1007#endif
1008
1009 return ret;
1010}
1011
Damjan Marion8389fb92017-10-13 18:29:53 +02001012static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001013tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
1014{
1015 tap_main_t *tm = &tap_main;
1016
1017 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1018 {
1019 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
1020 ;
1021 else
1022 return clib_error_return (0, "unknown input `%U'",
1023 format_unformat_error, input);
1024 }
1025
1026 return 0;
1027}
1028
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001029/*
1030 * Set host tap/tun interface speed in Mbps.
1031 */
1032int
1033tap_set_speed (u32 hw_if_index, u32 speed)
1034{
1035 vnet_main_t *vnm = vnet_get_main ();
1036 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1037 virtio_main_t *mm = &virtio_main;
1038 virtio_if_t *vif;
1039 int old_netns_fd = -1;
1040 int nfd = -1;
1041 int ctl_fd = -1;
1042 struct ifreq ifr;
1043 struct ethtool_cmd ecmd;
1044 int ret = -1;
1045
1046 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1047
1048 if (vif->net_ns)
1049 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001050 old_netns_fd = clib_netns_open (NULL /* self */);
1051 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001052 {
1053 clib_warning ("Cannot open netns");
1054 goto done;
1055 }
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001056 if (clib_setns (nfd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001057 {
1058 clib_warning ("Cannot set ns");
1059 goto done;
1060 }
1061 }
1062
1063 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1064 {
1065 clib_warning ("Cannot open control socket");
1066 goto done;
1067 }
1068
1069 ecmd.cmd = ETHTOOL_GSET;
1070 clib_memset (&ifr, 0, sizeof (ifr));
1071 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1072 strlen ((const char *) vif->host_if_name));
1073 ifr.ifr_data = (void *) &ecmd;
1074 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1075 {
1076 clib_warning ("Cannot get device settings");
1077 goto done;
1078 }
1079
1080 if (ethtool_cmd_speed (&ecmd) != speed)
1081 {
1082 ecmd.cmd = ETHTOOL_SSET;
1083 ethtool_cmd_speed_set (&ecmd, speed);
1084 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1085 {
1086 clib_warning ("Cannot set device settings");
1087 goto done;
1088 }
1089 }
1090
1091done:
1092 if (old_netns_fd != -1)
1093 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001094 if (clib_setns (old_netns_fd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001095 {
1096 clib_warning ("Cannot set old ns");
1097 }
1098 close (old_netns_fd);
1099 }
1100 if (nfd != -1)
1101 close (nfd);
1102 if (ctl_fd != -1)
1103 close (ctl_fd);
1104
1105 return ret;
1106}
1107
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001108/* tap { host-mtu <size> } configuration. */
1109VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1110
1111static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +02001112tap_init (vlib_main_t * vm)
1113{
Damjan Marion2df39092017-12-04 20:03:37 +01001114 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001115 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -07001116
Damjan Marion07a38572018-01-21 06:44:18 -08001117 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001118 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -07001119
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001120 tm->host_mtu_size = 0;
1121
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001122 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +02001123}
1124
1125VLIB_INIT_FUNCTION (tap_init);
1126
1127/*
1128 * fd.io coding-style-patch-verification: ON
1129 *
1130 * Local Variables:
1131 * eval: (c-set-style "gnu")
1132 * End:
1133 */