blob: 4c0b4e0251ce7911d529ba10a41caeff07593b56 [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
Neale Rannscbe8d652018-04-27 04:42:47 -070080#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010081
Damjan Marion7c6102b2019-11-08 17:59:56 +010082static void
83tap_free (vlib_main_t * vm, virtio_if_t * vif)
84{
85 virtio_main_t *mm = &virtio_main;
86 tap_main_t *tm = &tap_main;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +000087 clib_error_t *err = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +010088 int i;
89
90 /* *INDENT-OFF* */
91 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
92 close (vif->vhost_fds[i]);
93 vec_foreach_index (i, vif->rxq_vrings)
94 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
95 vec_foreach_index (i, vif->txq_vrings)
96 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
97 /* *INDENT-ON* */
98
Mohsin Kazmi108b15b2020-10-28 19:35:53 +010099 if (vif->tap_fds)
100 {
101 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
102 tap_log_dbg (vif, "TUNSETPERSIST: unset");
103 }
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000104error:
Aloys Augustin2857e782020-03-16 17:29:52 +0100105 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100106
Mohsin Kazmiefd967f2021-10-01 12:34:42 +0200107 vec_free (vif->tap_fds);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100108 vec_free (vif->vhost_fds);
109 vec_free (vif->rxq_vrings);
110 vec_free (vif->txq_vrings);
111 vec_free (vif->host_if_name);
112 vec_free (vif->net_ns);
113 vec_free (vif->host_bridge);
114 clib_error_free (vif->error);
115
116 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
117 clib_memset (vif, 0, sizeof (*vif));
118 pool_put (mm->interfaces, vif);
119}
120
Damjan Marion91c6ef72017-12-01 13:34:24 +0100121void
Damjan Marion8389fb92017-10-13 18:29:53 +0200122tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
123{
Damjan Marion7c6102b2019-11-08 17:59:56 +0100124 vlib_thread_main_t *thm = vlib_get_thread_main ();
Lijian.Zhangba0da572019-08-21 17:51:16 +0800125 vlib_physmem_main_t *vpm = &vm->physmem_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200126 vnet_main_t *vnm = vnet_get_main ();
127 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100128 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200129 vnet_sw_interface_t *sw;
130 vnet_hw_interface_t *hw;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000131 int i, num_vhost_queues;
Damjan Marion2df39092017-12-04 20:03:37 +0100132 int old_netns_fd = -1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200133 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000134 struct ifreq get_ifr = {.ifr_flags = 0 };
Damjan Marion8389fb92017-10-13 18:29:53 +0200135 size_t hdrsz;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200136 vhost_memory_t *vhost_mem = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200137 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100138 clib_error_t *err = 0;
Damjan Marion39807d02019-11-08 13:52:28 +0100139 unsigned int tap_features;
Aloys Augustin2857e782020-03-16 17:29:52 +0100140 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200141 char *host_if_name = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100142 unsigned int offload = 0;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000143 int sndbuf = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100144
145 if (args->id != ~0)
146 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700147 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100148 {
149 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
150 args->error = clib_error_return (0, "interface already exists");
151 return;
152 }
153 }
154 else
155 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700156 args->id = clib_bitmap_first_clear (tm->tap_ids);
157 }
Damjan Marion2df39092017-12-04 20:03:37 +0100158
Neale Rannscbe8d652018-04-27 04:42:47 -0700159 if (args->id > TAP_MAX_INSTANCE)
160 {
161 args->rv = VNET_API_ERROR_UNSPECIFIED;
162 args->error = clib_error_return (0, "cannot find free interface id");
163 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100164 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200165
Damjan Marion7c6102b2019-11-08 17:59:56 +0100166 pool_get_zero (vim->interfaces, vif);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200167
168 if (args->tap_flags & TAP_FLAG_TUN)
169 {
170 vif->type = VIRTIO_IF_TYPE_TUN;
171 ifr.ifr_flags |= IFF_TUN;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000172
173 /*
174 * From kernel 4.20, xdp support has been added in tun_sendmsg.
175 * If sndbuf == INT_MAX, vhost batches the packet and processes
176 * them using xdp data path for tun driver. It assumes packets
177 * are ethernet frames (It needs to be fixed).
178 * To avoid xdp data path in tun driver, sndbuf value should
179 * be < INT_MAX.
180 */
181 sndbuf = INT_MAX - 1;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200182 }
183 else
184 {
185 vif->type = VIRTIO_IF_TYPE_TAP;
186 ifr.ifr_flags |= IFF_TAP;
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000187 sndbuf = INT_MAX;
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200188 }
189
Damjan Marion8389fb92017-10-13 18:29:53 +0200190 vif->dev_instance = vif - vim->interfaces;
Damjan Marion2df39092017-12-04 20:03:37 +0100191 vif->id = args->id;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100192 vif->num_txqs = thm->n_vlib_mains;
Aloys Augustin2857e782020-03-16 17:29:52 +0100193 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
Damjan Marion2df39092017-12-04 20:03:37 +0100194
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000195 if (args->tap_flags & TAP_FLAG_ATTACH)
196 {
Duncan Eastoe764a8782021-09-08 19:11:33 +0100197 if (args->host_if_name == NULL)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000198 {
199 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
200 err = clib_error_return (0, "host_if_name is not provided");
201 goto error;
202 }
Nathan Skrzypczak979545e2021-09-22 17:46:02 +0200203 }
204
205 /* if namespace is specified, all further netlink messages should be executed
206 * after we change our net namespace */
207 if (args->host_namespace)
208 {
209 old_netns_fd = clib_netns_open (NULL /* self */);
210 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000211 {
Nathan Skrzypczak979545e2021-09-22 17:46:02 +0200212 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
213 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
214 args->host_namespace);
215 goto error;
216 }
217 if (clib_setns (nfd) == -1)
218 {
219 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
220 args->error =
221 clib_error_return_unix (0, "setns '%s'", args->host_namespace);
222 goto error;
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000223 }
224 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100225
Duncan Eastoe764a8782021-09-08 19:11:33 +0100226 if (args->host_if_name != NULL)
227 {
228 host_if_name = (char *) args->host_if_name;
229 clib_memcpy (ifr.ifr_name, host_if_name,
230 clib_min (IFNAMSIZ, vec_len (host_if_name)));
231 }
232
Aloys Augustin2857e782020-03-16 17:29:52 +0100233 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200234 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100235 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
236 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
237 goto error;
238 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100239 vec_add1 (vif->tap_fds, tfd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100240 tap_log_dbg (vif, "open tap fd %d", tfd);
241
242 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
243 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
244 if ((tap_features & IFF_VNET_HDR) == 0)
245 {
246 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
247 args->error = clib_error_return (0, "vhost-net backend not available");
Damjan Marion8389fb92017-10-13 18:29:53 +0200248 goto error;
249 }
250
Damjan Marion7c6102b2019-11-08 17:59:56 +0100251 if ((tap_features & IFF_MULTI_QUEUE) == 0)
252 {
Aloys Augustin2857e782020-03-16 17:29:52 +0100253 if (vif->num_rxqs > 1)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100254 {
255 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
256 args->error = clib_error_return (0, "multiqueue not supported");
257 goto error;
258 }
Aloys Augustin2857e782020-03-16 17:29:52 +0100259 vif->num_rxqs = vif->num_txqs = 1;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100260 }
261 else
262 ifr.ifr_flags |= IFF_MULTI_QUEUE;
263
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200264 hdrsz = sizeof (virtio_net_hdr_v1_t);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100265 if (args->tap_flags & TAP_FLAG_GSO)
266 {
267 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
268 vif->gso_enabled = 1;
269 }
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100270 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
271 {
272 offload = TUN_F_CSUM;
273 vif->csum_offload_enabled = 1;
274 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100275
276 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
277 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
278 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
279
280 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
281 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
282
283 if (!args->host_if_name)
284 host_if_name = ifr.ifr_ifrn.ifrn_name;
285 else
286 host_if_name = (char *) args->host_if_name;
287
Mohsin Kazmib49bc1a2020-02-14 17:51:04 +0000288 /*
289 * unset the persistence when attaching to existing
290 * interface
291 */
292 if (args->tap_flags & TAP_FLAG_ATTACH)
293 {
294 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
295 tap_log_dbg (vif, "TUNSETPERSIST: unset");
296 }
297
298 /* set the persistence */
299 if (args->tap_flags & TAP_FLAG_PERSIST)
300 {
301 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
302 tap_log_dbg (vif, "TUNSETPERSIST: set");
303
304 /* verify persistence is set, read the flags */
305 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
306 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
307 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
308 {
309 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
310 args->error = clib_error_return (0, "persistence not supported");
311 goto error;
312 }
313 }
314
Aloys Augustin2857e782020-03-16 17:29:52 +0100315 /* create additional queues on the linux side.
316 * we create as many linux queue pairs as we have rx queues
317 */
318 for (i = 1; i < vif->num_rxqs; i++)
319 {
320 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
321 {
322 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
323 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
324 goto error;
325 }
326 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
327 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
328 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
329 vec_add1 (vif->tap_fds, qfd);
330 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100331
Aloys Augustin2857e782020-03-16 17:29:52 +0100332 for (i = 0; i < vif->num_rxqs; i++)
333 {
334 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
335 vif->tap_fds[i], hdrsz);
336 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100337
Mohsin Kazmi4834a662020-07-06 18:03:41 +0000338 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
339 sndbuf);
340 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100341
Aloys Augustin2857e782020-03-16 17:29:52 +0100342 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
343 offload);
344 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
345
346 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
347 {
348 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
349 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
350 goto error;
351 }
352 }
353
354 /* open as many vhost-net fds as required and set ownership */
355 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
356 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100357 {
358 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
359 {
360 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
361 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
362 goto error;
363 }
364 vec_add1 (vif->vhost_fds, vfd);
365 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
366 _IOCTL (vfd, VHOST_SET_OWNER, 0);
367 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
368 }
369
370 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
371 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
372 vif->remote_features);
Damjan Marion8389fb92017-10-13 18:29:53 +0200373
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200374 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200375 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100376 args->rv = VNET_API_ERROR_UNSUPPORTED;
377 args->error = clib_error_return (0, "vhost-net backend doesn't support "
378 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200379 goto error;
380 }
381
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200382 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
383 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_RING_F_INDIRECT_DESC 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_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200392 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100393 args->rv = VNET_API_ERROR_UNSUPPORTED;
394 args->error = clib_error_return (0, "vhost-net backend doesn't support "
395 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200396 goto error;
397 }
398
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200399 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
400 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
401 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
402
403 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200404
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200405 if (vif->type == VIRTIO_IF_TYPE_TAP)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200406 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200407 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
408 ethernet_mac_address_generate (args->host_mac_addr.bytes);
409 args->error = vnet_netlink_set_link_addr (vif->ifindex,
410 args->host_mac_addr.bytes);
411 if (args->error)
412 {
413 args->rv = VNET_API_ERROR_NETLINK_ERROR;
414 goto error;
415 }
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200416
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000417 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100418 {
Mohsin Kazmiadd4a412020-06-26 13:47:21 +0000419 args->error = vnet_netlink_set_link_master (vif->ifindex,
420 (char *)
421 args->host_bridge);
422 if (args->error)
423 {
424 args->rv = VNET_API_ERROR_NETLINK_ERROR;
425 goto error;
426 }
Damjan Marion91c6ef72017-12-01 13:34:24 +0100427 }
428 }
429
430 if (args->host_ip4_prefix_len)
431 {
432 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
433 &args->host_ip4_addr,
434 args->host_ip4_prefix_len);
435 if (args->error)
436 {
437 args->rv = VNET_API_ERROR_NETLINK_ERROR;
438 goto error;
439 }
440 }
441
442 if (args->host_ip6_prefix_len)
443 {
444 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
445 &args->host_ip6_addr,
446 args->host_ip6_prefix_len);
447 if (args->error)
448 {
449 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200450 goto error;
451 }
452 }
453
Damjan Marion2df39092017-12-04 20:03:37 +0100454 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
455 if (args->error)
456 {
457 args->rv = VNET_API_ERROR_NETLINK_ERROR;
458 goto error;
459 }
460
Damjan Marion7866c452018-01-18 13:35:11 +0100461 if (args->host_ip4_gw_set)
462 {
463 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
464 if (args->error)
465 {
466 args->rv = VNET_API_ERROR_NETLINK_ERROR;
467 goto error;
468 }
469 }
470
471 if (args->host_ip6_gw_set)
472 {
473 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
474 if (args->error)
475 {
476 args->rv = VNET_API_ERROR_NETLINK_ERROR;
477 goto error;
478 }
479 }
480
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200481 if (args->host_mtu_set)
482 {
483 args->error =
484 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
485 if (args->error)
486 {
487 args->rv = VNET_API_ERROR_NETLINK_ERROR;
488 goto error;
489 }
490 }
491 else if (tm->host_mtu_size != 0)
492 {
493 args->error =
494 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
495 if (args->error)
496 {
497 args->rv = VNET_API_ERROR_NETLINK_ERROR;
498 goto error;
499 }
500 args->host_mtu_set = 1;
501 args->host_mtu_size = tm->host_mtu_size;
502 }
503
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100504 /* switch back to old net namespace */
505 if (args->host_namespace)
506 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +0200507 if (clib_setns (old_netns_fd) == -1)
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100508 {
509 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
510 args->error = clib_error_return_unix (0, "setns '%s'",
511 args->host_namespace);
512 goto error;
513 }
514 }
515
Aloys Augustin2857e782020-03-16 17:29:52 +0100516 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100517 {
518 if (i < vif->num_rxqs && (args->error =
519 virtio_vring_init (vm, vif, RX_QUEUE (i),
520 args->rx_ring_sz)))
521 {
522 args->rv = VNET_API_ERROR_INIT_FAILED;
523 goto error;
524 }
525
526 if (i < vif->num_txqs && (args->error =
527 virtio_vring_init (vm, vif, TX_QUEUE (i),
528 args->tx_ring_sz)))
529 {
530 args->rv = VNET_API_ERROR_INIT_FAILED;
531 goto error;
532 }
533 }
534
535 /* setup features and memtable */
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200536 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200537 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400538 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200539 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800540 vhost_mem->regions[0].memory_size = vpm->max_size;
541 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
542 vhost_mem->regions[0].userspace_addr =
543 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200544
Damjan Marion7c6102b2019-11-08 17:59:56 +0100545 for (i = 0; i < vhost_mem->nregions; i++)
546 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
547 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
548 vhost_mem->regions[0].memory_size,
549 vhost_mem->regions[0].guest_phys_addr,
550 vhost_mem->regions[0].userspace_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200551
Damjan Marion7c6102b2019-11-08 17:59:56 +0100552
Aloys Augustin2857e782020-03-16 17:29:52 +0100553 for (i = 0; i < num_vhost_queues; i++)
Damjan Marion8389fb92017-10-13 18:29:53 +0200554 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100555 int fd = vif->vhost_fds[i];
556 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
557 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
558 fd, vif->features);
559 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
560 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200561 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100562
563 /* finish initializing queue pair */
Aloys Augustin2857e782020-03-16 17:29:52 +0100564 for (i = 0; i < num_vhost_queues * 2; i++)
Damjan Marion7c6102b2019-11-08 17:59:56 +0100565 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200566 vhost_vring_addr_t addr = { 0 };
567 vhost_vring_state_t state = { 0 };
568 vhost_vring_file_t file = { 0 };
Damjan Marion7c6102b2019-11-08 17:59:56 +0100569 virtio_vring_t *vring;
570 u16 qp = i >> 1;
571 int fd = vif->vhost_fds[qp];
572
573 if (i & 1)
574 {
575 if (qp >= vif->num_txqs)
576 continue;
577 vring = vec_elt_at_index (vif->txq_vrings, qp);
578 }
579 else
580 {
581 if (qp >= vif->num_rxqs)
582 continue;
583 vring = vec_elt_at_index (vif->rxq_vrings, qp);
584 }
585
586 addr.index = state.index = file.index = vring->queue_id & 1;
587 state.num = vring->size;
588 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
589 state.index, state.num);
590 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
591
592 addr.flags = 0;
593 addr.desc_user_addr = pointer_to_uword (vring->desc);
594 addr.avail_user_addr = pointer_to_uword (vring->avail);
595 addr.used_user_addr = pointer_to_uword (vring->used);
596
597 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
598 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
599 "used_user_addr 0x%lx", fd, addr.index,
600 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
601 addr.used_user_addr);
602 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
603
604 file.fd = vring->call_fd;
605 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
606 fd, file.index, file.fd);
607 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
608
609 file.fd = vring->kick_fd;
610 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
611 fd, file.index, file.fd);
612 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
613
Aloys Augustin2857e782020-03-16 17:29:52 +0100614 file.fd = vif->tap_fds[qp % vif->num_rxqs];
Damjan Marion7c6102b2019-11-08 17:59:56 +0100615 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
616 fd, file.index, file.fd);
617 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
618 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200619
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200620 if (vif->type == VIRTIO_IF_TYPE_TAP)
621 {
622 if (!args->mac_addr_set)
623 ethernet_mac_address_generate (args->mac_addr.bytes);
Damjan Marion8389fb92017-10-13 18:29:53 +0200624
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200625 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
Matthew Smith2b716b12021-10-22 09:53:44 -0500626 if (args->host_bridge)
627 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);
Matthew Smith2b716b12021-10-22 09:53:44 -0500630 if (args->host_namespace)
631 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200632 vif->host_mtu_size = args->host_mtu_size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200633 vif->tap_flags = args->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200634 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100635 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
636 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
637 if (args->host_ip4_prefix_len)
638 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
639 if (args->host_ip6_prefix_len)
640 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
641
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200642 if (vif->type != VIRTIO_IF_TYPE_TUN)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100643 {
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200644 args->error =
645 ethernet_register_interface (vnm, virtio_device_class.index,
646 vif->dev_instance, vif->mac_addr,
647 &vif->hw_if_index,
648 virtio_eth_flag_change);
649 if (args->error)
650 {
651 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
652 goto error;
653 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200654
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200655 }
656 else
657 {
658 vif->hw_if_index = vnet_register_interface
659 (vnm, virtio_device_class.index,
660 vif->dev_instance /* device instance */ ,
661 tun_device_hw_interface_class.index, vif->dev_instance);
662
663 }
Neale Rannscbe8d652018-04-27 04:42:47 -0700664 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200665 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
666 vif->sw_if_index = sw->sw_if_index;
667 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200668 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200669 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100670 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200671 if (args->tap_flags & TAP_FLAG_GSO)
672 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100673 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
674 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
675 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100676 }
677 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
678 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100679 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
680 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200681 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200682 if ((args->tap_flags & TAP_FLAG_GSO)
683 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
684 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200685 virtio_set_packet_coalesce (vif);
686 }
Neale Ranns0cc23b72021-05-20 16:09:40 +0000687 if (vif->type == VIRTIO_IF_TYPE_TUN)
688 {
689 hw->max_supported_packet_bytes = TUN_MAX_PACKET_BYTES;
690 hw->min_packet_bytes = hw->min_supported_packet_bytes =
691 TUN_MIN_PACKET_BYTES;
692 hw->max_packet_bytes =
693 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES;
694 vnet_sw_interface_set_mtu (vnm, hw->sw_if_index, hw->max_packet_bytes);
695 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100696
Damjan Marion94100532020-11-06 23:25:57 +0100697 virtio_vring_set_rx_queues (vm, vif);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100698
Damjan Marion8389fb92017-10-13 18:29:53 +0200699 vif->per_interface_next_index = ~0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200700 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
701 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
702 VNET_HW_INTERFACE_FLAG_LINK_UP);
Matthew Smithbd50ed12020-07-24 13:38:03 -0500703 /*
704 * Host tun/tap driver link carrier state is "up" at creation. The
705 * driver never changes this unless the backend (VPP) changes it using
706 * TUNSETCARRIER ioctl(). See tap_set_carrier().
707 */
708 vif->host_carrier_up = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000709
Damjan Marion8389fb92017-10-13 18:29:53 +0200710 goto done;
711
712error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100713 if (err)
714 {
715 ASSERT (args->error == 0);
716 args->error = err;
717 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
718 }
Benoît Gannec30d87e2019-07-15 17:16:49 +0200719
Mohsin Kazmi91592c02020-01-30 16:08:08 +0100720 tap_log_err (vif, "%U", format_clib_error, args->error);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100721 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200722done:
723 if (vhost_mem)
724 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100725 if (old_netns_fd != -1)
Nathan Skrzypczak979545e2021-09-22 17:46:02 +0200726 {
727 /* in case we errored with a switched netns */
728 clib_setns (old_netns_fd);
729 close (old_netns_fd);
730 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100731 if (nfd != -1)
732 close (nfd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200733}
734
735int
736tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
737{
738 vnet_main_t *vnm = vnet_get_main ();
739 virtio_main_t *mm = &virtio_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200740 virtio_if_t *vif;
741 vnet_hw_interface_t *hw;
742
Dave Barach3940de32019-07-23 16:28:36 -0400743 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200744 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
745 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
746
747 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
748
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500749 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200750 return VNET_API_ERROR_INVALID_INTERFACE;
751
Damjan Marion8389fb92017-10-13 18:29:53 +0200752 /* bring down the interface */
753 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
754 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
755
Matthew Smith3d18fbf2020-04-29 11:17:08 -0500756 if (vif->type == VIRTIO_IF_TYPE_TAP)
757 ethernet_delete_interface (vnm, vif->hw_if_index);
758 else /* VIRTIO_IF_TYPE_TUN */
759 vnet_delete_hw_interface (vnm, vif->hw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200760 vif->hw_if_index = ~0;
761
Damjan Marion7c6102b2019-11-08 17:59:56 +0100762 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200763
764 return 0;
765}
766
767int
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100768tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
769 int enable_disable)
770{
771 vnet_main_t *vnm = vnet_get_main ();
772 virtio_main_t *mm = &virtio_main;
773 virtio_if_t *vif;
774 vnet_hw_interface_t *hw;
775 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100776 int i = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100777
778 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
779
780 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
781 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
782
783 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
784
785 const unsigned int csum_offload_on = TUN_F_CSUM;
786 const unsigned int csum_offload_off = 0;
787 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100788 vec_foreach_index (i, vif->tap_fds)
789 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100790 vif->gso_enabled = 0;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200791 vif->packet_coalesce = 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100792 vif->csum_offload_enabled = enable_disable ? 1 : 0;
793
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100794 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100795 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100796 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100797 }
798
799 if (enable_disable)
800 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100801 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100802 }
803 else
804 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100805 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100806 }
807
808error:
809 if (err)
810 {
811 clib_warning ("Error %s checksum offload on sw_if_index %d",
812 enable_disable ? "enabling" : "disabling", sw_if_index);
813 return VNET_API_ERROR_SYSCALL_ERROR_3;
814 }
815 return 0;
816}
817
818int
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200819tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
820 int is_packet_coalesce)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200821{
822 vnet_main_t *vnm = vnet_get_main ();
823 virtio_main_t *mm = &virtio_main;
824 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400825 vnet_hw_interface_t *hw;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200826 clib_error_t *err = 0;
Aloys Augustin2857e782020-03-16 17:29:52 +0100827 int i = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200828
Dave Barach3940de32019-07-23 16:28:36 -0400829 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
830
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200831 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
832 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
833
834 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
835
836 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
837 const unsigned int gso_off = 0;
838 unsigned int offload = enable_disable ? gso_on : gso_off;
Aloys Augustin2857e782020-03-16 17:29:52 +0100839 vec_foreach_index (i, vif->tap_fds)
840 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200841 vif->gso_enabled = enable_disable ? 1 : 0;
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100842 vif->csum_offload_enabled = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200843 if (enable_disable)
844 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100845 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) == 0)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200846 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100847 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
848 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200849 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200850 if (is_packet_coalesce)
851 {
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200852 virtio_set_packet_coalesce (vif);
853 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200854 }
855 else
856 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100857 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200858 {
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100859 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
860 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200861 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200862 vif->packet_coalesce = 0;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200863 }
864
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 Kazmi09a3bc52019-04-02 11:45:08 +0000881 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
886 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100887 pool_foreach (vif, mm->interfaces) {
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200888 if ((vif->type != VIRTIO_IF_TYPE_TAP)
889 && (vif->type != VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200890 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200891 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400892 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100893 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200894 tapid->sw_if_index = vif->sw_if_index;
895 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
896 clib_memcpy(tapid->dev_name, hi->name,
Vladimir Isaev84f3d9f2020-09-18 14:43:29 +0300897 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000898 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
899 tapid->rx_ring_sz = vring->size;
900 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
901 tapid->tx_ring_sz = vring->size;
Mohsin Kazmi86f281a2020-06-30 15:28:01 +0200902 tapid->tap_flags = vif->tap_flags;
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200903 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
Milan Lenco73e7f422017-12-14 10:04:25 +0100904 if (vif->host_if_name)
905 {
906 clib_memcpy(tapid->host_if_name, vif->host_if_name,
907 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200908 vec_len (vif->host_if_name)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100909 }
910 if (vif->net_ns)
911 {
912 clib_memcpy(tapid->host_namespace, vif->net_ns,
913 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200914 vec_len (vif->net_ns)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100915 }
916 if (vif->host_bridge)
917 {
918 clib_memcpy(tapid->host_bridge, vif->host_bridge,
919 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
Mohsin Kazmi03b76a22020-10-08 17:44:36 +0200920 vec_len (vif->host_bridge)));
Milan Lenco73e7f422017-12-14 10:04:25 +0100921 }
922 if (vif->host_ip4_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200923 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
Milan Lenco73e7f422017-12-14 10:04:25 +0100924 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
925 if (vif->host_ip6_prefix_len)
Jakub Grajciar5de4fb72019-09-03 10:40:01 +0200926 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
Milan Lenco73e7f422017-12-14 10:04:25 +0100927 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200928 tapid->host_mtu_size = vif->host_mtu_size;
Damjan Marionb2c31b62020-12-13 21:47:40 +0100929 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200930 /* *INDENT-ON* */
931
932 *out_tapids = r_tapids;
933
934 return 0;
935}
936
Matthew Smithbd50ed12020-07-24 13:38:03 -0500937/*
938 * Set host tap/tun interface carrier state so it will appear to host
939 * applications that the interface's link state changed.
940 *
941 * If the kernel we're building against does not have support for the
942 * TUNSETCARRIER ioctl command, do nothing.
943 */
944int
945tap_set_carrier (u32 hw_if_index, u32 carrier_up)
946{
947 int ret = 0;
948#ifdef TUNSETCARRIER
949 vnet_main_t *vnm = vnet_get_main ();
950 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
951 virtio_main_t *mm = &virtio_main;
952 virtio_if_t *vif;
953 int *fd;
954
955 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
956 vec_foreach (fd, vif->tap_fds)
957 {
958 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
959 if (ret < 0)
960 {
961 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
962 break;
963 }
964 }
965 if (!ret)
966 vif->host_carrier_up = (carrier_up != 0);
967#endif
968
969 return ret;
970}
971
Damjan Marion8389fb92017-10-13 18:29:53 +0200972static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200973tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
974{
975 tap_main_t *tm = &tap_main;
976
977 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
978 {
979 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
980 ;
981 else
982 return clib_error_return (0, "unknown input `%U'",
983 format_unformat_error, input);
984 }
985
986 return 0;
987}
988
Alexander Chernavina6c34a12020-09-04 09:24:20 -0400989/*
990 * Set host tap/tun interface speed in Mbps.
991 */
992int
993tap_set_speed (u32 hw_if_index, u32 speed)
994{
995 vnet_main_t *vnm = vnet_get_main ();
996 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
997 virtio_main_t *mm = &virtio_main;
998 virtio_if_t *vif;
999 int old_netns_fd = -1;
1000 int nfd = -1;
1001 int ctl_fd = -1;
1002 struct ifreq ifr;
1003 struct ethtool_cmd ecmd;
1004 int ret = -1;
1005
1006 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1007
1008 if (vif->net_ns)
1009 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001010 old_netns_fd = clib_netns_open (NULL /* self */);
1011 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001012 {
1013 clib_warning ("Cannot open netns");
1014 goto done;
1015 }
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001016 if (clib_setns (nfd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001017 {
1018 clib_warning ("Cannot set ns");
1019 goto done;
1020 }
1021 }
1022
1023 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1024 {
1025 clib_warning ("Cannot open control socket");
1026 goto done;
1027 }
1028
1029 ecmd.cmd = ETHTOOL_GSET;
1030 clib_memset (&ifr, 0, sizeof (ifr));
1031 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1032 strlen ((const char *) vif->host_if_name));
1033 ifr.ifr_data = (void *) &ecmd;
1034 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1035 {
1036 clib_warning ("Cannot get device settings");
1037 goto done;
1038 }
1039
1040 if (ethtool_cmd_speed (&ecmd) != speed)
1041 {
1042 ecmd.cmd = ETHTOOL_SSET;
1043 ethtool_cmd_speed_set (&ecmd, speed);
1044 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1045 {
1046 clib_warning ("Cannot set device settings");
1047 goto done;
1048 }
1049 }
1050
1051done:
1052 if (old_netns_fd != -1)
1053 {
Nathan Skrzypczak4cef6de2021-07-19 18:21:43 +02001054 if (clib_setns (old_netns_fd) == -1)
Alexander Chernavina6c34a12020-09-04 09:24:20 -04001055 {
1056 clib_warning ("Cannot set old ns");
1057 }
1058 close (old_netns_fd);
1059 }
1060 if (nfd != -1)
1061 close (nfd);
1062 if (ctl_fd != -1)
1063 close (ctl_fd);
1064
1065 return ret;
1066}
1067
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001068/* tap { host-mtu <size> } configuration. */
1069VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1070
1071static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +02001072tap_init (vlib_main_t * vm)
1073{
Damjan Marion2df39092017-12-04 20:03:37 +01001074 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001075 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -07001076
Damjan Marion07a38572018-01-21 06:44:18 -08001077 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001078 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -07001079
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +02001080 tm->host_mtu_size = 0;
1081
Mohsin Kazmia23b6152018-05-17 17:21:39 +02001082 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +02001083}
1084
1085VLIB_INIT_FUNCTION (tap_init);
1086
1087/*
1088 * fd.io coding-style-patch-verification: ON
1089 *
1090 * Local Variables:
1091 * eval: (c-set-style "gnu")
1092 * End:
1093 */