blob: 1e2ee87041de6b148b19e3a96595de4a7f63d245 [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 Kazmi0d05c0d2021-11-09 17:44:10 +000061VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) = {
Mohsin Kazmi206acf82020-04-06 14:19:54 +020062 .name = "tun-device",
63 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +000064 .tx_hash_fn_type = VNET_HASH_FN_TYPE_IP,
Mohsin Kazmi206acf82020-04-06 14:19:54 +020065};
Mohsin Kazmi206acf82020-04-06 14:19:54 +020066
Neale Ranns0cc23b72021-05-20 16:09:40 +000067#define TUN_MAX_PACKET_BYTES 65355
68#define TUN_MIN_PACKET_BYTES 64
69#define TUN_DEFAULT_PACKET_BYTES 1500
70
Damjan Marion8389fb92017-10-13 18:29:53 +020071static u32
72virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
73 u32 flags)
74{
75 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010076 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020077 return 0;
78}
79
Artem Glazychev66593a62022-02-18 12:23:20 +070080static clib_error_t *
81virtio_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
82 u32 frame_size)
83{
84 /* nothing for now */
85 return 0;
86}
87
Neale Rannscbe8d652018-04-27 04:42:47 -070088#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010089
Damjan Marion7c6102b2019-11-08 17:59:56 +010090static void
91tap_free (vlib_main_t * vm, virtio_if_t * vif)
92{
93 virtio_main_t *mm = &virtio_main;
94 tap_main_t *tm = &tap_main;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +000095 clib_error_t *err = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +010096 int i;
97
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +000098 virtio_pre_input_node_disable (vm, vif);
99
Damjan Marion7c6102b2019-11-08 17:59:56 +0100100 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
101 close (vif->vhost_fds[i]);
102 vec_foreach_index (i, vif->rxq_vrings)
103 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
104 vec_foreach_index (i, vif->txq_vrings)
105 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
Damjan Marion7c6102b2019-11-08 17:59:56 +0100106
Mohsin Kazmi108b15b2020-10-28 19:35:53 +0100107 if (vif->tap_fds)
108 {
109 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
110 tap_log_dbg (vif, "TUNSETPERSIST: unset");
111 }
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000112error:
Aloys Augustin2857e782020-03-16 17:29:52 +0100113 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100114
Mohsin Kazmiefd967f2021-10-01 12:34:42 +0200115 vec_free (vif->tap_fds);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100116 vec_free (vif->vhost_fds);
117 vec_free (vif->rxq_vrings);
118 vec_free (vif->txq_vrings);
119 vec_free (vif->host_if_name);
120 vec_free (vif->net_ns);
121 vec_free (vif->host_bridge);
122 clib_error_free (vif->error);
123
124 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
125 clib_memset (vif, 0, sizeof (*vif));
126 pool_put (mm->interfaces, vif);
127}
128
Damjan Marion91c6ef72017-12-01 13:34:24 +0100129void
Damjan Marion8389fb92017-10-13 18:29:53 +0200130tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
131{
Damjan Marion7c6102b2019-11-08 17:59:56 +0100132 vlib_thread_main_t *thm = vlib_get_thread_main ();
Lijian.Zhangba0da572019-08-21 17:51:16 +0800133 vlib_physmem_main_t *vpm = &vm->physmem_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200134 vnet_main_t *vnm = vnet_get_main ();
135 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100136 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200137 vnet_sw_interface_t *sw;
138 vnet_hw_interface_t *hw;
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000139 vnet_hw_if_caps_change_t cc;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000140 int i, num_vhost_queues;
Damjan Marion2df39092017-12-04 20:03:37 +0100141 int old_netns_fd = -1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200142 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000143 struct ifreq get_ifr = {.ifr_flags = 0 };
Damjan Marion8389fb92017-10-13 18:29:53 +0200144 size_t hdrsz;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200145 vhost_memory_t *vhost_mem = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200146 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100147 clib_error_t *err = 0;
Damjan Marion39807d02019-11-08 13:52:28 +0100148 unsigned int tap_features;
Aloys Augustin2857e782020-03-16 17:29:52 +0100149 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200150 char *host_if_name = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100151 unsigned int offload = 0;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000152 int sndbuf = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100153
154 if (args->id != ~0)
155 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700156 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100157 {
158 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
159 args->error = clib_error_return (0, "interface already exists");
160 return;
161 }
162 }
163 else
164 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700165 args->id = clib_bitmap_first_clear (tm->tap_ids);
166 }
Damjan Marion2df39092017-12-04 20:03:37 +0100167
Neale Rannscbe8d652018-04-27 04:42:47 -0700168 if (args->id > TAP_MAX_INSTANCE)
169 {
170 args->rv = VNET_API_ERROR_UNSPECIFIED;
171 args->error = clib_error_return (0, "cannot find free interface id");
172 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100173 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200174
Damjan Marion7c6102b2019-11-08 17:59:56 +0100175 pool_get_zero (vim->interfaces, vif);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200176
177 if (args->tap_flags & TAP_FLAG_TUN)
178 {
179 vif->type = VIRTIO_IF_TYPE_TUN;
180 ifr.ifr_flags |= IFF_TUN;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000181
182 /*
183 * From kernel 4.20, xdp support has been added in tun_sendmsg.
184 * If sndbuf == INT_MAX, vhost batches the packet and processes
185 * them using xdp data path for tun driver. It assumes packets
186 * are ethernet frames (It needs to be fixed).
187 * To avoid xdp data path in tun driver, sndbuf value should
188 * be < INT_MAX.
189 */
190 sndbuf = INT_MAX - 1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200191 }
192 else
193 {
194 vif->type = VIRTIO_IF_TYPE_TAP;
195 ifr.ifr_flags |= IFF_TAP;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000196 sndbuf = INT_MAX;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200197 }
198
Damjan Marion8389fb92017-10-13 18:29:53 +0200199 vif->dev_instance = vif - vim->interfaces;
Damjan Marion2df39092017-12-04 20:03:37 +0100200 vif->id = args->id;
Nathan Skrzypczak40edaf62021-12-15 18:45:59 +0100201 vif->num_txqs = clib_max (args->num_tx_queues, thm->n_vlib_mains);
Aloys Augustin2857e782020-03-16 17:29:52 +0100202 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
Damjan Marion2df39092017-12-04 20:03:37 +0100203
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000204 if (args->tap_flags & TAP_FLAG_ATTACH)
205 {
Duncan Eastoe764a8782021-09-08 19:11:33 +0100206 if (args->host_if_name == NULL)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000207 {
208 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
209 err = clib_error_return (0, "host_if_name is not provided");
210 goto error;
211 }
Nathan Skrzypczak979545e2021-09-22 17:46:02 +0200212 }
213
214 /* if namespace is specified, all further netlink messages should be executed
215 * after we change our net namespace */
216 if (args->host_namespace)
217 {
218 old_netns_fd = clib_netns_open (NULL /* self */);
219 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000220 {
Nathan Skrzypczak979545e2021-09-22 17:46:02 +0200221 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
222 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
223 args->host_namespace);
224 goto error;
225 }
226 if (clib_setns (nfd) == -1)
227 {
228 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
229 args->error =
230 clib_error_return_unix (0, "setns '%s'", args->host_namespace);
231 goto error;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000232 }
233 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100234
Duncan Eastoe764a8782021-09-08 19:11:33 +0100235 if (args->host_if_name != NULL)
236 {
237 host_if_name = (char *) args->host_if_name;
238 clib_memcpy (ifr.ifr_name, host_if_name,
239 clib_min (IFNAMSIZ, vec_len (host_if_name)));
240 }
241
Aloys Augustin2857e782020-03-16 17:29:52 +0100242 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200243 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100244 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
245 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
246 goto error;
247 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100248 vec_add1 (vif->tap_fds, tfd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100249 tap_log_dbg (vif, "open tap fd %d", tfd);
250
251 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
252 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
253 if ((tap_features & IFF_VNET_HDR) == 0)
254 {
255 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
256 args->error = clib_error_return (0, "vhost-net backend not available");
Damjan Marion8389fb92017-10-13 18:29:53 +0200257 goto error;
258 }
259
Damjan Marion7c6102b2019-11-08 17:59:56 +0100260 if ((tap_features & IFF_MULTI_QUEUE) == 0)
261 {
Aloys Augustin2857e782020-03-16 17:29:52 +0100262 if (vif->num_rxqs > 1)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100263 {
264 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
265 args->error = clib_error_return (0, "multiqueue not supported");
266 goto error;
267 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100268 vif->num_rxqs = vif->num_txqs = 1;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100269 }
270 else
271 ifr.ifr_flags |= IFF_MULTI_QUEUE;
272
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000273 hdrsz = sizeof (vnet_virtio_net_hdr_v1_t);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100274 if (args->tap_flags & TAP_FLAG_GSO)
275 {
276 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
277 vif->gso_enabled = 1;
278 }
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100279 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
280 {
281 offload = TUN_F_CSUM;
282 vif->csum_offload_enabled = 1;
283 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100284
285 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
286 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
287 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
288
289 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
290 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
291
292 if (!args->host_if_name)
293 host_if_name = ifr.ifr_ifrn.ifrn_name;
294 else
295 host_if_name = (char *) args->host_if_name;
296
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000297 /*
298 * unset the persistence when attaching to existing
299 * interface
300 */
301 if (args->tap_flags & TAP_FLAG_ATTACH)
302 {
303 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
304 tap_log_dbg (vif, "TUNSETPERSIST: unset");
305 }
306
307 /* set the persistence */
308 if (args->tap_flags & TAP_FLAG_PERSIST)
309 {
310 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
311 tap_log_dbg (vif, "TUNSETPERSIST: set");
312
313 /* verify persistence is set, read the flags */
314 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
315 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
316 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
317 {
318 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
319 args->error = clib_error_return (0, "persistence not supported");
320 goto error;
321 }
322 }
323
Aloys Augustin2857e782020-03-16 17:29:52 +0100324 /* create additional queues on the linux side.
325 * we create as many linux queue pairs as we have rx queues
326 */
327 for (i = 1; i < vif->num_rxqs; i++)
328 {
329 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
330 {
331 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
332 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
333 goto error;
334 }
Mohsin Kazmi0c740a62022-04-05 11:12:33 +0000335 vec_add1 (vif->tap_fds, qfd);
Aloys Augustin2857e782020-03-16 17:29:52 +0100336 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
337 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
338 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
Aloys Augustin2857e782020-03-16 17:29:52 +0100339 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100340
Aloys Augustin2857e782020-03-16 17:29:52 +0100341 for (i = 0; i < vif->num_rxqs; i++)
342 {
343 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
344 vif->tap_fds[i], hdrsz);
345 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100346
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000347 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
348 sndbuf);
349 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100350
Aloys Augustin2857e782020-03-16 17:29:52 +0100351 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
352 offload);
353 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
354
355 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
356 {
357 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
358 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
359 goto error;
360 }
361 }
362
363 /* open as many vhost-net fds as required and set ownership */
364 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
365 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100366 {
367 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
368 {
369 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
370 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
371 goto error;
372 }
373 vec_add1 (vif->vhost_fds, vfd);
374 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
375 _IOCTL (vfd, VHOST_SET_OWNER, 0);
376 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
377 }
378
379 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
380 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
381 vif->remote_features);
Damjan Marion8389fb92017-10-13 18:29:53 +0200382
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200383 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200384 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100385 args->rv = VNET_API_ERROR_UNSUPPORTED;
386 args->error = clib_error_return (0, "vhost-net backend doesn't support "
387 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200388 goto error;
389 }
390
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200391 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
392 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_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200397 goto error;
398 }
399
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200400 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200401 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100402 args->rv = VNET_API_ERROR_UNSUPPORTED;
403 args->error = clib_error_return (0, "vhost-net backend doesn't support "
404 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200405 goto error;
406 }
407
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200408 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
409 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
410 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
411
412 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200413
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200414 if (vif->type == VIRTIO_IF_TYPE_TAP)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200415 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200416 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
417 ethernet_mac_address_generate (args->host_mac_addr.bytes);
418 args->error = vnet_netlink_set_link_addr (vif->ifindex,
419 args->host_mac_addr.bytes);
420 if (args->error)
421 {
422 args->rv = VNET_API_ERROR_NETLINK_ERROR;
423 goto error;
424 }
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200425
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000426 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100427 {
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000428 args->error = vnet_netlink_set_link_master (vif->ifindex,
429 (char *)
430 args->host_bridge);
431 if (args->error)
432 {
433 args->rv = VNET_API_ERROR_NETLINK_ERROR;
434 goto error;
435 }
Damjan Marion91c6ef72017-12-01 13:34:24 +0100436 }
437 }
438
439 if (args->host_ip4_prefix_len)
440 {
441 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
442 &args->host_ip4_addr,
443 args->host_ip4_prefix_len);
444 if (args->error)
445 {
446 args->rv = VNET_API_ERROR_NETLINK_ERROR;
447 goto error;
448 }
449 }
450
451 if (args->host_ip6_prefix_len)
452 {
453 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
454 &args->host_ip6_addr,
455 args->host_ip6_prefix_len);
456 if (args->error)
457 {
458 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200459 goto error;
460 }
461 }
462
Damjan Marion2df39092017-12-04 20:03:37 +0100463 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
464 if (args->error)
465 {
466 args->rv = VNET_API_ERROR_NETLINK_ERROR;
467 goto error;
468 }
469
Damjan Marion7866c452018-01-18 13:35:11 +0100470 if (args->host_ip4_gw_set)
471 {
472 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
473 if (args->error)
474 {
475 args->rv = VNET_API_ERROR_NETLINK_ERROR;
476 goto error;
477 }
478 }
479
480 if (args->host_ip6_gw_set)
481 {
482 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
483 if (args->error)
484 {
485 args->rv = VNET_API_ERROR_NETLINK_ERROR;
486 goto error;
487 }
488 }
489
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200490 if (args->host_mtu_set)
491 {
492 args->error =
493 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
494 if (args->error)
495 {
496 args->rv = VNET_API_ERROR_NETLINK_ERROR;
497 goto error;
498 }
499 }
500 else if (tm->host_mtu_size != 0)
501 {
502 args->error =
503 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
504 if (args->error)
505 {
506 args->rv = VNET_API_ERROR_NETLINK_ERROR;
507 goto error;
508 }
509 args->host_mtu_set = 1;
510 args->host_mtu_size = tm->host_mtu_size;
511 }
512
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100513 /* switch back to old net namespace */
514 if (args->host_namespace)
515 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200516 if (clib_setns (old_netns_fd) == -1)
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100517 {
518 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
519 args->error = clib_error_return_unix (0, "setns '%s'",
520 args->host_namespace);
521 goto error;
522 }
523 }
524
Aloys Augustin2857e782020-03-16 17:29:52 +0100525 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100526 {
527 if (i < vif->num_rxqs && (args->error =
528 virtio_vring_init (vm, vif, RX_QUEUE (i),
529 args->rx_ring_sz)))
530 {
531 args->rv = VNET_API_ERROR_INIT_FAILED;
532 goto error;
533 }
534
535 if (i < vif->num_txqs && (args->error =
536 virtio_vring_init (vm, vif, TX_QUEUE (i),
537 args->tx_ring_sz)))
538 {
539 args->rv = VNET_API_ERROR_INIT_FAILED;
540 goto error;
541 }
542 }
543
544 /* setup features and memtable */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200545 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200546 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400547 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200548 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800549 vhost_mem->regions[0].memory_size = vpm->max_size;
550 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
551 vhost_mem->regions[0].userspace_addr =
552 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200553
Damjan Marion7c6102b2019-11-08 17:59:56 +0100554 for (i = 0; i < vhost_mem->nregions; i++)
555 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
556 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
557 vhost_mem->regions[0].memory_size,
558 vhost_mem->regions[0].guest_phys_addr,
559 vhost_mem->regions[0].userspace_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200560
Damjan Marion7c6102b2019-11-08 17:59:56 +0100561
Aloys Augustin2857e782020-03-16 17:29:52 +0100562 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion8389fb92017-10-13 18:29:53 +0200563 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100564 int fd = vif->vhost_fds[i];
565 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
566 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
567 fd, vif->features);
568 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
569 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200570 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100571
572 /* finish initializing queue pair */
Aloys Augustin2857e782020-03-16 17:29:52 +0100573 for (i = 0; i < num_vhost_queues * 2; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100574 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200575 vhost_vring_addr_t addr = { 0 };
576 vhost_vring_state_t state = { 0 };
577 vhost_vring_file_t file = { 0 };
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000578 vnet_virtio_vring_t *vring;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100579 u16 qp = i >> 1;
580 int fd = vif->vhost_fds[qp];
581
582 if (i & 1)
583 {
584 if (qp >= vif->num_txqs)
585 continue;
586 vring = vec_elt_at_index (vif->txq_vrings, qp);
587 }
588 else
589 {
590 if (qp >= vif->num_rxqs)
591 continue;
592 vring = vec_elt_at_index (vif->rxq_vrings, qp);
593 }
594
595 addr.index = state.index = file.index = vring->queue_id & 1;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000596 state.num = vring->queue_size;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100597 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
598 state.index, state.num);
599 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
600
601 addr.flags = 0;
602 addr.desc_user_addr = pointer_to_uword (vring->desc);
603 addr.avail_user_addr = pointer_to_uword (vring->avail);
604 addr.used_user_addr = pointer_to_uword (vring->used);
605
606 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
607 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
608 "used_user_addr 0x%lx", fd, addr.index,
609 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
610 addr.used_user_addr);
611 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
612
613 file.fd = vring->call_fd;
614 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
615 fd, file.index, file.fd);
616 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
617
618 file.fd = vring->kick_fd;
619 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
620 fd, file.index, file.fd);
621 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
622
Aloys Augustin2857e782020-03-16 17:29:52 +0100623 file.fd = vif->tap_fds[qp % vif->num_rxqs];
Damjan Marion7c6102b2019-11-08 17:59:56 +0100624 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
625 fd, file.index, file.fd);
626 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
627 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200628
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200629 if (vif->type == VIRTIO_IF_TYPE_TAP)
630 {
631 if (!args->mac_addr_set)
632 ethernet_mac_address_generate (args->mac_addr.bytes);
Damjan Marion8389fb92017-10-13 18:29:53 +0200633
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200634 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
Matthew Smith2b716b12021-10-22 09:53:44 -0500635 if (args->host_bridge)
636 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200637 }
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200638 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
Matthew Smith2b716b12021-10-22 09:53:44 -0500639 if (args->host_namespace)
640 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200641 vif->host_mtu_size = args->host_mtu_size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200642 vif->tap_flags = args->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200643 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100644 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
645 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
646 if (args->host_ip4_prefix_len)
647 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
648 if (args->host_ip6_prefix_len)
649 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
650
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200651 if (vif->type != VIRTIO_IF_TYPE_TUN)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100652 {
Damjan Marion5c954c42022-01-06 20:36:14 +0100653 vnet_eth_interface_registration_t eir = {};
Damjan Marion8389fb92017-10-13 18:29:53 +0200654
Damjan Marion5c954c42022-01-06 20:36:14 +0100655 eir.dev_class_index = virtio_device_class.index;
656 eir.dev_instance = vif->dev_instance;
657 eir.address = vif->mac_addr;
658 eir.cb.flag_change = virtio_eth_flag_change;
Artem Glazychev66593a62022-02-18 12:23:20 +0700659 eir.cb.set_max_frame_size = virtio_eth_set_max_frame_size;
Damjan Marion5c954c42022-01-06 20:36:14 +0100660 vif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200661 }
662 else
663 {
664 vif->hw_if_index = vnet_register_interface
665 (vnm, virtio_device_class.index,
666 vif->dev_instance /* device instance */ ,
667 tun_device_hw_interface_class.index, vif->dev_instance);
668
669 }
Neale Rannscbe8d652018-04-27 04:42:47 -0700670 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200671 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
672 vif->sw_if_index = sw->sw_if_index;
673 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200674 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200675 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000676 cc.mask = VNET_HW_IF_CAP_INT_MODE | VNET_HW_IF_CAP_TCP_GSO |
677 VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
678 cc.val = VNET_HW_IF_CAP_INT_MODE;
679
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200680 if (args->tap_flags & TAP_FLAG_GSO)
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000681 cc.val |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM |
682 VNET_HW_IF_CAP_TX_UDP_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100683 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000684 cc.val |= VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
685
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200686 if ((args->tap_flags & TAP_FLAG_GSO)
687 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
688 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200689 virtio_set_packet_coalesce (vif);
690 }
Neale Ranns0cc23b72021-05-20 16:09:40 +0000691 if (vif->type == VIRTIO_IF_TYPE_TUN)
692 {
Damjan Marion1cd0e5d2022-01-17 14:49:17 +0100693 hw->min_frame_size = TUN_MIN_PACKET_BYTES;
694 vnet_hw_interface_set_mtu (
Benoît Ganne30a6f822022-03-01 16:48:40 +0100695 vnm, hw->hw_if_index,
Damjan Marion1cd0e5d2022-01-17 14:49:17 +0100696 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES);
Neale Ranns0cc23b72021-05-20 16:09:40 +0000697 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100698
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000699 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000700 virtio_pre_input_node_enable (vm, vif);
Damjan Marion94100532020-11-06 23:25:57 +0100701 virtio_vring_set_rx_queues (vm, vif);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000702 virtio_vring_set_tx_queues (vm, vif);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100703
Damjan Marion8389fb92017-10-13 18:29:53 +0200704 vif->per_interface_next_index = ~0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200705 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
706 VNET_HW_INTERFACE_FLAG_LINK_UP);
Matthew Smithbd50ed12020-07-24 13:38:03 -0500707 /*
708 * Host tun/tap driver link carrier state is "up" at creation. The
709 * driver never changes this unless the backend (VPP) changes it using
710 * TUNSETCARRIER ioctl(). See tap_set_carrier().
711 */
712 vif->host_carrier_up = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000713
Damjan Marion8389fb92017-10-13 18:29:53 +0200714 goto done;
715
716error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100717 if (err)
718 {
719 ASSERT (args->error == 0);
720 args->error = err;
721 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
722 }
Benoît Gannec30d87e2019-07-15 17:16:49 +0200723
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100724 tap_log_err (vif, "%U", format_clib_error, args->error);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100725 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200726done:
727 if (vhost_mem)
728 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100729 if (old_netns_fd != -1)
Nathan Skrzypczak979545e2021-09-22 17:46:02 +0200730 {
731 /* in case we errored with a switched netns */
732 clib_setns (old_netns_fd);
733 close (old_netns_fd);
734 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100735 if (nfd != -1)
736 close (nfd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200737}
738
739int
740tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
741{
742 vnet_main_t *vnm = vnet_get_main ();
743 virtio_main_t *mm = &virtio_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200744 virtio_if_t *vif;
745 vnet_hw_interface_t *hw;
746
Dave Barach3940de32019-07-23 16:28:36 -0400747 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200748 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
749 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
750
751 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
752
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500753 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200754 return VNET_API_ERROR_INVALID_INTERFACE;
755
Damjan Marion8389fb92017-10-13 18:29:53 +0200756 /* bring down the interface */
757 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
758 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
759
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500760 if (vif->type == VIRTIO_IF_TYPE_TAP)
761 ethernet_delete_interface (vnm, vif->hw_if_index);
762 else /* VIRTIO_IF_TYPE_TUN */
763 vnet_delete_hw_interface (vnm, vif->hw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200764 vif->hw_if_index = ~0;
765
Damjan Marion7c6102b2019-11-08 17:59:56 +0100766 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200767
768 return 0;
769}
770
771int
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100772tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
773 int enable_disable)
774{
775 vnet_main_t *vnm = vnet_get_main ();
776 virtio_main_t *mm = &virtio_main;
777 virtio_if_t *vif;
778 vnet_hw_interface_t *hw;
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000779 vnet_hw_if_caps_change_t cc;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100780 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100781 int i = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100782
783 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
784
785 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
786 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
787
788 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
789
790 const unsigned int csum_offload_on = TUN_F_CSUM;
791 const unsigned int csum_offload_off = 0;
792 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100793 vec_foreach_index (i, vif->tap_fds)
794 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100795 vif->gso_enabled = 0;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200796 vif->packet_coalesce = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100797
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000798 cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100799 if (enable_disable)
800 {
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000801 cc.val = VNET_HW_IF_CAP_L4_TX_CKSUM;
802 vif->csum_offload_enabled = 1;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100803 }
804 else
805 {
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000806 cc.val = 0;
807 vif->csum_offload_enabled = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100808 }
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000809 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100810
811error:
812 if (err)
813 {
814 clib_warning ("Error %s checksum offload on sw_if_index %d",
815 enable_disable ? "enabling" : "disabling", sw_if_index);
816 return VNET_API_ERROR_SYSCALL_ERROR_3;
817 }
818 return 0;
819}
820
821int
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200822tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
823 int is_packet_coalesce)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200824{
825 vnet_main_t *vnm = vnet_get_main ();
826 virtio_main_t *mm = &virtio_main;
827 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400828 vnet_hw_interface_t *hw;
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000829 vnet_hw_if_caps_change_t cc;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200830 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100831 int i = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200832
Dave Barach3940de32019-07-23 16:28:36 -0400833 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
834
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200835 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
836 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
837
838 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
839
840 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
841 const unsigned int gso_off = 0;
842 unsigned int offload = enable_disable ? gso_on : gso_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100843 vec_foreach_index (i, vif->tap_fds)
844 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000845
846 cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM;
847
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200848 if (enable_disable)
849 {
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000850 cc.val = cc.mask;
851 vif->gso_enabled = 1;
852 vif->csum_offload_enabled = 1;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200853 if (is_packet_coalesce)
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000854 virtio_set_packet_coalesce (vif);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200855 }
856 else
857 {
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000858 cc.val = 0;
859 vif->gso_enabled = 0;
860 vif->csum_offload_enabled = 0;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200861 vif->packet_coalesce = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200862 }
Damjan Marionc48ec5d2022-01-10 19:38:57 +0000863 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200864
865error:
866 if (err)
867 {
868 clib_warning ("Error %s gso on sw_if_index %d",
869 enable_disable ? "enabling" : "disabling", sw_if_index);
870 return VNET_API_ERROR_SYSCALL_ERROR_3;
871 }
872 return 0;
873}
874
875int
Damjan Marion8389fb92017-10-13 18:29:53 +0200876tap_dump_ifs (tap_interface_details_t ** out_tapids)
877{
878 vnet_main_t *vnm = vnet_get_main ();
879 virtio_main_t *mm = &virtio_main;
880 virtio_if_t *vif;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000881 vnet_virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200882 vnet_hw_interface_t *hi;
883 tap_interface_details_t *r_tapids = NULL;
884 tap_interface_details_t *tapid = NULL;
885
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));
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000898 tapid->rx_ring_sz = vring->queue_size;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000899 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000900 tapid->tx_ring_sz = vring->queue_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
930 *out_tapids = r_tapids;
931
932 return 0;
933}
934
Matthew Smithbd50ed12020-07-24 13:38:03 -0500935/*
936 * Set host tap/tun interface carrier state so it will appear to host
937 * applications that the interface's link state changed.
938 *
939 * If the kernel we're building against does not have support for the
940 * TUNSETCARRIER ioctl command, do nothing.
941 */
942int
943tap_set_carrier (u32 hw_if_index, u32 carrier_up)
944{
945 int ret = 0;
946#ifdef TUNSETCARRIER
947 vnet_main_t *vnm = vnet_get_main ();
948 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
949 virtio_main_t *mm = &virtio_main;
950 virtio_if_t *vif;
951 int *fd;
952
953 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
954 vec_foreach (fd, vif->tap_fds)
955 {
956 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
957 if (ret < 0)
958 {
959 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
960 break;
961 }
962 }
963 if (!ret)
964 vif->host_carrier_up = (carrier_up != 0);
965#endif
966
967 return ret;
968}
969
Damjan Marion8389fb92017-10-13 18:29:53 +0200970static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200971tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
972{
973 tap_main_t *tm = &tap_main;
974
975 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
976 {
977 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
978 ;
979 else
980 return clib_error_return (0, "unknown input `%U'",
981 format_unformat_error, input);
982 }
983
984 return 0;
985}
986
Alexander Chernavina6c34a12020-09-04 09:24:20 -0400987/*
988 * Set host tap/tun interface speed in Mbps.
989 */
990int
991tap_set_speed (u32 hw_if_index, u32 speed)
992{
993 vnet_main_t *vnm = vnet_get_main ();
994 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
995 virtio_main_t *mm = &virtio_main;
996 virtio_if_t *vif;
997 int old_netns_fd = -1;
998 int nfd = -1;
999 int ctl_fd = -1;
1000 struct ifreq ifr;
1001 struct ethtool_cmd ecmd;
1002 int ret = -1;
1003
1004 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1005
1006 if (vif->net_ns)
1007 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001008 old_netns_fd = clib_netns_open (NULL /* self */);
1009 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001010 {
1011 clib_warning ("Cannot open netns");
1012 goto done;
1013 }
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001014 if (clib_setns (nfd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001015 {
1016 clib_warning ("Cannot set ns");
1017 goto done;
1018 }
1019 }
1020
1021 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1022 {
1023 clib_warning ("Cannot open control socket");
1024 goto done;
1025 }
1026
1027 ecmd.cmd = ETHTOOL_GSET;
1028 clib_memset (&ifr, 0, sizeof (ifr));
1029 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1030 strlen ((const char *) vif->host_if_name));
1031 ifr.ifr_data = (void *) &ecmd;
1032 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1033 {
1034 clib_warning ("Cannot get device settings");
1035 goto done;
1036 }
1037
1038 if (ethtool_cmd_speed (&ecmd) != speed)
1039 {
1040 ecmd.cmd = ETHTOOL_SSET;
1041 ethtool_cmd_speed_set (&ecmd, speed);
1042 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1043 {
1044 clib_warning ("Cannot set device settings");
1045 goto done;
1046 }
1047 }
1048
1049done:
1050 if (old_netns_fd != -1)
1051 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001052 if (clib_setns (old_netns_fd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001053 {
1054 clib_warning ("Cannot set old ns");
1055 }
1056 close (old_netns_fd);
1057 }
1058 if (nfd != -1)
1059 close (nfd);
1060 if (ctl_fd != -1)
1061 close (ctl_fd);
1062
1063 return ret;
1064}
1065
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001066/* tap { host-mtu <size> } configuration. */
1067VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1068
1069static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +02001070tap_init (vlib_main_t * vm)
1071{
Damjan Marion2df39092017-12-04 20:03:37 +01001072 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001073 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -07001074
Damjan Marion07a38572018-01-21 06:44:18 -08001075 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001076 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -07001077
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001078 tm->host_mtu_size = 0;
1079
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001080 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +02001081}
1082
1083VLIB_INIT_FUNCTION (tap_init);
1084
1085/*
1086 * fd.io coding-style-patch-verification: ON
1087 *
1088 * Local Variables:
1089 * eval: (c-set-style "gnu")
1090 * End:
1091 */