blob: 8bd03aa178e3d82836a22538cea6eaa5aa21f462 [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 }
Nathan Skrzypczak979545e2021-09-22 17:46:02 +0200204 }
205
206 /* if namespace is specified, all further netlink messages should be executed
207 * after we change our net namespace */
208 if (args->host_namespace)
209 {
210 old_netns_fd = clib_netns_open (NULL /* self */);
211 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000212 {
Nathan Skrzypczak979545e2021-09-22 17:46:02 +0200213 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
214 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
215 args->host_namespace);
216 goto error;
217 }
218 if (clib_setns (nfd) == -1)
219 {
220 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
221 args->error =
222 clib_error_return_unix (0, "setns '%s'", args->host_namespace);
223 goto error;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000224 }
225 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100226
Duncan Eastoe764a8782021-09-08 19:11:33 +0100227 if (args->host_if_name != NULL)
228 {
229 host_if_name = (char *) args->host_if_name;
230 clib_memcpy (ifr.ifr_name, host_if_name,
231 clib_min (IFNAMSIZ, vec_len (host_if_name)));
232 }
233
Aloys Augustin2857e782020-03-16 17:29:52 +0100234 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200235 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100236 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
237 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
238 goto error;
239 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100240 vec_add1 (vif->tap_fds, tfd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100241 tap_log_dbg (vif, "open tap fd %d", tfd);
242
243 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
244 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
245 if ((tap_features & IFF_VNET_HDR) == 0)
246 {
247 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
248 args->error = clib_error_return (0, "vhost-net backend not available");
Damjan Marion8389fb92017-10-13 18:29:53 +0200249 goto error;
250 }
251
Damjan Marion7c6102b2019-11-08 17:59:56 +0100252 if ((tap_features & IFF_MULTI_QUEUE) == 0)
253 {
Aloys Augustin2857e782020-03-16 17:29:52 +0100254 if (vif->num_rxqs > 1)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100255 {
256 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
257 args->error = clib_error_return (0, "multiqueue not supported");
258 goto error;
259 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100260 vif->num_rxqs = vif->num_txqs = 1;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100261 }
262 else
263 ifr.ifr_flags |= IFF_MULTI_QUEUE;
264
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200265 hdrsz = sizeof (virtio_net_hdr_v1_t);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100266 if (args->tap_flags & TAP_FLAG_GSO)
267 {
268 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
269 vif->gso_enabled = 1;
270 }
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100271 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
272 {
273 offload = TUN_F_CSUM;
274 vif->csum_offload_enabled = 1;
275 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100276
277 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
278 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
279 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
280
281 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
282 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
283
284 if (!args->host_if_name)
285 host_if_name = ifr.ifr_ifrn.ifrn_name;
286 else
287 host_if_name = (char *) args->host_if_name;
288
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000289 /*
290 * unset the persistence when attaching to existing
291 * interface
292 */
293 if (args->tap_flags & TAP_FLAG_ATTACH)
294 {
295 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
296 tap_log_dbg (vif, "TUNSETPERSIST: unset");
297 }
298
299 /* set the persistence */
300 if (args->tap_flags & TAP_FLAG_PERSIST)
301 {
302 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
303 tap_log_dbg (vif, "TUNSETPERSIST: set");
304
305 /* verify persistence is set, read the flags */
306 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
307 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
308 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
309 {
310 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
311 args->error = clib_error_return (0, "persistence not supported");
312 goto error;
313 }
314 }
315
Aloys Augustin2857e782020-03-16 17:29:52 +0100316 /* create additional queues on the linux side.
317 * we create as many linux queue pairs as we have rx queues
318 */
319 for (i = 1; i < vif->num_rxqs; i++)
320 {
321 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
322 {
323 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
324 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
325 goto error;
326 }
327 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
328 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
329 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
330 vec_add1 (vif->tap_fds, qfd);
331 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100332
Aloys Augustin2857e782020-03-16 17:29:52 +0100333 for (i = 0; i < vif->num_rxqs; i++)
334 {
335 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
336 vif->tap_fds[i], hdrsz);
337 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100338
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000339 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
340 sndbuf);
341 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100342
Aloys Augustin2857e782020-03-16 17:29:52 +0100343 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
344 offload);
345 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
346
347 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
348 {
349 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
350 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
351 goto error;
352 }
353 }
354
355 /* open as many vhost-net fds as required and set ownership */
356 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
357 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100358 {
359 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
360 {
361 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
362 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
363 goto error;
364 }
365 vec_add1 (vif->vhost_fds, vfd);
366 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
367 _IOCTL (vfd, VHOST_SET_OWNER, 0);
368 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
369 }
370
371 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
372 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
373 vif->remote_features);
Damjan Marion8389fb92017-10-13 18:29:53 +0200374
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200375 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200376 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100377 args->rv = VNET_API_ERROR_UNSUPPORTED;
378 args->error = clib_error_return (0, "vhost-net backend doesn't support "
379 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200380 goto error;
381 }
382
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200383 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
384 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200385 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100386 args->rv = VNET_API_ERROR_UNSUPPORTED;
387 args->error = clib_error_return (0, "vhost-net backend doesn't support "
388 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200389 goto error;
390 }
391
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200392 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200393 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100394 args->rv = VNET_API_ERROR_UNSUPPORTED;
395 args->error = clib_error_return (0, "vhost-net backend doesn't support "
396 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200397 goto error;
398 }
399
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200400 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
401 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
402 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
403
404 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200405
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200406 if (vif->type == VIRTIO_IF_TYPE_TAP)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200407 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200408 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
409 ethernet_mac_address_generate (args->host_mac_addr.bytes);
410 args->error = vnet_netlink_set_link_addr (vif->ifindex,
411 args->host_mac_addr.bytes);
412 if (args->error)
413 {
414 args->rv = VNET_API_ERROR_NETLINK_ERROR;
415 goto error;
416 }
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200417
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000418 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100419 {
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000420 args->error = vnet_netlink_set_link_master (vif->ifindex,
421 (char *)
422 args->host_bridge);
423 if (args->error)
424 {
425 args->rv = VNET_API_ERROR_NETLINK_ERROR;
426 goto error;
427 }
Damjan Marion91c6ef72017-12-01 13:34:24 +0100428 }
429 }
430
431 if (args->host_ip4_prefix_len)
432 {
433 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
434 &args->host_ip4_addr,
435 args->host_ip4_prefix_len);
436 if (args->error)
437 {
438 args->rv = VNET_API_ERROR_NETLINK_ERROR;
439 goto error;
440 }
441 }
442
443 if (args->host_ip6_prefix_len)
444 {
445 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
446 &args->host_ip6_addr,
447 args->host_ip6_prefix_len);
448 if (args->error)
449 {
450 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200451 goto error;
452 }
453 }
454
Damjan Marion2df39092017-12-04 20:03:37 +0100455 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
456 if (args->error)
457 {
458 args->rv = VNET_API_ERROR_NETLINK_ERROR;
459 goto error;
460 }
461
Damjan Marion7866c452018-01-18 13:35:11 +0100462 if (args->host_ip4_gw_set)
463 {
464 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
465 if (args->error)
466 {
467 args->rv = VNET_API_ERROR_NETLINK_ERROR;
468 goto error;
469 }
470 }
471
472 if (args->host_ip6_gw_set)
473 {
474 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
475 if (args->error)
476 {
477 args->rv = VNET_API_ERROR_NETLINK_ERROR;
478 goto error;
479 }
480 }
481
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200482 if (args->host_mtu_set)
483 {
484 args->error =
485 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
486 if (args->error)
487 {
488 args->rv = VNET_API_ERROR_NETLINK_ERROR;
489 goto error;
490 }
491 }
492 else if (tm->host_mtu_size != 0)
493 {
494 args->error =
495 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
496 if (args->error)
497 {
498 args->rv = VNET_API_ERROR_NETLINK_ERROR;
499 goto error;
500 }
501 args->host_mtu_set = 1;
502 args->host_mtu_size = tm->host_mtu_size;
503 }
504
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100505 /* switch back to old net namespace */
506 if (args->host_namespace)
507 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200508 if (clib_setns (old_netns_fd) == -1)
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100509 {
510 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
511 args->error = clib_error_return_unix (0, "setns '%s'",
512 args->host_namespace);
513 goto error;
514 }
515 }
516
Aloys Augustin2857e782020-03-16 17:29:52 +0100517 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100518 {
519 if (i < vif->num_rxqs && (args->error =
520 virtio_vring_init (vm, vif, RX_QUEUE (i),
521 args->rx_ring_sz)))
522 {
523 args->rv = VNET_API_ERROR_INIT_FAILED;
524 goto error;
525 }
526
527 if (i < vif->num_txqs && (args->error =
528 virtio_vring_init (vm, vif, TX_QUEUE (i),
529 args->tx_ring_sz)))
530 {
531 args->rv = VNET_API_ERROR_INIT_FAILED;
532 goto error;
533 }
534 }
535
536 /* setup features and memtable */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200537 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200538 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400539 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200540 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800541 vhost_mem->regions[0].memory_size = vpm->max_size;
542 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
543 vhost_mem->regions[0].userspace_addr =
544 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200545
Damjan Marion7c6102b2019-11-08 17:59:56 +0100546 for (i = 0; i < vhost_mem->nregions; i++)
547 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
548 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
549 vhost_mem->regions[0].memory_size,
550 vhost_mem->regions[0].guest_phys_addr,
551 vhost_mem->regions[0].userspace_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200552
Damjan Marion7c6102b2019-11-08 17:59:56 +0100553
Aloys Augustin2857e782020-03-16 17:29:52 +0100554 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion8389fb92017-10-13 18:29:53 +0200555 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100556 int fd = vif->vhost_fds[i];
557 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
558 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
559 fd, vif->features);
560 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
561 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200562 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100563
564 /* finish initializing queue pair */
Aloys Augustin2857e782020-03-16 17:29:52 +0100565 for (i = 0; i < num_vhost_queues * 2; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100566 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200567 vhost_vring_addr_t addr = { 0 };
568 vhost_vring_state_t state = { 0 };
569 vhost_vring_file_t file = { 0 };
Damjan Marion7c6102b2019-11-08 17:59:56 +0100570 virtio_vring_t *vring;
571 u16 qp = i >> 1;
572 int fd = vif->vhost_fds[qp];
573
574 if (i & 1)
575 {
576 if (qp >= vif->num_txqs)
577 continue;
578 vring = vec_elt_at_index (vif->txq_vrings, qp);
579 }
580 else
581 {
582 if (qp >= vif->num_rxqs)
583 continue;
584 vring = vec_elt_at_index (vif->rxq_vrings, qp);
585 }
586
587 addr.index = state.index = file.index = vring->queue_id & 1;
588 state.num = vring->size;
589 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
590 state.index, state.num);
591 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
592
593 addr.flags = 0;
594 addr.desc_user_addr = pointer_to_uword (vring->desc);
595 addr.avail_user_addr = pointer_to_uword (vring->avail);
596 addr.used_user_addr = pointer_to_uword (vring->used);
597
598 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
599 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
600 "used_user_addr 0x%lx", fd, addr.index,
601 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
602 addr.used_user_addr);
603 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
604
605 file.fd = vring->call_fd;
606 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
607 fd, file.index, file.fd);
608 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
609
610 file.fd = vring->kick_fd;
611 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
612 fd, file.index, file.fd);
613 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
614
Aloys Augustin2857e782020-03-16 17:29:52 +0100615 file.fd = vif->tap_fds[qp % vif->num_rxqs];
Damjan Marion7c6102b2019-11-08 17:59:56 +0100616 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
617 fd, file.index, file.fd);
618 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
619 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200620
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200621 if (vif->type == VIRTIO_IF_TYPE_TAP)
622 {
623 if (!args->mac_addr_set)
624 ethernet_mac_address_generate (args->mac_addr.bytes);
Damjan Marion8389fb92017-10-13 18:29:53 +0200625
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200626 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000627 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200628 }
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200629 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
Benoît Gannec30d87e2019-07-15 17:16:49 +0200630 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200631 vif->host_mtu_size = args->host_mtu_size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200632 vif->tap_flags = args->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200633 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100634 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
635 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
636 if (args->host_ip4_prefix_len)
637 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
638 if (args->host_ip6_prefix_len)
639 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
640
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200641 if (vif->type != VIRTIO_IF_TYPE_TUN)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100642 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200643 args->error =
644 ethernet_register_interface (vnm, virtio_device_class.index,
645 vif->dev_instance, vif->mac_addr,
646 &vif->hw_if_index,
647 virtio_eth_flag_change);
648 if (args->error)
649 {
650 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
651 goto error;
652 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200653
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200654 }
655 else
656 {
657 vif->hw_if_index = vnet_register_interface
658 (vnm, virtio_device_class.index,
659 vif->dev_instance /* device instance */ ,
660 tun_device_hw_interface_class.index, vif->dev_instance);
661
662 }
Neale Rannscbe8d652018-04-27 04:42:47 -0700663 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200664 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
665 vif->sw_if_index = sw->sw_if_index;
666 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200667 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200668 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100669 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200670 if (args->tap_flags & TAP_FLAG_GSO)
671 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100672 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
673 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
674 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100675 }
676 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
677 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100678 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
679 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200680 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200681 if ((args->tap_flags & TAP_FLAG_GSO)
682 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
683 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200684 virtio_set_packet_coalesce (vif);
685 }
Neale Ranns0cc23b72021-05-20 16:09:40 +0000686 if (vif->type == VIRTIO_IF_TYPE_TUN)
687 {
688 hw->max_supported_packet_bytes = TUN_MAX_PACKET_BYTES;
689 hw->min_packet_bytes = hw->min_supported_packet_bytes =
690 TUN_MIN_PACKET_BYTES;
691 hw->max_packet_bytes =
692 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES;
693 vnet_sw_interface_set_mtu (vnm, hw->sw_if_index, hw->max_packet_bytes);
694 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100695
Damjan Marion94100532020-11-06 23:25:57 +0100696 virtio_vring_set_rx_queues (vm, vif);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100697
Damjan Marion8389fb92017-10-13 18:29:53 +0200698 vif->per_interface_next_index = ~0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200699 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
700 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
701 VNET_HW_INTERFACE_FLAG_LINK_UP);
Matthew Smithbd50ed12020-07-24 13:38:03 -0500702 /*
703 * Host tun/tap driver link carrier state is "up" at creation. The
704 * driver never changes this unless the backend (VPP) changes it using
705 * TUNSETCARRIER ioctl(). See tap_set_carrier().
706 */
707 vif->host_carrier_up = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000708
Damjan Marion8389fb92017-10-13 18:29:53 +0200709 goto done;
710
711error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100712 if (err)
713 {
714 ASSERT (args->error == 0);
715 args->error = err;
716 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
717 }
Benoît Gannec30d87e2019-07-15 17:16:49 +0200718
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100719 tap_log_err (vif, "%U", format_clib_error, args->error);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100720 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200721done:
722 if (vhost_mem)
723 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100724 if (old_netns_fd != -1)
Nathan Skrzypczak979545e2021-09-22 17:46:02 +0200725 {
726 /* in case we errored with a switched netns */
727 clib_setns (old_netns_fd);
728 close (old_netns_fd);
729 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100730 if (nfd != -1)
731 close (nfd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200732}
733
734int
735tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
736{
737 vnet_main_t *vnm = vnet_get_main ();
738 virtio_main_t *mm = &virtio_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200739 virtio_if_t *vif;
740 vnet_hw_interface_t *hw;
741
Dave Barach3940de32019-07-23 16:28:36 -0400742 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200743 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
744 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
745
746 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
747
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500748 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200749 return VNET_API_ERROR_INVALID_INTERFACE;
750
Damjan Marion8389fb92017-10-13 18:29:53 +0200751 /* bring down the interface */
752 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
753 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
754
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500755 if (vif->type == VIRTIO_IF_TYPE_TAP)
756 ethernet_delete_interface (vnm, vif->hw_if_index);
757 else /* VIRTIO_IF_TYPE_TUN */
758 vnet_delete_hw_interface (vnm, vif->hw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200759 vif->hw_if_index = ~0;
760
Damjan Marion7c6102b2019-11-08 17:59:56 +0100761 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200762
763 return 0;
764}
765
766int
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100767tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
768 int enable_disable)
769{
770 vnet_main_t *vnm = vnet_get_main ();
771 virtio_main_t *mm = &virtio_main;
772 virtio_if_t *vif;
773 vnet_hw_interface_t *hw;
774 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100775 int i = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100776
777 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
778
779 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
780 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
781
782 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
783
784 const unsigned int csum_offload_on = TUN_F_CSUM;
785 const unsigned int csum_offload_off = 0;
786 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100787 vec_foreach_index (i, vif->tap_fds)
788 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100789 vif->gso_enabled = 0;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200790 vif->packet_coalesce = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100791 vif->csum_offload_enabled = enable_disable ? 1 : 0;
792
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100793 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100794 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100795 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100796 }
797
798 if (enable_disable)
799 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100800 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100801 }
802 else
803 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100804 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100805 }
806
807error:
808 if (err)
809 {
810 clib_warning ("Error %s checksum offload on sw_if_index %d",
811 enable_disable ? "enabling" : "disabling", sw_if_index);
812 return VNET_API_ERROR_SYSCALL_ERROR_3;
813 }
814 return 0;
815}
816
817int
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200818tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
819 int is_packet_coalesce)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200820{
821 vnet_main_t *vnm = vnet_get_main ();
822 virtio_main_t *mm = &virtio_main;
823 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400824 vnet_hw_interface_t *hw;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200825 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100826 int i = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200827
Dave Barach3940de32019-07-23 16:28:36 -0400828 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
829
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200830 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
831 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
832
833 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
834
835 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
836 const unsigned int gso_off = 0;
837 unsigned int offload = enable_disable ? gso_on : gso_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100838 vec_foreach_index (i, vif->tap_fds)
839 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200840 vif->gso_enabled = enable_disable ? 1 : 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100841 vif->csum_offload_enabled = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200842 if (enable_disable)
843 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100844 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) == 0)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200845 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100846 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
847 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200848 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200849 if (is_packet_coalesce)
850 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200851 virtio_set_packet_coalesce (vif);
852 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200853 }
854 else
855 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100856 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200857 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100858 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
859 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200860 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200861 vif->packet_coalesce = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200862 }
863
864error:
865 if (err)
866 {
867 clib_warning ("Error %s gso on sw_if_index %d",
868 enable_disable ? "enabling" : "disabling", sw_if_index);
869 return VNET_API_ERROR_SYSCALL_ERROR_3;
870 }
871 return 0;
872}
873
874int
Damjan Marion8389fb92017-10-13 18:29:53 +0200875tap_dump_ifs (tap_interface_details_t ** out_tapids)
876{
877 vnet_main_t *vnm = vnet_get_main ();
878 virtio_main_t *mm = &virtio_main;
879 virtio_if_t *vif;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000880 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200881 vnet_hw_interface_t *hi;
882 tap_interface_details_t *r_tapids = NULL;
883 tap_interface_details_t *tapid = NULL;
884
885 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100886 pool_foreach (vif, mm->interfaces) {
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200887 if ((vif->type != VIRTIO_IF_TYPE_TAP)
888 && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200889 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200890 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400891 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100892 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200893 tapid->sw_if_index = vif->sw_if_index;
894 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
895 clib_memcpy(tapid->dev_name, hi->name,
Vladimir Isaev84f3d9f2020-09-18 14:43:29 +0300896 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000897 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
898 tapid->rx_ring_sz = vring->size;
899 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
900 tapid->tx_ring_sz = vring->size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200901 tapid->tap_flags = vif->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200902 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100903 if (vif->host_if_name)
904 {
905 clib_memcpy(tapid->host_if_name, vif->host_if_name,
906 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200907 vec_len (vif->host_if_name)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100908 }
909 if (vif->net_ns)
910 {
911 clib_memcpy(tapid->host_namespace, vif->net_ns,
912 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200913 vec_len (vif->net_ns)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100914 }
915 if (vif->host_bridge)
916 {
917 clib_memcpy(tapid->host_bridge, vif->host_bridge,
918 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200919 vec_len (vif->host_bridge)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100920 }
921 if (vif->host_ip4_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200922 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
Milan Lenco73e7f422017-12-14 10:04:25 +0100923 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
924 if (vif->host_ip6_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200925 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
Milan Lenco73e7f422017-12-14 10:04:25 +0100926 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200927 tapid->host_mtu_size = vif->host_mtu_size;
Damjan Marionb2c31b62020-12-13 21:47:40 +0100928 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200929 /* *INDENT-ON* */
930
931 *out_tapids = r_tapids;
932
933 return 0;
934}
935
Matthew Smithbd50ed12020-07-24 13:38:03 -0500936/*
937 * Set host tap/tun interface carrier state so it will appear to host
938 * applications that the interface's link state changed.
939 *
940 * If the kernel we're building against does not have support for the
941 * TUNSETCARRIER ioctl command, do nothing.
942 */
943int
944tap_set_carrier (u32 hw_if_index, u32 carrier_up)
945{
946 int ret = 0;
947#ifdef TUNSETCARRIER
948 vnet_main_t *vnm = vnet_get_main ();
949 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
950 virtio_main_t *mm = &virtio_main;
951 virtio_if_t *vif;
952 int *fd;
953
954 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
955 vec_foreach (fd, vif->tap_fds)
956 {
957 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
958 if (ret < 0)
959 {
960 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
961 break;
962 }
963 }
964 if (!ret)
965 vif->host_carrier_up = (carrier_up != 0);
966#endif
967
968 return ret;
969}
970
Damjan Marion8389fb92017-10-13 18:29:53 +0200971static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200972tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
973{
974 tap_main_t *tm = &tap_main;
975
976 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
977 {
978 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
979 ;
980 else
981 return clib_error_return (0, "unknown input `%U'",
982 format_unformat_error, input);
983 }
984
985 return 0;
986}
987
Alexander Chernavina6c34a12020-09-04 09:24:20 -0400988/*
989 * Set host tap/tun interface speed in Mbps.
990 */
991int
992tap_set_speed (u32 hw_if_index, u32 speed)
993{
994 vnet_main_t *vnm = vnet_get_main ();
995 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
996 virtio_main_t *mm = &virtio_main;
997 virtio_if_t *vif;
998 int old_netns_fd = -1;
999 int nfd = -1;
1000 int ctl_fd = -1;
1001 struct ifreq ifr;
1002 struct ethtool_cmd ecmd;
1003 int ret = -1;
1004
1005 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1006
1007 if (vif->net_ns)
1008 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001009 old_netns_fd = clib_netns_open (NULL /* self */);
1010 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001011 {
1012 clib_warning ("Cannot open netns");
1013 goto done;
1014 }
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001015 if (clib_setns (nfd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001016 {
1017 clib_warning ("Cannot set ns");
1018 goto done;
1019 }
1020 }
1021
1022 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1023 {
1024 clib_warning ("Cannot open control socket");
1025 goto done;
1026 }
1027
1028 ecmd.cmd = ETHTOOL_GSET;
1029 clib_memset (&ifr, 0, sizeof (ifr));
1030 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1031 strlen ((const char *) vif->host_if_name));
1032 ifr.ifr_data = (void *) &ecmd;
1033 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1034 {
1035 clib_warning ("Cannot get device settings");
1036 goto done;
1037 }
1038
1039 if (ethtool_cmd_speed (&ecmd) != speed)
1040 {
1041 ecmd.cmd = ETHTOOL_SSET;
1042 ethtool_cmd_speed_set (&ecmd, speed);
1043 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1044 {
1045 clib_warning ("Cannot set device settings");
1046 goto done;
1047 }
1048 }
1049
1050done:
1051 if (old_netns_fd != -1)
1052 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001053 if (clib_setns (old_netns_fd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001054 {
1055 clib_warning ("Cannot set old ns");
1056 }
1057 close (old_netns_fd);
1058 }
1059 if (nfd != -1)
1060 close (nfd);
1061 if (ctl_fd != -1)
1062 close (ctl_fd);
1063
1064 return ret;
1065}
1066
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001067/* tap { host-mtu <size> } configuration. */
1068VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1069
1070static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +02001071tap_init (vlib_main_t * vm)
1072{
Damjan Marion2df39092017-12-04 20:03:37 +01001073 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001074 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -07001075
Damjan Marion07a38572018-01-21 06:44:18 -08001076 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001077 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -07001078
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001079 tm->host_mtu_size = 0;
1080
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001081 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +02001082}
1083
1084VLIB_INIT_FUNCTION (tap_init);
1085
1086/*
1087 * fd.io coding-style-patch-verification: ON
1088 *
1089 * Local Variables:
1090 * eval: (c-set-style "gnu")
1091 * End:
1092 */