rte_vhost_enqueue_burst + rte_vhost_dequeue_burst + vhost_user_set_vring_call +vhost_user_set_vring

下图显示了作为DPDK-APP的一部分运行的虚拟主机用户库如何使用virtio-device-model和virtio-pci设备与qemu和客户机进行交互:

几个要点:

  • virtio内存区域最初是由客户机分配的。
  • 相应的virtio驱动程序通常通过virtio规范中定义的PCI BARs配置接口与virtio设备进行交互。
  • virtio-device-model(位于QEMU内部)使用vhost-user协议配置vhost-user库,以及设置irqfd和ioeventfd文件描述符。
  • 客户机分配的virtio内存区域由vhost用户库(即DPDK应用程序)映射(使用mmap 系统调用)。
  • 结果是,DPDK应用程序可以直接在客户机内存中读取和写入数据包,并使用irqfd和ioeventfd机制直接对客户机发出通知。

rte_vhost_enqueue_burst通知虚拟机eventfd_write

static inline uint32_t __attribute__((always_inline))
virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
    struct rte_mbuf **pkts, uint32_t count)
{
    struct vhost_virtqueue *vq;
    struct vring_desc *desc;
    struct rte_mbuf *buff;
    /* The virtio_hdr is initialised to 0. */
    struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
  {0, 0, 0, 0, 0, 0}, 0};
    uint64_t buff_addr = 0;
    uint64_t buff_hdr_addr = 0;
    uint32_t head[MAX_PKT_BURST];
    uint32_t head_idx, packet_success = 0;
    uint16_t avail_idx, res_cur_idx;
    uint16_t res_base_idx, res_end_idx;
    uint16_t free_entries;
    uint8_t success = 0;

    LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
    if (unlikely(queue_id != VIRTIO_RXQ)) {
        LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
        return 0;
    }

    vq = dev->virtqueue[VIRTIO_RXQ];
    count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;

    /*
     * As many data cores may want access to available buffers,
     * they need to be reserved.
     */
    do {
        res_base_idx = vq->last_used_idx_res;
        avail_idx = *((volatile uint16_t *)&vq->avail->idx);

        free_entries = (avail_idx - res_base_idx);
        /*check that we have enough buffers*/
        if (unlikely(count > free_entries))
            count = free_entries;

        if (count == 0)
            return 0;

        res_end_idx = res_base_idx + count;
        /* vq->last_used_idx_res is atomically updated. */
        /* TODO: Allow to disable cmpset if no concurrency in application. */
        success = rte_atomic16_cmpset(&vq->last_used_idx_res,
                res_base_idx, res_end_idx);
    } while (unlikely(success == 0));
    res_cur_idx = res_base_idx;
    LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n",
            dev->device_fh, res_cur_idx, res_end_idx);

    /* Prefetch available ring to retrieve indexes. */
    rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);

    /* Retrieve all of the head indexes first to avoid caching issues. */
    for (head_idx = 0; head_idx < count; head_idx++)
        head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) &
                    (vq->size - 1)];

    /*Prefetch descriptor index. */
    rte_prefetch0(&vq->desc[head[packet_success]]);

    while (res_cur_idx != res_end_idx) {
        uint32_t offset = 0, vb_offset = 0;
        uint32_t pkt_len, len_to_cpy, data_len, total_copied = 0;
        uint8_t hdr = 0, uncompleted_pkt = 0;

        /* Get descriptor from available ring */
        desc = &vq->desc[head[packet_success]];

        buff = pkts[packet_success];

        /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
        buff_addr = gpa_to_vva(dev, desc->addr);
        /* Prefetch buffer address. */
        rte_prefetch0((void *)(uintptr_t)buff_addr);

        /* Copy virtio_hdr to packet and increment buffer address */
        buff_hdr_addr = buff_addr;

        /*
         * If the descriptors are chained the header and data are
         * placed in separate buffers.
         */
        if ((desc->flags & VRING_DESC_F_NEXT) &&
            (desc->len == vq->vhost_hlen)) {
            desc = &vq->desc[desc->next];
            /* Buffer address translation. */
            buff_addr = gpa_to_vva(dev, desc->addr);
        } else {
            vb_offset += vq->vhost_hlen;
            hdr = 1;
        }

        pkt_len = rte_pktmbuf_pkt_len(buff);
        data_len = rte_pktmbuf_data_len(buff);
        len_to_cpy = RTE_MIN(data_len,
            hdr ? desc->len - vq->vhost_hlen : desc->len);
        while (total_copied < pkt_len) {
            /* Copy mbuf data to buffer */
            rte_memcpy((void *)(uintptr_t)(buff_addr + vb_offset),
                rte_pktmbuf_mtod_offset(buff, const void *, offset),
                len_to_cpy);
            PRINT_PACKET(dev, (uintptr_t)(buff_addr + vb_offset),
                len_to_cpy, 0);

            offset += len_to_cpy;
            vb_offset += len_to_cpy;
            total_copied += len_to_cpy;

            /* The whole packet completes */
            if (total_copied == pkt_len)
                break;

            /* The current segment completes */
            if (offset == data_len) {
                buff = buff->next;
                offset = 0;
                data_len = rte_pktmbuf_data_len(buff);
            }

            /* The current vring descriptor done */
            if (vb_offset == desc->len) {
                if (desc->flags & VRING_DESC_F_NEXT) {
                    desc = &vq->desc[desc->next];
                    buff_addr = gpa_to_vva(dev, desc->addr);
                    vb_offset = 0;
                } else {
                    /* Room in vring buffer is not enough */
                    uncompleted_pkt = 1;
                    break;
                }
            }
            len_to_cpy = RTE_MIN(data_len - offset, desc->len - vb_offset);
        };

        /* Update used ring with desc information */
        vq->used->ring[res_cur_idx & (vq->size - 1)].id =
                            head[packet_success];

        /* Drop the packet if it is uncompleted */
        if (unlikely(uncompleted_pkt == 1))
            vq->used->ring[res_cur_idx & (vq->size - 1)].len =
                            vq->vhost_hlen;
        else
            vq->used->ring[res_cur_idx & (vq->size - 1)].len =
                            pkt_len + vq->vhost_hlen;

        res_cur_idx++;
        packet_success++;

        if (unlikely(uncompleted_pkt == 1))
            continue;

        rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
            (const void *)&virtio_hdr, vq->vhost_hlen);

        PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);

        if (res_cur_idx < res_end_idx) {
            /* Prefetch descriptor index. */
            rte_prefetch0(&vq->desc[head[packet_success]]);
        }
    }

    rte_compiler_barrier();

    /* Wait until it's our turn to add our buffer to the used ring. */
    while (unlikely(vq->last_used_idx != res_base_idx))
        rte_pause();

    *(volatile uint16_t *)&vq->used->idx += count;
    vq->last_used_idx = res_end_idx;

    /* flush used->idx update before we read avail->flags. */
    rte_mb();

    /* Kick the guest if necessary. */
    if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
        eventfd_write((int)vq->callfd, 1);
    return count;
}

eth_vhost_install_intr(struct rte_eth_dev *dev)

for (i = 0; i < nb_rxq; i++) {
                vq = dev->data->rx_queues[i];
                if (!vq) {
                        VHOST_LOG(INFO, "rxq-%d not setup yet, skip!\n", i);
                        continue;
                }

                ret = rte_vhost_get_vhost_vring(vq->vid, (i << 1) + 1, &vring);
                if (ret < 0) {
                        VHOST_LOG(INFO,
                                "Failed to get rxq-%d's vring, skip!\n", i);
                        continue;
                }

                if (vring.kickfd < 0) {
                        VHOST_LOG(INFO,
                                "rxq-%d's kickfd is invalid, skip!\n", i);
                        continue;
                }
                dev->intr_handle-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: rte_ring_sc_dequeue_bulk是DPDK中的一个函数,用于从单个生产者,单个消费者环形队列中批量出队一组元素。"sc"代表"single consumer",表示只有一个消费者在访问该队列。该函数的原型如下: ``` uint32_t rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, uint32_t n) ``` 其中,参数r是指向环形队列的指针,obj_table是一个指向指针数组的指针,用于存储出队的元素,n表示要出队的元素数量。 函数的返回值是实际出队的元素数量,可能小于请求的数量n。如果队列为空,则返回0。该函数是线程安全的,可以在多个线程之间并发调用。 ### 回答2: rte_ring_sc_dequeue_bulk是DPDK(Data Plane Development Kit)库中的一个函数,用于从指定的环形缓冲区中以"单一消费者"的方式批量取出元素。 该函数的作用是从环形缓冲区中按照先入先出原则取出一定数量(批量)的元素,并将它们存储到用户提供的缓冲区中。在单一消费者的情况下,该函数可以提供更高的性能。 该函数的原型为: ```c uint32_t rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, uint32_t n, const unsigned int *restrict offset) ``` 参数说明: - r:指向目标环形缓冲区的指针。 - obj_table:指向用户提供的缓冲区指针的指针,用于存储从环形缓冲区中取出的元素。 - n:用户期望从环形缓冲区中取出的元素数量。 - offset:用户提供的存储偏移量的数组指针,用于存储从缓冲区中每个元素的偏移量值。 该函数的返回值为实际取出的元素数量。 使用rte_ring_sc_dequeue_bulk函数可以实现高效地从环形缓冲区中取出一定数量的元素,可以提高数据处理的效率。需要注意的是,在使用该函数之前,必须先创建好环形缓冲区,并确保环形缓冲区中有足够的元素可供取出。 ### 回答3: rte_ring_sc_dequeue_bulk是一个函数,用于从单生产者、单消费者环形缓冲区中以原子操作的方式批量出队元素。 它的功能是从环形缓冲区中连续出队指定数量的元素,并返回实际出队的元素数量。它是无锁的,采用强制屏障以确保原子性。 使用rte_ring_sc_dequeue_bulk函数时,需要传入一个指向环形缓冲区的指针,以及一个指向存储出队元素的数组的指针,以及期望出队的元素数量。 函数会按先进先出的顺序出队元素,并将其存储到数组中。如果实际出队的元素数量小于期望的数量,则代表环形缓冲区中的元素数量不足,所有剩余的元素都将被出队。 rte_ring_sc_dequeue_bulk函数会根据环形缓冲区的状态,使用原子操作进行出队操作,避免多个线程同时修改环形缓冲区造成冲突。在出队操作的同时,它使用强制屏障来确保原子性,确保出队操作的结果对其他线程可见。 总之,rte_ring_sc_dequeue_bulk函数是一个高效、无锁的函数,用于在单生产者、单消费者环形缓冲区中以原子操作的方式批量出队元素。它可以帮助提高多线程程序的性能,并保证线程安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值