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_toiovec(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 if (ndev->mode == NET_MODE_TAP) { 394 if (!virtio_net__tap_init(ndev)) 395 die_perror("You have requested a TAP device, but creation of one has failed because"); 396 } else { 397 ndev->info.vnet_hdr_len = has_virtio_feature(ndev, VIRTIO_NET_F_MRG_RXBUF) ? 398 sizeof(struct virtio_net_hdr_mrg_rxbuf) : 399 sizeof(struct virtio_net_hdr); 400 uip_init(&ndev->info); 401 } 402 } 403 404 static bool is_ctrl_vq(struct net_dev *ndev, u32 vq) 405 { 406 return vq == (u32)(ndev->queue_pairs * 2); 407 } 408 409 static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align, 410 u32 pfn) 411 { 412 struct vhost_vring_state state = { .index = vq }; 413 struct vhost_vring_addr addr; 414 struct net_dev *ndev = dev; 415 struct virt_queue *queue; 416 void *p; 417 int r; 418 419 compat__remove_message(compat_id); 420 421 queue = &ndev->vqs[vq]; 422 queue->pfn = pfn; 423 p = virtio_get_vq(kvm, queue->pfn, page_size); 424 425 vring_init(&queue->vring, VIRTIO_NET_QUEUE_SIZE, p, align); 426 427 mutex_init(&ndev->io_lock[vq]); 428 pthread_cond_init(&ndev->io_cond[vq], NULL); 429 if (is_ctrl_vq(ndev, vq)) { 430 pthread_create(&ndev->io_thread[vq], NULL, virtio_net_ctrl_thread, ndev); 431 432 return 0; 433 } else if (ndev->vhost_fd == 0 ) { 434 if (vq & 1) 435 pthread_create(&ndev->io_thread[vq], NULL, virtio_net_tx_thread, ndev); 436 else 437 pthread_create(&ndev->io_thread[vq], NULL, virtio_net_rx_thread, ndev); 438 439 return 0; 440 } 441 442 state.num = queue->vring.num; 443 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_NUM, &state); 444 if (r < 0) 445 die_perror("VHOST_SET_VRING_NUM failed"); 446 state.num = 0; 447 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_BASE, &state); 448 if (r < 0) 449 die_perror("VHOST_SET_VRING_BASE failed"); 450 451 addr = (struct vhost_vring_addr) { 452 .index = vq, 453 .desc_user_addr = (u64)(unsigned long)queue->vring.desc, 454 .avail_user_addr = (u64)(unsigned long)queue->vring.avail, 455 .used_user_addr = (u64)(unsigned long)queue->vring.used, 456 }; 457 458 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_ADDR, &addr); 459 if (r < 0) 460 die_perror("VHOST_SET_VRING_ADDR failed"); 461 462 return 0; 463 } 464 465 static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) 466 { 467 struct net_dev *ndev = dev; 468 struct kvm_irqfd irq; 469 struct vhost_vring_file file; 470 int r; 471 472 if (ndev->vhost_fd == 0) 473 return; 474 475 irq = (struct kvm_irqfd) { 476 .gsi = gsi, 477 .fd = eventfd(0, 0), 478 }; 479 file = (struct vhost_vring_file) { 480 .index = vq, 481 .fd = irq.fd, 482 }; 483 484 r = ioctl(kvm->vm_fd, KVM_IRQFD, &irq); 485 if (r < 0) 486 die_perror("KVM_IRQFD failed"); 487 488 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_CALL, &file); 489 if (r < 0) 490 die_perror("VHOST_SET_VRING_CALL failed"); 491 file.fd = ndev->tap_fd; 492 r = ioctl(ndev->vhost_fd, VHOST_NET_SET_BACKEND, &file); 493 if (r != 0) 494 die("VHOST_NET_SET_BACKEND failed %d", errno); 495 496 } 497 498 static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) 499 { 500 struct net_dev *ndev = dev; 501 struct vhost_vring_file file = { 502 .index = vq, 503 .fd = efd, 504 }; 505 int r; 506 507 if (ndev->vhost_fd == 0 || is_ctrl_vq(ndev, vq)) 508 return; 509 510 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_KICK, &file); 511 if (r < 0) 512 die_perror("VHOST_SET_VRING_KICK failed"); 513 } 514 515 static int notify_vq(struct kvm *kvm, void *dev, u32 vq) 516 { 517 struct net_dev *ndev = dev; 518 519 virtio_net_handle_callback(kvm, ndev, vq); 520 521 return 0; 522 } 523 524 static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq) 525 { 526 struct net_dev *ndev = dev; 527 528 return ndev->vqs[vq].pfn; 529 } 530 531 static int get_size_vq(struct kvm *kvm, void *dev, u32 vq) 532 { 533 /* FIXME: dynamic */ 534 return VIRTIO_NET_QUEUE_SIZE; 535 } 536 537 static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size) 538 { 539 /* FIXME: dynamic */ 540 return size; 541 } 542 543 static struct virtio_ops net_dev_virtio_ops = (struct virtio_ops) { 544 .get_config = get_config, 545 .get_host_features = get_host_features, 546 .set_guest_features = set_guest_features, 547 .init_vq = init_vq, 548 .get_pfn_vq = get_pfn_vq, 549 .get_size_vq = get_size_vq, 550 .set_size_vq = set_size_vq, 551 .notify_vq = notify_vq, 552 .notify_vq_gsi = notify_vq_gsi, 553 .notify_vq_eventfd = notify_vq_eventfd, 554 }; 555 556 static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev) 557 { 558 u64 features = 1UL << VIRTIO_RING_F_EVENT_IDX; 559 struct vhost_memory *mem; 560 int r; 561 562 ndev->vhost_fd = open("/dev/vhost-net", O_RDWR); 563 if (ndev->vhost_fd < 0) 564 die_perror("Failed openning vhost-net device"); 565 566 mem = calloc(1, sizeof(*mem) + sizeof(struct vhost_memory_region)); 567 if (mem == NULL) 568 die("Failed allocating memory for vhost memory map"); 569 570 mem->nregions = 1; 571 mem->regions[0] = (struct vhost_memory_region) { 572 .guest_phys_addr = 0, 573 .memory_size = kvm->ram_size, 574 .userspace_addr = (unsigned long)kvm->ram_start, 575 }; 576 577 r = ioctl(ndev->vhost_fd, VHOST_SET_OWNER); 578 if (r != 0) 579 die_perror("VHOST_SET_OWNER failed"); 580 581 r = ioctl(ndev->vhost_fd, VHOST_SET_FEATURES, &features); 582 if (r != 0) 583 die_perror("VHOST_SET_FEATURES failed"); 584 r = ioctl(ndev->vhost_fd, VHOST_SET_MEM_TABLE, mem); 585 if (r != 0) 586 die_perror("VHOST_SET_MEM_TABLE failed"); 587 588 ndev->vdev.use_vhost = true; 589 590 free(mem); 591 } 592 593 static inline void str_to_mac(const char *str, char *mac) 594 { 595 sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 596 mac, mac+1, mac+2, mac+3, mac+4, mac+5); 597 } 598 static int set_net_param(struct kvm *kvm, struct virtio_net_params *p, 599 const char *param, const char *val) 600 { 601 if (strcmp(param, "guest_mac") == 0) { 602 str_to_mac(val, p->guest_mac); 603 } else if (strcmp(param, "mode") == 0) { 604 if (!strncmp(val, "user", 4)) { 605 int i; 606 607 for (i = 0; i < kvm->cfg.num_net_devices; i++) 608 if (kvm->cfg.net_params[i].mode == NET_MODE_USER) 609 die("Only one usermode network device allowed at a time"); 610 p->mode = NET_MODE_USER; 611 } else if (!strncmp(val, "tap", 3)) { 612 p->mode = NET_MODE_TAP; 613 } else if (!strncmp(val, "none", 4)) { 614 kvm->cfg.no_net = 1; 615 return -1; 616 } else 617 die("Unknown network mode %s, please use user, tap or none", kvm->cfg.network); 618 } else if (strcmp(param, "script") == 0) { 619 p->script = strdup(val); 620 } else if (strcmp(param, "guest_ip") == 0) { 621 p->guest_ip = strdup(val); 622 } else if (strcmp(param, "host_ip") == 0) { 623 p->host_ip = strdup(val); 624 } else if (strcmp(param, "trans") == 0) { 625 p->trans = strdup(val); 626 } else if (strcmp(param, "vhost") == 0) { 627 p->vhost = atoi(val); 628 } else if (strcmp(param, "fd") == 0) { 629 p->fd = atoi(val); 630 } else if (strcmp(param, "mq") == 0) { 631 p->mq = atoi(val); 632 } else 633 die("Unknown network parameter %s", param); 634 635 return 0; 636 } 637 638 int netdev_parser(const struct option *opt, const char *arg, int unset) 639 { 640 struct virtio_net_params p; 641 char *buf = NULL, *cmd = NULL, *cur = NULL; 642 bool on_cmd = true; 643 struct kvm *kvm = opt->ptr; 644 645 if (arg) { 646 buf = strdup(arg); 647 if (buf == NULL) 648 die("Failed allocating new net buffer"); 649 cur = strtok(buf, ",="); 650 } 651 652 p = (struct virtio_net_params) { 653 .guest_ip = DEFAULT_GUEST_ADDR, 654 .host_ip = DEFAULT_HOST_ADDR, 655 .script = DEFAULT_SCRIPT, 656 .mode = NET_MODE_TAP, 657 }; 658 659 str_to_mac(DEFAULT_GUEST_MAC, p.guest_mac); 660 p.guest_mac[5] += kvm->cfg.num_net_devices; 661 662 while (cur) { 663 if (on_cmd) { 664 cmd = cur; 665 } else { 666 if (set_net_param(kvm, &p, cmd, cur) < 0) 667 goto done; 668 } 669 on_cmd = !on_cmd; 670 671 cur = strtok(NULL, ",="); 672 }; 673 674 kvm->cfg.num_net_devices++; 675 676 kvm->cfg.net_params = realloc(kvm->cfg.net_params, kvm->cfg.num_net_devices * sizeof(*kvm->cfg.net_params)); 677 if (kvm->cfg.net_params == NULL) 678 die("Failed adding new network device"); 679 680 kvm->cfg.net_params[kvm->cfg.num_net_devices - 1] = p; 681 682 done: 683 free(buf); 684 return 0; 685 } 686 687 static int virtio_net__init_one(struct virtio_net_params *params) 688 { 689 int i; 690 struct net_dev *ndev; 691 692 ndev = calloc(1, sizeof(struct net_dev)); 693 if (ndev == NULL) 694 return -ENOMEM; 695 696 list_add_tail(&ndev->list, &ndevs); 697 698 ndev->kvm = params->kvm; 699 ndev->params = params; 700 701 mutex_init(&ndev->mutex); 702 ndev->queue_pairs = max(1, min(VIRTIO_NET_NUM_QUEUES, params->mq)); 703 ndev->config.status = VIRTIO_NET_S_LINK_UP; 704 if (ndev->queue_pairs > 1) 705 ndev->config.max_virtqueue_pairs = ndev->queue_pairs; 706 707 for (i = 0 ; i < 6 ; i++) { 708 ndev->config.mac[i] = params->guest_mac[i]; 709 ndev->info.guest_mac.addr[i] = params->guest_mac[i]; 710 ndev->info.host_mac.addr[i] = params->host_mac[i]; 711 } 712 713 ndev->mode = params->mode; 714 if (ndev->mode == NET_MODE_TAP) { 715 ndev->ops = &tap_ops; 716 } else { 717 ndev->info.host_ip = ntohl(inet_addr(params->host_ip)); 718 ndev->info.guest_ip = ntohl(inet_addr(params->guest_ip)); 719 ndev->info.guest_netmask = ntohl(inet_addr("255.255.255.0")); 720 ndev->info.buf_nr = 20, 721 ndev->ops = &uip_ops; 722 uip_static_init(&ndev->info); 723 } 724 725 if (params->trans && strcmp(params->trans, "mmio") == 0) 726 virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops, 727 VIRTIO_MMIO, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET); 728 else 729 virtio_init(params->kvm, ndev, &ndev->vdev, &net_dev_virtio_ops, 730 VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET); 731 732 if (params->vhost) 733 virtio_net__vhost_init(params->kvm, ndev); 734 735 if (compat_id == -1) 736 compat_id = virtio_compat_add_message("virtio-net", "CONFIG_VIRTIO_NET"); 737 738 return 0; 739 } 740 741 int virtio_net__init(struct kvm *kvm) 742 { 743 int i; 744 745 for (i = 0; i < kvm->cfg.num_net_devices; i++) { 746 kvm->cfg.net_params[i].kvm = kvm; 747 virtio_net__init_one(&kvm->cfg.net_params[i]); 748 } 749 750 if (kvm->cfg.num_net_devices == 0 && kvm->cfg.no_net == 0) { 751 static struct virtio_net_params net_params; 752 753 net_params = (struct virtio_net_params) { 754 .guest_ip = kvm->cfg.guest_ip, 755 .host_ip = kvm->cfg.host_ip, 756 .kvm = kvm, 757 .script = kvm->cfg.script, 758 .mode = NET_MODE_USER, 759 }; 760 str_to_mac(kvm->cfg.guest_mac, net_params.guest_mac); 761 str_to_mac(kvm->cfg.host_mac, net_params.host_mac); 762 763 virtio_net__init_one(&net_params); 764 } 765 766 return 0; 767 } 768 virtio_dev_init(virtio_net__init); 769 770 int virtio_net__exit(struct kvm *kvm) 771 { 772 return 0; 773 } 774 virtio_dev_exit(virtio_net__exit); 775