blob: cbdbbe9e4e237b7218926b73e9a0cdb6bf609260 [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;
Aloys Augustin2857e782020-03-16 17:29:52 +0100142 int i, j, 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;
Damjan Marion2df39092017-12-04 20:03:37 +0100154
155 if (args->id != ~0)
156 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700157 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100158 {
159 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
160 args->error = clib_error_return (0, "interface already exists");
161 return;
162 }
163 }
164 else
165 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700166 args->id = clib_bitmap_first_clear (tm->tap_ids);
167 }
Damjan Marion2df39092017-12-04 20:03:37 +0100168
Neale Rannscbe8d652018-04-27 04:42:47 -0700169 if (args->id > TAP_MAX_INSTANCE)
170 {
171 args->rv = VNET_API_ERROR_UNSPECIFIED;
172 args->error = clib_error_return (0, "cannot find free interface id");
173 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100174 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200175
Damjan Marion7c6102b2019-11-08 17:59:56 +0100176 pool_get_zero (vim->interfaces, vif);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200177
178 if (args->tap_flags & TAP_FLAG_TUN)
179 {
180 vif->type = VIRTIO_IF_TYPE_TUN;
181 ifr.ifr_flags |= IFF_TUN;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200182 }
183 else
184 {
185 vif->type = VIRTIO_IF_TYPE_TAP;
186 ifr.ifr_flags |= IFF_TAP;
187 }
188
Damjan Marion8389fb92017-10-13 18:29:53 +0200189 vif->dev_instance = vif - vim->interfaces;
Damjan Marion2df39092017-12-04 20:03:37 +0100190 vif->id = args->id;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100191 vif->num_txqs = thm->n_vlib_mains;
Aloys Augustin2857e782020-03-16 17:29:52 +0100192 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
Damjan Marion2df39092017-12-04 20:03:37 +0100193
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000194 if (args->tap_flags & TAP_FLAG_ATTACH)
195 {
196 if (args->host_if_name != NULL)
197 {
198 host_if_name = (char *) args->host_if_name;
199 clib_memcpy (ifr.ifr_name, host_if_name,
200 clib_min (IFNAMSIZ, strlen (host_if_name)));
201 }
202 else
203 {
204 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
205 err = clib_error_return (0, "host_if_name is not provided");
206 goto error;
207 }
208 if (args->host_namespace)
209 {
210 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
211 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
212 {
213 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
214 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
215 args->host_namespace);
216 goto error;
217 }
218 if (setns (nfd, CLONE_NEWNET) == -1)
219 {
220 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
221 args->error = clib_error_return_unix (0, "setns '%s'",
222 args->host_namespace);
223 goto error;
224 }
225 }
226 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100227
228 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200229 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100230 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
231 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
232 goto error;
233 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100234 vec_add1 (vif->tap_fds, tfd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100235 tap_log_dbg (vif, "open tap fd %d", tfd);
236
237 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
238 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
239 if ((tap_features & IFF_VNET_HDR) == 0)
240 {
241 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
242 args->error = clib_error_return (0, "vhost-net backend not available");
Damjan Marion8389fb92017-10-13 18:29:53 +0200243 goto error;
244 }
245
Damjan Marion7c6102b2019-11-08 17:59:56 +0100246 if ((tap_features & IFF_MULTI_QUEUE) == 0)
247 {
Aloys Augustin2857e782020-03-16 17:29:52 +0100248 if (vif->num_rxqs > 1)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100249 {
250 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
251 args->error = clib_error_return (0, "multiqueue not supported");
252 goto error;
253 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100254 vif->num_rxqs = vif->num_txqs = 1;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100255 }
256 else
257 ifr.ifr_flags |= IFF_MULTI_QUEUE;
258
259 hdrsz = sizeof (struct virtio_net_hdr_v1);
260 if (args->tap_flags & TAP_FLAG_GSO)
261 {
262 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
263 vif->gso_enabled = 1;
264 }
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100265 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
266 {
267 offload = TUN_F_CSUM;
268 vif->csum_offload_enabled = 1;
269 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100270
271 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
272 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
273 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
274
275 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
276 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
277
278 if (!args->host_if_name)
279 host_if_name = ifr.ifr_ifrn.ifrn_name;
280 else
281 host_if_name = (char *) args->host_if_name;
282
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000283 /*
284 * unset the persistence when attaching to existing
285 * interface
286 */
287 if (args->tap_flags & TAP_FLAG_ATTACH)
288 {
289 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
290 tap_log_dbg (vif, "TUNSETPERSIST: unset");
291 }
292
293 /* set the persistence */
294 if (args->tap_flags & TAP_FLAG_PERSIST)
295 {
296 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
297 tap_log_dbg (vif, "TUNSETPERSIST: set");
298
299 /* verify persistence is set, read the flags */
300 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
301 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
302 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
303 {
304 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
305 args->error = clib_error_return (0, "persistence not supported");
306 goto error;
307 }
308 }
309
Aloys Augustin2857e782020-03-16 17:29:52 +0100310 /* create additional queues on the linux side.
311 * we create as many linux queue pairs as we have rx queues
312 */
313 for (i = 1; i < vif->num_rxqs; i++)
314 {
315 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
316 {
317 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
318 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
319 goto error;
320 }
321 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
322 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
323 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
324 vec_add1 (vif->tap_fds, qfd);
325 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100326
Aloys Augustin2857e782020-03-16 17:29:52 +0100327 for (i = 0; i < vif->num_rxqs; i++)
328 {
329 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
330 vif->tap_fds[i], hdrsz);
331 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100332
Aloys Augustin2857e782020-03-16 17:29:52 +0100333 j = INT_MAX;
334 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i], j);
335 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &j);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100336
Aloys Augustin2857e782020-03-16 17:29:52 +0100337 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
338 offload);
339 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
340
341 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
342 {
343 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
344 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
345 goto error;
346 }
347 }
348
349 /* open as many vhost-net fds as required and set ownership */
350 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
351 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100352 {
353 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
354 {
355 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
356 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
357 goto error;
358 }
359 vec_add1 (vif->vhost_fds, vfd);
360 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
361 _IOCTL (vfd, VHOST_SET_OWNER, 0);
362 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
363 }
364
365 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
366 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
367 vif->remote_features);
Damjan Marion8389fb92017-10-13 18:29:53 +0200368
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200369 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200370 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100371 args->rv = VNET_API_ERROR_UNSUPPORTED;
372 args->error = clib_error_return (0, "vhost-net backend doesn't support "
373 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200374 goto error;
375 }
376
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200377 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
378 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200379 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100380 args->rv = VNET_API_ERROR_UNSUPPORTED;
381 args->error = clib_error_return (0, "vhost-net backend doesn't support "
382 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200383 goto error;
384 }
385
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200386 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200387 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100388 args->rv = VNET_API_ERROR_UNSUPPORTED;
389 args->error = clib_error_return (0, "vhost-net backend doesn't support "
390 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200391 goto error;
392 }
393
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200394 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
395 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
396 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
397
398 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200399
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000400 if (!(args->tap_flags & TAP_FLAG_ATTACH))
Damjan Marion8389fb92017-10-13 18:29:53 +0200401 {
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000402 /* if namespace is specified, all further netlink messages should be executed
403 after we change our net namespace */
404 if (args->host_namespace)
Damjan Marion2df39092017-12-04 20:03:37 +0100405 {
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000406 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
407 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
408 {
409 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
410 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
411 args->host_namespace);
412 goto error;
413 }
414 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
415 host_if_name);
416 if (args->error)
417 {
418 args->rv = VNET_API_ERROR_NETLINK_ERROR;
419 goto error;
420 }
421 if (setns (nfd, CLONE_NEWNET) == -1)
422 {
423 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
424 args->error = clib_error_return_unix (0, "setns '%s'",
425 args->host_namespace);
426 goto error;
427 }
428 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
429 {
430 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
431 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200432 host_if_name);
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000433 goto error;
434 }
435 }
436 else if (host_if_name)
437 {
438 args->error =
439 vnet_netlink_set_link_name (vif->ifindex, host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100440 if (args->error)
441 {
442 args->rv = VNET_API_ERROR_NETLINK_ERROR;
443 goto error;
444 }
445 }
446 }
447
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200448 if (vif->type == VIRTIO_IF_TYPE_TAP)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200449 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200450 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
451 ethernet_mac_address_generate (args->host_mac_addr.bytes);
452 args->error = vnet_netlink_set_link_addr (vif->ifindex,
453 args->host_mac_addr.bytes);
454 if (args->error)
455 {
456 args->rv = VNET_API_ERROR_NETLINK_ERROR;
457 goto error;
458 }
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200459
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000460 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100461 {
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000462 args->error = vnet_netlink_set_link_master (vif->ifindex,
463 (char *)
464 args->host_bridge);
465 if (args->error)
466 {
467 args->rv = VNET_API_ERROR_NETLINK_ERROR;
468 goto error;
469 }
Damjan Marion91c6ef72017-12-01 13:34:24 +0100470 }
471 }
472
473 if (args->host_ip4_prefix_len)
474 {
475 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
476 &args->host_ip4_addr,
477 args->host_ip4_prefix_len);
478 if (args->error)
479 {
480 args->rv = VNET_API_ERROR_NETLINK_ERROR;
481 goto error;
482 }
483 }
484
485 if (args->host_ip6_prefix_len)
486 {
487 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
488 &args->host_ip6_addr,
489 args->host_ip6_prefix_len);
490 if (args->error)
491 {
492 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200493 goto error;
494 }
495 }
496
Damjan Marion2df39092017-12-04 20:03:37 +0100497 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
498 if (args->error)
499 {
500 args->rv = VNET_API_ERROR_NETLINK_ERROR;
501 goto error;
502 }
503
Damjan Marion7866c452018-01-18 13:35:11 +0100504 if (args->host_ip4_gw_set)
505 {
506 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
507 if (args->error)
508 {
509 args->rv = VNET_API_ERROR_NETLINK_ERROR;
510 goto error;
511 }
512 }
513
514 if (args->host_ip6_gw_set)
515 {
516 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
517 if (args->error)
518 {
519 args->rv = VNET_API_ERROR_NETLINK_ERROR;
520 goto error;
521 }
522 }
523
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200524 if (args->host_mtu_set)
525 {
526 args->error =
527 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
528 if (args->error)
529 {
530 args->rv = VNET_API_ERROR_NETLINK_ERROR;
531 goto error;
532 }
533 }
534 else if (tm->host_mtu_size != 0)
535 {
536 args->error =
537 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
538 if (args->error)
539 {
540 args->rv = VNET_API_ERROR_NETLINK_ERROR;
541 goto error;
542 }
543 args->host_mtu_set = 1;
544 args->host_mtu_size = tm->host_mtu_size;
545 }
546
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100547 /* switch back to old net namespace */
548 if (args->host_namespace)
549 {
550 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
551 {
552 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
553 args->error = clib_error_return_unix (0, "setns '%s'",
554 args->host_namespace);
555 goto error;
556 }
557 }
558
Aloys Augustin2857e782020-03-16 17:29:52 +0100559 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100560 {
561 if (i < vif->num_rxqs && (args->error =
562 virtio_vring_init (vm, vif, RX_QUEUE (i),
563 args->rx_ring_sz)))
564 {
565 args->rv = VNET_API_ERROR_INIT_FAILED;
566 goto error;
567 }
568
569 if (i < vif->num_txqs && (args->error =
570 virtio_vring_init (vm, vif, TX_QUEUE (i),
571 args->tx_ring_sz)))
572 {
573 args->rv = VNET_API_ERROR_INIT_FAILED;
574 goto error;
575 }
576 }
577
578 /* setup features and memtable */
Damjan Marion8389fb92017-10-13 18:29:53 +0200579 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
580 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400581 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200582 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800583 vhost_mem->regions[0].memory_size = vpm->max_size;
584 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
585 vhost_mem->regions[0].userspace_addr =
586 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200587
Damjan Marion7c6102b2019-11-08 17:59:56 +0100588 for (i = 0; i < vhost_mem->nregions; i++)
589 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
590 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
591 vhost_mem->regions[0].memory_size,
592 vhost_mem->regions[0].guest_phys_addr,
593 vhost_mem->regions[0].userspace_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200594
Damjan Marion7c6102b2019-11-08 17:59:56 +0100595
Aloys Augustin2857e782020-03-16 17:29:52 +0100596 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion8389fb92017-10-13 18:29:53 +0200597 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100598 int fd = vif->vhost_fds[i];
599 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
600 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
601 fd, vif->features);
602 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
603 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200604 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100605
606 /* finish initializing queue pair */
Aloys Augustin2857e782020-03-16 17:29:52 +0100607 for (i = 0; i < num_vhost_queues * 2; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100608 {
609 struct vhost_vring_addr addr = { 0 };
610 struct vhost_vring_state state = { 0 };
611 struct vhost_vring_file file = { 0 };
612 virtio_vring_t *vring;
613 u16 qp = i >> 1;
614 int fd = vif->vhost_fds[qp];
615
616 if (i & 1)
617 {
618 if (qp >= vif->num_txqs)
619 continue;
620 vring = vec_elt_at_index (vif->txq_vrings, qp);
621 }
622 else
623 {
624 if (qp >= vif->num_rxqs)
625 continue;
626 vring = vec_elt_at_index (vif->rxq_vrings, qp);
627 }
628
629 addr.index = state.index = file.index = vring->queue_id & 1;
630 state.num = vring->size;
631 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
632 state.index, state.num);
633 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
634
635 addr.flags = 0;
636 addr.desc_user_addr = pointer_to_uword (vring->desc);
637 addr.avail_user_addr = pointer_to_uword (vring->avail);
638 addr.used_user_addr = pointer_to_uword (vring->used);
639
640 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
641 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
642 "used_user_addr 0x%lx", fd, addr.index,
643 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
644 addr.used_user_addr);
645 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
646
647 file.fd = vring->call_fd;
648 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
649 fd, file.index, file.fd);
650 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
651
652 file.fd = vring->kick_fd;
653 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
654 fd, file.index, file.fd);
655 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
656
Aloys Augustin2857e782020-03-16 17:29:52 +0100657 file.fd = vif->tap_fds[qp % vif->num_rxqs];
Damjan Marion7c6102b2019-11-08 17:59:56 +0100658 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
659 fd, file.index, file.fd);
660 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
661 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200662
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200663 if (vif->type == VIRTIO_IF_TYPE_TAP)
664 {
665 if (!args->mac_addr_set)
666 ethernet_mac_address_generate (args->mac_addr.bytes);
Damjan Marion8389fb92017-10-13 18:29:53 +0200667
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200668 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000669 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200670 }
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200671 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
Benoît Gannec30d87e2019-07-15 17:16:49 +0200672 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200673 vif->host_mtu_size = args->host_mtu_size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200674 vif->tap_flags = args->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200675 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100676 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
677 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
678 if (args->host_ip4_prefix_len)
679 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
680 if (args->host_ip6_prefix_len)
681 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
682
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200683 if (vif->type != VIRTIO_IF_TYPE_TUN)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100684 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200685 args->error =
686 ethernet_register_interface (vnm, virtio_device_class.index,
687 vif->dev_instance, vif->mac_addr,
688 &vif->hw_if_index,
689 virtio_eth_flag_change);
690 if (args->error)
691 {
692 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
693 goto error;
694 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200695
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200696 }
697 else
698 {
699 vif->hw_if_index = vnet_register_interface
700 (vnm, virtio_device_class.index,
701 vif->dev_instance /* device instance */ ,
702 tun_device_hw_interface_class.index, vif->dev_instance);
703
704 }
Neale Rannscbe8d652018-04-27 04:42:47 -0700705 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200706 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
707 vif->sw_if_index = sw->sw_if_index;
708 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200709 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200710 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
711 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200712 if (args->tap_flags & TAP_FLAG_GSO)
713 {
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100714 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
715 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
716 }
717 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
718 {
719 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200720 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200721 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
722 virtio_input_node.index);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100723
724 for (i = 0; i < vif->num_rxqs; i++)
725 {
726 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
727 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
728 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
Mohsin Kazmi547a6162020-03-18 13:17:00 +0100729 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
Damjan Marion7c6102b2019-11-08 17:59:56 +0100730 }
731
Damjan Marion8389fb92017-10-13 18:29:53 +0200732 vif->per_interface_next_index = ~0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200733 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
734 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
735 VNET_HW_INTERFACE_FLAG_LINK_UP);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000736 vif->cxq_vring = NULL;
737
Damjan Marion8389fb92017-10-13 18:29:53 +0200738 goto done;
739
740error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100741 if (err)
742 {
743 ASSERT (args->error == 0);
744 args->error = err;
745 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
746 }
Benoît Gannec30d87e2019-07-15 17:16:49 +0200747
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100748 tap_log_err (vif, "%U", format_clib_error, args->error);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100749 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200750done:
751 if (vhost_mem)
752 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100753 if (old_netns_fd != -1)
754 close (old_netns_fd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100755 if (nfd != -1)
756 close (nfd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200757}
758
759int
760tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
761{
762 vnet_main_t *vnm = vnet_get_main ();
763 virtio_main_t *mm = &virtio_main;
764 int i;
765 virtio_if_t *vif;
766 vnet_hw_interface_t *hw;
767
Dave Barach3940de32019-07-23 16:28:36 -0400768 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200769 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
770 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
771
772 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
773
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500774 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200775 return VNET_API_ERROR_INVALID_INTERFACE;
776
Damjan Marion8389fb92017-10-13 18:29:53 +0200777 /* bring down the interface */
778 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
779 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100780 for (i = 0; i < vif->num_rxqs; i++)
781 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200782
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500783 if (vif->type == VIRTIO_IF_TYPE_TAP)
784 ethernet_delete_interface (vnm, vif->hw_if_index);
785 else /* VIRTIO_IF_TYPE_TUN */
786 vnet_delete_hw_interface (vnm, vif->hw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200787 vif->hw_if_index = ~0;
788
Damjan Marion7c6102b2019-11-08 17:59:56 +0100789 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200790
791 return 0;
792}
793
794int
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100795tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
796 int enable_disable)
797{
798 vnet_main_t *vnm = vnet_get_main ();
799 virtio_main_t *mm = &virtio_main;
800 virtio_if_t *vif;
801 vnet_hw_interface_t *hw;
802 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100803 int i = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100804
805 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
806
807 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
808 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
809
810 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
811
812 const unsigned int csum_offload_on = TUN_F_CSUM;
813 const unsigned int csum_offload_off = 0;
814 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100815 vec_foreach_index (i, vif->tap_fds)
816 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100817 vif->gso_enabled = 0;
818 vif->csum_offload_enabled = enable_disable ? 1 : 0;
819
820 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
821 {
822 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
823 }
824
825 if (enable_disable)
826 {
827 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) ==
828 0)
829 {
830 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
831 }
832 }
833 else
834 {
835 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) !=
836 0)
837 {
838 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
839 }
840 }
841
842error:
843 if (err)
844 {
845 clib_warning ("Error %s checksum offload on sw_if_index %d",
846 enable_disable ? "enabling" : "disabling", sw_if_index);
847 return VNET_API_ERROR_SYSCALL_ERROR_3;
848 }
849 return 0;
850}
851
852int
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200853tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
854{
855 vnet_main_t *vnm = vnet_get_main ();
856 virtio_main_t *mm = &virtio_main;
857 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400858 vnet_hw_interface_t *hw;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200859 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100860 int i = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200861
Dave Barach3940de32019-07-23 16:28:36 -0400862 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
863
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200864 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
865 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
866
867 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
868
869 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
870 const unsigned int gso_off = 0;
871 unsigned int offload = enable_disable ? gso_on : gso_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100872 vec_foreach_index (i, vif->tap_fds)
873 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200874 vif->gso_enabled = enable_disable ? 1 : 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100875 vif->csum_offload_enabled = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200876 if (enable_disable)
877 {
878 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
879 {
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100880 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
881 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200882 }
883 }
884 else
885 {
886 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
887 {
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100888 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
889 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200890 }
891 }
892
893error:
894 if (err)
895 {
896 clib_warning ("Error %s gso on sw_if_index %d",
897 enable_disable ? "enabling" : "disabling", sw_if_index);
898 return VNET_API_ERROR_SYSCALL_ERROR_3;
899 }
900 return 0;
901}
902
903int
Damjan Marion8389fb92017-10-13 18:29:53 +0200904tap_dump_ifs (tap_interface_details_t ** out_tapids)
905{
906 vnet_main_t *vnm = vnet_get_main ();
907 virtio_main_t *mm = &virtio_main;
908 virtio_if_t *vif;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000909 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200910 vnet_hw_interface_t *hi;
911 tap_interface_details_t *r_tapids = NULL;
912 tap_interface_details_t *tapid = NULL;
913
914 /* *INDENT-OFF* */
915 pool_foreach (vif, mm->interfaces,
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200916 if ((vif->type != VIRTIO_IF_TYPE_TAP)
917 && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200918 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200919 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400920 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100921 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200922 tapid->sw_if_index = vif->sw_if_index;
923 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
924 clib_memcpy(tapid->dev_name, hi->name,
Milan Lenco73e7f422017-12-14 10:04:25 +0100925 MIN (ARRAY_LEN (tapid->dev_name) - 1,
926 strlen ((const char *) hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000927 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
928 tapid->rx_ring_sz = vring->size;
929 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
930 tapid->tx_ring_sz = vring->size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200931 tapid->tap_flags = vif->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200932 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100933 if (vif->host_if_name)
934 {
935 clib_memcpy(tapid->host_if_name, vif->host_if_name,
936 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
937 strlen ((const char *) vif->host_if_name)));
938 }
939 if (vif->net_ns)
940 {
941 clib_memcpy(tapid->host_namespace, vif->net_ns,
942 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
943 strlen ((const char *) vif->net_ns)));
944 }
945 if (vif->host_bridge)
946 {
947 clib_memcpy(tapid->host_bridge, vif->host_bridge,
948 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
949 strlen ((const char *) vif->host_bridge)));
950 }
951 if (vif->host_ip4_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200952 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
Milan Lenco73e7f422017-12-14 10:04:25 +0100953 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
954 if (vif->host_ip6_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200955 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
Milan Lenco73e7f422017-12-14 10:04:25 +0100956 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200957 tapid->host_mtu_size = vif->host_mtu_size;
Damjan Marion8389fb92017-10-13 18:29:53 +0200958 );
959 /* *INDENT-ON* */
960
961 *out_tapids = r_tapids;
962
963 return 0;
964}
965
966static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200967tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
968{
969 tap_main_t *tm = &tap_main;
970
971 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
972 {
973 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
974 ;
975 else
976 return clib_error_return (0, "unknown input `%U'",
977 format_unformat_error, input);
978 }
979
980 return 0;
981}
982
983/* tap { host-mtu <size> } configuration. */
984VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
985
986static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +0200987tap_init (vlib_main_t * vm)
988{
Damjan Marion2df39092017-12-04 20:03:37 +0100989 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200990 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -0700991
Damjan Marion07a38572018-01-21 06:44:18 -0800992 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200993 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -0700994
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200995 tm->host_mtu_size = 0;
996
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200997 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +0200998}
999
1000VLIB_INIT_FUNCTION (tap_init);
1001
1002/*
1003 * fd.io coding-style-patch-verification: ON
1004 *
1005 * Local Variables:
1006 * eval: (c-set-style "gnu")
1007 * End:
1008 */