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