1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4 
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <net/if.h>
10 #include <net/if_arp.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <limits.h>
14 
15 #include <rte_ether.h>
16 
17 #include "vhost_kernel_tap.h"
18 #include "../virtio_logs.h"
19 
20 int
21 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
22 			 const char *mac)
23 {
24 	unsigned int tap_features;
25 	int sndbuf = INT_MAX;
26 	struct ifreq ifr;
27 	int tapfd;
28 	unsigned int offload =
29 			TUN_F_CSUM |
30 			TUN_F_TSO4 |
31 			TUN_F_TSO6 |
32 			TUN_F_TSO_ECN |
33 			TUN_F_UFO;
34 
35 	/* TODO:
36 	 * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
37 	 * 2. get number of memory regions from vhost module parameter
38 	 * max_mem_regions, supported in newer version linux kernel
39 	 */
40 	tapfd = open(PATH_NET_TUN, O_RDWR);
41 	if (tapfd < 0) {
42 		PMD_DRV_LOG(ERR, "fail to open %s: %s",
43 			    PATH_NET_TUN, strerror(errno));
44 		return -1;
45 	}
46 
47 	/* Construct ifr */
48 	memset(&ifr, 0, sizeof(ifr));
49 	ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
50 
51 	if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
52 		PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
53 		goto error;
54 	}
55 	if (tap_features & IFF_ONE_QUEUE)
56 		ifr.ifr_flags |= IFF_ONE_QUEUE;
57 
58 	/* Let tap instead of vhost-net handle vnet header, as the latter does
59 	 * not support offloading. And in this case, we should not set feature
60 	 * bit VHOST_NET_F_VIRTIO_NET_HDR.
61 	 */
62 	if (tap_features & IFF_VNET_HDR) {
63 		ifr.ifr_flags |= IFF_VNET_HDR;
64 	} else {
65 		PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
66 		goto error;
67 	}
68 
69 	if (req_mq)
70 		ifr.ifr_flags |= IFF_MULTI_QUEUE;
71 
72 	if (*p_ifname)
73 		strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
74 	else
75 		strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ - 1);
76 	if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
77 		PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
78 		goto error;
79 	}
80 
81 	fcntl(tapfd, F_SETFL, O_NONBLOCK);
82 
83 	if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
84 		PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
85 		goto error;
86 	}
87 
88 	if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
89 		PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
90 		goto error;
91 	}
92 
93 	/* TODO: before set the offload capabilities, we'd better (1) check
94 	 * negotiated features to see if necessary to offload; (2) query tap
95 	 * to see if it supports the offload capabilities.
96 	 */
97 	if (ioctl(tapfd, TUNSETOFFLOAD, offload) != 0)
98 		PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s",
99 			   strerror(errno));
100 
101 	memset(&ifr, 0, sizeof(ifr));
102 	ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
103 	memcpy(ifr.ifr_hwaddr.sa_data, mac, ETHER_ADDR_LEN);
104 	if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
105 		PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
106 		goto error;
107 	}
108 
109 	if (!(*p_ifname))
110 		*p_ifname = strdup(ifr.ifr_name);
111 
112 	return tapfd;
113 error:
114 	close(tapfd);
115 	return -1;
116 }
117