1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2018 Intel Corporation 3 */ 4 5 #ifndef _VHOST_NET_CDEV_H_ 6 #define _VHOST_NET_CDEV_H_ 7 #include <stdint.h> 8 #include <stdio.h> 9 #include <stdbool.h> 10 #include <sys/types.h> 11 #include <sys/queue.h> 12 #include <unistd.h> 13 #include <linux/vhost.h> 14 #include <linux/virtio_net.h> 15 #include <sys/socket.h> 16 #include <linux/if.h> 17 18 #include <rte_log.h> 19 #include <rte_ether.h> 20 #include <rte_rwlock.h> 21 #include <rte_malloc.h> 22 23 #include "rte_vhost.h" 24 #include "rte_vdpa.h" 25 #include "rte_vdpa_dev.h" 26 27 #include "rte_vhost_async.h" 28 29 /* Used to indicate that the device is running on a data core */ 30 #define VIRTIO_DEV_RUNNING ((uint32_t)1 << 0) 31 /* Used to indicate that the device is ready to operate */ 32 #define VIRTIO_DEV_READY ((uint32_t)1 << 1) 33 /* Used to indicate that the built-in vhost net device backend is enabled */ 34 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET ((uint32_t)1 << 2) 35 /* Used to indicate that the device has its own data path and configured */ 36 #define VIRTIO_DEV_VDPA_CONFIGURED ((uint32_t)1 << 3) 37 /* Used to indicate that the feature negotiation failed */ 38 #define VIRTIO_DEV_FEATURES_FAILED ((uint32_t)1 << 4) 39 /* Used to indicate that the virtio_net tx code should fill TX ol_flags */ 40 #define VIRTIO_DEV_LEGACY_OL_FLAGS ((uint32_t)1 << 5) 41 42 /* Backend value set by guest. */ 43 #define VIRTIO_DEV_STOPPED -1 44 45 #define BUF_VECTOR_MAX 256 46 47 #define VHOST_LOG_CACHE_NR 32 48 49 #define MAX_PKT_BURST 32 50 51 #define VHOST_MAX_ASYNC_IT (MAX_PKT_BURST * 2) 52 #define VHOST_MAX_ASYNC_VEC (BUF_VECTOR_MAX * 4) 53 54 #define PACKED_DESC_ENQUEUE_USED_FLAG(w) \ 55 ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ 56 VRING_DESC_F_WRITE) 57 #define PACKED_DESC_DEQUEUE_USED_FLAG(w) \ 58 ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0) 59 #define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ 60 VRING_DESC_F_INDIRECT) 61 62 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ 63 sizeof(struct vring_packed_desc)) 64 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) 65 66 #ifdef VHOST_GCC_UNROLL_PRAGMA 67 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \ 68 for (iter = val; iter < size; iter++) 69 #endif 70 71 #ifdef VHOST_CLANG_UNROLL_PRAGMA 72 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \ 73 for (iter = val; iter < size; iter++) 74 #endif 75 76 #ifdef VHOST_ICC_UNROLL_PRAGMA 77 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \ 78 for (iter = val; iter < size; iter++) 79 #endif 80 81 #ifndef vhost_for_each_try_unroll 82 #define vhost_for_each_try_unroll(iter, val, num) \ 83 for (iter = val; iter < num; iter++) 84 #endif 85 86 /** 87 * Structure contains buffer address, length and descriptor index 88 * from vring to do scatter RX. 89 */ 90 struct buf_vector { 91 uint64_t buf_iova; 92 uint64_t buf_addr; 93 uint32_t buf_len; 94 uint32_t desc_idx; 95 }; 96 97 /* 98 * Structure contains the info for each batched memory copy. 99 */ 100 struct batch_copy_elem { 101 void *dst; 102 void *src; 103 uint32_t len; 104 uint64_t log_addr; 105 }; 106 107 /* 108 * Structure that contains the info for batched dirty logging. 109 */ 110 struct log_cache_entry { 111 uint32_t offset; 112 unsigned long val; 113 }; 114 115 struct vring_used_elem_packed { 116 uint16_t id; 117 uint16_t flags; 118 uint32_t len; 119 uint32_t count; 120 }; 121 122 /** 123 * Structure contains variables relevant to RX/TX virtqueues. 124 */ 125 struct vhost_virtqueue { 126 union { 127 struct vring_desc *desc; 128 struct vring_packed_desc *desc_packed; 129 }; 130 union { 131 struct vring_avail *avail; 132 struct vring_packed_desc_event *driver_event; 133 }; 134 union { 135 struct vring_used *used; 136 struct vring_packed_desc_event *device_event; 137 }; 138 uint16_t size; 139 140 uint16_t last_avail_idx; 141 uint16_t last_used_idx; 142 /* Last used index we notify to front end. */ 143 uint16_t signalled_used; 144 bool signalled_used_valid; 145 #define VIRTIO_INVALID_EVENTFD (-1) 146 #define VIRTIO_UNINITIALIZED_EVENTFD (-2) 147 148 bool enabled; 149 bool access_ok; 150 bool ready; 151 152 rte_spinlock_t access_lock; 153 154 155 union { 156 struct vring_used_elem *shadow_used_split; 157 struct vring_used_elem_packed *shadow_used_packed; 158 }; 159 uint16_t shadow_used_idx; 160 /* Record packed ring enqueue latest desc cache aligned index */ 161 uint16_t shadow_aligned_idx; 162 /* Record packed ring first dequeue desc index */ 163 uint16_t shadow_last_used_idx; 164 165 uint16_t batch_copy_nb_elems; 166 struct batch_copy_elem *batch_copy_elems; 167 int numa_node; 168 bool used_wrap_counter; 169 bool avail_wrap_counter; 170 171 /* Physical address of used ring, for logging */ 172 uint16_t log_cache_nb_elem; 173 uint64_t log_guest_addr; 174 struct log_cache_entry *log_cache; 175 176 rte_rwlock_t iotlb_lock; 177 rte_rwlock_t iotlb_pending_lock; 178 struct rte_mempool *iotlb_pool; 179 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list; 180 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list; 181 int iotlb_cache_nr; 182 183 /* Used to notify the guest (trigger interrupt) */ 184 int callfd; 185 /* Currently unused as polling mode is enabled */ 186 int kickfd; 187 188 /* inflight share memory info */ 189 union { 190 struct rte_vhost_inflight_info_split *inflight_split; 191 struct rte_vhost_inflight_info_packed *inflight_packed; 192 }; 193 struct rte_vhost_resubmit_info *resubmit_inflight; 194 uint64_t global_counter; 195 196 /* operation callbacks for async dma */ 197 struct rte_vhost_async_channel_ops async_ops; 198 199 struct rte_vhost_iov_iter *it_pool; 200 struct iovec *vec_pool; 201 202 /* async data transfer status */ 203 struct async_inflight_info *async_pkts_info; 204 uint16_t async_pkts_idx; 205 uint16_t async_pkts_inflight_n; 206 uint16_t async_last_pkts_n; 207 union { 208 struct vring_used_elem *async_descs_split; 209 struct vring_used_elem_packed *async_buffers_packed; 210 }; 211 union { 212 uint16_t async_desc_idx_split; 213 uint16_t async_buffer_idx_packed; 214 }; 215 union { 216 uint16_t last_async_desc_idx_split; 217 uint16_t last_async_buffer_idx_packed; 218 }; 219 220 /* vq async features */ 221 bool async_inorder; 222 bool async_registered; 223 uint16_t async_threshold; 224 225 int notif_enable; 226 #define VIRTIO_UNINITIALIZED_NOTIF (-1) 227 228 struct vhost_vring_addr ring_addrs; 229 } __rte_cache_aligned; 230 231 /* Virtio device status as per Virtio specification */ 232 #define VIRTIO_DEVICE_STATUS_RESET 0x00 233 #define VIRTIO_DEVICE_STATUS_ACK 0x01 234 #define VIRTIO_DEVICE_STATUS_DRIVER 0x02 235 #define VIRTIO_DEVICE_STATUS_DRIVER_OK 0x04 236 #define VIRTIO_DEVICE_STATUS_FEATURES_OK 0x08 237 #define VIRTIO_DEVICE_STATUS_DEV_NEED_RESET 0x40 238 #define VIRTIO_DEVICE_STATUS_FAILED 0x80 239 240 #define VHOST_MAX_VRING 0x100 241 #define VHOST_MAX_QUEUE_PAIRS 0x80 242 243 /* Declare IOMMU related bits for older kernels */ 244 #ifndef VIRTIO_F_IOMMU_PLATFORM 245 246 #define VIRTIO_F_IOMMU_PLATFORM 33 247 248 struct vhost_iotlb_msg { 249 __u64 iova; 250 __u64 size; 251 __u64 uaddr; 252 #define VHOST_ACCESS_RO 0x1 253 #define VHOST_ACCESS_WO 0x2 254 #define VHOST_ACCESS_RW 0x3 255 __u8 perm; 256 #define VHOST_IOTLB_MISS 1 257 #define VHOST_IOTLB_UPDATE 2 258 #define VHOST_IOTLB_INVALIDATE 3 259 #define VHOST_IOTLB_ACCESS_FAIL 4 260 __u8 type; 261 }; 262 263 #define VHOST_IOTLB_MSG 0x1 264 265 struct vhost_msg { 266 int type; 267 union { 268 struct vhost_iotlb_msg iotlb; 269 __u8 padding[64]; 270 }; 271 }; 272 #endif 273 274 /* 275 * Define virtio 1.0 for older kernels 276 */ 277 #ifndef VIRTIO_F_VERSION_1 278 #define VIRTIO_F_VERSION_1 32 279 #endif 280 281 /* Declare packed ring related bits for older kernels */ 282 #ifndef VIRTIO_F_RING_PACKED 283 284 #define VIRTIO_F_RING_PACKED 34 285 286 struct vring_packed_desc { 287 uint64_t addr; 288 uint32_t len; 289 uint16_t id; 290 uint16_t flags; 291 }; 292 293 struct vring_packed_desc_event { 294 uint16_t off_wrap; 295 uint16_t flags; 296 }; 297 #endif 298 299 /* 300 * Declare below packed ring defines unconditionally 301 * as Kernel header might use different names. 302 */ 303 #define VRING_DESC_F_AVAIL (1ULL << 7) 304 #define VRING_DESC_F_USED (1ULL << 15) 305 306 #define VRING_EVENT_F_ENABLE 0x0 307 #define VRING_EVENT_F_DISABLE 0x1 308 #define VRING_EVENT_F_DESC 0x2 309 310 /* 311 * Available and used descs are in same order 312 */ 313 #ifndef VIRTIO_F_IN_ORDER 314 #define VIRTIO_F_IN_ORDER 35 315 #endif 316 317 /* Features supported by this builtin vhost-user net driver. */ 318 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \ 319 (1ULL << VIRTIO_F_ANY_LAYOUT) | \ 320 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \ 321 (1ULL << VIRTIO_NET_F_CTRL_RX) | \ 322 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \ 323 (1ULL << VIRTIO_NET_F_MQ) | \ 324 (1ULL << VIRTIO_F_VERSION_1) | \ 325 (1ULL << VHOST_F_LOG_ALL) | \ 326 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \ 327 (1ULL << VIRTIO_NET_F_GSO) | \ 328 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \ 329 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \ 330 (1ULL << VIRTIO_NET_F_HOST_UFO) | \ 331 (1ULL << VIRTIO_NET_F_HOST_ECN) | \ 332 (1ULL << VIRTIO_NET_F_CSUM) | \ 333 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \ 334 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \ 335 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \ 336 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \ 337 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \ 338 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \ 339 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \ 340 (1ULL << VIRTIO_NET_F_MTU) | \ 341 (1ULL << VIRTIO_F_IN_ORDER) | \ 342 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \ 343 (1ULL << VIRTIO_F_RING_PACKED)) 344 345 346 struct guest_page { 347 uint64_t guest_phys_addr; 348 uint64_t host_phys_addr; 349 uint64_t size; 350 }; 351 352 struct inflight_mem_info { 353 int fd; 354 void *addr; 355 uint64_t size; 356 }; 357 358 /** 359 * Device structure contains all configuration information relating 360 * to the device. 361 */ 362 struct virtio_net { 363 /* Frontend (QEMU) memory and memory region information */ 364 struct rte_vhost_memory *mem; 365 uint64_t features; 366 uint64_t protocol_features; 367 int vid; 368 uint32_t flags; 369 uint16_t vhost_hlen; 370 /* to tell if we need broadcast rarp packet */ 371 int16_t broadcast_rarp; 372 uint32_t nr_vring; 373 int async_copy; 374 int extbuf; 375 int linearbuf; 376 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2]; 377 struct inflight_mem_info *inflight_info; 378 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ) 379 char ifname[IF_NAME_SZ]; 380 uint64_t log_size; 381 uint64_t log_base; 382 uint64_t log_addr; 383 struct rte_ether_addr mac; 384 uint16_t mtu; 385 uint8_t status; 386 387 struct vhost_device_ops const *notify_ops; 388 389 uint32_t nr_guest_pages; 390 uint32_t max_guest_pages; 391 struct guest_page *guest_pages; 392 393 int slave_req_fd; 394 rte_spinlock_t slave_req_lock; 395 396 int postcopy_ufd; 397 int postcopy_listening; 398 399 struct rte_vdpa_device *vdpa_dev; 400 401 /* context data for the external message handlers */ 402 void *extern_data; 403 /* pre and post vhost user message handlers for the device */ 404 struct rte_vhost_user_extern_ops extern_ops; 405 } __rte_cache_aligned; 406 407 static __rte_always_inline bool 408 vq_is_packed(struct virtio_net *dev) 409 { 410 return dev->features & (1ull << VIRTIO_F_RING_PACKED); 411 } 412 413 static inline bool 414 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter) 415 { 416 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE); 417 418 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) && 419 wrap_counter != !!(flags & VRING_DESC_F_USED); 420 } 421 422 static inline void 423 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num) 424 { 425 vq->last_used_idx += num; 426 if (vq->last_used_idx >= vq->size) { 427 vq->used_wrap_counter ^= 1; 428 vq->last_used_idx -= vq->size; 429 } 430 } 431 432 static inline void 433 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num) 434 { 435 vq->last_avail_idx += num; 436 if (vq->last_avail_idx >= vq->size) { 437 vq->avail_wrap_counter ^= 1; 438 vq->last_avail_idx -= vq->size; 439 } 440 } 441 442 void __vhost_log_cache_write(struct virtio_net *dev, 443 struct vhost_virtqueue *vq, 444 uint64_t addr, uint64_t len); 445 void __vhost_log_cache_write_iova(struct virtio_net *dev, 446 struct vhost_virtqueue *vq, 447 uint64_t iova, uint64_t len); 448 void __vhost_log_cache_sync(struct virtio_net *dev, 449 struct vhost_virtqueue *vq); 450 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len); 451 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq, 452 uint64_t iova, uint64_t len); 453 454 static __rte_always_inline void 455 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len) 456 { 457 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) 458 __vhost_log_write(dev, addr, len); 459 } 460 461 static __rte_always_inline void 462 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq) 463 { 464 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) 465 __vhost_log_cache_sync(dev, vq); 466 } 467 468 static __rte_always_inline void 469 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq, 470 uint64_t addr, uint64_t len) 471 { 472 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) 473 __vhost_log_cache_write(dev, vq, addr, len); 474 } 475 476 static __rte_always_inline void 477 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq, 478 uint64_t offset, uint64_t len) 479 { 480 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) { 481 if (unlikely(vq->log_guest_addr == 0)) 482 return; 483 __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, 484 len); 485 } 486 } 487 488 static __rte_always_inline void 489 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq, 490 uint64_t offset, uint64_t len) 491 { 492 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) { 493 if (unlikely(vq->log_guest_addr == 0)) 494 return; 495 __vhost_log_write(dev, vq->log_guest_addr + offset, len); 496 } 497 } 498 499 static __rte_always_inline void 500 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq, 501 uint64_t iova, uint64_t len) 502 { 503 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL)))) 504 return; 505 506 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) 507 __vhost_log_cache_write_iova(dev, vq, iova, len); 508 else 509 __vhost_log_cache_write(dev, vq, iova, len); 510 } 511 512 static __rte_always_inline void 513 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq, 514 uint64_t iova, uint64_t len) 515 { 516 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL)))) 517 return; 518 519 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) 520 __vhost_log_write_iova(dev, vq, iova, len); 521 else 522 __vhost_log_write(dev, iova, len); 523 } 524 525 extern int vhost_config_log_level; 526 extern int vhost_data_log_level; 527 528 #define VHOST_LOG_CONFIG(level, fmt, args...) \ 529 rte_log(RTE_LOG_ ## level, vhost_config_log_level, \ 530 "VHOST_CONFIG: " fmt, ##args) 531 532 #define VHOST_LOG_DATA(level, fmt, args...) \ 533 (void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ? \ 534 rte_log(RTE_LOG_ ## level, vhost_data_log_level, \ 535 "VHOST_DATA : " fmt, ##args) : \ 536 0) 537 538 #ifdef RTE_LIBRTE_VHOST_DEBUG 539 #define VHOST_MAX_PRINT_BUFF 6072 540 #define PRINT_PACKET(device, addr, size, header) do { \ 541 char *pkt_addr = (char *)(addr); \ 542 unsigned int index; \ 543 char packet[VHOST_MAX_PRINT_BUFF]; \ 544 \ 545 if ((header)) \ 546 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \ 547 else \ 548 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \ 549 for (index = 0; index < (size); index++) { \ 550 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \ 551 "%02hhx ", pkt_addr[index]); \ 552 } \ 553 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \ 554 \ 555 VHOST_LOG_DATA(DEBUG, "%s", packet); \ 556 } while (0) 557 #else 558 #define PRINT_PACKET(device, addr, size, header) do {} while (0) 559 #endif 560 561 #define MAX_VHOST_DEVICE 1024 562 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE]; 563 564 #define VHOST_BINARY_SEARCH_THRESH 256 565 566 static __rte_always_inline int guest_page_addrcmp(const void *p1, 567 const void *p2) 568 { 569 const struct guest_page *page1 = (const struct guest_page *)p1; 570 const struct guest_page *page2 = (const struct guest_page *)p2; 571 572 if (page1->guest_phys_addr > page2->guest_phys_addr) 573 return 1; 574 if (page1->guest_phys_addr < page2->guest_phys_addr) 575 return -1; 576 577 return 0; 578 } 579 580 static __rte_always_inline rte_iova_t 581 gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa, 582 uint64_t gpa_size, uint64_t *hpa_size) 583 { 584 uint32_t i; 585 struct guest_page *page; 586 struct guest_page key; 587 588 *hpa_size = gpa_size; 589 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) { 590 key.guest_phys_addr = gpa & ~(dev->guest_pages[0].size - 1); 591 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages, 592 sizeof(struct guest_page), guest_page_addrcmp); 593 if (page) { 594 if (gpa + gpa_size <= 595 page->guest_phys_addr + page->size) { 596 return gpa - page->guest_phys_addr + 597 page->host_phys_addr; 598 } else if (gpa < page->guest_phys_addr + 599 page->size) { 600 *hpa_size = page->guest_phys_addr + 601 page->size - gpa; 602 return gpa - page->guest_phys_addr + 603 page->host_phys_addr; 604 } 605 } 606 } else { 607 for (i = 0; i < dev->nr_guest_pages; i++) { 608 page = &dev->guest_pages[i]; 609 610 if (gpa >= page->guest_phys_addr) { 611 if (gpa + gpa_size <= 612 page->guest_phys_addr + page->size) { 613 return gpa - page->guest_phys_addr + 614 page->host_phys_addr; 615 } else if (gpa < page->guest_phys_addr + 616 page->size) { 617 *hpa_size = page->guest_phys_addr + 618 page->size - gpa; 619 return gpa - page->guest_phys_addr + 620 page->host_phys_addr; 621 } 622 } 623 } 624 } 625 626 *hpa_size = 0; 627 return 0; 628 } 629 630 /* Convert guest physical address to host physical address */ 631 static __rte_always_inline rte_iova_t 632 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size) 633 { 634 rte_iova_t hpa; 635 uint64_t hpa_size; 636 637 hpa = gpa_to_first_hpa(dev, gpa, size, &hpa_size); 638 return hpa_size == size ? hpa : 0; 639 } 640 641 static __rte_always_inline uint64_t 642 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len) 643 { 644 struct rte_vhost_mem_region *r; 645 uint32_t i; 646 647 if (unlikely(!dev || !dev->mem)) 648 return 0; 649 650 for (i = 0; i < dev->mem->nregions; i++) { 651 r = &dev->mem->regions[i]; 652 653 if (vva >= r->host_user_addr && 654 vva + len < r->host_user_addr + r->size) { 655 return r->guest_phys_addr + vva - r->host_user_addr; 656 } 657 } 658 return 0; 659 } 660 661 static __rte_always_inline struct virtio_net * 662 get_device(int vid) 663 { 664 struct virtio_net *dev = vhost_devices[vid]; 665 666 if (unlikely(!dev)) { 667 VHOST_LOG_CONFIG(ERR, 668 "(%d) device not found.\n", vid); 669 } 670 671 return dev; 672 } 673 674 int vhost_new_device(void); 675 void cleanup_device(struct virtio_net *dev, int destroy); 676 void reset_device(struct virtio_net *dev); 677 void vhost_destroy_device(int); 678 void vhost_destroy_device_notify(struct virtio_net *dev); 679 680 void cleanup_vq(struct vhost_virtqueue *vq, int destroy); 681 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq); 682 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq); 683 684 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx); 685 686 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev); 687 688 void vhost_set_ifname(int, const char *if_name, unsigned int if_len); 689 void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags); 690 void vhost_enable_extbuf(int vid); 691 void vhost_enable_linearbuf(int vid); 692 int vhost_enable_guest_notification(struct virtio_net *dev, 693 struct vhost_virtqueue *vq, int enable); 694 695 struct vhost_device_ops const *vhost_driver_callback_get(const char *path); 696 697 /* 698 * Backend-specific cleanup. 699 * 700 * TODO: fix it; we have one backend now 701 */ 702 void vhost_backend_cleanup(struct virtio_net *dev); 703 704 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq, 705 uint64_t iova, uint64_t *len, uint8_t perm); 706 void *vhost_alloc_copy_ind_table(struct virtio_net *dev, 707 struct vhost_virtqueue *vq, 708 uint64_t desc_addr, uint64_t desc_len); 709 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq); 710 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq, 711 uint64_t log_addr); 712 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq); 713 714 static __rte_always_inline uint64_t 715 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq, 716 uint64_t iova, uint64_t *len, uint8_t perm) 717 { 718 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) 719 return rte_vhost_va_from_guest_pa(dev->mem, iova, len); 720 721 return __vhost_iova_to_vva(dev, vq, iova, len, perm); 722 } 723 724 #define vhost_avail_event(vr) \ 725 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size]) 726 #define vhost_used_event(vr) \ 727 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size]) 728 729 /* 730 * The following is used with VIRTIO_RING_F_EVENT_IDX. 731 * Assuming a given event_idx value from the other size, if we have 732 * just incremented index from old to new_idx, should we trigger an 733 * event? 734 */ 735 static __rte_always_inline int 736 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old) 737 { 738 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old); 739 } 740 741 static __rte_always_inline void 742 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq) 743 { 744 /* Flush used->idx update before we read avail->flags. */ 745 rte_atomic_thread_fence(__ATOMIC_SEQ_CST); 746 747 /* Don't kick guest if we don't reach index specified by guest. */ 748 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) { 749 uint16_t old = vq->signalled_used; 750 uint16_t new = vq->last_used_idx; 751 bool signalled_used_valid = vq->signalled_used_valid; 752 753 vq->signalled_used = new; 754 vq->signalled_used_valid = true; 755 756 VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n", 757 __func__, 758 vhost_used_event(vq), 759 old, new); 760 761 if ((vhost_need_event(vhost_used_event(vq), new, old) && 762 (vq->callfd >= 0)) || 763 unlikely(!signalled_used_valid)) { 764 eventfd_write(vq->callfd, (eventfd_t) 1); 765 if (dev->notify_ops->guest_notified) 766 dev->notify_ops->guest_notified(dev->vid); 767 } 768 } else { 769 /* Kick the guest if necessary. */ 770 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT) 771 && (vq->callfd >= 0)) { 772 eventfd_write(vq->callfd, (eventfd_t)1); 773 if (dev->notify_ops->guest_notified) 774 dev->notify_ops->guest_notified(dev->vid); 775 } 776 } 777 } 778 779 static __rte_always_inline void 780 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq) 781 { 782 uint16_t old, new, off, off_wrap; 783 bool signalled_used_valid, kick = false; 784 785 /* Flush used desc update. */ 786 rte_atomic_thread_fence(__ATOMIC_SEQ_CST); 787 788 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) { 789 if (vq->driver_event->flags != 790 VRING_EVENT_F_DISABLE) 791 kick = true; 792 goto kick; 793 } 794 795 old = vq->signalled_used; 796 new = vq->last_used_idx; 797 vq->signalled_used = new; 798 signalled_used_valid = vq->signalled_used_valid; 799 vq->signalled_used_valid = true; 800 801 if (vq->driver_event->flags != VRING_EVENT_F_DESC) { 802 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE) 803 kick = true; 804 goto kick; 805 } 806 807 if (unlikely(!signalled_used_valid)) { 808 kick = true; 809 goto kick; 810 } 811 812 rte_atomic_thread_fence(__ATOMIC_ACQUIRE); 813 814 off_wrap = vq->driver_event->off_wrap; 815 off = off_wrap & ~(1 << 15); 816 817 if (new <= old) 818 old -= vq->size; 819 820 if (vq->used_wrap_counter != off_wrap >> 15) 821 off -= vq->size; 822 823 if (vhost_need_event(off, new, old)) 824 kick = true; 825 kick: 826 if (kick) { 827 eventfd_write(vq->callfd, (eventfd_t)1); 828 if (dev->notify_ops->guest_notified) 829 dev->notify_ops->guest_notified(dev->vid); 830 } 831 } 832 833 static __rte_always_inline void 834 free_ind_table(void *idesc) 835 { 836 rte_free(idesc); 837 } 838 839 static __rte_always_inline void 840 restore_mbuf(struct rte_mbuf *m) 841 { 842 uint32_t mbuf_size, priv_size; 843 844 while (m) { 845 priv_size = rte_pktmbuf_priv_size(m->pool); 846 mbuf_size = sizeof(struct rte_mbuf) + priv_size; 847 /* start of buffer is after mbuf structure and priv data */ 848 849 m->buf_addr = (char *)m + mbuf_size; 850 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size; 851 m = m->next; 852 } 853 } 854 855 static __rte_always_inline bool 856 mbuf_is_consumed(struct rte_mbuf *m) 857 { 858 while (m) { 859 if (rte_mbuf_refcnt_read(m) > 1) 860 return false; 861 m = m->next; 862 } 863 864 return true; 865 } 866 867 #endif /* _VHOST_NET_CDEV_H_ */ 868