blob: 35f1f2a345175a3c60107aad8080680bddcfb4ef [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 Marion2df39092017-12-04 20:03:37 +010028#include <sched.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020029
30#include <linux/netlink.h>
31#include <linux/rtnetlink.h>
32
33#include <vlib/vlib.h>
34#include <vlib/unix/unix.h>
35#include <vnet/ethernet/ethernet.h>
Damjan Marion91c6ef72017-12-01 13:34:24 +010036#include <vnet/ip/ip4_packet.h>
37#include <vnet/ip/ip6_packet.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010038#include <vnet/devices/netlink.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020039#include <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010040#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020041
Damjan Marion2df39092017-12-04 20:03:37 +010042tap_main_t tap_main;
43
Damjan Marion8389fb92017-10-13 18:29:53 +020044#define _IOCTL(fd,a,...) \
45 if (ioctl (fd, a, __VA_ARGS__) < 0) \
46 { \
47 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
48 goto error; \
49 }
50
51static u32
52virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
53 u32 flags)
54{
55 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010056 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020057 return 0;
58}
59
Mohsin Kazmie4ac48e2019-04-17 12:06:57 +020060void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
61
62static clib_error_t *
63call_tap_read_ready (clib_file_t * uf)
64{
65 /* nothing to do */
66 return 0;
67}
68
69static void
70tap_delete_if_cp (u32 * sw_if_index)
71{
72 vlib_main_t *vm = vlib_get_main ();
73 tap_delete_if (vm, *sw_if_index);
74}
75
76/*
77 * Tap clean-up routine:
78 * Linux side of tap interface can be deleted i.e. tap is
79 * attached to container and if someone will delete this
80 * container, will also removes tap interface. While VPP
81 * will have other side of tap. This function will RPC
82 * main thread to call the tap_delete_if to cleanup tap.
83 */
84static clib_error_t *
85call_tap_error_ready (clib_file_t * uf)
86{
87 vl_api_rpc_call_main_thread (tap_delete_if_cp, (u8 *) & uf->private_data,
88 sizeof (uf->private_data));
89 return 0;
90}
91
Damjan Marion2df39092017-12-04 20:03:37 +010092static int
93open_netns_fd (char *netns)
94{
95 u8 *s = 0;
96 int fd;
97
98 if (strncmp (netns, "pid:", 4) == 0)
99 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
100 else if (netns[0] == '/')
101 s = format (0, "%s%c", netns, 0);
102 else
103 s = format (0, "/var/run/netns/%s%c", netns, 0);
104
105 fd = open ((char *) s, O_RDONLY);
106 vec_free (s);
107 return fd;
108}
109
Neale Rannscbe8d652018-04-27 04:42:47 -0700110#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +0100111
Damjan Marion91c6ef72017-12-01 13:34:24 +0100112void
Damjan Marion8389fb92017-10-13 18:29:53 +0200113tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
114{
115 vnet_main_t *vnm = vnet_get_main ();
116 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100117 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200118 vnet_sw_interface_t *sw;
119 vnet_hw_interface_t *hw;
Damjan Marion4e671d22017-12-09 21:19:01 +0100120 int i;
Damjan Marion2df39092017-12-04 20:03:37 +0100121 int old_netns_fd = -1;
Damjan Marion8389fb92017-10-13 18:29:53 +0200122 struct ifreq ifr;
123 size_t hdrsz;
124 struct vhost_memory *vhost_mem = 0;
125 virtio_if_t *vif = 0;
Mohsin Kazmie4ac48e2019-04-17 12:06:57 +0200126 clib_file_t t = { 0 };
Damjan Marion91c6ef72017-12-01 13:34:24 +0100127 clib_error_t *err = 0;
Steven Luong4a310d22019-02-21 14:55:52 -0800128 int fd = -1;
Damjan Marion2df39092017-12-04 20:03:37 +0100129
130 if (args->id != ~0)
131 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700132 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100133 {
134 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
135 args->error = clib_error_return (0, "interface already exists");
136 return;
137 }
138 }
139 else
140 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700141 args->id = clib_bitmap_first_clear (tm->tap_ids);
142 }
Damjan Marion2df39092017-12-04 20:03:37 +0100143
Neale Rannscbe8d652018-04-27 04:42:47 -0700144 if (args->id > TAP_MAX_INSTANCE)
145 {
146 args->rv = VNET_API_ERROR_UNSPECIFIED;
147 args->error = clib_error_return (0, "cannot find free interface id");
148 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100149 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200150
Dave Barachb7b92992018-10-17 10:38:51 -0400151 clib_memset (&ifr, 0, sizeof (ifr));
Damjan Marion8389fb92017-10-13 18:29:53 +0200152 pool_get (vim->interfaces, vif);
153 vif->dev_instance = vif - vim->interfaces;
154 vif->tap_fd = -1;
Damjan Marion2df39092017-12-04 20:03:37 +0100155 vif->id = args->id;
156
Damjan Marion8389fb92017-10-13 18:29:53 +0200157 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
158 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100159 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
160 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200161 goto error;
162 }
163
164 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
165
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200166 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200167 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100168 args->rv = VNET_API_ERROR_UNSUPPORTED;
169 args->error = clib_error_return (0, "vhost-net backend doesn't support "
170 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200171 goto error;
172 }
173
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200174 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
175 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200176 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100177 args->rv = VNET_API_ERROR_UNSUPPORTED;
178 args->error = clib_error_return (0, "vhost-net backend doesn't support "
179 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200180 goto error;
181 }
182
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200183 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200184 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100185 args->rv = VNET_API_ERROR_UNSUPPORTED;
186 args->error = clib_error_return (0, "vhost-net backend doesn't support "
187 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200188 goto error;
189 }
190
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200191 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
192 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
193 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
194
195 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200196
197 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
198
199 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
200 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100201 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
202 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200203 goto error;
204 }
205
206 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200207 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
Damjan Marion2df39092017-12-04 20:03:37 +0100208 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
Damjan Marion8389fb92017-10-13 18:29:53 +0200209
Mohsin Kazmi2a6861f2019-04-15 13:17:55 +0200210 if (!args->host_if_name)
211 args->host_if_name = (u8 *) ifr.ifr_ifrn.ifrn_name;
212
Damjan Marion8389fb92017-10-13 18:29:53 +0200213 unsigned int offload = 0;
214 hdrsz = sizeof (struct virtio_net_hdr_v1);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200215 if (args->tap_flags & TAP_FLAG_GSO)
216 {
217 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
218 vif->gso_enabled = 1;
219 }
220 else
221 {
222 vif->gso_enabled = 0;
223 }
224
Damjan Marion8389fb92017-10-13 18:29:53 +0200225 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
226 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
227 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
228
Damjan Marion2df39092017-12-04 20:03:37 +0100229 /* if namespace is specified, all further netlink messages should be excuted
230 after we change our net namespace */
231 if (args->host_namespace)
Damjan Marion8389fb92017-10-13 18:29:53 +0200232 {
Damjan Marion2df39092017-12-04 20:03:37 +0100233 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
234 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
235 {
236 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
237 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
238 args->host_namespace);
239 goto error;
240 }
241 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
242 (char *) args->host_if_name);
243 if (args->error)
244 {
245 args->rv = VNET_API_ERROR_NETLINK_ERROR;
246 goto error;
247 }
Steven Luong4a310d22019-02-21 14:55:52 -0800248 if (setns (fd, CLONE_NEWNET) == -1)
Damjan Marion2df39092017-12-04 20:03:37 +0100249 {
250 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
251 args->error = clib_error_return_unix (0, "setns '%s'",
252 args->host_namespace);
253 goto error;
254 }
Damjan Marion2df39092017-12-04 20:03:37 +0100255 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
256 {
257 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
258 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
259 args->host_if_name);
260 goto error;
261 }
262 }
263 else
264 {
265 if (args->host_if_name)
266 {
267 args->error = vnet_netlink_set_link_name (vif->ifindex,
268 (char *)
269 args->host_if_name);
270 if (args->error)
271 {
272 args->rv = VNET_API_ERROR_NETLINK_ERROR;
273 goto error;
274 }
275 }
276 }
277
278 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
279 {
280 args->error = vnet_netlink_set_link_addr (vif->ifindex,
281 args->host_mac_addr);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100282 if (args->error)
Damjan Marion8389fb92017-10-13 18:29:53 +0200283 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100284 args->rv = VNET_API_ERROR_NETLINK_ERROR;
285 goto error;
286 }
287 }
288
Damjan Marion2df39092017-12-04 20:03:37 +0100289 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100290 {
Damjan Marion2df39092017-12-04 20:03:37 +0100291 args->error = vnet_netlink_set_link_master (vif->ifindex,
292 (char *) args->host_bridge);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100293 if (args->error)
294 {
295 args->rv = VNET_API_ERROR_NETLINK_ERROR;
296 goto error;
297 }
298 }
299
Damjan Marion2df39092017-12-04 20:03:37 +0100300
Damjan Marion91c6ef72017-12-01 13:34:24 +0100301 if (args->host_ip4_prefix_len)
302 {
303 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
304 &args->host_ip4_addr,
305 args->host_ip4_prefix_len);
306 if (args->error)
307 {
308 args->rv = VNET_API_ERROR_NETLINK_ERROR;
309 goto error;
310 }
311 }
312
313 if (args->host_ip6_prefix_len)
314 {
315 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
316 &args->host_ip6_addr,
317 args->host_ip6_prefix_len);
318 if (args->error)
319 {
320 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200321 goto error;
322 }
323 }
324
Damjan Marion2df39092017-12-04 20:03:37 +0100325 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
326 if (args->error)
327 {
328 args->rv = VNET_API_ERROR_NETLINK_ERROR;
329 goto error;
330 }
331
Damjan Marion7866c452018-01-18 13:35:11 +0100332 if (args->host_ip4_gw_set)
333 {
334 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
335 if (args->error)
336 {
337 args->rv = VNET_API_ERROR_NETLINK_ERROR;
338 goto error;
339 }
340 }
341
342 if (args->host_ip6_gw_set)
343 {
344 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
345 if (args->error)
346 {
347 args->rv = VNET_API_ERROR_NETLINK_ERROR;
348 goto error;
349 }
350 }
351
Damjan Marion2df39092017-12-04 20:03:37 +0100352 /* switch back to old net namespace */
353 if (args->host_namespace)
354 {
355 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
356 {
357 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
358 args->error = clib_error_return_unix (0, "setns '%s'",
359 args->host_namespace);
360 goto error;
361 }
362 }
363
Damjan Marion8389fb92017-10-13 18:29:53 +0200364 /* Set vhost memory table */
365 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
366 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400367 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200368 vhost_mem->nregions = 1;
369 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
370 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
371
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000372 if ((args->error =
373 virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200374 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100375 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200376 goto error;
377 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000378 vif->num_rxqs = 1;
Damjan Marion8389fb92017-10-13 18:29:53 +0200379
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000380 if ((args->error =
381 virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200382 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100383 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200384 goto error;
385 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000386 vif->num_txqs = 1;
Damjan Marion8389fb92017-10-13 18:29:53 +0200387
Damjan Marion2df39092017-12-04 20:03:37 +0100388 if (!args->mac_addr_set)
Benoît Gannefe750c22019-03-25 11:41:34 +0100389 ethernet_mac_address_generate (args->mac_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200390
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200391 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
392
Damjan Marion2df39092017-12-04 20:03:37 +0100393 vif->host_if_name = args->host_if_name;
394 args->host_if_name = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100395 vif->net_ns = args->host_namespace;
396 args->host_namespace = 0;
Milan Lenco73e7f422017-12-14 10:04:25 +0100397 vif->host_bridge = args->host_bridge;
398 args->host_bridge = 0;
399 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
400 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
401 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
402 if (args->host_ip4_prefix_len)
403 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
404 if (args->host_ip6_prefix_len)
405 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
406
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200407 vif->type = VIRTIO_IF_TYPE_TAP;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100408 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
Damjan Marion2df39092017-12-04 20:03:37 +0100409 vif->dev_instance,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200410 vif->mac_addr,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100411 &vif->hw_if_index,
412 virtio_eth_flag_change);
413 if (args->error)
414 {
415 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
416 goto error;
417 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200418
Neale Rannscbe8d652018-04-27 04:42:47 -0700419 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200420 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
421 vif->sw_if_index = sw->sw_if_index;
422 args->sw_if_index = vif->sw_if_index;
423 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
424 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200425 if (args->tap_flags & TAP_FLAG_GSO)
426 {
427 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
428 vnm->interface_main.gso_interface_count++;
429 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200430 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
431 virtio_input_node.index);
432 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
433 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
434 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
435 vif->per_interface_next_index = ~0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000436 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
Damjan Marion8389fb92017-10-13 18:29:53 +0200437 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
438 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
439 VNET_HW_INTERFACE_FLAG_LINK_UP);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000440 vif->cxq_vring = NULL;
441
Mohsin Kazmie4ac48e2019-04-17 12:06:57 +0200442 t.read_function = call_tap_read_ready;
443 t.error_function = call_tap_error_ready;
444 t.file_descriptor = vif->tap_fd;
445 t.private_data = vif->sw_if_index;
446 t.description = format (0, "tap sw_if_index %u fd: %u",
447 vif->sw_if_index, vif->tap_fd);
448 vif->tap_file_index = clib_file_add (&file_main, &t);
449
Damjan Marion8389fb92017-10-13 18:29:53 +0200450 goto done;
451
452error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100453 if (err)
454 {
455 ASSERT (args->error == 0);
456 args->error = err;
457 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
458 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200459 if (vif->tap_fd != -1)
460 close (vif->tap_fd);
461 if (vif->fd != -1)
462 close (vif->fd);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000463 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
464 RX_QUEUE (i));
465 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
466 TX_QUEUE (i));
467 vec_free (vif->rxq_vrings);
468 vec_free (vif->txq_vrings);
Dave Barachb7b92992018-10-17 10:38:51 -0400469 clib_memset (vif, 0, sizeof (virtio_if_t));
Damjan Marion8389fb92017-10-13 18:29:53 +0200470 pool_put (vim->interfaces, vif);
471
472done:
473 if (vhost_mem)
474 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100475 if (old_netns_fd != -1)
476 close (old_netns_fd);
Steven Luong4a310d22019-02-21 14:55:52 -0800477 if (fd != -1)
478 close (fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200479}
480
481int
482tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
483{
484 vnet_main_t *vnm = vnet_get_main ();
485 virtio_main_t *mm = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100486 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200487 int i;
488 virtio_if_t *vif;
489 vnet_hw_interface_t *hw;
490
491 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
492 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
493 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
494
495 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
496
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200497 if (vif->type != VIRTIO_IF_TYPE_TAP)
498 return VNET_API_ERROR_INVALID_INTERFACE;
499
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200500 /* decrement if this was a GSO interface */
501 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
502 vnm->interface_main.gso_interface_count--;
503
Mohsin Kazmie4ac48e2019-04-17 12:06:57 +0200504 clib_file_del_by_index (&file_main, vif->tap_file_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200505 /* bring down the interface */
506 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
507 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000508 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0));
Damjan Marion8389fb92017-10-13 18:29:53 +0200509
510 ethernet_delete_interface (vnm, vif->hw_if_index);
511 vif->hw_if_index = ~0;
512
513 if (vif->tap_fd != -1)
514 close (vif->tap_fd);
515 if (vif->fd != -1)
516 close (vif->fd);
517
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000518 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
519 RX_QUEUE (i));
520 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
521 TX_QUEUE (i));
522 vec_free (vif->rxq_vrings);
523 vec_free (vif->txq_vrings);
Damjan Marion8389fb92017-10-13 18:29:53 +0200524
Neale Rannscbe8d652018-04-27 04:42:47 -0700525 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
Dave Barachb7b92992018-10-17 10:38:51 -0400526 clib_memset (vif, 0, sizeof (*vif));
Damjan Marion8389fb92017-10-13 18:29:53 +0200527 pool_put (mm->interfaces, vif);
528
529 return 0;
530}
531
532int
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200533tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
534{
535 vnet_main_t *vnm = vnet_get_main ();
536 virtio_main_t *mm = &virtio_main;
537 virtio_if_t *vif;
538 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
539 clib_error_t *err = 0;
540
541 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
542 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
543
544 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
545
546 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
547 const unsigned int gso_off = 0;
548 unsigned int offload = enable_disable ? gso_on : gso_off;
549 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
550 vif->gso_enabled = enable_disable ? 1 : 0;
551 if (enable_disable)
552 {
553 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
554 {
555 vnm->interface_main.gso_interface_count++;
556 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
557 }
558 }
559 else
560 {
561 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
562 {
563 vnm->interface_main.gso_interface_count--;
564 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
565 }
566 }
567
568error:
569 if (err)
570 {
571 clib_warning ("Error %s gso on sw_if_index %d",
572 enable_disable ? "enabling" : "disabling", sw_if_index);
573 return VNET_API_ERROR_SYSCALL_ERROR_3;
574 }
575 return 0;
576}
577
578int
Damjan Marion8389fb92017-10-13 18:29:53 +0200579tap_dump_ifs (tap_interface_details_t ** out_tapids)
580{
581 vnet_main_t *vnm = vnet_get_main ();
582 virtio_main_t *mm = &virtio_main;
583 virtio_if_t *vif;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000584 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200585 vnet_hw_interface_t *hi;
586 tap_interface_details_t *r_tapids = NULL;
587 tap_interface_details_t *tapid = NULL;
588
589 /* *INDENT-OFF* */
590 pool_foreach (vif, mm->interfaces,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200591 if (vif->type != VIRTIO_IF_TYPE_TAP)
592 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200593 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400594 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100595 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200596 tapid->sw_if_index = vif->sw_if_index;
597 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
598 clib_memcpy(tapid->dev_name, hi->name,
Milan Lenco73e7f422017-12-14 10:04:25 +0100599 MIN (ARRAY_LEN (tapid->dev_name) - 1,
600 strlen ((const char *) hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000601 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
602 tapid->rx_ring_sz = vring->size;
603 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
604 tapid->tx_ring_sz = vring->size;
Milan Lenco73e7f422017-12-14 10:04:25 +0100605 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
606 if (vif->host_if_name)
607 {
608 clib_memcpy(tapid->host_if_name, vif->host_if_name,
609 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
610 strlen ((const char *) vif->host_if_name)));
611 }
612 if (vif->net_ns)
613 {
614 clib_memcpy(tapid->host_namespace, vif->net_ns,
615 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
616 strlen ((const char *) vif->net_ns)));
617 }
618 if (vif->host_bridge)
619 {
620 clib_memcpy(tapid->host_bridge, vif->host_bridge,
621 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
622 strlen ((const char *) vif->host_bridge)));
623 }
624 if (vif->host_ip4_prefix_len)
625 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
626 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
627 if (vif->host_ip6_prefix_len)
628 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
629 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Damjan Marion8389fb92017-10-13 18:29:53 +0200630 );
631 /* *INDENT-ON* */
632
633 *out_tapids = r_tapids;
634
635 return 0;
636}
637
638static clib_error_t *
639tap_init (vlib_main_t * vm)
640{
Damjan Marion2df39092017-12-04 20:03:37 +0100641 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200642 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -0700643
Damjan Marion07a38572018-01-21 06:44:18 -0800644 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200645 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -0700646
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200647 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +0200648}
649
650VLIB_INIT_FUNCTION (tap_init);
651
652/*
653 * fd.io coding-style-patch-verification: ON
654 *
655 * Local Variables:
656 * eval: (c-set-style "gnu")
657 * End:
658 */