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