blob: 8fca35df2025ddf29d75bd2ecb0b22f8e2f0ead1 [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
Damjan Marion2df39092017-12-04 20:03:37 +010018#define _GNU_SOURCE
Damjan Marion8389fb92017-10-13 18:29:53 +020019#include <sys/types.h>
20#include <sys/stat.h>
Alexander Chernavina6c34a12020-09-04 09:24:20 -040021#include <sys/socket.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020022#include <fcntl.h>
23#include <net/if.h>
24#include <linux/if_tun.h>
25#include <sys/ioctl.h>
Alexander Chernavina6c34a12020-09-04 09:24:20 -040026#include <linux/ethtool.h>
27#include <linux/sockios.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020028#include <sys/eventfd.h>
Damjan Marion7c6102b2019-11-08 17:59:56 +010029#include <net/if_arp.h>
Damjan Marion7c6102b2019-11-08 17:59:56 +010030#include <limits.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020031
32#include <linux/netlink.h>
33#include <linux/rtnetlink.h>
34
35#include <vlib/vlib.h>
Lijian.Zhangba0da572019-08-21 17:51:16 +080036#include <vlib/physmem.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020037#include <vlib/unix/unix.h>
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +020038#include <vppinfra/linux/netns.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020039#include <vnet/ethernet/ethernet.h>
Damjan Marion91c6ef72017-12-01 13:34:24 +010040#include <vnet/ip/ip4_packet.h>
41#include <vnet/ip/ip6_packet.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010042#include <vnet/devices/netlink.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020043#include <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010044#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020045
Damjan Marion2df39092017-12-04 20:03:37 +010046tap_main_t tap_main;
47
Damjan Marion7c6102b2019-11-08 17:59:56 +010048#define tap_log_err(dev, f, ...) \
49 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
50#define tap_log_dbg(dev, f, ...) \
51 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
52
Damjan Marion8389fb92017-10-13 18:29:53 +020053#define _IOCTL(fd,a,...) \
54 if (ioctl (fd, a, __VA_ARGS__) < 0) \
55 { \
56 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
Damjan Marion7c6102b2019-11-08 17:59:56 +010057 tap_log_err (vif, "%U", format_clib_error, err); \
Damjan Marion8389fb92017-10-13 18:29:53 +020058 goto error; \
59 }
60
Mohsin Kazmi206acf82020-04-06 14:19:54 +020061 /* *INDENT-OFF* */
62VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) =
63{
64 .name = "tun-device",
65 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
66};
67 /* *INDENT-ON* */
68
Neale Ranns0cc23b72021-05-20 16:09:40 +000069#define TUN_MAX_PACKET_BYTES 65355
70#define TUN_MIN_PACKET_BYTES 64
71#define TUN_DEFAULT_PACKET_BYTES 1500
72
Damjan Marion8389fb92017-10-13 18:29:53 +020073static u32
74virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
75 u32 flags)
76{
77 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010078 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020079 return 0;
80}
81
Neale Rannscbe8d652018-04-27 04:42:47 -070082#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010083
Damjan Marion7c6102b2019-11-08 17:59:56 +010084static void
85tap_free (vlib_main_t * vm, virtio_if_t * vif)
86{
87 virtio_main_t *mm = &virtio_main;
88 tap_main_t *tm = &tap_main;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +000089 clib_error_t *err = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +010090 int i;
91
92 /* *INDENT-OFF* */
93 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
94 close (vif->vhost_fds[i]);
95 vec_foreach_index (i, vif->rxq_vrings)
96 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
97 vec_foreach_index (i, vif->txq_vrings)
98 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
99 /* *INDENT-ON* */
100
Mohsin Kazmi108b15b2020-10-28 19:35:53 +0100101 if (vif->tap_fds)
102 {
103 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
104 tap_log_dbg (vif, "TUNSETPERSIST: unset");
105 }
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000106error:
Aloys Augustin2857e782020-03-16 17:29:52 +0100107 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100108
109 vec_free (vif->vhost_fds);
110 vec_free (vif->rxq_vrings);
111 vec_free (vif->txq_vrings);
112 vec_free (vif->host_if_name);
113 vec_free (vif->net_ns);
114 vec_free (vif->host_bridge);
115 clib_error_free (vif->error);
116
117 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
118 clib_memset (vif, 0, sizeof (*vif));
119 pool_put (mm->interfaces, vif);
120}
121
Damjan Marion91c6ef72017-12-01 13:34:24 +0100122void
Damjan Marion8389fb92017-10-13 18:29:53 +0200123tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
124{
Damjan Marion7c6102b2019-11-08 17:59:56 +0100125 vlib_thread_main_t *thm = vlib_get_thread_main ();
Lijian.Zhangba0da572019-08-21 17:51:16 +0800126 vlib_physmem_main_t *vpm = &vm->physmem_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200127 vnet_main_t *vnm = vnet_get_main ();
128 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100129 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200130 vnet_sw_interface_t *sw;
131 vnet_hw_interface_t *hw;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000132 int i, num_vhost_queues;
Damjan Marion2df39092017-12-04 20:03:37 +0100133 int old_netns_fd = -1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200134 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000135 struct ifreq get_ifr = {.ifr_flags = 0 };
Damjan Marion8389fb92017-10-13 18:29:53 +0200136 size_t hdrsz;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200137 vhost_memory_t *vhost_mem = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200138 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100139 clib_error_t *err = 0;
Damjan Marion39807d02019-11-08 13:52:28 +0100140 unsigned int tap_features;
Aloys Augustin2857e782020-03-16 17:29:52 +0100141 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200142 char *host_if_name = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100143 unsigned int offload = 0;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000144 int sndbuf = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100145
146 if (args->id != ~0)
147 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700148 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100149 {
150 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
151 args->error = clib_error_return (0, "interface already exists");
152 return;
153 }
154 }
155 else
156 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700157 args->id = clib_bitmap_first_clear (tm->tap_ids);
158 }
Damjan Marion2df39092017-12-04 20:03:37 +0100159
Neale Rannscbe8d652018-04-27 04:42:47 -0700160 if (args->id > TAP_MAX_INSTANCE)
161 {
162 args->rv = VNET_API_ERROR_UNSPECIFIED;
163 args->error = clib_error_return (0, "cannot find free interface id");
164 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100165 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200166
Damjan Marion7c6102b2019-11-08 17:59:56 +0100167 pool_get_zero (vim->interfaces, vif);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200168
169 if (args->tap_flags & TAP_FLAG_TUN)
170 {
171 vif->type = VIRTIO_IF_TYPE_TUN;
172 ifr.ifr_flags |= IFF_TUN;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000173
174 /*
175 * From kernel 4.20, xdp support has been added in tun_sendmsg.
176 * If sndbuf == INT_MAX, vhost batches the packet and processes
177 * them using xdp data path for tun driver. It assumes packets
178 * are ethernet frames (It needs to be fixed).
179 * To avoid xdp data path in tun driver, sndbuf value should
180 * be < INT_MAX.
181 */
182 sndbuf = INT_MAX - 1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200183 }
184 else
185 {
186 vif->type = VIRTIO_IF_TYPE_TAP;
187 ifr.ifr_flags |= IFF_TAP;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000188 sndbuf = INT_MAX;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200189 }
190
Damjan Marion8389fb92017-10-13 18:29:53 +0200191 vif->dev_instance = vif - vim->interfaces;
Damjan Marion2df39092017-12-04 20:03:37 +0100192 vif->id = args->id;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100193 vif->num_txqs = thm->n_vlib_mains;
Aloys Augustin2857e782020-03-16 17:29:52 +0100194 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
Damjan Marion2df39092017-12-04 20:03:37 +0100195
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000196 if (args->tap_flags & TAP_FLAG_ATTACH)
197 {
Duncan Eastoe764a8782021-09-08 19:11:33 +0100198 if (args->host_if_name == NULL)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000199 {
200 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
201 err = clib_error_return (0, "host_if_name is not provided");
202 goto error;
203 }
204 if (args->host_namespace)
205 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200206 old_netns_fd = clib_netns_open (NULL /* self */);
207 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000208 {
209 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200210 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000211 args->host_namespace);
212 goto error;
213 }
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200214 if (clib_setns (nfd) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000215 {
216 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
217 args->error = clib_error_return_unix (0, "setns '%s'",
218 args->host_namespace);
219 goto error;
220 }
221 }
222 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100223
Duncan Eastoe764a8782021-09-08 19:11:33 +0100224 if (args->host_if_name != NULL)
225 {
226 host_if_name = (char *) args->host_if_name;
227 clib_memcpy (ifr.ifr_name, host_if_name,
228 clib_min (IFNAMSIZ, vec_len (host_if_name)));
229 }
230
Aloys Augustin2857e782020-03-16 17:29:52 +0100231 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200232 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100233 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
234 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
235 goto error;
236 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100237 vec_add1 (vif->tap_fds, tfd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100238 tap_log_dbg (vif, "open tap fd %d", tfd);
239
240 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
241 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
242 if ((tap_features & IFF_VNET_HDR) == 0)
243 {
244 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
245 args->error = clib_error_return (0, "vhost-net backend not available");
Damjan Marion8389fb92017-10-13 18:29:53 +0200246 goto error;
247 }
248
Damjan Marion7c6102b2019-11-08 17:59:56 +0100249 if ((tap_features & IFF_MULTI_QUEUE) == 0)
250 {
Aloys Augustin2857e782020-03-16 17:29:52 +0100251 if (vif->num_rxqs > 1)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100252 {
253 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
254 args->error = clib_error_return (0, "multiqueue not supported");
255 goto error;
256 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100257 vif->num_rxqs = vif->num_txqs = 1;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100258 }
259 else
260 ifr.ifr_flags |= IFF_MULTI_QUEUE;
261
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200262 hdrsz = sizeof (virtio_net_hdr_v1_t);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100263 if (args->tap_flags & TAP_FLAG_GSO)
264 {
265 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
266 vif->gso_enabled = 1;
267 }
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100268 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
269 {
270 offload = TUN_F_CSUM;
271 vif->csum_offload_enabled = 1;
272 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100273
274 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
275 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
276 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
277
278 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
279 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
280
281 if (!args->host_if_name)
282 host_if_name = ifr.ifr_ifrn.ifrn_name;
283 else
284 host_if_name = (char *) args->host_if_name;
285
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000286 /*
287 * unset the persistence when attaching to existing
288 * interface
289 */
290 if (args->tap_flags & TAP_FLAG_ATTACH)
291 {
292 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
293 tap_log_dbg (vif, "TUNSETPERSIST: unset");
294 }
295
296 /* set the persistence */
297 if (args->tap_flags & TAP_FLAG_PERSIST)
298 {
299 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
300 tap_log_dbg (vif, "TUNSETPERSIST: set");
301
302 /* verify persistence is set, read the flags */
303 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
304 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
305 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
306 {
307 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
308 args->error = clib_error_return (0, "persistence not supported");
309 goto error;
310 }
311 }
312
Aloys Augustin2857e782020-03-16 17:29:52 +0100313 /* create additional queues on the linux side.
314 * we create as many linux queue pairs as we have rx queues
315 */
316 for (i = 1; i < vif->num_rxqs; i++)
317 {
318 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
319 {
320 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
321 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
322 goto error;
323 }
324 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
325 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
326 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
327 vec_add1 (vif->tap_fds, qfd);
328 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100329
Aloys Augustin2857e782020-03-16 17:29:52 +0100330 for (i = 0; i < vif->num_rxqs; i++)
331 {
332 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
333 vif->tap_fds[i], hdrsz);
334 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100335
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000336 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
337 sndbuf);
338 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100339
Aloys Augustin2857e782020-03-16 17:29:52 +0100340 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
341 offload);
342 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
343
344 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
345 {
346 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
347 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
348 goto error;
349 }
350 }
351
352 /* open as many vhost-net fds as required and set ownership */
353 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
354 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100355 {
356 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
357 {
358 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
359 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
360 goto error;
361 }
362 vec_add1 (vif->vhost_fds, vfd);
363 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
364 _IOCTL (vfd, VHOST_SET_OWNER, 0);
365 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
366 }
367
368 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
369 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
370 vif->remote_features);
Damjan Marion8389fb92017-10-13 18:29:53 +0200371
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200372 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200373 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100374 args->rv = VNET_API_ERROR_UNSUPPORTED;
375 args->error = clib_error_return (0, "vhost-net backend doesn't support "
376 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200377 goto error;
378 }
379
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200380 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
381 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_RING_F_INDIRECT_DESC 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_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200390 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100391 args->rv = VNET_API_ERROR_UNSUPPORTED;
392 args->error = clib_error_return (0, "vhost-net backend doesn't support "
393 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200394 goto error;
395 }
396
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200397 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
398 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
399 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
400
401 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200402
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000403 if (!(args->tap_flags & TAP_FLAG_ATTACH))
Damjan Marion8389fb92017-10-13 18:29:53 +0200404 {
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000405 /* if namespace is specified, all further netlink messages should be executed
406 after we change our net namespace */
407 if (args->host_namespace)
Damjan Marion2df39092017-12-04 20:03:37 +0100408 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200409 old_netns_fd = clib_netns_open (NULL /* self */);
410 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000411 {
412 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200413 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000414 args->host_namespace);
415 goto error;
416 }
417 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
418 host_if_name);
419 if (args->error)
420 {
421 args->rv = VNET_API_ERROR_NETLINK_ERROR;
422 goto error;
423 }
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200424 if (clib_setns (nfd) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000425 {
426 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
427 args->error = clib_error_return_unix (0, "setns '%s'",
428 args->host_namespace);
429 goto error;
430 }
431 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
432 {
433 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
434 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200435 host_if_name);
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000436 goto error;
437 }
438 }
Damjan Marion2df39092017-12-04 20:03:37 +0100439 }
440
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200441 if (vif->type == VIRTIO_IF_TYPE_TAP)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200442 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200443 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
444 ethernet_mac_address_generate (args->host_mac_addr.bytes);
445 args->error = vnet_netlink_set_link_addr (vif->ifindex,
446 args->host_mac_addr.bytes);
447 if (args->error)
448 {
449 args->rv = VNET_API_ERROR_NETLINK_ERROR;
450 goto error;
451 }
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200452
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000453 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100454 {
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000455 args->error = vnet_netlink_set_link_master (vif->ifindex,
456 (char *)
457 args->host_bridge);
458 if (args->error)
459 {
460 args->rv = VNET_API_ERROR_NETLINK_ERROR;
461 goto error;
462 }
Damjan Marion91c6ef72017-12-01 13:34:24 +0100463 }
464 }
465
466 if (args->host_ip4_prefix_len)
467 {
468 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
469 &args->host_ip4_addr,
470 args->host_ip4_prefix_len);
471 if (args->error)
472 {
473 args->rv = VNET_API_ERROR_NETLINK_ERROR;
474 goto error;
475 }
476 }
477
478 if (args->host_ip6_prefix_len)
479 {
480 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
481 &args->host_ip6_addr,
482 args->host_ip6_prefix_len);
483 if (args->error)
484 {
485 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200486 goto error;
487 }
488 }
489
Damjan Marion2df39092017-12-04 20:03:37 +0100490 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
491 if (args->error)
492 {
493 args->rv = VNET_API_ERROR_NETLINK_ERROR;
494 goto error;
495 }
496
Damjan Marion7866c452018-01-18 13:35:11 +0100497 if (args->host_ip4_gw_set)
498 {
499 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
500 if (args->error)
501 {
502 args->rv = VNET_API_ERROR_NETLINK_ERROR;
503 goto error;
504 }
505 }
506
507 if (args->host_ip6_gw_set)
508 {
509 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
510 if (args->error)
511 {
512 args->rv = VNET_API_ERROR_NETLINK_ERROR;
513 goto error;
514 }
515 }
516
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200517 if (args->host_mtu_set)
518 {
519 args->error =
520 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
521 if (args->error)
522 {
523 args->rv = VNET_API_ERROR_NETLINK_ERROR;
524 goto error;
525 }
526 }
527 else if (tm->host_mtu_size != 0)
528 {
529 args->error =
530 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
531 if (args->error)
532 {
533 args->rv = VNET_API_ERROR_NETLINK_ERROR;
534 goto error;
535 }
536 args->host_mtu_set = 1;
537 args->host_mtu_size = tm->host_mtu_size;
538 }
539
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100540 /* switch back to old net namespace */
541 if (args->host_namespace)
542 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200543 if (clib_setns (old_netns_fd) == -1)
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100544 {
545 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
546 args->error = clib_error_return_unix (0, "setns '%s'",
547 args->host_namespace);
548 goto error;
549 }
550 }
551
Aloys Augustin2857e782020-03-16 17:29:52 +0100552 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100553 {
554 if (i < vif->num_rxqs && (args->error =
555 virtio_vring_init (vm, vif, RX_QUEUE (i),
556 args->rx_ring_sz)))
557 {
558 args->rv = VNET_API_ERROR_INIT_FAILED;
559 goto error;
560 }
561
562 if (i < vif->num_txqs && (args->error =
563 virtio_vring_init (vm, vif, TX_QUEUE (i),
564 args->tx_ring_sz)))
565 {
566 args->rv = VNET_API_ERROR_INIT_FAILED;
567 goto error;
568 }
569 }
570
571 /* setup features and memtable */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200572 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200573 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400574 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200575 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800576 vhost_mem->regions[0].memory_size = vpm->max_size;
577 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
578 vhost_mem->regions[0].userspace_addr =
579 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200580
Damjan Marion7c6102b2019-11-08 17:59:56 +0100581 for (i = 0; i < vhost_mem->nregions; i++)
582 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
583 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
584 vhost_mem->regions[0].memory_size,
585 vhost_mem->regions[0].guest_phys_addr,
586 vhost_mem->regions[0].userspace_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200587
Damjan Marion7c6102b2019-11-08 17:59:56 +0100588
Aloys Augustin2857e782020-03-16 17:29:52 +0100589 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion8389fb92017-10-13 18:29:53 +0200590 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100591 int fd = vif->vhost_fds[i];
592 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
593 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
594 fd, vif->features);
595 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
596 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200597 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100598
599 /* finish initializing queue pair */
Aloys Augustin2857e782020-03-16 17:29:52 +0100600 for (i = 0; i < num_vhost_queues * 2; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100601 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200602 vhost_vring_addr_t addr = { 0 };
603 vhost_vring_state_t state = { 0 };
604 vhost_vring_file_t file = { 0 };
Damjan Marion7c6102b2019-11-08 17:59:56 +0100605 virtio_vring_t *vring;
606 u16 qp = i >> 1;
607 int fd = vif->vhost_fds[qp];
608
609 if (i & 1)
610 {
611 if (qp >= vif->num_txqs)
612 continue;
613 vring = vec_elt_at_index (vif->txq_vrings, qp);
614 }
615 else
616 {
617 if (qp >= vif->num_rxqs)
618 continue;
619 vring = vec_elt_at_index (vif->rxq_vrings, qp);
620 }
621
622 addr.index = state.index = file.index = vring->queue_id & 1;
623 state.num = vring->size;
624 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
625 state.index, state.num);
626 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
627
628 addr.flags = 0;
629 addr.desc_user_addr = pointer_to_uword (vring->desc);
630 addr.avail_user_addr = pointer_to_uword (vring->avail);
631 addr.used_user_addr = pointer_to_uword (vring->used);
632
633 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
634 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
635 "used_user_addr 0x%lx", fd, addr.index,
636 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
637 addr.used_user_addr);
638 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
639
640 file.fd = vring->call_fd;
641 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
642 fd, file.index, file.fd);
643 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
644
645 file.fd = vring->kick_fd;
646 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
647 fd, file.index, file.fd);
648 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
649
Aloys Augustin2857e782020-03-16 17:29:52 +0100650 file.fd = vif->tap_fds[qp % vif->num_rxqs];
Damjan Marion7c6102b2019-11-08 17:59:56 +0100651 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
652 fd, file.index, file.fd);
653 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
654 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200655
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200656 if (vif->type == VIRTIO_IF_TYPE_TAP)
657 {
658 if (!args->mac_addr_set)
659 ethernet_mac_address_generate (args->mac_addr.bytes);
Damjan Marion8389fb92017-10-13 18:29:53 +0200660
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200661 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000662 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200663 }
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200664 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
Benoît Gannec30d87e2019-07-15 17:16:49 +0200665 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200666 vif->host_mtu_size = args->host_mtu_size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200667 vif->tap_flags = args->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200668 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100669 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
670 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
671 if (args->host_ip4_prefix_len)
672 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
673 if (args->host_ip6_prefix_len)
674 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
675
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200676 if (vif->type != VIRTIO_IF_TYPE_TUN)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100677 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200678 args->error =
679 ethernet_register_interface (vnm, virtio_device_class.index,
680 vif->dev_instance, vif->mac_addr,
681 &vif->hw_if_index,
682 virtio_eth_flag_change);
683 if (args->error)
684 {
685 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
686 goto error;
687 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200688
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200689 }
690 else
691 {
692 vif->hw_if_index = vnet_register_interface
693 (vnm, virtio_device_class.index,
694 vif->dev_instance /* device instance */ ,
695 tun_device_hw_interface_class.index, vif->dev_instance);
696
697 }
Neale Rannscbe8d652018-04-27 04:42:47 -0700698 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200699 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
700 vif->sw_if_index = sw->sw_if_index;
701 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200702 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200703 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100704 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200705 if (args->tap_flags & TAP_FLAG_GSO)
706 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100707 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
708 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
709 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100710 }
711 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
712 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100713 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
714 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200715 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200716 if ((args->tap_flags & TAP_FLAG_GSO)
717 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
718 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200719 virtio_set_packet_coalesce (vif);
720 }
Neale Ranns0cc23b72021-05-20 16:09:40 +0000721 if (vif->type == VIRTIO_IF_TYPE_TUN)
722 {
723 hw->max_supported_packet_bytes = TUN_MAX_PACKET_BYTES;
724 hw->min_packet_bytes = hw->min_supported_packet_bytes =
725 TUN_MIN_PACKET_BYTES;
726 hw->max_packet_bytes =
727 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES;
728 vnet_sw_interface_set_mtu (vnm, hw->sw_if_index, hw->max_packet_bytes);
729 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100730
Damjan Marion94100532020-11-06 23:25:57 +0100731 virtio_vring_set_rx_queues (vm, vif);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100732
Damjan Marion8389fb92017-10-13 18:29:53 +0200733 vif->per_interface_next_index = ~0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200734 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
735 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
736 VNET_HW_INTERFACE_FLAG_LINK_UP);
Matthew Smithbd50ed12020-07-24 13:38:03 -0500737 /*
738 * Host tun/tap driver link carrier state is "up" at creation. The
739 * driver never changes this unless the backend (VPP) changes it using
740 * TUNSETCARRIER ioctl(). See tap_set_carrier().
741 */
742 vif->host_carrier_up = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000743 vif->cxq_vring = NULL;
744
Damjan Marion8389fb92017-10-13 18:29:53 +0200745 goto done;
746
747error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100748 if (err)
749 {
750 ASSERT (args->error == 0);
751 args->error = err;
752 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
753 }
Benoît Gannec30d87e2019-07-15 17:16:49 +0200754
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100755 tap_log_err (vif, "%U", format_clib_error, args->error);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100756 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200757done:
758 if (vhost_mem)
759 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100760 if (old_netns_fd != -1)
761 close (old_netns_fd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100762 if (nfd != -1)
763 close (nfd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200764}
765
766int
767tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
768{
769 vnet_main_t *vnm = vnet_get_main ();
770 virtio_main_t *mm = &virtio_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200771 virtio_if_t *vif;
772 vnet_hw_interface_t *hw;
773
Dave Barach3940de32019-07-23 16:28:36 -0400774 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200775 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
776 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
777
778 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
779
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500780 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200781 return VNET_API_ERROR_INVALID_INTERFACE;
782
Damjan Marion8389fb92017-10-13 18:29:53 +0200783 /* bring down the interface */
784 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
785 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
786
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500787 if (vif->type == VIRTIO_IF_TYPE_TAP)
788 ethernet_delete_interface (vnm, vif->hw_if_index);
789 else /* VIRTIO_IF_TYPE_TUN */
790 vnet_delete_hw_interface (vnm, vif->hw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200791 vif->hw_if_index = ~0;
792
Damjan Marion7c6102b2019-11-08 17:59:56 +0100793 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200794
795 return 0;
796}
797
798int
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100799tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
800 int enable_disable)
801{
802 vnet_main_t *vnm = vnet_get_main ();
803 virtio_main_t *mm = &virtio_main;
804 virtio_if_t *vif;
805 vnet_hw_interface_t *hw;
806 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100807 int i = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100808
809 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
810
811 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
812 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
813
814 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
815
816 const unsigned int csum_offload_on = TUN_F_CSUM;
817 const unsigned int csum_offload_off = 0;
818 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100819 vec_foreach_index (i, vif->tap_fds)
820 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100821 vif->gso_enabled = 0;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200822 vif->packet_coalesce = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100823 vif->csum_offload_enabled = enable_disable ? 1 : 0;
824
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100825 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100826 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100827 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100828 }
829
830 if (enable_disable)
831 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100832 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100833 }
834 else
835 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100836 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100837 }
838
839error:
840 if (err)
841 {
842 clib_warning ("Error %s checksum offload on sw_if_index %d",
843 enable_disable ? "enabling" : "disabling", sw_if_index);
844 return VNET_API_ERROR_SYSCALL_ERROR_3;
845 }
846 return 0;
847}
848
849int
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200850tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
851 int is_packet_coalesce)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200852{
853 vnet_main_t *vnm = vnet_get_main ();
854 virtio_main_t *mm = &virtio_main;
855 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400856 vnet_hw_interface_t *hw;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200857 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100858 int i = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200859
Dave Barach3940de32019-07-23 16:28:36 -0400860 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
861
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200862 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
863 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
864
865 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
866
867 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
868 const unsigned int gso_off = 0;
869 unsigned int offload = enable_disable ? gso_on : gso_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100870 vec_foreach_index (i, vif->tap_fds)
871 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200872 vif->gso_enabled = enable_disable ? 1 : 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100873 vif->csum_offload_enabled = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200874 if (enable_disable)
875 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100876 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) == 0)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200877 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100878 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
879 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200880 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200881 if (is_packet_coalesce)
882 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200883 virtio_set_packet_coalesce (vif);
884 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200885 }
886 else
887 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100888 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200889 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100890 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
891 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200892 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200893 vif->packet_coalesce = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200894 }
895
896error:
897 if (err)
898 {
899 clib_warning ("Error %s gso on sw_if_index %d",
900 enable_disable ? "enabling" : "disabling", sw_if_index);
901 return VNET_API_ERROR_SYSCALL_ERROR_3;
902 }
903 return 0;
904}
905
906int
Damjan Marion8389fb92017-10-13 18:29:53 +0200907tap_dump_ifs (tap_interface_details_t ** out_tapids)
908{
909 vnet_main_t *vnm = vnet_get_main ();
910 virtio_main_t *mm = &virtio_main;
911 virtio_if_t *vif;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000912 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200913 vnet_hw_interface_t *hi;
914 tap_interface_details_t *r_tapids = NULL;
915 tap_interface_details_t *tapid = NULL;
916
917 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100918 pool_foreach (vif, mm->interfaces) {
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200919 if ((vif->type != VIRTIO_IF_TYPE_TAP)
920 && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200921 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200922 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400923 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100924 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200925 tapid->sw_if_index = vif->sw_if_index;
926 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
927 clib_memcpy(tapid->dev_name, hi->name,
Vladimir Isaev84f3d9f2020-09-18 14:43:29 +0300928 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000929 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
930 tapid->rx_ring_sz = vring->size;
931 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
932 tapid->tx_ring_sz = vring->size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200933 tapid->tap_flags = vif->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200934 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100935 if (vif->host_if_name)
936 {
937 clib_memcpy(tapid->host_if_name, vif->host_if_name,
938 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200939 vec_len (vif->host_if_name)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100940 }
941 if (vif->net_ns)
942 {
943 clib_memcpy(tapid->host_namespace, vif->net_ns,
944 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200945 vec_len (vif->net_ns)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100946 }
947 if (vif->host_bridge)
948 {
949 clib_memcpy(tapid->host_bridge, vif->host_bridge,
950 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200951 vec_len (vif->host_bridge)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100952 }
953 if (vif->host_ip4_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200954 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
Milan Lenco73e7f422017-12-14 10:04:25 +0100955 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
956 if (vif->host_ip6_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200957 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
Milan Lenco73e7f422017-12-14 10:04:25 +0100958 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200959 tapid->host_mtu_size = vif->host_mtu_size;
Damjan Marionb2c31b62020-12-13 21:47:40 +0100960 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200961 /* *INDENT-ON* */
962
963 *out_tapids = r_tapids;
964
965 return 0;
966}
967
Matthew Smithbd50ed12020-07-24 13:38:03 -0500968/*
969 * Set host tap/tun interface carrier state so it will appear to host
970 * applications that the interface's link state changed.
971 *
972 * If the kernel we're building against does not have support for the
973 * TUNSETCARRIER ioctl command, do nothing.
974 */
975int
976tap_set_carrier (u32 hw_if_index, u32 carrier_up)
977{
978 int ret = 0;
979#ifdef TUNSETCARRIER
980 vnet_main_t *vnm = vnet_get_main ();
981 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
982 virtio_main_t *mm = &virtio_main;
983 virtio_if_t *vif;
984 int *fd;
985
986 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
987 vec_foreach (fd, vif->tap_fds)
988 {
989 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
990 if (ret < 0)
991 {
992 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
993 break;
994 }
995 }
996 if (!ret)
997 vif->host_carrier_up = (carrier_up != 0);
998#endif
999
1000 return ret;
1001}
1002
Damjan Marion8389fb92017-10-13 18:29:53 +02001003static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001004tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
1005{
1006 tap_main_t *tm = &tap_main;
1007
1008 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1009 {
1010 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
1011 ;
1012 else
1013 return clib_error_return (0, "unknown input `%U'",
1014 format_unformat_error, input);
1015 }
1016
1017 return 0;
1018}
1019
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001020/*
1021 * Set host tap/tun interface speed in Mbps.
1022 */
1023int
1024tap_set_speed (u32 hw_if_index, u32 speed)
1025{
1026 vnet_main_t *vnm = vnet_get_main ();
1027 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1028 virtio_main_t *mm = &virtio_main;
1029 virtio_if_t *vif;
1030 int old_netns_fd = -1;
1031 int nfd = -1;
1032 int ctl_fd = -1;
1033 struct ifreq ifr;
1034 struct ethtool_cmd ecmd;
1035 int ret = -1;
1036
1037 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1038
1039 if (vif->net_ns)
1040 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001041 old_netns_fd = clib_netns_open (NULL /* self */);
1042 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001043 {
1044 clib_warning ("Cannot open netns");
1045 goto done;
1046 }
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001047 if (clib_setns (nfd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001048 {
1049 clib_warning ("Cannot set ns");
1050 goto done;
1051 }
1052 }
1053
1054 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1055 {
1056 clib_warning ("Cannot open control socket");
1057 goto done;
1058 }
1059
1060 ecmd.cmd = ETHTOOL_GSET;
1061 clib_memset (&ifr, 0, sizeof (ifr));
1062 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1063 strlen ((const char *) vif->host_if_name));
1064 ifr.ifr_data = (void *) &ecmd;
1065 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1066 {
1067 clib_warning ("Cannot get device settings");
1068 goto done;
1069 }
1070
1071 if (ethtool_cmd_speed (&ecmd) != speed)
1072 {
1073 ecmd.cmd = ETHTOOL_SSET;
1074 ethtool_cmd_speed_set (&ecmd, speed);
1075 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1076 {
1077 clib_warning ("Cannot set device settings");
1078 goto done;
1079 }
1080 }
1081
1082done:
1083 if (old_netns_fd != -1)
1084 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001085 if (clib_setns (old_netns_fd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001086 {
1087 clib_warning ("Cannot set old ns");
1088 }
1089 close (old_netns_fd);
1090 }
1091 if (nfd != -1)
1092 close (nfd);
1093 if (ctl_fd != -1)
1094 close (ctl_fd);
1095
1096 return ret;
1097}
1098
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001099/* tap { host-mtu <size> } configuration. */
1100VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1101
1102static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +02001103tap_init (vlib_main_t * vm)
1104{
Damjan Marion2df39092017-12-04 20:03:37 +01001105 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001106 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -07001107
Damjan Marion07a38572018-01-21 06:44:18 -08001108 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001109 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -07001110
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001111 tm->host_mtu_size = 0;
1112
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001113 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +02001114}
1115
1116VLIB_INIT_FUNCTION (tap_init);
1117
1118/*
1119 * fd.io coding-style-patch-verification: ON
1120 *
1121 * Local Variables:
1122 * eval: (c-set-style "gnu")
1123 * End:
1124 */