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 = (ndev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? 247 sizeof(struct virtio_net_hdr_mrg_rxbuf) : 248 sizeof(struct virtio_net_hdr); 249 if (ioctl(ndev->tap_fd, TUNSETVNETHDRSZ, &hdr_len) < 0) 250 pr_warning("Config tap device TUNSETVNETHDRSZ error"); 251 252 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | TUN_F_UFO; 253 if (ioctl(ndev->tap_fd, TUNSETOFFLOAD, offload) < 0) { 254 pr_warning("Config tap device TUNSETOFFLOAD error"); 255 goto fail; 256 } 257 258 if (strcmp(params->script, "none")) { 259 pid = fork(); 260 if (pid == 0) { 261 execl(params->script, params->script, ndev->tap_name, NULL); 262 _exit(1); 263 } else { 264 waitpid(pid, &status, 0); 265 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { 266 pr_warning("Fail to setup tap by %s", params->script); 267 goto fail; 268 } 269 } 270 } else { 271 memset(&ifr, 0, sizeof(ifr)); 272 strncpy(ifr.ifr_name, ndev->tap_name, sizeof(ndev->tap_name)); 273 sin.sin_addr.s_addr = inet_addr(params->host_ip); 274 memcpy(&(ifr.ifr_addr), &sin, sizeof(ifr.ifr_addr)); 275 ifr.ifr_addr.sa_family = AF_INET; 276 if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) { 277 pr_warning("Could not set ip address on tap device"); 278 goto fail; 279 } 280 } 281 282 memset(&ifr, 0, sizeof(ifr)); 283 strncpy(ifr.ifr_name, ndev->tap_name, sizeof(ndev->tap_name)); 284 ioctl(sock, SIOCGIFFLAGS, &ifr); 285 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 286 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) 287 pr_warning("Could not bring tap device up"); 288 289 close(sock); 290 291 return 1; 292 293 fail: 294 if (sock >= 0) 295 close(sock); 296 if (ndev->tap_fd >= 0) 297 close(ndev->tap_fd); 298 299 return 0; 300 } 301 302 static inline int tap_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev) 303 { 304 return writev(ndev->tap_fd, iov, out); 305 } 306 307 static inline int tap_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev) 308 { 309 return readv(ndev->tap_fd, iov, in); 310 } 311 312 static inline int uip_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev) 313 { 314 return uip_tx(iov, out, &ndev->info); 315 } 316 317 static inline int uip_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev) 318 { 319 return uip_rx(iov, in, &ndev->info); 320 } 321 322 static struct net_dev_operations tap_ops = { 323 .rx = tap_ops_rx, 324 .tx = tap_ops_tx, 325 }; 326 327 static struct net_dev_operations uip_ops = { 328 .rx = uip_ops_rx, 329 .tx = uip_ops_tx, 330 }; 331 332 static u8 *get_config(struct kvm *kvm, void *dev) 333 { 334 struct net_dev *ndev = dev; 335 336 return ((u8 *)(&ndev->config)); 337 } 338 339 static u32 get_host_features(struct kvm *kvm, void *dev) 340 { 341 struct net_dev *ndev = dev; 342 343 return 1UL << VIRTIO_NET_F_MAC 344 | 1UL << VIRTIO_NET_F_CSUM 345 | 1UL << VIRTIO_NET_F_HOST_UFO 346 | 1UL << VIRTIO_NET_F_HOST_TSO4 347 | 1UL << VIRTIO_NET_F_HOST_TSO6 348 | 1UL << VIRTIO_NET_F_GUEST_UFO 349 | 1UL << VIRTIO_NET_F_GUEST_TSO4 350 | 1UL << VIRTIO_NET_F_GUEST_TSO6 351 | 1UL << VIRTIO_RING_F_EVENT_IDX 352 | 1UL << VIRTIO_RING_F_INDIRECT_DESC 353 | 1UL << VIRTIO_NET_F_CTRL_VQ 354 | 1UL << (ndev->queue_pairs > 1 ? VIRTIO_NET_F_MQ : 0); 355 } 356 357 static void set_guest_features(struct kvm *kvm, void *dev, u32 features) 358 { 359 struct net_dev *ndev = dev; 360 361 ndev->features = features; 362 } 363 364 static bool is_ctrl_vq(struct net_dev *ndev, u32 vq) 365 { 366 return vq == (u32)(ndev->queue_pairs * 2); 367 } 368 369 static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align, 370 u32 pfn) 371 { 372 struct vhost_vring_state state = { .index = vq }; 373 struct vhost_vring_addr addr; 374 struct net_dev *ndev = dev; 375 struct virt_queue *queue; 376 void *p; 377 int r; 378 379 compat__remove_message(compat_id); 380 381 queue = &ndev->vqs[vq]; 382 queue->pfn = pfn; 383 p = guest_flat_to_host(kvm, queue->pfn * page_size); 384 385 vring_init(&queue->vring, VIRTIO_NET_QUEUE_SIZE, p, align); 386 387 mutex_init(&ndev->io_lock[vq]); 388 pthread_cond_init(&ndev->io_cond[vq], NULL); 389 if (is_ctrl_vq(ndev, vq)) { 390 pthread_create(&ndev->io_thread[vq], NULL, virtio_net_ctrl_thread, ndev); 391 392 return 0; 393 } else if (ndev->vhost_fd == 0 ) { 394 if (vq & 1) 395 pthread_create(&ndev->io_thread[vq], NULL, virtio_net_tx_thread, ndev); 396 else 397 pthread_create(&ndev->io_thread[vq], NULL, virtio_net_rx_thread, ndev); 398 399 return 0; 400 } 401 402 state.num = queue->vring.num; 403 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_NUM, &state); 404 if (r < 0) 405 die_perror("VHOST_SET_VRING_NUM failed"); 406 state.num = 0; 407 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_BASE, &state); 408 if (r < 0) 409 die_perror("VHOST_SET_VRING_BASE failed"); 410 411 addr = (struct vhost_vring_addr) { 412 .index = vq, 413 .desc_user_addr = (u64)(unsigned long)queue->vring.desc, 414 .avail_user_addr = (u64)(unsigned long)queue->vring.avail, 415 .used_user_addr = (u64)(unsigned long)queue->vring.used, 416 }; 417 418 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_ADDR, &addr); 419 if (r < 0) 420 die_perror("VHOST_SET_VRING_ADDR failed"); 421 422 return 0; 423 } 424 425 static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) 426 { 427 struct net_dev *ndev = dev; 428 struct kvm_irqfd irq; 429 struct vhost_vring_file file; 430 int r; 431 432 if (ndev->vhost_fd == 0) 433 return; 434 435 irq = (struct kvm_irqfd) { 436 .gsi = gsi, 437 .fd = eventfd(0, 0), 438 }; 439 file = (struct vhost_vring_file) { 440 .index = vq, 441 .fd = irq.fd, 442 }; 443 444 r = ioctl(kvm->vm_fd, KVM_IRQFD, &irq); 445 if (r < 0) 446 die_perror("KVM_IRQFD failed"); 447 448 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_CALL, &file); 449 if (r < 0) 450 die_perror("VHOST_SET_VRING_CALL failed"); 451 file.fd = ndev->tap_fd; 452 r = ioctl(ndev->vhost_fd, VHOST_NET_SET_BACKEND, &file); 453 if (r != 0) 454 die("VHOST_NET_SET_BACKEND failed %d", errno); 455 456 } 457 458 static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) 459 { 460 struct net_dev *ndev = dev; 461 struct vhost_vring_file file = { 462 .index = vq, 463 .fd = efd, 464 }; 465 int r; 466 467 if (ndev->vhost_fd == 0 || is_ctrl_vq(ndev, vq)) 468 return; 469 470 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_KICK, &file); 471 if (r < 0) 472 die_perror("VHOST_SET_VRING_KICK failed"); 473 } 474 475 static int notify_vq(struct kvm *kvm, void *dev, u32 vq) 476 { 477 struct net_dev *ndev = dev; 478 479 virtio_net_handle_callback(kvm, ndev, vq); 480 481 return 0; 482 } 483 484 static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq) 485 { 486 struct net_dev *ndev = dev; 487 488 return ndev->vqs[vq].pfn; 489 } 490 491 static int get_size_vq(struct kvm *kvm, void *dev, u32 vq) 492 { 493 /* FIXME: dynamic */ 494 return VIRTIO_NET_QUEUE_SIZE; 495 } 496 497 static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size) 498 { 499 /* FIXME: dynamic */ 500 return size; 501 } 502 503 static void notify_status(struct kvm *kvm, void *dev, u8 status); 504 505 static struct virtio_ops net_dev_virtio_ops = (struct virtio_ops) { 506 .get_config = get_config, 507 .get_host_features = get_host_features, 508 .set_guest_features = set_guest_features, 509 .init_vq = init_vq, 510 .get_pfn_vq = get_pfn_vq, 511 .get_size_vq = get_size_vq, 512 .set_size_vq = set_size_vq, 513 .notify_vq = notify_vq, 514 .notify_vq_gsi = notify_vq_gsi, 515 .notify_vq_eventfd = notify_vq_eventfd, 516 .notify_status = notify_status, 517 }; 518 519 static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev) 520 { 521 u64 features = 1UL << VIRTIO_RING_F_EVENT_IDX; 522 struct vhost_memory *mem; 523 int r; 524 525 ndev->vhost_fd = open("/dev/vhost-net", O_RDWR); 526 if (ndev->vhost_fd < 0) 527 die_perror("Failed openning vhost-net device"); 528 529 mem = calloc(1, sizeof(*mem) + sizeof(struct vhost_memory_region)); 530 if (mem == NULL) 531 die("Failed allocating memory for vhost memory map"); 532 533 mem->nregions = 1; 534 mem->regions[0] = (struct vhost_memory_region) { 535 .guest_phys_addr = 0, 536 .memory_size = kvm->ram_size, 537 .userspace_addr = (unsigned long)kvm->ram_start, 538 }; 539 540 r = ioctl(ndev->vhost_fd, VHOST_SET_OWNER); 541 if (r != 0) 542 die_perror("VHOST_SET_OWNER failed"); 543 544 r = ioctl(ndev->vhost_fd, VHOST_SET_FEATURES, &features); 545 if (r != 0) 546 die_perror("VHOST_SET_FEATURES failed"); 547 r = ioctl(ndev->vhost_fd, VHOST_SET_MEM_TABLE, mem); 548 if (r != 0) 549 die_perror("VHOST_SET_MEM_TABLE failed"); 550 551 ndev->vdev.use_vhost = true; 552 553 free(mem); 554 } 555 556 static inline void str_to_mac(const char *str, char *mac) 557 { 558 sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 559 mac, mac+1, mac+2, mac+3, mac+4, mac+5); 560 } 561 static int set_net_param(struct kvm *kvm, struct virtio_net_params *p, 562 const char *param, const char *val) 563 { 564 if (strcmp(param, "guest_mac") == 0) { 565 str_to_mac(val, p->guest_mac); 566 } else if (strcmp(param, "mode") == 0) { 567 if (!strncmp(val, "user", 4)) { 568 int i; 569 570 for (i = 0; i < kvm->cfg.num_net_devices; i++) 571 if (kvm->cfg.net_params[i].mode == NET_MODE_USER) 572 die("Only one usermode network device allowed at a time"); 573 p->mode = NET_MODE_USER; 574 } else if (!strncmp(val, "tap", 3)) { 575 p->mode = NET_MODE_TAP; 576 } else if (!strncmp(val, "none", 4)) { 577 kvm->cfg.no_net = 1; 578 return -1; 579 } else 580 die("Unknown network mode %s, please use user, tap or none", kvm->cfg.network); 581 } else if (strcmp(param, "script") == 0) { 582 p->script = strdup(val); 583 } else if (strcmp(param, "guest_ip") == 0) { 584 p->guest_ip = strdup(val); 585 } else if (strcmp(param, "host_ip") == 0) { 586 p->host_ip = strdup(val); 587 } else if (strcmp(param, "trans") == 0) { 588 p->trans = strdup(val); 589 } else if (strcmp(param, "vhost") == 0) { 590 p->vhost = atoi(val); 591 } else if (strcmp(param, "fd") == 0) { 592 p->fd = atoi(val); 593 } else if (strcmp(param, "mq") == 0) { 594 p->mq = atoi(val); 595 } else 596 die("Unknown network parameter %s", param); 597 598 return 0; 599 } 600 601 int netdev_parser(const struct option *opt, const char *arg, int unset) 602 { 603 struct virtio_net_params p; 604 char *buf = NULL, *cmd = NULL, *cur = NULL; 605 bool on_cmd = true; 606 struct kvm *kvm = opt->ptr; 607 608 if (arg) { 609 buf = strdup(arg); 610 if (buf == NULL) 611 die("Failed allocating new net buffer"); 612 cur = strtok(buf, ",="); 613 } 614 615 p = (struct virtio_net_params) { 616 .guest_ip = DEFAULT_GUEST_ADDR, 617 .host_ip = DEFAULT_HOST_ADDR, 618 .script = DEFAULT_SCRIPT, 619 .mode = NET_MODE_TAP, 620 }; 621 622 str_to_mac(DEFAULT_GUEST_MAC, p.guest_mac); 623 p.guest_mac[5] += kvm->cfg.num_net_devices; 624 625 while (cur) { 626 if (on_cmd) { 627 cmd = cur; 628 } else { 629 if (set_net_param(kvm, &p, cmd, cur) < 0) 630 goto done; 631 } 632 on_cmd = !on_cmd; 633 634 cur = strtok(NULL, ",="); 635 }; 636 637 kvm->cfg.num_net_devices++; 638 639 kvm->cfg.net_params = realloc(kvm->cfg.net_params, kvm->cfg.num_net_devices * sizeof(*kvm->cfg.net_params)); 640 if (kvm->cfg.net_params == NULL) 641 die("Failed adding new network device"); 642 643 kvm->cfg.net_params[kvm->cfg.num_net_devices - 1] = p; 644 645 done: 646 free(buf); 647 return 0; 648 } 649 650 static int virtio_net__init_one(struct virtio_net_params *params) 651 { 652 int i; 653 struct net_dev *ndev; 654 655 ndev = calloc(1, sizeof(struct net_dev)); 656 if (ndev == NULL) 657 return -ENOMEM; 658 659 list_add_tail(&ndev->list, &ndevs); 660 661 ndev->kvm = params->kvm; 662 ndev->params = params; 663 664 mutex_init(&ndev->mutex); 665 ndev->queue_pairs = max(1, min(VIRTIO_NET_NUM_QUEUES, params->mq)); 666 ndev->config.status = VIRTIO_NET_S_LINK_UP; 667 if (ndev->queue_pairs > 1) 668 ndev->config.max_virtqueue_pairs = ndev->queue_pairs; 669 670 for (i = 0 ; i < 6 ; i++) { 671 ndev->config.mac[i] = params->guest_mac[i]; 672 ndev->info.guest_mac.addr[i] = params->guest_mac[i]; 673 ndev->info.host_mac.addr[i] = params->host_mac[i]; 674 } 675 676 ndev->mode = params->mode; 677 if (ndev->mode == NET_MODE_TAP) { 678 ndev->ops = &tap_ops; 679 } else { 680 ndev->info.host_ip = ntohl(inet_addr(params->host_ip)); 681 ndev->info.guest_ip = ntohl(inet_addr(params->guest_ip)); 682 ndev->info.guest_netmask = ntohl(inet_addr("255.255.255.0")); 683 ndev->info.buf_nr = 20, 684 ndev->ops = &uip_ops; 685 } 686 687 if (params->trans && strcmp(params->trans, "mmio") == 0) 688 virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops, 689 VIRTIO_MMIO, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET); 690 else 691 virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops, 692 VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET); 693 694 if (params->vhost) 695 virtio_net__vhost_init(params->kvm, ndev); 696 697 if (compat_id == -1) 698 compat_id = virtio_compat_add_message("virtio-net", "CONFIG_VIRTIO_NET"); 699 700 return 0; 701 } 702 703 static void notify_status(struct kvm *kvm, void *dev, u8 status) 704 { 705 struct net_dev *ndev = dev; 706 707 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) 708 return; 709 710 if (ndev->mode == NET_MODE_TAP) { 711 if (!virtio_net__tap_init(ndev)) 712 die_perror("You have requested a TAP device, but creation of one has failed because"); 713 } else { 714 ndev->info.vnet_hdr_len = (ndev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? 715 sizeof(struct virtio_net_hdr_mrg_rxbuf) : 716 sizeof(struct virtio_net_hdr); 717 uip_init(&ndev->info); 718 } 719 } 720 721 int virtio_net__init(struct kvm *kvm) 722 { 723 int i; 724 725 for (i = 0; i < kvm->cfg.num_net_devices; i++) { 726 kvm->cfg.net_params[i].kvm = kvm; 727 virtio_net__init_one(&kvm->cfg.net_params[i]); 728 } 729 730 if (kvm->cfg.num_net_devices == 0 && kvm->cfg.no_net == 0) { 731 static struct virtio_net_params net_params; 732 733 net_params = (struct virtio_net_params) { 734 .guest_ip = kvm->cfg.guest_ip, 735 .host_ip = kvm->cfg.host_ip, 736 .kvm = kvm, 737 .script = kvm->cfg.script, 738 .mode = NET_MODE_USER, 739 }; 740 str_to_mac(kvm->cfg.guest_mac, net_params.guest_mac); 741 str_to_mac(kvm->cfg.host_mac, net_params.host_mac); 742 743 virtio_net__init_one(&net_params); 744 } 745 746 return 0; 747 } 748 virtio_dev_init(virtio_net__init); 749 750 int virtio_net__exit(struct kvm *kvm) 751 { 752 return 0; 753 } 754 virtio_dev_exit(virtio_net__exit); 755