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