xref: /kvmtool/virtio/net.c (revision f715177d99aa34758585f1c62b4cbaf131e12959)
1 #include "kvm/virtio-pci-dev.h"
2 #include "kvm/virtio-net.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 #include "kvm/irq.h"
11 #include "kvm/uip.h"
12 #include "kvm/ioeventfd.h"
13 
14 #include <linux/virtio_net.h>
15 #include <linux/if_tun.h>
16 
17 #include <arpa/inet.h>
18 #include <net/if.h>
19 
20 #include <unistd.h>
21 #include <assert.h>
22 #include <fcntl.h>
23 
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 
29 #define VIRTIO_NET_QUEUE_SIZE		128
30 #define VIRTIO_NET_NUM_QUEUES		2
31 #define VIRTIO_NET_RX_QUEUE		0
32 #define VIRTIO_NET_TX_QUEUE		1
33 
34 static struct pci_device_header pci_header = {
35 	.vendor_id			= PCI_VENDOR_ID_REDHAT_QUMRANET,
36 	.device_id			= PCI_DEVICE_ID_VIRTIO_NET,
37 	.header_type			= PCI_HEADER_TYPE_NORMAL,
38 	.revision_id			= 0,
39 	.class				= 0x020000,
40 	.subsys_vendor_id		= PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET,
41 	.subsys_id			= VIRTIO_ID_NET,
42 };
43 
44 struct net_dev;
45 
46 struct net_dev_operations {
47 	int (*rx)(struct iovec *iov, u16 in, struct net_dev *ndev);
48 	int (*tx)(struct iovec *iov, u16 in, struct net_dev *ndev);
49 };
50 
51 struct net_dev {
52 	pthread_mutex_t			mutex;
53 
54 	struct virt_queue		vqs[VIRTIO_NET_NUM_QUEUES];
55 	struct virtio_net_config	config;
56 	u32				host_features;
57 	u32				guest_features;
58 	u16				config_vector;
59 	u8				status;
60 	u8				isr;
61 	u16				queue_selector;
62 	u16				base_addr;
63 
64 	pthread_t			io_rx_thread;
65 	pthread_mutex_t			io_rx_lock;
66 	pthread_cond_t			io_rx_cond;
67 
68 	pthread_t			io_tx_thread;
69 	pthread_mutex_t			io_tx_lock;
70 	pthread_cond_t			io_tx_cond;
71 
72 	int				tap_fd;
73 	char				tap_name[IFNAMSIZ];
74 
75 	int				mode;
76 
77 	struct uip_info			info;
78 	struct net_dev_operations	*ops;
79 };
80 
81 static struct net_dev ndev = {
82 	.mutex	= PTHREAD_MUTEX_INITIALIZER,
83 
84 	.config = {
85 		.status			= VIRTIO_NET_S_LINK_UP,
86 	},
87 	.host_features			= 1UL << VIRTIO_NET_F_MAC
88 					| 1UL << VIRTIO_NET_F_CSUM
89 					| 1UL << VIRTIO_NET_F_HOST_UFO
90 					| 1UL << VIRTIO_NET_F_HOST_TSO4
91 					| 1UL << VIRTIO_NET_F_HOST_TSO6
92 					| 1UL << VIRTIO_NET_F_GUEST_UFO
93 					| 1UL << VIRTIO_NET_F_GUEST_TSO4
94 					| 1UL << VIRTIO_NET_F_GUEST_TSO6,
95 	.info = {
96 		.host_mac.addr		= {0x00, 0x01, 0x01, 0x01, 0x01, 0x01},
97 		.guest_mac.addr		= {0x00, 0x15, 0x15, 0x15, 0x15, 0x15},
98 		.host_ip		= 0xc0a82101,
99 		.buf_nr			= 20,
100 	}
101 };
102 
103 static void *virtio_net_rx_thread(void *p)
104 {
105 	struct iovec iov[VIRTIO_NET_QUEUE_SIZE];
106 	struct virt_queue *vq;
107 	struct kvm *kvm;
108 	u16 out, in;
109 	u16 head;
110 	int len;
111 
112 	kvm	= p;
113 	vq	= &ndev.vqs[VIRTIO_NET_RX_QUEUE];
114 
115 	while (1) {
116 
117 		mutex_lock(&ndev.io_rx_lock);
118 		if (!virt_queue__available(vq))
119 			pthread_cond_wait(&ndev.io_rx_cond, &ndev.io_rx_lock);
120 		mutex_unlock(&ndev.io_rx_lock);
121 
122 		while (virt_queue__available(vq)) {
123 
124 			head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
125 
126 			len = ndev.ops->rx(iov, in, &ndev);
127 
128 			virt_queue__set_used_elem(vq, head, len);
129 
130 			/* We should interrupt guest right now, otherwise latency is huge. */
131 			virt_queue__trigger_irq(vq, pci_header.irq_line, &ndev.isr, kvm);
132 		}
133 
134 	}
135 
136 	pthread_exit(NULL);
137 	return NULL;
138 
139 }
140 
141 static void *virtio_net_tx_thread(void *p)
142 {
143 	struct iovec iov[VIRTIO_NET_QUEUE_SIZE];
144 	struct virt_queue *vq;
145 	struct kvm *kvm;
146 	u16 out, in;
147 	u16 head;
148 	int len;
149 
150 	kvm	= p;
151 	vq	= &ndev.vqs[VIRTIO_NET_TX_QUEUE];
152 
153 	while (1) {
154 		mutex_lock(&ndev.io_tx_lock);
155 		if (!virt_queue__available(vq))
156 			pthread_cond_wait(&ndev.io_tx_cond, &ndev.io_tx_lock);
157 		mutex_unlock(&ndev.io_tx_lock);
158 
159 		while (virt_queue__available(vq)) {
160 
161 			head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
162 
163 			len = ndev.ops->tx(iov, out, &ndev);
164 
165 			virt_queue__set_used_elem(vq, head, len);
166 		}
167 
168 		virt_queue__trigger_irq(vq, pci_header.irq_line, &ndev.isr, kvm);
169 
170 	}
171 
172 	pthread_exit(NULL);
173 
174 	return NULL;
175 
176 }
177 
178 static bool virtio_net_pci_io_device_specific_in(void *data, unsigned long offset, int size, u32 count)
179 {
180 	u8 *config_space = (u8 *)&ndev.config;
181 
182 	if (size != 1 || count != 1)
183 		return false;
184 
185 	if ((offset - VIRTIO_MSI_CONFIG_VECTOR) > sizeof(struct virtio_net_config))
186 		pr_error("config offset is too big: %li", offset - VIRTIO_MSI_CONFIG_VECTOR);
187 
188 	ioport__write8(data, config_space[offset - VIRTIO_MSI_CONFIG_VECTOR]);
189 
190 	return true;
191 }
192 
193 static bool virtio_net_pci_io_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count)
194 {
195 	unsigned long	offset	= port - ndev.base_addr;
196 	bool		ret	= true;
197 
198 	mutex_lock(&ndev.mutex);
199 
200 	switch (offset) {
201 	case VIRTIO_PCI_HOST_FEATURES:
202 		ioport__write32(data, ndev.host_features);
203 		break;
204 	case VIRTIO_PCI_GUEST_FEATURES:
205 		ret = false;
206 		break;
207 	case VIRTIO_PCI_QUEUE_PFN:
208 		ioport__write32(data, ndev.vqs[ndev.queue_selector].pfn);
209 		break;
210 	case VIRTIO_PCI_QUEUE_NUM:
211 		ioport__write16(data, VIRTIO_NET_QUEUE_SIZE);
212 		break;
213 	case VIRTIO_PCI_QUEUE_SEL:
214 	case VIRTIO_PCI_QUEUE_NOTIFY:
215 		ret = false;
216 		break;
217 	case VIRTIO_PCI_STATUS:
218 		ioport__write8(data, ndev.status);
219 		break;
220 	case VIRTIO_PCI_ISR:
221 		ioport__write8(data, ndev.isr);
222 		kvm__irq_line(kvm, pci_header.irq_line, VIRTIO_IRQ_LOW);
223 		ndev.isr = VIRTIO_IRQ_LOW;
224 		break;
225 	case VIRTIO_MSI_CONFIG_VECTOR:
226 		ioport__write16(data, ndev.config_vector);
227 		break;
228 	default:
229 		ret = virtio_net_pci_io_device_specific_in(data, offset, size, count);
230 	};
231 
232 	mutex_unlock(&ndev.mutex);
233 
234 	return ret;
235 }
236 
237 static void virtio_net_handle_callback(struct kvm *kvm, u16 queue_index)
238 {
239 	switch (queue_index) {
240 	case VIRTIO_NET_TX_QUEUE:
241 		mutex_lock(&ndev.io_tx_lock);
242 		pthread_cond_signal(&ndev.io_tx_cond);
243 		mutex_unlock(&ndev.io_tx_lock);
244 		break;
245 	case VIRTIO_NET_RX_QUEUE:
246 		mutex_lock(&ndev.io_rx_lock);
247 		pthread_cond_signal(&ndev.io_rx_cond);
248 		mutex_unlock(&ndev.io_rx_lock);
249 		break;
250 	default:
251 		pr_warning("Unknown queue index %u", queue_index);
252 	}
253 }
254 
255 static bool virtio_net_pci_io_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count)
256 {
257 	unsigned long	offset		= port - ndev.base_addr;
258 	bool		ret		= true;
259 
260 	mutex_lock(&ndev.mutex);
261 
262 	switch (offset) {
263 	case VIRTIO_PCI_GUEST_FEATURES:
264 		ndev.guest_features	= ioport__read32(data);
265 		break;
266 	case VIRTIO_PCI_QUEUE_PFN: {
267 		struct virt_queue *queue;
268 		void *p;
269 
270 		assert(ndev.queue_selector < VIRTIO_NET_NUM_QUEUES);
271 
272 		queue			= &ndev.vqs[ndev.queue_selector];
273 		queue->pfn		= ioport__read32(data);
274 		p			= guest_pfn_to_host(kvm, queue->pfn);
275 
276 		vring_init(&queue->vring, VIRTIO_NET_QUEUE_SIZE, p, VIRTIO_PCI_VRING_ALIGN);
277 
278 		break;
279 	}
280 	case VIRTIO_PCI_QUEUE_SEL:
281 		ndev.queue_selector	= ioport__read16(data);
282 		break;
283 	case VIRTIO_PCI_QUEUE_NOTIFY: {
284 		u16 queue_index;
285 
286 		queue_index		= ioport__read16(data);
287 		virtio_net_handle_callback(kvm, queue_index);
288 		break;
289 	}
290 	case VIRTIO_PCI_STATUS:
291 		ndev.status		= ioport__read8(data);
292 		break;
293 	case VIRTIO_MSI_CONFIG_VECTOR:
294 		ndev.config_vector	= VIRTIO_MSI_NO_VECTOR;
295 		break;
296 	case VIRTIO_MSI_QUEUE_VECTOR:
297 		break;
298 	default:
299 		ret			= false;
300 	};
301 
302 	mutex_unlock(&ndev.mutex);
303 
304 	return ret;
305 }
306 
307 static void ioevent_callback(struct kvm *kvm, void *param)
308 {
309 	virtio_net_handle_callback(kvm, (u64)(long)param);
310 }
311 
312 static struct ioport_operations virtio_net_io_ops = {
313 	.io_in	= virtio_net_pci_io_in,
314 	.io_out	= virtio_net_pci_io_out,
315 };
316 
317 static bool virtio_net__tap_init(const struct virtio_net_parameters *params)
318 {
319 	int sock = socket(AF_INET, SOCK_STREAM, 0);
320 	int pid, status, offload, hdr_len;
321 	struct sockaddr_in sin = {0};
322 	struct ifreq ifr;
323 
324 	ndev.tap_fd = open("/dev/net/tun", O_RDWR);
325 	if (ndev.tap_fd < 0) {
326 		pr_warning("Unable to open /dev/net/tun");
327 		goto fail;
328 	}
329 
330 	memset(&ifr, 0, sizeof(ifr));
331 	ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
332 	if (ioctl(ndev.tap_fd, TUNSETIFF, &ifr) < 0) {
333 		pr_warning("Config tap device error. Are you root?");
334 		goto fail;
335 	}
336 
337 	strncpy(ndev.tap_name, ifr.ifr_name, sizeof(ndev.tap_name));
338 
339 	if (ioctl(ndev.tap_fd, TUNSETNOCSUM, 1) < 0) {
340 		pr_warning("Config tap device TUNSETNOCSUM error");
341 		goto fail;
342 	}
343 
344 	hdr_len = sizeof(struct virtio_net_hdr);
345 	if (ioctl(ndev.tap_fd, TUNSETVNETHDRSZ, &hdr_len) < 0) {
346 		pr_warning("Config tap device TUNSETVNETHDRSZ error");
347 	}
348 
349 	offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | TUN_F_UFO;
350 	if (ioctl(ndev.tap_fd, TUNSETOFFLOAD, offload) < 0) {
351 		pr_warning("Config tap device TUNSETOFFLOAD error");
352 		goto fail;
353 	}
354 
355 	if (strcmp(params->script, "none")) {
356 		pid = fork();
357 		if (pid == 0) {
358 			execl(params->script, params->script, ndev.tap_name, NULL);
359 			_exit(1);
360 		} else {
361 			waitpid(pid, &status, 0);
362 			if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
363 				pr_warning("Fail to setup tap by %s", params->script);
364 				goto fail;
365 			}
366 		}
367 	} else {
368 		memset(&ifr, 0, sizeof(ifr));
369 		strncpy(ifr.ifr_name, ndev.tap_name, sizeof(ndev.tap_name));
370 		sin.sin_addr.s_addr = inet_addr(params->host_ip);
371 		memcpy(&(ifr.ifr_addr), &sin, sizeof(ifr.ifr_addr));
372 		ifr.ifr_addr.sa_family = AF_INET;
373 		if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
374 			pr_warning("Could not set ip address on tap device");
375 			goto fail;
376 		}
377 	}
378 
379 	memset(&ifr, 0, sizeof(ifr));
380 	strncpy(ifr.ifr_name, ndev.tap_name, sizeof(ndev.tap_name));
381 	ioctl(sock, SIOCGIFFLAGS, &ifr);
382 	ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
383 	if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0)
384 		pr_warning("Could not bring tap device up");
385 
386 	close(sock);
387 
388 	return 1;
389 
390 fail:
391 	if (sock >= 0)
392 		close(sock);
393 	if (ndev.tap_fd >= 0)
394 		close(ndev.tap_fd);
395 
396 	return 0;
397 }
398 
399 static void virtio_net__io_thread_init(struct kvm *kvm)
400 {
401 	pthread_mutex_init(&ndev.io_rx_lock, NULL);
402 	pthread_cond_init(&ndev.io_tx_cond, NULL);
403 
404 	pthread_mutex_init(&ndev.io_rx_lock, NULL);
405 	pthread_cond_init(&ndev.io_tx_cond, NULL);
406 
407 	pthread_create(&ndev.io_rx_thread, NULL, virtio_net_rx_thread, (void *)kvm);
408 	pthread_create(&ndev.io_tx_thread, NULL, virtio_net_tx_thread, (void *)kvm);
409 }
410 
411 static inline int tap_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev)
412 {
413 	return writev(ndev->tap_fd, iov, out);
414 }
415 
416 static inline int tap_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev)
417 {
418 	return readv(ndev->tap_fd, iov, in);
419 }
420 
421 static inline int uip_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev)
422 {
423 	return uip_tx(iov, out, &ndev->info);
424 }
425 
426 static inline int uip_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev)
427 {
428 	return uip_rx(iov, in, &ndev->info);
429 }
430 
431 static struct net_dev_operations tap_ops = {
432 	.rx	= tap_ops_rx,
433 	.tx	= tap_ops_tx,
434 };
435 
436 static struct net_dev_operations uip_ops = {
437 	.rx	= uip_ops_rx,
438 	.tx	= uip_ops_tx,
439 };
440 
441 void virtio_net__init(const struct virtio_net_parameters *params)
442 {
443 	struct ioevent ioevent;
444 	u8 dev, line, pin;
445 	u16 net_base_addr;
446 	int i;
447 
448 	if (irq__register_device(VIRTIO_ID_NET, &dev, &pin, &line) < 0)
449 		return;
450 
451 	pci_header.irq_pin  = pin;
452 	pci_header.irq_line = line;
453 	net_base_addr	    = ioport__register(IOPORT_EMPTY, &virtio_net_io_ops, IOPORT_SIZE, NULL);
454 	pci_header.bar[0]   = net_base_addr | PCI_BASE_ADDRESS_SPACE_IO;
455 	ndev.base_addr	    = net_base_addr;
456 	pci__register(&pci_header, dev);
457 
458 	for (i = 0 ; i < 6 ; i++)
459 		ndev.config.mac[i] = params->guest_mac[i];
460 
461 	ndev.mode = params->mode;
462 	if (ndev.mode == NET_MODE_TAP) {
463 		virtio_net__tap_init(params);
464 		ndev.ops = &tap_ops;
465 	} else {
466 		uip_init(&ndev.info);
467 		ndev.ops = &uip_ops;
468 	}
469 
470 	virtio_net__io_thread_init(params->kvm);
471 
472 	for (i = 0; i < VIRTIO_NET_NUM_QUEUES; i++) {
473 		ioevent = (struct ioevent) {
474 			.io_addr	= net_base_addr + VIRTIO_PCI_QUEUE_NOTIFY,
475 			.io_len		= sizeof(u16),
476 			.fn		= ioevent_callback,
477 			.datamatch	= i,
478 			.fn_ptr		= (void *)(long)i,
479 			.fn_kvm		= params->kvm,
480 			.fd		= eventfd(0, 0),
481 		};
482 
483 		ioeventfd__add_event(&ioevent);
484 	}
485 }
486