[dpdk-dev] [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx

Xie, Huawei huawei.xie at intel.com
Mon Mar 7 08:52:22 CET 2016


On 2/18/2016 9:48 PM, Yuanhan Liu wrote:
> Current virtio_dev_merge_rx() implementation just looks like the
> old rte_vhost_dequeue_burst(), full of twisted logic, that you
> can see same code block in quite many different places.
>
> However, the logic of virtio_dev_merge_rx() is quite similar to
> virtio_dev_rx().  The big difference is that the mergeable one
> could allocate more than one available entries to hold the data.
> Fetching all available entries to vec_buf at once makes the
[...]
> -	}
> +static inline uint32_t __attribute__((always_inline))
> +copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
> +			    uint16_t res_start_idx, uint16_t res_end_idx,
> +			    struct rte_mbuf *m)
> +{
> +	struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
> +	uint32_t vec_idx = 0;
> +	uint16_t cur_idx = res_start_idx;
> +	uint64_t desc_addr;
> +	uint32_t mbuf_offset, mbuf_avail;
> +	uint32_t desc_offset, desc_avail;
> +	uint32_t cpy_len;
> +	uint16_t desc_idx, used_idx;
> +	uint32_t nr_used = 0;
>  
> -	cpy_len = RTE_MIN(vb_avail, seg_avail);
> +	if (m == NULL)
> +		return 0;

Is this inherited from old code? Let us remove the unnecessary check.
Caller ensures it is not NULL.

>  
> -	while (cpy_len > 0) {
> -		/* Copy mbuf data to vring buffer */
> -		rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
> -			rte_pktmbuf_mtod_offset(pkt, const void *, seg_offset),
> -			cpy_len);
> +	LOG_DEBUG(VHOST_DATA,
> +		"(%"PRIu64") Current Index %d| End Index %d\n",
> +		dev->device_fh, cur_idx, res_end_idx);
>  
> -		PRINT_PACKET(dev,
> -			(uintptr_t)(vb_addr + vb_offset),
> -			cpy_len, 0);
> +	desc_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
> +	rte_prefetch0((void *)(uintptr_t)desc_addr);
> +
> +	virtio_hdr.num_buffers = res_end_idx - res_start_idx;
> +	LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
> +		dev->device_fh, virtio_hdr.num_buffers);
>  
> -		seg_offset += cpy_len;
> -		vb_offset += cpy_len;
> -		seg_avail -= cpy_len;
> -		vb_avail -= cpy_len;
> -		entry_len += cpy_len;
> -
> -		if (seg_avail != 0) {
> -			/*
> -			 * The virtio buffer in this vring
> -			 * entry reach to its end.
> -			 * But the segment doesn't complete.
> -			 */
> -			if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
> -				VRING_DESC_F_NEXT) == 0) {
> +	virtio_enqueue_offload(m, &virtio_hdr.hdr);
> +	rte_memcpy((void *)(uintptr_t)desc_addr,
> +		(const void *)&virtio_hdr, vq->vhost_hlen);
> +	PRINT_PACKET(dev, (uintptr_t)desc_addr, vq->vhost_hlen, 0);
> +
> +	desc_avail  = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
> +	desc_offset = vq->vhost_hlen;

As we know we are in merge-able path, use sizeof(virtio_net_hdr) to save
one load for the header len.

> +
> +	mbuf_avail  = rte_pktmbuf_data_len(m);
> +	mbuf_offset = 0;
> +	while (1) {
> +		/* done with current desc buf, get the next one */
> +
[...]
> +		if (reserve_avail_buf_mergeable(vq, pkt_len, &start, &end) < 0)
> +			break;
>  
> +		nr_used = copy_mbuf_to_desc_mergeable(dev, vq, start, end,
> +						      pkts[pkt_idx]);

In which case couldn't we get nr_used from start and end?

>  		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))
> +		while (unlikely(vq->last_used_idx != start))
>  			rte_pause();
>  
> -		*(volatile uint16_t *)&vq->used->idx += entry_success;
> -		vq->last_used_idx = res_cur_idx;
> +		*(volatile uint16_t *)&vq->used->idx += nr_used;
> +		vq->last_used_idx = end;
>  	}
>  
> -merge_rx_exit:
>  	if (likely(pkt_idx)) {
>  		/* flush used->idx update before we read avail->flags. */
>  		rte_mb();



More information about the dev mailing list