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