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