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 <rte_string_fns.h> 17 #include <rte_eal_memconfig.h> 18 19 #include "vhost.h" 20 #include "virtio_user_dev.h" 21 #include "../virtio_ethdev.h" 22 23 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb" 24 25 static int 26 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel) 27 { 28 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come 29 * firstly because vhost depends on this msg to allocate virtqueue 30 * pair. 31 */ 32 struct vhost_vring_file file; 33 34 file.index = queue_sel; 35 file.fd = dev->callfds[queue_sel]; 36 dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file); 37 38 return 0; 39 } 40 41 static int 42 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel) 43 { 44 struct vhost_vring_file file; 45 struct vhost_vring_state state; 46 struct vring *vring = &dev->vrings[queue_sel]; 47 struct vring_packed *pq_vring = &dev->packed_vrings[queue_sel]; 48 struct vhost_vring_addr addr = { 49 .index = queue_sel, 50 .log_guest_addr = 0, 51 .flags = 0, /* disable log */ 52 }; 53 54 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) { 55 addr.desc_user_addr = 56 (uint64_t)(uintptr_t)pq_vring->desc; 57 addr.avail_user_addr = 58 (uint64_t)(uintptr_t)pq_vring->driver; 59 addr.used_user_addr = 60 (uint64_t)(uintptr_t)pq_vring->device; 61 } else { 62 addr.desc_user_addr = (uint64_t)(uintptr_t)vring->desc; 63 addr.avail_user_addr = (uint64_t)(uintptr_t)vring->avail; 64 addr.used_user_addr = (uint64_t)(uintptr_t)vring->used; 65 } 66 67 state.index = queue_sel; 68 state.num = vring->num; 69 dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state); 70 71 state.index = queue_sel; 72 state.num = 0; /* no reservation */ 73 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) 74 state.num |= (1 << 15); 75 dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state); 76 77 dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr); 78 79 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes 80 * lastly because vhost depends on this msg to judge if 81 * virtio is ready. 82 */ 83 file.index = queue_sel; 84 file.fd = dev->kickfds[queue_sel]; 85 dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file); 86 87 return 0; 88 } 89 90 static int 91 virtio_user_queue_setup(struct virtio_user_dev *dev, 92 int (*fn)(struct virtio_user_dev *, uint32_t)) 93 { 94 uint32_t i, queue_sel; 95 96 for (i = 0; i < dev->max_queue_pairs; ++i) { 97 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX; 98 if (fn(dev, queue_sel) < 0) { 99 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i); 100 return -1; 101 } 102 } 103 for (i = 0; i < dev->max_queue_pairs; ++i) { 104 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX; 105 if (fn(dev, queue_sel) < 0) { 106 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i); 107 return -1; 108 } 109 } 110 111 return 0; 112 } 113 114 int 115 is_vhost_user_by_type(const char *path) 116 { 117 struct stat sb; 118 119 if (stat(path, &sb) == -1) 120 return 0; 121 122 return S_ISSOCK(sb.st_mode); 123 } 124 125 int 126 virtio_user_start_device(struct virtio_user_dev *dev) 127 { 128 uint64_t features; 129 int ret; 130 131 /* 132 * XXX workaround! 133 * 134 * We need to make sure that the locks will be 135 * taken in the correct order to avoid deadlocks. 136 * 137 * Before releasing this lock, this thread should 138 * not trigger any memory hotplug events. 139 * 140 * This is a temporary workaround, and should be 141 * replaced when we get proper supports from the 142 * memory subsystem in the future. 143 */ 144 rte_mcfg_mem_read_lock(); 145 pthread_mutex_lock(&dev->mutex); 146 147 if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0) 148 goto error; 149 150 /* Step 0: tell vhost to create queues */ 151 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0) 152 goto error; 153 154 /* Step 1: set features */ 155 features = dev->features; 156 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */ 157 features &= ~(1ull << VIRTIO_NET_F_MAC); 158 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */ 159 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ); 160 features &= ~(1ull << VIRTIO_NET_F_STATUS); 161 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features); 162 if (ret < 0) 163 goto error; 164 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features); 165 166 /* Step 2: share memory regions */ 167 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL); 168 if (ret < 0) 169 goto error; 170 171 /* Step 3: kick queues */ 172 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0) 173 goto error; 174 175 /* Step 4: enable queues 176 * we enable the 1st queue pair by default. 177 */ 178 dev->ops->enable_qp(dev, 0, 1); 179 180 dev->started = true; 181 pthread_mutex_unlock(&dev->mutex); 182 rte_mcfg_mem_read_unlock(); 183 184 return 0; 185 error: 186 pthread_mutex_unlock(&dev->mutex); 187 rte_mcfg_mem_read_unlock(); 188 /* TODO: free resource here or caller to check */ 189 return -1; 190 } 191 192 int virtio_user_stop_device(struct virtio_user_dev *dev) 193 { 194 struct vhost_vring_state state; 195 uint32_t i; 196 int error = 0; 197 198 pthread_mutex_lock(&dev->mutex); 199 if (!dev->started) 200 goto out; 201 202 for (i = 0; i < dev->max_queue_pairs; ++i) 203 dev->ops->enable_qp(dev, i, 0); 204 205 /* Stop the backend. */ 206 for (i = 0; i < dev->max_queue_pairs * 2; ++i) { 207 state.index = i; 208 if (dev->ops->send_request(dev, VHOST_USER_GET_VRING_BASE, 209 &state) < 0) { 210 PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u\n", 211 i); 212 error = -1; 213 goto out; 214 } 215 } 216 217 dev->started = false; 218 out: 219 pthread_mutex_unlock(&dev->mutex); 220 221 return error; 222 } 223 224 static inline void 225 parse_mac(struct virtio_user_dev *dev, const char *mac) 226 { 227 struct rte_ether_addr tmp; 228 229 if (!mac) 230 return; 231 232 if (rte_ether_unformat_addr(mac, &tmp) == 0) { 233 memcpy(dev->mac_addr, &tmp, RTE_ETHER_ADDR_LEN); 234 dev->mac_specified = 1; 235 } else { 236 /* ignore the wrong mac, use random mac */ 237 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac); 238 } 239 } 240 241 static int 242 virtio_user_dev_init_notify(struct virtio_user_dev *dev) 243 { 244 uint32_t i, j; 245 int callfd; 246 int kickfd; 247 248 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) { 249 if (i >= dev->max_queue_pairs * 2) { 250 dev->kickfds[i] = -1; 251 dev->callfds[i] = -1; 252 continue; 253 } 254 255 /* May use invalid flag, but some backend uses kickfd and 256 * callfd as criteria to judge if dev is alive. so finally we 257 * use real event_fd. 258 */ 259 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); 260 if (callfd < 0) { 261 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno)); 262 break; 263 } 264 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); 265 if (kickfd < 0) { 266 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno)); 267 break; 268 } 269 dev->callfds[i] = callfd; 270 dev->kickfds[i] = kickfd; 271 } 272 273 if (i < VIRTIO_MAX_VIRTQUEUES) { 274 for (j = 0; j <= i; ++j) { 275 close(dev->callfds[j]); 276 close(dev->kickfds[j]); 277 } 278 279 return -1; 280 } 281 282 return 0; 283 } 284 285 static int 286 virtio_user_fill_intr_handle(struct virtio_user_dev *dev) 287 { 288 uint32_t i; 289 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id]; 290 291 if (!eth_dev->intr_handle) { 292 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle)); 293 if (!eth_dev->intr_handle) { 294 PMD_DRV_LOG(ERR, "fail to allocate intr_handle"); 295 return -1; 296 } 297 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle)); 298 } 299 300 for (i = 0; i < dev->max_queue_pairs; ++i) 301 eth_dev->intr_handle->efds[i] = dev->callfds[i]; 302 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs; 303 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1; 304 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV; 305 /* For virtio vdev, no need to read counter for clean */ 306 eth_dev->intr_handle->efd_counter_size = 0; 307 eth_dev->intr_handle->fd = -1; 308 if (dev->vhostfd >= 0) 309 eth_dev->intr_handle->fd = dev->vhostfd; 310 else if (dev->is_server) 311 eth_dev->intr_handle->fd = dev->listenfd; 312 313 return 0; 314 } 315 316 static void 317 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused, 318 const void *addr, 319 size_t len __rte_unused, 320 void *arg) 321 { 322 struct virtio_user_dev *dev = arg; 323 struct rte_memseg_list *msl; 324 uint16_t i; 325 326 /* ignore externally allocated memory */ 327 msl = rte_mem_virt2memseg_list(addr); 328 if (msl->external) 329 return; 330 331 pthread_mutex_lock(&dev->mutex); 332 333 if (dev->started == false) 334 goto exit; 335 336 /* Step 1: pause the active queues */ 337 for (i = 0; i < dev->queue_pairs; i++) 338 dev->ops->enable_qp(dev, i, 0); 339 340 /* Step 2: update memory regions */ 341 dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL); 342 343 /* Step 3: resume the active queues */ 344 for (i = 0; i < dev->queue_pairs; i++) 345 dev->ops->enable_qp(dev, i, 1); 346 347 exit: 348 pthread_mutex_unlock(&dev->mutex); 349 } 350 351 static int 352 virtio_user_dev_setup(struct virtio_user_dev *dev) 353 { 354 uint32_t q; 355 356 dev->vhostfd = -1; 357 dev->vhostfds = NULL; 358 dev->tapfds = NULL; 359 360 if (dev->is_server) { 361 if (access(dev->path, F_OK) == 0 && 362 !is_vhost_user_by_type(dev->path)) { 363 PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!"); 364 return -1; 365 } 366 dev->ops = &virtio_ops_user; 367 } else { 368 if (is_vhost_user_by_type(dev->path)) { 369 dev->ops = &virtio_ops_user; 370 } else { 371 dev->ops = &virtio_ops_kernel; 372 373 dev->vhostfds = malloc(dev->max_queue_pairs * 374 sizeof(int)); 375 dev->tapfds = malloc(dev->max_queue_pairs * 376 sizeof(int)); 377 if (!dev->vhostfds || !dev->tapfds) { 378 PMD_INIT_LOG(ERR, "Failed to malloc"); 379 return -1; 380 } 381 382 for (q = 0; q < dev->max_queue_pairs; ++q) { 383 dev->vhostfds[q] = -1; 384 dev->tapfds[q] = -1; 385 } 386 } 387 } 388 389 if (dev->ops->setup(dev) < 0) 390 return -1; 391 392 if (virtio_user_dev_init_notify(dev) < 0) 393 return -1; 394 395 if (virtio_user_fill_intr_handle(dev) < 0) 396 return -1; 397 398 return 0; 399 } 400 401 /* Use below macro to filter features from vhost backend */ 402 #define VIRTIO_USER_SUPPORTED_FEATURES \ 403 (1ULL << VIRTIO_NET_F_MAC | \ 404 1ULL << VIRTIO_NET_F_STATUS | \ 405 1ULL << VIRTIO_NET_F_MQ | \ 406 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \ 407 1ULL << VIRTIO_NET_F_CTRL_VQ | \ 408 1ULL << VIRTIO_NET_F_CTRL_RX | \ 409 1ULL << VIRTIO_NET_F_CTRL_VLAN | \ 410 1ULL << VIRTIO_NET_F_CSUM | \ 411 1ULL << VIRTIO_NET_F_HOST_TSO4 | \ 412 1ULL << VIRTIO_NET_F_HOST_TSO6 | \ 413 1ULL << VIRTIO_NET_F_MRG_RXBUF | \ 414 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \ 415 1ULL << VIRTIO_NET_F_GUEST_CSUM | \ 416 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \ 417 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \ 418 1ULL << VIRTIO_F_IN_ORDER | \ 419 1ULL << VIRTIO_F_VERSION_1 | \ 420 1ULL << VIRTIO_F_RING_PACKED) 421 422 int 423 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues, 424 int cq, int queue_size, const char *mac, char **ifname, 425 int server, int mrg_rxbuf, int in_order, int packed_vq) 426 { 427 pthread_mutex_init(&dev->mutex, NULL); 428 strlcpy(dev->path, path, PATH_MAX); 429 dev->started = 0; 430 dev->max_queue_pairs = queues; 431 dev->queue_pairs = 1; /* mq disabled by default */ 432 dev->queue_size = queue_size; 433 dev->is_server = server; 434 dev->mac_specified = 0; 435 dev->frontend_features = 0; 436 dev->unsupported_features = ~VIRTIO_USER_SUPPORTED_FEATURES; 437 parse_mac(dev, mac); 438 439 if (*ifname) { 440 dev->ifname = *ifname; 441 *ifname = NULL; 442 } 443 444 if (virtio_user_dev_setup(dev) < 0) { 445 PMD_INIT_LOG(ERR, "backend set up fails"); 446 return -1; 447 } 448 449 if (!dev->is_server) { 450 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, 451 NULL) < 0) { 452 PMD_INIT_LOG(ERR, "set_owner fails: %s", 453 strerror(errno)); 454 return -1; 455 } 456 457 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES, 458 &dev->device_features) < 0) { 459 PMD_INIT_LOG(ERR, "get_features failed: %s", 460 strerror(errno)); 461 return -1; 462 } 463 } else { 464 /* We just pretend vhost-user can support all these features. 465 * Note that this could be problematic that if some feature is 466 * negotiated but not supported by the vhost-user which comes 467 * later. 468 */ 469 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES; 470 } 471 472 if (!mrg_rxbuf) 473 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF); 474 475 if (!in_order) 476 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER); 477 478 if (!packed_vq) 479 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED); 480 481 if (dev->mac_specified) 482 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC); 483 else 484 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC); 485 486 if (cq) { 487 /* device does not really need to know anything about CQ, 488 * so if necessary, we just claim to support CQ 489 */ 490 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ); 491 } else { 492 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ); 493 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */ 494 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX); 495 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN); 496 dev->unsupported_features |= 497 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE); 498 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ); 499 dev->unsupported_features |= 500 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR); 501 } 502 503 /* The backend will not report this feature, we add it explicitly */ 504 if (is_vhost_user_by_type(dev->path)) 505 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS); 506 507 /* 508 * Device features = 509 * (frontend_features | backend_features) & ~unsupported_features; 510 */ 511 dev->device_features |= dev->frontend_features; 512 dev->device_features &= ~dev->unsupported_features; 513 514 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME, 515 virtio_user_mem_event_cb, dev)) { 516 if (rte_errno != ENOTSUP) { 517 PMD_INIT_LOG(ERR, "Failed to register mem event" 518 " callback\n"); 519 return -1; 520 } 521 } 522 523 return 0; 524 } 525 526 void 527 virtio_user_dev_uninit(struct virtio_user_dev *dev) 528 { 529 uint32_t i; 530 531 virtio_user_stop_device(dev); 532 533 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev); 534 535 for (i = 0; i < dev->max_queue_pairs * 2; ++i) { 536 close(dev->callfds[i]); 537 close(dev->kickfds[i]); 538 } 539 540 if (dev->vhostfd >= 0) 541 close(dev->vhostfd); 542 543 if (dev->is_server && dev->listenfd >= 0) { 544 close(dev->listenfd); 545 dev->listenfd = -1; 546 } 547 548 if (dev->vhostfds) { 549 for (i = 0; i < dev->max_queue_pairs; ++i) { 550 close(dev->vhostfds[i]); 551 if (dev->tapfds[i] >= 0) 552 close(dev->tapfds[i]); 553 } 554 free(dev->vhostfds); 555 free(dev->tapfds); 556 } 557 558 free(dev->ifname); 559 560 if (dev->is_server) 561 unlink(dev->path); 562 } 563 564 uint8_t 565 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs) 566 { 567 uint16_t i; 568 uint8_t ret = 0; 569 570 if (q_pairs > dev->max_queue_pairs) { 571 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported", 572 q_pairs, dev->max_queue_pairs); 573 return -1; 574 } 575 576 /* Server mode can't enable queue pairs if vhostfd is invalid, 577 * always return 0 in this case. 578 */ 579 if (!dev->is_server || dev->vhostfd >= 0) { 580 for (i = 0; i < q_pairs; ++i) 581 ret |= dev->ops->enable_qp(dev, i, 1); 582 for (i = q_pairs; i < dev->max_queue_pairs; ++i) 583 ret |= dev->ops->enable_qp(dev, i, 0); 584 } 585 dev->queue_pairs = q_pairs; 586 587 return ret; 588 } 589 590 static uint32_t 591 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring, 592 uint16_t idx_hdr) 593 { 594 struct virtio_net_ctrl_hdr *hdr; 595 virtio_net_ctrl_ack status = ~0; 596 uint16_t i, idx_data, idx_status; 597 uint32_t n_descs = 0; 598 599 /* locate desc for header, data, and status */ 600 idx_data = vring->desc[idx_hdr].next; 601 n_descs++; 602 603 i = idx_data; 604 while (vring->desc[i].flags == VRING_DESC_F_NEXT) { 605 i = vring->desc[i].next; 606 n_descs++; 607 } 608 609 /* locate desc for status */ 610 idx_status = i; 611 n_descs++; 612 613 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr; 614 if (hdr->class == VIRTIO_NET_CTRL_MQ && 615 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 616 uint16_t queues; 617 618 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr; 619 status = virtio_user_handle_mq(dev, queues); 620 } else if (hdr->class == VIRTIO_NET_CTRL_RX || 621 hdr->class == VIRTIO_NET_CTRL_MAC || 622 hdr->class == VIRTIO_NET_CTRL_VLAN) { 623 status = 0; 624 } 625 626 /* Update status */ 627 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status; 628 629 return n_descs; 630 } 631 632 static inline int 633 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter) 634 { 635 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE); 636 637 return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) && 638 wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED); 639 } 640 641 static uint32_t 642 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev, 643 struct vring_packed *vring, 644 uint16_t idx_hdr) 645 { 646 struct virtio_net_ctrl_hdr *hdr; 647 virtio_net_ctrl_ack status = ~0; 648 uint16_t idx_data, idx_status; 649 /* initialize to one, header is first */ 650 uint32_t n_descs = 1; 651 652 /* locate desc for header, data, and status */ 653 idx_data = idx_hdr + 1; 654 if (idx_data >= dev->queue_size) 655 idx_data -= dev->queue_size; 656 657 n_descs++; 658 659 idx_status = idx_data; 660 while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) { 661 idx_status++; 662 if (idx_status >= dev->queue_size) 663 idx_status -= dev->queue_size; 664 n_descs++; 665 } 666 667 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr; 668 if (hdr->class == VIRTIO_NET_CTRL_MQ && 669 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 670 uint16_t queues; 671 672 queues = *(uint16_t *)(uintptr_t) 673 vring->desc[idx_data].addr; 674 status = virtio_user_handle_mq(dev, queues); 675 } else if (hdr->class == VIRTIO_NET_CTRL_RX || 676 hdr->class == VIRTIO_NET_CTRL_MAC || 677 hdr->class == VIRTIO_NET_CTRL_VLAN) { 678 status = 0; 679 } 680 681 /* Update status */ 682 *(virtio_net_ctrl_ack *)(uintptr_t) 683 vring->desc[idx_status].addr = status; 684 685 /* Update used descriptor */ 686 vring->desc[idx_hdr].id = vring->desc[idx_status].id; 687 vring->desc[idx_hdr].len = sizeof(status); 688 689 return n_descs; 690 } 691 692 void 693 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx) 694 { 695 struct virtio_user_queue *vq = &dev->packed_queues[queue_idx]; 696 struct vring_packed *vring = &dev->packed_vrings[queue_idx]; 697 uint16_t n_descs, flags; 698 699 /* Perform a load-acquire barrier in desc_is_avail to 700 * enforce the ordering between desc flags and desc 701 * content. 702 */ 703 while (desc_is_avail(&vring->desc[vq->used_idx], 704 vq->used_wrap_counter)) { 705 706 n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring, 707 vq->used_idx); 708 709 flags = VRING_DESC_F_WRITE; 710 if (vq->used_wrap_counter) 711 flags |= VRING_PACKED_DESC_F_AVAIL_USED; 712 713 __atomic_store_n(&vring->desc[vq->used_idx].flags, flags, 714 __ATOMIC_RELEASE); 715 716 vq->used_idx += n_descs; 717 if (vq->used_idx >= dev->queue_size) { 718 vq->used_idx -= dev->queue_size; 719 vq->used_wrap_counter ^= 1; 720 } 721 } 722 } 723 724 void 725 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx) 726 { 727 uint16_t avail_idx, desc_idx; 728 struct vring_used_elem *uep; 729 uint32_t n_descs; 730 struct vring *vring = &dev->vrings[queue_idx]; 731 732 /* Consume avail ring, using used ring idx as first one */ 733 while (__atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED) 734 != vring->avail->idx) { 735 avail_idx = __atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED) 736 & (vring->num - 1); 737 desc_idx = vring->avail->ring[avail_idx]; 738 739 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx); 740 741 /* Update used ring */ 742 uep = &vring->used->ring[avail_idx]; 743 uep->id = desc_idx; 744 uep->len = n_descs; 745 746 __atomic_add_fetch(&vring->used->idx, 1, __ATOMIC_RELAXED); 747 } 748 } 749