blob: fa5eef19c0db3d371d81ad0c586c2bd68da4f607 [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>
21#include <fcntl.h>
22#include <net/if.h>
23#include <linux/if_tun.h>
24#include <sys/ioctl.h>
25#include <linux/virtio_net.h>
26#include <linux/vhost.h>
27#include <sys/eventfd.h>
Damjan Marion7c6102b2019-11-08 17:59:56 +010028#include <net/if_arp.h>
Damjan Marion2df39092017-12-04 20:03:37 +010029#include <sched.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>
38#include <vnet/ethernet/ethernet.h>
Damjan Marion91c6ef72017-12-01 13:34:24 +010039#include <vnet/ip/ip4_packet.h>
40#include <vnet/ip/ip6_packet.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010041#include <vnet/devices/netlink.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020042#include <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010043#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020044
Damjan Marion2df39092017-12-04 20:03:37 +010045tap_main_t tap_main;
46
Damjan Marion7c6102b2019-11-08 17:59:56 +010047#define tap_log_err(dev, f, ...) \
48 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
49#define tap_log_dbg(dev, f, ...) \
50 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
51
Damjan Marion8389fb92017-10-13 18:29:53 +020052#define _IOCTL(fd,a,...) \
53 if (ioctl (fd, a, __VA_ARGS__) < 0) \
54 { \
55 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
Damjan Marion7c6102b2019-11-08 17:59:56 +010056 tap_log_err (vif, "%U", format_clib_error, err); \
Damjan Marion8389fb92017-10-13 18:29:53 +020057 goto error; \
58 }
59
Mohsin Kazmi206acf82020-04-06 14:19:54 +020060 /* *INDENT-OFF* */
61VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) =
62{
63 .name = "tun-device",
64 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
65};
66 /* *INDENT-ON* */
67
Damjan Marion8389fb92017-10-13 18:29:53 +020068static u32
69virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
70 u32 flags)
71{
72 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010073 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020074 return 0;
75}
76
Damjan Marion2df39092017-12-04 20:03:37 +010077static int
78open_netns_fd (char *netns)
79{
80 u8 *s = 0;
81 int fd;
82
83 if (strncmp (netns, "pid:", 4) == 0)
84 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
85 else if (netns[0] == '/')
86 s = format (0, "%s%c", netns, 0);
87 else
88 s = format (0, "/var/run/netns/%s%c", netns, 0);
89
90 fd = open ((char *) s, O_RDONLY);
91 vec_free (s);
92 return fd;
93}
94
Neale Rannscbe8d652018-04-27 04:42:47 -070095#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010096
Damjan Marion7c6102b2019-11-08 17:59:56 +010097static void
98tap_free (vlib_main_t * vm, virtio_if_t * vif)
99{
100 virtio_main_t *mm = &virtio_main;
101 tap_main_t *tm = &tap_main;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000102 clib_error_t *err = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100103 int i;
104
105 /* *INDENT-OFF* */
106 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
107 close (vif->vhost_fds[i]);
108 vec_foreach_index (i, vif->rxq_vrings)
109 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
110 vec_foreach_index (i, vif->txq_vrings)
111 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
112 /* *INDENT-ON* */
113
Aloys Augustin2857e782020-03-16 17:29:52 +0100114 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000115 tap_log_dbg (vif, "TUNSETPERSIST: unset");
116error:
Aloys Augustin2857e782020-03-16 17:29:52 +0100117 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100118
119 vec_free (vif->vhost_fds);
120 vec_free (vif->rxq_vrings);
121 vec_free (vif->txq_vrings);
122 vec_free (vif->host_if_name);
123 vec_free (vif->net_ns);
124 vec_free (vif->host_bridge);
125 clib_error_free (vif->error);
126
127 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
128 clib_memset (vif, 0, sizeof (*vif));
129 pool_put (mm->interfaces, vif);
130}
131
Damjan Marion91c6ef72017-12-01 13:34:24 +0100132void
Damjan Marion8389fb92017-10-13 18:29:53 +0200133tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
134{
Damjan Marion7c6102b2019-11-08 17:59:56 +0100135 vlib_thread_main_t *thm = vlib_get_thread_main ();
Lijian.Zhangba0da572019-08-21 17:51:16 +0800136 vlib_physmem_main_t *vpm = &vm->physmem_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200137 vnet_main_t *vnm = vnet_get_main ();
138 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100139 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200140 vnet_sw_interface_t *sw;
141 vnet_hw_interface_t *hw;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000142 int i, num_vhost_queues;
Damjan Marion2df39092017-12-04 20:03:37 +0100143 int old_netns_fd = -1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200144 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000145 struct ifreq get_ifr = {.ifr_flags = 0 };
Damjan Marion8389fb92017-10-13 18:29:53 +0200146 size_t hdrsz;
147 struct vhost_memory *vhost_mem = 0;
148 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100149 clib_error_t *err = 0;
Damjan Marion39807d02019-11-08 13:52:28 +0100150 unsigned int tap_features;
Aloys Augustin2857e782020-03-16 17:29:52 +0100151 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200152 char *host_if_name = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100153 unsigned int offload = 0;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000154 int sndbuf = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100155
156 if (args->id != ~0)
157 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700158 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100159 {
160 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
161 args->error = clib_error_return (0, "interface already exists");
162 return;
163 }
164 }
165 else
166 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700167 args->id = clib_bitmap_first_clear (tm->tap_ids);
168 }
Damjan Marion2df39092017-12-04 20:03:37 +0100169
Neale Rannscbe8d652018-04-27 04:42:47 -0700170 if (args->id > TAP_MAX_INSTANCE)
171 {
172 args->rv = VNET_API_ERROR_UNSPECIFIED;
173 args->error = clib_error_return (0, "cannot find free interface id");
174 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100175 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200176
Damjan Marion7c6102b2019-11-08 17:59:56 +0100177 pool_get_zero (vim->interfaces, vif);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200178
179 if (args->tap_flags & TAP_FLAG_TUN)
180 {
181 vif->type = VIRTIO_IF_TYPE_TUN;
182 ifr.ifr_flags |= IFF_TUN;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000183
184 /*
185 * From kernel 4.20, xdp support has been added in tun_sendmsg.
186 * If sndbuf == INT_MAX, vhost batches the packet and processes
187 * them using xdp data path for tun driver. It assumes packets
188 * are ethernet frames (It needs to be fixed).
189 * To avoid xdp data path in tun driver, sndbuf value should
190 * be < INT_MAX.
191 */
192 sndbuf = INT_MAX - 1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200193 }
194 else
195 {
196 vif->type = VIRTIO_IF_TYPE_TAP;
197 ifr.ifr_flags |= IFF_TAP;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000198 sndbuf = INT_MAX;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200199 }
200
Damjan Marion8389fb92017-10-13 18:29:53 +0200201 vif->dev_instance = vif - vim->interfaces;
Damjan Marion2df39092017-12-04 20:03:37 +0100202 vif->id = args->id;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100203 vif->num_txqs = thm->n_vlib_mains;
Aloys Augustin2857e782020-03-16 17:29:52 +0100204 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
Damjan Marion2df39092017-12-04 20:03:37 +0100205
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000206 if (args->tap_flags & TAP_FLAG_ATTACH)
207 {
208 if (args->host_if_name != NULL)
209 {
210 host_if_name = (char *) args->host_if_name;
211 clib_memcpy (ifr.ifr_name, host_if_name,
212 clib_min (IFNAMSIZ, strlen (host_if_name)));
213 }
214 else
215 {
216 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
217 err = clib_error_return (0, "host_if_name is not provided");
218 goto error;
219 }
220 if (args->host_namespace)
221 {
222 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
223 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
224 {
225 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
226 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
227 args->host_namespace);
228 goto error;
229 }
230 if (setns (nfd, CLONE_NEWNET) == -1)
231 {
232 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
233 args->error = clib_error_return_unix (0, "setns '%s'",
234 args->host_namespace);
235 goto error;
236 }
237 }
238 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100239
240 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200241 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100242 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
243 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
244 goto error;
245 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100246 vec_add1 (vif->tap_fds, tfd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100247 tap_log_dbg (vif, "open tap fd %d", tfd);
248
249 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
250 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
251 if ((tap_features & IFF_VNET_HDR) == 0)
252 {
253 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
254 args->error = clib_error_return (0, "vhost-net backend not available");
Damjan Marion8389fb92017-10-13 18:29:53 +0200255 goto error;
256 }
257
Damjan Marion7c6102b2019-11-08 17:59:56 +0100258 if ((tap_features & IFF_MULTI_QUEUE) == 0)
259 {
Aloys Augustin2857e782020-03-16 17:29:52 +0100260 if (vif->num_rxqs > 1)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100261 {
262 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
263 args->error = clib_error_return (0, "multiqueue not supported");
264 goto error;
265 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100266 vif->num_rxqs = vif->num_txqs = 1;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100267 }
268 else
269 ifr.ifr_flags |= IFF_MULTI_QUEUE;
270
271 hdrsz = sizeof (struct virtio_net_hdr_v1);
272 if (args->tap_flags & TAP_FLAG_GSO)
273 {
274 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
275 vif->gso_enabled = 1;
276 }
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100277 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
278 {
279 offload = TUN_F_CSUM;
280 vif->csum_offload_enabled = 1;
281 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100282
283 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
284 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
285 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
286
287 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
288 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
289
290 if (!args->host_if_name)
291 host_if_name = ifr.ifr_ifrn.ifrn_name;
292 else
293 host_if_name = (char *) args->host_if_name;
294
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000295 /*
296 * unset the persistence when attaching to existing
297 * interface
298 */
299 if (args->tap_flags & TAP_FLAG_ATTACH)
300 {
301 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
302 tap_log_dbg (vif, "TUNSETPERSIST: unset");
303 }
304
305 /* set the persistence */
306 if (args->tap_flags & TAP_FLAG_PERSIST)
307 {
308 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
309 tap_log_dbg (vif, "TUNSETPERSIST: set");
310
311 /* verify persistence is set, read the flags */
312 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
313 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
314 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
315 {
316 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
317 args->error = clib_error_return (0, "persistence not supported");
318 goto error;
319 }
320 }
321
Aloys Augustin2857e782020-03-16 17:29:52 +0100322 /* create additional queues on the linux side.
323 * we create as many linux queue pairs as we have rx queues
324 */
325 for (i = 1; i < vif->num_rxqs; i++)
326 {
327 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
328 {
329 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
330 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
331 goto error;
332 }
333 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
334 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
335 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
336 vec_add1 (vif->tap_fds, qfd);
337 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100338
Aloys Augustin2857e782020-03-16 17:29:52 +0100339 for (i = 0; i < vif->num_rxqs; i++)
340 {
341 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
342 vif->tap_fds[i], hdrsz);
343 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100344
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000345 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
346 sndbuf);
347 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100348
Aloys Augustin2857e782020-03-16 17:29:52 +0100349 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
350 offload);
351 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
352
353 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
354 {
355 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
356 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
357 goto error;
358 }
359 }
360
361 /* open as many vhost-net fds as required and set ownership */
362 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
363 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100364 {
365 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
366 {
367 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
368 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
369 goto error;
370 }
371 vec_add1 (vif->vhost_fds, vfd);
372 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
373 _IOCTL (vfd, VHOST_SET_OWNER, 0);
374 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
375 }
376
377 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
378 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
379 vif->remote_features);
Damjan Marion8389fb92017-10-13 18:29:53 +0200380
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200381 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200382 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100383 args->rv = VNET_API_ERROR_UNSUPPORTED;
384 args->error = clib_error_return (0, "vhost-net backend doesn't support "
385 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200386 goto error;
387 }
388
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200389 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
390 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200391 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100392 args->rv = VNET_API_ERROR_UNSUPPORTED;
393 args->error = clib_error_return (0, "vhost-net backend doesn't support "
394 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200395 goto error;
396 }
397
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200398 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200399 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100400 args->rv = VNET_API_ERROR_UNSUPPORTED;
401 args->error = clib_error_return (0, "vhost-net backend doesn't support "
402 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200403 goto error;
404 }
405
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200406 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
407 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
408 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
409
410 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200411
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000412 if (!(args->tap_flags & TAP_FLAG_ATTACH))
Damjan Marion8389fb92017-10-13 18:29:53 +0200413 {
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000414 /* if namespace is specified, all further netlink messages should be executed
415 after we change our net namespace */
416 if (args->host_namespace)
Damjan Marion2df39092017-12-04 20:03:37 +0100417 {
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000418 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
419 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
420 {
421 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
422 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
423 args->host_namespace);
424 goto error;
425 }
426 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
427 host_if_name);
428 if (args->error)
429 {
430 args->rv = VNET_API_ERROR_NETLINK_ERROR;
431 goto error;
432 }
433 if (setns (nfd, CLONE_NEWNET) == -1)
434 {
435 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
436 args->error = clib_error_return_unix (0, "setns '%s'",
437 args->host_namespace);
438 goto error;
439 }
440 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
441 {
442 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
443 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200444 host_if_name);
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000445 goto error;
446 }
447 }
448 else if (host_if_name)
449 {
450 args->error =
451 vnet_netlink_set_link_name (vif->ifindex, host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100452 if (args->error)
453 {
454 args->rv = VNET_API_ERROR_NETLINK_ERROR;
455 goto error;
456 }
457 }
458 }
459
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200460 if (vif->type == VIRTIO_IF_TYPE_TAP)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200461 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200462 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
463 ethernet_mac_address_generate (args->host_mac_addr.bytes);
464 args->error = vnet_netlink_set_link_addr (vif->ifindex,
465 args->host_mac_addr.bytes);
466 if (args->error)
467 {
468 args->rv = VNET_API_ERROR_NETLINK_ERROR;
469 goto error;
470 }
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200471
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000472 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100473 {
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000474 args->error = vnet_netlink_set_link_master (vif->ifindex,
475 (char *)
476 args->host_bridge);
477 if (args->error)
478 {
479 args->rv = VNET_API_ERROR_NETLINK_ERROR;
480 goto error;
481 }
Damjan Marion91c6ef72017-12-01 13:34:24 +0100482 }
483 }
484
485 if (args->host_ip4_prefix_len)
486 {
487 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
488 &args->host_ip4_addr,
489 args->host_ip4_prefix_len);
490 if (args->error)
491 {
492 args->rv = VNET_API_ERROR_NETLINK_ERROR;
493 goto error;
494 }
495 }
496
497 if (args->host_ip6_prefix_len)
498 {
499 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
500 &args->host_ip6_addr,
501 args->host_ip6_prefix_len);
502 if (args->error)
503 {
504 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200505 goto error;
506 }
507 }
508
Damjan Marion2df39092017-12-04 20:03:37 +0100509 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
510 if (args->error)
511 {
512 args->rv = VNET_API_ERROR_NETLINK_ERROR;
513 goto error;
514 }
515
Damjan Marion7866c452018-01-18 13:35:11 +0100516 if (args->host_ip4_gw_set)
517 {
518 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
519 if (args->error)
520 {
521 args->rv = VNET_API_ERROR_NETLINK_ERROR;
522 goto error;
523 }
524 }
525
526 if (args->host_ip6_gw_set)
527 {
528 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
529 if (args->error)
530 {
531 args->rv = VNET_API_ERROR_NETLINK_ERROR;
532 goto error;
533 }
534 }
535
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200536 if (args->host_mtu_set)
537 {
538 args->error =
539 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
540 if (args->error)
541 {
542 args->rv = VNET_API_ERROR_NETLINK_ERROR;
543 goto error;
544 }
545 }
546 else if (tm->host_mtu_size != 0)
547 {
548 args->error =
549 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
550 if (args->error)
551 {
552 args->rv = VNET_API_ERROR_NETLINK_ERROR;
553 goto error;
554 }
555 args->host_mtu_set = 1;
556 args->host_mtu_size = tm->host_mtu_size;
557 }
558
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100559 /* switch back to old net namespace */
560 if (args->host_namespace)
561 {
562 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
563 {
564 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
565 args->error = clib_error_return_unix (0, "setns '%s'",
566 args->host_namespace);
567 goto error;
568 }
569 }
570
Aloys Augustin2857e782020-03-16 17:29:52 +0100571 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100572 {
573 if (i < vif->num_rxqs && (args->error =
574 virtio_vring_init (vm, vif, RX_QUEUE (i),
575 args->rx_ring_sz)))
576 {
577 args->rv = VNET_API_ERROR_INIT_FAILED;
578 goto error;
579 }
580
581 if (i < vif->num_txqs && (args->error =
582 virtio_vring_init (vm, vif, TX_QUEUE (i),
583 args->tx_ring_sz)))
584 {
585 args->rv = VNET_API_ERROR_INIT_FAILED;
586 goto error;
587 }
588 }
589
590 /* setup features and memtable */
Damjan Marion8389fb92017-10-13 18:29:53 +0200591 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
592 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400593 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200594 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800595 vhost_mem->regions[0].memory_size = vpm->max_size;
596 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
597 vhost_mem->regions[0].userspace_addr =
598 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200599
Damjan Marion7c6102b2019-11-08 17:59:56 +0100600 for (i = 0; i < vhost_mem->nregions; i++)
601 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
602 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
603 vhost_mem->regions[0].memory_size,
604 vhost_mem->regions[0].guest_phys_addr,
605 vhost_mem->regions[0].userspace_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200606
Damjan Marion7c6102b2019-11-08 17:59:56 +0100607
Aloys Augustin2857e782020-03-16 17:29:52 +0100608 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion8389fb92017-10-13 18:29:53 +0200609 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100610 int fd = vif->vhost_fds[i];
611 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
612 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
613 fd, vif->features);
614 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
615 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200616 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100617
618 /* finish initializing queue pair */
Aloys Augustin2857e782020-03-16 17:29:52 +0100619 for (i = 0; i < num_vhost_queues * 2; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100620 {
621 struct vhost_vring_addr addr = { 0 };
622 struct vhost_vring_state state = { 0 };
623 struct vhost_vring_file file = { 0 };
624 virtio_vring_t *vring;
625 u16 qp = i >> 1;
626 int fd = vif->vhost_fds[qp];
627
628 if (i & 1)
629 {
630 if (qp >= vif->num_txqs)
631 continue;
632 vring = vec_elt_at_index (vif->txq_vrings, qp);
633 }
634 else
635 {
636 if (qp >= vif->num_rxqs)
637 continue;
638 vring = vec_elt_at_index (vif->rxq_vrings, qp);
639 }
640
641 addr.index = state.index = file.index = vring->queue_id & 1;
642 state.num = vring->size;
643 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
644 state.index, state.num);
645 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
646
647 addr.flags = 0;
648 addr.desc_user_addr = pointer_to_uword (vring->desc);
649 addr.avail_user_addr = pointer_to_uword (vring->avail);
650 addr.used_user_addr = pointer_to_uword (vring->used);
651
652 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
653 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
654 "used_user_addr 0x%lx", fd, addr.index,
655 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
656 addr.used_user_addr);
657 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
658
659 file.fd = vring->call_fd;
660 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
661 fd, file.index, file.fd);
662 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
663
664 file.fd = vring->kick_fd;
665 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
666 fd, file.index, file.fd);
667 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
668
Aloys Augustin2857e782020-03-16 17:29:52 +0100669 file.fd = vif->tap_fds[qp % vif->num_rxqs];
Damjan Marion7c6102b2019-11-08 17:59:56 +0100670 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
671 fd, file.index, file.fd);
672 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
673 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200674
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200675 if (vif->type == VIRTIO_IF_TYPE_TAP)
676 {
677 if (!args->mac_addr_set)
678 ethernet_mac_address_generate (args->mac_addr.bytes);
Damjan Marion8389fb92017-10-13 18:29:53 +0200679
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200680 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000681 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200682 }
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200683 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
Benoît Gannec30d87e2019-07-15 17:16:49 +0200684 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200685 vif->host_mtu_size = args->host_mtu_size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200686 vif->tap_flags = args->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200687 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100688 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
689 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
690 if (args->host_ip4_prefix_len)
691 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
692 if (args->host_ip6_prefix_len)
693 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
694
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200695 if (vif->type != VIRTIO_IF_TYPE_TUN)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100696 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200697 args->error =
698 ethernet_register_interface (vnm, virtio_device_class.index,
699 vif->dev_instance, vif->mac_addr,
700 &vif->hw_if_index,
701 virtio_eth_flag_change);
702 if (args->error)
703 {
704 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
705 goto error;
706 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200707
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200708 }
709 else
710 {
711 vif->hw_if_index = vnet_register_interface
712 (vnm, virtio_device_class.index,
713 vif->dev_instance /* device instance */ ,
714 tun_device_hw_interface_class.index, vif->dev_instance);
715
716 }
Neale Rannscbe8d652018-04-27 04:42:47 -0700717 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200718 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
719 vif->sw_if_index = sw->sw_if_index;
720 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200721 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200722 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
723 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200724 if (args->tap_flags & TAP_FLAG_GSO)
725 {
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100726 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
727 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
728 }
729 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
730 {
731 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200732 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200733 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
734 virtio_input_node.index);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100735
736 for (i = 0; i < vif->num_rxqs; i++)
737 {
738 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
739 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
740 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
Mohsin Kazmi547a6162020-03-18 13:17:00 +0100741 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
Damjan Marion7c6102b2019-11-08 17:59:56 +0100742 }
743
Damjan Marion8389fb92017-10-13 18:29:53 +0200744 vif->per_interface_next_index = ~0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200745 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
746 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
747 VNET_HW_INTERFACE_FLAG_LINK_UP);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000748 vif->cxq_vring = NULL;
749
Damjan Marion8389fb92017-10-13 18:29:53 +0200750 goto done;
751
752error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100753 if (err)
754 {
755 ASSERT (args->error == 0);
756 args->error = err;
757 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
758 }
Benoît Gannec30d87e2019-07-15 17:16:49 +0200759
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100760 tap_log_err (vif, "%U", format_clib_error, args->error);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100761 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200762done:
763 if (vhost_mem)
764 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100765 if (old_netns_fd != -1)
766 close (old_netns_fd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100767 if (nfd != -1)
768 close (nfd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200769}
770
771int
772tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
773{
774 vnet_main_t *vnm = vnet_get_main ();
775 virtio_main_t *mm = &virtio_main;
776 int i;
777 virtio_if_t *vif;
778 vnet_hw_interface_t *hw;
779
Dave Barach3940de32019-07-23 16:28:36 -0400780 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200781 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
782 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
783
784 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
785
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500786 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200787 return VNET_API_ERROR_INVALID_INTERFACE;
788
Damjan Marion8389fb92017-10-13 18:29:53 +0200789 /* bring down the interface */
790 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
791 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100792 for (i = 0; i < vif->num_rxqs; i++)
793 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200794
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500795 if (vif->type == VIRTIO_IF_TYPE_TAP)
796 ethernet_delete_interface (vnm, vif->hw_if_index);
797 else /* VIRTIO_IF_TYPE_TUN */
798 vnet_delete_hw_interface (vnm, vif->hw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200799 vif->hw_if_index = ~0;
800
Damjan Marion7c6102b2019-11-08 17:59:56 +0100801 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200802
803 return 0;
804}
805
806int
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100807tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
808 int enable_disable)
809{
810 vnet_main_t *vnm = vnet_get_main ();
811 virtio_main_t *mm = &virtio_main;
812 virtio_if_t *vif;
813 vnet_hw_interface_t *hw;
814 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100815 int i = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100816
817 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
818
819 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
820 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
821
822 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
823
824 const unsigned int csum_offload_on = TUN_F_CSUM;
825 const unsigned int csum_offload_off = 0;
826 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100827 vec_foreach_index (i, vif->tap_fds)
828 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100829 vif->gso_enabled = 0;
830 vif->csum_offload_enabled = enable_disable ? 1 : 0;
831
832 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
833 {
834 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
835 }
836
837 if (enable_disable)
838 {
839 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) ==
840 0)
841 {
842 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
843 }
844 }
845 else
846 {
847 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) !=
848 0)
849 {
850 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
851 }
852 }
853
854error:
855 if (err)
856 {
857 clib_warning ("Error %s checksum offload on sw_if_index %d",
858 enable_disable ? "enabling" : "disabling", sw_if_index);
859 return VNET_API_ERROR_SYSCALL_ERROR_3;
860 }
861 return 0;
862}
863
864int
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200865tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
866{
867 vnet_main_t *vnm = vnet_get_main ();
868 virtio_main_t *mm = &virtio_main;
869 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400870 vnet_hw_interface_t *hw;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200871 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100872 int i = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200873
Dave Barach3940de32019-07-23 16:28:36 -0400874 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
875
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200876 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
877 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
878
879 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
880
881 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
882 const unsigned int gso_off = 0;
883 unsigned int offload = enable_disable ? gso_on : gso_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100884 vec_foreach_index (i, vif->tap_fds)
885 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200886 vif->gso_enabled = enable_disable ? 1 : 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100887 vif->csum_offload_enabled = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200888 if (enable_disable)
889 {
890 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
891 {
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100892 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
893 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200894 }
895 }
896 else
897 {
898 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
899 {
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100900 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
901 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200902 }
903 }
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* */
927 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,
Milan Lenco73e7f422017-12-14 10:04:25 +0100937 MIN (ARRAY_LEN (tapid->dev_name) - 1,
938 strlen ((const char *) hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000939 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
940 tapid->rx_ring_sz = vring->size;
941 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
942 tapid->tx_ring_sz = vring->size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200943 tapid->tap_flags = vif->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200944 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100945 if (vif->host_if_name)
946 {
947 clib_memcpy(tapid->host_if_name, vif->host_if_name,
948 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
949 strlen ((const char *) vif->host_if_name)));
950 }
951 if (vif->net_ns)
952 {
953 clib_memcpy(tapid->host_namespace, vif->net_ns,
954 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
955 strlen ((const char *) vif->net_ns)));
956 }
957 if (vif->host_bridge)
958 {
959 clib_memcpy(tapid->host_bridge, vif->host_bridge,
960 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
961 strlen ((const char *) vif->host_bridge)));
962 }
963 if (vif->host_ip4_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200964 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
Milan Lenco73e7f422017-12-14 10:04:25 +0100965 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
966 if (vif->host_ip6_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200967 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
Milan Lenco73e7f422017-12-14 10:04:25 +0100968 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200969 tapid->host_mtu_size = vif->host_mtu_size;
Damjan Marion8389fb92017-10-13 18:29:53 +0200970 );
971 /* *INDENT-ON* */
972
973 *out_tapids = r_tapids;
974
975 return 0;
976}
977
978static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200979tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
980{
981 tap_main_t *tm = &tap_main;
982
983 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
984 {
985 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
986 ;
987 else
988 return clib_error_return (0, "unknown input `%U'",
989 format_unformat_error, input);
990 }
991
992 return 0;
993}
994
995/* tap { host-mtu <size> } configuration. */
996VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
997
998static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +0200999tap_init (vlib_main_t * vm)
1000{
Damjan Marion2df39092017-12-04 20:03:37 +01001001 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001002 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -07001003
Damjan Marion07a38572018-01-21 06:44:18 -08001004 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001005 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -07001006
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001007 tm->host_mtu_size = 0;
1008
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001009 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +02001010}
1011
1012VLIB_INIT_FUNCTION (tap_init);
1013
1014/*
1015 * fd.io coding-style-patch-verification: ON
1016 *
1017 * Local Variables:
1018 * eval: (c-set-style "gnu")
1019 * End:
1020 */