vhost rxtx refactor
cover vhost rxtx refactor
Vhost rxtx code is derived from vhost-switch example, which is very likely the most messy code in DPDK. Unluckily, the move also brings over the bad merits: twisted logic, bad comments.
When I joined this team firstly, I was quite scared off by the messy and long vhost rxtx code. While adding the vhost-user live migration support, that I have to make fews changes to it, I then ventured to look at it again, to understand it better, in the meantime, to see if I can refactor it.
And, here you go.
The first 3 patches refactor 3 major functions at vhost_rxtx.c, respectively. It simplifies the code logic, making it more readable. On the other hand, it reduces code size, due to a lot of same code are removed.
Patch 4 gets rid of the rte_memcpy for virtio_hdr copy, which nearly saves 12K bytes of code size!
Till now, the code has been greatly reduced: 39348 vs 24179.
Patch 5 removes "unlikely" for VIRTIO_NET_F_MRG_RXBUF detection.
Note that the code could be further simplified or reduced. However, judging that it's a first try and it's the *key* data path, I guess it's okay to not be radical and stop here so far.
Another note is that we should add more secure checks at rxtx side. It could be a standalone task for v2.3, and this series is more about refactor, hence I leave it for future enhancement.
---
Yuanhan Liu (5):
vhost: refactor rte_vhost_dequeue_burst
vhost: refactor virtio_dev_rx
vhost: refactor virtio_dev_merge_rx
vhost: do not use rte_memcpy for virtio_hdr copy
vhost: don't use unlikely for VIRTIO_NET_F_MRG_RXBUF detection
lib/librte_vhost/vhost_rxtx.c | 959 ++++++++++++++++++------------------------ 1 file changed, 407 insertions(+), 552 deletions(-)
Signature · 1 line
1.9.0
PATCH 1/5 vhost: refactor rte_vhost_dequeue_burst
The current rte_vhost_dequeue_burst() implementation is a bit messy and logic twisted. And you could see repeat code here and there: it invokes rte_pktmbuf_alloc() 3 at three different places!
However, rte_vhost_dequeue_burst() acutally does a simple job: copy the packet data from vring desc to mbuf. What's tricky here is:
- desc buff could be chained (by desc->next field), so that you need fetch next one if current is wholly drained.
- One mbuf could not be big enough to hold all desc buff, hence you need to chain the mbuf as well, by the mbuf->next field.
Even though, the logic could be simple. Here is the pseudo code.
while (this_desc_is_not_drained_totally || has_next_desc) {
if (this_desc_has_drained_totally) {
this_desc = next_desc();
}
if (mbuf_has_no_room) {
mbuf = allocate_a_new_mbuf();
}
COPY(mbuf, desc);
}And this is how I refactored this patch.
Note that the old patch does a special handling for skipping virtio header. However, that could be simply done by adjusting desc_avail and desc_offset var:
/* Discard virtio header */ desc_avail = desc->len - vq->vhost_hlen; desc_offset = vq->vhost_hlen;
This refactor makes the code much more readable (IMO), yet it reduces code size (nearly 2K):
# without this patch
$ size /path/to/vhost_rxtx.o
text data bss dec hex filename
39348 0 0 39348 99b4 /path/to/vhost_rxtx.o # with this patch
$ size /path/to/vhost_rxtx.o
text data bss dec hex filename
37435 0 0 37435 923b /path/to/vhost_rxtx.o--- lib/librte_vhost/vhost_rxtx.c | 287 +++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 174 deletions(-)
lib/librte_vhost/vhost_rxtx.c +113 -174
@@ -43,6 +43,18 @@#define MAX_PKT_BURST 32 +#define COPY (dst, src) do { \ + cpy_len = RTE_MIN(desc_avail, mbuf_avail); \ + rte_memcpy((void *)(uintptr_t)(dst), \ + (const void *)(uintptr_t)(src), cpy_len); \ + \ + mbuf_avail -= cpy_len; \ + mbuf_offset += cpy_len; \ + desc_avail -= cpy_len; \ + desc_offset += cpy_len; \ +} while(0) + +static bool is_valid_virt_queue_idx (uint32_t idx,int is_tx,uint32_t qp_nb) {
@@ -568,19 +580,89 @@ rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,return virtio_dev_rx (dev, queue_id, pkts, count); } +static inline struct rte_mbuf * +copy_desc_to_mbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t desc_idx,struct rte_mempool *mbuf_pool) +{ +struct vring_desc *desc; +uint64_t desc_addr; +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; + +struct rte_mbuf *head =NULL ; +struct rte_mbuf *cur =NULL , *prev =NULL ; + + desc = &vq->desc[desc_idx]; + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +/* Discard virtio header */ + desc_avail = desc->len - vq->vhost_hlen; + desc_offset = vq->vhost_hlen; + + mbuf_avail =0 ; + mbuf_offset =0 ; +while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) { +/* This desc reachs to its end, get the next one */ +if (desc_avail ==0 ) { + desc = &vq->desc[desc->next]; + + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + + desc_offset =0 ; + desc_avail = desc->len; + +PRINT_PACKET (dev, (uintptr_t )desc_addr, desc->len,0 ); + } + +/* +* This mbuf reachs to its end, get a new one +* to hold more data. +*/ +if (mbuf_avail ==0 ) { + cur =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (!cur)) { +RTE_LOG (ERR ,VHOST_DATA ,"Failed to " +"allocate memory for mbuf.\n" ); +rte_pktmbuf_free (head); +return NULL ; + } +if (!head) { + head = cur; + }else { + prev->next = cur; + prev->data_len = mbuf_offset; + head->nb_segs +=1 ; + } + head->pkt_len += mbuf_offset; + prev = cur; + + mbuf_offset =0 ; + mbuf_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; + } + +COPY (rte_pktmbuf_mtod_offset (cur, uint64_t, mbuf_offset), + desc_addr + desc_offset); + } + prev->data_len = mbuf_offset; + head->pkt_len += mbuf_offset; + +return head; +} +uint16_t rte_vhost_dequeue_burst (struct virtio_net *dev,uint16_t queue_id,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) { -struct rte_mbuf *m, *prev;struct vhost_virtqueue *vq; -struct vring_desc *desc; -uint64_t vb_addr =0 ; -uint32_t head[MAX_PKT_BURST ]; +uint32_t desc_indexes[MAX_PKT_BURST ];uint32_t used_idx;uint32_t i; -uint16_t free_entries, entry_success =0 ; +uint16_t free_entries;uint16_t avail_idx; +struct rte_mbuf *m;if (unlikely (!is_valid_virt_queue_idx (queue_id,1 , dev->virt_qp_nb))) { RTE_LOG(ERR, VHOST_DATA,
@@ -594,192 +676,49 @@ rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,return 0 ; avail_idx = *((volatile uint16_t *)&vq->avail->idx); - -/* If there are no available buffers then return. */ -if (vq->last_used_idx == avail_idx) + free_entries = avail_idx - vq->last_used_idx; +if (free_entries ==0 )return 0 ; -LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, - dev->device_fh); +LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, dev->device_fh); -/* Prefetch available ring to retrieve head indexes. */ -rte_prefetch0 (&vq->avail->ring[vq->last_used_idx & (vq->size -1 )]); + used_idx = vq->last_used_idx & (vq->size -1 ); -/*get the number of free entries in the ring*/ - free_entries = (avail_idx - vq->last_used_idx); +/* Prefetch available ring to retrieve head indexes. */ +rte_prefetch0 (&vq->avail->ring[used_idx]); - free_entries =RTE_MIN (free_entries, count); -/* Limit to MAX_PKT_BURST. */ - free_entries =RTE_MIN (free_entries,MAX_PKT_BURST ); + count =RTE_MIN (count,MAX_PKT_BURST ); + count =RTE_MIN (count, free_entries); +LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") about to dequene %u buffers\n" , + dev->device_fh, count); -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Buffers available %d\n" , - dev->device_fh, free_entries);/* Retrieve all of the head indexes first to avoid caching issues. */ -for (i =0 ; i < free_entries; i++) - head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size -1 )]; +for (i =0 ; i < count; i++) { + desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) & + (vq->size -1 )]; + }/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success]]); +rte_prefetch0 (&vq->desc[desc_indexes[0 ]]);rte_prefetch0 (&vq->used->ring[vq->last_used_idx & (vq->size -1 )]); -while (entry_success < free_entries) { -uint32_t vb_avail, vb_offset; -uint32_t seg_avail, seg_offset; -uint32_t cpy_len; -uint32_t seg_num =0 ; -struct rte_mbuf *cur; -uint8_t alloc_err =0 ; - - desc = &vq->desc[head[entry_success]]; - -/* Discard first buffer as it is the virtio header */ -if (desc->flags &VRING_DESC_F_NEXT ) { - desc = &vq->desc[desc->next]; - vb_offset =0 ; - vb_avail = desc->len; - }else { - vb_offset = vq->vhost_hlen; - vb_avail = desc->len - vb_offset; - } - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - - used_idx = vq->last_used_idx & (vq->size -1 ); - -if (entry_success < (free_entries -1 )) { -/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success+1 ]]); -rte_prefetch0 (&vq->used->ring[(used_idx +1 ) & (vq->size -1 )]); - } - -/* Update used index buffer information. */ - vq->used->ring[used_idx].id = head[entry_success]; - vq->used->ring[used_idx].len =0 ; - -/* Allocate an mbuf and populate the structure. */ - m =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (m ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA , -"Failed to allocate memory for mbuf.\n" ); -break ; - } - seg_offset =0 ; - seg_avail = m->buf_len -RTE_PKTMBUF_HEADROOM ; - cpy_len =RTE_MIN (vb_avail, seg_avail); - -PRINT_PACKET (dev, (uintptr_t )vb_addr, desc->len,0 ); - - seg_num++; - cur = m; - prev = m; -while (cpy_len !=0 ) { - rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset), - (void *)((uintptr_t)(vb_addr + vb_offset)), - cpy_len); - - seg_offset += cpy_len; - vb_offset += cpy_len; - vb_avail -= cpy_len; - seg_avail -= cpy_len; - -if (vb_avail !=0 ) { -/* -* The segment reachs to its end, -* while the virtio buffer in TX vring has -* more data to be copied. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* Allocate mbuf and populate the structure. */ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA ,"Failed to " -"allocate memory for mbuf.\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - }else { -if (desc->flags &VRING_DESC_F_NEXT ) { -/* -* There are more virtio buffers in -* same vring entry need to be copied. -*/ -if (seg_avail ==0 ) { -/* -* The current segment hasn't -* room to accomodate more -* data. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* -* Allocate an mbuf and -* populate the structure. -*/ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR , -VHOST_DATA , -"Failed to " -"allocate memory " -"for mbuf\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - } - - desc = &vq->desc[desc->next]; - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - vb_offset =0 ; - vb_avail = desc->len; - -PRINT_PACKET (dev, (uintptr_t )vb_addr, - desc->len,0 ); - }else { -/* The whole packet completes. */ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; - vb_avail =0 ; - } - } - - cpy_len =RTE_MIN (vb_avail, seg_avail); - } - -if (unlikely (alloc_err ==1 )) +for (i =0 ; i < count; i++) { + m =copy_desc_to_mbuf (dev, vq, desc_indexes[i], mbuf_pool); +if (m ==NULL )break ; + pkts[i] = m; - m->nb_segs = seg_num; - - pkts[entry_success] = m; - vq->last_used_idx++; - entry_success++; + used_idx = vq->last_used_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_indexes[i]; + vq->used->ring[used_idx].len =0 ; }rte_compiler_barrier (); - vq->used->idx += entry_success; + vq->used->idx += i; +/* Kick guest if required. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return entry_success; + +return i; }
Signature · 1 line
1.9.0
Re: [PATCH 1/5] vhost: refactor rte_vhost_dequeue_burst
lib/librte_vhost/vhost_rxtx.c line 56
Quoted 11 lines · On Thu, 3 Dec 2015 14:06:09 +0800 Yuanhan Liu <[email protected]>…
lib/librte_vhost/vhost_rxtx.c +11
-0+#define COPY (dst, src) do { \ + cpy_len = RTE_MIN(desc_avail, mbuf_avail); \ + rte_memcpy((void *)(uintptr_t)(dst), \ + (const void *)(uintptr_t)(src), cpy_len); \ + \ + mbuf_avail -= cpy_len; \ + mbuf_offset += cpy_len; \ + desc_avail -= cpy_len; \ + desc_offset += cpy_len; \ +} while(0) +
I see lots of issues here.
All those void * casts are unnecessary, C casts arguements already. rte_memcpy is slower for constant size values than memcpy()
This macro violates the rule that ther should be no hidden variables in a macro. I.e you are assuming cpy_len, desc_avail, and mbuf_avail are defined in all code using the macro.
Why use an un-typed macro when an inline function would be just as fast and give type safety?
Re: [PATCH 1/5] vhost: refactor rte_vhost_dequeue_burst
Quoted 17 lines · Stephen Hemminger
lib/librte_vhost/vhost_rxtx.c line 56
Quoted 11 lines · On Thu, 3 Dec 2015 14:06:09 +0800 Yuanhan Liu <[email protected]>…
lib/librte_vhost/vhost_rxtx.c +11
-0+#define COPY (dst, src) do { \ + cpy_len = RTE_MIN(desc_avail, mbuf_avail); \ + rte_memcpy((void *)(uintptr_t)(dst), \ + (const void *)(uintptr_t)(src), cpy_len); \ + \ + mbuf_avail -= cpy_len; \ + mbuf_offset += cpy_len; \ + desc_avail -= cpy_len; \ + desc_offset += cpy_len; \ +} while(0) +I see lots of issues here.
Quoted 1 line
All those void * casts are unnecessary, C casts arguements already.
Hi Stephen,
Without the cast, the compile will not work, as dst is actully with uint64_t type.
Quoted 1 line
rte_memcpyis slower for constant size values thanmemcpy()
Sorry, what does it have something to do with this patch?
Quoted 3 lines
This macro violates the rule that ther should be no hidden variables in a macro. I.e you are assuming
cpy_len,desc_avail, andmbuf_availare defined in all code using the macro.
Yes, I'm aware of that. And I agree that it's not a good usage _in general_.
But I'm thinking that it's okay to use it here, for the three major functions has quite similar logic. And if my memory servers me right, there are also quite many codes like that in Linux kernel.
Quoted 2 lines
Why use an un-typed macro when an inline function would be just as fast and give type safety?
It references too many variables, upto 6. And there are 4 vars needed to be updated. Therefore, making it to be a function would make the caller long and ugly. That's why I was thinking to use a macro to remove few lines of repeat code.
So, if hidden var macro is forbidden here, I guess I would go with just unfolding those lines of code, but not introducing a helper function.
--yliu
Re: [PATCH 1/5] vhost: refactor rte_vhost_dequeue_burst
In reply to Yuanhan LiuReview
Quoted 1 line · On Thu, 3 Dec 2015 14:06:09 +0800 Yuanhan Liu <[email protected]>…
(quoted diff) +1
-0+ rte_prefetch0((void *)(uintptr_t)desc_addr);
Another unnecessary set of casts.
Re: [PATCH 1/5] vhost: refactor rte_vhost_dequeue_burst
In reply to Yuanhan LiuQuoted 6 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +4
-0+static inline struct rte_mbuf * +copy_desc_to_mbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t desc_idx,struct rte_mempool *mbuf_pool) +{
...
lib/librte_vhost/vhost_rxtx.c rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id, · line 601
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +7
-0+ + desc = &vq->desc[desc_idx]; + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +/* Discard virtio header */ + desc_avail = desc->len - vq->vhost_hlen;
If desc->len is zero (happens all the time when restarting DPDK apps in the guest) then desc_avail will be huge.
lib/librte_vhost/vhost_rxtx.c rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id, · line 606
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +5
-0+ desc_offset = vq->vhost_hlen; + + mbuf_avail =0 ; + mbuf_offset =0 ; +while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) {
+ /* This desc reachs to its end, get the next one */
lib/librte_vhost/vhost_rxtx.c rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id, · line 609
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +2
-0+if (desc_avail ==0 ) { + desc = &vq->desc[desc->next];
Need to check desc->next against vq->size.
There should be a limit on the number of descriptors in a chain to prevent an infinite loop.
uint16_t
lib/librte_vhost/vhost_rxtx.c rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, · line 681
Quoted 13 lines
rte_vhost_dequeue_burst(struct
virtio_net*dev,uint16_tqueue_id,
structrte_mempool*mbuf_pool, structrte_mbuf**pkts,uint16_t
count)
{
...avail_idx = *((volatile uint16_t *)&vq->avail->idx);lib/librte_vhost/vhost_rxtx.c +2
-3- -/* If there are no available buffers then return. */ -if (vq->last_used_idx == avail_idx) + free_entries = avail_idx - vq->last_used_idx; +if (free_entries ==0 )return 0 ;
A common problem is that avail_idx goes backwards when the guest zeroes its virtqueues. This function could check for free_entries > vq->size and reset vq->last_used_idx.
+ LOG_DEBUG(VHOST_DATA, "(%"PRIu64") about to dequene %u buffers\n",
Quoted 2 lines
+
dev->device_fh, count);
Typo at "dequene".
Re: [PATCH 1/5] vhost: refactor rte_vhost_dequeue_burst
Quoted 19 lines · Rich Lane
On Wed, Dec 2, 2015 at 10:06 PM, Yuanhan Liu <[email protected]> wrote:
+static inline struct
rte_mbuf*
+copy_desc_to_mbuf(structvirtio_net*dev, structvhost_virtqueue*vq, + uint16_tdesc_idx, structrte_mempool*mbuf_pool) +{...
+ + desc = &vq->desc[desc_idx]; + desc_addr = gpa_to_vva(dev, desc->addr); + rte_prefetch0((void *)(uintptr_t)desc_addr); + + /* Discard virtio header */ + desc_avail = desc->len - vq->vhost_hlen;
Quoted 2 lines
If
desc->lenis zero (happens all the time when restarting DPDK apps in the guest) thendesc_availwill be huge.
I'm aware of it; I have already noted that in the cover letter. This patch set is just a code refactor. Things like that will do a in a latter patch set. (The thing is that Huawei is very cagy about making any changes to vhost rxtx code, as it's the hot data path. So, I will not make any future changes base on this refactor, unless it's applied).
Quoted 13 lines
+ desc_offset = vq->vhost_hlen;
+
+ mbuf_avail = 0;
+ mbuf_offset = 0;
+ while (desc_avail || (desc->flags & VRING_DESC_F_NEXT) != 0) {
+ /* This desc reachs to its end, get the next one */
+ if (desc_avail == 0) {
+ desc = &vq->desc[desc->next];Quoted 1 line
Need to check
desc->nextagainstvq->size.
Thanks for the remind.
Quoted 20 lines
There should be a limit on the number of descriptors in a chain to prevent an infinite loop.
uint16_t rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count) { ... avail_idx = *((volatile uint16_t *)&vq->avail->idx); - - /* If there are no available buffers then return. */ - if (vq->last_used_idx == avail_idx) + free_entries = avail_idx - vq->last_used_idx; + if (free_entries == 0) return 0;
Quoted 3 lines
A common problem is that
avail_idxgoes backwards when the guest zeroes its virtqueues. This function could check forfree_entries>vq->sizeand resetvq->last_used_idx.
Thanks, but ditto, will consider it in another patch set.
Quoted 7 lines
+ LOG_DEBUG(VHOST_DATA, "(%"PRIu64") about to dequene %u buffers\n", + dev->device_fh, count);
Typo at "dequene".
Oops; thanks.
--yliu
Re: [PATCH 1/5] vhost: refactor rte_vhost_dequeue_burst
In reply to Yuanhan LiuQuoted 4 lines · Yuanhan Liu
Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 287 +++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 174 deletions(-)
Prefer to unroll copy_mbuf_to_desc and your COPY macro. It prevents us processing descriptors in a burst way in future.
Re: [PATCH 1/5] vhost: refactor rte_vhost_dequeue_burst
Quoted 7 lines · Huawei
Quoted 4 lines · Yuanhan Liu
Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 287 +++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 174 deletions(-)Prefer to unroll
copy_mbuf_to_descand your COPY macro. It prevents us
I'm okay to unroll COPY macro. But for copy_mbuf_to_desc, I prefer not to do that, unless it has a good reason.
Quoted 1 line
processing descriptors in a burst way in future.
So, do you have a plan?
--yliu
Re: [PATCH 1/5] vhost: refactor rte_vhost_dequeue_burst
Quoted 6 lines · Yuanhan Liu
Quoted 5 lines · Huawei
Quoted 4 lines · Yuanhan Liu
Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 287 +++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 174 deletions(-)
Quoted 6 lines
Quoted 1 line
Prefer to unroll
copy_mbuf_to_descand your COPY macro. It prevents usI'm okay to unroll COPY macro. But for
copy_mbuf_to_desc, I prefer not to do that, unless it has a good reason.Quoted 1 line
processing descriptors in a burst way in future.
So, do you have a plan?
I think it is OK. If we need unroll in future, we could do that then. I am open to this. Just my preference. I understand that wrapping makes code more readable.
Quoted 3 lines
--yliu
Re: [PATCH 1/5] vhost: refactor rte_vhost_dequeue_burst
Quoted 11 lines · Huawei
Quoted 10 lines · Yuanhan Liu
Quoted 6 lines · Huawei
Quoted 4 lines · Yuanhan Liu
Signed-off-by Yuanhan Liu <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 287 +++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 174 deletions(-)Prefer to unroll
copy_mbuf_to_descand your COPY macro. It prevents usI'm okay to unroll COPY macro. But for
copy_mbuf_to_desc, I prefer not to do that, unless it has a good reason.
Quoted 6 lines
Quoted 2 lines
Quoted 1 line
processing descriptors in a burst way in future.
So, do you have a plan?
I think it is OK. If we need unroll in future, we could do that then. I am open to this. Just my preference. I understand that wrapping makes code more readable.
Okay, let's consider it then: unroll would be easy after all.
--yliu
PATCH 2/5 vhost: refactor virtio_dev_rx
In reply to Yuanhan LiuThis is a simple refactor, as there isn't any twisted logic in old code. Here I just broke the code and introduced two helper functions, reserve_avail_buf() and copy_mbuf_to_desc() to make the code more readable.
It saves nearly 1K bytes of code size:
# without this patch
$ size /path/to/vhost_rxtx.o
text data bss dec hex filename
37435 0 0 37435 923b /path/to/vhost_rxtx.o # with this patch
$ size /path/to/vhost_rxtx.o
text data bss dec hex filename
36539 0 0 36539 8ebb /path/to/vhost_rxtx.o--- lib/librte_vhost/vhost_rxtx.c | 275 ++++++++++++++++++++---------------------- 1 file changed, 129 insertions(+), 146 deletions(-)
lib/librte_vhost/vhost_rxtx.c +129 -146
@@ -61,6 +61,107 @@ is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)return (is_tx ^ (idx &1 )) ==0 && idx < qp_nb *VIRTIO_QNUM ; } +static inline int __attribute__((always_inline)) +copy_mbuf_to_desc (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *m,uint16_t desc_idx,uint32_t *copied) +{ +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; +struct vring_desc *desc; +uint64_t desc_addr; +struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; + + desc = &vq->desc[desc_idx]; + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +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_offset = vq->vhost_hlen; + desc_avail = desc->len - vq->vhost_hlen; + + mbuf_avail =rte_pktmbuf_data_len (m); + mbuf_offset =0 ; +while (1 ) { +/* done with current mbuf, fetch next */ +if (mbuf_avail ==0 ) { + m = m->next; +if (m ==NULL ) +break ; + + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (m); + } + +/* done with current desc buf, fetch next */ +if (desc_avail ==0 ) { +if ((desc->flags &VRING_DESC_F_NEXT ) ==0 ) { +/* Room in vring buffer is not enough */ +return -1 ; + } + + desc = &vq->desc[desc->next]; + desc_addr =gpa_to_vva (dev, desc->addr); + desc_offset =0 ; + desc_avail = desc->len; + } + +COPY (desc_addr + desc_offset, +rte_pktmbuf_mtod_offset (m, uint64_t, mbuf_offset)); +PRINT_PACKET (dev, (uintptr_t )(desc_addr + desc_offset), + cpy_len,0 ); + } + *copied =rte_pktmbuf_pkt_len (m); + +return 0 ; +} + +/* +* As many data cores may want access to available buffers +* they need to be reserved. +*/ +static inline uint32_t +reserve_avail_buf (struct vhost_virtqueue *vq,uint32_t count, +uint16_t *start,uint16_t *end) +{ +uint16_t res_start_idx; +uint16_t res_end_idx; +uint16_t avail_idx; +uint16_t free_entries; +int success; + + count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); + +again: + res_start_idx = vq->last_used_idx_res; + avail_idx = *((volatile uint16_t *)&vq->avail->idx); + + free_entries = (avail_idx - res_start_idx); + count =RTE_MIN (count, free_entries); +if (count ==0 ) +return 0 ; + + res_end_idx = res_start_idx + count; + +/* +* update vq->last_used_idx_res atomically; try again if failed. +* +* TODO: Allow to disable cmpset if no concurrency in application. +*/ + success =rte_atomic16_cmpset (&vq->last_used_idx_res, + res_start_idx, res_end_idx); +if (!success) + goto again; + + *start = res_start_idx; + *end = res_end_idx; + +return count; +} +/** * This function adds buffers to the virtio devices RX virtqueue. Buffers can * be received from the physical port or from another virtio device. A packet
@@ -70,21 +171,12 @@ is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)*/ 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 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 ; +uint16_t res_start_idx, res_end_idx; +uint16_t desc_indexes[MAX_PKT_BURST ]; +uint32_t i;LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") virtio_dev_rx()\n" , dev->device_fh);if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) {
@@ -98,152 +190,43 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,if (unlikely (vq->enabled ==0 ))return 0 ; - 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 ; + count =reserve_avail_buf (vq, count, &res_start_idx, &res_end_idx); +if (count ==0 ) +return 0 ; -/* Get descriptor from available ring */ - desc = &vq->desc[head[packet_success]]; +LOG_DEBUG (VHOST_DATA , +"(%" PRIu64") res_start_idx %d| res_end_idx Index %d\n" , + dev->device_fh, res_start_idx, res_end_idx); - buff = pkts[packet_success]; +/* Retrieve all of the desc indexes first to avoid caching issues. */ +rte_prefetch0 (&vq->avail->ring[res_start_idx & (vq->size -1 )]); +for (i =0 ; i < count; i++) + desc_indexes[i] = vq->avail->ring[(res_start_idx + i) & (vq->size -1 )]; -/* 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); +rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); +for (i =0 ; i < count; i++) { +uint16_t desc_idx = desc_indexes[i]; +uint16_t used_idx = (res_start_idx + i) & (vq->size -1 ); +uint32_t copied; +int err; -/* Copy virtio_hdr to packet and increment buffer address */ - buff_hdr_addr = buff_addr; + err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx, &copied); -/* -* 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); + vq->used->ring[used_idx].id = desc_idx; +if (unlikely (err)) { + vq->used->ring[used_idx].len = vq->vhost_hlen; }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); + vq->used->ring[used_idx].len = copied + vq->vhost_hlen; } -/* 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]]); - } +if (i +1 < count) +rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); }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 != res_start_idx))rte_pause (); *(volatile uint16_t *)&vq->used->idx += count;
Signature · 1 line
1.9.0
Re: [PATCH 2/5] vhost: refactor virtio_dev_rx
Quoted 26 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +24
-0+static inline int __attribute__((always_inline)) +copy_mbuf_to_desc (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *m,uint16_t desc_idx,uint32_t *copied) +{ +while (1 ) { +/* done with current mbuf, fetch next */ +if (mbuf_avail ==0 ) { + m = m->next; +if (m ==NULL ) +break ; + + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (m); + } + +/* done with current desc buf, fetch next */ +if (desc_avail ==0 ) { +if ((desc->flags &VRING_DESC_F_NEXT ) ==0 ) { +/* Room in vring buffer is not enough */ +return -1 ; + } + + desc = &vq->desc[desc->next]; + desc_addr =gpa_to_vva (dev, desc->addr);
lib/librte_vhost/vhost_rxtx.c is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb) · line 117
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+ desc_offset =0 ; + desc_avail = desc->len; + } + +COPY (desc_addr + desc_offset, +rte_pktmbuf_mtod_offset (m, uint64_t, mbuf_offset)); +PRINT_PACKET (dev, (uintptr_t )(desc_addr + desc_offset), + cpy_len,0 ); + } + *copied =rte_pktmbuf_pkt_len (m);
AFAICT m will always be NULL at this point so the call to rte_pktmbuf_len will segfault.
Re: [PATCH 2/5] vhost: refactor virtio_dev_rx
Quoted 40 lines · Rich Lane
On Wed, Dec 2, 2015 at 10:06 PM, Yuanhan Liu <[email protected]> wrote:
+static inline int __attribute__((always_inline)) +copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, + struct rte_mbuf *m, uint16_t desc_idx, uint32_t *copied) +{ ... + while (1) { + /* done with current mbuf, fetch next */ + if (mbuf_avail == 0) { + m = m->next; + if (m == NULL) + break; + + mbuf_offset = 0; + mbuf_avail = rte_pktmbuf_data_len(m); + } + + /* done with current desc buf, fetch next */ + if (desc_avail == 0) { + if ((desc->flags & VRING_DESC_F_NEXT) == 0) { + /* Room in vring buffer is not enough */ + return -1; + } + + desc = &vq->desc[desc->next]; + desc_addr = gpa_to_vva(dev, desc->addr); + desc_offset = 0; + desc_avail = desc->len; + } + + COPY(desc_addr + desc_offset, + rte_pktmbuf_mtod_offset(m, uint64_t, mbuf_offset)); + PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset), + cpy_len, 0); + } + *copied = rte_pktmbuf_pkt_len(m);
Quoted 2 lines
AFAICT m will always be NULL at this point so the call to
rte_pktmbuf_lenwill segfault.
Right, I should move it in the beginning of this function.
--yliu
Re: [PATCH 2/5] vhost: refactor virtio_dev_rx
Hi Yuanhan,
2015-12-14 2:47 GMT+01:00 Yuanhan Liu <[email protected]>:
Quoted 1 line
Right, I should move it in the beginning of this function.
Any news about this refactoring ?
Signature · 10 lines
Jérôme Jutteau, Tel : 0826.206.307 (poste 304) IMPORTANT: The information contained in this message may be privileged and confidential and protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer.
Re: [PATCH 2/5] vhost: refactor virtio_dev_rx
Quoted 6 lines · Jérôme Jutteau
Hi Yuanhan,
2015-12-14 2:47 GMT+01:00 Yuanhan Liu <[email protected]>:
Quoted 1 line
Right, I should move it in the beginning of this function.
Any news about this refactoring ?
Hi Jérôme,
Thanks for showing interests in this patch set; I was waiting for Huawei's comments. And fortunately, he starts making comments.
--yliu
PATCH 3/5 vhost: refactor virtio_dev_merge_rx
In reply to Yuanhan LiuCurrent virtio_dev_merge_rx just looks like the old rte_vhost_dequeue_burst, twisted logic, that you can see same code block in quite many places.
However, the logic virtio_dev_merge_rx is quite similar to virtio_dev_rx. The big difference is that the meregeable one could allocate more than one available entries to hold the data. Fetching all available entries to vec_buf at once makes the difference a bit bigger then.
Anyway, it could be simpler, just like what we did for virtio_dev_rx(). The difference is that we need to update used ring properly, as there could be more than one entries:
while (1) {
if (this_desc_has_no_room) {
this_desc = fetch_next_from_vec_buf();
if (it is the last of a desc chain) {
update_used_ring();
}
}
if (this_mbuf_has_drained_totally) {
this_mbuf = fetch_next_mbuf();
if (this_mbuf == NULL)
break;
}
COPY(this_desc, this_mbuf);
}It reduces quite many lines of code, but it just saves a few bytes of code size (which is expected, btw):
# without this patch
$ size /path/to/vhost_rxtx.o
text data bss dec hex filename
36539 0 0 36539 8ebb /path/to/vhost_rxtx.o # with this patch
$ size /path/to/vhost_rxtx.o
text data bss dec hex filename
36171 0 0 36171 8d4b /path/to/vhost_rxtx.o--- lib/librte_vhost/vhost_rxtx.c | 389 +++++++++++++++++------------------------- 1 file changed, 158 insertions(+), 231 deletions(-)
lib/librte_vhost/vhost_rxtx.c +158 -231
@@ -241,235 +241,194 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,return count; } +static inline int +fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, +uint32_t *allocated,uint32_t *vec_idx) +{ +uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated; + +while (1 ) { +if (vec_id >=BUF_VECTOR_MAX ) +return -1 ; + + len += vq->desc[idx].len; + vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr; + vq->buf_vec[vec_id].buf_len = vq->desc[idx].len; + vq->buf_vec[vec_id].desc_idx = idx; + vec_id++; + +if ((vq->desc[idx].flags &VRING_DESC_F_NEXT ) ==0 ) +break ; + + idx = vq->desc[idx].next; + } + + *allocated = len; + *vec_idx = vec_id; + +return 0 ; +} + +/* +* As many data cores may want to access available buffers concurrently, +* they need to be reserved. +* +* Returns -1 on fail, 0 on success +*/ +static inline int +reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, +uint16_t *start,uint16_t *end) +{ +uint16_t res_start_idx; +uint16_t res_cur_idx; +uint16_t avail_idx; +uint32_t allocated; +uint32_t vec_idx; +uint16_t tries; + +again: + res_start_idx = vq->last_used_idx_res; + res_cur_idx = res_start_idx; + + allocated =0 ; + vec_idx =0 ; + tries =0 ; +while (1 ) { + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +if (unlikely (res_cur_idx == avail_idx)) { +LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Failed " +"to get enough desc from vring\n" , + dev->device_fh); +return -1 ; + } + +if (fill_vec_buf (vq, res_cur_idx, &allocated, &vec_idx) <0 ) +return -1 ; + + res_cur_idx++; + tries++; + +if (allocated >= size) +break ; + +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (tries >= vq->size) +return -1 ; + } + +/* +* update vq->last_used_idx_res atomically. +* retry again if failed. +*/ +if (rte_atomic16_cmpset (&vq->last_used_idx_res, + res_start_idx, res_cur_idx) ==0 ) + goto again; + + *start = res_start_idx; + *end = res_cur_idx; +return 0 ; +} +static inline uint32_t __attribute__((always_inline)) -copy_from_mbuf_to_vring (struct virtio_net *dev,uint32_t queue_id, -uint16_t res_base_idx,uint16_t res_end_idx, -struct rte_mbuf *pkt) +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 *pkt) { +struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 };uint32_t vec_idx =0 ; -uint32_t entry_success =0 ; -struct vhost_virtqueue *vq; -/* The virtio_hdr is initialised to 0. */ -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = { - {0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint16_t cur_idx = res_base_idx; -uint64_t vb_addr =0 ; -uint64_t vb_hdr_addr =0 ; -uint32_t seg_offset =0 ; -uint32_t vb_offset =0 ; -uint32_t seg_avail; -uint32_t vb_avail; -uint32_t cpy_len, entry_len; +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 ;if (pkt ==NULL )return 0 ; -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Current Index %d| " -"End Index %d\n" , +LOG_DEBUG (VHOST_DATA , +"(%" PRIu64") Current Index %d| End Index %d\n" , dev->device_fh, cur_idx, res_end_idx); -/* -* Convert from gpa to vva -* (guest physical addr -> vhost virtual addr) -*/ - vq = dev->virtqueue[queue_id]; + desc_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); - vb_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); - vb_hdr_addr = vb_addr; - -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); - virtio_hdr.num_buffers = res_end_idx - res_base_idx; + 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); -rte_memcpy ((void *)(uintptr_t )vb_hdr_addr, +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 ); -PRINT_PACKET (dev, (uintptr_t )vb_hdr_addr, vq->vhost_hlen,1 ); + desc_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen; + desc_offset = vq->vhost_hlen; - seg_avail =rte_pktmbuf_data_len (pkt); - vb_offset = vq->vhost_hlen; - vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen; + mbuf_avail =rte_pktmbuf_data_len (pkt); + mbuf_offset =0 ; +while (1 ) { +/* done with current desc buf, get the next one */ +if (desc_avail ==0 ) { + desc_idx = vq->buf_vec[vec_idx].desc_idx; - entry_len = vq->vhost_hlen; +if ((vq->desc[desc_idx].flags &VRING_DESC_F_NEXT ) ==0 ) { +/* Update used ring with desc information */ + used_idx = cur_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_idx; + vq->used->ring[used_idx].len = desc_offset; -if (vb_avail ==0 ) { -uint32_t desc_idx = - vq->buf_vec[vec_idx].desc_idx; + nr_used++; + } -if ((vq->desc[desc_idx].flags - &VRING_DESC_F_NEXT ) ==0 ) { -/* Update used ring with desc information */ - vq->used->ring[cur_idx & (vq->size -1 )].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[cur_idx & (vq->size -1 )].len - = entry_len; + vec_idx++; + desc_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); - entry_len =0 ; - cur_idx++; - entry_success++; +/* Prefetch buffer address. */ +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + desc_offset =0 ; + desc_avail = vq->buf_vec[vec_idx].buf_len; } - vec_idx++; - vb_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); - -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - vb_offset =0 ; - vb_avail = vq->buf_vec[vec_idx].buf_len; - } - - cpy_len =RTE_MIN (vb_avail, seg_avail); - -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); - -PRINT_PACKET (dev, - (uintptr_t )(vb_addr + vb_offset), - cpy_len,0 ); - - 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 ) { -/* Update used ring with desc information */ - vq->used->ring[cur_idx & (vq->size -1 )].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[cur_idx & (vq->size -1 )].len - = entry_len; - entry_len =0 ; - cur_idx++; - entry_success++; - } - - vec_idx++; - vb_addr =gpa_to_vva (dev, - vq->buf_vec[vec_idx].buf_addr); - vb_offset =0 ; - vb_avail = vq->buf_vec[vec_idx].buf_len; - cpy_len =RTE_MIN (vb_avail, seg_avail); - }else { -/* -* This current segment complete, need continue to -* check if the whole packet complete or not. -*/ +/* done with current mbuf, get the next one */ +if (mbuf_avail ==0 ) { pkt = pkt->next; -if (pkt !=NULL ) { -/* -* There are more segments. -*/ -if (vb_avail ==0 ) { -/* -* This current buffer from vring is -* used up, need fetch next buffer -* from buf_vec. -*/ -uint32_t desc_idx = - vq->buf_vec[vec_idx].desc_idx; - -if ((vq->desc[desc_idx].flags & -VRING_DESC_F_NEXT ) ==0 ) { -uint16_t wrapped_idx = - cur_idx & (vq->size -1 ); -/* -* Update used ring with the -* descriptor information -*/ - vq->used->ring[wrapped_idx].id - = desc_idx; - vq->used->ring[wrapped_idx].len - = entry_len; - entry_success++; - entry_len =0 ; - cur_idx++; - } - -/* Get next buffer from buf_vec. */ - vec_idx++; - vb_addr =gpa_to_vva (dev, - vq->buf_vec[vec_idx].buf_addr); - vb_avail = - vq->buf_vec[vec_idx].buf_len; - vb_offset =0 ; - } - - seg_offset =0 ; - seg_avail =rte_pktmbuf_data_len (pkt); - cpy_len =RTE_MIN (vb_avail, seg_avail); - }else { -/* -* This whole packet completes. -*/ -/* Update used ring with desc information */ - vq->used->ring[cur_idx & (vq->size -1 )].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[cur_idx & (vq->size -1 )].len - = entry_len; - entry_success++; +if (!pkt)break ; - } - } - } - -return entry_success; -} -static inline void __attribute__((always_inline)) -update_secure_len (struct vhost_virtqueue *vq,uint32_t id, -uint32_t *secure_len,uint32_t *vec_idx) -{ -uint16_t wrapped_idx = id & (vq->size -1 ); -uint32_t idx = vq->avail->ring[wrapped_idx]; -uint8_t next_desc; -uint32_t len = *secure_len; -uint32_t vec_id = *vec_idx; + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (pkt); + } -do { - next_desc =0 ; - len += vq->desc[idx].len; - vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - vq->buf_vec[vec_id].buf_len = vq->desc[idx].len; - vq->buf_vec[vec_id].desc_idx = idx; - vec_id++; +COPY (desc_addr + desc_offset, +rte_pktmbuf_mtod_offset (pkt, uint64_t, mbuf_offset)); +PRINT_PACKET (dev, (uintptr_t )(desc_addr + desc_offset), + cpy_len,0 ); + } -if (vq->desc[idx].flags &VRING_DESC_F_NEXT ) { - idx = vq->desc[idx].next; - next_desc =1 ; - } - }while (next_desc); + used_idx = cur_idx & (vq->size -1 ); + vq->used->ring[used_idx].id = vq->buf_vec[vec_idx].desc_idx; + vq->used->ring[used_idx].len = desc_offset; + nr_used++; - *secure_len = len; - *vec_idx = vec_id; +return nr_used; } -/* -* This function works for mergeable RX. -*/ static inline uint32_t __attribute__((always_inline))virtio_dev_merge_rx (struct virtio_net *dev,uint16_t queue_id,struct rte_mbuf **pkts,uint32_t count) {struct vhost_virtqueue *vq; -uint32_t pkt_idx =0 , entry_success =0 ; -uint16_t avail_idx; -uint16_t res_base_idx, res_cur_idx; -uint8_t success =0 ; +uint32_t pkt_idx =0 , nr_used =0 ; +uint16_t start, end;LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") virtio_dev_merge_rx()\n" , dev->device_fh);
@@ -485,62 +444,30 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,return 0 ; count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); -if (count ==0 )return 0 ;for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) {uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen; -do { -/* -* As many data cores may want access to available -* buffers, they need to be reserved. -*/ -uint32_t secure_len =0 ; -uint32_t vec_idx =0 ; - - res_base_idx = vq->last_used_idx_res; - res_cur_idx = res_base_idx; - -do { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (res_cur_idx == avail_idx)) { -LOG_DEBUG (VHOST_DATA , -"(%" PRIu64") Failed " -"to get enough desc from " -"vring\n" , - dev->device_fh); - goto merge_rx_exit; - }else { -update_secure_len (vq, res_cur_idx, &secure_len, &vec_idx); - res_cur_idx++; - } - }while (pkt_len > secure_len); - -/* vq->last_used_idx_res is atomically updated. */ - success =rte_atomic16_cmpset (&vq->last_used_idx_res, - res_base_idx, - res_cur_idx); - }while (success ==0 ); - - entry_success =copy_from_mbuf_to_vring (dev, queue_id, - res_base_idx, res_cur_idx, pkts[pkt_idx]); +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]);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 ();
Signature · 1 line
1.9.0
PATCH 4/5 vhost: do not use rte_memcpy for virtio_hdr copy
In reply to Yuanhan LiuFirst of all, rte_memcpy() is mostly useful for coping big packets by leveraging hardware advanced instructions like AVX. But for virtio net hdr, which is 12 bytes at most, invoking rte_memcpy() will not introduce any performance boost.
And, to my suprise, rte_memcpy() is huge. Since rte_memcpy() is inlined, it takes more space every time we call it at a different place. Replacing the two rte_memcpy() with directly copy saves nearly 12K bytes of code size!
# without this patch
$ size /path/to/vhost_rxtx.o
text data bss dec hex filename
36171 0 0 36171 8d4b /path/to/vhost_rxtx.o # with this patch
$ size /path/to/vhost_rxtx.o
text data bss dec hex filename
24179 0 0 24179 5e73 /path/to/vhost_rxtx.o--- lib/librte_vhost/vhost_rxtx.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
lib/librte_vhost/vhost_rxtx.c +13 -7
@@ -70,14 +70,17 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t cpy_len;struct vring_desc *desc;uint64_t desc_addr; -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; +struct virtio_net_hdr_mrg_rxbuf hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; desc = &vq->desc[desc_idx]; desc_addr =gpa_to_vva (dev, desc->addr);rte_prefetch0 ((void *)(uintptr_t )desc_addr); -rte_memcpy ((void *)(uintptr_t )desc_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); +if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + }PRINT_PACKET (dev, (uintptr_t )desc_addr, vq->vhost_hlen,0 ); desc_offset = vq->vhost_hlen;
@@ -340,7 +343,7 @@ 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 *pkt) { - struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0}; + struct virtio_net_hdr_mrg_rxbuf hdr = {{0, 0, 0, 0, 0, 0}, 0};uint32_t vec_idx =0 ;uint16_t cur_idx = res_start_idx;uint64_t desc_addr;
@@ -361,13 +364,16 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,rte_prefetch0 ((void *)(uintptr_t )desc_addr); - virtio_hdr.num_buffers = res_end_idx - res_start_idx; + 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); -rte_memcpy ((void *)(uintptr_t )desc_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); +if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + }PRINT_PACKET (dev, (uintptr_t )desc_addr, vq->vhost_hlen,0 ); desc_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
Signature · 1 line
1.9.0
Re: [PATCH 4/5] vhost: do not use rte_memcpy for virtio_hdr copy
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 83
Quoted 5 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +5
-0+if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + }
Thanks!
We might simplify this further. Just reset the first two fields flags and gso_type.
Re: [PATCH 4/5] vhost: do not use rte_memcpy for virtio_hdr copy
Quoted 10 lines · Huawei
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 83
Quoted 5 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +5
-0+if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + }Thanks!
We might simplify this further. Just reset the first two fields flags andgso_type.
What's this "simplification" for? Don't even to say that we will add TSO support, which modifies few more files, such as csum_start: reseting the first two fields only is wrong here.
--yliu
Re: [PATCH 4/5] vhost: do not use rte_memcpy for virtio_hdr copy
Quoted 10 lines · Yuanhan Liu
Quoted 9 lines · Huawei
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 83
Quoted 5 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +5
-0+if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + }Thanks!
We might simplify this further. Just reset the first two fields flags andgso_type.
Quoted 3 lines
What's this "simplification" for? Don't even to say that we will add TSO support, which modifies few more files, such as
csum_start: reseting the first two fields only is wrong here.
I know TSO before commenting, but at least in this implementation and this specific patch, i guess zeroing two fields are enough.
What is wrong resetting only two fields?
Quoted 3 lines
--yliu
Re: [PATCH 4/5] vhost: do not use rte_memcpy for virtio_hdr copy
Quoted 11 lines · Huawei
Quoted 10 lines · Yuanhan Liu
Quoted 9 lines · Huawei
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 83
Quoted 5 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +5
-0+if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + }Thanks!
We might simplify this further. Just reset the first two fields flags andgso_type.
Quoted 8 lines
Quoted 3 lines
What's this "simplification" for? Don't even to say that we will add TSO support, which modifies few more files, such as
csum_start: reseting the first two fields only is wrong here.I know TSO before commenting, but at least in this implementation and this specific patch, i guess zeroing two fields are enough.
What is wrong resetting only two fields?
I then have to ask "What is the benifit of resetting only two fields"? If doing so, we have to change it back for TSO. My proposal requires no extra change when adding TSO support.
--yliu
Re: [PATCH 4/5] vhost: do not use rte_memcpy for virtio_hdr copy
Quoted 15 lines · Yuanhan Liu
Quoted 14 lines · Huawei
Quoted 13 lines · Yuanhan Liu
Quoted 9 lines · Huawei
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 83
Quoted 5 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +5
-0+if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + }Thanks!
We might simplify this further. Just reset the first two fields flags andgso_type.What's this "simplification" for? Don't even to say that we will add TSO support, which modifies few more files, such as
csum_start: reseting the first two fields only is wrong here.
Quoted 7 lines
Quoted 4 lines
I know TSO before commenting, but at least in this implementation and this specific patch, i guess zeroing two fields are enough.
What is wrong resetting only two fields?
I then have to ask "What is the benifit of resetting only two fields"? If doing so, we have to change it back for TSO. My proposal requires no extra change when adding TSO support.
? Benefit is we save four unnecessary stores.
Quoted 3 lines
--yliu
Re: [PATCH 4/5] vhost: do not use rte_memcpy for virtio_hdr copy
Quoted 19 lines · Huawei
Quoted 18 lines · Yuanhan Liu
Quoted 17 lines · Huawei
Quoted 10 lines · Yuanhan Liu
Quoted 9 lines · Huawei
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 83
Quoted 5 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +5
-0+if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + }Thanks!
We might simplify this further. Just reset the first two fields flags andgso_type.Quoted 3 lines
What's this "simplification" for? Don't even to say that we will add TSO support, which modifies few more files, such as
csum_start: reseting the first two fields only is wrong here.I know TSO before commenting, but at least in this implementation and this specific patch, i guess zeroing two fields are enough.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
What is wrong resetting only two fields?
I then have to ask "What is the benifit of resetting only two fields"? If doing so, we have to change it back for TSO. My proposal requires no extra change when adding TSO support.
? Benefit is we save four unnecessary stores.
Hmm..., the hdr size is 12 bytes at most. I mean, does it really matter, coping 3 bytes, or coping 12 bytes in a row?
--yliu
PATCH 5/5 vhost: don't use unlikely for VIRTIO_NET_F_MRG_RXBUF detection
In reply to Yuanhan LiuVIRTIO_NET_F_MRG_RXBUF is a default feature supported by vhost. Adding unlikely for VIRTIO_NET_F_MRG_RXBUF detection doesn't make sense to me at all.
--- lib/librte_vhost/vhost_rxtx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost_rxtx.c +1 -1
@@ -490,7 +490,7 @@ uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id, struct rte_mbuf **pkts, uint16_t count) { - if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))) + if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) return virtio_dev_merge_rx(dev, queue_id, pkts, count); else return virtio_dev_rx(dev, queue_id, pkts, count);
Signature · 1 line
1.9.0
Hi Yuanhan,
2015-12-03 14:06, Yuanhan Liu:
Quoted 11 lines
Vhost rxtx code is derived from vhost-switch example, which is very likely the most messy code in DPDK. Unluckily, the move also brings over the bad merits: twisted logic, bad comments.
When I joined this team firstly, I was quite scared off by the messy and long vhost rxtx code. While adding the vhost-user live migration support, that I have to make fews changes to it, I then ventured to look at it again, to understand it better, in the meantime, to see if I can refactor it.
And, here you go.
There were several comments and a typo detected.
Please what is the status of this patchset?
Quoted 13 lines · Thomas Monjalon
Hi Yuanhan,
2015-12-03 14:06, Yuanhan Liu:
Quoted 10 lines
Vhost rxtx code is derived from vhost-switch example, which is very likely the most messy code in DPDK. Unluckily, the move also brings over the bad merits: twisted logic, bad comments.
When I joined this team firstly, I was quite scared off by the messy and long vhost rxtx code. While adding the vhost-user live migration support, that I have to make fews changes to it, I then ventured to look at it again, to understand it better, in the meantime, to see if I can refactor it.
Quoted 4 lines
Quoted 1 line
And, here you go.
There were several comments and a typo detected.
Please what is the status of this patchset?
Hi Thomas,
It was delayed; and I will address those comments and send out a new version recently.
--yliu
v2 cover vhost rxtx refactor
In reply to Yuanhan LiuHere is a patchset for refactoring vhost rxtx code, mainly for improving readability.
The first 3 patches refactor 3 major functions at vhost_rxtx.c, respectively. It simplifies the code logic, making it more readable. On the other hand, it reduces binary code size, due to a lot of duplicate code are removed.
Patch 4 gets rid of the rte_memcpy for virtio_hdr copy, which nearly saves 12K bytes of binary code size!
Till now, the code has been greatly reduced: 39k vs 24k.
Patch 5 removes "unlikely" for VIRTIO_NET_F_MRG_RXBUF detection.
Patch 6 and 7 do some sanity check for two desc fields, to make vhost robust and be protected from malicious guest or abnormal use cases.
It's key data path changes, and I have done some test to make sure it doesn't break something. However, more test are welcome!
---
Yuanhan Liu (7):
vhost: refactor rte_vhost_dequeue_burst
vhost: refactor virtio_dev_rx
vhost: refactor virtio_dev_merge_rx
vhost: do not use rte_memcpy for virtio_hdr copy
vhost: don't use unlikely for VIRTIO_NET_F_MRG_RXBUF detection
vhost: do sanity check for desc->len
vhost: do sanity check for desc->next
lib/librte_vhost/vhost_rxtx.c | 1000 ++++++++++++++++++----------------------- 1 file changed, 442 insertions(+), 558 deletions(-)
Signature · 1 line
1.9.0
PATCH v2 1/7 vhost: refactor rte_vhost_dequeue_burst
The current rte_vhost_dequeue_burst() implementation is a bit messy and logic twisted. And you could see repeat code here and there: it invokes rte_pktmbuf_alloc() three times at three different places!
However, rte_vhost_dequeue_burst() acutally does a simple job: copy the packet data from vring desc to mbuf. What's tricky here is:
- desc buff could be chained (by desc->next field), so that you need fetch next one if current is wholly drained.
- One mbuf could not be big enough to hold all desc buff, hence you need to chain the mbuf as well, by the mbuf->next field.
Even though, the logic could be simple. Here is the pseudo code.
while (this_desc_is_not_drained_totally || has_next_desc) {
if (this_desc_has_drained_totally) {
this_desc = next_desc();
}
if (mbuf_has_no_room) {
mbuf = allocate_a_new_mbuf();
}
COPY(mbuf, desc);
}And this is how I refactored rte_vhost_dequeue_burst.
Note that the old patch does a special handling for skipping virtio header. However, that could be simply done by adjusting desc_avail and desc_offset var:
desc_avail = desc->len - vq->vhost_hlen; desc_offset = vq->vhost_hlen;
This refactor makes the code much more readable (IMO), yet it reduces binary code size (nearly 2K).
---
v2: - fix potential NULL dereference bug of var "prev" and "head"
--- lib/librte_vhost/vhost_rxtx.c | 297 +++++++++++++++++------------------------- 1 file changed, 116 insertions(+), 181 deletions(-)
lib/librte_vhost/vhost_rxtx.c +116 -181
@@ -702,21 +702,104 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) } } +static inline struct rte_mbuf * +copy_desc_to_mbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t desc_idx,struct rte_mempool *mbuf_pool) +{ +struct vring_desc *desc; +uint64_t desc_addr; +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; +struct rte_mbuf *head =NULL ; +struct rte_mbuf *cur =NULL , *prev =NULL ; +struct virtio_net_hdr *hdr; + + desc = &vq->desc[desc_idx]; + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +/* Retrieve virtio net header */ + hdr = (struct virtio_net_hdr *)((uintptr_t )desc_addr); + desc_avail = desc->len - vq->vhost_hlen; + desc_offset = vq->vhost_hlen; + + mbuf_avail =0 ; + mbuf_offset =0 ; +while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) { +/* This desc reachs to its end, get the next one */ +if (desc_avail ==0 ) { + desc = &vq->desc[desc->next]; + + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + + desc_offset =0 ; + desc_avail = desc->len; + +PRINT_PACKET (dev, (uintptr_t )desc_addr, desc->len,0 ); + } + +/* +* This mbuf reachs to its end, get a new one +* to hold more data. +*/ +if (mbuf_avail ==0 ) { + cur =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (!cur)) { +RTE_LOG (ERR ,VHOST_DATA ,"Failed to " +"allocate memory for mbuf.\n" ); +if (head) +rte_pktmbuf_free (head); +return NULL ; + } +if (!head) { + head = cur; + }else { + prev->next = cur; + prev->data_len = mbuf_offset; + head->nb_segs +=1 ; + } + head->pkt_len += mbuf_offset; + prev = cur; + + mbuf_offset =0 ; + mbuf_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; + } + + cpy_len =RTE_MIN (desc_avail, mbuf_avail); + rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset), + (void *)((uintptr_t)(desc_addr + desc_offset)), + cpy_len); + + mbuf_avail -= cpy_len; + mbuf_offset += cpy_len; + desc_avail -= cpy_len; + desc_offset += cpy_len; + } + +if (prev) { + prev->data_len = mbuf_offset; + head->pkt_len += mbuf_offset; + +if (hdr->flags !=0 || hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE ) +vhost_dequeue_offload (hdr, head); + } + +return head; +} +uint16_t rte_vhost_dequeue_burst (struct virtio_net *dev,uint16_t queue_id,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) { -struct rte_mbuf *m, *prev;struct vhost_virtqueue *vq; -struct vring_desc *desc; -uint64_t vb_addr =0 ; -uint64_t vb_net_hdr_addr =0 ; -uint32_t head[MAX_PKT_BURST ]; +uint32_t desc_indexes[MAX_PKT_BURST ];uint32_t used_idx;uint32_t i; -uint16_t free_entries, entry_success =0 ; +uint16_t free_entries;uint16_t avail_idx; -struct virtio_net_hdr *hdr =NULL ; +struct rte_mbuf *m;if (unlikely (!is_valid_virt_queue_idx (queue_id,1 , dev->virt_qp_nb))) { RTE_LOG(ERR, VHOST_DATA,
@@ -730,197 +813,49 @@ rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,return 0 ; avail_idx = *((volatile uint16_t *)&vq->avail->idx); - -/* If there are no available buffers then return. */ -if (vq->last_used_idx == avail_idx) + free_entries = avail_idx - vq->last_used_idx; +if (free_entries ==0 )return 0 ; -LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, - dev->device_fh); +LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, dev->device_fh); -/* Prefetch available ring to retrieve head indexes. */ -rte_prefetch0 (&vq->avail->ring[vq->last_used_idx & (vq->size -1 )]); + used_idx = vq->last_used_idx & (vq->size -1 ); -/*get the number of free entries in the ring*/ - free_entries = (avail_idx - vq->last_used_idx); +/* Prefetch available ring to retrieve head indexes. */ +rte_prefetch0 (&vq->avail->ring[used_idx]); - free_entries =RTE_MIN (free_entries, count); -/* Limit to MAX_PKT_BURST. */ - free_entries =RTE_MIN (free_entries,MAX_PKT_BURST ); + count =RTE_MIN (count,MAX_PKT_BURST ); + count =RTE_MIN (count, free_entries); +LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") about to dequeue %u buffers\n" , + dev->device_fh, count); -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Buffers available %d\n" , - dev->device_fh, free_entries);/* Retrieve all of the head indexes first to avoid caching issues. */ -for (i =0 ; i < free_entries; i++) - head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size -1 )]; +for (i =0 ; i < count; i++) { + desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) & + (vq->size -1 )]; + }/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success]]); +rte_prefetch0 (&vq->desc[desc_indexes[0 ]]);rte_prefetch0 (&vq->used->ring[vq->last_used_idx & (vq->size -1 )]); -while (entry_success < free_entries) { -uint32_t vb_avail, vb_offset; -uint32_t seg_avail, seg_offset; -uint32_t cpy_len; -uint32_t seg_num =0 ; -struct rte_mbuf *cur; -uint8_t alloc_err =0 ; - - desc = &vq->desc[head[entry_success]]; - - vb_net_hdr_addr =gpa_to_vva (dev, desc->addr); - hdr = (struct virtio_net_hdr *)((uintptr_t )vb_net_hdr_addr); - -/* Discard first buffer as it is the virtio header */ -if (desc->flags &VRING_DESC_F_NEXT ) { - desc = &vq->desc[desc->next]; - vb_offset =0 ; - vb_avail = desc->len; - }else { - vb_offset = vq->vhost_hlen; - vb_avail = desc->len - vb_offset; - } - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - - used_idx = vq->last_used_idx & (vq->size -1 ); - -if (entry_success < (free_entries -1 )) { -/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success+1 ]]); -rte_prefetch0 (&vq->used->ring[(used_idx +1 ) & (vq->size -1 )]); - } - -/* Update used index buffer information. */ - vq->used->ring[used_idx].id = head[entry_success]; - vq->used->ring[used_idx].len =0 ; - -/* Allocate an mbuf and populate the structure. */ - m =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (m ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA , -"Failed to allocate memory for mbuf.\n" ); -break ; - } - seg_offset =0 ; - seg_avail = m->buf_len -RTE_PKTMBUF_HEADROOM ; - cpy_len =RTE_MIN (vb_avail, seg_avail); - -PRINT_PACKET (dev, (uintptr_t )vb_addr, desc->len,0 ); - - seg_num++; - cur = m; - prev = m; -while (cpy_len !=0 ) { - rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset), - (void *)((uintptr_t)(vb_addr + vb_offset)), - cpy_len); - - seg_offset += cpy_len; - vb_offset += cpy_len; - vb_avail -= cpy_len; - seg_avail -= cpy_len; - -if (vb_avail !=0 ) { -/* -* The segment reachs to its end, -* while the virtio buffer in TX vring has -* more data to be copied. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* Allocate mbuf and populate the structure. */ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA ,"Failed to " -"allocate memory for mbuf.\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - }else { -if (desc->flags &VRING_DESC_F_NEXT ) { -/* -* There are more virtio buffers in -* same vring entry need to be copied. -*/ -if (seg_avail ==0 ) { -/* -* The current segment hasn't -* room to accomodate more -* data. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* -* Allocate an mbuf and -* populate the structure. -*/ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR , -VHOST_DATA , -"Failed to " -"allocate memory " -"for mbuf\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - } - - desc = &vq->desc[desc->next]; - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - vb_offset =0 ; - vb_avail = desc->len; - -PRINT_PACKET (dev, (uintptr_t )vb_addr, - desc->len,0 ); - }else { -/* The whole packet completes. */ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; - vb_avail =0 ; - } - } - - cpy_len =RTE_MIN (vb_avail, seg_avail); - } - -if (unlikely (alloc_err ==1 )) +for (i =0 ; i < count; i++) { + m =copy_desc_to_mbuf (dev, vq, desc_indexes[i], mbuf_pool); +if (m ==NULL )break ; + pkts[i] = m; - m->nb_segs = seg_num; -if ((hdr->flags !=0 ) || (hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE )) -vhost_dequeue_offload (hdr, m); - - pkts[entry_success] = m; - vq->last_used_idx++; - entry_success++; + used_idx = vq->last_used_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_indexes[i]; + vq->used->ring[used_idx].len =0 ; }rte_compiler_barrier (); - vq->used->idx += entry_success; + vq->used->idx += i; +/* Kick guest if required. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return entry_success; + +return i; }
Signature · 1 line
1.9.0
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 65 lines · Yuanhan Liu
The current
rte_vhost_dequeue_burst()implementation is a bit messy and logic twisted. And you could see repeat code here and there: it invokesrte_pktmbuf_alloc()three times at three different places!However,
rte_vhost_dequeue_burst()acutally does a simple job: copy the packet data from vring desc to mbuf. What's tricky here is:- desc buff could be chained (by
desc->nextfield), so that you need fetch next one if current is wholly drained.- One mbuf could not be big enough to hold all desc buff, hence you need to chain the mbuf as well, by the
mbuf->nextfield.Even though, the logic could be simple. Here is the pseudo code.
while (this_desc_is_not_drained_totally || has_next_desc) { if (this_desc_has_drained_totally) { this_desc = next_desc(); } if (mbuf_has_no_room) { mbuf = allocate_a_new_mbuf(); } COPY(mbuf, desc); }And this is how I refactored
rte_vhost_dequeue_burst.Note that the old patch does a special handling for skipping virtio header. However, that could be simply done by adjusting
desc_availanddesc_offsetvar:desc_avail = desc->len - vq->vhost_hlen; desc_offset = vq->vhost_hlen;This refactor makes the code much more readable (IMO), yet it reduces binary code size (nearly 2K).
Signed-off-by Yuanhan Liu <[email protected]>---
v2: - fix potential NULL dereference bug of var "prev" and "head"
--- lib/librte_vhost/vhost_rxtx.c | 297 +++++++++++++++++------------------------- 1 file changed, 116 insertions(+), 181 deletions(-)lib/librte_vhost/vhost_rxtx.c +10
-0@@ -702,21 +702,104 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) } } +static inline struct rte_mbuf * +copy_desc_to_mbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t desc_idx,struct rte_mempool *mbuf_pool) +{ +struct vring_desc *desc; +uint64_t desc_addr; +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; +struct rte_mbuf *head =NULL ;
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 724
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+struct rte_mbuf *cur =NULL , *prev =NULL ; +struct virtio_net_hdr *hdr; + + desc = &vq->desc[desc_idx]; + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +/* Retrieve virtio net header */ + hdr = (struct virtio_net_hdr *)((uintptr_t )desc_addr); + desc_avail = desc->len - vq->vhost_hlen;
There is a serious bug here, desc->len - vq->vhost_len could overflow. VM could easily create this case. Let us fix it here.
Quoted 312 lines
lib/librte_vhost/vhost_rxtx.c +96
-181+ desc_offset = vq->vhost_hlen; + + mbuf_avail =0 ; + mbuf_offset =0 ; +while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) { +/* This desc reachs to its end, get the next one */ +if (desc_avail ==0 ) { + desc = &vq->desc[desc->next]; + + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + + desc_offset =0 ; + desc_avail = desc->len; + +PRINT_PACKET (dev, (uintptr_t )desc_addr, desc->len,0 ); + } + +/* +* This mbuf reachs to its end, get a new one +* to hold more data. +*/ +if (mbuf_avail ==0 ) { + cur =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (!cur)) { +RTE_LOG (ERR ,VHOST_DATA ,"Failed to " +"allocate memory for mbuf.\n" ); +if (head) +rte_pktmbuf_free (head); +return NULL ; + } +if (!head) { + head = cur; + }else { + prev->next = cur; + prev->data_len = mbuf_offset; + head->nb_segs +=1 ; + } + head->pkt_len += mbuf_offset; + prev = cur; + + mbuf_offset =0 ; + mbuf_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; + } + + cpy_len =RTE_MIN (desc_avail, mbuf_avail); + rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset), + (void *)((uintptr_t)(desc_addr + desc_offset)), + cpy_len); + + mbuf_avail -= cpy_len; + mbuf_offset += cpy_len; + desc_avail -= cpy_len; + desc_offset += cpy_len; + } + +if (prev) { + prev->data_len = mbuf_offset; + head->pkt_len += mbuf_offset; + +if (hdr->flags !=0 || hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE ) +vhost_dequeue_offload (hdr, head); + } + +return head; +} +uint16_t rte_vhost_dequeue_burst (struct virtio_net *dev,uint16_t queue_id,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) { -struct rte_mbuf *m, *prev;struct vhost_virtqueue *vq; -struct vring_desc *desc; -uint64_t vb_addr =0 ; -uint64_t vb_net_hdr_addr =0 ; -uint32_t head[MAX_PKT_BURST ]; +uint32_t desc_indexes[MAX_PKT_BURST ];uint32_t used_idx;uint32_t i; -uint16_t free_entries, entry_success =0 ; +uint16_t free_entries;uint16_t avail_idx; -struct virtio_net_hdr *hdr =NULL ; +struct rte_mbuf *m;if (unlikely (!is_valid_virt_queue_idx (queue_id,1 , dev->virt_qp_nb))) { RTE_LOG(ERR, VHOST_DATA,@@ -730,197 +813,49 @@ rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,return 0 ; avail_idx = *((volatile uint16_t *)&vq->avail->idx); - -/* If there are no available buffers then return. */ -if (vq->last_used_idx == avail_idx) + free_entries = avail_idx - vq->last_used_idx; +if (free_entries ==0 )return 0 ; -LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, - dev->device_fh); +LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, dev->device_fh); -/* Prefetch available ring to retrieve head indexes. */ -rte_prefetch0 (&vq->avail->ring[vq->last_used_idx & (vq->size -1 )]); + used_idx = vq->last_used_idx & (vq->size -1 ); -/*get the number of free entries in the ring*/ - free_entries = (avail_idx - vq->last_used_idx); +/* Prefetch available ring to retrieve head indexes. */ +rte_prefetch0 (&vq->avail->ring[used_idx]); - free_entries =RTE_MIN (free_entries, count); -/* Limit to MAX_PKT_BURST. */ - free_entries =RTE_MIN (free_entries,MAX_PKT_BURST ); + count =RTE_MIN (count,MAX_PKT_BURST ); + count =RTE_MIN (count, free_entries); +LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") about to dequeue %u buffers\n" , + dev->device_fh, count); -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Buffers available %d\n" , - dev->device_fh, free_entries);/* Retrieve all of the head indexes first to avoid caching issues. */ -for (i =0 ; i < free_entries; i++) - head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size -1 )]; +for (i =0 ; i < count; i++) { + desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) & + (vq->size -1 )]; + }/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success]]); +rte_prefetch0 (&vq->desc[desc_indexes[0 ]]);rte_prefetch0 (&vq->used->ring[vq->last_used_idx & (vq->size -1 )]); -while (entry_success < free_entries) { -uint32_t vb_avail, vb_offset; -uint32_t seg_avail, seg_offset; -uint32_t cpy_len; -uint32_t seg_num =0 ; -struct rte_mbuf *cur; -uint8_t alloc_err =0 ; - - desc = &vq->desc[head[entry_success]]; - - vb_net_hdr_addr =gpa_to_vva (dev, desc->addr); - hdr = (struct virtio_net_hdr *)((uintptr_t )vb_net_hdr_addr); - -/* Discard first buffer as it is the virtio header */ -if (desc->flags &VRING_DESC_F_NEXT ) { - desc = &vq->desc[desc->next]; - vb_offset =0 ; - vb_avail = desc->len; - }else { - vb_offset = vq->vhost_hlen; - vb_avail = desc->len - vb_offset; - } - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - - used_idx = vq->last_used_idx & (vq->size -1 ); - -if (entry_success < (free_entries -1 )) { -/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success+1 ]]); -rte_prefetch0 (&vq->used->ring[(used_idx +1 ) & (vq->size -1 )]); - } - -/* Update used index buffer information. */ - vq->used->ring[used_idx].id = head[entry_success]; - vq->used->ring[used_idx].len =0 ; - -/* Allocate an mbuf and populate the structure. */ - m =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (m ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA , -"Failed to allocate memory for mbuf.\n" ); -break ; - } - seg_offset =0 ; - seg_avail = m->buf_len -RTE_PKTMBUF_HEADROOM ; - cpy_len =RTE_MIN (vb_avail, seg_avail); - -PRINT_PACKET (dev, (uintptr_t )vb_addr, desc->len,0 ); - - seg_num++; - cur = m; - prev = m; -while (cpy_len !=0 ) { - rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset), - (void *)((uintptr_t)(vb_addr + vb_offset)), - cpy_len); - - seg_offset += cpy_len; - vb_offset += cpy_len; - vb_avail -= cpy_len; - seg_avail -= cpy_len; - -if (vb_avail !=0 ) { -/* -* The segment reachs to its end, -* while the virtio buffer in TX vring has -* more data to be copied. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* Allocate mbuf and populate the structure. */ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA ,"Failed to " -"allocate memory for mbuf.\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - }else { -if (desc->flags &VRING_DESC_F_NEXT ) { -/* -* There are more virtio buffers in -* same vring entry need to be copied. -*/ -if (seg_avail ==0 ) { -/* -* The current segment hasn't -* room to accomodate more -* data. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* -* Allocate an mbuf and -* populate the structure. -*/ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR , -VHOST_DATA , -"Failed to " -"allocate memory " -"for mbuf\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - } - - desc = &vq->desc[desc->next]; - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - vb_offset =0 ; - vb_avail = desc->len; - -PRINT_PACKET (dev, (uintptr_t )vb_addr, - desc->len,0 ); - }else { -/* The whole packet completes. */ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; - vb_avail =0 ; - } - } - - cpy_len =RTE_MIN (vb_avail, seg_avail); - } - -if (unlikely (alloc_err ==1 )) +for (i =0 ; i < count; i++) { + m =copy_desc_to_mbuf (dev, vq, desc_indexes[i], mbuf_pool); +if (m ==NULL )break ; + pkts[i] = m; - m->nb_segs = seg_num; -if ((hdr->flags !=0 ) || (hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE )) -vhost_dequeue_offload (hdr, m); - - pkts[entry_success] = m; - vq->last_used_idx++; - entry_success++; + used_idx = vq->last_used_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_indexes[i]; + vq->used->ring[used_idx].len =0 ; }rte_compiler_barrier (); - vq->used->idx += entry_success; + vq->used->idx += i; +/* Kick guest if required. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return entry_success; + +return i; }
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 71 lines · Huawei
Quoted 70 lines · Yuanhan Liu
The current
rte_vhost_dequeue_burst()implementation is a bit messy and logic twisted. And you could see repeat code here and there: it invokesrte_pktmbuf_alloc()three times at three different places!However,
rte_vhost_dequeue_burst()acutally does a simple job: copy the packet data from vring desc to mbuf. What's tricky here is:- desc buff could be chained (by
desc->nextfield), so that you need fetch next one if current is wholly drained.- One mbuf could not be big enough to hold all desc buff, hence you need to chain the mbuf as well, by the
mbuf->nextfield.Even though, the logic could be simple. Here is the pseudo code.
while (this_desc_is_not_drained_totally || has_next_desc) { if (this_desc_has_drained_totally) { this_desc = next_desc(); } if (mbuf_has_no_room) { mbuf = allocate_a_new_mbuf(); } COPY(mbuf, desc); }And this is how I refactored
rte_vhost_dequeue_burst.Note that the old patch does a special handling for skipping virtio header. However, that could be simply done by adjusting
desc_availanddesc_offsetvar:desc_avail = desc->len - vq->vhost_hlen; desc_offset = vq->vhost_hlen;This refactor makes the code much more readable (IMO), yet it reduces binary code size (nearly 2K).
Signed-off-by Yuanhan Liu <[email protected]>---
v2: - fix potential NULL dereference bug of var "prev" and "head"
--- lib/librte_vhost/vhost_rxtx.c | 297 +++++++++++++++++------------------------- 1 file changed, 116 insertions(+), 181 deletions(-)lib/librte_vhost/vhost_rxtx.c +15
-0@@ -702,21 +702,104 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) } } +static inline struct rte_mbuf * +copy_desc_to_mbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t desc_idx,struct rte_mempool *mbuf_pool) +{ +struct vring_desc *desc; +uint64_t desc_addr; +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; +struct rte_mbuf *head =NULL ; +struct rte_mbuf *cur =NULL , *prev =NULL ; +struct virtio_net_hdr *hdr; + + desc = &vq->desc[desc_idx]; + desc_addr =gpa_to_vva (dev, desc->addr);
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 724
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +5
-0+rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +/* Retrieve virtio net header */ + hdr = (struct virtio_net_hdr *)((uintptr_t )desc_addr); + desc_avail = desc->len - vq->vhost_hlen;There is a serious bug here,
desc->len-vq->vhost_lencould overflow. VM could easily create this case. Let us fix it here.
Nope, this issue has been there since the beginning, and this patch is a refactor: we should not bring any functional changes. Therefore, we should not fix it here.
And actually, it's been fixed in the 6th patch in this series:
[PATCH v2 6/7] vhost: do sanity check for desc->len --yliu
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 79 lines · Yuanhan Liu
Quoted 70 lines · Huawei
Quoted 69 lines · Yuanhan Liu
The current
rte_vhost_dequeue_burst()implementation is a bit messy and logic twisted. And you could see repeat code here and there: it invokesrte_pktmbuf_alloc()three times at three different places!However,
rte_vhost_dequeue_burst()acutally does a simple job: copy the packet data from vring desc to mbuf. What's tricky here is:- desc buff could be chained (by
desc->nextfield), so that you need fetch next one if current is wholly drained.- One mbuf could not be big enough to hold all desc buff, hence you need to chain the mbuf as well, by the
mbuf->nextfield.Even though, the logic could be simple. Here is the pseudo code.
while (this_desc_is_not_drained_totally || has_next_desc) { if (this_desc_has_drained_totally) { this_desc = next_desc(); } if (mbuf_has_no_room) { mbuf = allocate_a_new_mbuf(); } COPY(mbuf, desc); }And this is how I refactored
rte_vhost_dequeue_burst.Note that the old patch does a special handling for skipping virtio header. However, that could be simply done by adjusting
desc_availanddesc_offsetvar:desc_avail = desc->len - vq->vhost_hlen; desc_offset = vq->vhost_hlen;This refactor makes the code much more readable (IMO), yet it reduces binary code size (nearly 2K).
Signed-off-by Yuanhan Liu <[email protected]>---
v2: - fix potential NULL dereference bug of var "prev" and "head"
--- lib/librte_vhost/vhost_rxtx.c | 297 +++++++++++++++++------------------------- 1 file changed, 116 insertions(+), 181 deletions(-)lib/librte_vhost/vhost_rxtx.c +14
-0@@ -702,21 +702,104 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) } } +static inline struct rte_mbuf * +copy_desc_to_mbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t desc_idx,struct rte_mempool *mbuf_pool) +{ +struct vring_desc *desc; +uint64_t desc_addr; +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; +struct rte_mbuf *head =NULL ; +struct rte_mbuf *cur =NULL , *prev =NULL ; +struct virtio_net_hdr *hdr; + + desc = &vq->desc[desc_idx];Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 724
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c +6
-0+ desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +/* Retrieve virtio net header */ + hdr = (struct virtio_net_hdr *)((uintptr_t )desc_addr); + desc_avail = desc->len - vq->vhost_hlen;There is a serious bug here,
desc->len-vq->vhost_lencould overflow. VM could easily create this case. Let us fix it here.
Quoted 3 lines
Nope, this issue has been there since the beginning, and this patch is a refactor: we should not bring any functional changes. Therefore, we should not fix it here.
No, I don't mean exactly fixing in this patch but in this series.
Besides, from refactoring's perspective, actually we could make things further much simpler and more readable. Both the desc chains and mbuf could be converted into iovec, then both dequeue(copy_desc_to_mbuf) and enqueue(copy_mbuf_to_desc) could use the commonly used iovec copying algorithms As data path are performance oriented, let us stop here.
Quoted 2 lines
And actually, it's been fixed in the 6th patch in this series:
Will check that.
Quoted 5 lines
[PATCH v2 6/7] vhost: do sanity check for desc->len
--yliuRe: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 84 lines · Huawei
Quoted 79 lines · Yuanhan Liu
Quoted 70 lines · Huawei
Quoted 69 lines · Yuanhan Liu
The current
rte_vhost_dequeue_burst()implementation is a bit messy and logic twisted. And you could see repeat code here and there: it invokesrte_pktmbuf_alloc()three times at three different places!However,
rte_vhost_dequeue_burst()acutally does a simple job: copy the packet data from vring desc to mbuf. What's tricky here is:- desc buff could be chained (by
desc->nextfield), so that you need fetch next one if current is wholly drained.- One mbuf could not be big enough to hold all desc buff, hence you need to chain the mbuf as well, by the
mbuf->nextfield.Even though, the logic could be simple. Here is the pseudo code.
while (this_desc_is_not_drained_totally || has_next_desc) { if (this_desc_has_drained_totally) { this_desc = next_desc(); } if (mbuf_has_no_room) { mbuf = allocate_a_new_mbuf(); } COPY(mbuf, desc); }And this is how I refactored
rte_vhost_dequeue_burst.Note that the old patch does a special handling for skipping virtio header. However, that could be simply done by adjusting
desc_availanddesc_offsetvar:desc_avail = desc->len - vq->vhost_hlen; desc_offset = vq->vhost_hlen;This refactor makes the code much more readable (IMO), yet it reduces binary code size (nearly 2K).
Signed-off-by Yuanhan Liu <[email protected]>---
v2: - fix potential NULL dereference bug of var "prev" and "head"
--- lib/librte_vhost/vhost_rxtx.c | 297 +++++++++++++++++------------------------- 1 file changed, 116 insertions(+), 181 deletions(-)lib/librte_vhost/vhost_rxtx.c +14
-0@@ -702,21 +702,104 @@ vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) } } +static inline struct rte_mbuf * +copy_desc_to_mbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t desc_idx,struct rte_mempool *mbuf_pool) +{ +struct vring_desc *desc; +uint64_t desc_addr; +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; +struct rte_mbuf *head =NULL ; +struct rte_mbuf *cur =NULL , *prev =NULL ; +struct virtio_net_hdr *hdr; + + desc = &vq->desc[desc_idx];Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 724
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c +6
-0+ desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +/* Retrieve virtio net header */ + hdr = (struct virtio_net_hdr *)((uintptr_t )desc_addr); + desc_avail = desc->len - vq->vhost_hlen;There is a serious bug here,
desc->len-vq->vhost_lencould overflow. VM could easily create this case. Let us fix it here.Quoted 3 lines
Nope, this issue has been there since the beginning, and this patch is a refactor: we should not bring any functional changes. Therefore, we should not fix it here.
Quoted 7 lines
No, I don't mean exactly fixing in this patch but in this series.
Besides, from refactoring's perspective, actually we could make things further much simpler and more readable. Both the desc chains and mbuf could be converted into iovec, then both
dequeue(copy_desc_to_mbuf)andenqueue(copy_mbuf_to_desc)could use the commonly used iovec copying algorithms As data path are performance oriented, let us stop here.
Agreed, I had same performance concern on further simplication, therefore I didn't go further.
Quoted 4 lines
Quoted 2 lines
And actually, it's been fixed in the 6th patch in this series:
Will check that.
Do you have other comments for other patches? I'm considering to send a new version recently, say maybe tomorrow.
--yliu
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
In reply to Yuanhan LiuQuoted 19 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +19
-0+ mbuf_avail =0 ; + mbuf_offset =0 ; +while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) { +/* This desc reachs to its end, get the next one */ +if (desc_avail ==0 ) { + desc = &vq->desc[desc->next]; + + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + + desc_offset =0 ; + desc_avail = desc->len; + +PRINT_PACKET (dev, (uintptr_t )desc_addr, desc->len,0 ); + } + +/* +* This mbuf reachs to its end, get a new one +* to hold more data.
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 755
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+*/ +if (mbuf_avail ==0 ) { + cur =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (!cur)) { +RTE_LOG (ERR ,VHOST_DATA ,"Failed to " +"allocate memory for mbuf.\n" ); +if (head) +rte_pktmbuf_free (head); +return NULL ; + }
We could always allocate the head mbuf before the loop, then we save the following branch and make the code more streamlined.
It reminds me that this change prevents the possibility of mbuf bulk allocation, one solution is we pass the head mbuf from an additional parameter.
Btw, put unlikely before the check of mbuf_avail and checks elsewhere.
Quoted 25 lines
lib/librte_vhost/vhost_rxtx.c +25
-0+if (!head) { + head = cur; + }else { + prev->next = cur; + prev->data_len = mbuf_offset; + head->nb_segs +=1 ; + } + head->pkt_len += mbuf_offset; + prev = cur; + + mbuf_offset =0 ; + mbuf_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; + } + + cpy_len =RTE_MIN (desc_avail, mbuf_avail); + rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset), + (void *)((uintptr_t)(desc_addr + desc_offset)), + cpy_len); + + mbuf_avail -= cpy_len; + mbuf_offset += cpy_len; + desc_avail -= cpy_len; + desc_offset += cpy_len; + } +
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 28 lines · Huawei
Quoted 27 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +27
-0+ mbuf_avail =0 ; + mbuf_offset =0 ; +while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) { +/* This desc reachs to its end, get the next one */ +if (desc_avail ==0 ) { + desc = &vq->desc[desc->next]; + + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + + desc_offset =0 ; + desc_avail = desc->len; + +PRINT_PACKET (dev, (uintptr_t )desc_addr, desc->len,0 ); + } + +/* +* This mbuf reachs to its end, get a new one +* to hold more data. +*/ +if (mbuf_avail ==0 ) { + cur =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (!cur)) { +RTE_LOG (ERR ,VHOST_DATA ,"Failed to " +"allocate memory for mbuf.\n" ); +if (head) +rte_pktmbuf_free (head);
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return NULL; + }We could always allocate the head mbuf before the loop, then we save the following branch and make the code more streamlined.
It reminds me that this change prevents the possibility of mbuf bulk allocation, one solution is we pass the head mbuf from an additional parameter.
Yep, that's also something I have thought of.
Quoted 1 line
Btw, put unlikely before the check of
mbuf_availand checks elsewhere.
I don't think so. It would benifit for the small packets. What if, however, when TSO or jumbo frame is enabled that we have big packets?
--yliu
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 36 lines · Yuanhan Liu
Quoted 27 lines · Huawei
Quoted 26 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +26
-0+ mbuf_avail =0 ; + mbuf_offset =0 ; +while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) { +/* This desc reachs to its end, get the next one */ +if (desc_avail ==0 ) { + desc = &vq->desc[desc->next]; + + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + + desc_offset =0 ; + desc_avail = desc->len; + +PRINT_PACKET (dev, (uintptr_t )desc_addr, desc->len,0 ); + } + +/* +* This mbuf reachs to its end, get a new one +* to hold more data. +*/ +if (mbuf_avail ==0 ) { + cur =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (!cur)) { +RTE_LOG (ERR ,VHOST_DATA ,"Failed to " +"allocate memory for mbuf.\n" ); +if (head)Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 755
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +3
-0+rte_pktmbuf_free (head); +return NULL ; + }We could always allocate the head mbuf before the loop, then we save the following branch and make the code more streamlined.
It reminds me that this change prevents the possibility of mbuf bulk allocation, one solution is we pass the head mbuf from an additional parameter.
Quoted 5 lines
Yep, that's also something I have thought of.
Quoted 1 line
Btw, put unlikely before the check of
mbuf_availand checks elsewhere.I don't think so. It would benifit for the small packets. What if, however, when TSO or jumbo frame is enabled that we have big packets?
Prefer to favor the path that packet could fit in one mbuf.
Btw, not specially to this, return "m = copy_desc_to_mbuf(dev, vq, desc_indexes[i], mbuf_pool)", failure case is unlikely to happen, so add unlikely for the check if (m == NULL) there. Please check all branches elsewhere.
Quoted 2 lines
--yliu
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 37 lines · Huawei
Quoted 36 lines · Yuanhan Liu
Quoted 35 lines · Huawei
Quoted 19 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +19
-0+ mbuf_avail =0 ; + mbuf_offset =0 ; +while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) { +/* This desc reachs to its end, get the next one */ +if (desc_avail ==0 ) { + desc = &vq->desc[desc->next]; + + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + + desc_offset =0 ; + desc_avail = desc->len; + +PRINT_PACKET (dev, (uintptr_t )desc_addr, desc->len,0 ); + } + +/* +* This mbuf reachs to its end, get a new one +* to hold more data. lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 755
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+*/ +if (mbuf_avail ==0 ) { + cur =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (!cur)) { +RTE_LOG (ERR ,VHOST_DATA ,"Failed to " +"allocate memory for mbuf.\n" ); +if (head) +rte_pktmbuf_free (head); +return NULL ; + }We could always allocate the head mbuf before the loop, then we save the following branch and make the code more streamlined.
It reminds me that this change prevents the possibility of mbuf bulk allocation, one solution is we pass the head mbuf from an additional parameter.
Quoted 7 lines
Quoted 5 lines
Yep, that's also something I have thought of.
Quoted 1 line
Btw, put unlikely before the check of
mbuf_availand checks elsewhere.I don't think so. It would benifit for the small packets. What if, however, when TSO or jumbo frame is enabled that we have big packets?
Prefer to favor the path that packet could fit in one mbuf.
Hmmm, why? While I know that TSO and mergeable buf is disable by default in our vhost example vhost-switch, they are enabled by default in real life.
Quoted 4 lines
Btw, not specially to this, return "m =
copy_desc_to_mbuf(dev, vq, desc_indexes[i], mbuf_pool)", failure case is unlikely to happen, so add unlikely for the check if (m == NULL) there. Please check all branches elsewhere.
Thanks for the remind, will have a detail check.
--yliu
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 40 lines · Yuanhan Liu
Quoted 39 lines · Huawei
Quoted 38 lines · Yuanhan Liu
Quoted 27 lines · Huawei
Quoted 26 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +26
-0+ mbuf_avail =0 ; + mbuf_offset =0 ; +while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) { +/* This desc reachs to its end, get the next one */ +if (desc_avail ==0 ) { + desc = &vq->desc[desc->next]; + + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + + desc_offset =0 ; + desc_avail = desc->len; + +PRINT_PACKET (dev, (uintptr_t )desc_addr, desc->len,0 ); + } + +/* +* This mbuf reachs to its end, get a new one +* to hold more data. +*/ +if (mbuf_avail ==0 ) { + cur =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (!cur)) { +RTE_LOG (ERR ,VHOST_DATA ,"Failed to " +"allocate memory for mbuf.\n" ); +if (head)Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 755
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +3
-0+rte_pktmbuf_free (head); +return NULL ; + }We could always allocate the head mbuf before the loop, then we save the following branch and make the code more streamlined.
It reminds me that this change prevents the possibility of mbuf bulk allocation, one solution is we pass the head mbuf from an additional parameter.Yep, that's also something I have thought of.
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
Btw, put unlikely before the check of
mbuf_availand checks elsewhere.I don't think so. It would benifit for the small packets. What if, however, when TSO or jumbo frame is enabled that we have big packets?
Prefer to favor the path that packet could fit in one mbuf.
Hmmm, why? While I know that TSO and mergeable buf is disable by default in our vhost example vhost-switch, they are enabled by default in real life.
mergable is only meaningful in RX path.
If you mean big packets in TX path, i personally favor the path that packet fits in one mbuf.
Quoted 8 lines
Quoted 4 lines
Btw, not specially to this, return "m =
copy_desc_to_mbuf(dev, vq, desc_indexes[i], mbuf_pool)", failure case is unlikely to happen, so add unlikely for the check if (m == NULL) there. Please check all branches elsewhere.Thanks for the remind, will have a detail check.
--yliu
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 15 lines · Huawei
Quoted 14 lines · Yuanhan Liu
Quoted 13 lines · Huawei
Quoted 12 lines · Yuanhan Liu
Quoted 6 lines · Huawei
On 2/18/2016 9:48 PM, Yuanhan Liu wrote:
We could always allocate the head mbuf before the loop, then we save the following branch and make the code more streamlined.
It reminds me that this change prevents the possibility of mbuf bulk allocation, one solution is we pass the head mbuf from an additional parameter.Yep, that's also something I have thought of.
Quoted 1 line
Btw, put unlikely before the check of
mbuf_availand checks elsewhere.I don't think so. It would benifit for the small packets. What if, however, when TSO or jumbo frame is enabled that we have big packets?
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Prefer to favor the path that packet could fit in one mbuf.
Hmmm, why? While I know that TSO and mergeable buf is disable by default in our vhost example vhost-switch, they are enabled by default in real life.
mergable is only meaningful in RX path.
If you mean big packets in TX path,
Sorry, and yes, I meant that.
Quoted 2 lines
i personally favor the path that
packet fits in one mbuf.
Sorry, that will not convince me.
--yliu
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
In reply to Yuanhan LiuQuoted 1 line · Yuanhan Liu
[...]
CCed changchun, the author for the chained handling of desc and mbuf. The change makes the code more readable, but i think the following commit message is simple and enough.
Quoted 13 lines
while (this_desc_is_not_drained_totally || has_next_desc) {
if (this_desc_has_drained_totally) {
this_desc = next_desc();
}
if (mbuf_has_no_room) {
mbuf = allocate_a_new_mbuf();
}
COPY(mbuf, desc);
}Quoted 4 lines
[...]
This refactor makes the code much more readable (IMO), yet it reduces binary code size (nearly 2K).
I guess the reduced binary code size comes from reduced inline calls to mbuf allocation.
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 5 lines · Huawei
Quoted 1 line · Yuanhan Liu
[...]
CCed changchun, the author for the chained handling of desc and mbuf. The change makes the code more readable, but i think the following commit message is simple and enough.
Hmm.., my commit log tells a full story:
- What is the issue? (messy/logic twisted code)
- What the code does? (And what are the challenges: few tricky places)
- What's the proposed solution to fix it. (the below pseudo code)
And you suggest me to get rid of the first 2 items and leave 3rd item (a solution) only?
--yliu
Quoted 20 lines
Quoted 13 lines
while (this_desc_is_not_drained_totally || has_next_desc) { if (this_desc_has_drained_totally) { this_desc = next_desc(); } if (mbuf_has_no_room) { mbuf = allocate_a_new_mbuf(); } COPY(mbuf, desc); }Quoted 4 lines
[...]
This refactor makes the code much more readable (IMO), yet it reduces binary code size (nearly 2K).
I guess the reduced binary code size comes from reduced inline calls to mbuf allocation.
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 8 lines · Yuanhan Liu
Quoted 5 lines · Huawei
Quoted 1 line · Yuanhan Liu
[...]
CCed changchun, the author for the chained handling of desc and mbuf. The change makes the code more readable, but i think the following commit message is simple and enough.
Hmm.., my commit log tells a full story:
Quoted 8 lines
- What is the issue? (messy/logic twisted code)
- What the code does? (And what are the challenges: few tricky places)
- What's the proposed solution to fix it. (the below pseudo code)
And you suggest me to get rid of the first 2 items and leave 3rd item (a solution) only?
The following are simple and enough with just one additional statement for the repeated mbuf allocation or your twisted. Other commit messages are overly duplicated. Just my personal opinion. Up to you.
To this special case, for example, we could make both mbuf and vring_desc chains into iovec, then use commonly used iovec copy algorithms for both dequeue and enqueue, which further makes the code much simpler and more readable. For this change, one or two sentences are clear to me.
Quoted 21 lines
--yliuQuoted 19 lines
Quoted 12 lines
while (this_desc_is_not_drained_totally || has_next_desc) { if (this_desc_has_drained_totally) { this_desc = next_desc(); } if (mbuf_has_no_room) { mbuf = allocate_a_new_mbuf(); } COPY(mbuf, desc); }Quoted 4 lines
[...]
This refactor makes the code much more readable (IMO), yet it reduces binary code size (nearly 2K).
I guess the reduced binary code size comes from reduced inline calls to mbuf allocation.
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
In reply to Yuanhan LiuQuoted 1 line · Yuanhan Liu
The current
rte_vhost_dequeue_burst()implementation is a bit messy
[...]
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 797
Quoted 12 lines
lib/librte_vhost/vhost_rxtx.c +2
-5+uint16_t rte_vhost_dequeue_burst (struct virtio_net *dev,uint16_t queue_id,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) { -struct rte_mbuf *m, *prev;struct vhost_virtqueue *vq; -struct vring_desc *desc; -uint64_t vb_addr =0 ; -uint64_t vb_net_hdr_addr =0 ; -uint32_t head[MAX_PKT_BURST ]; +uint32_t desc_indexes[MAX_PKT_BURST ];
indices
Quoted 82 lines
lib/librte_vhost/vhost_rxtx.c +17
-44uint32_t used_idx;uint32_t i; -uint16_t free_entries, entry_success =0 ; +uint16_t free_entries;uint16_t avail_idx; -struct virtio_net_hdr *hdr =NULL ; +struct rte_mbuf *m;if (unlikely (!is_valid_virt_queue_idx (queue_id,1 , dev->virt_qp_nb))) { RTE_LOG(ERR, VHOST_DATA,@@ -730,197 +813,49 @@ rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,return 0 ; avail_idx = *((volatile uint16_t *)&vq->avail->idx); - -/* If there are no available buffers then return. */ -if (vq->last_used_idx == avail_idx) + free_entries = avail_idx - vq->last_used_idx; +if (free_entries ==0 )return 0 ; -LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, - dev->device_fh); +LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, dev->device_fh); -/* Prefetch available ring to retrieve head indexes. */ -rte_prefetch0 (&vq->avail->ring[vq->last_used_idx & (vq->size -1 )]); + used_idx = vq->last_used_idx & (vq->size -1 ); -/*get the number of free entries in the ring*/ - free_entries = (avail_idx - vq->last_used_idx); +/* Prefetch available ring to retrieve head indexes. */ +rte_prefetch0 (&vq->avail->ring[used_idx]); - free_entries =RTE_MIN (free_entries, count); -/* Limit to MAX_PKT_BURST. */ - free_entries =RTE_MIN (free_entries,MAX_PKT_BURST ); + count =RTE_MIN (count,MAX_PKT_BURST ); + count =RTE_MIN (count, free_entries); +LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") about to dequeue %u buffers\n" , + dev->device_fh, count); -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Buffers available %d\n" , - dev->device_fh, free_entries);/* Retrieve all of the head indexes first to avoid caching issues. */ -for (i =0 ; i < free_entries; i++) - head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size -1 )]; +for (i =0 ; i < count; i++) { + desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) & + (vq->size -1 )]; + }/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success]]); +rte_prefetch0 (&vq->desc[desc_indexes[0 ]]);rte_prefetch0 (&vq->used->ring[vq->last_used_idx & (vq->size -1 )]); -while (entry_success < free_entries) { -uint32_t vb_avail, vb_offset; -uint32_t seg_avail, seg_offset; -uint32_t cpy_len; -uint32_t seg_num =0 ; -struct rte_mbuf *cur; -uint8_t alloc_err =0 ; - - desc = &vq->desc[head[entry_success]]; - - vb_net_hdr_addr =gpa_to_vva (dev, desc->addr); - hdr = (struct virtio_net_hdr *)((uintptr_t )vb_net_hdr_addr); - -/* Discard first buffer as it is the virtio header */ -if (desc->flags &VRING_DESC_F_NEXT ) { - desc = &vq->desc[desc->next]; - vb_offset =0 ; - vb_avail = desc->len; - }else { - vb_offset = vq->vhost_hlen; - vb_avail = desc->len - vb_offset; - } - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr);
lib/librte_vhost/vhost_rxtx.c rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, · line 795
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +0
-10-/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - - used_idx = vq->last_used_idx & (vq->size -1 ); - -if (entry_success < (free_entries -1 )) { -/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success+1 ]]); -rte_prefetch0 (&vq->used->ring[(used_idx +1 ) & (vq->size -1 )]); - }
Why is this prefetch silently dropped in the patch?
Quoted 106 lines
lib/librte_vhost/vhost_rxtx.c +0
-106- -/* Update used index buffer information. */ - vq->used->ring[used_idx].id = head[entry_success]; - vq->used->ring[used_idx].len =0 ; - -/* Allocate an mbuf and populate the structure. */ - m =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (m ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA , -"Failed to allocate memory for mbuf.\n" ); -break ; - } - seg_offset =0 ; - seg_avail = m->buf_len -RTE_PKTMBUF_HEADROOM ; - cpy_len =RTE_MIN (vb_avail, seg_avail); - -PRINT_PACKET (dev, (uintptr_t )vb_addr, desc->len,0 ); - - seg_num++; - cur = m; - prev = m; -while (cpy_len !=0 ) { - rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset), - (void *)((uintptr_t)(vb_addr + vb_offset)), - cpy_len); - - seg_offset += cpy_len; - vb_offset += cpy_len; - vb_avail -= cpy_len; - seg_avail -= cpy_len; - -if (vb_avail !=0 ) { -/* -* The segment reachs to its end, -* while the virtio buffer in TX vring has -* more data to be copied. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* Allocate mbuf and populate the structure. */ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA ,"Failed to " -"allocate memory for mbuf.\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - }else { -if (desc->flags &VRING_DESC_F_NEXT ) { -/* -* There are more virtio buffers in -* same vring entry need to be copied. -*/ -if (seg_avail ==0 ) { -/* -* The current segment hasn't -* room to accomodate more -* data. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* -* Allocate an mbuf and -* populate the structure. -*/ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR , -VHOST_DATA , -"Failed to " -"allocate memory " -"for mbuf\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - } - - desc = &vq->desc[desc->next]; - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - vb_offset =0 ; - vb_avail = desc->len; - -PRINT_PACKET (dev, (uintptr_t )vb_addr, - desc->len,0 ); - }else { -/* The whole packet completes. */ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; - vb_avail =0 ;
lib/librte_vhost/vhost_rxtx.c rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, · line 844
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +3
-7- } - } - - cpy_len =RTE_MIN (vb_avail, seg_avail); - } - -if (unlikely (alloc_err ==1 )) +for (i =0 ; i < count; i++) { + m =copy_desc_to_mbuf (dev, vq, desc_indexes[i], mbuf_pool); +if (m ==NULL )
add unlikely for every case not possible to happen
lib/librte_vhost/vhost_rxtx.c rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, · line 850
Quoted 13 lines
lib/librte_vhost/vhost_rxtx.c +4
-7break ; + pkts[i] = m; - m->nb_segs = seg_num; -if ((hdr->flags !=0 ) || (hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE )) -vhost_dequeue_offload (hdr, m); - - pkts[entry_success] = m; - vq->last_used_idx++; - entry_success++; + used_idx = vq->last_used_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_indexes[i]; + vq->used->ring[used_idx].len =0 ;
What is the correct value for ring[used_idx].len, the packet length or 0?
Quoted 13 lines
lib/librte_vhost/vhost_rxtx.c +4
-2}rte_compiler_barrier (); - vq->used->idx += entry_success; + vq->used->idx += i; +/* Kick guest if required. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return entry_success; + +return i; }
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 17 lines · Huawei
Quoted 1 line · Yuanhan Liu
The current
rte_vhost_dequeue_burst()implementation is a bit messy[...]
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 797
Quoted 12 lines
lib/librte_vhost/vhost_rxtx.c +2
-5+uint16_t rte_vhost_dequeue_burst (struct virtio_net *dev,uint16_t queue_id,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) { -struct rte_mbuf *m, *prev;struct vhost_virtqueue *vq; -struct vring_desc *desc; -uint64_t vb_addr =0 ; -uint64_t vb_net_hdr_addr =0 ; -uint32_t head[MAX_PKT_BURST ]; +uint32_t desc_indexes[MAX_PKT_BURST ];indices
http://dictionary.reference.com/browse/index
index
noun, plural indexes, indices
Quoted 15 lines
Quoted 15 lines
lib/librte_vhost/vhost_rxtx.c +2
-2uint32_t used_idx;uint32_t i; -uint16_t free_entries, entry_success =0 ; +uint16_t free_entries;uint16_t avail_idx; -struct virtio_net_hdr *hdr =NULL ; +struct rte_mbuf *m;if (unlikely (!is_valid_virt_queue_idx (queue_id,1 , dev->virt_qp_nb))) { RTE_LOG(ERR, VHOST_DATA,@@ -730,197 +813,49 @@ rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,return 0 ;
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, · line 795
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +0
-5-if (entry_success < (free_entries -1 )) { -/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success+1 ]]); -rte_prefetch0 (&vq->used->ring[(used_idx +1 ) & (vq->size -1 )]); - }Why is this prefetch silently dropped in the patch?
Oops, good catching. Will fix it. Thanks.
Quoted 15 lines
lib/librte_vhost/vhost_rxtx.c rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, · line 850
Quoted 13 lines
lib/librte_vhost/vhost_rxtx.c +4
-7break ; + pkts[i] = m; - m->nb_segs = seg_num; -if ((hdr->flags !=0 ) || (hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE )) -vhost_dequeue_offload (hdr, m); - - pkts[entry_success] = m; - vq->last_used_idx++; - entry_success++; + used_idx = vq->last_used_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_indexes[i]; + vq->used->ring[used_idx].len =0 ;What is the correct value for ring[used_idx].len, the packet length or 0?
Good question. I didn't notice that before. Sounds buggy to me. However, that's from the old code. Will check it.
--yliu
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
Quoted 17 lines · Yuanhan Liu
Quoted 16 lines · Huawei
Quoted 1 line · Yuanhan Liu
The current
rte_vhost_dequeue_burst()implementation is a bit messy[...]
lib/librte_vhost/vhost_rxtx.c vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m) · line 797
Quoted 12 lines
lib/librte_vhost/vhost_rxtx.c +2
-5+uint16_t rte_vhost_dequeue_burst (struct virtio_net *dev,uint16_t queue_id,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) { -struct rte_mbuf *m, *prev;struct vhost_virtqueue *vq; -struct vring_desc *desc; -uint64_t vb_addr =0 ; -uint64_t vb_net_hdr_addr =0 ; -uint32_t head[MAX_PKT_BURST ]; +uint32_t desc_indexes[MAX_PKT_BURST ];indices
Quoted 4 lines
http://dictionary.reference.com/browse/index
index
noun, plural indexes, indices
ok, i see both two are used.
Quoted 37 lines
Quoted 14 lines
Quoted 14 lines
lib/librte_vhost/vhost_rxtx.c +2
-2uint32_t used_idx;uint32_t i; -uint16_t free_entries, entry_success =0 ; +uint16_t free_entries;uint16_t avail_idx; -struct virtio_net_hdr *hdr =NULL ; +struct rte_mbuf *m;if (unlikely (!is_valid_virt_queue_idx (queue_id,1 , dev->virt_qp_nb))) { RTE_LOG(ERR, VHOST_DATA,@@ -730,197 +813,49 @@ rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,return 0 ;Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, · line 795
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +0
-5-if (entry_success < (free_entries -1 )) { -/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success+1 ]]); -rte_prefetch0 (&vq->used->ring[(used_idx +1 ) & (vq->size -1 )]); - }Why is this prefetch silently dropped in the patch?
Oops, good catching. Will fix it. Thanks.
Quoted 14 lines
lib/librte_vhost/vhost_rxtx.c rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, · line 850
Quoted 13 lines
lib/librte_vhost/vhost_rxtx.c +4
-7break ; + pkts[i] = m; - m->nb_segs = seg_num; -if ((hdr->flags !=0 ) || (hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE )) -vhost_dequeue_offload (hdr, m); - - pkts[entry_success] = m; - vq->last_used_idx++; - entry_success++; + used_idx = vq->last_used_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_indexes[i]; + vq->used->ring[used_idx].len =0 ;What is the correct value for ring[used_idx].len, the packet length or 0?
Quoted 2 lines
Good question. I didn't notice that before. Sounds buggy to me. However, that's from the old code. Will check it.
Yes, i knew it is in old code also. Thanks.
Quoted 2 lines
--yliu
Re: [PATCH v2 1/7] vhost: refactor rte_vhost_dequeue_burst
In reply to Yuanhan LiuReview
Quoted 2 lines · Yuanhan Liu
(quoted diff) +2
-0+ mbuf_avail = 0; + mbuf_offset = 0;
one cs nit, put it at the definition.
PATCH v2 2/7 vhost: refactor virtio_dev_rx
In reply to Yuanhan LiuThis is a simple refactor, as there isn't any twisted logic in old code. Here I just broke the code and introduced two helper functions, reserve_avail_buf() and copy_mbuf_to_desc() to make the code more readable.
Also, it saves nearly 1K bytes of binary code size.
---
v2: - fix NULL dereference bug found by Rich.
--- lib/librte_vhost/vhost_rxtx.c | 286 ++++++++++++++++++++---------------------- 1 file changed, 137 insertions(+), 149 deletions(-)
lib/librte_vhost/vhost_rxtx.c +137 -149
@@ -92,6 +92,115 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)return ; } +static inline int __attribute__((always_inline)) +copy_mbuf_to_desc (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *m,uint16_t desc_idx,uint32_t *copied) +{ +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; +struct vring_desc *desc; +uint64_t desc_addr; +struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; + + desc = &vq->desc[desc_idx]; + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +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_offset = vq->vhost_hlen; + desc_avail = desc->len - vq->vhost_hlen; + + *copied =rte_pktmbuf_pkt_len (m); + mbuf_avail =rte_pktmbuf_data_len (m); + mbuf_offset =0 ; +while (1 ) { +/* done with current mbuf, fetch next */ +if (mbuf_avail ==0 ) { + m = m->next; +if (m ==NULL ) +break ; + + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (m); + } + +/* done with current desc buf, fetch next */ +if (desc_avail ==0 ) { +if ((desc->flags &VRING_DESC_F_NEXT ) ==0 ) { +/* Room in vring buffer is not enough */ +return -1 ; + } + + desc = &vq->desc[desc->next]; + desc_addr =gpa_to_vva (dev, desc->addr); + desc_offset =0 ; + desc_avail = desc->len; + } + + cpy_len =RTE_MIN (desc_avail, mbuf_avail); +rte_memcpy ((void *)((uintptr_t )(desc_addr + desc_offset)), + rte_pktmbuf_mtod_offset(m, void *, mbuf_offset), + cpy_len); +PRINT_PACKET (dev, (uintptr_t )(desc_addr + desc_offset), + cpy_len,0 ); + + mbuf_avail -= cpy_len; + mbuf_offset += cpy_len; + desc_avail -= cpy_len; + desc_offset += cpy_len; + } + +return 0 ; +} + +/* +* As many data cores may want to access available buffers +* they need to be reserved. +*/ +static inline uint32_t +reserve_avail_buf (struct vhost_virtqueue *vq,uint32_t count, +uint16_t *start,uint16_t *end) +{ +uint16_t res_start_idx; +uint16_t res_end_idx; +uint16_t avail_idx; +uint16_t free_entries; +int success; + + count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); + +again: + res_start_idx = vq->last_used_idx_res; + avail_idx = *((volatile uint16_t *)&vq->avail->idx); + + free_entries = (avail_idx - res_start_idx); + count =RTE_MIN (count, free_entries); +if (count ==0 ) +return 0 ; + + res_end_idx = res_start_idx + count; + +/* +* update vq->last_used_idx_res atomically; try again if failed. +* +* TODO: Allow to disable cmpset if no concurrency in application. +*/ + success =rte_atomic16_cmpset (&vq->last_used_idx_res, + res_start_idx, res_end_idx); +if (!success) + goto again; + + *start = res_start_idx; + *end = res_end_idx; + +return count; +} +/** * This function adds buffers to the virtio devices RX virtqueue. Buffers can * be received from the physical port or from another virtio device. A packet
@@ -101,21 +210,12 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)*/ 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 rte_mbuf **pkts,uint32_t count) {struct vhost_virtqueue *vq; -struct vring_desc *desc; -struct rte_mbuf *buff, *first_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 ; +uint16_t res_start_idx, res_end_idx; +uint16_t desc_indexes[MAX_PKT_BURST ]; +uint32_t i;LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") virtio_dev_rx()\n" , dev->device_fh);if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) {
@@ -129,155 +229,43 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,if (unlikely (vq->enabled ==0 ))return 0 ; - 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 ; + count =reserve_avail_buf (vq, count, &res_start_idx, &res_end_idx); +if (count ==0 ) +return 0 ; -/* Get descriptor from available ring */ - desc = &vq->desc[head[packet_success]]; +LOG_DEBUG (VHOST_DATA , +"(%" PRIu64") res_start_idx %d| res_end_idx Index %d\n" , + dev->device_fh, res_start_idx, res_end_idx); - buff = pkts[packet_success]; - first_buff = buff; +/* Retrieve all of the desc indexes first to avoid caching issues. */ +rte_prefetch0 (&vq->avail->ring[res_start_idx & (vq->size -1 )]); +for (i =0 ; i < count; i++) + desc_indexes[i] = vq->avail->ring[(res_start_idx + i) & (vq->size -1 )]; -/* 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); +rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); +for (i =0 ; i < count; i++) { +uint16_t desc_idx = desc_indexes[i]; +uint16_t used_idx = (res_start_idx + i) & (vq->size -1 ); +uint32_t copied; +int err; -/* Copy virtio_hdr to packet and increment buffer address */ - buff_hdr_addr = buff_addr; + err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx, &copied); -/* -* 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); + vq->used->ring[used_idx].id = desc_idx; +if (unlikely (err)) { + vq->used->ring[used_idx].len = vq->vhost_hlen; }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); + vq->used->ring[used_idx].len = copied + vq->vhost_hlen; } -/* 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 ; - -virtio_enqueue_offload (first_buff, &virtio_hdr.hdr); - -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]]); - } +if (i +1 < count) +rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); }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 != res_start_idx))rte_pause (); *(volatile uint16_t *)&vq->used->idx += count;
Signature · 1 line
1.9.0
Re: [PATCH v2 2/7] vhost: refactor virtio_dev_rx
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 131
Quoted 11 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +11
-0+while (1 ) { +/* done with current mbuf, fetch next */ +if (mbuf_avail ==0 ) { + m = m->next; +if (m ==NULL ) +break ; + + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (m); + } +
You could use while (mbuf_avail || m->next) to align with the style of coyp_desc_to_mbuf?
Re: [PATCH v2 2/7] vhost: refactor virtio_dev_rx
Quoted 15 lines · Huawei
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 131
Quoted 11 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +11
-0+while (1 ) { +/* done with current mbuf, fetch next */ +if (mbuf_avail ==0 ) { + m = m->next; +if (m ==NULL ) +break ; + + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (m); + } +You could use while (
mbuf_avail||m->next) to align with the style ofcoyp_desc_to_mbuf?
Good suggestion, will do that.
Thanks.
--yliu
PATCH v2 3/7 vhost: refactor virtio_dev_merge_rx
In reply to Yuanhan LiuCurrent 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 difference a bit bigger then.
Anyway, it could be simpler, just like what we did for virtio_dev_rx(). One difference is that we need to update used ring properly.
The pseudo code looks like below:
while (1) {
if (this_desc_has_no_room) {
this_desc = fetch_next_from_vec_buf();
if (it is the last of a desc chain) {
update_used_ring();
}
}
if (this_mbuf_has_drained_totally) {
this_mbuf = fetch_next_mbuf();
if (this_mbuf == NULL)
break;
}
COPY(this_desc, this_mbuf);
}This patch reduces quite many lines of code, therefore, make it much more readable.
--- lib/librte_vhost/vhost_rxtx.c | 390 ++++++++++++++++++------------------------ 1 file changed, 163 insertions(+), 227 deletions(-)
lib/librte_vhost/vhost_rxtx.c +163 -227
@@ -280,237 +280,200 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,return count; } -static inline uint32_t __attribute__((always_inline)) -copy_from_mbuf_to_vring (struct virtio_net *dev,uint32_t queue_id, -uint16_t res_base_idx,uint16_t res_end_idx, -struct rte_mbuf *pkt) +static inline int +fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, +uint32_t *allocated,uint32_t *vec_idx) { -uint32_t vec_idx =0 ; -uint32_t entry_success =0 ; -struct vhost_virtqueue *vq; -/* The virtio_hdr is initialised to 0. */ -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = { - {0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint16_t cur_idx = res_base_idx; -uint64_t vb_addr =0 ; -uint64_t vb_hdr_addr =0 ; -uint32_t seg_offset =0 ; -uint32_t vb_offset =0 ; -uint32_t seg_avail; -uint32_t vb_avail; -uint32_t cpy_len, entry_len; - -if (pkt ==NULL ) -return 0 ; +uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated; -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Current Index %d| " -"End Index %d\n" , - dev->device_fh, cur_idx, res_end_idx); +while (1 ) { +if (vec_id >=BUF_VECTOR_MAX ) +return -1 ; -/* -* Convert from gpa to vva -* (guest physical addr -> vhost virtual addr) -*/ - vq = dev->virtqueue[queue_id]; + len += vq->desc[idx].len; + vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr; + vq->buf_vec[vec_id].buf_len = vq->desc[idx].len; + vq->buf_vec[vec_id].desc_idx = idx; + vec_id++; - vb_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); - vb_hdr_addr = vb_addr; +if ((vq->desc[idx].flags &VRING_DESC_F_NEXT ) ==0 ) +break ; -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); + idx = vq->desc[idx].next; + } - virtio_hdr.num_buffers = res_end_idx - res_base_idx; + *allocated = len; + *vec_idx = vec_id; -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") RX: Num merge buffers %d\n" , - dev->device_fh, virtio_hdr.num_buffers); +return 0 ; +} -virtio_enqueue_offload (pkt, &virtio_hdr.hdr); +/* +* As many data cores may want to access available buffers concurrently, +* they need to be reserved. +* +* Returns -1 on fail, 0 on success +*/ +static inline int +reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, +uint16_t *start,uint16_t *end) +{ +uint16_t res_start_idx; +uint16_t res_cur_idx; +uint16_t avail_idx; +uint32_t allocated; +uint32_t vec_idx; +uint16_t tries; -rte_memcpy ((void *)(uintptr_t )vb_hdr_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); +again: + res_start_idx = vq->last_used_idx_res; + res_cur_idx = res_start_idx; -PRINT_PACKET (dev, (uintptr_t )vb_hdr_addr, vq->vhost_hlen,1 ); + allocated =0 ; + vec_idx =0 ; + tries =0 ; +while (1 ) { + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +if (unlikely (res_cur_idx == avail_idx)) { +LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Failed " +"to get enough desc from vring\n" , + dev->device_fh); +return -1 ; + } - seg_avail =rte_pktmbuf_data_len (pkt); - vb_offset = vq->vhost_hlen; - vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen; +if (fill_vec_buf (vq, res_cur_idx, &allocated, &vec_idx) <0 ) +return -1 ; - entry_len = vq->vhost_hlen; + res_cur_idx++; + tries++; -if (vb_avail ==0 ) { -uint32_t desc_idx = - vq->buf_vec[vec_idx].desc_idx; +if (allocated >= size) +break ; -if ((vq->desc[desc_idx].flags - &VRING_DESC_F_NEXT ) ==0 ) { -/* Update used ring with desc information */ - vq->used->ring[cur_idx & (vq->size -1 )].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[cur_idx & (vq->size -1 )].len - = entry_len; +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (tries >= vq->size) +return -1 ; + } - entry_len =0 ; - cur_idx++; - entry_success++; - } +/* +* update vq->last_used_idx_res atomically. +* retry again if failed. +*/ +if (rte_atomic16_cmpset (&vq->last_used_idx_res, + res_start_idx, res_cur_idx) ==0 ) + goto again; - vec_idx++; - vb_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); + *start = res_start_idx; + *end = res_cur_idx; +return 0 ; +} -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - vb_offset =0 ; - vb_avail = vq->buf_vec[vec_idx].buf_len; - } +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 ; -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; + + mbuf_avail =rte_pktmbuf_data_len (m); + mbuf_offset =0 ; +while (1 ) { +/* done with current desc buf, get the next one */ +if (desc_avail ==0 ) { + desc_idx = vq->buf_vec[vec_idx].desc_idx; + +if ((vq->desc[desc_idx].flags &VRING_DESC_F_NEXT ) ==0 ) {/* Update used ring with desc information */ - vq->used->ring[cur_idx & (vq->size -1 )].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[cur_idx & (vq->size -1 )].len - = entry_len; - entry_len =0 ; - cur_idx++; - entry_success++; + used_idx = cur_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_idx; + vq->used->ring[used_idx].len = desc_offset; + + nr_used++; } vec_idx++; - vb_addr =gpa_to_vva (dev, - vq->buf_vec[vec_idx].buf_addr); - vb_offset =0 ; - vb_avail = vq->buf_vec[vec_idx].buf_len; - cpy_len =RTE_MIN (vb_avail, seg_avail); - }else { -/* -* This current segment complete, need continue to -* check if the whole packet complete or not. -*/ - pkt = pkt->next; -if (pkt !=NULL ) { -/* -* There are more segments. -*/ -if (vb_avail ==0 ) { -/* -* This current buffer from vring is -* used up, need fetch next buffer -* from buf_vec. -*/ -uint32_t desc_idx = - vq->buf_vec[vec_idx].desc_idx; - -if ((vq->desc[desc_idx].flags & -VRING_DESC_F_NEXT ) ==0 ) { -uint16_t wrapped_idx = - cur_idx & (vq->size -1 ); -/* -* Update used ring with the -* descriptor information -*/ - vq->used->ring[wrapped_idx].id - = desc_idx; - vq->used->ring[wrapped_idx].len - = entry_len; - entry_success++; - entry_len =0 ; - cur_idx++; - } - -/* Get next buffer from buf_vec. */ - vec_idx++; - vb_addr =gpa_to_vva (dev, - vq->buf_vec[vec_idx].buf_addr); - vb_avail = - vq->buf_vec[vec_idx].buf_len; - vb_offset =0 ; - } - - seg_offset =0 ; - seg_avail =rte_pktmbuf_data_len (pkt); - cpy_len =RTE_MIN (vb_avail, seg_avail); - }else { -/* -* This whole packet completes. -*/ -/* Update used ring with desc information */ - vq->used->ring[cur_idx & (vq->size -1 )].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[cur_idx & (vq->size -1 )].len - = entry_len; - entry_success++; -break ; - } + desc_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); + +/* Prefetch buffer address. */ +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + desc_offset =0 ; + desc_avail = vq->buf_vec[vec_idx].buf_len; } - } -return entry_success; -} +/* done with current mbuf, get the next one */ +if (mbuf_avail ==0 ) { + m = m->next; +if (!m) +break ; -static inline void __attribute__((always_inline)) -update_secure_len (struct vhost_virtqueue *vq,uint32_t id, -uint32_t *secure_len,uint32_t *vec_idx) -{ -uint16_t wrapped_idx = id & (vq->size -1 ); -uint32_t idx = vq->avail->ring[wrapped_idx]; -uint8_t next_desc; -uint32_t len = *secure_len; -uint32_t vec_id = *vec_idx; + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (m); + } -do { - next_desc =0 ; - len += vq->desc[idx].len; - vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - vq->buf_vec[vec_id].buf_len = vq->desc[idx].len; - vq->buf_vec[vec_id].desc_idx = idx; - vec_id++; + cpy_len =RTE_MIN (desc_avail, mbuf_avail); +rte_memcpy ((void *)((uintptr_t )(desc_addr + desc_offset)), + rte_pktmbuf_mtod_offset(m, void *, mbuf_offset), + cpy_len); +PRINT_PACKET (dev, (uintptr_t )(desc_addr + desc_offset), + cpy_len,0 ); -if (vq->desc[idx].flags &VRING_DESC_F_NEXT ) { - idx = vq->desc[idx].next; - next_desc =1 ; - } - }while (next_desc); + mbuf_avail -= cpy_len; + mbuf_offset += cpy_len; + desc_avail -= cpy_len; + desc_offset += cpy_len; + } + + used_idx = cur_idx & (vq->size -1 ); + vq->used->ring[used_idx].id = vq->buf_vec[vec_idx].desc_idx; + vq->used->ring[used_idx].len = desc_offset; + nr_used++; - *secure_len = len; - *vec_idx = vec_id; +return nr_used; } -/* -* This function works for mergeable RX. -*/ static inline uint32_t __attribute__((always_inline))virtio_dev_merge_rx (struct virtio_net *dev,uint16_t queue_id,struct rte_mbuf **pkts,uint32_t count) {struct vhost_virtqueue *vq; -uint32_t pkt_idx =0 , entry_success =0 ; -uint16_t avail_idx; -uint16_t res_base_idx, res_cur_idx; -uint8_t success =0 ; +uint32_t pkt_idx =0 , nr_used =0 ; +uint16_t start, end;LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") virtio_dev_merge_rx()\n" , dev->device_fh);
@@ -526,57 +489,30 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,return 0 ; count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); -if (count ==0 )return 0 ;for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) {uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen; -do { -/* -* As many data cores may want access to available -* buffers, they need to be reserved. -*/ -uint32_t secure_len =0 ; -uint32_t vec_idx =0 ; - - res_base_idx = vq->last_used_idx_res; - res_cur_idx = res_base_idx; - -do { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (res_cur_idx == avail_idx)) - goto merge_rx_exit; - -update_secure_len (vq, res_cur_idx, - &secure_len, &vec_idx); - res_cur_idx++; - }while (pkt_len > secure_len); - -/* vq->last_used_idx_res is atomically updated. */ - success =rte_atomic16_cmpset (&vq->last_used_idx_res, - res_base_idx, - res_cur_idx); - }while (success ==0 ); - - entry_success =copy_from_mbuf_to_vring (dev, queue_id, - res_base_idx, res_cur_idx, pkts[pkt_idx]); +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]);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 ();
Signature · 1 line
1.9.0
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 289
Quoted 4 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +3
-0+uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated;
There is bug not using volatile to retrieve the avail idx.
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
Quoted 6 lines · Huawei
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 289
Quoted 4 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +3
-0+uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated;There is bug not using volatile to retrieve the avail idx.
avail_idx? This is actually from "vq->last_used_idx_res".
--yliu
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
Quoted 8 lines · Yuanhan Liu
Quoted 6 lines · Huawei
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 289
Quoted 4 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +3
-0+uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated;There is bug not using volatile to retrieve the avail idx.
avail_idx? This is actually from "vq->last_used_idx_res".
uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)]
the idx retrieved from avail->ring.
Quoted 3 lines
--yliu
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
Quoted 8 lines · Huawei
Quoted 7 lines · Yuanhan Liu
Quoted 6 lines · Huawei
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 289
Quoted 4 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +3
-0+uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated;There is bug not using volatile to retrieve the avail idx.
Quoted 5 lines
Quoted 1 line
avail_idx? This is actually from "vq->last_used_idx_res".
uint16_tidx = vq->avail->ring[avail_idx & (vq->size- 1)]the idx retrieved from
avail->ring.
Hmm.. I saw quite many similar lines of code retrieving an index from avail->ring, but none of them acutally use "volatile". So, a bug?
--yliu
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
Quoted 9 lines · Yuanhan Liu
Quoted 8 lines · Huawei
Quoted 7 lines · Yuanhan Liu
Quoted 6 lines · Huawei
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 289
Quoted 4 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +3
-0+uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated;There is bug not using volatile to retrieve the avail idx.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
avail_idx? This is actually from "vq->last_used_idx_res".
uint16_tidx = vq->avail->ring[avail_idx & (vq->size- 1)]the idx retrieved from
avail->ring.Hmm.. I saw quite many similar lines of code retrieving an index from
avail->ring, but none of them acutally use "volatile". So, a bug?
Others are not. This function is inline, and is in one translation unit with its caller.
Quoted 3 lines
--yliu
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
Quoted 10 lines · Huawei
Quoted 9 lines · Yuanhan Liu
Quoted 8 lines · Huawei
Quoted 7 lines · Yuanhan Liu
Quoted 6 lines · Huawei
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 289
Quoted 4 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +3
-0+uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated;There is bug not using volatile to retrieve the avail idx.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
avail_idx? This is actually from "vq->last_used_idx_res".
uint16_tidx = vq->avail->ring[avail_idx & (vq->size- 1)]the idx retrieved from
avail->ring.Hmm.. I saw quite many similar lines of code retrieving an index from
avail->ring, but none of them acutally use "volatile". So, a bug?Others are not. This function is inline, and is in one translation unit with its caller.
Oh, my fault. For the avail idx, we should take care on whether using volatile.
Quoted 3 lines
Quoted 2 lines
--yliu
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
Quoted 14 lines · Huawei
Quoted 13 lines · Huawei
Quoted 12 lines · Yuanhan Liu
Quoted 11 lines · Huawei
Quoted 8 lines · Yuanhan Liu
Quoted 6 lines · Huawei
Review
Quoted 4 lines · Yuanhan Liu
(quoted diff) +3
-0+ uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)]; + uint32_t vec_id = *vec_idx; + uint32_t len = *allocated;There is bug not using volatile to retrieve the avail idx.
avail_idx? This is actually from "vq->last_used_idx_res".
uint16_tidx = vq->avail->ring[avail_idx & (vq->size- 1)]
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
the idx retrieved from
avail->ring.Hmm.. I saw quite many similar lines of code retrieving an index from
avail->ring, but none of them acutally use "volatile". So, a bug?Others are not. This function is inline, and is in one translation unit with its caller.
Oh, my fault. For the avail idx, we should take care on whether using volatile.
I will keep it as it is. If there are any issues with it, let's fix it in another patch, but not in this refactor patch.
--yliu
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
In reply to Yuanhan LiuQuoted 8 lines · Yuanhan Liu
Current
virtio_dev_merge_rx()implementation just looks like the oldrte_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 tovirtio_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 tovec_bufat once makes the
[...]
Quoted 9 lines
lib/librte_vhost/vhost_rxtx.c +8
-1- } +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;
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 393
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +8
-1+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.
Quoted 31 lines
lib/librte_vhost/vhost_rxtx.c +9
-19-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.
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 412
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +7
-3-*/ -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.
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c +6
-0+ + mbuf_avail =rte_pktmbuf_data_len (m); + mbuf_offset =0 ; +while (1 ) { +/* done with current desc buf, get the next one */ +
[...]
lib/librte_vhost/vhost_rxtx.c virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id, · line 502
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +4
-0+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?
Quoted 20 lines
lib/librte_vhost/vhost_rxtx.c +3
-4rte_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 ();
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
Quoted 26 lines · Huawei
Quoted 8 lines · Yuanhan Liu
Current
virtio_dev_merge_rx()implementation just looks like the oldrte_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 tovirtio_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 tovec_bufat once makes the[...]
Quoted 16 lines
lib/librte_vhost/vhost_rxtx.c +14
-1- } +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 ;
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 393
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +2
-1- cpy_len =RTE_MIN (vb_avail, seg_avail); +if (m ==NULL ) +return 0 ;Is this inherited from old code?
Yes.
Quoted 2 lines
Let us remove the unnecessary check.
Caller ensures it is not NULL.
...
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 412
Quoted 2 lines
lib/librte_vhost/vhost_rxtx.c +2
-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.
Please, it's a refactor patch series. You have mentioned quite many trivial issues here and there, which I don't care too much and I don't think they would matter somehow. In addition, they are actually from the old code.
Quoted 8 lines
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +6
-0+ + mbuf_avail =rte_pktmbuf_data_len (m); + mbuf_offset =0 ; +while (1 ) { +/* done with current desc buf, get the next one */ +[...]
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id, · line 502
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +4
-0+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_usedfrom start and end?
When pkts[pkt_idx] is NULL, though you suggest to remove it, the check is here.
--yliu
Re: [PATCH v2 3/7] vhost: refactor virtio_dev_merge_rx
Quoted 36 lines · Yuanhan Liu
Quoted 26 lines · Huawei
Quoted 8 lines · Yuanhan Liu
Current
virtio_dev_merge_rx()implementation just looks like the oldrte_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 tovirtio_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 tovec_bufat once makes the[...]
Quoted 16 lines
lib/librte_vhost/vhost_rxtx.c +14
-1- } +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 ;Quoted 4 lines
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 393
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +2
-1- cpy_len =RTE_MIN (vb_avail, seg_avail); +if (m ==NULL ) +return 0 ;Is this inherited from old code?
Yes.
Quoted 2 lines
Let us remove the unnecessary check.
Caller ensures it is not NULL....
Quoted 8 lines
Quoted 4 lines
lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 412
Quoted 2 lines
lib/librte_vhost/vhost_rxtx.c +2
-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.Please, it's a refactor patch series. You have mentioned quite many trivial issues here and there, which I don't care too much and I don't think they would matter somehow. In addition, they are actually from the old code.
For normal code, it would be better using vq->vhost_hlen for example for future compatibility. For DPDK, we don't waste cycles whenever possible, especially vhost is the centralized bottleneck.
For the check of m == NULL, it should be removed, which not only occupies unnecessary branch predication resource but also causes confusion for return nr_used from copy_mbuf_to_desc_mergeable. It is OK if you don't want to fix this in this patchset.
Quoted 18 lines
Quoted 7 lines
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c +6
-0+ + mbuf_avail =rte_pktmbuf_data_len (m); + mbuf_offset =0 ; +while (1 ) { +/* done with current desc buf, get the next one */ +[...]
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id, · line 502
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +4
-0+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_usedfrom start and end?When pkts[pkt_idx] is NULL, though you suggest to remove it, the check is here.
--yliu
PATCH v2 4/7 vhost: do not use rte_memcpy for virtio_hdr copy
In reply to Yuanhan LiuFirst of all, rte_memcpy() is mostly useful for coping big packets by leveraging hardware advanced instructions like AVX. But for virtio net hdr, which is 12 bytes at most, invoking rte_memcpy() will not introduce any performance boost.
And, to my suprise, rte_memcpy() is VERY huge. Since rte_memcpy() is inlined, it increases the binary code size linearly every time we call it at a different place. Replacing the two rte_memcpy() with directly copy saves nearly 12K bytes of code size!
--- lib/librte_vhost/vhost_rxtx.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
lib/librte_vhost/vhost_rxtx.c +13 -4
@@ -92,6 +92,17 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)return ; } +static inline void +copy_virtio_net_hdr (struct vhost_virtqueue *vq,uint64_t desc_addr, +struct virtio_net_hdr_mrg_rxbuf hdr) +{ +if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + } +} +static inline int __attribute__((always_inline))copy_mbuf_to_desc (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf *m,uint16_t desc_idx,uint32_t *copied)
@@ -108,8 +119,7 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,rte_prefetch0 ((void *)(uintptr_t )desc_addr);virtio_enqueue_offload (m, &virtio_hdr.hdr); -rte_memcpy ((void *)(uintptr_t )desc_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); +copy_virtio_net_hdr (vq, desc_addr, virtio_hdr);PRINT_PACKET (dev, (uintptr_t )desc_addr, vq->vhost_hlen,0 ); desc_offset = vq->vhost_hlen;
@@ -404,8 +414,7 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, dev->device_fh, virtio_hdr.num_buffers);virtio_enqueue_offload (m, &virtio_hdr.hdr); -rte_memcpy ((void *)(uintptr_t )desc_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); +copy_virtio_net_hdr (vq, desc_addr, virtio_hdr);PRINT_PACKET (dev, (uintptr_t )desc_addr, vq->vhost_hlen,0 ); desc_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
Signature · 1 line
1.9.0
Re: [PATCH v2 4/7] vhost: do not use rte_memcpy for virtio_hdr copy
Quoted 1 line · Yuanhan Liu
Signed-off-by Yuanhan Liu <[email protected]>
Re: [PATCH v2 4/7] vhost: do not use rte_memcpy for virtio_hdr copy
In reply to Yuanhan Liulib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 105
Quoted 11 lines · On Thu, 18 Feb 2016 21:49:09 +0800 Yuanhan Liu <[email protected]>…
lib/librte_vhost/vhost_rxtx.c +11
-0+static inline void +copy_virtio_net_hdr (struct vhost_virtqueue *vq,uint64_t desc_addr, +struct virtio_net_hdr_mrg_rxbuf hdr) +{ +if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + } +} +
Don't use {} around single statements.
Since you are doing all this casting, why not just use regular old memcpy which will be inlined by Gcc into same instructions anyway. And since are always casting the desc_addr, why not pass a type that doesn't need the additional cast (like void *)
Re: [PATCH v2 4/7] vhost: do not use rte_memcpy for virtio_hdr copy
Quoted 15 lines · Stephen Hemminger
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 105
Quoted 11 lines · On Thu, 18 Feb 2016 21:49:09 +0800 Yuanhan Liu <[email protected]>…
lib/librte_vhost/vhost_rxtx.c +11
-0+static inline void +copy_virtio_net_hdr (struct vhost_virtqueue *vq,uint64_t desc_addr, +struct virtio_net_hdr_mrg_rxbuf hdr) +{ +if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + } +} +Don't use {} around single statements.
There are other cs issues, like
used_idx = vq->last_used_idx & (vq->size -1);
^ space needed
Please run checkpatch against your patch.
Quoted 5 lines
Since you are doing all this casting, why not just use regular old memcpy which will be inlined by Gcc into same instructions anyway. And since are always casting the
desc_addr, why not pass a type that doesn't need the additional cast (like void *)
Re: [PATCH v2 4/7] vhost: do not use rte_memcpy for virtio_hdr copy
In reply to Stephen HemmingerQuoted 16 lines · Stephen Hemminger
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 105
Quoted 11 lines · On Thu, 18 Feb 2016 21:49:09 +0800 Yuanhan Liu <[email protected]>…
lib/librte_vhost/vhost_rxtx.c +11
-0+static inline void +copy_virtio_net_hdr (struct vhost_virtqueue *vq,uint64_t desc_addr, +struct virtio_net_hdr_mrg_rxbuf hdr) +{ +if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) { + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; + }else { + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; + } +} +Don't use {} around single statements.
Oh, I was thinking that it's a personal preference. Okay, I will remove them.
Quoted 2 lines
Since you are doing all this casting, why not just use regular old memcpy which will be inlined by Gcc into same instructions anyway.
I thought there are some (tiny) differences: memcpy() is not an inlined function. And I was thinking it generates some slightly more complicated instructions.
Quoted 2 lines
And since are always casting the
desc_addr, why not pass a type that doesn't need the additional cast (like void *)
You have to cast it from "uint64_t" to "void *" as well while call it. So, that makes no difference.
--yliu
PATCH v2 5/7 vhost: don't use unlikely for VIRTIO_NET_F_MRG_RXBUF detection
In reply to Yuanhan LiuVIRTIO_NET_F_MRG_RXBUF is a default feature supported by vhost. Adding unlikely for VIRTIO_NET_F_MRG_RXBUF detection doesn't make sense to me at all.
--- lib/librte_vhost/vhost_rxtx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost_rxtx.c +1 -1
@@ -538,7 +538,7 @@ uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id, struct rte_mbuf **pkts, uint16_t count) { - if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))) + if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) return virtio_dev_merge_rx(dev, queue_id, pkts, count); else return virtio_dev_rx(dev, queue_id, pkts, count);
Signature · 1 line
1.9.0
PATCH v2 6/7 vhost: do sanity check for desc->len
In reply to Yuanhan LiuWe need make sure that desc->len is bigger than the size of virtio net header, otherwise, unexpected behaviour might happen due to "desc_avail" would become a huge number with for following code:
desc_avail = desc->len - vq->vhost_hlen;
For dequeue code path, it will try to allocate enough mbuf to hold such size of desc buf, which ends up with consuming all mbufs, leading to no free mbuf is avaliable. Therefore, you might see an error message:
Failed to allocate memory for mbuf.
Also, for both dequeue/enqueue code path, while it copies data from/to desc buf, the big "desc_avail" would result to access memory not belong the desc buf, which could lead to some potential memory access errors.
A malicious guest could easily forge such malformed vring desc buf. Every time we restart an interrupted DPDK application inside guest would also trigger this issue, as all huge pages are reset to 0 during DPDK re-init, leading to desc->len being 0.
Therefore, this patch does a sanity check for desc->len, to make vhost robust.
--- lib/librte_vhost/vhost_rxtx.c | 9 +++++++++ 1 file changed, 9 insertions(+)
lib/librte_vhost/vhost_rxtx.c +9 -0
@@ -115,6 +115,9 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; desc = &vq->desc[desc_idx]; +if (unlikely (desc->len < vq->vhost_hlen)) +return -1 ; + desc_addr =gpa_to_vva (dev, desc->addr);rte_prefetch0 ((void *)(uintptr_t )desc_addr);
@@ -406,6 +409,9 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,"(%" PRIu64") Current Index %d| End Index %d\n" , dev->device_fh, cur_idx, res_end_idx); +if (vq->buf_vec[vec_idx].buf_len < vq->vhost_hlen) +return -1 ; + desc_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr);rte_prefetch0 ((void *)(uintptr_t )desc_addr);
@@ -649,6 +655,9 @@ copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,struct virtio_net_hdr *hdr; desc = &vq->desc[desc_idx]; +if (unlikely (desc->len < vq->vhost_hlen)) +return NULL ; + desc_addr =gpa_to_vva (dev, desc->addr);rte_prefetch0 ((void *)(uintptr_t )desc_addr);
Signature · 1 line
1.9.0
PATCH v2 7/7 vhost: do sanity check for desc->next
In reply to Yuanhan LiuA malicious guest may easily forge some illegal vring desc buf. To make our vhost robust, we need make sure desc->next will not go beyond the vq->desc[] array.
--- lib/librte_vhost/vhost_rxtx.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
lib/librte_vhost/vhost_rxtx.c +11 -4
@@ -148,6 +148,8 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,/* Room in vring buffer is not enough */ return -1 ; } +if (unlikely (desc->next >= vq->size)) +return -1 ; desc = &vq->desc[desc->next]; desc_addr =gpa_to_vva (dev, desc->addr);
@@ -302,7 +304,7 @@ fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,uint32_t len = *allocated;while (1 ) { -if (vec_id >=BUF_VECTOR_MAX ) +if (unlikely (vec_id >=BUF_VECTOR_MAX || idx >= vq->size))return -1 ; len += vq->desc[idx].len;
@@ -671,6 +673,8 @@ copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,while (desc_avail || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) {/* This desc reachs to its end, get the next one */ if (desc_avail ==0 ) { +if (unlikely (desc->next >= vq->size)) + goto fail; desc = &vq->desc[desc->next]; desc_addr =gpa_to_vva (dev, desc->addr);
@@ -691,9 +695,7 @@ copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,if (unlikely (!cur)) {RTE_LOG (ERR ,VHOST_DATA ,"Failed to " "allocate memory for mbuf.\n" ); -if (head) -rte_pktmbuf_free (head); -return NULL ; + goto fail; }if (!head) { head = cur;
@@ -729,6 +731,11 @@ copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, }return head; + +fail: +if (head) +rte_pktmbuf_free (head); +return NULL ; }uint16_t
Signature · 1 line
1.9.0
Re: [PATCH v2 7/7] vhost: do sanity check for desc->next
lib/librte_vhost/vhost_rxtx.c copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 677
Quoted 2 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +2
-0+if (unlikely (desc->next >= vq->size)) + goto fail;
desc chains could be forged into a loop then vhost runs the dead loop until it exhaust all mbuf memory.
Re: [PATCH v2 7/7] vhost: do sanity check for desc->next
Quoted 6 lines · Huawei
lib/librte_vhost/vhost_rxtx.c copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 677
Quoted 2 lines · Yuanhan Liu
lib/librte_vhost/vhost_rxtx.c +2
-0+if (unlikely (desc->next >= vq->size)) + goto fail;desc chains could be forged into a loop then vhost runs the dead loop until it exhaust all mbuf memory.
Good point. Any elegant solution to avoid that?
--yliu
Hi Yuanhan
2016-02-18 21:49, Yuanhan Liu:
Quoted 2 lines
Here is a patchset for refactoring vhost rxtx code, mainly for improving readability.
This series requires to be rebased.
And maybe you could check also the series about numa_realloc.
Thanks
Quoted 9 lines · Thomas Monjalon
Hi Yuanhan
2016-02-18 21:49, Yuanhan Liu:
Quoted 2 lines
Here is a patchset for refactoring vhost rxtx code, mainly for improving readability.
This series requires to be rebased.
And maybe you could check also the series about
numa_realloc.
Hi Thomas,
Sure, I will. And since you are considering to merge it, I will do more tests, espeically on this patchset (it touches the vhost-user core). Thus, I may send the new version out a bit late, say, next week.
--yliu
v3 cover vhost rxtx refactor and fixes
In reply to Yuanhan Liuv3: - quite few minor changes, including using likely/unlikely
when possible.
- Added a new patch 8 to avoid desc dead loop chain
The first 3 patches refactor 3 major functions at vhost_rxtx.c. It simplifies the code logic, making it more readable. OTOH, it reduces binary code size, due to a lot of duplicate code are removed, as well as some huge inline functions are diminished.
Patch 4 gets rid of the rte_memcpy for virtio_hdr copy, which nearly saves 12K bytes of binary code size!
Patch 5 removes "unlikely" for VIRTIO_NET_F_MRG_RXBUF detection.
Patch 6, 7 and 8 do some sanity check for two desc fields, to make vhost robust and be protected from malicious guest or abnormal use cases.
---
Yuanhan Liu (8):
vhost: refactor rte_vhost_dequeue_burst
vhost: refactor virtio_dev_rx
vhost: refactor virtio_dev_merge_rx
vhost: do not use rte_memcpy for virtio_hdr copy
vhost: don't use unlikely for VIRTIO_NET_F_MRG_RXBUF detection
vhost: do sanity check for desc->len
vhost: do sanity check for desc->next against with vq->size
vhost: avoid dead loop chain.
lib/librte_vhost/vhost_rxtx.c | 1027 ++++++++++++++++++----------------------- 1 file changed, 453 insertions(+), 574 deletions(-)
Signature · 1 line
1.9.0
PATCH v3 1/8 vhost: refactor rte_vhost_dequeue_burst
The current rte_vhost_dequeue_burst() implementation is a bit messy and logic twisted. And you could see repeat code here and there.
However, rte_vhost_dequeue_burst() acutally does a simple job: copy the packet data from vring desc to mbuf. What's tricky here is:
- desc buff could be chained (by desc->next field), so that you need fetch next one if current is wholly drained.
- One mbuf could not be big enough to hold all desc buff, hence you need to chain the mbuf as well, by the mbuf->next field.
The simplified code looks like following:
while (this_desc_is_not_drained_totally || has_next_desc) {
if (this_desc_has_drained_totally) {
this_desc = next_desc();
}
if (mbuf_has_no_room) {
mbuf = allocate_a_new_mbuf();
}
COPY(mbuf, desc);
}Note that the old patch does a special handling for skipping virtio header. However, that could be simply done by adjusting desc_avail and desc_offset var:
desc_avail = desc->len - vq->vhost_hlen; desc_offset = vq->vhost_hlen;
This refactor makes the code much more readable (IMO), yet it reduces binary code size.
---
v2: - fix potential NULL dereference bug of var "prev" and "head"
v3: - add back missing prefetches reported by Huawei
- Passing head mbuf as an arg, instead of allocating it at copy_desc_to_mbuf().
--- lib/librte_vhost/vhost_rxtx.c | 301 +++++++++++++++++------------------------- 1 file changed, 121 insertions(+), 180 deletions(-)
lib/librte_vhost/vhost_rxtx.c +121 -180
@@ -801,21 +801,97 @@ make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)return 0 ; } +static inline int __attribute__((always_inline)) +copy_desc_to_mbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *m,uint16_t desc_idx, +struct rte_mempool *mbuf_pool) +{ +struct vring_desc *desc; +uint64_t desc_addr; +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; +struct rte_mbuf *cur = m, *prev = m; +struct virtio_net_hdr *hdr; + + desc = &vq->desc[desc_idx]; + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +/* Retrieve virtio net header */ + hdr = (struct virtio_net_hdr *)((uintptr_t )desc_addr); + desc_avail = desc->len - vq->vhost_hlen; + desc_offset = vq->vhost_hlen; + + mbuf_offset =0 ; + mbuf_avail = m->buf_len -RTE_PKTMBUF_HEADROOM ; +while (desc_avail !=0 || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) { +/* This desc reaches to its end, get the next one */ +if (desc_avail ==0 ) { + desc = &vq->desc[desc->next]; + + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + + desc_offset =0 ; + desc_avail = desc->len; + +PRINT_PACKET (dev, (uintptr_t )desc_addr, desc->len,0 ); + } + +/* +* This mbuf reaches to its end, get a new one +* to hold more data. +*/ +if (mbuf_avail ==0 ) { + cur =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (cur ==NULL )) { +RTE_LOG (ERR ,VHOST_DATA ,"Failed to " +"allocate memory for mbuf.\n" ); +return -1 ; + } + + prev->next = cur; + prev->data_len = mbuf_offset; + m->nb_segs +=1 ; + m->pkt_len += mbuf_offset; + prev = cur; + + mbuf_offset =0 ; + mbuf_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; + } + + cpy_len =RTE_MIN (desc_avail, mbuf_avail); + rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset), + (void *)((uintptr_t)(desc_addr + desc_offset)), + cpy_len); + + mbuf_avail -= cpy_len; + mbuf_offset += cpy_len; + desc_avail -= cpy_len; + desc_offset += cpy_len; + } + + prev->data_len = mbuf_offset; + m->pkt_len += mbuf_offset; + +if (hdr->flags !=0 || hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE ) +vhost_dequeue_offload (hdr, m); + +return 0 ; +} +uint16_t rte_vhost_dequeue_burst (struct virtio_net *dev,uint16_t queue_id,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) { -struct rte_mbuf *m, *prev, *rarp_mbuf =NULL ; +struct rte_mbuf *rarp_mbuf =NULL ;struct vhost_virtqueue *vq; -struct vring_desc *desc; -uint64_t vb_addr =0 ; -uint64_t vb_net_hdr_addr =0 ; -uint32_t head[MAX_PKT_BURST ]; +uint32_t desc_indexes[MAX_PKT_BURST ];uint32_t used_idx; -uint32_t i; -uint16_t free_entries, entry_success =0 ; +uint32_t i =0 ; +uint16_t free_entries;uint16_t avail_idx; -struct virtio_net_hdr *hdr =NULL ;if (unlikely (!is_valid_virt_queue_idx (queue_id,1 , dev->virt_qp_nb))) { RTE_LOG(ERR, VHOST_DATA,
@@ -852,198 +928,63 @@ rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, } avail_idx = *((volatile uint16_t *)&vq->avail->idx); - -/* If there are no available buffers then return. */ -if (vq->last_used_idx == avail_idx) + free_entries = avail_idx - vq->last_used_idx; +if (free_entries ==0 ) goto out; -LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, - dev->device_fh); +LOG_DEBUG (VHOST_DATA ,"%s (%" PRIu64")\n" , __func__, dev->device_fh);/* Prefetch available ring to retrieve head indexes. */ -rte_prefetch0 (&vq->avail->ring[vq->last_used_idx & (vq->size -1 )]); + used_idx = vq->last_used_idx & (vq->size -1 ); +rte_prefetch0 (&vq->avail->ring[used_idx]); -/*get the number of free entries in the ring*/ - free_entries = (avail_idx - vq->last_used_idx); + count =RTE_MIN (count,MAX_PKT_BURST ); + count =RTE_MIN (count, free_entries); +LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") about to dequeue %u buffers\n" , + dev->device_fh, count); - free_entries =RTE_MIN (free_entries, count); -/* Limit to MAX_PKT_BURST. */ - free_entries =RTE_MIN (free_entries,MAX_PKT_BURST ); - -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Buffers available %d\n" , - dev->device_fh, free_entries);/* Retrieve all of the head indexes first to avoid caching issues. */ -for (i =0 ; i < free_entries; i++) - head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size -1 )]; +for (i =0 ; i < count; i++) { + desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) & + (vq->size -1 )]; + }/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success]]); +rte_prefetch0 (&vq->desc[desc_indexes[0 ]]);rte_prefetch0 (&vq->used->ring[vq->last_used_idx & (vq->size -1 )]); -while (entry_success < free_entries) { -uint32_t vb_avail, vb_offset; -uint32_t seg_avail, seg_offset; -uint32_t cpy_len; -uint32_t seg_num =0 ; -struct rte_mbuf *cur; -uint8_t alloc_err =0 ; - - desc = &vq->desc[head[entry_success]]; - - vb_net_hdr_addr =gpa_to_vva (dev, desc->addr); - hdr = (struct virtio_net_hdr *)((uintptr_t )vb_net_hdr_addr); - -/* Discard first buffer as it is the virtio header */ -if (desc->flags &VRING_DESC_F_NEXT ) { - desc = &vq->desc[desc->next]; - vb_offset =0 ; - vb_avail = desc->len; - }else { - vb_offset = vq->vhost_hlen; - vb_avail = desc->len - vb_offset; - } - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - - used_idx = vq->last_used_idx & (vq->size -1 ); +for (i =0 ; i < count; i++) { +int err; -if (entry_success < (free_entries -1 )) { -/* Prefetch descriptor index. */ -rte_prefetch0 (&vq->desc[head[entry_success+1 ]]); -rte_prefetch0 (&vq->used->ring[(used_idx +1 ) & (vq->size -1 )]); +if (likely (i +1 < count)) { +rte_prefetch0 (&vq->desc[desc_indexes[i +1 ]]); +rte_prefetch0 (&vq->used->ring[(used_idx +1 ) & + (vq->size -1 )]); } -/* Update used index buffer information. */ - vq->used->ring[used_idx].id = head[entry_success]; - vq->used->ring[used_idx].len =0 ; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - -/* Allocate an mbuf and populate the structure. */ - m =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (m ==NULL )) { + pkts[i] =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (pkts[i] ==NULL )) {RTE_LOG (ERR ,VHOST_DATA ,"Failed to allocate memory for mbuf.\n" );break ; } - seg_offset =0 ; - seg_avail = m->buf_len -RTE_PKTMBUF_HEADROOM ; - cpy_len =RTE_MIN (vb_avail, seg_avail); - -PRINT_PACKET (dev, (uintptr_t )vb_addr, desc->len,0 ); - - seg_num++; - cur = m; - prev = m; -while (cpy_len !=0 ) { - rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset), - (void *)((uintptr_t)(vb_addr + vb_offset)), - cpy_len); - - seg_offset += cpy_len; - vb_offset += cpy_len; - vb_avail -= cpy_len; - seg_avail -= cpy_len; - -if (vb_avail !=0 ) { -/* -* The segment reachs to its end, -* while the virtio buffer in TX vring has -* more data to be copied. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* Allocate mbuf and populate the structure. */ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA ,"Failed to " -"allocate memory for mbuf.\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - }else { -if (desc->flags &VRING_DESC_F_NEXT ) { -/* -* There are more virtio buffers in -* same vring entry need to be copied. -*/ -if (seg_avail ==0 ) { -/* -* The current segment hasn't -* room to accomodate more -* data. -*/ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; -/* -* Allocate an mbuf and -* populate the structure. -*/ - cur =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (cur ==NULL )) { -RTE_LOG (ERR , -VHOST_DATA , -"Failed to " -"allocate memory " -"for mbuf\n" ); -rte_pktmbuf_free (m); - alloc_err =1 ; -break ; - } - seg_num++; - prev->next = cur; - prev = cur; - seg_offset =0 ; - seg_avail = cur->buf_len -RTE_PKTMBUF_HEADROOM ; - } - - desc = &vq->desc[desc->next]; - -/* Buffer address translation. */ - vb_addr =gpa_to_vva (dev, desc->addr); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - vb_offset =0 ; - vb_avail = desc->len; - -PRINT_PACKET (dev, (uintptr_t )vb_addr, - desc->len,0 ); - }else { -/* The whole packet completes. */ - cur->data_len = seg_offset; - m->pkt_len += seg_offset; - vb_avail =0 ; - } - } - - cpy_len =RTE_MIN (vb_avail, seg_avail); - } - -if (unlikely (alloc_err ==1 )) + err =copy_desc_to_mbuf (dev, vq, pkts[i], desc_indexes[i], + mbuf_pool); +if (unlikely (err)) { +rte_pktmbuf_free (pkts[i]);break ; + } - m->nb_segs = seg_num; -if ((hdr->flags !=0 ) || (hdr->gso_type !=VIRTIO_NET_HDR_GSO_NONE )) -vhost_dequeue_offload (hdr, m); - - pkts[entry_success] = m; - vq->last_used_idx++; - entry_success++; + used_idx = vq->last_used_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_indexes[i]; + vq->used->ring[used_idx].len =0 ; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used, ring[used_idx]), + sizeof(vq->used->ring[used_idx])); }rte_compiler_barrier (); - vq->used->idx += entry_success; + vq->used->idx += i;vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx),sizeof (vq->used->idx));
@@ -1057,10 +998,10 @@ out:* Inject it to the head of "pkts" array, so that switch's mac * learning table will get updated first. */ -memmove (&pkts[1 ], pkts, entry_success *sizeof (m)); +memmove (&pkts[1 ], pkts, i *sizeof (struct rte_mbuf *)); pkts[0 ] = rarp_mbuf; - entry_success +=1 ; + i +=1 ; } -return entry_success; +return i; }
Signature · 1 line
1.9.0
PATCH v3 2/8 vhost: refactor virtio_dev_rx
In reply to Yuanhan LiuThis is a simple refactor, as there isn't any twisted logic in old code. Here I just broke the code and introduced two helper functions, reserve_avail_buf() and copy_mbuf_to_desc() to make the code more readable.
Also, it saves nearly 1K bytes of binary code size.
---
v2: - fix NULL dereference bug found by Rich.
v3: - use while (mbuf_avail || m->next) to align with the style of
coyp_desc_to_mbuf() -- suggestec by Huawei
- use likely/unlikely when possible
--- lib/librte_vhost/vhost_rxtx.c | 296 ++++++++++++++++++++---------------------- 1 file changed, 141 insertions(+), 155 deletions(-)
lib/librte_vhost/vhost_rxtx.c +141 -155
@@ -129,6 +129,115 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)return ; } +static inline int __attribute__((always_inline)) +copy_mbuf_to_desc (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *m,uint16_t desc_idx,uint32_t *copied) +{ +uint32_t desc_avail, desc_offset; +uint32_t mbuf_avail, mbuf_offset; +uint32_t cpy_len; +struct vring_desc *desc; +uint64_t desc_addr; +struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; + + desc = &vq->desc[desc_idx]; + desc_addr =gpa_to_vva (dev, desc->addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + +virtio_enqueue_offload (m, &virtio_hdr.hdr); +rte_memcpy ((void *)(uintptr_t )desc_addr, + (const void *)&virtio_hdr, vq->vhost_hlen); +vhost_log_write (dev, desc->addr, vq->vhost_hlen); +PRINT_PACKET (dev, (uintptr_t )desc_addr, vq->vhost_hlen,0 ); + + desc_offset = vq->vhost_hlen; + desc_avail = desc->len - vq->vhost_hlen; + + *copied =rte_pktmbuf_pkt_len (m); + mbuf_avail =rte_pktmbuf_data_len (m); + mbuf_offset =0 ; +while (mbuf_avail !=0 || m->next !=NULL ) { +/* done with current mbuf, fetch next */ +if (mbuf_avail ==0 ) { + m = m->next; + + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (m); + } + +/* done with current desc buf, fetch next */ +if (desc_avail ==0 ) { +if ((desc->flags &VRING_DESC_F_NEXT ) ==0 ) { +/* Room in vring buffer is not enough */ +return -1 ; + } + + desc = &vq->desc[desc->next]; + desc_addr =gpa_to_vva (dev, desc->addr); + desc_offset =0 ; + desc_avail = desc->len; + } + + cpy_len =RTE_MIN (desc_avail, mbuf_avail); +rte_memcpy ((void *)((uintptr_t )(desc_addr + desc_offset)), + rte_pktmbuf_mtod_offset(m, void *, mbuf_offset), + cpy_len); +vhost_log_write (dev, desc->addr + desc_offset, cpy_len); +PRINT_PACKET (dev, (uintptr_t )(desc_addr + desc_offset), + cpy_len,0 ); + + mbuf_avail -= cpy_len; + mbuf_offset += cpy_len; + desc_avail -= cpy_len; + desc_offset += cpy_len; + } + +return 0 ; +} + +/* +* As many data cores may want to access available buffers +* they need to be reserved. +*/ +static inline uint32_t +reserve_avail_buf (struct vhost_virtqueue *vq,uint32_t count, +uint16_t *start,uint16_t *end) +{ +uint16_t res_start_idx; +uint16_t res_end_idx; +uint16_t avail_idx; +uint16_t free_entries; +int success; + + count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); + +again: + res_start_idx = vq->last_used_idx_res; + avail_idx = *((volatile uint16_t *)&vq->avail->idx); + + free_entries = avail_idx - res_start_idx; + count =RTE_MIN (count, free_entries); +if (count ==0 ) +return 0 ; + + res_end_idx = res_start_idx + count; + +/* +* update vq->last_used_idx_res atomically; try again if failed. +* +* TODO: Allow to disable cmpset if no concurrency in application. +*/ + success =rte_atomic16_cmpset (&vq->last_used_idx_res, + res_start_idx, res_end_idx); +if (unlikely (!success)) + goto again; + + *start = res_start_idx; + *end = res_end_idx; + +return count; +} +/** * This function adds buffers to the virtio devices RX virtqueue. Buffers can * be received from the physical port or from another virtio device. A packet
@@ -138,21 +247,12 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)*/ 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 rte_mbuf **pkts,uint32_t count) {struct vhost_virtqueue *vq; -struct vring_desc *desc, *hdr_desc; -struct rte_mbuf *buff, *first_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 ; +uint16_t res_start_idx, res_end_idx; +uint16_t desc_indexes[MAX_PKT_BURST ]; +uint32_t i;LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") virtio_dev_rx()\n" , dev->device_fh);if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) {
@@ -166,161 +266,47 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,if (unlikely (vq->enabled ==0 ))return 0 ; - 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 ; -uint16_t idx; - -/* Get descriptor from available ring */ - desc = &vq->desc[head[packet_success]]; - - buff = pkts[packet_success]; - first_buff = buff; - -/* 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; - hdr_desc = desc; - -/* -* 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 ; - } + count =reserve_avail_buf (vq, count, &res_start_idx, &res_end_idx); +if (count ==0 ) +return 0 ; - 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); -vhost_log_write (dev, desc->addr + vb_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 ; +LOG_DEBUG (VHOST_DATA , +"(%" PRIu64") res_start_idx %d| res_end_idx Index %d\n" , + dev->device_fh, res_start_idx, res_end_idx); -/* The current segment completes */ -if (offset == data_len) { - buff = buff->next; - offset =0 ; - data_len =rte_pktmbuf_data_len (buff); - } +/* Retrieve all of the desc indexes first to avoid caching issues. */ +rte_prefetch0 (&vq->avail->ring[res_start_idx & (vq->size -1 )]); +for (i =0 ; i < count; i++) { + desc_indexes[i] = vq->avail->ring[(res_start_idx + i) & + (vq->size -1 )]; + } -/* 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); - } +rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); +for (i =0 ; i < count; i++) { +uint16_t desc_idx = desc_indexes[i]; +uint16_t used_idx = (res_start_idx + i) & (vq->size -1 ); +uint32_t copied; +int err; -/* Update used ring with desc information */ - idx = res_cur_idx & (vq->size -1 ); - vq->used->ring[idx].id = head[packet_success]; + err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx, &copied); -/* Drop the packet if it is uncompleted */ -if (unlikely (uncompleted_pkt ==1 )) - vq->used->ring[idx].len = vq->vhost_hlen; + vq->used->ring[used_idx].id = desc_idx; +if (unlikely (err)) + vq->used->ring[used_idx].len = vq->vhost_hlen;else - vq->used->ring[idx].len = pkt_len + vq->vhost_hlen; - + vq->used->ring[used_idx].len = copied + vq->vhost_hlen;vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[idx]), - sizeof(vq->used->ring[idx])); + offsetof(struct vring_used, ring[used_idx]), + sizeof(vq->used->ring[used_idx])); - res_cur_idx++; - packet_success++; - -if (unlikely (uncompleted_pkt ==1 )) -continue ; - -virtio_enqueue_offload (first_buff, &virtio_hdr.hdr); - -rte_memcpy ((void *)(uintptr_t )buff_hdr_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); -vhost_log_write (dev, hdr_desc->addr, 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]]); - } +if (i +1 < count) +rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); }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 != res_start_idx))rte_pause (); *(volatile uint16_t *)&vq->used->idx += count;
Signature · 1 line
1.9.0
PATCH v3 3/8 vhost: refactor virtio_dev_merge_rx
In reply to Yuanhan LiuCurrent 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 difference a bit bigger then.
The refactored code looks like below:
while (mbuf_has_not_drained_totally || mbuf_has_next) {
if (this_desc_has_no_room) {
this_desc = fetch_next_from_vec_buf();
if (it is the last of a desc chain)
update_used_ring();
}
if (this_mbuf_has_drained_totally)
mbuf = fetch_next_mbuf();
COPY(this_desc, this_mbuf);
}This patch reduces quite many lines of code, therefore, make it much more readable.
--- lib/librte_vhost/vhost_rxtx.c | 404 +++++++++++++++++------------------------- 1 file changed, 166 insertions(+), 238 deletions(-)
lib/librte_vhost/vhost_rxtx.c +166 -238
@@ -324,251 +324,204 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,return count; } -static inline uint32_t __attribute__((always_inline)) -copy_from_mbuf_to_vring (struct virtio_net *dev,uint32_t queue_id, -uint16_t res_base_idx,uint16_t res_end_idx, -struct rte_mbuf *pkt) +static inline int +fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, +uint32_t *allocated,uint32_t *vec_idx) { -uint32_t vec_idx =0 ; -uint32_t entry_success =0 ; -struct vhost_virtqueue *vq; -/* The virtio_hdr is initialised to 0. */ -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = { - {0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint16_t cur_idx = res_base_idx; -uint64_t vb_addr =0 ; -uint64_t vb_hdr_addr =0 ; -uint32_t seg_offset =0 ; -uint32_t vb_offset =0 ; -uint32_t seg_avail; -uint32_t vb_avail; -uint32_t cpy_len, entry_len; -uint16_t idx; - -if (pkt ==NULL ) -return 0 ; +uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated; -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Current Index %d| " -"End Index %d\n" , - dev->device_fh, cur_idx, res_end_idx); +while (1 ) { +if (vec_id >=BUF_VECTOR_MAX ) +return -1 ; -/* -* Convert from gpa to vva -* (guest physical addr -> vhost virtual addr) -*/ - vq = dev->virtqueue[queue_id]; + len += vq->desc[idx].len; + vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr; + vq->buf_vec[vec_id].buf_len = vq->desc[idx].len; + vq->buf_vec[vec_id].desc_idx = idx; + vec_id++; - vb_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); - vb_hdr_addr = vb_addr; +if ((vq->desc[idx].flags &VRING_DESC_F_NEXT ) ==0 ) +break ; -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); + idx = vq->desc[idx].next; + } - virtio_hdr.num_buffers = res_end_idx - res_base_idx; + *allocated = len; + *vec_idx = vec_id; -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") RX: Num merge buffers %d\n" , - dev->device_fh, virtio_hdr.num_buffers); +return 0 ; +} -virtio_enqueue_offload (pkt, &virtio_hdr.hdr); +/* +* As many data cores may want to access available buffers concurrently, +* they need to be reserved. +* +* Returns -1 on fail, 0 on success +*/ +static inline int +reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, +uint16_t *start,uint16_t *end) +{ +uint16_t res_start_idx; +uint16_t res_cur_idx; +uint16_t avail_idx; +uint32_t allocated; +uint32_t vec_idx; +uint16_t tries; -rte_memcpy ((void *)(uintptr_t )vb_hdr_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); -vhost_log_write (dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen); +again: + res_start_idx = vq->last_used_idx_res; + res_cur_idx = res_start_idx; + + allocated =0 ; + vec_idx =0 ; + tries =0 ; +while (1 ) { + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +if (unlikely (res_cur_idx == avail_idx)) { +LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Failed " +"to get enough desc from vring\n" , + dev->device_fh); +return -1 ; + } -PRINT_PACKET (dev, (uintptr_t )vb_hdr_addr, vq->vhost_hlen,1 ); +if (fill_vec_buf (vq, res_cur_idx, &allocated, &vec_idx) <0 ) +return -1 ; - seg_avail =rte_pktmbuf_data_len (pkt); - vb_offset = vq->vhost_hlen; - vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen; + res_cur_idx++; + tries++; - entry_len = vq->vhost_hlen; +if (allocated >= size) +break ; -if (vb_avail ==0 ) { -uint32_t desc_idx = vq->buf_vec[vec_idx].desc_idx; +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (tries >= vq->size) +return -1 ; + } -if ((vq->desc[desc_idx].flags &VRING_DESC_F_NEXT ) ==0 ) { - idx = cur_idx & (vq->size -1 ); +/* +* update vq->last_used_idx_res atomically. +* retry again if failed. +*/ +if (rte_atomic16_cmpset (&vq->last_used_idx_res, + res_start_idx, res_cur_idx) ==0 ) + goto again; -/* Update used ring with desc information */ - vq->used->ring[idx].id = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[idx].len = entry_len; + *start = res_start_idx; + *end = res_cur_idx; +return 0 ; +} -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[idx]), - sizeof(vq->used->ring[idx])); +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; - entry_len =0 ; - cur_idx++; - entry_success++; - } +if (unlikely (m ==NULL )) +return 0 ; - vec_idx++; - vb_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); +LOG_DEBUG (VHOST_DATA , +"(%" PRIu64") Current Index %d| End Index %d\n" , + dev->device_fh, cur_idx, res_end_idx); -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - vb_offset =0 ; - vb_avail = vq->buf_vec[vec_idx].buf_len; - } + desc_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); - cpy_len =RTE_MIN (vb_avail, seg_avail); + 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); -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); -vhost_log_write (dev, vq->buf_vec[vec_idx].buf_addr + vb_offset, - cpy_len); +virtio_enqueue_offload (m, &virtio_hdr.hdr); +rte_memcpy ((void *)(uintptr_t )desc_addr, + (const void *)&virtio_hdr, vq->vhost_hlen); +vhost_log_write (dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen); +PRINT_PACKET (dev, (uintptr_t )desc_addr, vq->vhost_hlen,0 ); -PRINT_PACKET (dev, - (uintptr_t )(vb_addr + vb_offset), - cpy_len,0 ); + desc_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen; + desc_offset = vq->vhost_hlen; - 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 ) { + mbuf_avail =rte_pktmbuf_data_len (m); + mbuf_offset =0 ; +while (mbuf_avail !=0 || m->next !=NULL ) { +/* done with current desc buf, get the next one */ +if (desc_avail ==0 ) { + desc_idx = vq->buf_vec[vec_idx].desc_idx; + +if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) {/* Update used ring with desc information */ - idx = cur_idx & (vq->size -1 ); - vq->used->ring[idx].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[idx].len = entry_len; + used_idx = cur_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_idx; + vq->used->ring[used_idx].len = desc_offset;vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[idx]), - sizeof(vq->used->ring[idx])); - entry_len =0 ; - cur_idx++; - entry_success++; + offsetof(struct vring_used , + ring[used_idx]), + sizeof(vq->used->ring[used_idx])); } vec_idx++; - vb_addr =gpa_to_vva (dev, - vq->buf_vec[vec_idx].buf_addr); - vb_offset =0 ; - vb_avail = vq->buf_vec[vec_idx].buf_len; - cpy_len =RTE_MIN (vb_avail, seg_avail); - }else { -/* -* This current segment complete, need continue to -* check if the whole packet complete or not. -*/ - pkt = pkt->next; -if (pkt !=NULL ) { -/* -* There are more segments. -*/ -if (vb_avail ==0 ) { -/* -* This current buffer from vring is -* used up, need fetch next buffer -* from buf_vec. -*/ -uint32_t desc_idx = - vq->buf_vec[vec_idx].desc_idx; - -if ((vq->desc[desc_idx].flags & -VRING_DESC_F_NEXT ) ==0 ) { - idx = cur_idx & (vq->size -1 ); -/* -* Update used ring with the -* descriptor information -*/ - vq->used->ring[idx].id - = desc_idx; - vq->used->ring[idx].len - = entry_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[idx]), - sizeof(vq->used->ring[idx])); - entry_success++; - entry_len =0 ; - cur_idx++; - } - -/* Get next buffer from buf_vec. */ - vec_idx++; - vb_addr =gpa_to_vva (dev, - vq->buf_vec[vec_idx].buf_addr); - vb_avail = - vq->buf_vec[vec_idx].buf_len; - vb_offset =0 ; - } - - seg_offset =0 ; - seg_avail =rte_pktmbuf_data_len (pkt); - cpy_len =RTE_MIN (vb_avail, seg_avail); - }else { -/* -* This whole packet completes. -*/ -/* Update used ring with desc information */ - idx = cur_idx & (vq->size -1 ); - vq->used->ring[idx].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[idx].len = entry_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[idx]), - sizeof(vq->used->ring[idx])); - entry_success++; -break ; - } + desc_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); + +/* Prefetch buffer address. */ +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + desc_offset =0 ; + desc_avail = vq->buf_vec[vec_idx].buf_len; } - } -return entry_success; -} +/* done with current mbuf, get the next one */ +if (mbuf_avail ==0 ) { + m = m->next; -static inline void __attribute__((always_inline)) -update_secure_len (struct vhost_virtqueue *vq,uint32_t id, -uint32_t *secure_len,uint32_t *vec_idx) -{ -uint16_t wrapped_idx = id & (vq->size -1 ); -uint32_t idx = vq->avail->ring[wrapped_idx]; -uint8_t next_desc; -uint32_t len = *secure_len; -uint32_t vec_id = *vec_idx; + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (m); + } -do { - next_desc =0 ; - len += vq->desc[idx].len; - vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - vq->buf_vec[vec_id].buf_len = vq->desc[idx].len; - vq->buf_vec[vec_id].desc_idx = idx; - vec_id++; + cpy_len =RTE_MIN (desc_avail, mbuf_avail); +rte_memcpy ((void *)((uintptr_t )(desc_addr + desc_offset)), + rte_pktmbuf_mtod_offset(m, void *, mbuf_offset), + cpy_len); +vhost_log_write (dev, vq->buf_vec[vec_idx].buf_addr + desc_offset, + cpy_len); +PRINT_PACKET (dev, (uintptr_t )(desc_addr + desc_offset), + cpy_len,0 ); -if (vq->desc[idx].flags &VRING_DESC_F_NEXT ) { - idx = vq->desc[idx].next; - next_desc =1 ; - } - }while (next_desc); + mbuf_avail -= cpy_len; + mbuf_offset += cpy_len; + desc_avail -= cpy_len; + desc_offset += cpy_len; + } + + used_idx = cur_idx & (vq->size -1 ); + vq->used->ring[used_idx].id = vq->buf_vec[vec_idx].desc_idx; + vq->used->ring[used_idx].len = desc_offset; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used, ring[used_idx]), + sizeof(vq->used->ring[used_idx])); - *secure_len = len; - *vec_idx = vec_id; +return res_end_idx - res_start_idx; } -/* -* This function works for mergeable RX. -*/ static inline uint32_t __attribute__((always_inline))virtio_dev_merge_rx (struct virtio_net *dev,uint16_t queue_id,struct rte_mbuf **pkts,uint32_t count) {struct vhost_virtqueue *vq; -uint32_t pkt_idx =0 , entry_success =0 ; -uint16_t avail_idx; -uint16_t res_base_idx, res_cur_idx; -uint8_t success =0 ; +uint32_t pkt_idx =0 , nr_used =0 ; +uint16_t start, end;LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") virtio_dev_merge_rx()\n" , dev->device_fh);
@@ -584,57 +537,32 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,return 0 ; count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); -if (count ==0 )return 0 ;for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) {uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen; -do { -/* -* As many data cores may want access to available -* buffers, they need to be reserved. -*/ -uint32_t secure_len =0 ; -uint32_t vec_idx =0 ; - - res_base_idx = vq->last_used_idx_res; - res_cur_idx = res_base_idx; - -do { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (res_cur_idx == avail_idx)) - goto merge_rx_exit; - -update_secure_len (vq, res_cur_idx, - &secure_len, &vec_idx); - res_cur_idx++; - }while (pkt_len > secure_len); - -/* vq->last_used_idx_res is atomically updated. */ - success =rte_atomic16_cmpset (&vq->last_used_idx_res, - res_base_idx, - res_cur_idx); - }while (success ==0 ); - - entry_success =copy_from_mbuf_to_vring (dev, queue_id, - res_base_idx, res_cur_idx, pkts[pkt_idx]); +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]);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; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx)); + vq->last_used_idx = end; } -merge_rx_exit:if (likely (pkt_idx)) {/* flush used->idx update before we read avail->flags. */ rte_mb ();
Signature · 1 line
1.9.0
Re: [PATCH v3 3/8] vhost: refactor virtio_dev_merge_rx
This patch does not compile:lib/librte_vhost/vhost_rxtx.c:386:5: error: ‘dev’ undeclared
Re: [PATCH v3 3/8] vhost: refactor virtio_dev_merge_rx
Quoted 2 lines · Thomas Monjalon
This patch does not compile:
lib/librte_vhost/vhost_rxtx.c:386:5: error: ‘dev’ undeclared
Oops, I acutally did the basic compile test on every commit and every time I did a rebase. I had a quick look, it turned out that it's a build bug when RTE_LIBRTE_VHOST_DEBUG is enabled. It is disabled by default, and that's why I didn't catch it.
Thanks for the report, I will fix it soon.
--yliu
PATCH v4 3/8 vhost: refactor virtio_dev_merge_rx
In reply to Thomas MonjalonCurrent 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 difference a bit bigger then.
The refactored code looks like below:
while (mbuf_has_not_drained_totally || mbuf_has_next) {
if (this_desc_has_no_room) {
this_desc = fetch_next_from_vec_buf();
if (it is the last of a desc chain)
update_used_ring();
}
if (this_mbuf_has_drained_totally)
mbuf = fetch_next_mbuf();
COPY(this_desc, this_mbuf);
}This patch reduces quite many lines of code, therefore, make it much more readable.
---
v4: fix build error when DEBUG is enabled
--- lib/librte_vhost/vhost_rxtx.c | 406 +++++++++++++++++------------------------- 1 file changed, 168 insertions(+), 238 deletions(-)
lib/librte_vhost/vhost_rxtx.c +168 -238
@@ -324,251 +324,201 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,return count; } -static inline uint32_t __attribute__((always_inline)) -copy_from_mbuf_to_vring (struct virtio_net *dev,uint32_t queue_id, -uint16_t res_base_idx,uint16_t res_end_idx, -struct rte_mbuf *pkt) +static inline int +fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, +uint32_t *allocated,uint32_t *vec_idx) { -uint32_t vec_idx =0 ; -uint32_t entry_success =0 ; -struct vhost_virtqueue *vq; -/* The virtio_hdr is initialised to 0. */ -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = { - {0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint16_t cur_idx = res_base_idx; -uint64_t vb_addr =0 ; -uint64_t vb_hdr_addr =0 ; -uint32_t seg_offset =0 ; -uint32_t vb_offset =0 ; -uint32_t seg_avail; -uint32_t vb_avail; -uint32_t cpy_len, entry_len; -uint16_t idx; - -if (pkt ==NULL ) -return 0 ; +uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; +uint32_t vec_id = *vec_idx; +uint32_t len = *allocated; -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") Current Index %d| " -"End Index %d\n" , - dev->device_fh, cur_idx, res_end_idx); +while (1 ) { +if (vec_id >=BUF_VECTOR_MAX ) +return -1 ; -/* -* Convert from gpa to vva -* (guest physical addr -> vhost virtual addr) -*/ - vq = dev->virtqueue[queue_id]; + len += vq->desc[idx].len; + vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr; + vq->buf_vec[vec_id].buf_len = vq->desc[idx].len; + vq->buf_vec[vec_id].desc_idx = idx; + vec_id++; - vb_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); - vb_hdr_addr = vb_addr; +if ((vq->desc[idx].flags &VRING_DESC_F_NEXT ) ==0 ) +break ; -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); + idx = vq->desc[idx].next; + } - virtio_hdr.num_buffers = res_end_idx - res_base_idx; + *allocated = len; + *vec_idx = vec_id; -LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") RX: Num merge buffers %d\n" , - dev->device_fh, virtio_hdr.num_buffers); +return 0 ; +} -virtio_enqueue_offload (pkt, &virtio_hdr.hdr); +/* +* As many data cores may want to access available buffers concurrently, +* they need to be reserved. +* +* Returns -1 on fail, 0 on success +*/ +static inline int +reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, +uint16_t *start,uint16_t *end) +{ +uint16_t res_start_idx; +uint16_t res_cur_idx; +uint16_t avail_idx; +uint32_t allocated; +uint32_t vec_idx; +uint16_t tries; -rte_memcpy ((void *)(uintptr_t )vb_hdr_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); -vhost_log_write (dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen); +again: + res_start_idx = vq->last_used_idx_res; + res_cur_idx = res_start_idx; -PRINT_PACKET (dev, (uintptr_t )vb_hdr_addr, vq->vhost_hlen,1 ); + allocated =0 ; + vec_idx =0 ; + tries =0 ; +while (1 ) { + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +if (unlikely (res_cur_idx == avail_idx)) +return -1 ; - seg_avail =rte_pktmbuf_data_len (pkt); - vb_offset = vq->vhost_hlen; - vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen; +if (unlikely (fill_vec_buf (vq, res_cur_idx, &allocated, + &vec_idx) <0 )) +return -1 ; - entry_len = vq->vhost_hlen; + res_cur_idx++; + tries++; -if (vb_avail ==0 ) { -uint32_t desc_idx = vq->buf_vec[vec_idx].desc_idx; +if (allocated >= size) +break ; -if ((vq->desc[desc_idx].flags &VRING_DESC_F_NEXT ) ==0 ) { - idx = cur_idx & (vq->size -1 ); +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (tries >= vq->size)) +return -1 ; + } -/* Update used ring with desc information */ - vq->used->ring[idx].id = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[idx].len = entry_len; +/* +* update vq->last_used_idx_res atomically. +* retry again if failed. +*/ +if (rte_atomic16_cmpset (&vq->last_used_idx_res, + res_start_idx, res_cur_idx) ==0 ) + goto again; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[idx]), - sizeof(vq->used->ring[idx])); + *start = res_start_idx; + *end = res_cur_idx; +return 0 ; +} - entry_len =0 ; - cur_idx++; - entry_success++; - } +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; - vec_idx++; - vb_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); +if (unlikely (m ==NULL )) +return 0 ; -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )vb_addr); - vb_offset =0 ; - vb_avail = vq->buf_vec[vec_idx].buf_len; - } +LOG_DEBUG (VHOST_DATA , +"(%" PRIu64") Current Index %d| End Index %d\n" , + dev->device_fh, cur_idx, res_end_idx); - cpy_len =RTE_MIN (vb_avail, seg_avail); + desc_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); +rte_prefetch0 ((void *)(uintptr_t )desc_addr); -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); -vhost_log_write (dev, vq->buf_vec[vec_idx].buf_addr + vb_offset, - cpy_len); + 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); -PRINT_PACKET (dev, - (uintptr_t )(vb_addr + vb_offset), - cpy_len,0 ); +virtio_enqueue_offload (m, &virtio_hdr.hdr); +rte_memcpy ((void *)(uintptr_t )desc_addr, + (const void *)&virtio_hdr, vq->vhost_hlen); +vhost_log_write (dev, vq->buf_vec[vec_idx].buf_addr, 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; - 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 ) { + mbuf_avail =rte_pktmbuf_data_len (m); + mbuf_offset =0 ; +while (mbuf_avail !=0 || m->next !=NULL ) { +/* done with current desc buf, get the next one */ +if (desc_avail ==0 ) { + desc_idx = vq->buf_vec[vec_idx].desc_idx; + +if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) {/* Update used ring with desc information */ - idx = cur_idx & (vq->size -1 ); - vq->used->ring[idx].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[idx].len = entry_len; + used_idx = cur_idx++ & (vq->size -1 ); + vq->used->ring[used_idx].id = desc_idx; + vq->used->ring[used_idx].len = desc_offset;vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[idx]), - sizeof(vq->used->ring[idx])); - entry_len =0 ; - cur_idx++; - entry_success++; + offsetof(struct vring_used , + ring[used_idx]), + sizeof(vq->used->ring[used_idx])); } vec_idx++; - vb_addr =gpa_to_vva (dev, - vq->buf_vec[vec_idx].buf_addr); - vb_offset =0 ; - vb_avail = vq->buf_vec[vec_idx].buf_len; - cpy_len =RTE_MIN (vb_avail, seg_avail); - }else { -/* -* This current segment complete, need continue to -* check if the whole packet complete or not. -*/ - pkt = pkt->next; -if (pkt !=NULL ) { -/* -* There are more segments. -*/ -if (vb_avail ==0 ) { -/* -* This current buffer from vring is -* used up, need fetch next buffer -* from buf_vec. -*/ -uint32_t desc_idx = - vq->buf_vec[vec_idx].desc_idx; - -if ((vq->desc[desc_idx].flags & -VRING_DESC_F_NEXT ) ==0 ) { - idx = cur_idx & (vq->size -1 ); -/* -* Update used ring with the -* descriptor information -*/ - vq->used->ring[idx].id - = desc_idx; - vq->used->ring[idx].len - = entry_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[idx]), - sizeof(vq->used->ring[idx])); - entry_success++; - entry_len =0 ; - cur_idx++; - } - -/* Get next buffer from buf_vec. */ - vec_idx++; - vb_addr =gpa_to_vva (dev, - vq->buf_vec[vec_idx].buf_addr); - vb_avail = - vq->buf_vec[vec_idx].buf_len; - vb_offset =0 ; - } - - seg_offset =0 ; - seg_avail =rte_pktmbuf_data_len (pkt); - cpy_len =RTE_MIN (vb_avail, seg_avail); - }else { -/* -* This whole packet completes. -*/ -/* Update used ring with desc information */ - idx = cur_idx & (vq->size -1 ); - vq->used->ring[idx].id - = vq->buf_vec[vec_idx].desc_idx; - vq->used->ring[idx].len = entry_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[idx]), - sizeof(vq->used->ring[idx])); - entry_success++; -break ; - } + desc_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr); + +/* Prefetch buffer address. */ +rte_prefetch0 ((void *)(uintptr_t )desc_addr); + desc_offset =0 ; + desc_avail = vq->buf_vec[vec_idx].buf_len; } - } -return entry_success; -} +/* done with current mbuf, get the next one */ +if (mbuf_avail ==0 ) { + m = m->next; -static inline void __attribute__((always_inline)) -update_secure_len (struct vhost_virtqueue *vq,uint32_t id, -uint32_t *secure_len,uint32_t *vec_idx) -{ -uint16_t wrapped_idx = id & (vq->size -1 ); -uint32_t idx = vq->avail->ring[wrapped_idx]; -uint8_t next_desc; -uint32_t len = *secure_len; -uint32_t vec_id = *vec_idx; + mbuf_offset =0 ; + mbuf_avail =rte_pktmbuf_data_len (m); + } -do { - next_desc =0 ; - len += vq->desc[idx].len; - vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - vq->buf_vec[vec_id].buf_len = vq->desc[idx].len; - vq->buf_vec[vec_id].desc_idx = idx; - vec_id++; + cpy_len =RTE_MIN (desc_avail, mbuf_avail); +rte_memcpy ((void *)((uintptr_t )(desc_addr + desc_offset)), + rte_pktmbuf_mtod_offset(m, void *, mbuf_offset), + cpy_len); +vhost_log_write (dev, vq->buf_vec[vec_idx].buf_addr + desc_offset, + cpy_len); +PRINT_PACKET (dev, (uintptr_t )(desc_addr + desc_offset), + cpy_len,0 ); -if (vq->desc[idx].flags &VRING_DESC_F_NEXT ) { - idx = vq->desc[idx].next; - next_desc =1 ; - } - }while (next_desc); + mbuf_avail -= cpy_len; + mbuf_offset += cpy_len; + desc_avail -= cpy_len; + desc_offset += cpy_len; + } - *secure_len = len; - *vec_idx = vec_id; + used_idx = cur_idx & (vq->size -1 ); + vq->used->ring[used_idx].id = vq->buf_vec[vec_idx].desc_idx; + vq->used->ring[used_idx].len = desc_offset; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used, ring[used_idx]), + sizeof(vq->used->ring[used_idx])); + +return res_end_idx - res_start_idx; } -/* -* This function works for mergeable RX. -*/ static inline uint32_t __attribute__((always_inline))virtio_dev_merge_rx (struct virtio_net *dev,uint16_t queue_id,struct rte_mbuf **pkts,uint32_t count) {struct vhost_virtqueue *vq; -uint32_t pkt_idx =0 , entry_success =0 ; -uint16_t avail_idx; -uint16_t res_base_idx, res_cur_idx; -uint8_t success =0 ; +uint32_t pkt_idx =0 , nr_used =0 ; +uint16_t start, end;LOG_DEBUG (VHOST_DATA ,"(%" PRIu64") virtio_dev_merge_rx()\n" , dev->device_fh);
@@ -584,57 +534,37 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,return 0 ; count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); -if (count ==0 )return 0 ;for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) {uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen; -do { -/* -* As many data cores may want access to available -* buffers, they need to be reserved. -*/ -uint32_t secure_len =0 ; -uint32_t vec_idx =0 ; - - res_base_idx = vq->last_used_idx_res; - res_cur_idx = res_base_idx; - -do { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (res_cur_idx == avail_idx)) - goto merge_rx_exit; - -update_secure_len (vq, res_cur_idx, - &secure_len, &vec_idx); - res_cur_idx++; - }while (pkt_len > secure_len); - -/* vq->last_used_idx_res is atomically updated. */ - success =rte_atomic16_cmpset (&vq->last_used_idx_res, - res_base_idx, - res_cur_idx); - }while (success ==0 ); - - entry_success =copy_from_mbuf_to_vring (dev, queue_id, - res_base_idx, res_cur_idx, pkts[pkt_idx]); +if (unlikely (reserve_avail_buf_mergeable (vq, pkt_len, + &start, &end) <0 )) { +LOG_DEBUG (VHOST_DATA , +"(%" PRIu64") Failed to get enough desc from vring\n" , + dev->device_fh); +break ; + } + nr_used =copy_mbuf_to_desc_mergeable (dev, vq, start, end, + pkts[pkt_idx]);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; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx)); + vq->last_used_idx = end; } -merge_rx_exit:if (likely (pkt_idx)) {/* flush used->idx update before we read avail->flags. */ rte_mb ();
Signature · 1 line
1.9.0
PATCH v3 4/8 vhost: do not use rte_memcpy for virtio_hdr copy
In reply to Yuanhan LiuFirst of all, rte_memcpy() is mostly useful for coping big packets by leveraging hardware advanced instructions like AVX. But for virtio net hdr, which is 12 bytes at most, invoking rte_memcpy() will not introduce any performance boost.
And, to my suprise, rte_memcpy() is VERY huge. Since rte_memcpy() is inlined, it increases the binary code size linearly every time we call it at a different place. Replacing the two rte_memcpy() with directly copy saves nearly 12K bytes of code size!
--- lib/librte_vhost/vhost_rxtx.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
lib/librte_vhost/vhost_rxtx.c +12 -4
@@ -129,6 +129,16 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)return ; } +static inline void +copy_virtio_net_hdr (struct vhost_virtqueue *vq,uint64_t desc_addr, +struct virtio_net_hdr_mrg_rxbuf hdr) +{ +if (vq->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) + *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr = hdr; +else + *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; +} +static inline int __attribute__((always_inline))copy_mbuf_to_desc (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf *m,uint16_t desc_idx,uint32_t *copied)
@@ -145,8 +155,7 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,rte_prefetch0 ((void *)(uintptr_t )desc_addr);virtio_enqueue_offload (m, &virtio_hdr.hdr); -rte_memcpy ((void *)(uintptr_t )desc_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); +copy_virtio_net_hdr (vq, desc_addr, virtio_hdr);vhost_log_write (dev, desc->addr, vq->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, vq->vhost_hlen,0 );
@@ -447,8 +456,7 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, dev->device_fh, virtio_hdr.num_buffers);virtio_enqueue_offload (m, &virtio_hdr.hdr); -rte_memcpy ((void *)(uintptr_t )desc_addr, - (const void *)&virtio_hdr, vq->vhost_hlen); +copy_virtio_net_hdr (vq, desc_addr, virtio_hdr);vhost_log_write (dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, vq->vhost_hlen,0 );
Signature · 1 line
1.9.0
PATCH v3 5/8 vhost: don't use unlikely for VIRTIO_NET_F_MRG_RXBUF detection
In reply to Yuanhan LiuVIRTIO_NET_F_MRG_RXBUF is a default feature supported by vhost. Adding unlikely for VIRTIO_NET_F_MRG_RXBUF detection doesn't make sense to me at all.
--- lib/librte_vhost/vhost_rxtx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost_rxtx.c +1 -1
@@ -587,7 +587,7 @@ uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id, struct rte_mbuf **pkts, uint16_t count) { - if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))) + if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) return virtio_dev_merge_rx(dev, queue_id, pkts, count); else return virtio_dev_rx(dev, queue_id, pkts, count);
Signature · 1 line
1.9.0
PATCH v3 6/8 vhost: do sanity check for desc->len
In reply to Yuanhan LiuWe need make sure that desc->len is bigger than the size of virtio net header, otherwise, unexpected behaviour might happen due to "desc_avail" would become a huge number with for following code:
desc_avail = desc->len - vq->vhost_hlen;
For dequeue code path, it will try to allocate enough mbuf to hold such size of desc buf, which ends up with consuming all mbufs, leading to no free mbuf is available. Therefore, you might see an error message:
Failed to allocate memory for mbuf.
Also, for both dequeue/enqueue code path, while it copies data from/to desc buf, the big "desc_avail" would result to access memory not belong the desc buf, which could lead to some potential memory access errors.
A malicious guest could easily forge such malformed vring desc buf. Every time we restart an interrupted DPDK application inside guest would also trigger this issue, as all huge pages are reset to 0 during DPDK re-init, leading to desc->len being 0.
Therefore, this patch does a sanity check for desc->len, to make vhost robust.
--- lib/librte_vhost/vhost_rxtx.c | 9 +++++++++ 1 file changed, 9 insertions(+)
lib/librte_vhost/vhost_rxtx.c +9 -0
@@ -151,6 +151,9 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; desc = &vq->desc[desc_idx]; +if (unlikely (desc->len < vq->vhost_hlen)) +return -1 ; + desc_addr =gpa_to_vva (dev, desc->addr);rte_prefetch0 ((void *)(uintptr_t )desc_addr);
@@ -448,6 +451,9 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,"(%" PRIu64") Current Index %d| End Index %d\n" , dev->device_fh, cur_idx, res_end_idx); +if (vq->buf_vec[vec_idx].buf_len < vq->vhost_hlen) +return -1 ; + desc_addr =gpa_to_vva (dev, vq->buf_vec[vec_idx].buf_addr);rte_prefetch0 ((void *)(uintptr_t )desc_addr);
@@ -737,6 +743,9 @@ copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,struct virtio_net_hdr *hdr; desc = &vq->desc[desc_idx]; +if (unlikely (desc->len < vq->vhost_hlen)) +return -1 ; + desc_addr =gpa_to_vva (dev, desc->addr);rte_prefetch0 ((void *)(uintptr_t )desc_addr);
Signature · 1 line
1.9.0
PATCH v3 7/8 vhost: do sanity check for desc->next against with vq->size
In reply to Yuanhan LiuA malicious guest may easily forge some illegal vring desc buf. To make our vhost robust, we need make sure desc->next will not go beyond the vq->desc[] array.
--- lib/librte_vhost/vhost_rxtx.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
lib/librte_vhost/vhost_rxtx.c +5 -1
@@ -183,6 +183,8 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,/* Room in vring buffer is not enough */ return -1 ; } +if (unlikely (desc->next >= vq->size)) +return -1 ; desc = &vq->desc[desc->next]; desc_addr =gpa_to_vva (dev, desc->addr);
@@ -345,7 +347,7 @@ fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,uint32_t len = *allocated;while (1 ) { -if (vec_id >=BUF_VECTOR_MAX ) +if (unlikely (vec_id >=BUF_VECTOR_MAX || idx >= vq->size))return -1 ; len += vq->desc[idx].len;
@@ -759,6 +761,8 @@ copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,while (desc_avail !=0 || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) {/* This desc reaches to its end, get the next one */ if (desc_avail ==0 ) { +if (unlikely (desc->next >= vq->size)) +return -1 ; desc = &vq->desc[desc->next]; desc_addr =gpa_to_vva (dev, desc->addr);
Signature · 1 line
1.9.0
PATCH v3 8/8 vhost: avoid dead loop chain.
In reply to Yuanhan LiuIf a malicious guest forges a dead loop chain, it could lead to a dead loop of copying the desc buf to mbuf, which results to all mbuf being exhausted.
Add a var nr_desc to avoid such case.
--- lib/librte_vhost/vhost_rxtx.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
lib/librte_vhost/vhost_rxtx.c +4 -1
@@ -743,6 +743,8 @@ copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t cpy_len;struct rte_mbuf *cur = m, *prev = m;struct virtio_net_hdr *hdr; +/* A counter to avoid desc dead loop chain */ +uint32_t nr_desc =1 ; desc = &vq->desc[desc_idx];if (unlikely (desc->len < vq->vhost_hlen))
@@ -761,7 +763,8 @@ copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,while (desc_avail !=0 || (desc->flags &VRING_DESC_F_NEXT ) !=0 ) {/* This desc reaches to its end, get the next one */ if (desc_avail ==0 ) { -if (unlikely (desc->next >= vq->size)) +if (unlikely (desc->next >= vq->size || + ++nr_desc >= vq->size))return -1 ; desc = &vq->desc[desc->next];
Signature · 1 line
1.9.0
Re: [PATCH v3 0/8] vhost rxtx refactor and fixes
In reply to Yuanhan Liu2016-03-10 12:32, Yuanhan Liu:
Quoted 19 lines
v3: - quite few minor changes, including using likely/unlikely
when possible.- Added a new patch 8 to avoid desc dead loop chain
The first 3 patches refactor 3 major functions at
vhost_rxtx.c. It simplifies the code logic, making it more readable. OTOH, it reduces binary code size, due to a lot of duplicate code are removed, as well as some huge inline functions are diminished.Patch 4 gets rid of the
rte_memcpyforvirtio_hdrcopy, which nearly saves 12K bytes of binary code size!Patch 5 removes "unlikely" for
VIRTIO_NET_F_MRG_RXBUFdetection.Patch 6, 7 and 8 do some sanity check for two desc fields, to make vhost robust and be protected from malicious guest or abnormal use cases.
Quoted 10 lines
---
Yuanhan Liu (8):
vhost: refactorrte_vhost_dequeue_burst
vhost: refactorvirtio_dev_rx
vhost: refactorvirtio_dev_merge_rx
vhost: do not userte_memcpyforvirtio_hdrcopy
vhost: don't use unlikely forVIRTIO_NET_F_MRG_RXBUFdetection
vhost: do sanity check fordesc->len
vhost: do sanity check fordesc->nextagainst withvq->size
vhost: avoid dead loop chain.
Applied with 3/8 v4, thanks.