xref: /kvmtool/virtio/net.c (revision 6d6cc14b76d4ee88c823a6cfd75ff0d872ea1742)
1 #include "kvm/virtio-pci-dev.h"
2 #include "kvm/virtio-net.h"
3 #include "kvm/virtio.h"
4 #include "kvm/types.h"
5 #include "kvm/mutex.h"
6 #include "kvm/util.h"
7 #include "kvm/kvm.h"
8 #include "kvm/irq.h"
9 #include "kvm/uip.h"
10 #include "kvm/guest_compat.h"
11 #include "kvm/iovec.h"
12 
13 #include <linux/vhost.h>
14 #include <linux/virtio_net.h>
15 #include <linux/if_tun.h>
16 #include <linux/types.h>
17 
18 #include <arpa/inet.h>
19 #include <net/if.h>
20 
21 #include <unistd.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 #include <sys/eventfd.h>
29 
30 #define VIRTIO_NET_QUEUE_SIZE		256
31 #define VIRTIO_NET_NUM_QUEUES		8
32 
33 struct net_dev;
34 
35 struct net_dev_operations {
36 	int (*rx)(struct iovec *iov, u16 in, struct net_dev *ndev);
37 	int (*tx)(struct iovec *iov, u16 in, struct net_dev *ndev);
38 };
39 
40 struct net_dev {
41 	struct mutex			mutex;
42 	struct virtio_device		vdev;
43 	struct list_head		list;
44 
45 	struct virt_queue		vqs[VIRTIO_NET_NUM_QUEUES * 2 + 1];
46 	struct virtio_net_config	config;
47 	u32				features, rx_vqs, tx_vqs, queue_pairs;
48 
49 	pthread_t			io_thread[VIRTIO_NET_NUM_QUEUES * 2 + 1];
50 	struct mutex			io_lock[VIRTIO_NET_NUM_QUEUES * 2 + 1];
51 	pthread_cond_t			io_cond[VIRTIO_NET_NUM_QUEUES * 2 + 1];
52 
53 	int				vhost_fd;
54 	int				tap_fd;
55 	char				tap_name[IFNAMSIZ];
56 
57 	int				mode;
58 
59 	struct uip_info			info;
60 	struct net_dev_operations	*ops;
61 	struct kvm			*kvm;
62 
63 	struct virtio_net_params	*params;
64 };
65 
66 static LIST_HEAD(ndevs);
67 static int compat_id = -1;
68 
69 #define MAX_PACKET_SIZE 65550
70 
71 static bool has_virtio_feature(struct net_dev *ndev, u32 feature)
72 {
73 	return ndev->features & (1 << feature);
74 }
75 
76 static void *virtio_net_rx_thread(void *p)
77 {
78 	struct iovec iov[VIRTIO_NET_QUEUE_SIZE];
79 	struct virt_queue *vq;
80 	struct kvm *kvm;
81 	struct net_dev *ndev = p;
82 	u16 out, in;
83 	u16 head;
84 	int len, copied;
85 	u32 id;
86 
87 	mutex_lock(&ndev->mutex);
88 	id = ndev->rx_vqs++ * 2;
89 	mutex_unlock(&ndev->mutex);
90 
91 	kvm__set_thread_name("virtio-net-rx");
92 
93 	kvm = ndev->kvm;
94 	vq = &ndev->vqs[id];
95 
96 	while (1) {
97 		mutex_lock(&ndev->io_lock[id]);
98 		if (!virt_queue__available(vq))
99 			pthread_cond_wait(&ndev->io_cond[id], &ndev->io_lock[id].mutex);
100 		mutex_unlock(&ndev->io_lock[id]);
101 
102 		while (virt_queue__available(vq)) {
103 			unsigned char buffer[MAX_PACKET_SIZE + sizeof(struct virtio_net_hdr_mrg_rxbuf)];
104 			struct iovec dummy_iov = {
105 				.iov_base = buffer,
106 				.iov_len  = sizeof(buffer),
107 			};
108 			struct virtio_net_hdr_mrg_rxbuf *hdr;
109 
110 			len = ndev->ops->rx(&dummy_iov, 1, ndev);
111 			if (len < 0) {
112 				pr_warning("%s: rx on vq %u failed (%d), exiting thread\n",
113 						__func__, id, len);
114 				goto out_err;
115 			}
116 
117 			copied = 0;
118 			head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
119 			hdr = (void *)iov[0].iov_base;
120 			while (copied < len) {
121 				size_t iovsize = min_t(size_t, len - copied, iov_size(iov, in));
122 
123 				memcpy_toiovec(iov, buffer + copied, iovsize);
124 				copied += iovsize;
125 				if (has_virtio_feature(ndev, VIRTIO_NET_F_MRG_RXBUF))
126 					hdr->num_buffers++;
127 				virt_queue__set_used_elem(vq, head, iovsize);
128 				if (copied == len)
129 					break;
130 				while (!virt_queue__available(vq))
131 					sleep(0);
132 				head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
133 			}
134 			/* We should interrupt guest right now, otherwise latency is huge. */
135 			if (virtio_queue__should_signal(vq))
136 				ndev->vdev.ops->signal_vq(kvm, &ndev->vdev, id);
137 		}
138 	}
139 
140 out_err:
141 	pthread_exit(NULL);
142 	return NULL;
143 
144 }
145 
146 static void *virtio_net_tx_thread(void *p)
147 {
148 	struct iovec iov[VIRTIO_NET_QUEUE_SIZE];
149 	struct virt_queue *vq;
150 	struct kvm *kvm;
151 	struct net_dev *ndev = p;
152 	u16 out, in;
153 	u16 head;
154 	int len;
155 	u32 id;
156 
157 	mutex_lock(&ndev->mutex);
158 	id = ndev->tx_vqs++ * 2 + 1;
159 	mutex_unlock(&ndev->mutex);
160 
161 	kvm__set_thread_name("virtio-net-tx");
162 
163 	kvm = ndev->kvm;
164 	vq = &ndev->vqs[id];
165 
166 	while (1) {
167 		mutex_lock(&ndev->io_lock[id]);
168 		if (!virt_queue__available(vq))
169 			pthread_cond_wait(&ndev->io_cond[id], &ndev->io_lock[id].mutex);
170 		mutex_unlock(&ndev->io_lock[id]);
171 
172 		while (virt_queue__available(vq)) {
173 			head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
174 			len = ndev->ops->tx(iov, out, ndev);
175 			if (len < 0) {
176 				pr_warning("%s: tx on vq %u failed (%d)\n",
177 						__func__, id, len);
178 				goto out_err;
179 			}
180 
181 			virt_queue__set_used_elem(vq, head, len);
182 		}
183 
184 		if (virtio_queue__should_signal(vq))
185 			ndev->vdev.ops->signal_vq(kvm, &ndev->vdev, id);
186 	}
187 
188 out_err:
189 	pthread_exit(NULL);
190 	return NULL;
191 }
192 
193 static virtio_net_ctrl_ack virtio_net_handle_mq(struct kvm* kvm, struct net_dev *ndev, struct virtio_net_ctrl_hdr *ctrl)
194 {
195 	/* Not much to do here */
196 	return VIRTIO_NET_OK;
197 }
198 
199 static void *virtio_net_ctrl_thread(void *p)
200 {
201 	struct iovec iov[VIRTIO_NET_QUEUE_SIZE];
202 	u16 out, in, head;
203 	struct net_dev *ndev = p;
204 	struct kvm *kvm = ndev->kvm;
205 	u32 id = ndev->queue_pairs * 2;
206 	struct virt_queue *vq = &ndev->vqs[id];
207 	struct virtio_net_ctrl_hdr *ctrl;
208 	virtio_net_ctrl_ack *ack;
209 
210 	while (1) {
211 		mutex_lock(&ndev->io_lock[id]);
212 		if (!virt_queue__available(vq))
213 			pthread_cond_wait(&ndev->io_cond[id], &ndev->io_lock[id].mutex);
214 		mutex_unlock(&ndev->io_lock[id]);
215 
216 		while (virt_queue__available(vq)) {
217 			head = virt_queue__get_iov(&ndev->vqs[id], iov, &out, &in, kvm);
218 			ctrl = iov[0].iov_base;
219 			ack = iov[out].iov_base;
220 
221 			switch (ctrl->class) {
222 			case VIRTIO_NET_CTRL_MQ:
223 				*ack = virtio_net_handle_mq(kvm, ndev, ctrl);
224 				break;
225 			default:
226 				*ack = VIRTIO_NET_ERR;
227 				break;
228 			}
229 			virt_queue__set_used_elem(&ndev->vqs[id], head, iov[out].iov_len);
230 		}
231 
232 		if (virtio_queue__should_signal(&ndev->vqs[id]))
233 			ndev->vdev.ops->signal_vq(kvm, &ndev->vdev, id);
234 	}
235 
236 	pthread_exit(NULL);
237 
238 	return NULL;
239 }
240 
241 static void virtio_net_handle_callback(struct kvm *kvm, struct net_dev *ndev, int queue)
242 {
243 	if ((u32)queue >= (ndev->queue_pairs * 2 + 1)) {
244 		pr_warning("Unknown queue index %u", queue);
245 		return;
246 	}
247 
248 	mutex_lock(&ndev->io_lock[queue]);
249 	pthread_cond_signal(&ndev->io_cond[queue]);
250 	mutex_unlock(&ndev->io_lock[queue]);
251 }
252 
253 static bool virtio_net__tap_init(struct net_dev *ndev)
254 {
255 	int sock = socket(AF_INET, SOCK_STREAM, 0);
256 	int pid, status, offload, hdr_len;
257 	struct sockaddr_in sin = {0};
258 	struct ifreq ifr;
259 	const struct virtio_net_params *params = ndev->params;
260 
261 	/* Did the user already gave us the FD? */
262 	if (params->fd) {
263 		ndev->tap_fd = params->fd;
264 		return 1;
265 	}
266 
267 	ndev->tap_fd = open("/dev/net/tun", O_RDWR);
268 	if (ndev->tap_fd < 0) {
269 		pr_warning("Unable to open /dev/net/tun");
270 		goto fail;
271 	}
272 
273 	memset(&ifr, 0, sizeof(ifr));
274 	ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
275 	if (ioctl(ndev->tap_fd, TUNSETIFF, &ifr) < 0) {
276 		pr_warning("Config tap device error. Are you root?");
277 		goto fail;
278 	}
279 
280 	strncpy(ndev->tap_name, ifr.ifr_name, sizeof(ndev->tap_name));
281 
282 	if (ioctl(ndev->tap_fd, TUNSETNOCSUM, 1) < 0) {
283 		pr_warning("Config tap device TUNSETNOCSUM error");
284 		goto fail;
285 	}
286 
287 	hdr_len = has_virtio_feature(ndev, VIRTIO_NET_F_MRG_RXBUF) ?
288 			sizeof(struct virtio_net_hdr_mrg_rxbuf) :
289 			sizeof(struct virtio_net_hdr);
290 	if (ioctl(ndev->tap_fd, TUNSETVNETHDRSZ, &hdr_len) < 0)
291 		pr_warning("Config tap device TUNSETVNETHDRSZ error");
292 
293 	offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | TUN_F_UFO;
294 	if (ioctl(ndev->tap_fd, TUNSETOFFLOAD, offload) < 0) {
295 		pr_warning("Config tap device TUNSETOFFLOAD error");
296 		goto fail;
297 	}
298 
299 	if (strcmp(params->script, "none")) {
300 		pid = fork();
301 		if (pid == 0) {
302 			execl(params->script, params->script, ndev->tap_name, NULL);
303 			_exit(1);
304 		} else {
305 			waitpid(pid, &status, 0);
306 			if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
307 				pr_warning("Fail to setup tap by %s", params->script);
308 				goto fail;
309 			}
310 		}
311 	} else {
312 		memset(&ifr, 0, sizeof(ifr));
313 		strncpy(ifr.ifr_name, ndev->tap_name, sizeof(ndev->tap_name));
314 		sin.sin_addr.s_addr = inet_addr(params->host_ip);
315 		memcpy(&(ifr.ifr_addr), &sin, sizeof(ifr.ifr_addr));
316 		ifr.ifr_addr.sa_family = AF_INET;
317 		if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
318 			pr_warning("Could not set ip address on tap device");
319 			goto fail;
320 		}
321 	}
322 
323 	memset(&ifr, 0, sizeof(ifr));
324 	strncpy(ifr.ifr_name, ndev->tap_name, sizeof(ndev->tap_name));
325 	ioctl(sock, SIOCGIFFLAGS, &ifr);
326 	ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
327 	if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0)
328 		pr_warning("Could not bring tap device up");
329 
330 	close(sock);
331 
332 	return 1;
333 
334 fail:
335 	if (sock >= 0)
336 		close(sock);
337 	if (ndev->tap_fd >= 0)
338 		close(ndev->tap_fd);
339 
340 	return 0;
341 }
342 
343 static inline int tap_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev)
344 {
345 	return writev(ndev->tap_fd, iov, out);
346 }
347 
348 static inline int tap_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev)
349 {
350 	return readv(ndev->tap_fd, iov, in);
351 }
352 
353 static inline int uip_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev)
354 {
355 	return uip_tx(iov, out, &ndev->info);
356 }
357 
358 static inline int uip_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev)
359 {
360 	return uip_rx(iov, in, &ndev->info);
361 }
362 
363 static struct net_dev_operations tap_ops = {
364 	.rx	= tap_ops_rx,
365 	.tx	= tap_ops_tx,
366 };
367 
368 static struct net_dev_operations uip_ops = {
369 	.rx	= uip_ops_rx,
370 	.tx	= uip_ops_tx,
371 };
372 
373 static u8 *get_config(struct kvm *kvm, void *dev)
374 {
375 	struct net_dev *ndev = dev;
376 
377 	return ((u8 *)(&ndev->config));
378 }
379 
380 static u32 get_host_features(struct kvm *kvm, void *dev)
381 {
382 	struct net_dev *ndev = dev;
383 
384 	return 1UL << VIRTIO_NET_F_MAC
385 		| 1UL << VIRTIO_NET_F_CSUM
386 		| 1UL << VIRTIO_NET_F_HOST_UFO
387 		| 1UL << VIRTIO_NET_F_HOST_TSO4
388 		| 1UL << VIRTIO_NET_F_HOST_TSO6
389 		| 1UL << VIRTIO_NET_F_GUEST_UFO
390 		| 1UL << VIRTIO_NET_F_GUEST_TSO4
391 		| 1UL << VIRTIO_NET_F_GUEST_TSO6
392 		| 1UL << VIRTIO_RING_F_EVENT_IDX
393 		| 1UL << VIRTIO_RING_F_INDIRECT_DESC
394 		| 1UL << VIRTIO_NET_F_CTRL_VQ
395 		| 1UL << VIRTIO_NET_F_MRG_RXBUF
396 		| 1UL << (ndev->queue_pairs > 1 ? VIRTIO_NET_F_MQ : 0);
397 }
398 
399 static int virtio_net__vhost_set_features(struct net_dev *ndev)
400 {
401 	u64 features = 1UL << VIRTIO_RING_F_EVENT_IDX;
402 	u64 vhost_features;
403 
404 	if (ioctl(ndev->vhost_fd, VHOST_GET_FEATURES, &vhost_features) != 0)
405 		die_perror("VHOST_GET_FEATURES failed");
406 
407 	/* make sure both side support mergable rx buffers */
408 	if (vhost_features & 1UL << VIRTIO_NET_F_MRG_RXBUF &&
409 			has_virtio_feature(ndev, VIRTIO_NET_F_MRG_RXBUF))
410 		features |= 1UL << VIRTIO_NET_F_MRG_RXBUF;
411 
412 	return ioctl(ndev->vhost_fd, VHOST_SET_FEATURES, &features);
413 }
414 
415 static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
416 {
417 	struct net_dev *ndev = dev;
418 
419 	ndev->features = features;
420 
421 	if (ndev->mode == NET_MODE_TAP) {
422 		if (!virtio_net__tap_init(ndev))
423 			die_perror("You have requested a TAP device, but creation of one has failed because");
424 		if (ndev->vhost_fd &&
425 				virtio_net__vhost_set_features(ndev) != 0)
426 			die_perror("VHOST_SET_FEATURES failed");
427 	} else {
428 		ndev->info.vnet_hdr_len = has_virtio_feature(ndev, VIRTIO_NET_F_MRG_RXBUF) ?
429 						sizeof(struct virtio_net_hdr_mrg_rxbuf) :
430 						sizeof(struct virtio_net_hdr);
431 		uip_init(&ndev->info);
432 	}
433 }
434 
435 static bool is_ctrl_vq(struct net_dev *ndev, u32 vq)
436 {
437 	return vq == (u32)(ndev->queue_pairs * 2);
438 }
439 
440 static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align,
441 		   u32 pfn)
442 {
443 	struct vhost_vring_state state = { .index = vq };
444 	struct vhost_vring_addr addr;
445 	struct net_dev *ndev = dev;
446 	struct virt_queue *queue;
447 	void *p;
448 	int r;
449 
450 	compat__remove_message(compat_id);
451 
452 	queue		= &ndev->vqs[vq];
453 	queue->pfn	= pfn;
454 	p		= virtio_get_vq(kvm, queue->pfn, page_size);
455 
456 	vring_init(&queue->vring, VIRTIO_NET_QUEUE_SIZE, p, align);
457 
458 	mutex_init(&ndev->io_lock[vq]);
459 	pthread_cond_init(&ndev->io_cond[vq], NULL);
460 	if (is_ctrl_vq(ndev, vq)) {
461 		pthread_create(&ndev->io_thread[vq], NULL, virtio_net_ctrl_thread, ndev);
462 
463 		return 0;
464 	} else if (ndev->vhost_fd == 0 ) {
465 		if (vq & 1)
466 			pthread_create(&ndev->io_thread[vq], NULL, virtio_net_tx_thread, ndev);
467 		else
468 			pthread_create(&ndev->io_thread[vq], NULL, virtio_net_rx_thread, ndev);
469 
470 		return 0;
471 	}
472 
473 	state.num = queue->vring.num;
474 	r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_NUM, &state);
475 	if (r < 0)
476 		die_perror("VHOST_SET_VRING_NUM failed");
477 	state.num = 0;
478 	r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_BASE, &state);
479 	if (r < 0)
480 		die_perror("VHOST_SET_VRING_BASE failed");
481 
482 	addr = (struct vhost_vring_addr) {
483 		.index = vq,
484 		.desc_user_addr = (u64)(unsigned long)queue->vring.desc,
485 		.avail_user_addr = (u64)(unsigned long)queue->vring.avail,
486 		.used_user_addr = (u64)(unsigned long)queue->vring.used,
487 	};
488 
489 	r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_ADDR, &addr);
490 	if (r < 0)
491 		die_perror("VHOST_SET_VRING_ADDR failed");
492 
493 	return 0;
494 }
495 
496 static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi)
497 {
498 	struct net_dev *ndev = dev;
499 	struct kvm_irqfd irq;
500 	struct vhost_vring_file file;
501 	int r;
502 
503 	if (ndev->vhost_fd == 0)
504 		return;
505 
506 	irq = (struct kvm_irqfd) {
507 		.gsi	= gsi,
508 		.fd	= eventfd(0, 0),
509 	};
510 	file = (struct vhost_vring_file) {
511 		.index	= vq,
512 		.fd	= irq.fd,
513 	};
514 
515 	r = ioctl(kvm->vm_fd, KVM_IRQFD, &irq);
516 	if (r < 0)
517 		die_perror("KVM_IRQFD failed");
518 
519 	r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_CALL, &file);
520 	if (r < 0)
521 		die_perror("VHOST_SET_VRING_CALL failed");
522 	file.fd = ndev->tap_fd;
523 	r = ioctl(ndev->vhost_fd, VHOST_NET_SET_BACKEND, &file);
524 	if (r != 0)
525 		die("VHOST_NET_SET_BACKEND failed %d", errno);
526 
527 }
528 
529 static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd)
530 {
531 	struct net_dev *ndev = dev;
532 	struct vhost_vring_file file = {
533 		.index	= vq,
534 		.fd	= efd,
535 	};
536 	int r;
537 
538 	if (ndev->vhost_fd == 0 || is_ctrl_vq(ndev, vq))
539 		return;
540 
541 	r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_KICK, &file);
542 	if (r < 0)
543 		die_perror("VHOST_SET_VRING_KICK failed");
544 }
545 
546 static int notify_vq(struct kvm *kvm, void *dev, u32 vq)
547 {
548 	struct net_dev *ndev = dev;
549 
550 	virtio_net_handle_callback(kvm, ndev, vq);
551 
552 	return 0;
553 }
554 
555 static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq)
556 {
557 	struct net_dev *ndev = dev;
558 
559 	return ndev->vqs[vq].pfn;
560 }
561 
562 static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)
563 {
564 	/* FIXME: dynamic */
565 	return VIRTIO_NET_QUEUE_SIZE;
566 }
567 
568 static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size)
569 {
570 	/* FIXME: dynamic */
571 	return size;
572 }
573 
574 static struct virtio_ops net_dev_virtio_ops = (struct virtio_ops) {
575 	.get_config		= get_config,
576 	.get_host_features	= get_host_features,
577 	.set_guest_features	= set_guest_features,
578 	.init_vq		= init_vq,
579 	.get_pfn_vq		= get_pfn_vq,
580 	.get_size_vq		= get_size_vq,
581 	.set_size_vq		= set_size_vq,
582 	.notify_vq		= notify_vq,
583 	.notify_vq_gsi		= notify_vq_gsi,
584 	.notify_vq_eventfd	= notify_vq_eventfd,
585 };
586 
587 static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev)
588 {
589 	struct vhost_memory *mem;
590 	int r;
591 
592 	ndev->vhost_fd = open("/dev/vhost-net", O_RDWR);
593 	if (ndev->vhost_fd < 0)
594 		die_perror("Failed openning vhost-net device");
595 
596 	mem = calloc(1, sizeof(*mem) + sizeof(struct vhost_memory_region));
597 	if (mem == NULL)
598 		die("Failed allocating memory for vhost memory map");
599 
600 	mem->nregions = 1;
601 	mem->regions[0] = (struct vhost_memory_region) {
602 		.guest_phys_addr	= 0,
603 		.memory_size		= kvm->ram_size,
604 		.userspace_addr		= (unsigned long)kvm->ram_start,
605 	};
606 
607 	r = ioctl(ndev->vhost_fd, VHOST_SET_OWNER);
608 	if (r != 0)
609 		die_perror("VHOST_SET_OWNER failed");
610 
611 	r = ioctl(ndev->vhost_fd, VHOST_SET_MEM_TABLE, mem);
612 	if (r != 0)
613 		die_perror("VHOST_SET_MEM_TABLE failed");
614 
615 	ndev->vdev.use_vhost = true;
616 
617 	free(mem);
618 }
619 
620 static inline void str_to_mac(const char *str, char *mac)
621 {
622 	sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
623 		mac, mac+1, mac+2, mac+3, mac+4, mac+5);
624 }
625 static int set_net_param(struct kvm *kvm, struct virtio_net_params *p,
626 			const char *param, const char *val)
627 {
628 	if (strcmp(param, "guest_mac") == 0) {
629 		str_to_mac(val, p->guest_mac);
630 	} else if (strcmp(param, "mode") == 0) {
631 		if (!strncmp(val, "user", 4)) {
632 			int i;
633 
634 			for (i = 0; i < kvm->cfg.num_net_devices; i++)
635 				if (kvm->cfg.net_params[i].mode == NET_MODE_USER)
636 					die("Only one usermode network device allowed at a time");
637 			p->mode = NET_MODE_USER;
638 		} else if (!strncmp(val, "tap", 3)) {
639 			p->mode = NET_MODE_TAP;
640 		} else if (!strncmp(val, "none", 4)) {
641 			kvm->cfg.no_net = 1;
642 			return -1;
643 		} else
644 			die("Unknown network mode %s, please use user, tap or none", kvm->cfg.network);
645 	} else if (strcmp(param, "script") == 0) {
646 		p->script = strdup(val);
647 	} else if (strcmp(param, "guest_ip") == 0) {
648 		p->guest_ip = strdup(val);
649 	} else if (strcmp(param, "host_ip") == 0) {
650 		p->host_ip = strdup(val);
651 	} else if (strcmp(param, "trans") == 0) {
652 		p->trans = strdup(val);
653 	} else if (strcmp(param, "vhost") == 0) {
654 		p->vhost = atoi(val);
655 	} else if (strcmp(param, "fd") == 0) {
656 		p->fd = atoi(val);
657 	} else if (strcmp(param, "mq") == 0) {
658 		p->mq = atoi(val);
659 	} else
660 		die("Unknown network parameter %s", param);
661 
662 	return 0;
663 }
664 
665 int netdev_parser(const struct option *opt, const char *arg, int unset)
666 {
667 	struct virtio_net_params p;
668 	char *buf = NULL, *cmd = NULL, *cur = NULL;
669 	bool on_cmd = true;
670 	struct kvm *kvm = opt->ptr;
671 
672 	if (arg) {
673 		buf = strdup(arg);
674 		if (buf == NULL)
675 			die("Failed allocating new net buffer");
676 		cur = strtok(buf, ",=");
677 	}
678 
679 	p = (struct virtio_net_params) {
680 		.guest_ip	= DEFAULT_GUEST_ADDR,
681 		.host_ip	= DEFAULT_HOST_ADDR,
682 		.script		= DEFAULT_SCRIPT,
683 		.mode		= NET_MODE_TAP,
684 	};
685 
686 	str_to_mac(DEFAULT_GUEST_MAC, p.guest_mac);
687 	p.guest_mac[5] += kvm->cfg.num_net_devices;
688 
689 	while (cur) {
690 		if (on_cmd) {
691 			cmd = cur;
692 		} else {
693 			if (set_net_param(kvm, &p, cmd, cur) < 0)
694 				goto done;
695 		}
696 		on_cmd = !on_cmd;
697 
698 		cur = strtok(NULL, ",=");
699 	};
700 
701 	kvm->cfg.num_net_devices++;
702 
703 	kvm->cfg.net_params = realloc(kvm->cfg.net_params, kvm->cfg.num_net_devices * sizeof(*kvm->cfg.net_params));
704 	if (kvm->cfg.net_params == NULL)
705 		die("Failed adding new network device");
706 
707 	kvm->cfg.net_params[kvm->cfg.num_net_devices - 1] = p;
708 
709 done:
710 	free(buf);
711 	return 0;
712 }
713 
714 static int virtio_net__init_one(struct virtio_net_params *params)
715 {
716 	int i;
717 	struct net_dev *ndev;
718 
719 	ndev = calloc(1, sizeof(struct net_dev));
720 	if (ndev == NULL)
721 		return -ENOMEM;
722 
723 	list_add_tail(&ndev->list, &ndevs);
724 
725 	ndev->kvm = params->kvm;
726 	ndev->params = params;
727 
728 	mutex_init(&ndev->mutex);
729 	ndev->queue_pairs = max(1, min(VIRTIO_NET_NUM_QUEUES, params->mq));
730 	ndev->config.status = VIRTIO_NET_S_LINK_UP;
731 	if (ndev->queue_pairs > 1)
732 		ndev->config.max_virtqueue_pairs = ndev->queue_pairs;
733 
734 	for (i = 0 ; i < 6 ; i++) {
735 		ndev->config.mac[i]		= params->guest_mac[i];
736 		ndev->info.guest_mac.addr[i]	= params->guest_mac[i];
737 		ndev->info.host_mac.addr[i]	= params->host_mac[i];
738 	}
739 
740 	ndev->mode = params->mode;
741 	if (ndev->mode == NET_MODE_TAP) {
742 		ndev->ops = &tap_ops;
743 	} else {
744 		ndev->info.host_ip		= ntohl(inet_addr(params->host_ip));
745 		ndev->info.guest_ip		= ntohl(inet_addr(params->guest_ip));
746 		ndev->info.guest_netmask	= ntohl(inet_addr("255.255.255.0"));
747 		ndev->info.buf_nr		= 20,
748 		ndev->ops = &uip_ops;
749 		uip_static_init(&ndev->info);
750 	}
751 
752 	if (params->trans && strcmp(params->trans, "mmio") == 0)
753 		virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,
754 			    VIRTIO_MMIO, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET);
755 	else
756 		virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops,
757 			    VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET);
758 
759 	if (params->vhost)
760 		virtio_net__vhost_init(params->kvm, ndev);
761 
762 	if (compat_id == -1)
763 		compat_id = virtio_compat_add_message("virtio-net", "CONFIG_VIRTIO_NET");
764 
765 	return 0;
766 }
767 
768 int virtio_net__init(struct kvm *kvm)
769 {
770 	int i;
771 
772 	for (i = 0; i < kvm->cfg.num_net_devices; i++) {
773 		kvm->cfg.net_params[i].kvm = kvm;
774 		virtio_net__init_one(&kvm->cfg.net_params[i]);
775 	}
776 
777 	if (kvm->cfg.num_net_devices == 0 && kvm->cfg.no_net == 0) {
778 		static struct virtio_net_params net_params;
779 
780 		net_params = (struct virtio_net_params) {
781 			.guest_ip	= kvm->cfg.guest_ip,
782 			.host_ip	= kvm->cfg.host_ip,
783 			.kvm		= kvm,
784 			.script		= kvm->cfg.script,
785 			.mode		= NET_MODE_USER,
786 		};
787 		str_to_mac(kvm->cfg.guest_mac, net_params.guest_mac);
788 		str_to_mac(kvm->cfg.host_mac, net_params.host_mac);
789 
790 		virtio_net__init_one(&net_params);
791 	}
792 
793 	return 0;
794 }
795 virtio_dev_init(virtio_net__init);
796 
797 int virtio_net__exit(struct kvm *kvm)
798 {
799 	return 0;
800 }
801 virtio_dev_exit(virtio_net__exit);
802