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/virtio-trans.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 <assert.h> 23 #include <fcntl.h> 24 25 #include <sys/socket.h> 26 #include <sys/ioctl.h> 27 #include <sys/types.h> 28 #include <sys/wait.h> 29 #include <sys/eventfd.h> 30 31 #define VIRTIO_NET_QUEUE_SIZE 128 32 #define VIRTIO_NET_NUM_QUEUES 2 33 #define VIRTIO_NET_RX_QUEUE 0 34 #define VIRTIO_NET_TX_QUEUE 1 35 36 struct net_dev; 37 38 extern struct kvm *kvm; 39 40 struct net_dev_operations { 41 int (*rx)(struct iovec *iov, u16 in, struct net_dev *ndev); 42 int (*tx)(struct iovec *iov, u16 in, struct net_dev *ndev); 43 }; 44 45 struct net_dev { 46 pthread_mutex_t mutex; 47 struct virtio_trans vtrans; 48 struct list_head list; 49 50 struct virt_queue vqs[VIRTIO_NET_NUM_QUEUES]; 51 struct virtio_net_config config; 52 u32 features; 53 54 pthread_t io_rx_thread; 55 pthread_mutex_t io_rx_lock; 56 pthread_cond_t io_rx_cond; 57 58 pthread_t io_tx_thread; 59 pthread_mutex_t io_tx_lock; 60 pthread_cond_t io_tx_cond; 61 62 int vhost_fd; 63 int tap_fd; 64 char tap_name[IFNAMSIZ]; 65 66 int mode; 67 68 struct uip_info info; 69 struct net_dev_operations *ops; 70 struct kvm *kvm; 71 }; 72 73 static LIST_HEAD(ndevs); 74 static int compat_id = -1; 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 int len; 85 86 kvm = ndev->kvm; 87 vq = &ndev->vqs[VIRTIO_NET_RX_QUEUE]; 88 89 while (1) { 90 mutex_lock(&ndev->io_rx_lock); 91 if (!virt_queue__available(vq)) 92 pthread_cond_wait(&ndev->io_rx_cond, &ndev->io_rx_lock); 93 mutex_unlock(&ndev->io_rx_lock); 94 95 while (virt_queue__available(vq)) { 96 head = virt_queue__get_iov(vq, iov, &out, &in, kvm); 97 len = ndev->ops->rx(iov, in, ndev); 98 virt_queue__set_used_elem(vq, head, len); 99 100 /* We should interrupt guest right now, otherwise latency is huge. */ 101 if (virtio_queue__should_signal(&ndev->vqs[VIRTIO_NET_RX_QUEUE])) 102 ndev->vtrans.trans_ops->signal_vq(kvm, &ndev->vtrans, 103 VIRTIO_NET_RX_QUEUE); 104 } 105 } 106 107 pthread_exit(NULL); 108 return NULL; 109 110 } 111 112 static void *virtio_net_tx_thread(void *p) 113 { 114 struct iovec iov[VIRTIO_NET_QUEUE_SIZE]; 115 struct virt_queue *vq; 116 struct kvm *kvm; 117 struct net_dev *ndev = p; 118 u16 out, in; 119 u16 head; 120 int len; 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); 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->vtrans.trans_ops->signal_vq(kvm, &ndev->vtrans, 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 pthread_mutex_init(&ndev->io_tx_lock, NULL); 256 pthread_mutex_init(&ndev->io_rx_lock, NULL); 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 void set_config(struct kvm *kvm, void *dev, u8 data, u32 offset) 296 { 297 struct net_dev *ndev = dev; 298 299 ((u8 *)(&ndev->config))[offset] = data; 300 } 301 302 static u8 get_config(struct kvm *kvm, void *dev, u32 offset) 303 { 304 struct net_dev *ndev = dev; 305 306 return ((u8 *)(&ndev->config))[offset]; 307 } 308 309 static u32 get_host_features(struct kvm *kvm, void *dev) 310 { 311 return 1UL << VIRTIO_NET_F_MAC 312 | 1UL << VIRTIO_NET_F_CSUM 313 | 1UL << VIRTIO_NET_F_HOST_UFO 314 | 1UL << VIRTIO_NET_F_HOST_TSO4 315 | 1UL << VIRTIO_NET_F_HOST_TSO6 316 | 1UL << VIRTIO_NET_F_GUEST_UFO 317 | 1UL << VIRTIO_NET_F_GUEST_TSO4 318 | 1UL << VIRTIO_NET_F_GUEST_TSO6 319 | 1UL << VIRTIO_RING_F_EVENT_IDX 320 | 1UL << VIRTIO_RING_F_INDIRECT_DESC; 321 } 322 323 static void set_guest_features(struct kvm *kvm, void *dev, u32 features) 324 { 325 struct net_dev *ndev = dev; 326 327 ndev->features = features; 328 } 329 330 static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 pfn) 331 { 332 struct vhost_vring_state state = { .index = vq }; 333 struct vhost_vring_addr addr; 334 struct net_dev *ndev = dev; 335 struct virt_queue *queue; 336 void *p; 337 int r; 338 339 compat__remove_message(compat_id); 340 341 queue = &ndev->vqs[vq]; 342 queue->pfn = pfn; 343 p = guest_pfn_to_host(kvm, queue->pfn); 344 345 vring_init(&queue->vring, VIRTIO_NET_QUEUE_SIZE, p, VIRTIO_PCI_VRING_ALIGN); 346 347 if (ndev->vhost_fd == 0) 348 return 0; 349 350 state.num = queue->vring.num; 351 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_NUM, &state); 352 if (r < 0) 353 die_perror("VHOST_SET_VRING_NUM failed"); 354 state.num = 0; 355 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_BASE, &state); 356 if (r < 0) 357 die_perror("VHOST_SET_VRING_BASE failed"); 358 359 addr = (struct vhost_vring_addr) { 360 .index = vq, 361 .desc_user_addr = (u64)(unsigned long)queue->vring.desc, 362 .avail_user_addr = (u64)(unsigned long)queue->vring.avail, 363 .used_user_addr = (u64)(unsigned long)queue->vring.used, 364 }; 365 366 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_ADDR, &addr); 367 if (r < 0) 368 die_perror("VHOST_SET_VRING_ADDR failed"); 369 370 return 0; 371 } 372 373 static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) 374 { 375 struct net_dev *ndev = dev; 376 struct kvm_irqfd irq; 377 struct vhost_vring_file file; 378 int r; 379 380 if (ndev->vhost_fd == 0) 381 return; 382 383 irq = (struct kvm_irqfd) { 384 .gsi = gsi, 385 .fd = eventfd(0, 0), 386 }; 387 file = (struct vhost_vring_file) { 388 .index = vq, 389 .fd = irq.fd, 390 }; 391 392 r = ioctl(kvm->vm_fd, KVM_IRQFD, &irq); 393 if (r < 0) 394 die_perror("KVM_IRQFD failed"); 395 396 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_CALL, &file); 397 if (r < 0) 398 die_perror("VHOST_SET_VRING_CALL failed"); 399 file.fd = ndev->tap_fd; 400 r = ioctl(ndev->vhost_fd, VHOST_NET_SET_BACKEND, &file); 401 if (r != 0) 402 die("VHOST_NET_SET_BACKEND failed %d", errno); 403 404 } 405 406 static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) 407 { 408 struct net_dev *ndev = dev; 409 struct vhost_vring_file file = { 410 .index = vq, 411 .fd = efd, 412 }; 413 int r; 414 415 if (ndev->vhost_fd == 0) 416 return; 417 418 r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_KICK, &file); 419 if (r < 0) 420 die_perror("VHOST_SET_VRING_KICK failed"); 421 } 422 423 static int notify_vq(struct kvm *kvm, void *dev, u32 vq) 424 { 425 struct net_dev *ndev = dev; 426 427 virtio_net_handle_callback(kvm, ndev, vq); 428 429 return 0; 430 } 431 432 static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq) 433 { 434 struct net_dev *ndev = dev; 435 436 return ndev->vqs[vq].pfn; 437 } 438 439 static int get_size_vq(struct kvm *kvm, void *dev, u32 vq) 440 { 441 return VIRTIO_NET_QUEUE_SIZE; 442 } 443 444 static struct virtio_ops net_dev_virtio_ops = (struct virtio_ops) { 445 .set_config = set_config, 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 .notify_vq = notify_vq, 451 .get_pfn_vq = get_pfn_vq, 452 .get_size_vq = get_size_vq, 453 .notify_vq_gsi = notify_vq_gsi, 454 .notify_vq_eventfd = notify_vq_eventfd, 455 }; 456 457 static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev) 458 { 459 u64 features = 1UL << VIRTIO_RING_F_EVENT_IDX; 460 struct vhost_memory *mem; 461 int r; 462 463 ndev->vhost_fd = open("/dev/vhost-net", O_RDWR); 464 if (ndev->vhost_fd < 0) 465 die_perror("Failed openning vhost-net device"); 466 467 mem = malloc(sizeof(*mem) + sizeof(struct vhost_memory_region)); 468 if (mem == NULL) 469 die("Failed allocating memory for vhost memory map"); 470 471 mem->nregions = 1; 472 mem->regions[0] = (struct vhost_memory_region) { 473 .guest_phys_addr = 0, 474 .memory_size = kvm->ram_size, 475 .userspace_addr = (unsigned long)kvm->ram_start, 476 }; 477 478 r = ioctl(ndev->vhost_fd, VHOST_SET_OWNER); 479 if (r != 0) 480 die_perror("VHOST_SET_OWNER failed"); 481 482 r = ioctl(ndev->vhost_fd, VHOST_SET_FEATURES, &features); 483 if (r != 0) 484 die_perror("VHOST_SET_FEATURES failed"); 485 r = ioctl(ndev->vhost_fd, VHOST_SET_MEM_TABLE, mem); 486 if (r != 0) 487 die_perror("VHOST_SET_MEM_TABLE failed"); 488 free(mem); 489 } 490 491 void virtio_net__init(const struct virtio_net_params *params) 492 { 493 int i; 494 struct net_dev *ndev; 495 496 if (!params) 497 return; 498 499 ndev = calloc(1, sizeof(struct net_dev)); 500 if (ndev == NULL) 501 die("Failed allocating ndev"); 502 503 list_add_tail(&ndev->list, &ndevs); 504 505 ndev->kvm = params->kvm; 506 507 mutex_init(&ndev->mutex); 508 ndev->config.status = VIRTIO_NET_S_LINK_UP; 509 510 for (i = 0 ; i < 6 ; i++) { 511 ndev->config.mac[i] = params->guest_mac[i]; 512 ndev->info.guest_mac.addr[i] = params->guest_mac[i]; 513 ndev->info.host_mac.addr[i] = params->host_mac[i]; 514 } 515 516 ndev->mode = params->mode; 517 if (ndev->mode == NET_MODE_TAP) { 518 if (!virtio_net__tap_init(params, ndev)) 519 die_perror("You have requested a TAP device, but creation of one has" 520 "failed because:"); 521 ndev->ops = &tap_ops; 522 } else { 523 ndev->info.host_ip = ntohl(inet_addr(params->host_ip)); 524 ndev->info.guest_ip = ntohl(inet_addr(params->guest_ip)); 525 ndev->info.guest_netmask = ntohl(inet_addr("255.255.255.0")); 526 ndev->info.buf_nr = 20, 527 uip_init(&ndev->info); 528 ndev->ops = &uip_ops; 529 } 530 531 virtio_trans_init(&ndev->vtrans, VIRTIO_PCI); 532 ndev->vtrans.trans_ops->init(kvm, &ndev->vtrans, ndev, PCI_DEVICE_ID_VIRTIO_NET, 533 VIRTIO_ID_NET, PCI_CLASS_NET); 534 ndev->vtrans.virtio_ops = &net_dev_virtio_ops; 535 536 if (params->vhost) 537 virtio_net__vhost_init(params->kvm, ndev); 538 else 539 virtio_net__io_thread_init(params->kvm, ndev); 540 541 if (compat_id != -1) 542 compat_id = compat__add_message("virtio-net device was not detected", 543 "While you have requested a virtio-net device, " 544 "the guest kernel did not initialize it.\n" 545 "Please make sure that the guest kernel was " 546 "compiled with CONFIG_VIRTIO_NET=y enabled " 547 "in its .config"); 548 } 549