blob: 38f0605277d5cd1c4c5ba88e2df2236160c0dedd [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>
21#include <fcntl.h>
22#include <net/if.h>
23#include <linux/if_tun.h>
24#include <sys/ioctl.h>
25#include <linux/virtio_net.h>
26#include <linux/vhost.h>
27#include <sys/eventfd.h>
Damjan Marion7c6102b2019-11-08 17:59:56 +010028#include <net/if_arp.h>
Damjan Marion2df39092017-12-04 20:03:37 +010029#include <sched.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>
38#include <vnet/ethernet/ethernet.h>
Damjan Marion91c6ef72017-12-01 13:34:24 +010039#include <vnet/ip/ip4_packet.h>
40#include <vnet/ip/ip6_packet.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010041#include <vnet/devices/netlink.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020042#include <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010043#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020044
Damjan Marion2df39092017-12-04 20:03:37 +010045tap_main_t tap_main;
46
Damjan Marion7c6102b2019-11-08 17:59:56 +010047#define tap_log_err(dev, f, ...) \
48 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
49#define tap_log_dbg(dev, f, ...) \
50 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
51
Damjan Marion8389fb92017-10-13 18:29:53 +020052#define _IOCTL(fd,a,...) \
53 if (ioctl (fd, a, __VA_ARGS__) < 0) \
54 { \
55 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
Damjan Marion7c6102b2019-11-08 17:59:56 +010056 tap_log_err (vif, "%U", format_clib_error, err); \
Damjan Marion8389fb92017-10-13 18:29:53 +020057 goto error; \
58 }
59
60static u32
61virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
62 u32 flags)
63{
64 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010065 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020066 return 0;
67}
68
Damjan Marion2df39092017-12-04 20:03:37 +010069static int
70open_netns_fd (char *netns)
71{
72 u8 *s = 0;
73 int fd;
74
75 if (strncmp (netns, "pid:", 4) == 0)
76 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
77 else if (netns[0] == '/')
78 s = format (0, "%s%c", netns, 0);
79 else
80 s = format (0, "/var/run/netns/%s%c", netns, 0);
81
82 fd = open ((char *) s, O_RDONLY);
83 vec_free (s);
84 return fd;
85}
86
Neale Rannscbe8d652018-04-27 04:42:47 -070087#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010088
Damjan Marion7c6102b2019-11-08 17:59:56 +010089static void
90tap_free (vlib_main_t * vm, virtio_if_t * vif)
91{
92 virtio_main_t *mm = &virtio_main;
93 tap_main_t *tm = &tap_main;
94 int i;
95
96 /* *INDENT-OFF* */
97 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
98 close (vif->vhost_fds[i]);
99 vec_foreach_index (i, vif->rxq_vrings)
100 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
101 vec_foreach_index (i, vif->txq_vrings)
102 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
103 /* *INDENT-ON* */
104
105 if (vif->tap_fd != -1)
106 close (vif->tap_fd);
107
108 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;
Damjan Marion4e671d22017-12-09 21:19:01 +0100131 int i;
Damjan Marion2df39092017-12-04 20:03:37 +0100132 int old_netns_fd = -1;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100133 struct ifreq ifr = {.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR };
Damjan Marion8389fb92017-10-13 18:29:53 +0200134 size_t hdrsz;
135 struct vhost_memory *vhost_mem = 0;
136 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100137 clib_error_t *err = 0;
Damjan Marion39807d02019-11-08 13:52:28 +0100138 unsigned int tap_features;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100139 int tfd, vfd, nfd = -1;
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200140 char *host_if_name = 0;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100141 unsigned int offload = 0;
142 u16 num_q_pairs;
Damjan Marion2df39092017-12-04 20:03:37 +0100143
144 if (args->id != ~0)
145 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700146 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100147 {
148 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
149 args->error = clib_error_return (0, "interface already exists");
150 return;
151 }
152 }
153 else
154 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700155 args->id = clib_bitmap_first_clear (tm->tap_ids);
156 }
Damjan Marion2df39092017-12-04 20:03:37 +0100157
Neale Rannscbe8d652018-04-27 04:42:47 -0700158 if (args->id > TAP_MAX_INSTANCE)
159 {
160 args->rv = VNET_API_ERROR_UNSPECIFIED;
161 args->error = clib_error_return (0, "cannot find free interface id");
162 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100163 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200164
Damjan Marion7c6102b2019-11-08 17:59:56 +0100165 pool_get_zero (vim->interfaces, vif);
166 vif->type = VIRTIO_IF_TYPE_TAP;
Damjan Marion8389fb92017-10-13 18:29:53 +0200167 vif->dev_instance = vif - vim->interfaces;
Damjan Marion2df39092017-12-04 20:03:37 +0100168 vif->id = args->id;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100169 vif->num_txqs = thm->n_vlib_mains;
170 vif->num_rxqs = args->num_rx_queues;
171 num_q_pairs = clib_max (vif->num_rxqs, vif->num_txqs);
Damjan Marion2df39092017-12-04 20:03:37 +0100172
Damjan Marion7c6102b2019-11-08 17:59:56 +0100173 if (ethernet_mac_address_is_zero (args->host_mac_addr))
174 ethernet_mac_address_generate (args->host_mac_addr);
175 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
176
177 if ((vif->tap_fd = tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200178 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100179 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
180 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
181 goto error;
182 }
183 tap_log_dbg (vif, "open tap fd %d", tfd);
184
185 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
186 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
187 if ((tap_features & IFF_VNET_HDR) == 0)
188 {
189 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
190 args->error = clib_error_return (0, "vhost-net backend not available");
Damjan Marion8389fb92017-10-13 18:29:53 +0200191 goto error;
192 }
193
Damjan Marion7c6102b2019-11-08 17:59:56 +0100194 if ((tap_features & IFF_MULTI_QUEUE) == 0)
195 {
196 if (args->num_rx_queues > 1)
197 {
198 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
199 args->error = clib_error_return (0, "multiqueue not supported");
200 goto error;
201 }
202 vif->num_rxqs = vif->num_txqs = num_q_pairs = 1;
203 }
204 else
205 ifr.ifr_flags |= IFF_MULTI_QUEUE;
206
207 hdrsz = sizeof (struct virtio_net_hdr_v1);
208 if (args->tap_flags & TAP_FLAG_GSO)
209 {
210 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
211 vif->gso_enabled = 1;
212 }
213
214 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
215 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
216 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
217
218 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
219 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
220
221 if (!args->host_if_name)
222 host_if_name = ifr.ifr_ifrn.ifrn_name;
223 else
224 host_if_name = (char *) args->host_if_name;
225
Andrew Yourtchenko0d3d4822019-11-25 18:25:46 +0000226 if (fcntl (tfd, F_SETFL, O_NONBLOCK) < 0)
227 {
228 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
229 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
230 goto error;
231 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100232
233 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u", tfd, hdrsz);
234 _IOCTL (tfd, TUNSETVNETHDRSZ, &hdrsz);
235
236 i = INT_MAX;
237 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", tfd, i);
238 _IOCTL (tfd, TUNSETSNDBUF, &i);
239
240 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", tfd, offload);
241 _IOCTL (tfd, TUNSETOFFLOAD, offload);
242
243 clib_memset (&ifr, 0, sizeof (ifr));
244 ifr.ifr_addr.sa_family = ARPHRD_ETHER;
245 clib_memcpy (ifr.ifr_hwaddr.sa_data, vif->host_mac_addr, 6);
246 tap_log_dbg (vif, "SIOCSIFHWADDR: fd %d hwaddr %U", tfd,
247 format_hex_bytes, ifr.ifr_hwaddr.sa_data, 6);
248 _IOCTL (tfd, SIOCSIFHWADDR, (void *) &ifr);
249
250 /* open vhost-net fd for each queue pair and set ownership */
251 for (i = 0; i < num_q_pairs; i++)
252 {
253 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
254 {
255 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
256 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
257 goto error;
258 }
259 vec_add1 (vif->vhost_fds, vfd);
260 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
261 _IOCTL (vfd, VHOST_SET_OWNER, 0);
262 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
263 }
264
265 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
266 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
267 vif->remote_features);
Damjan Marion8389fb92017-10-13 18:29:53 +0200268
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200269 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200270 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100271 args->rv = VNET_API_ERROR_UNSUPPORTED;
272 args->error = clib_error_return (0, "vhost-net backend doesn't support "
273 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200274 goto error;
275 }
276
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200277 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
278 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200279 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100280 args->rv = VNET_API_ERROR_UNSUPPORTED;
281 args->error = clib_error_return (0, "vhost-net backend doesn't support "
282 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200283 goto error;
284 }
285
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200286 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200287 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100288 args->rv = VNET_API_ERROR_UNSUPPORTED;
289 args->error = clib_error_return (0, "vhost-net backend doesn't support "
290 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200291 goto error;
292 }
293
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200294 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
295 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
296 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
297
298 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200299
Paul Vinciguerra97c998c2019-10-29 16:11:09 -0400300 /* if namespace is specified, all further netlink messages should be executed
Damjan Marion2df39092017-12-04 20:03:37 +0100301 after we change our net namespace */
302 if (args->host_namespace)
Damjan Marion8389fb92017-10-13 18:29:53 +0200303 {
Damjan Marion2df39092017-12-04 20:03:37 +0100304 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100305 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
Damjan Marion2df39092017-12-04 20:03:37 +0100306 {
307 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
308 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
309 args->host_namespace);
310 goto error;
311 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100312 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200313 host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100314 if (args->error)
315 {
316 args->rv = VNET_API_ERROR_NETLINK_ERROR;
317 goto error;
318 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100319 if (setns (nfd, CLONE_NEWNET) == -1)
Damjan Marion2df39092017-12-04 20:03:37 +0100320 {
321 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
322 args->error = clib_error_return_unix (0, "setns '%s'",
323 args->host_namespace);
324 goto error;
325 }
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200326 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
Damjan Marion2df39092017-12-04 20:03:37 +0100327 {
328 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
329 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200330 host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100331 goto error;
332 }
333 }
334 else
335 {
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200336 if (host_if_name)
Damjan Marion2df39092017-12-04 20:03:37 +0100337 {
338 args->error = vnet_netlink_set_link_name (vif->ifindex,
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200339 host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100340 if (args->error)
341 {
342 args->rv = VNET_API_ERROR_NETLINK_ERROR;
343 goto error;
344 }
345 }
346 }
347
Damjan Marion2df39092017-12-04 20:03:37 +0100348 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100349 {
Damjan Marion2df39092017-12-04 20:03:37 +0100350 args->error = vnet_netlink_set_link_master (vif->ifindex,
351 (char *) args->host_bridge);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100352 if (args->error)
353 {
354 args->rv = VNET_API_ERROR_NETLINK_ERROR;
355 goto error;
356 }
357 }
358
359 if (args->host_ip4_prefix_len)
360 {
361 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
362 &args->host_ip4_addr,
363 args->host_ip4_prefix_len);
364 if (args->error)
365 {
366 args->rv = VNET_API_ERROR_NETLINK_ERROR;
367 goto error;
368 }
369 }
370
371 if (args->host_ip6_prefix_len)
372 {
373 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
374 &args->host_ip6_addr,
375 args->host_ip6_prefix_len);
376 if (args->error)
377 {
378 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200379 goto error;
380 }
381 }
382
Damjan Marion2df39092017-12-04 20:03:37 +0100383 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
384 if (args->error)
385 {
386 args->rv = VNET_API_ERROR_NETLINK_ERROR;
387 goto error;
388 }
389
Damjan Marion7866c452018-01-18 13:35:11 +0100390 if (args->host_ip4_gw_set)
391 {
392 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
393 if (args->error)
394 {
395 args->rv = VNET_API_ERROR_NETLINK_ERROR;
396 goto error;
397 }
398 }
399
400 if (args->host_ip6_gw_set)
401 {
402 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
403 if (args->error)
404 {
405 args->rv = VNET_API_ERROR_NETLINK_ERROR;
406 goto error;
407 }
408 }
409
Damjan Marion2df39092017-12-04 20:03:37 +0100410 /* switch back to old net namespace */
411 if (args->host_namespace)
412 {
413 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
414 {
415 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
416 args->error = clib_error_return_unix (0, "setns '%s'",
417 args->host_namespace);
418 goto error;
419 }
420 }
421
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200422 if (args->host_mtu_set)
423 {
424 args->error =
425 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
426 if (args->error)
427 {
428 args->rv = VNET_API_ERROR_NETLINK_ERROR;
429 goto error;
430 }
431 }
432 else if (tm->host_mtu_size != 0)
433 {
434 args->error =
435 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
436 if (args->error)
437 {
438 args->rv = VNET_API_ERROR_NETLINK_ERROR;
439 goto error;
440 }
441 args->host_mtu_set = 1;
442 args->host_mtu_size = tm->host_mtu_size;
443 }
444
Damjan Marion7c6102b2019-11-08 17:59:56 +0100445 for (i = 0; i < num_q_pairs; i++)
446 {
447 if (i < vif->num_rxqs && (args->error =
448 virtio_vring_init (vm, vif, RX_QUEUE (i),
449 args->rx_ring_sz)))
450 {
451 args->rv = VNET_API_ERROR_INIT_FAILED;
452 goto error;
453 }
454
455 if (i < vif->num_txqs && (args->error =
456 virtio_vring_init (vm, vif, TX_QUEUE (i),
457 args->tx_ring_sz)))
458 {
459 args->rv = VNET_API_ERROR_INIT_FAILED;
460 goto error;
461 }
462 }
463
464 /* setup features and memtable */
Damjan Marion8389fb92017-10-13 18:29:53 +0200465 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
466 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400467 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200468 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800469 vhost_mem->regions[0].memory_size = vpm->max_size;
470 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
471 vhost_mem->regions[0].userspace_addr =
472 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200473
Damjan Marion7c6102b2019-11-08 17:59:56 +0100474 for (i = 0; i < vhost_mem->nregions; i++)
475 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
476 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
477 vhost_mem->regions[0].memory_size,
478 vhost_mem->regions[0].guest_phys_addr,
479 vhost_mem->regions[0].userspace_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200480
Damjan Marion7c6102b2019-11-08 17:59:56 +0100481
482 for (i = 0; i < num_q_pairs; i++)
Damjan Marion8389fb92017-10-13 18:29:53 +0200483 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100484 int fd = vif->vhost_fds[i];
485 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
486 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
487 fd, vif->features);
488 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
489 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200490 }
Damjan Marion7c6102b2019-11-08 17:59:56 +0100491
492 /* finish initializing queue pair */
493 for (i = 0; i < num_q_pairs * 2; i++)
494 {
495 struct vhost_vring_addr addr = { 0 };
496 struct vhost_vring_state state = { 0 };
497 struct vhost_vring_file file = { 0 };
498 virtio_vring_t *vring;
499 u16 qp = i >> 1;
500 int fd = vif->vhost_fds[qp];
501
502 if (i & 1)
503 {
504 if (qp >= vif->num_txqs)
505 continue;
506 vring = vec_elt_at_index (vif->txq_vrings, qp);
507 }
508 else
509 {
510 if (qp >= vif->num_rxqs)
511 continue;
512 vring = vec_elt_at_index (vif->rxq_vrings, qp);
513 }
514
515 addr.index = state.index = file.index = vring->queue_id & 1;
516 state.num = vring->size;
517 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
518 state.index, state.num);
519 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
520
521 addr.flags = 0;
522 addr.desc_user_addr = pointer_to_uword (vring->desc);
523 addr.avail_user_addr = pointer_to_uword (vring->avail);
524 addr.used_user_addr = pointer_to_uword (vring->used);
525
526 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
527 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
528 "used_user_addr 0x%lx", fd, addr.index,
529 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
530 addr.used_user_addr);
531 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
532
533 file.fd = vring->call_fd;
534 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
535 fd, file.index, file.fd);
536 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
537
538 file.fd = vring->kick_fd;
539 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
540 fd, file.index, file.fd);
541 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
542
543 file.fd = tfd;
544 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
545 fd, file.index, file.fd);
546 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
547 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200548
Damjan Marion2df39092017-12-04 20:03:37 +0100549 if (!args->mac_addr_set)
Benoît Gannefe750c22019-03-25 11:41:34 +0100550 ethernet_mac_address_generate (args->mac_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200551
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200552 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
553
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200554 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
Benoît Gannec30d87e2019-07-15 17:16:49 +0200555 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
556 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200557 vif->host_mtu_size = args->host_mtu_size;
Milan Lenco73e7f422017-12-14 10:04:25 +0100558 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
559 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
560 if (args->host_ip4_prefix_len)
561 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
562 if (args->host_ip6_prefix_len)
563 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
564
Damjan Marion91c6ef72017-12-01 13:34:24 +0100565 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
Damjan Marion2df39092017-12-04 20:03:37 +0100566 vif->dev_instance,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200567 vif->mac_addr,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100568 &vif->hw_if_index,
569 virtio_eth_flag_change);
570 if (args->error)
571 {
572 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
573 goto error;
574 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200575
Neale Rannscbe8d652018-04-27 04:42:47 -0700576 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200577 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
578 vif->sw_if_index = sw->sw_if_index;
579 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200580 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200581 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
582 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200583 if (args->tap_flags & TAP_FLAG_GSO)
584 {
585 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200586 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200587 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
588 virtio_input_node.index);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100589
590 for (i = 0; i < vif->num_rxqs; i++)
591 {
592 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
593 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
594 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
595 }
596
Damjan Marion8389fb92017-10-13 18:29:53 +0200597 vif->per_interface_next_index = ~0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000598 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
Damjan Marion8389fb92017-10-13 18:29:53 +0200599 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
600 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
601 VNET_HW_INTERFACE_FLAG_LINK_UP);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000602 vif->cxq_vring = NULL;
603
Damjan Marion8389fb92017-10-13 18:29:53 +0200604 goto done;
605
606error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100607 if (err)
608 {
609 ASSERT (args->error == 0);
610 args->error = err;
611 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
612 }
Benoît Gannec30d87e2019-07-15 17:16:49 +0200613
Damjan Marion7c6102b2019-11-08 17:59:56 +0100614 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200615done:
616 if (vhost_mem)
617 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100618 if (old_netns_fd != -1)
619 close (old_netns_fd);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100620 if (nfd != -1)
621 close (nfd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200622}
623
624int
625tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
626{
627 vnet_main_t *vnm = vnet_get_main ();
628 virtio_main_t *mm = &virtio_main;
629 int i;
630 virtio_if_t *vif;
631 vnet_hw_interface_t *hw;
632
Dave Barach3940de32019-07-23 16:28:36 -0400633 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200634 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
635 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
636
637 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
638
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200639 if (vif->type != VIRTIO_IF_TYPE_TAP)
640 return VNET_API_ERROR_INVALID_INTERFACE;
641
Damjan Marion8389fb92017-10-13 18:29:53 +0200642 /* bring down the interface */
643 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
644 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100645 for (i = 0; i < vif->num_rxqs; i++)
646 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200647
648 ethernet_delete_interface (vnm, vif->hw_if_index);
649 vif->hw_if_index = ~0;
650
Damjan Marion7c6102b2019-11-08 17:59:56 +0100651 tap_free (vm, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200652
653 return 0;
654}
655
656int
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200657tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
658{
659 vnet_main_t *vnm = vnet_get_main ();
660 virtio_main_t *mm = &virtio_main;
661 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400662 vnet_hw_interface_t *hw;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200663 clib_error_t *err = 0;
664
Dave Barach3940de32019-07-23 16:28:36 -0400665 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
666
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200667 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
668 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
669
670 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
671
672 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
673 const unsigned int gso_off = 0;
674 unsigned int offload = enable_disable ? gso_on : gso_off;
675 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
676 vif->gso_enabled = enable_disable ? 1 : 0;
677 if (enable_disable)
678 {
679 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
680 {
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200681 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
682 }
683 }
684 else
685 {
686 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
687 {
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200688 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
689 }
690 }
691
692error:
693 if (err)
694 {
695 clib_warning ("Error %s gso on sw_if_index %d",
696 enable_disable ? "enabling" : "disabling", sw_if_index);
697 return VNET_API_ERROR_SYSCALL_ERROR_3;
698 }
699 return 0;
700}
701
702int
Damjan Marion8389fb92017-10-13 18:29:53 +0200703tap_dump_ifs (tap_interface_details_t ** out_tapids)
704{
705 vnet_main_t *vnm = vnet_get_main ();
706 virtio_main_t *mm = &virtio_main;
707 virtio_if_t *vif;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000708 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200709 vnet_hw_interface_t *hi;
710 tap_interface_details_t *r_tapids = NULL;
711 tap_interface_details_t *tapid = NULL;
712
713 /* *INDENT-OFF* */
714 pool_foreach (vif, mm->interfaces,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200715 if (vif->type != VIRTIO_IF_TYPE_TAP)
716 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200717 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400718 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100719 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200720 tapid->sw_if_index = vif->sw_if_index;
721 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
722 clib_memcpy(tapid->dev_name, hi->name,
Milan Lenco73e7f422017-12-14 10:04:25 +0100723 MIN (ARRAY_LEN (tapid->dev_name) - 1,
724 strlen ((const char *) hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000725 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
726 tapid->rx_ring_sz = vring->size;
727 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
728 tapid->tx_ring_sz = vring->size;
Milan Lenco73e7f422017-12-14 10:04:25 +0100729 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
730 if (vif->host_if_name)
731 {
732 clib_memcpy(tapid->host_if_name, vif->host_if_name,
733 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
734 strlen ((const char *) vif->host_if_name)));
735 }
736 if (vif->net_ns)
737 {
738 clib_memcpy(tapid->host_namespace, vif->net_ns,
739 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
740 strlen ((const char *) vif->net_ns)));
741 }
742 if (vif->host_bridge)
743 {
744 clib_memcpy(tapid->host_bridge, vif->host_bridge,
745 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
746 strlen ((const char *) vif->host_bridge)));
747 }
748 if (vif->host_ip4_prefix_len)
749 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
750 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
751 if (vif->host_ip6_prefix_len)
752 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
753 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200754 tapid->host_mtu_size = vif->host_mtu_size;
Damjan Marion8389fb92017-10-13 18:29:53 +0200755 );
756 /* *INDENT-ON* */
757
758 *out_tapids = r_tapids;
759
760 return 0;
761}
762
763static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200764tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
765{
766 tap_main_t *tm = &tap_main;
767
768 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
769 {
770 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
771 ;
772 else
773 return clib_error_return (0, "unknown input `%U'",
774 format_unformat_error, input);
775 }
776
777 return 0;
778}
779
780/* tap { host-mtu <size> } configuration. */
781VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
782
783static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +0200784tap_init (vlib_main_t * vm)
785{
Damjan Marion2df39092017-12-04 20:03:37 +0100786 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200787 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -0700788
Damjan Marion07a38572018-01-21 06:44:18 -0800789 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200790 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -0700791
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200792 tm->host_mtu_size = 0;
793
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200794 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +0200795}
796
797VLIB_INIT_FUNCTION (tap_init);
798
799/*
800 * fd.io coding-style-patch-verification: ON
801 *
802 * Local Variables:
803 * eval: (c-set-style "gnu")
804 * End:
805 */