1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2016 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 #include <stdio.h> 7 #include <fcntl.h> 8 #include <string.h> 9 #include <errno.h> 10 #include <sys/mman.h> 11 #include <unistd.h> 12 #include <sys/eventfd.h> 13 #include <sys/types.h> 14 #include <sys/stat.h> 15 16 #include "vhost.h" 17 #include "virtio_user_dev.h" 18 #include "../virtio_ethdev.h" 19 20 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb" 21 22 static int 23 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel) 24 { 25 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come 26 * firstly because vhost depends on this msg to allocate virtqueue 27 * pair. 28 */ 29 struct vhost_vring_file file; 30 31 file.index = queue_sel; 32 file.fd = dev->callfds[queue_sel]; 33 dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file); 34 35 return 0; 36 } 37 38 static int 39 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel) 40 { 41 struct vhost_vring_file file; 42 struct vhost_vring_state state; 43 struct vring *vring = &dev->vrings[queue_sel]; 44 struct vhost_vring_addr addr = { 45 .index = queue_sel, 46 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc, 47 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail, 48 .used_user_addr = (uint64_t)(uintptr_t)vring->used, 49 .log_guest_addr = 0, 50 .flags = 0, /* disable log */ 51 }; 52 53 state.index = queue_sel; 54 state.num = vring->num; 55 dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state); 56 57 state.index = queue_sel; 58 state.num = 0; /* no reservation */ 59 dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state); 60 61 dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr); 62 63 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes 64 * lastly because vhost depends on this msg to judge if 65 * virtio is ready. 66 */ 67 file.index = queue_sel; 68 file.fd = dev->kickfds[queue_sel]; 69 dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file); 70 71 return 0; 72 } 73 74 static int 75 virtio_user_queue_setup(struct virtio_user_dev *dev, 76 int (*fn)(struct virtio_user_dev *, uint32_t)) 77 { 78 uint32_t i, queue_sel; 79 80 for (i = 0; i < dev->max_queue_pairs; ++i) { 81 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX; 82 if (fn(dev, queue_sel) < 0) { 83 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i); 84 return -1; 85 } 86 } 87 for (i = 0; i < dev->max_queue_pairs; ++i) { 88 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX; 89 if (fn(dev, queue_sel) < 0) { 90 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i); 91 return -1; 92 } 93 } 94 95 return 0; 96 } 97 98 int 99 is_vhost_user_by_type(const char *path) 100 { 101 struct stat sb; 102 103 if (stat(path, &sb) == -1) 104 return 0; 105 106 return S_ISSOCK(sb.st_mode); 107 } 108 109 int 110 virtio_user_start_device(struct virtio_user_dev *dev) 111 { 112 uint64_t features; 113 int ret; 114 115 pthread_mutex_lock(&dev->mutex); 116 117 if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0) 118 goto error; 119 120 /* Do not check return as already done in init, or reset in stop */ 121 dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL); 122 123 /* Step 0: tell vhost to create queues */ 124 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0) 125 goto error; 126 127 /* Step 1: set features */ 128 features = dev->features; 129 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */ 130 features &= ~(1ull << VIRTIO_NET_F_MAC); 131 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */ 132 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ); 133 features &= ~(1ull << VIRTIO_NET_F_STATUS); 134 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features); 135 if (ret < 0) 136 goto error; 137 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features); 138 139 /* Step 2: share memory regions */ 140 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL); 141 if (ret < 0) 142 goto error; 143 144 /* Step 3: kick queues */ 145 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0) 146 goto error; 147 148 /* Step 4: enable queues 149 * we enable the 1st queue pair by default. 150 */ 151 dev->ops->enable_qp(dev, 0, 1); 152 153 dev->started = true; 154 pthread_mutex_unlock(&dev->mutex); 155 156 return 0; 157 error: 158 pthread_mutex_unlock(&dev->mutex); 159 /* TODO: free resource here or caller to check */ 160 return -1; 161 } 162 163 int virtio_user_stop_device(struct virtio_user_dev *dev) 164 { 165 uint32_t i; 166 167 pthread_mutex_lock(&dev->mutex); 168 for (i = 0; i < dev->max_queue_pairs; ++i) 169 dev->ops->enable_qp(dev, i, 0); 170 171 if (dev->ops->send_request(dev, VHOST_USER_RESET_OWNER, NULL) < 0) { 172 PMD_DRV_LOG(INFO, "Failed to reset the device\n"); 173 pthread_mutex_unlock(&dev->mutex); 174 return -1; 175 } 176 dev->started = false; 177 pthread_mutex_unlock(&dev->mutex); 178 179 return 0; 180 } 181 182 static inline void 183 parse_mac(struct virtio_user_dev *dev, const char *mac) 184 { 185 int i, r; 186 uint32_t tmp[ETHER_ADDR_LEN]; 187 188 if (!mac) 189 return; 190 191 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0], 192 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]); 193 if (r == ETHER_ADDR_LEN) { 194 for (i = 0; i < ETHER_ADDR_LEN; ++i) 195 dev->mac_addr[i] = (uint8_t)tmp[i]; 196 dev->mac_specified = 1; 197 } else { 198 /* ignore the wrong mac, use random mac */ 199 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac); 200 } 201 } 202 203 static int 204 virtio_user_dev_init_notify(struct virtio_user_dev *dev) 205 { 206 uint32_t i, j; 207 int callfd; 208 int kickfd; 209 210 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) { 211 if (i >= dev->max_queue_pairs * 2) { 212 dev->kickfds[i] = -1; 213 dev->callfds[i] = -1; 214 continue; 215 } 216 217 /* May use invalid flag, but some backend uses kickfd and 218 * callfd as criteria to judge if dev is alive. so finally we 219 * use real event_fd. 220 */ 221 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); 222 if (callfd < 0) { 223 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno)); 224 break; 225 } 226 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); 227 if (kickfd < 0) { 228 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno)); 229 break; 230 } 231 dev->callfds[i] = callfd; 232 dev->kickfds[i] = kickfd; 233 } 234 235 if (i < VIRTIO_MAX_VIRTQUEUES) { 236 for (j = 0; j <= i; ++j) { 237 close(dev->callfds[j]); 238 close(dev->kickfds[j]); 239 } 240 241 return -1; 242 } 243 244 return 0; 245 } 246 247 static int 248 virtio_user_fill_intr_handle(struct virtio_user_dev *dev) 249 { 250 uint32_t i; 251 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id]; 252 253 if (!eth_dev->intr_handle) { 254 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle)); 255 if (!eth_dev->intr_handle) { 256 PMD_DRV_LOG(ERR, "fail to allocate intr_handle"); 257 return -1; 258 } 259 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle)); 260 } 261 262 for (i = 0; i < dev->max_queue_pairs; ++i) 263 eth_dev->intr_handle->efds[i] = dev->callfds[i]; 264 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs; 265 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1; 266 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV; 267 /* For virtio vdev, no need to read counter for clean */ 268 eth_dev->intr_handle->efd_counter_size = 0; 269 eth_dev->intr_handle->fd = -1; 270 if (dev->vhostfd >= 0) 271 eth_dev->intr_handle->fd = dev->vhostfd; 272 else if (dev->is_server) 273 eth_dev->intr_handle->fd = dev->listenfd; 274 275 return 0; 276 } 277 278 static void 279 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused, 280 const void *addr __rte_unused, 281 size_t len __rte_unused, 282 void *arg) 283 { 284 struct virtio_user_dev *dev = arg; 285 uint16_t i; 286 287 pthread_mutex_lock(&dev->mutex); 288 289 if (dev->started == false) 290 goto exit; 291 292 /* Step 1: pause the active queues */ 293 for (i = 0; i < dev->queue_pairs; i++) 294 dev->ops->enable_qp(dev, i, 0); 295 296 /* Step 2: update memory regions */ 297 dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL); 298 299 /* Step 3: resume the active queues */ 300 for (i = 0; i < dev->queue_pairs; i++) 301 dev->ops->enable_qp(dev, i, 1); 302 303 exit: 304 pthread_mutex_unlock(&dev->mutex); 305 } 306 307 static int 308 virtio_user_dev_setup(struct virtio_user_dev *dev) 309 { 310 uint32_t q; 311 312 dev->vhostfd = -1; 313 dev->vhostfds = NULL; 314 dev->tapfds = NULL; 315 316 if (dev->is_server) { 317 if (access(dev->path, F_OK) == 0 && 318 !is_vhost_user_by_type(dev->path)) { 319 PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!"); 320 return -1; 321 } 322 dev->ops = &ops_user; 323 } else { 324 if (is_vhost_user_by_type(dev->path)) { 325 dev->ops = &ops_user; 326 } else { 327 dev->ops = &ops_kernel; 328 329 dev->vhostfds = malloc(dev->max_queue_pairs * 330 sizeof(int)); 331 dev->tapfds = malloc(dev->max_queue_pairs * 332 sizeof(int)); 333 if (!dev->vhostfds || !dev->tapfds) { 334 PMD_INIT_LOG(ERR, "Failed to malloc"); 335 return -1; 336 } 337 338 for (q = 0; q < dev->max_queue_pairs; ++q) { 339 dev->vhostfds[q] = -1; 340 dev->tapfds[q] = -1; 341 } 342 } 343 } 344 345 if (dev->ops->setup(dev) < 0) 346 return -1; 347 348 if (virtio_user_dev_init_notify(dev) < 0) 349 return -1; 350 351 if (virtio_user_fill_intr_handle(dev) < 0) 352 return -1; 353 354 return 0; 355 } 356 357 /* Use below macro to filter features from vhost backend */ 358 #define VIRTIO_USER_SUPPORTED_FEATURES \ 359 (1ULL << VIRTIO_NET_F_MAC | \ 360 1ULL << VIRTIO_NET_F_STATUS | \ 361 1ULL << VIRTIO_NET_F_MQ | \ 362 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \ 363 1ULL << VIRTIO_NET_F_CTRL_VQ | \ 364 1ULL << VIRTIO_NET_F_CTRL_RX | \ 365 1ULL << VIRTIO_NET_F_CTRL_VLAN | \ 366 1ULL << VIRTIO_NET_F_CSUM | \ 367 1ULL << VIRTIO_NET_F_HOST_TSO4 | \ 368 1ULL << VIRTIO_NET_F_HOST_TSO6 | \ 369 1ULL << VIRTIO_NET_F_MRG_RXBUF | \ 370 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \ 371 1ULL << VIRTIO_NET_F_GUEST_CSUM | \ 372 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \ 373 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \ 374 1ULL << VIRTIO_F_VERSION_1) 375 376 int 377 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues, 378 int cq, int queue_size, const char *mac, char **ifname) 379 { 380 pthread_mutex_init(&dev->mutex, NULL); 381 snprintf(dev->path, PATH_MAX, "%s", path); 382 dev->started = 0; 383 dev->max_queue_pairs = queues; 384 dev->queue_pairs = 1; /* mq disabled by default */ 385 dev->queue_size = queue_size; 386 dev->mac_specified = 0; 387 parse_mac(dev, mac); 388 389 if (*ifname) { 390 dev->ifname = *ifname; 391 *ifname = NULL; 392 } 393 394 if (virtio_user_dev_setup(dev) < 0) { 395 PMD_INIT_LOG(ERR, "backend set up fails"); 396 return -1; 397 } 398 399 if (!dev->is_server) { 400 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, 401 NULL) < 0) { 402 PMD_INIT_LOG(ERR, "set_owner fails: %s", 403 strerror(errno)); 404 return -1; 405 } 406 407 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES, 408 &dev->device_features) < 0) { 409 PMD_INIT_LOG(ERR, "get_features failed: %s", 410 strerror(errno)); 411 return -1; 412 } 413 } else { 414 /* We just pretend vhost-user can support all these features. 415 * Note that this could be problematic that if some feature is 416 * negotiated but not supported by the vhost-user which comes 417 * later. 418 */ 419 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES; 420 } 421 422 if (dev->mac_specified) 423 dev->device_features |= (1ull << VIRTIO_NET_F_MAC); 424 else 425 dev->device_features &= ~(1ull << VIRTIO_NET_F_MAC); 426 427 if (cq) { 428 /* device does not really need to know anything about CQ, 429 * so if necessary, we just claim to support CQ 430 */ 431 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ); 432 } else { 433 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ); 434 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */ 435 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX); 436 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN); 437 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE); 438 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ); 439 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR); 440 } 441 442 /* The backend will not report this feature, we add it explicitly */ 443 if (is_vhost_user_by_type(dev->path)) 444 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS); 445 446 dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES; 447 448 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME, 449 virtio_user_mem_event_cb, dev)) { 450 if (rte_errno != ENOTSUP) { 451 PMD_INIT_LOG(ERR, "Failed to register mem event" 452 " callback\n"); 453 return -1; 454 } 455 } 456 457 return 0; 458 } 459 460 void 461 virtio_user_dev_uninit(struct virtio_user_dev *dev) 462 { 463 uint32_t i; 464 465 virtio_user_stop_device(dev); 466 467 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev); 468 469 for (i = 0; i < dev->max_queue_pairs * 2; ++i) { 470 close(dev->callfds[i]); 471 close(dev->kickfds[i]); 472 } 473 474 close(dev->vhostfd); 475 476 if (dev->is_server && dev->listenfd >= 0) { 477 close(dev->listenfd); 478 dev->listenfd = -1; 479 } 480 481 if (dev->vhostfds) { 482 for (i = 0; i < dev->max_queue_pairs; ++i) 483 close(dev->vhostfds[i]); 484 free(dev->vhostfds); 485 free(dev->tapfds); 486 } 487 488 free(dev->ifname); 489 490 if (dev->is_server) 491 unlink(dev->path); 492 } 493 494 uint8_t 495 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs) 496 { 497 uint16_t i; 498 uint8_t ret = 0; 499 500 if (q_pairs > dev->max_queue_pairs) { 501 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported", 502 q_pairs, dev->max_queue_pairs); 503 return -1; 504 } 505 506 /* Server mode can't enable queue pairs if vhostfd is invalid, 507 * always return 0 in this case. 508 */ 509 if (dev->vhostfd >= 0) { 510 for (i = 0; i < q_pairs; ++i) 511 ret |= dev->ops->enable_qp(dev, i, 1); 512 for (i = q_pairs; i < dev->max_queue_pairs; ++i) 513 ret |= dev->ops->enable_qp(dev, i, 0); 514 } else if (!dev->is_server) { 515 ret = ~0; 516 } 517 dev->queue_pairs = q_pairs; 518 519 return ret; 520 } 521 522 static uint32_t 523 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring, 524 uint16_t idx_hdr) 525 { 526 struct virtio_net_ctrl_hdr *hdr; 527 virtio_net_ctrl_ack status = ~0; 528 uint16_t i, idx_data, idx_status; 529 uint32_t n_descs = 0; 530 531 /* locate desc for header, data, and status */ 532 idx_data = vring->desc[idx_hdr].next; 533 n_descs++; 534 535 i = idx_data; 536 while (vring->desc[i].flags == VRING_DESC_F_NEXT) { 537 i = vring->desc[i].next; 538 n_descs++; 539 } 540 541 /* locate desc for status */ 542 idx_status = i; 543 n_descs++; 544 545 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr; 546 if (hdr->class == VIRTIO_NET_CTRL_MQ && 547 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 548 uint16_t queues; 549 550 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr; 551 status = virtio_user_handle_mq(dev, queues); 552 } 553 554 /* Update status */ 555 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status; 556 557 return n_descs; 558 } 559 560 void 561 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx) 562 { 563 uint16_t avail_idx, desc_idx; 564 struct vring_used_elem *uep; 565 uint32_t n_descs; 566 struct vring *vring = &dev->vrings[queue_idx]; 567 568 /* Consume avail ring, using used ring idx as first one */ 569 while (vring->used->idx != vring->avail->idx) { 570 avail_idx = (vring->used->idx) & (vring->num - 1); 571 desc_idx = vring->avail->ring[avail_idx]; 572 573 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx); 574 575 /* Update used ring */ 576 uep = &vring->used->ring[avail_idx]; 577 uep->id = avail_idx; 578 uep->len = n_descs; 579 580 vring->used->idx++; 581 } 582 } 583