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