xref: /kvmtool/virtio/net.c (revision 3c8d07a7be4ac2c4b241f918eb569e6221b4f716)
1 #include "kvm/virtio-net.h"
2 #include "kvm/virtio-pci.h"
3 #include "kvm/virtio.h"
4 #include "kvm/ioport.h"
5 #include "kvm/types.h"
6 #include "kvm/mutex.h"
7 #include "kvm/util.h"
8 #include "kvm/kvm.h"
9 #include "kvm/pci.h"
10 
11 #include <linux/virtio_net.h>
12 #include <linux/if_tun.h>
13 #include <net/if.h>
14 #include <sys/ioctl.h>
15 #include <assert.h>
16 #include <fcntl.h>
17 #include <arpa/inet.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <unistd.h>
21 #include <sys/wait.h>
22 
23 #define VIRTIO_NET_IRQ		14
24 #define VIRTIO_NET_PIN		3
25 
26 #define VIRTIO_NET_QUEUE_SIZE	128
27 #define VIRTIO_NET_NUM_QUEUES	2
28 #define VIRTIO_NET_RX_QUEUE	0
29 #define VIRTIO_NET_TX_QUEUE	1
30 #define PCI_VIRTIO_NET_DEVNUM	3
31 
32 struct net_device {
33 	pthread_mutex_t			mutex;
34 
35 	struct virt_queue		vqs[VIRTIO_NET_NUM_QUEUES];
36 	struct virtio_net_config	net_config;
37 	uint32_t			host_features;
38 	uint32_t			guest_features;
39 	uint16_t			config_vector;
40 	uint8_t				status;
41 	uint16_t			queue_selector;
42 
43 	pthread_t			io_rx_thread;
44 	pthread_mutex_t			io_rx_mutex;
45 	pthread_cond_t			io_rx_cond;
46 
47 	pthread_t			io_tx_thread;
48 	pthread_mutex_t			io_tx_mutex;
49 	pthread_cond_t			io_tx_cond;
50 
51 	int				tap_fd;
52 	char				tap_name[IFNAMSIZ];
53 };
54 
55 static struct net_device net_device = {
56 	.mutex			= PTHREAD_MUTEX_INITIALIZER,
57 
58 	.net_config = {
59 		.mac		= {0x00, 0x11, 0x22, 0x33, 0x44, 0x55},
60 		.status		= VIRTIO_NET_S_LINK_UP,
61 	},
62 	.host_features		= 1UL << VIRTIO_NET_F_MAC |
63 				  1UL << VIRTIO_NET_F_CSUM |
64 				  1UL << VIRTIO_NET_F_HOST_UFO |
65 				  1UL << VIRTIO_NET_F_HOST_TSO4 |
66 				  1UL << VIRTIO_NET_F_HOST_TSO6 |
67 				  1UL << VIRTIO_NET_F_GUEST_UFO |
68 				  1UL << VIRTIO_NET_F_GUEST_TSO4 |
69 				  1UL << VIRTIO_NET_F_GUEST_TSO6,
70 };
71 
72 static void *virtio_net_rx_thread(void *p)
73 {
74 	struct iovec iov[VIRTIO_NET_QUEUE_SIZE];
75 	struct virt_queue *vq;
76 	struct kvm *self;
77 	uint16_t out, in;
78 	uint16_t head;
79 	int len;
80 
81 	self = p;
82 	vq = &net_device.vqs[VIRTIO_NET_RX_QUEUE];
83 
84 	while (1) {
85 		mutex_lock(&net_device.io_rx_mutex);
86 		if (!virt_queue__available(vq))
87 			pthread_cond_wait(&net_device.io_rx_cond, &net_device.io_rx_mutex);
88 		mutex_unlock(&net_device.io_rx_mutex);
89 
90 		while (virt_queue__available(vq)) {
91 			head = virt_queue__get_iov(vq, iov, &out, &in, self);
92 			len = readv(net_device.tap_fd, iov, in);
93 			virt_queue__set_used_elem(vq, head, len);
94 			/* We should interrupt guest right now, otherwise latency is huge. */
95 			kvm__irq_line(self, VIRTIO_NET_IRQ, 1);
96 		}
97 
98 	}
99 
100 	pthread_exit(NULL);
101 	return NULL;
102 
103 }
104 
105 static void *virtio_net_tx_thread(void *p)
106 {
107 	struct iovec iov[VIRTIO_NET_QUEUE_SIZE];
108 	struct virt_queue *vq;
109 	struct kvm *self;
110 	uint16_t out, in;
111 	uint16_t head;
112 	int len;
113 
114 	self = p;
115 	vq = &net_device.vqs[VIRTIO_NET_TX_QUEUE];
116 
117 	while (1) {
118 		mutex_lock(&net_device.io_tx_mutex);
119 		if (!virt_queue__available(vq))
120 			pthread_cond_wait(&net_device.io_tx_cond, &net_device.io_tx_mutex);
121 		mutex_unlock(&net_device.io_tx_mutex);
122 
123 		while (virt_queue__available(vq)) {
124 			head = virt_queue__get_iov(vq, iov, &out, &in, self);
125 			len = writev(net_device.tap_fd, iov, out);
126 			virt_queue__set_used_elem(vq, head, len);
127 		}
128 
129 		kvm__irq_line(self, VIRTIO_NET_IRQ, 1);
130 	}
131 
132 	pthread_exit(NULL);
133 	return NULL;
134 
135 }
136 static bool virtio_net_pci_io_device_specific_in(void *data, unsigned long offset, int size, uint32_t count)
137 {
138 	uint8_t *config_space = (uint8_t *) &net_device.net_config;
139 
140 	if (size != 1 || count != 1)
141 		return false;
142 
143 	if ((offset - VIRTIO_PCI_CONFIG_NOMSI) > sizeof(struct virtio_net_config))
144 		error("config offset is too big: %li", offset - VIRTIO_PCI_CONFIG_NOMSI);
145 
146 	ioport__write8(data, config_space[offset - VIRTIO_PCI_CONFIG_NOMSI]);
147 
148 	return true;
149 }
150 
151 static bool virtio_net_pci_io_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
152 {
153 	unsigned long offset = port - IOPORT_VIRTIO_NET;
154 	bool ret = true;
155 
156 	mutex_lock(&net_device.mutex);
157 
158 	switch (offset) {
159 	case VIRTIO_PCI_HOST_FEATURES:
160 		ioport__write32(data, net_device.host_features);
161 		break;
162 	case VIRTIO_PCI_GUEST_FEATURES:
163 		ret = false;
164 		break;
165 	case VIRTIO_PCI_QUEUE_PFN:
166 		ioport__write32(data, net_device.vqs[net_device.queue_selector].pfn);
167 		break;
168 	case VIRTIO_PCI_QUEUE_NUM:
169 		ioport__write16(data, VIRTIO_NET_QUEUE_SIZE);
170 		break;
171 	case VIRTIO_PCI_QUEUE_SEL:
172 	case VIRTIO_PCI_QUEUE_NOTIFY:
173 		ret = false;
174 		break;
175 	case VIRTIO_PCI_STATUS:
176 		ioport__write8(data, net_device.status);
177 		break;
178 	case VIRTIO_PCI_ISR:
179 		ioport__write8(data, 0x1);
180 		kvm__irq_line(self, VIRTIO_NET_IRQ, 0);
181 		break;
182 	case VIRTIO_MSI_CONFIG_VECTOR:
183 		ioport__write16(data, net_device.config_vector);
184 		break;
185 	default:
186 		ret = virtio_net_pci_io_device_specific_in(data, offset, size, count);
187 	};
188 
189 	mutex_unlock(&net_device.mutex);
190 
191 	return ret;
192 }
193 
194 static void virtio_net_handle_callback(struct kvm *self, uint16_t queue_index)
195 {
196 	if (queue_index == VIRTIO_NET_TX_QUEUE) {
197 
198 		mutex_lock(&net_device.io_tx_mutex);
199 		pthread_cond_signal(&net_device.io_tx_cond);
200 		mutex_unlock(&net_device.io_tx_mutex);
201 
202 	} else if (queue_index == VIRTIO_NET_RX_QUEUE) {
203 
204 		mutex_lock(&net_device.io_rx_mutex);
205 		pthread_cond_signal(&net_device.io_rx_cond);
206 		mutex_unlock(&net_device.io_rx_mutex);
207 
208 	}
209 }
210 
211 static bool virtio_net_pci_io_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
212 {
213 	unsigned long offset = port - IOPORT_VIRTIO_NET;
214 	bool ret = true;
215 
216 	mutex_lock(&net_device.mutex);
217 
218 	switch (offset) {
219 	case VIRTIO_PCI_GUEST_FEATURES:
220 		net_device.guest_features	= ioport__read32(data);
221 		break;
222 	case VIRTIO_PCI_QUEUE_PFN: {
223 		struct virt_queue *queue;
224 		void *p;
225 
226 		assert(net_device.queue_selector < VIRTIO_NET_NUM_QUEUES);
227 
228 		queue		= &net_device.vqs[net_device.queue_selector];
229 		queue->pfn	= ioport__read32(data);
230 		p		= guest_flat_to_host(self, queue->pfn << 12);
231 
232 		vring_init(&queue->vring, VIRTIO_NET_QUEUE_SIZE, p, 4096);
233 
234 		break;
235 	}
236 	case VIRTIO_PCI_QUEUE_SEL:
237 		net_device.queue_selector	= ioport__read16(data);
238 		break;
239 	case VIRTIO_PCI_QUEUE_NOTIFY: {
240 		uint16_t queue_index;
241 		queue_index	= ioport__read16(data);
242 		virtio_net_handle_callback(self, queue_index);
243 		break;
244 	}
245 	case VIRTIO_PCI_STATUS:
246 		net_device.status		= ioport__read8(data);
247 		break;
248 	case VIRTIO_MSI_CONFIG_VECTOR:
249 		net_device.config_vector	= VIRTIO_MSI_NO_VECTOR;
250 		break;
251 	case VIRTIO_MSI_QUEUE_VECTOR:
252 		break;
253 	default:
254 		ret = false;
255 	};
256 
257 	mutex_unlock(&net_device.mutex);
258 	return ret;
259 }
260 
261 static struct ioport_operations virtio_net_io_ops = {
262 	.io_in	= virtio_net_pci_io_in,
263 	.io_out	= virtio_net_pci_io_out,
264 };
265 
266 #define PCI_VENDOR_ID_REDHAT_QUMRANET		0x1af4
267 #define PCI_DEVICE_ID_VIRTIO_NET		0x1000
268 #define PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET	0x1af4
269 #define PCI_SUBSYSTEM_ID_VIRTIO_NET		0x0001
270 
271 static struct pci_device_header virtio_net_pci_device = {
272 	.vendor_id		= PCI_VENDOR_ID_REDHAT_QUMRANET,
273 	.device_id		= PCI_DEVICE_ID_VIRTIO_NET,
274 	.header_type		= PCI_HEADER_TYPE_NORMAL,
275 	.revision_id		= 0,
276 	.class			= 0x020000,
277 	.subsys_vendor_id	= PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET,
278 	.subsys_id		= PCI_SUBSYSTEM_ID_VIRTIO_NET,
279 	.bar[0]			= IOPORT_VIRTIO_NET | PCI_BASE_ADDRESS_SPACE_IO,
280 	.irq_pin		= VIRTIO_NET_PIN,
281 	.irq_line		= VIRTIO_NET_IRQ,
282 };
283 
284 static bool virtio_net__tap_init(const struct virtio_net_parameters *params)
285 {
286 	int sock = socket(AF_INET, SOCK_STREAM, 0);
287 	int i, pid, status, offload, hdr_len;
288 	struct sockaddr_in sin = {0};
289 	struct ifreq ifr;
290 
291 	for (i = 0 ; i < 6 ; i++)
292 		net_device.net_config.mac[i] = params->guest_mac[i];
293 
294 	net_device.tap_fd = open("/dev/net/tun", O_RDWR);
295 	if (net_device.tap_fd < 0) {
296 		warning("Unable to open /dev/net/tun");
297 		goto fail;
298 	}
299 
300 	memset(&ifr, 0, sizeof(ifr));
301 	ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
302 	if (ioctl(net_device.tap_fd, TUNSETIFF, &ifr) < 0) {
303 		warning("Config tap device error. Are you root?");
304 		goto fail;
305 	}
306 
307 	strncpy(net_device.tap_name, ifr.ifr_name, sizeof(net_device.tap_name));
308 
309 	if (ioctl(net_device.tap_fd, TUNSETNOCSUM, 1) < 0) {
310 		warning("Config tap device TUNSETNOCSUM error");
311 		goto fail;
312 	}
313 
314 	hdr_len = sizeof(struct virtio_net_hdr);
315 	if (ioctl(net_device.tap_fd, TUNSETVNETHDRSZ, &hdr_len) < 0) {
316 		warning("Config tap device TUNSETVNETHDRSZ error");
317 		goto fail;
318 	}
319 
320 	offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | TUN_F_UFO;
321 	if (ioctl(net_device.tap_fd, TUNSETOFFLOAD, offload) < 0) {
322 		warning("Config tap device TUNSETOFFLOAD error");
323 		goto fail;
324 	}
325 
326 	if (strcmp(params->script, "none")) {
327 		pid = fork();
328 		if (pid == 0) {
329 			execl(params->script, params->script, net_device.tap_name, NULL);
330 			_exit(1);
331 		} else {
332 			waitpid(pid, &status, 0);
333 			if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
334 				warning("Fail to setup tap by %s", params->script);
335 				goto fail;
336 			}
337 		}
338 	} else {
339 		memset(&ifr, 0, sizeof(ifr));
340 		strncpy(ifr.ifr_name, net_device.tap_name, sizeof(net_device.tap_name));
341 		sin.sin_addr.s_addr = inet_addr(params->host_ip);
342 		memcpy(&(ifr.ifr_addr), &sin, sizeof(ifr.ifr_addr));
343 		ifr.ifr_addr.sa_family = AF_INET;
344 		if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
345 			warning("Could not set ip address on tap device");
346 			goto fail;
347 		}
348 	}
349 
350 	memset(&ifr, 0, sizeof(ifr));
351 	strncpy(ifr.ifr_name, net_device.tap_name, sizeof(net_device.tap_name));
352 	ioctl(sock, SIOCGIFFLAGS, &ifr);
353 	ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
354 	if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0)
355 		warning("Could not bring tap device up");
356 
357 	close(sock);
358 
359 	return 1;
360 
361 fail:
362 	if (sock >= 0)
363 		close(sock);
364 	if (net_device.tap_fd >= 0)
365 		close(net_device.tap_fd);
366 
367 	return 0;
368 }
369 
370 static void virtio_net__io_thread_init(struct kvm *self)
371 {
372 	pthread_mutex_init(&net_device.io_rx_mutex, NULL);
373 	pthread_cond_init(&net_device.io_tx_cond, NULL);
374 
375 	pthread_mutex_init(&net_device.io_rx_mutex, NULL);
376 	pthread_cond_init(&net_device.io_tx_cond, NULL);
377 
378 	pthread_create(&net_device.io_rx_thread, NULL, virtio_net_rx_thread, (void *)self);
379 	pthread_create(&net_device.io_tx_thread, NULL, virtio_net_tx_thread, (void *)self);
380 }
381 
382 void virtio_net__init(const struct virtio_net_parameters *params)
383 {
384 	if (virtio_net__tap_init(params)) {
385 		pci__register(&virtio_net_pci_device, PCI_VIRTIO_NET_DEVNUM);
386 		ioport__register(IOPORT_VIRTIO_NET, &virtio_net_io_ops, IOPORT_VIRTIO_NET_SIZE);
387 
388 		virtio_net__io_thread_init(params->self);
389 	}
390 }
391