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