1 #include "kvm/virtio-pci-dev.h" 2 #include "kvm/virtio-net.h" 3 #include "kvm/virtio.h" 4 #include "kvm/ioport.h" 5 #include "kvm/types.h" 6 #include "kvm/mutex.h" 7 #include "kvm/util.h" 8 #include "kvm/kvm.h" 9 #include "kvm/pci.h" 10 #include "kvm/irq.h" 11 #include "kvm/uip.h" 12 #include "kvm/ioeventfd.h" 13 14 #include <linux/virtio_net.h> 15 #include <linux/if_tun.h> 16 17 #include <arpa/inet.h> 18 #include <net/if.h> 19 20 #include <unistd.h> 21 #include <assert.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 29 #define VIRTIO_NET_QUEUE_SIZE 128 30 #define VIRTIO_NET_NUM_QUEUES 2 31 #define VIRTIO_NET_RX_QUEUE 0 32 #define VIRTIO_NET_TX_QUEUE 1 33 34 static struct pci_device_header pci_header = { 35 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, 36 .device_id = PCI_DEVICE_ID_VIRTIO_NET, 37 .header_type = PCI_HEADER_TYPE_NORMAL, 38 .revision_id = 0, 39 .class = 0x020000, 40 .subsys_vendor_id = PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET, 41 .subsys_id = VIRTIO_ID_NET, 42 }; 43 44 struct net_dev; 45 46 struct net_dev_operations { 47 int (*rx)(struct iovec *iov, u16 in, struct net_dev *ndev); 48 int (*tx)(struct iovec *iov, u16 in, struct net_dev *ndev); 49 }; 50 51 struct net_dev { 52 pthread_mutex_t mutex; 53 54 struct virt_queue vqs[VIRTIO_NET_NUM_QUEUES]; 55 struct virtio_net_config config; 56 u32 host_features; 57 u32 guest_features; 58 u16 config_vector; 59 u8 status; 60 u8 isr; 61 u16 queue_selector; 62 u16 base_addr; 63 u32 vq_vector[VIRTIO_NET_NUM_QUEUES]; 64 u32 gsis[VIRTIO_NET_NUM_QUEUES]; 65 u32 msix_io_block; 66 67 pthread_t io_rx_thread; 68 pthread_mutex_t io_rx_lock; 69 pthread_cond_t io_rx_cond; 70 71 pthread_t io_tx_thread; 72 pthread_mutex_t io_tx_lock; 73 pthread_cond_t io_tx_cond; 74 75 int tap_fd; 76 char tap_name[IFNAMSIZ]; 77 78 int mode; 79 80 struct uip_info info; 81 struct net_dev_operations *ops; 82 }; 83 84 static struct net_dev ndev = { 85 .mutex = PTHREAD_MUTEX_INITIALIZER, 86 87 .config = { 88 .status = VIRTIO_NET_S_LINK_UP, 89 }, 90 .host_features = 1UL << VIRTIO_NET_F_MAC 91 | 1UL << VIRTIO_NET_F_CSUM 92 | 1UL << VIRTIO_NET_F_HOST_UFO 93 | 1UL << VIRTIO_NET_F_HOST_TSO4 94 | 1UL << VIRTIO_NET_F_HOST_TSO6 95 | 1UL << VIRTIO_NET_F_GUEST_UFO 96 | 1UL << VIRTIO_NET_F_GUEST_TSO4 97 | 1UL << VIRTIO_NET_F_GUEST_TSO6, 98 .info = { 99 .buf_nr = 20, 100 } 101 }; 102 103 static void *virtio_net_rx_thread(void *p) 104 { 105 struct iovec iov[VIRTIO_NET_QUEUE_SIZE]; 106 struct virt_queue *vq; 107 struct kvm *kvm; 108 u16 out, in; 109 u16 head; 110 int len; 111 112 kvm = p; 113 vq = &ndev.vqs[VIRTIO_NET_RX_QUEUE]; 114 115 while (1) { 116 117 mutex_lock(&ndev.io_rx_lock); 118 if (!virt_queue__available(vq)) 119 pthread_cond_wait(&ndev.io_rx_cond, &ndev.io_rx_lock); 120 mutex_unlock(&ndev.io_rx_lock); 121 122 while (virt_queue__available(vq)) { 123 124 head = virt_queue__get_iov(vq, iov, &out, &in, kvm); 125 126 len = ndev.ops->rx(iov, in, &ndev); 127 128 virt_queue__set_used_elem(vq, head, len); 129 130 /* We should interrupt guest right now, otherwise latency is huge. */ 131 kvm__irq_trigger(kvm, ndev.gsis[VIRTIO_NET_RX_QUEUE]); 132 } 133 134 } 135 136 pthread_exit(NULL); 137 return NULL; 138 139 } 140 141 static void *virtio_net_tx_thread(void *p) 142 { 143 struct iovec iov[VIRTIO_NET_QUEUE_SIZE]; 144 struct virt_queue *vq; 145 struct kvm *kvm; 146 u16 out, in; 147 u16 head; 148 int len; 149 150 kvm = p; 151 vq = &ndev.vqs[VIRTIO_NET_TX_QUEUE]; 152 153 while (1) { 154 mutex_lock(&ndev.io_tx_lock); 155 if (!virt_queue__available(vq)) 156 pthread_cond_wait(&ndev.io_tx_cond, &ndev.io_tx_lock); 157 mutex_unlock(&ndev.io_tx_lock); 158 159 while (virt_queue__available(vq)) { 160 161 head = virt_queue__get_iov(vq, iov, &out, &in, kvm); 162 163 len = ndev.ops->tx(iov, out, &ndev); 164 165 virt_queue__set_used_elem(vq, head, len); 166 } 167 168 kvm__irq_trigger(kvm, ndev.gsis[VIRTIO_NET_TX_QUEUE]); 169 } 170 171 pthread_exit(NULL); 172 173 return NULL; 174 175 } 176 177 static bool virtio_net_pci_io_device_specific_in(void *data, unsigned long offset, int size, u32 count) 178 { 179 u8 *config_space = (u8 *)&ndev.config; 180 181 if (size != 1 || count != 1) 182 return false; 183 184 if ((offset - VIRTIO_MSI_CONFIG_VECTOR) > sizeof(struct virtio_net_config)) 185 pr_error("config offset is too big: %li", offset - VIRTIO_MSI_CONFIG_VECTOR); 186 187 ioport__write8(data, config_space[offset - VIRTIO_MSI_CONFIG_VECTOR]); 188 189 return true; 190 } 191 192 static bool virtio_net_pci_io_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count) 193 { 194 unsigned long offset = port - ndev.base_addr; 195 bool ret = true; 196 197 mutex_lock(&ndev.mutex); 198 199 switch (offset) { 200 case VIRTIO_PCI_HOST_FEATURES: 201 ioport__write32(data, ndev.host_features); 202 break; 203 case VIRTIO_PCI_GUEST_FEATURES: 204 ret = false; 205 break; 206 case VIRTIO_PCI_QUEUE_PFN: 207 ioport__write32(data, ndev.vqs[ndev.queue_selector].pfn); 208 break; 209 case VIRTIO_PCI_QUEUE_NUM: 210 ioport__write16(data, VIRTIO_NET_QUEUE_SIZE); 211 break; 212 case VIRTIO_PCI_QUEUE_SEL: 213 case VIRTIO_PCI_QUEUE_NOTIFY: 214 ret = false; 215 break; 216 case VIRTIO_PCI_STATUS: 217 ioport__write8(data, ndev.status); 218 break; 219 case VIRTIO_PCI_ISR: 220 ioport__write8(data, ndev.isr); 221 kvm__irq_line(kvm, pci_header.irq_line, VIRTIO_IRQ_LOW); 222 ndev.isr = VIRTIO_IRQ_LOW; 223 break; 224 default: 225 ret = virtio_net_pci_io_device_specific_in(data, offset, size, count); 226 }; 227 228 mutex_unlock(&ndev.mutex); 229 230 return ret; 231 } 232 233 static void virtio_net_handle_callback(struct kvm *kvm, u16 queue_index) 234 { 235 switch (queue_index) { 236 case VIRTIO_NET_TX_QUEUE: 237 mutex_lock(&ndev.io_tx_lock); 238 pthread_cond_signal(&ndev.io_tx_cond); 239 mutex_unlock(&ndev.io_tx_lock); 240 break; 241 case VIRTIO_NET_RX_QUEUE: 242 mutex_lock(&ndev.io_rx_lock); 243 pthread_cond_signal(&ndev.io_rx_cond); 244 mutex_unlock(&ndev.io_rx_lock); 245 break; 246 default: 247 pr_warning("Unknown queue index %u", queue_index); 248 } 249 } 250 251 static bool virtio_net_pci_io_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count) 252 { 253 unsigned long offset = port - ndev.base_addr; 254 bool ret = true; 255 256 mutex_lock(&ndev.mutex); 257 258 switch (offset) { 259 case VIRTIO_PCI_GUEST_FEATURES: 260 ndev.guest_features = ioport__read32(data); 261 break; 262 case VIRTIO_PCI_QUEUE_PFN: { 263 struct virt_queue *queue; 264 void *p; 265 266 assert(ndev.queue_selector < VIRTIO_NET_NUM_QUEUES); 267 268 queue = &ndev.vqs[ndev.queue_selector]; 269 queue->pfn = ioport__read32(data); 270 p = guest_pfn_to_host(kvm, queue->pfn); 271 272 vring_init(&queue->vring, VIRTIO_NET_QUEUE_SIZE, p, VIRTIO_PCI_VRING_ALIGN); 273 274 break; 275 } 276 case VIRTIO_PCI_QUEUE_SEL: 277 ndev.queue_selector = ioport__read16(data); 278 break; 279 case VIRTIO_PCI_QUEUE_NOTIFY: { 280 u16 queue_index; 281 282 queue_index = ioport__read16(data); 283 virtio_net_handle_callback(kvm, queue_index); 284 break; 285 } 286 case VIRTIO_PCI_STATUS: 287 ndev.status = ioport__read8(data); 288 break; 289 case VIRTIO_MSI_CONFIG_VECTOR: 290 ndev.config_vector = ioport__read16(data); 291 break; 292 case VIRTIO_MSI_QUEUE_VECTOR: { 293 u32 gsi; 294 u32 vec; 295 296 vec = ndev.vq_vector[ndev.queue_selector] = ioport__read16(data); 297 298 gsi = irq__add_msix_route(kvm, 299 pci_header.msix.table[vec].low, 300 pci_header.msix.table[vec].high, 301 pci_header.msix.table[vec].data); 302 303 ndev.gsis[ndev.queue_selector] = gsi; 304 break; 305 } 306 default: 307 ret = false; 308 }; 309 310 mutex_unlock(&ndev.mutex); 311 312 return ret; 313 } 314 315 static void ioevent_callback(struct kvm *kvm, void *param) 316 { 317 virtio_net_handle_callback(kvm, (u64)(long)param); 318 } 319 320 static struct ioport_operations virtio_net_io_ops = { 321 .io_in = virtio_net_pci_io_in, 322 .io_out = virtio_net_pci_io_out, 323 }; 324 325 static void callback_mmio(u64 addr, u8 *data, u32 len, u8 is_write, void *ptr) 326 { 327 void *table = pci_header.msix.table; 328 if (is_write) 329 memcpy(table + addr - ndev.msix_io_block, data, len); 330 else 331 memcpy(data, table + addr - ndev.msix_io_block, len); 332 } 333 334 static bool virtio_net__tap_init(const struct virtio_net_parameters *params) 335 { 336 int sock = socket(AF_INET, SOCK_STREAM, 0); 337 int pid, status, offload, hdr_len; 338 struct sockaddr_in sin = {0}; 339 struct ifreq ifr; 340 341 ndev.tap_fd = open("/dev/net/tun", O_RDWR); 342 if (ndev.tap_fd < 0) { 343 pr_warning("Unable to open /dev/net/tun"); 344 goto fail; 345 } 346 347 memset(&ifr, 0, sizeof(ifr)); 348 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR; 349 if (ioctl(ndev.tap_fd, TUNSETIFF, &ifr) < 0) { 350 pr_warning("Config tap device error. Are you root?"); 351 goto fail; 352 } 353 354 strncpy(ndev.tap_name, ifr.ifr_name, sizeof(ndev.tap_name)); 355 356 if (ioctl(ndev.tap_fd, TUNSETNOCSUM, 1) < 0) { 357 pr_warning("Config tap device TUNSETNOCSUM error"); 358 goto fail; 359 } 360 361 hdr_len = sizeof(struct virtio_net_hdr); 362 if (ioctl(ndev.tap_fd, TUNSETVNETHDRSZ, &hdr_len) < 0) { 363 pr_warning("Config tap device TUNSETVNETHDRSZ error"); 364 } 365 366 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | TUN_F_UFO; 367 if (ioctl(ndev.tap_fd, TUNSETOFFLOAD, offload) < 0) { 368 pr_warning("Config tap device TUNSETOFFLOAD error"); 369 goto fail; 370 } 371 372 if (strcmp(params->script, "none")) { 373 pid = fork(); 374 if (pid == 0) { 375 execl(params->script, params->script, ndev.tap_name, NULL); 376 _exit(1); 377 } else { 378 waitpid(pid, &status, 0); 379 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { 380 pr_warning("Fail to setup tap by %s", params->script); 381 goto fail; 382 } 383 } 384 } else { 385 memset(&ifr, 0, sizeof(ifr)); 386 strncpy(ifr.ifr_name, ndev.tap_name, sizeof(ndev.tap_name)); 387 sin.sin_addr.s_addr = inet_addr(params->host_ip); 388 memcpy(&(ifr.ifr_addr), &sin, sizeof(ifr.ifr_addr)); 389 ifr.ifr_addr.sa_family = AF_INET; 390 if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) { 391 pr_warning("Could not set ip address on tap device"); 392 goto fail; 393 } 394 } 395 396 memset(&ifr, 0, sizeof(ifr)); 397 strncpy(ifr.ifr_name, ndev.tap_name, sizeof(ndev.tap_name)); 398 ioctl(sock, SIOCGIFFLAGS, &ifr); 399 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 400 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) 401 pr_warning("Could not bring tap device up"); 402 403 close(sock); 404 405 return 1; 406 407 fail: 408 if (sock >= 0) 409 close(sock); 410 if (ndev.tap_fd >= 0) 411 close(ndev.tap_fd); 412 413 return 0; 414 } 415 416 static void virtio_net__io_thread_init(struct kvm *kvm) 417 { 418 pthread_mutex_init(&ndev.io_rx_lock, NULL); 419 pthread_cond_init(&ndev.io_tx_cond, NULL); 420 421 pthread_mutex_init(&ndev.io_rx_lock, NULL); 422 pthread_cond_init(&ndev.io_tx_cond, NULL); 423 424 pthread_create(&ndev.io_rx_thread, NULL, virtio_net_rx_thread, (void *)kvm); 425 pthread_create(&ndev.io_tx_thread, NULL, virtio_net_tx_thread, (void *)kvm); 426 } 427 428 static inline int tap_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev) 429 { 430 return writev(ndev->tap_fd, iov, out); 431 } 432 433 static inline int tap_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev) 434 { 435 return readv(ndev->tap_fd, iov, in); 436 } 437 438 static inline int uip_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev) 439 { 440 return uip_tx(iov, out, &ndev->info); 441 } 442 443 static inline int uip_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev) 444 { 445 return uip_rx(iov, in, &ndev->info); 446 } 447 448 static struct net_dev_operations tap_ops = { 449 .rx = tap_ops_rx, 450 .tx = tap_ops_tx, 451 }; 452 453 static struct net_dev_operations uip_ops = { 454 .rx = uip_ops_rx, 455 .tx = uip_ops_tx, 456 }; 457 458 void virtio_net__init(const struct virtio_net_parameters *params) 459 { 460 struct ioevent ioevent; 461 u8 dev, line, pin; 462 u16 net_base_addr; 463 int i; 464 465 if (irq__register_device(VIRTIO_ID_NET, &dev, &pin, &line) < 0) 466 return; 467 468 pci_header.irq_pin = pin; 469 pci_header.irq_line = line; 470 net_base_addr = ioport__register(IOPORT_EMPTY, &virtio_net_io_ops, IOPORT_SIZE, NULL); 471 pci_header.bar[0] = net_base_addr | PCI_BASE_ADDRESS_SPACE_IO; 472 ndev.base_addr = net_base_addr; 473 pci__register(&pci_header, dev); 474 475 for (i = 0 ; i < 6 ; i++) { 476 ndev.config.mac[i] = params->guest_mac[i]; 477 ndev.info.guest_mac.addr[i] = params->guest_mac[i]; 478 ndev.info.host_mac.addr[i] = params->host_mac[i]; 479 } 480 481 ndev.mode = params->mode; 482 if (ndev.mode == NET_MODE_TAP) { 483 virtio_net__tap_init(params); 484 ndev.ops = &tap_ops; 485 } else { 486 ndev.info.host_ip = ntohl(inet_addr(params->host_ip)); 487 ndev.info.guest_ip = ntohl(inet_addr(params->guest_ip)); 488 ndev.info.guest_netmask = ntohl(inet_addr("255.255.255.0")); 489 uip_init(&ndev.info); 490 ndev.ops = &uip_ops; 491 } 492 493 ndev.msix_io_block = pci_get_io_space_block(); 494 kvm__register_mmio(params->kvm, ndev.msix_io_block, 0x100, callback_mmio, NULL); 495 pci_header.bar[1] = ndev.msix_io_block | 496 PCI_BASE_ADDRESS_SPACE_MEMORY | 497 PCI_BASE_ADDRESS_MEM_TYPE_64; 498 /* bar[2] is the continuation of bar[1] for 64bit addressing */ 499 pci_header.bar[2] = 0; 500 pci_header.status = PCI_STATUS_CAP_LIST; 501 pci_header.capabilities = (void *)&pci_header.msix - (void *)&pci_header; 502 503 pci_header.msix.cap = PCI_CAP_ID_MSIX; 504 pci_header.msix.next = 0; 505 pci_header.msix.table_size = (VIRTIO_NET_NUM_QUEUES + 1) | PCI_MSIX_FLAGS_ENABLE; 506 pci_header.msix.table_offset = 1; /* Use BAR 1 */ 507 508 virtio_net__io_thread_init(params->kvm); 509 510 for (i = 0; i < VIRTIO_NET_NUM_QUEUES; i++) { 511 ioevent = (struct ioevent) { 512 .io_addr = net_base_addr + VIRTIO_PCI_QUEUE_NOTIFY, 513 .io_len = sizeof(u16), 514 .fn = ioevent_callback, 515 .datamatch = i, 516 .fn_ptr = (void *)(long)i, 517 .fn_kvm = params->kvm, 518 .fd = eventfd(0, 0), 519 }; 520 521 ioeventfd__add_event(&ioevent); 522 } 523 } 524