blob: b65879c42fc7ffcfc87a6a83eb41f21236772d45 [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 Marion2df39092017-12-04 20:03:37 +010030#include <sched.h>
Damjan Marion7c6102b2019-11-08 17:59:56 +010031#include <limits.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020032
33#include <linux/netlink.h>
34#include <linux/rtnetlink.h>
35
36#include <vlib/vlib.h>
Lijian.Zhangba0da572019-08-21 17:51:16 +080037#include <vlib/physmem.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020038#include <vlib/unix/unix.h>
39#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
Damjan Marion8389fb92017-10-13 18:29:53 +020069static u32
70virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
71 u32 flags)
72{
73 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010074 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020075 return 0;
76}
77
Damjan Marion2df39092017-12-04 20:03:37 +010078static int
79open_netns_fd (char *netns)
80{
81 u8 *s = 0;
82 int fd;
83
84 if (strncmp (netns, "pid:", 4) == 0)
85 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
86 else if (netns[0] == '/')
87 s = format (0, "%s%c", netns, 0);
88 else
89 s = format (0, "/var/run/netns/%s%c", netns, 0);
90
91 fd = open ((char *) s, O_RDONLY);
92 vec_free (s);
93 return fd;
94}
95
Neale Rannscbe8d652018-04-27 04:42:47 -070096#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010097
Damjan Marion7c6102b2019-11-08 17:59:56 +010098static void
99tap_free (vlib_main_t * vm, virtio_if_t * vif)
100{
101 virtio_main_t *mm = &virtio_main;
102 tap_main_t *tm = &tap_main;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000103 clib_error_t *err = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100104 int i;
105
106 /* *INDENT-OFF* */
107 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
108 close (vif->vhost_fds[i]);
109 vec_foreach_index (i, vif->rxq_vrings)
110 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
111 vec_foreach_index (i, vif->txq_vrings)
112 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
113 /* *INDENT-ON* */
114
Mohsin Kazmi108b15b2020-10-28 19:35:53 +0100115 if (vif->tap_fds)
116 {
117 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
118 tap_log_dbg (vif, "TUNSETPERSIST: unset");
119 }
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000120error:
Aloys Augustin2857e782020-03-16 17:29:52 +0100121 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100122
123 vec_free (vif->vhost_fds);
124 vec_free (vif->rxq_vrings);
125 vec_free (vif->txq_vrings);
126 vec_free (vif->host_if_name);
127 vec_free (vif->net_ns);
128 vec_free (vif->host_bridge);
129 clib_error_free (vif->error);
130
131 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
132 clib_memset (vif, 0, sizeof (*vif));
133 pool_put (mm->interfaces, vif);
134}
135
Damjan Marion91c6ef72017-12-01 13:34:24 +0100136void
Damjan Marion8389fb92017-10-13 18:29:53 +0200137tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
138{
Damjan Marion7c6102b2019-11-08 17:59:56 +0100139 vlib_thread_main_t *thm = vlib_get_thread_main ();
Lijian.Zhangba0da572019-08-21 17:51:16 +0800140 vlib_physmem_main_t *vpm = &vm->physmem_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200141 vnet_main_t *vnm = vnet_get_main ();
142 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100143 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200144 vnet_sw_interface_t *sw;
145 vnet_hw_interface_t *hw;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000146 int i, num_vhost_queues;
Damjan Marion2df39092017-12-04 20:03:37 +0100147 int old_netns_fd = -1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200148 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000149 struct ifreq get_ifr = {.ifr_flags = 0 };
Damjan Marion8389fb92017-10-13 18:29:53 +0200150 size_t hdrsz;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200151 vhost_memory_t *vhost_mem = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200152 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100153 clib_error_t *err = 0;
Damjan Marion39807d02019-11-08 13:52:28 +0100154 unsigned int tap_features;
Aloys Augustin2857e782020-03-16 17:29:52 +0100155 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200156 char *host_if_name = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100157 unsigned int offload = 0;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000158 int sndbuf = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100159
160 if (args->id != ~0)
161 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700162 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100163 {
164 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
165 args->error = clib_error_return (0, "interface already exists");
166 return;
167 }
168 }
169 else
170 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700171 args->id = clib_bitmap_first_clear (tm->tap_ids);
172 }
Damjan Marion2df39092017-12-04 20:03:37 +0100173
Neale Rannscbe8d652018-04-27 04:42:47 -0700174 if (args->id > TAP_MAX_INSTANCE)
175 {
176 args->rv = VNET_API_ERROR_UNSPECIFIED;
177 args->error = clib_error_return (0, "cannot find free interface id");
178 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100179 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200180
Damjan Marion7c6102b2019-11-08 17:59:56 +0100181 pool_get_zero (vim->interfaces, vif);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200182
183 if (args->tap_flags & TAP_FLAG_TUN)
184 {
185 vif->type = VIRTIO_IF_TYPE_TUN;
186 ifr.ifr_flags |= IFF_TUN;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000187
188 /*
189 * From kernel 4.20, xdp support has been added in tun_sendmsg.
190 * If sndbuf == INT_MAX, vhost batches the packet and processes
191 * them using xdp data path for tun driver. It assumes packets
192 * are ethernet frames (It needs to be fixed).
193 * To avoid xdp data path in tun driver, sndbuf value should
194 * be < INT_MAX.
195 */
196 sndbuf = INT_MAX - 1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200197 }
198 else
199 {
200 vif->type = VIRTIO_IF_TYPE_TAP;
201 ifr.ifr_flags |= IFF_TAP;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000202 sndbuf = INT_MAX;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200203 }
204
Damjan Marion8389fb92017-10-13 18:29:53 +0200205 vif->dev_instance = vif - vim->interfaces;
Damjan Marion2df39092017-12-04 20:03:37 +0100206 vif->id = args->id;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100207 vif->num_txqs = thm->n_vlib_mains;
Aloys Augustin2857e782020-03-16 17:29:52 +0100208 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
Damjan Marion2df39092017-12-04 20:03:37 +0100209
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000210 if (args->tap_flags & TAP_FLAG_ATTACH)
211 {
212 if (args->host_if_name != NULL)
213 {
214 host_if_name = (char *) args->host_if_name;
215 clib_memcpy (ifr.ifr_name, host_if_name,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200216 clib_min (IFNAMSIZ, vec_len (host_if_name)));
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000217 }
218 else
219 {
220 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
221 err = clib_error_return (0, "host_if_name is not provided");
222 goto error;
223 }
224 if (args->host_namespace)
225 {
226 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
227 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
228 {
229 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
230 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
231 args->host_namespace);
232 goto error;
233 }
234 if (setns (nfd, CLONE_NEWNET) == -1)
235 {
236 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
237 args->error = clib_error_return_unix (0, "setns '%s'",
238 args->host_namespace);
239 goto error;
240 }
241 }
242 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100243
244 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200245 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100246 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
247 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
248 goto error;
249 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100250 vec_add1 (vif->tap_fds, tfd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100251 tap_log_dbg (vif, "open tap fd %d", tfd);
252
253 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
254 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
255 if ((tap_features & IFF_VNET_HDR) == 0)
256 {
257 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
258 args->error = clib_error_return (0, "vhost-net backend not available");
Damjan Marion8389fb92017-10-13 18:29:53 +0200259 goto error;
260 }
261
Damjan Marion7c6102b2019-11-08 17:59:56 +0100262 if ((tap_features & IFF_MULTI_QUEUE) == 0)
263 {
Aloys Augustin2857e782020-03-16 17:29:52 +0100264 if (vif->num_rxqs > 1)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100265 {
266 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
267 args->error = clib_error_return (0, "multiqueue not supported");
268 goto error;
269 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100270 vif->num_rxqs = vif->num_txqs = 1;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100271 }
272 else
273 ifr.ifr_flags |= IFF_MULTI_QUEUE;
274
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200275 hdrsz = sizeof (virtio_net_hdr_v1_t);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100276 if (args->tap_flags & TAP_FLAG_GSO)
277 {
278 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
279 vif->gso_enabled = 1;
280 }
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100281 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
282 {
283 offload = TUN_F_CSUM;
284 vif->csum_offload_enabled = 1;
285 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100286
287 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
288 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
289 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
290
291 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
292 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
293
294 if (!args->host_if_name)
295 host_if_name = ifr.ifr_ifrn.ifrn_name;
296 else
297 host_if_name = (char *) args->host_if_name;
298
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000299 /*
300 * unset the persistence when attaching to existing
301 * interface
302 */
303 if (args->tap_flags & TAP_FLAG_ATTACH)
304 {
305 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
306 tap_log_dbg (vif, "TUNSETPERSIST: unset");
307 }
308
309 /* set the persistence */
310 if (args->tap_flags & TAP_FLAG_PERSIST)
311 {
312 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
313 tap_log_dbg (vif, "TUNSETPERSIST: set");
314
315 /* verify persistence is set, read the flags */
316 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
317 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
318 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
319 {
320 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
321 args->error = clib_error_return (0, "persistence not supported");
322 goto error;
323 }
324 }
325
Aloys Augustin2857e782020-03-16 17:29:52 +0100326 /* create additional queues on the linux side.
327 * we create as many linux queue pairs as we have rx queues
328 */
329 for (i = 1; i < vif->num_rxqs; i++)
330 {
331 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
332 {
333 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
334 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
335 goto error;
336 }
337 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
338 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
339 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
340 vec_add1 (vif->tap_fds, qfd);
341 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100342
Aloys Augustin2857e782020-03-16 17:29:52 +0100343 for (i = 0; i < vif->num_rxqs; i++)
344 {
345 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
346 vif->tap_fds[i], hdrsz);
347 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100348
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000349 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
350 sndbuf);
351 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100352
Aloys Augustin2857e782020-03-16 17:29:52 +0100353 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
354 offload);
355 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
356
357 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
358 {
359 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
360 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
361 goto error;
362 }
363 }
364
365 /* open as many vhost-net fds as required and set ownership */
366 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
367 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100368 {
369 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
370 {
371 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
372 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
373 goto error;
374 }
375 vec_add1 (vif->vhost_fds, vfd);
376 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
377 _IOCTL (vfd, VHOST_SET_OWNER, 0);
378 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
379 }
380
381 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
382 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
383 vif->remote_features);
Damjan Marion8389fb92017-10-13 18:29:53 +0200384
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200385 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200386 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100387 args->rv = VNET_API_ERROR_UNSUPPORTED;
388 args->error = clib_error_return (0, "vhost-net backend doesn't support "
389 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200390 goto error;
391 }
392
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200393 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
394 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200395 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100396 args->rv = VNET_API_ERROR_UNSUPPORTED;
397 args->error = clib_error_return (0, "vhost-net backend doesn't support "
398 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200399 goto error;
400 }
401
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200402 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200403 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100404 args->rv = VNET_API_ERROR_UNSUPPORTED;
405 args->error = clib_error_return (0, "vhost-net backend doesn't support "
406 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200407 goto error;
408 }
409
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200410 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
411 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
412 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
413
414 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200415
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000416 if (!(args->tap_flags & TAP_FLAG_ATTACH))
Damjan Marion8389fb92017-10-13 18:29:53 +0200417 {
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000418 /* if namespace is specified, all further netlink messages should be executed
419 after we change our net namespace */
420 if (args->host_namespace)
Damjan Marion2df39092017-12-04 20:03:37 +0100421 {
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000422 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
423 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
424 {
425 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
426 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
427 args->host_namespace);
428 goto error;
429 }
430 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
431 host_if_name);
432 if (args->error)
433 {
434 args->rv = VNET_API_ERROR_NETLINK_ERROR;
435 goto error;
436 }
437 if (setns (nfd, CLONE_NEWNET) == -1)
438 {
439 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
440 args->error = clib_error_return_unix (0, "setns '%s'",
441 args->host_namespace);
442 goto error;
443 }
444 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
445 {
446 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
447 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200448 host_if_name);
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000449 goto error;
450 }
451 }
452 else if (host_if_name)
453 {
454 args->error =
455 vnet_netlink_set_link_name (vif->ifindex, host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100456 if (args->error)
457 {
458 args->rv = VNET_API_ERROR_NETLINK_ERROR;
459 goto error;
460 }
461 }
462 }
463
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200464 if (vif->type == VIRTIO_IF_TYPE_TAP)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200465 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200466 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
467 ethernet_mac_address_generate (args->host_mac_addr.bytes);
468 args->error = vnet_netlink_set_link_addr (vif->ifindex,
469 args->host_mac_addr.bytes);
470 if (args->error)
471 {
472 args->rv = VNET_API_ERROR_NETLINK_ERROR;
473 goto error;
474 }
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200475
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000476 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100477 {
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000478 args->error = vnet_netlink_set_link_master (vif->ifindex,
479 (char *)
480 args->host_bridge);
481 if (args->error)
482 {
483 args->rv = VNET_API_ERROR_NETLINK_ERROR;
484 goto error;
485 }
Damjan Marion91c6ef72017-12-01 13:34:24 +0100486 }
487 }
488
489 if (args->host_ip4_prefix_len)
490 {
491 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
492 &args->host_ip4_addr,
493 args->host_ip4_prefix_len);
494 if (args->error)
495 {
496 args->rv = VNET_API_ERROR_NETLINK_ERROR;
497 goto error;
498 }
499 }
500
501 if (args->host_ip6_prefix_len)
502 {
503 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
504 &args->host_ip6_addr,
505 args->host_ip6_prefix_len);
506 if (args->error)
507 {
508 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200509 goto error;
510 }
511 }
512
Damjan Marion2df39092017-12-04 20:03:37 +0100513 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
514 if (args->error)
515 {
516 args->rv = VNET_API_ERROR_NETLINK_ERROR;
517 goto error;
518 }
519
Damjan Marion7866c452018-01-18 13:35:11 +0100520 if (args->host_ip4_gw_set)
521 {
522 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
523 if (args->error)
524 {
525 args->rv = VNET_API_ERROR_NETLINK_ERROR;
526 goto error;
527 }
528 }
529
530 if (args->host_ip6_gw_set)
531 {
532 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
533 if (args->error)
534 {
535 args->rv = VNET_API_ERROR_NETLINK_ERROR;
536 goto error;
537 }
538 }
539
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200540 if (args->host_mtu_set)
541 {
542 args->error =
543 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
544 if (args->error)
545 {
546 args->rv = VNET_API_ERROR_NETLINK_ERROR;
547 goto error;
548 }
549 }
550 else if (tm->host_mtu_size != 0)
551 {
552 args->error =
553 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
554 if (args->error)
555 {
556 args->rv = VNET_API_ERROR_NETLINK_ERROR;
557 goto error;
558 }
559 args->host_mtu_set = 1;
560 args->host_mtu_size = tm->host_mtu_size;
561 }
562
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100563 /* switch back to old net namespace */
564 if (args->host_namespace)
565 {
566 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
567 {
568 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
569 args->error = clib_error_return_unix (0, "setns '%s'",
570 args->host_namespace);
571 goto error;
572 }
573 }
574
Aloys Augustin2857e782020-03-16 17:29:52 +0100575 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100576 {
577 if (i < vif->num_rxqs && (args->error =
578 virtio_vring_init (vm, vif, RX_QUEUE (i),
579 args->rx_ring_sz)))
580 {
581 args->rv = VNET_API_ERROR_INIT_FAILED;
582 goto error;
583 }
584
585 if (i < vif->num_txqs && (args->error =
586 virtio_vring_init (vm, vif, TX_QUEUE (i),
587 args->tx_ring_sz)))
588 {
589 args->rv = VNET_API_ERROR_INIT_FAILED;
590 goto error;
591 }
592 }
593
594 /* setup features and memtable */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200595 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200596 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400597 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200598 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800599 vhost_mem->regions[0].memory_size = vpm->max_size;
600 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
601 vhost_mem->regions[0].userspace_addr =
602 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200603
Damjan Marion7c6102b2019-11-08 17:59:56 +0100604 for (i = 0; i < vhost_mem->nregions; i++)
605 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
606 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
607 vhost_mem->regions[0].memory_size,
608 vhost_mem->regions[0].guest_phys_addr,
609 vhost_mem->regions[0].userspace_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200610
Damjan Marion7c6102b2019-11-08 17:59:56 +0100611
Aloys Augustin2857e782020-03-16 17:29:52 +0100612 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion8389fb92017-10-13 18:29:53 +0200613 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100614 int fd = vif->vhost_fds[i];
615 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
616 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
617 fd, vif->features);
618 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
619 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200620 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100621
622 /* finish initializing queue pair */
Aloys Augustin2857e782020-03-16 17:29:52 +0100623 for (i = 0; i < num_vhost_queues * 2; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100624 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200625 vhost_vring_addr_t addr = { 0 };
626 vhost_vring_state_t state = { 0 };
627 vhost_vring_file_t file = { 0 };
Damjan Marion7c6102b2019-11-08 17:59:56 +0100628 virtio_vring_t *vring;
629 u16 qp = i >> 1;
630 int fd = vif->vhost_fds[qp];
631
632 if (i & 1)
633 {
634 if (qp >= vif->num_txqs)
635 continue;
636 vring = vec_elt_at_index (vif->txq_vrings, qp);
637 }
638 else
639 {
640 if (qp >= vif->num_rxqs)
641 continue;
642 vring = vec_elt_at_index (vif->rxq_vrings, qp);
643 }
644
645 addr.index = state.index = file.index = vring->queue_id & 1;
646 state.num = vring->size;
647 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
648 state.index, state.num);
649 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
650
651 addr.flags = 0;
652 addr.desc_user_addr = pointer_to_uword (vring->desc);
653 addr.avail_user_addr = pointer_to_uword (vring->avail);
654 addr.used_user_addr = pointer_to_uword (vring->used);
655
656 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
657 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
658 "used_user_addr 0x%lx", fd, addr.index,
659 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
660 addr.used_user_addr);
661 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
662
663 file.fd = vring->call_fd;
664 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
665 fd, file.index, file.fd);
666 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
667
668 file.fd = vring->kick_fd;
669 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
670 fd, file.index, file.fd);
671 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
672
Aloys Augustin2857e782020-03-16 17:29:52 +0100673 file.fd = vif->tap_fds[qp % vif->num_rxqs];
Damjan Marion7c6102b2019-11-08 17:59:56 +0100674 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
675 fd, file.index, file.fd);
676 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
677 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200678
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200679 if (vif->type == VIRTIO_IF_TYPE_TAP)
680 {
681 if (!args->mac_addr_set)
682 ethernet_mac_address_generate (args->mac_addr.bytes);
Damjan Marion8389fb92017-10-13 18:29:53 +0200683
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200684 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000685 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200686 }
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200687 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
Benoît Gannec30d87e2019-07-15 17:16:49 +0200688 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200689 vif->host_mtu_size = args->host_mtu_size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200690 vif->tap_flags = args->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200691 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100692 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
693 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
694 if (args->host_ip4_prefix_len)
695 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
696 if (args->host_ip6_prefix_len)
697 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
698
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200699 if (vif->type != VIRTIO_IF_TYPE_TUN)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100700 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200701 args->error =
702 ethernet_register_interface (vnm, virtio_device_class.index,
703 vif->dev_instance, vif->mac_addr,
704 &vif->hw_if_index,
705 virtio_eth_flag_change);
706 if (args->error)
707 {
708 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
709 goto error;
710 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200711
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200712 }
713 else
714 {
715 vif->hw_if_index = vnet_register_interface
716 (vnm, virtio_device_class.index,
717 vif->dev_instance /* device instance */ ,
718 tun_device_hw_interface_class.index, vif->dev_instance);
719
720 }
Neale Rannscbe8d652018-04-27 04:42:47 -0700721 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200722 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
723 vif->sw_if_index = sw->sw_if_index;
724 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200725 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200726 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
727 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200728 if (args->tap_flags & TAP_FLAG_GSO)
729 {
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100730 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
731 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
732 }
733 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
734 {
735 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200736 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200737 if ((args->tap_flags & TAP_FLAG_GSO)
738 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
739 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200740 virtio_set_packet_coalesce (vif);
741 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200742 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
743 virtio_input_node.index);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100744
745 for (i = 0; i < vif->num_rxqs; i++)
746 {
747 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
748 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
Damjan Marioneabd4242020-10-07 20:59:07 +0200749 VNET_HW_IF_RX_MODE_DEFAULT);
Mohsin Kazmi547a6162020-03-18 13:17:00 +0100750 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
Damjan Marion7c6102b2019-11-08 17:59:56 +0100751 }
752
Damjan Marion8389fb92017-10-13 18:29:53 +0200753 vif->per_interface_next_index = ~0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200754 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
755 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
756 VNET_HW_INTERFACE_FLAG_LINK_UP);
Matthew Smithbd50ed12020-07-24 13:38:03 -0500757 /*
758 * Host tun/tap driver link carrier state is "up" at creation. The
759 * driver never changes this unless the backend (VPP) changes it using
760 * TUNSETCARRIER ioctl(). See tap_set_carrier().
761 */
762 vif->host_carrier_up = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000763 vif->cxq_vring = NULL;
764
Damjan Marion8389fb92017-10-13 18:29:53 +0200765 goto done;
766
767error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100768 if (err)
769 {
770 ASSERT (args->error == 0);
771 args->error = err;
772 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
773 }
Benoît Gannec30d87e2019-07-15 17:16:49 +0200774
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100775 tap_log_err (vif, "%U", format_clib_error, args->error);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100776 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200777done:
778 if (vhost_mem)
779 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100780 if (old_netns_fd != -1)
781 close (old_netns_fd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100782 if (nfd != -1)
783 close (nfd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200784}
785
786int
787tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
788{
789 vnet_main_t *vnm = vnet_get_main ();
790 virtio_main_t *mm = &virtio_main;
791 int i;
792 virtio_if_t *vif;
793 vnet_hw_interface_t *hw;
794
Dave Barach3940de32019-07-23 16:28:36 -0400795 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200796 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
797 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
798
799 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
800
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500801 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200802 return VNET_API_ERROR_INVALID_INTERFACE;
803
Damjan Marion8389fb92017-10-13 18:29:53 +0200804 /* bring down the interface */
805 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
806 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100807 for (i = 0; i < vif->num_rxqs; i++)
808 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200809
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500810 if (vif->type == VIRTIO_IF_TYPE_TAP)
811 ethernet_delete_interface (vnm, vif->hw_if_index);
812 else /* VIRTIO_IF_TYPE_TUN */
813 vnet_delete_hw_interface (vnm, vif->hw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200814 vif->hw_if_index = ~0;
815
Damjan Marion7c6102b2019-11-08 17:59:56 +0100816 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200817
818 return 0;
819}
820
821int
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100822tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
823 int enable_disable)
824{
825 vnet_main_t *vnm = vnet_get_main ();
826 virtio_main_t *mm = &virtio_main;
827 virtio_if_t *vif;
828 vnet_hw_interface_t *hw;
829 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100830 int i = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100831
832 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
833
834 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
835 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
836
837 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
838
839 const unsigned int csum_offload_on = TUN_F_CSUM;
840 const unsigned int csum_offload_off = 0;
841 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100842 vec_foreach_index (i, vif->tap_fds)
843 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100844 vif->gso_enabled = 0;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200845 vif->packet_coalesce = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100846 vif->csum_offload_enabled = enable_disable ? 1 : 0;
847
848 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
849 {
850 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
851 }
852
853 if (enable_disable)
854 {
855 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) ==
856 0)
857 {
858 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
859 }
860 }
861 else
862 {
863 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) !=
864 0)
865 {
866 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
867 }
868 }
869
870error:
871 if (err)
872 {
873 clib_warning ("Error %s checksum offload on sw_if_index %d",
874 enable_disable ? "enabling" : "disabling", sw_if_index);
875 return VNET_API_ERROR_SYSCALL_ERROR_3;
876 }
877 return 0;
878}
879
880int
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200881tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
882 int is_packet_coalesce)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200883{
884 vnet_main_t *vnm = vnet_get_main ();
885 virtio_main_t *mm = &virtio_main;
886 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400887 vnet_hw_interface_t *hw;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200888 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100889 int i = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200890
Dave Barach3940de32019-07-23 16:28:36 -0400891 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
892
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200893 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
894 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
895
896 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
897
898 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
899 const unsigned int gso_off = 0;
900 unsigned int offload = enable_disable ? gso_on : gso_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100901 vec_foreach_index (i, vif->tap_fds)
902 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200903 vif->gso_enabled = enable_disable ? 1 : 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100904 vif->csum_offload_enabled = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200905 if (enable_disable)
906 {
907 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
908 {
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100909 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
910 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200911 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200912 if (is_packet_coalesce)
913 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200914 virtio_set_packet_coalesce (vif);
915 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200916 }
917 else
918 {
919 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
920 {
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100921 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
922 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200923 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200924 vif->packet_coalesce = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200925 }
926
927error:
928 if (err)
929 {
930 clib_warning ("Error %s gso on sw_if_index %d",
931 enable_disable ? "enabling" : "disabling", sw_if_index);
932 return VNET_API_ERROR_SYSCALL_ERROR_3;
933 }
934 return 0;
935}
936
937int
Damjan Marion8389fb92017-10-13 18:29:53 +0200938tap_dump_ifs (tap_interface_details_t ** out_tapids)
939{
940 vnet_main_t *vnm = vnet_get_main ();
941 virtio_main_t *mm = &virtio_main;
942 virtio_if_t *vif;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000943 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200944 vnet_hw_interface_t *hi;
945 tap_interface_details_t *r_tapids = NULL;
946 tap_interface_details_t *tapid = NULL;
947
948 /* *INDENT-OFF* */
949 pool_foreach (vif, mm->interfaces,
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200950 if ((vif->type != VIRTIO_IF_TYPE_TAP)
951 && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200952 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200953 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400954 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100955 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200956 tapid->sw_if_index = vif->sw_if_index;
957 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
958 clib_memcpy(tapid->dev_name, hi->name,
Vladimir Isaev84f3d9f2020-09-18 14:43:29 +0300959 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000960 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
961 tapid->rx_ring_sz = vring->size;
962 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
963 tapid->tx_ring_sz = vring->size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200964 tapid->tap_flags = vif->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200965 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100966 if (vif->host_if_name)
967 {
968 clib_memcpy(tapid->host_if_name, vif->host_if_name,
969 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200970 vec_len (vif->host_if_name)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100971 }
972 if (vif->net_ns)
973 {
974 clib_memcpy(tapid->host_namespace, vif->net_ns,
975 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200976 vec_len (vif->net_ns)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100977 }
978 if (vif->host_bridge)
979 {
980 clib_memcpy(tapid->host_bridge, vif->host_bridge,
981 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200982 vec_len (vif->host_bridge)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100983 }
984 if (vif->host_ip4_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200985 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
Milan Lenco73e7f422017-12-14 10:04:25 +0100986 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
987 if (vif->host_ip6_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200988 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
Milan Lenco73e7f422017-12-14 10:04:25 +0100989 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200990 tapid->host_mtu_size = vif->host_mtu_size;
Damjan Marion8389fb92017-10-13 18:29:53 +0200991 );
992 /* *INDENT-ON* */
993
994 *out_tapids = r_tapids;
995
996 return 0;
997}
998
Matthew Smithbd50ed12020-07-24 13:38:03 -0500999/*
1000 * Set host tap/tun interface carrier state so it will appear to host
1001 * applications that the interface's link state changed.
1002 *
1003 * If the kernel we're building against does not have support for the
1004 * TUNSETCARRIER ioctl command, do nothing.
1005 */
1006int
1007tap_set_carrier (u32 hw_if_index, u32 carrier_up)
1008{
1009 int ret = 0;
1010#ifdef TUNSETCARRIER
1011 vnet_main_t *vnm = vnet_get_main ();
1012 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1013 virtio_main_t *mm = &virtio_main;
1014 virtio_if_t *vif;
1015 int *fd;
1016
1017 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1018 vec_foreach (fd, vif->tap_fds)
1019 {
1020 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
1021 if (ret < 0)
1022 {
1023 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
1024 break;
1025 }
1026 }
1027 if (!ret)
1028 vif->host_carrier_up = (carrier_up != 0);
1029#endif
1030
1031 return ret;
1032}
1033
Damjan Marion8389fb92017-10-13 18:29:53 +02001034static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001035tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
1036{
1037 tap_main_t *tm = &tap_main;
1038
1039 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1040 {
1041 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
1042 ;
1043 else
1044 return clib_error_return (0, "unknown input `%U'",
1045 format_unformat_error, input);
1046 }
1047
1048 return 0;
1049}
1050
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001051/*
1052 * Set host tap/tun interface speed in Mbps.
1053 */
1054int
1055tap_set_speed (u32 hw_if_index, u32 speed)
1056{
1057 vnet_main_t *vnm = vnet_get_main ();
1058 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1059 virtio_main_t *mm = &virtio_main;
1060 virtio_if_t *vif;
1061 int old_netns_fd = -1;
1062 int nfd = -1;
1063 int ctl_fd = -1;
1064 struct ifreq ifr;
1065 struct ethtool_cmd ecmd;
1066 int ret = -1;
1067
1068 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1069
1070 if (vif->net_ns)
1071 {
1072 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
1073 if ((nfd = open_netns_fd ((char *) vif->net_ns)) == -1)
1074 {
1075 clib_warning ("Cannot open netns");
1076 goto done;
1077 }
1078 if (setns (nfd, CLONE_NEWNET) == -1)
1079 {
1080 clib_warning ("Cannot set ns");
1081 goto done;
1082 }
1083 }
1084
1085 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1086 {
1087 clib_warning ("Cannot open control socket");
1088 goto done;
1089 }
1090
1091 ecmd.cmd = ETHTOOL_GSET;
1092 clib_memset (&ifr, 0, sizeof (ifr));
1093 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1094 strlen ((const char *) vif->host_if_name));
1095 ifr.ifr_data = (void *) &ecmd;
1096 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1097 {
1098 clib_warning ("Cannot get device settings");
1099 goto done;
1100 }
1101
1102 if (ethtool_cmd_speed (&ecmd) != speed)
1103 {
1104 ecmd.cmd = ETHTOOL_SSET;
1105 ethtool_cmd_speed_set (&ecmd, speed);
1106 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1107 {
1108 clib_warning ("Cannot set device settings");
1109 goto done;
1110 }
1111 }
1112
1113done:
1114 if (old_netns_fd != -1)
1115 {
1116 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
1117 {
1118 clib_warning ("Cannot set old ns");
1119 }
1120 close (old_netns_fd);
1121 }
1122 if (nfd != -1)
1123 close (nfd);
1124 if (ctl_fd != -1)
1125 close (ctl_fd);
1126
1127 return ret;
1128}
1129
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001130/* tap { host-mtu <size> } configuration. */
1131VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1132
1133static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +02001134tap_init (vlib_main_t * vm)
1135{
Damjan Marion2df39092017-12-04 20:03:37 +01001136 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001137 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -07001138
Damjan Marion07a38572018-01-21 06:44:18 -08001139 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001140 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -07001141
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001142 tm->host_mtu_size = 0;
1143
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001144 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +02001145}
1146
1147VLIB_INIT_FUNCTION (tap_init);
1148
1149/*
1150 * fd.io coding-style-patch-verification: ON
1151 *
1152 * Local Variables:
1153 * eval: (c-set-style "gnu")
1154 * End:
1155 */