optimize vhost enqueue
PATCH 1/1 optimize vhost enqueue
This patch optimizes the vhost enqueue function: rte_vhost_enqueue_burst.
Currently there're 2 callbacks for vhost enqueue:
* virtio_dev_merge_rx for mrg_rxbuf turned on cases.
* virtio_dev_rx for mrg_rxbuf turned off cases.
The virtio_dev_merge_rx doesn't provide optimal performance, also it is reported having compatibility issue working with Windows VMs.
Besides, having 2 separated functions increases maintenance efforts.
This patch uses a single function logic to replace the current 2 for better maintainability, and provides better performance by optimizing caching behavior especially for mrg_rxbuf turned on cases.
It also fixes the issue working with Windows VMs.
--- lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 582 ++++++++++++++---------------------------- lib/librte_vhost/virtio-net.c | 15 +- 3 files changed, 208 insertions(+), 395 deletions(-)
lib/librte_vhost/vhost-net.h +5 -1
@@ -71,7 +71,7 @@ struct vhost_virtqueue {uint32_t size;/* Last index used on the available ring */ -volatile uint16_t last_used_idx; +uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
@@ -85,6 +85,10 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +/* Shadow used ring for performance */ +struct vring_used_elem *shadow_used_ring; +uint32_t shadow_used_idx; } __rte_cache_aligned;/* Old kernels have no such macro defined */
lib/librte_vhost/vhost_rxtx.c +191 -391
625 lines not on this page · raw message
lib/librte_vhost/virtio-net.c +12 -3
@@ -152,10 +152,14 @@ cleanup_device(struct virtio_net *dev, int destroy)static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq = dev->virtqueue[i *VIRTIO_QNUM ]; +rte_free (vq->shadow_used_ring); +rte_free (vq); + }rte_free (dev); }
@@ -418,13 +422,18 @@ int vhost_set_vring_num(int vid, struct vhost_vring_state *state) { struct virtio_net *dev; +struct vhost_virtqueue *vq; dev =get_device (vid);if (dev ==NULL )return -1 ;/* State->index refers to the queue index. The txq is 1, rxq is 0. */ - dev->virtqueue[state->index]->size = state->num; + vq = dev->virtqueue[state->index]; + vq->size = state->num; + vq->shadow_used_ring =rte_malloc ("" , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE );return 0 ; }
Signature · 1 line
2.7.4
Hi Zhihong,
Quoted 8 lines · Zhihong Wang
This patch optimizes the vhost enqueue function:
rte_vhost_enqueue_burst.Currently there're 2 callbacks for vhost enqueue:
*virtio_dev_merge_rxformrg_rxbufturned on cases.
*virtio_dev_rxformrg_rxbufturned off cases.The
virtio_dev_merge_rxdoesn't provide optimal performance, also it is reported having compatibility issue working with Windows VMs.
Could you tell us more please about this compatibility issue?
Quoted 6 lines
Besides, having 2 separated functions increases maintenance efforts.
This patch uses a single function logic to replace the current 2 for better maintainability, and provides better performance by optimizing caching behavior especially for
mrg_rxbufturned on cases.
Do you have some benchmark comparison before and after your change?
Also, for maintainability, I would suggest the that the enqueue function be split. Because vhost_enqueue_burst becomes very long (220 LoC), and max level of indentation is too high (6).
It makes the code hard to understand, and prone to miss bugs during review and maintenance.
Quoted 2 lines
It also fixes the issue working with Windows VMs.
Ideally, the fix should be sent separately, before the rework. Indeed, we might want to have the fix in the stable branch, without picking the optimization.
Quoted 7 lines
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 582 ++++++++++++++---------------------------- lib/librte_vhost/virtio-net.c | 15 +- 3 files changed, 208 insertions(+), 395 deletions(-)
582 lines changed is a huge patch.
If possible, it would be better splitting it in incremental changes, making the review process easier.
Also, for v2, please prefix the commit title with "vhost:".
Thanks for your contribution, I'm looking forward for the v2.
- Maxime
Quoted 13 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Tuesday, August 16, 2016 10:00 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueHi Zhihong,
Quoted 2 lines · Zhihong Wang
This patch optimizes the vhost enqueue function:
rte_vhost_enqueue_burst.
Quoted 7 lines
Quoted 6 lines
Currently there're 2 callbacks for vhost enqueue:
*virtio_dev_merge_rxformrg_rxbufturned on cases.
*virtio_dev_rxformrg_rxbufturned off cases.The
virtio_dev_merge_rxdoesn't provide optimal performance, also it is reported having compatibility issue working with Windows VMs.Could you tell us more please about this compatibility issue?
For example, when you have testpmd in the host and Window VM as the guest, with mrg_rxbuf turned on, the guest will hang once there's packets enqueued by virtio_dev_merge_rx.
Let me know if you see the same issue.
Quoted 7 lines
Quoted 7 lines
Besides, having 2 separated functions increases maintenance efforts.
This patch uses a single function logic to replace the current 2 for better maintainability, and provides better performance by optimizing caching behavior especially for
mrg_rxbufturned on cases.
Quoted 8 lines
Do you have some benchmark comparison before and after your change?
Also, for maintainability, I would suggest the that the enqueue function be split. Because
vhost_enqueue_burstbecomes very long (220 LoC), and max level of indentation is too high (6).It makes the code hard to understand, and prone to miss bugs during review and maintenance.
This is something I've thought about while writing the code, the reason I keep it as one function body is that:
1. This function is very performance sensitive, and we need full control of code ordering (You can compare with the current performance with the mrg_rxbuf feature turned on to see the difference).
2. I somehow find that a single function logic makes it easier to understand, surely I can add comments to make it easiler to read for .
Please let me know if you still insist, we can discuss more on it.
Quoted 14 lines
Quoted 3 lines
It also fixes the issue working with Windows VMs.
Ideally, the fix should be sent separately, before the rework. Indeed, we might want to have the fix in the stable branch, without picking the optimization.
Quoted 7 lines
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 582 ++++++++++++++---------------------------- lib/librte_vhost/virtio-net.c | 15 +- 3 files changed, 208 insertions(+), 395 deletions(-)
Quoted 3 lines
582 lines changed is a huge patch.
If possible, it would be better splitting it in incremental changes, making the review process easier.
It looks like a huge patch, but it simply deletes the current implementation and add the new code. I think perhaps split it into 2, 1st one to replace just the rte_vhost_enqueue_burst, 2nd one to delete all the obsolete functions. It should make the patch clear, how do you think? :)
Quoted 2 lines
Also, for v2, please prefix the commit title with "vhost:".
Thanks for the hint! Will do.
Quoted 3 lines
Thanks for your contribution, I'm looking forward for the v2.
- Maxime
Quoted 17 lines · Zhihong
Quoted 17 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Tuesday, August 16, 2016 10:00 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueHi Zhihong,
Quoted 6 lines · Zhihong Wang
This patch optimizes the vhost enqueue function:
rte_vhost_enqueue_burst.Currently there're 2 callbacks for vhost enqueue:
*virtio_dev_merge_rxformrg_rxbufturned on cases.
*virtio_dev_rxformrg_rxbufturned off cases.
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines
The
virtio_dev_merge_rxdoesn't provide optimal performance, also it is reported having compatibility issue working with Windows VMs.Could you tell us more please about this compatibility issue?
For example, when you have testpmd in the host and Window VM as the guest, with
mrg_rxbufturned on, the guest will hang once there's packets enqueued byvirtio_dev_merge_rx.
You should put it into commit log.
Quoted 10 lines
Let me know if you see the same issue.
Quoted 7 lines
Quoted 7 lines
Besides, having 2 separated functions increases maintenance efforts.
This patch uses a single function logic to replace the current 2 for better maintainability, and provides better performance by optimizing caching behavior especially for
mrg_rxbufturned on cases.
Here, here sounds two parts to me:
- one to unite mergeable and non-mergeable Rx
- another one to optimize the mergeable path
That means you should do it in two patches, with that we can have clear understanding what changes the performance boost. It also helps review.
Quoted 8 lines
Quoted 8 lines
Do you have some benchmark comparison before and after your change?
Also, for maintainability, I would suggest the that the enqueue function be split. Because
vhost_enqueue_burstbecomes very long (220 LoC), and max level of indentation is too high (6).It makes the code hard to understand, and prone to miss bugs during review and maintenance.
Agreed.
Quoted 7 lines
This is something I've thought about while writing the code, the reason I keep it as one function body is that:
1. This function is very performance sensitive, and we need full control of code ordering (You can compare with the current performance with the
mrg_rxbuffeature turned on to see the difference).
Will inline functions help?
Quoted 4 lines
2. I somehow find that a single function logic makes it easier to understand, surely I can add comments to make it easiler to read for .
Please let me know if you still insist, we can discuss more on it.
I am personally not a fan of huge function; I would try hard to avoid too many levels of indentation as well.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
It also fixes the issue working with Windows VMs.
Ideally, the fix should be sent separately, before the rework. Indeed, we might want to have the fix in the stable branch, without picking the optimization.
Agreed.
Quoted 13 lines
Quoted 11 lines
Quoted 8 lines
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 582 ++++++++++++++---------------------------- lib/librte_vhost/virtio-net.c | 15 +- 3 files changed, 208 insertions(+), 395 deletions(-)582 lines changed is a huge patch.
If possible, it would be better splitting it in incremental changes, making the review process easier.
Quoted 4 lines
It looks like a huge patch, but it simply deletes the current implementation and add the new code. I think perhaps split it into 2, 1st one to replace just the
rte_vhost_enqueue_burst, 2nd one to delete all the obsolete functions. It should make the patch clear, how do you think? :)
Nope, it's not working in that way. It should be:
- one patch to fix the hang issue for windows guest
Please cc it to [email protected] as well so that we could pick it for v16.07 stable release.
- one patch to unite the two different Rx code path
- another patch to optimize mergeable code path
--yliu
Quoted 33 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Wednesday, August 17, 2016 10:38 AM
To: Wang, Zhihong <[email protected]>
Cc: Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueQuoted 8 lines · Zhihong
Quoted 8 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Tuesday, August 16, 2016 10:00 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueQuoted 4 lines
Quoted 4 lines
Hi Zhihong,
Quoted 1 line · Zhihong Wang
This patch optimizes the vhost enqueue function:
rte_vhost_enqueue_burst.Quoted 10 lines
Quoted 8 lines
Quoted 7 lines
Currently there're 2 callbacks for vhost enqueue:
*virtio_dev_merge_rxformrg_rxbufturned on cases.
*virtio_dev_rxformrg_rxbufturned off cases.The
virtio_dev_merge_rxdoesn't provide optimal performance, also it is reported having compatibility issue working with Windows VMs.Could you tell us more please about this compatibility issue?
Quoted 6 lines
Quoted 1 line
For example, when you have testpmd in the host and Window VM as the
guest,
Quoted 2 lines
with
mrg_rxbufturned on, the guest will hang once there's packets enqueued byvirtio_dev_merge_rx.You should put it into commit log.
Okay.
Quoted 12 lines
Quoted 11 lines
Let me know if you see the same issue.
Quoted 7 lines
Quoted 7 lines
Besides, having 2 separated functions increases maintenance efforts.
This patch uses a single function logic to replace the current 2 for better maintainability, and provides better performance by optimizing caching behavior especially for
mrg_rxbufturned on cases.
Quoted 8 lines
Here, here sounds two parts to me:
- one to unite mergeable and non-mergeable Rx
- another one to optimize the mergeable path
That means you should do it in two patches, with that we can have clear understanding what changes the performance boost. It also helps review.
Please see explanation below.
Quoted 13 lines
Quoted 9 lines
Quoted 9 lines
Do you have some benchmark comparison before and after your change?
Also, for maintainability, I would suggest the that the enqueue function be split. Because
vhost_enqueue_burstbecomes very long (220 LoC), and max level of indentation is too high (6).It makes the code hard to understand, and prone to miss bugs during review and maintenance.
Agreed.
Quoted 1 line
Quoted 8 lines
Quoted 6 lines
This is something I've thought about while writing the code, the reason I keep it as one function body is that:
1. This function is very performance sensitive, and we need full control of code ordering (You can compare with the current performance with the
mrg_rxbuffeature turned on to see the difference).Will inline functions help?
Optimization in this patch actually reorganizes the code from its logic, so it's not suitable for making separated functions.
I'll explain this in v2.
Quoted 12 lines
Quoted 5 lines
2. I somehow find that a single function logic makes it easier to understand, surely I can add comments to make it easiler to read for .
Please let me know if you still insist, we can discuss more on it.
I am personally not a fan of huge function; I would try hard to avoid too many levels of indentation as well.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
It also fixes the issue working with Windows VMs.
Ideally, the fix should be sent separately, before the rework. Indeed, we might want to have the fix in the stable branch, without picking the optimization.
Agreed.
The fact is that I don't have much time to debug with the current code since it's messy and I don't have Windows virtio code and the debugging environment.
This patch doesn't try to fix this issue, it rewrites the logic totally, and somehow fixes this issue.
Do you think integrating this whole patch into the stable branch will work? Personally I think it makes more sense.
Quoted 23 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 582++++++++++++++----------------------------
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
lib/librte_vhost/virtio-net.c | 15 +- 3 files changed, 208 insertions(+), 395 deletions(-)582 lines changed is a huge patch.
If possible, it would be better splitting it in incremental changes, making the review process easier.It looks like a huge patch, but it simply deletes the current implementation and add the new code. I think perhaps split it into 2, 1st one to replace just the
rte_vhost_enqueue_burst, 2nd one to delete all the obsoletefunctions.
Quoted 1 line
It should make the patch clear, how do you think? :)
Nope, it's not working in that way. It should be:
Quoted 8 lines
- one patch to fix the hang issue for windows guest
Please cc it to [email protected] as well so that we could pick it for v16.07 stable release.
- one patch to unite the two different Rx code path
- another patch to optimize mergeable code path
I can separate optimization from the basic code in v2, however as I explained this patch is built from scratch and doesn't take anything from the existing code, so there's no way to transform from the existing code incrementally into the new code.
Quoted 2 lines
--yliu
Quoted 89 lines · Zhihong
Quoted 33 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Wednesday, August 17, 2016 10:38 AM
To: Wang, Zhihong <[email protected]>
Cc: Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueQuoted 8 lines · Zhihong
Quoted 8 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Tuesday, August 16, 2016 10:00 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueQuoted 4 lines
Quoted 4 lines
Hi Zhihong,
Quoted 1 line · Zhihong Wang
This patch optimizes the vhost enqueue function:
rte_vhost_enqueue_burst.Quoted 10 lines
Quoted 8 lines
Quoted 7 lines
Currently there're 2 callbacks for vhost enqueue:
*virtio_dev_merge_rxformrg_rxbufturned on cases.
*virtio_dev_rxformrg_rxbufturned off cases.The
virtio_dev_merge_rxdoesn't provide optimal performance, also it is reported having compatibility issue working with Windows VMs.Could you tell us more please about this compatibility issue?
Quoted 6 lines
Quoted 1 line
For example, when you have testpmd in the host and Window VM as the
guest,
Quoted 2 lines
with
mrg_rxbufturned on, the guest will hang once there's packets enqueued byvirtio_dev_merge_rx.You should put it into commit log.
Okay.
Quoted 12 lines
Quoted 11 lines
Let me know if you see the same issue.
Quoted 7 lines
Quoted 7 lines
Besides, having 2 separated functions increases maintenance efforts.
This patch uses a single function logic to replace the current 2 for better maintainability, and provides better performance by optimizing caching behavior especially for
mrg_rxbufturned on cases.Quoted 8 lines
Here, here sounds two parts to me:
- one to unite mergeable and non-mergeable Rx
- another one to optimize the mergeable path
That means you should do it in two patches, with that we can have clear understanding what changes the performance boost. It also helps review.
Please see explanation below.
Quoted 20 lines
Quoted 9 lines
Quoted 9 lines
Do you have some benchmark comparison before and after your change?
Also, for maintainability, I would suggest the that the enqueue function be split. Because
vhost_enqueue_burstbecomes very long (220 LoC), and max level of indentation is too high (6).It makes the code hard to understand, and prone to miss bugs during review and maintenance.
Agreed.
Quoted 7 lines
This is something I've thought about while writing the code, the reason I keep it as one function body is that:
1. This function is very performance sensitive, and we need full control of code ordering (You can compare with the current performance with the
mrg_rxbuffeature turned on to see the difference).
Quoted 7 lines
Quoted 1 line
Will inline functions help?
Optimization in this patch actually reorganizes the code from its logic, so it's not suitable for making separated functions.
I'll explain this in v2.
I agree with Yuanhan.
Inline functions should not break the optimizations. IMHO, this is mandatory for the patch to be accepted.
Quoted 19 lines
Quoted 19 lines
Quoted 7 lines
2. I somehow find that a single function logic makes it easier to understand, surely I can add comments to make it easiler to read for .
Please let me know if you still insist, we can discuss more on it.
I am personally not a fan of huge function; I would try hard to avoid too many levels of indentation as well.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
It also fixes the issue working with Windows VMs.
Ideally, the fix should be sent separately, before the rework. Indeed, we might want to have the fix in the stable branch, without picking the optimization.
Quoted 6 lines
Quoted 1 line
Agreed.
The fact is that I don't have much time to debug with the current code since it's messy and I don't have Windows virtio code and the debugging environment.
It seems you are not the only one facing the issue:
https://github.com/YanVugenfirer/kvm-guest-drivers-windows/issues/70
So a dedicated fix is really important.
Quoted 5 lines
This patch doesn't try to fix this issue, it rewrites the logic totally, and somehow fixes this issue.
Do you think integrating this whole patch into the stable branch will work? Personally I think it makes more sense.
No.
We don't even know why/how it fixes the Windows issue, which would be the first thing to understand before integrating a fix in stable branch.
And the stable branch is not meant for integrating such big reworks, it is only meant to fix bugs.
The risk of regressions have to be avoided as much as possible.
Quoted 43 lines
Quoted 25 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 582++++++++++++++----------------------------
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
lib/librte_vhost/virtio-net.c | 15 +- 3 files changed, 208 insertions(+), 395 deletions(-)582 lines changed is a huge patch.
If possible, it would be better splitting it in incremental changes, making the review process easier.It looks like a huge patch, but it simply deletes the current implementation and add the new code. I think perhaps split it into 2, 1st one to replace just the
rte_vhost_enqueue_burst, 2nd one to delete all the obsoletefunctions.
Quoted 1 line
It should make the patch clear, how do you think? :)
Nope, it's not working in that way. It should be:
Quoted 8 lines
- one patch to fix the hang issue for windows guest
Please cc it to [email protected] as well so that we could pick it for v16.07 stable release.
- one patch to unite the two different Rx code path
- another patch to optimize mergeable code path
I can separate optimization from the basic code in v2, however as I explained this patch is built from scratch and doesn't take anything from the existing code, so there's no way to transform from the existing code incrementally into the new code.
Quoted 2 lines
--yliu
Quoted 10 lines · Maxime Coquelin
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
This is something I've thought about while writing the code, the reason I keep it as one function body is that:
1. This function is very performance sensitive, and we need full control of code ordering (You can compare with the current performance with the
mrg_rxbuffeature turned on to see the difference).Will inline functions help?
Quoted 8 lines
Quoted 4 lines
Optimization in this patch actually reorganizes the code from its logic, so it's not suitable for making separated functions.
I'll explain this in v2.
I agree with Yuanhan.
Inline functions should not break the optimizations. IMHO, this is mandatory for the patch to be accepted.
Yes.
Quoted 4 lines
It seems you are not the only one facing the issue:
https://github.com/YanVugenfirer/kvm-guest-drivers-windows/issues/70So a dedicated fix is really important.
Yes.
Quoted 10 lines
Quoted 6 lines
This patch doesn't try to fix this issue, it rewrites the logic totally, and somehow fixes this issue.
Do you think integrating this whole patch into the stable branch will work? Personally I think it makes more sense.
No.
We don't even know why/how it fixes the Windows issue, which would be the first thing to understand before integrating a fix in stable branch.
Yes.
Quoted 3 lines
And the stable branch is not meant for integrating such big reworks, it is only meant to fix bugs.
Yes.
Quoted 1 line
The risk of regressions have to be avoided as much as possible.
Yes.
--yliu
Thanks Maxime and Yuanhan for your review and suggestions! Please help review the v2 of this patch.
Quoted 61 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Wednesday, August 17, 2016 5:51 PM
To: Maxime Coquelin <[email protected]>
Cc: Wang, Zhihong <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueQuoted 5 lines · Maxime Coquelin
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
This is something I've thought about while writing the code, the reason I keep it as one function body is that:
1. This function is very performance sensitive, and we need full control of code ordering (You can compare with the current performance with
the
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
mrg_rxbuffeature turned on to see the difference).Will inline functions help?
Quoted 8 lines
Quoted 4 lines
Optimization in this patch actually reorganizes the code from its logic, so it's not suitable for making separated functions.
I'll explain this in v2.
I agree with Yuanhan.
Inline functions should not break the optimizations. IMHO, this is mandatory for the patch to be accepted.Yes.
Quoted 4 lines
It seems you are not the only one facing the issue:
https://github.com/YanVugenfirer/kvm-guest-drivers-windows/issues/70So a dedicated fix is really important.
Yes.
Quoted 10 lines
Quoted 6 lines
This patch doesn't try to fix this issue, it rewrites the logic totally, and somehow fixes this issue.
Do you think integrating this whole patch into the stable branch will work? Personally I think it makes more sense.
No.
We don't even know why/how it fixes the Windows issue, which would be the first thing to understand before integrating a fix in stable branch.Yes.
Quoted 3 lines
And the stable branch is not meant for integrating such big reworks, it is only meant to fix bugs.
Yes.
Quoted 1 line
The risk of regressions have to be avoided as much as possible.
Yes.
--yliu
Quoted 106 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Wednesday, August 17, 2016 5:18 PM
To: Wang, Zhihong <[email protected]>; Yuanhan Liu <[email protected]>
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueQuoted 28 lines · Zhihong
Quoted 28 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Wednesday, August 17, 2016 10:38 AM
To: Wang, Zhihong <[email protected]>
Cc: Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueQuoted 8 lines · Zhihong
Quoted 8 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Tuesday, August 16, 2016 10:00 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH] optimize vhost enqueueQuoted 4 lines
Quoted 4 lines
Hi Zhihong,
Quoted 1 line · Zhihong Wang
This patch optimizes the vhost enqueue function:
rte_vhost_enqueue_burst.Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Currently there're 2 callbacks for vhost enqueue:
*virtio_dev_merge_rxformrg_rxbufturned on cases.
*virtio_dev_rxformrg_rxbufturned off cases.Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
The
virtio_dev_merge_rxdoesn't provide optimal performance, also it is reported having compatibility issue working with Windows VMs.Could you tell us more please about this compatibility issue?
For example, when you have testpmd in the host and Window VM as the
guest,
Quoted 1 line
with
mrg_rxbufturned on, the guest will hang once there's packetsenqueued
Quoted 56 lines
Quoted 3 lines
Quoted 1 line
by
virtio_dev_merge_rx.You should put it into commit log.
Okay.
Quoted 12 lines
Quoted 11 lines
Let me know if you see the same issue.
Quoted 7 lines
Quoted 7 lines
Besides, having 2 separated functions increases maintenance efforts.
This patch uses a single function logic to replace the current 2 for better maintainability, and provides better performance by optimizing caching behavior especially for
mrg_rxbufturned on cases.Quoted 8 lines
Here, here sounds two parts to me:
- one to unite mergeable and non-mergeable Rx
- another one to optimize the mergeable path
That means you should do it in two patches, with that we can have clear understanding what changes the performance boost. It also helps review.
Please see explanation below.
Quoted 21 lines
Quoted 9 lines
Quoted 9 lines
Do you have some benchmark comparison before and after your change?
Also, for maintainability, I would suggest the that the enqueue function be split. Because
vhost_enqueue_burstbecomes very long (220 LoC), and max level of indentation is too high (6).It makes the code hard to understand, and prone to miss bugs during review and maintenance.
Agreed.
Quoted 7 lines
This is something I've thought about while writing the code, the reason I keep it as one function body is that:
1. This function is very performance sensitive, and we need full control of code ordering (You can compare with the current performance with the
mrg_rxbuffeature turned on to see the difference).Will inline functions help?
Quoted 8 lines
Quoted 4 lines
Optimization in this patch actually reorganizes the code from its logic, so it's not suitable for making separated functions.
I'll explain this in v2.
I agree with Yuanhan.
Inline functions should not break the optimizations. IMHO, this is mandatory for the patch to be accepted.
Excellent!
Quoted 23 lines
Quoted 23 lines
Quoted 21 lines
Quoted 8 lines
2. I somehow find that a single function logic makes it easier to understand, surely I can add comments to make it easiler to read for .
Please let me know if you still insist, we can discuss more on it.
I am personally not a fan of huge function; I would try hard to avoid too many levels of indentation as well.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
It also fixes the issue working with Windows VMs.
Ideally, the fix should be sent separately, before the rework. Indeed, we might want to have the fix in the stable branch, without picking the optimization.
Agreed.
Quoted 8 lines
Quoted 3 lines
The fact is that I don't have much time to debug with the current code since it's messy and I don't have Windows virtio code and the debugging environment.
It seems you are not the only one facing the issue:
https://github.com/YanVugenfirer/kvm-guest-drivers-windows/issues/70So a dedicated fix is really important.
Yeah that's me raising this issue there.
But I think it's another standalone task to identify the root cause and find the fix for the existing code.
Quoted 59 lines
Quoted 6 lines
This patch doesn't try to fix this issue, it rewrites the logic totally, and somehow fixes this issue.
Do you think integrating this whole patch into the stable branch will work? Personally I think it makes more sense.
No.
We don't even know why/how it fixes the Windows issue, which would be the first thing to understand before integrating a fix in stable branch.And the stable branch is not meant for integrating such big reworks, it is only meant to fix bugs.
The risk of regressions have to be avoided as much as possible.
Quoted 43 lines
Quoted 25 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 582++++++++++++++----------------------------
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
lib/librte_vhost/virtio-net.c | 15 +- 3 files changed, 208 insertions(+), 395 deletions(-)582 lines changed is a huge patch.
If possible, it would be better splitting it in incremental changes, making the review process easier.It looks like a huge patch, but it simply deletes the current implementation and add the new code. I think perhaps split it into 2, 1st one to replace just the
rte_vhost_enqueue_burst, 2nd one to delete all the obsoletefunctions.
Quoted 1 line
It should make the patch clear, how do you think? :)
Nope, it's not working in that way. It should be:
Quoted 8 lines
- one patch to fix the hang issue for windows guest
Please cc it to [email protected] as well so that we could pick it for v16.07 stable release.
- one patch to unite the two different Rx code path
- another patch to optimize mergeable code path
I can separate optimization from the basic code in v2, however as I explained this patch is built from scratch and doesn't take anything from the existing code, so there's no way to transform from the existing code incrementally into the new code.
Quoted 2 lines
--yliu
v2 cover vhost: optimize enqueue
In reply to Zhihong WangThis patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed for high performance and good maintainability, and improves CPU efficiency significantly by optimizing cache access, which means:
* For fast frontends (eg. DPDK virtio pmd), higher performance (maximum throughput) can be achieved.
* For slow frontends (eg. kernel virtio-net), better scalability can be achieved, each vhost core can support more connections since it takes less cycles to handle each single frontend.
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
In the existing code there're 2 callbacks for vhost enqueue:
* virtio_dev_merge_rx for mrg_rxbuf turned on cases.
* virtio_dev_rx for mrg_rxbuf turned off cases.
The performance of the existing code is not optimal, especially when the mrg_rxbuf feature turned on. Also, having 2 separated functions increases maintenance efforts.
---
Changes in v2:
1. Split the big function into several small ones
2. Use multiple patches to explain each optimization
3. Add comments
Zhihong Wang (6):
vhost: rewrite enqueue
vhost: remove obsolete
vhost: remove useless volatile
vhost: add desc prefetch
vhost: batch update used ring
vhost: optimize cache access
lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 582 +++++++++++++++--------------------------- lib/librte_vhost/virtio-net.c | 15 +- 3 files changed, 228 insertions(+), 375 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/6 vhost: rewrite enqueue
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
--- lib/librte_vhost/vhost_rxtx.c | 212 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 205 insertions(+), 7 deletions(-)
lib/librte_vhost/vhost_rxtx.c +205 -7
@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {
@@ -533,19 +533,217 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,return pkt_idx; } +static inline uint32_t __attribute__((always_inline)) +loop_check (struct vhost_virtqueue *vq,uint16_t avail_idx,uint32_t pkt_left) +{ +if (pkt_left ==0 || avail_idx == vq->last_used_idx) +return 1 ; + +return 0 ; +} + +static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) +{ +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr; +struct vring_desc *desc; +uint64_t desc_host_write_addr =0 ; +uint32_t desc_chain_head =0 ; +uint32_t desc_chain_len =0 ; +uint32_t desc_current =0 ; +uint32_t desc_write_offset =0 ; +uint32_t mbuf_len =0 ; +uint32_t mbuf_len_left =0 ; +uint32_t copy_len =0 ; +uint32_t extra_buffers =0 ; +uint32_t used_idx_round =0 ; + +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_len_left = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_host_write_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_host_write_addr)) + goto error; + +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *) + (uintptr_t )desc_host_write_addr; +memset ((void *)(uintptr_t )&(virtio_hdr->hdr), +0 , dev->vhost_hlen); +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +vhost_log_write (dev, desc->addr, dev->vhost_hlen); + desc_write_offset = dev->vhost_hlen; + desc_chain_len = desc_write_offset; + desc_host_write_addr += desc_write_offset; +if (is_mrg_rxbuf) + virtio_hdr->num_buffers =1 ; + +/* start copy from mbuf to desc */ +while (1 ) { +/* get the next mbuf if the current done */ +if (!mbuf_len_left) { +if (mbuf->next) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_len_left = mbuf_len; + }else +break ; + } + +/* get the next desc if the current done */ +if (desc->len <= desc_write_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */ + desc_write_offset =0 ; + desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_host_write_addr = +gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_host_write_addr)) + goto rollback; + }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ + used_idx_round = vq->last_used_idx + & (vq->size -1 ); + vq->used->ring[used_idx_round].id = + desc_chain_head; + vq->used->ring[used_idx_round].len = + desc_chain_len; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_round]), +sizeof (vq->used->ring[ + used_idx_round])); + vq->last_used_idx++; + extra_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto rollback; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_host_write_addr = +gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_host_write_addr)) + goto rollback; + + desc_chain_len =0 ; + desc_write_offset =0 ; + }else + goto rollback; + } + +/* copy mbuf data */ + copy_len =RTE_MIN (desc->len - desc_write_offset, + mbuf_len_left); +rte_memcpy ((void *)(uintptr_t )desc_host_write_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_len_left), + copy_len); +vhost_log_write (dev, desc->addr + desc_write_offset, + copy_len); + mbuf_len_left -= copy_len; + desc_write_offset += copy_len; + desc_host_write_addr += copy_len; + desc_chain_len += copy_len; + } + + used_idx_round = vq->last_used_idx & (vq->size -1 ); + vq->used->ring[used_idx_round].id = desc_chain_head; + vq->used->ring[used_idx_round].len = desc_chain_len; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used, ring[used_idx_round]), + sizeof(vq->used->ring[used_idx_round])); + vq->last_used_idx++; + +return 0 ; + +rollback: +/* rollback on any error if last_used_idx update on-the-fly */ +if (is_mrg_rxbuf) + vq->last_used_idx -= extra_buffers; + +error: +return 1 ; +} + +static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{ +rte_smp_wmb (); + vq->used->idx = vq->last_used_idx; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx)); +rte_mb (); +if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) + && (vq->callfd >=0 )) +eventfd_write (vq->callfd, (eventfd_t )1 ); +} +uint16_t rte_vhost_enqueue_burst (int vid,uint16_t queue_id,struct rte_mbuf **pkts,uint16_t count) { -struct virtio_net *dev =get_device (vid); +struct vhost_virtqueue *vq; +struct virtio_net *dev; +uint32_t pkt_idx =0 ; +uint32_t pkt_left =0 ; +uint32_t pkt_sent =0 ; +uint32_t is_mrg_rxbuf =0 ; +uint16_t avail_idx =0 ; + +/* precheck */ +if (unlikely (count ==0 )) +return 0 ; -if (!dev) + count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); + + dev =get_device (vid); +if (unlikely (!dev))return 0 ; -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); +if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) +return 0 ; + + vq = dev->virtqueue[queue_id]; +if (unlikely (!vq->enabled)) +return 0 ; + +if (dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF )) + is_mrg_rxbuf =1 ; + +/* start enqueuing packets 1 by 1 */ + pkt_idx =0 ; + pkt_left = count; + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (1 ) { +if (loop_check (vq, avail_idx, pkt_left)) +break ; + +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], + is_mrg_rxbuf)) +break ; + + pkt_idx++; + pkt_sent++; + pkt_left--; + } + +/* update used idx and kick the guest if necessary */ +if (pkt_sent) +notify_guest (dev, vq); + +return pkt_sent; } static void
Signature · 1 line
2.7.4
Re: [PATCH v2 1/6] vhost: rewrite enqueue
Quoted 23 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 212 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 205 insertions(+), 7 deletions(-)lib/librte_vhost/vhost_rxtx.c +1
-1@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {@@ -533,19 +533,217 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,return pkt_idx;
lib/librte_vhost/vhost_rxtx.c virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id, · line 543
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +8
-0} +static inline uint32_t __attribute__((always_inline)) +loop_check (struct vhost_virtqueue *vq,uint16_t avail_idx,uint32_t pkt_left) +{ +if (pkt_left ==0 || avail_idx == vq->last_used_idx) +return 1 ; + +return 0 ; +}
Hmmm, I don't see any benifit from making such simple check into a function.
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +5
-0+static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) +{
lib/librte_vhost/vhost_rxtx.c virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id, · line 559
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+struct virtio_net_hdr_mrg_rxbuf *virtio_hdr; +struct vring_desc *desc; +uint64_t desc_host_write_addr =0 ; +uint32_t desc_chain_head =0 ; +uint32_t desc_chain_len =0 ; +uint32_t desc_current =0 ; +uint32_t desc_write_offset =0 ; +uint32_t mbuf_len =0 ; +uint32_t mbuf_len_left =0 ; +uint32_t copy_len =0 ;
The dequeue function uses var like desc_addr, desc_avail, desc_offset, mbuf_avail, ..., I see no reason to use something different here. This breaks the code consistency. Besides that, var name like desc_host_write_addr looks redundant; desc_addr is much cleaner.
--yliu
Re: [PATCH v2 1/6] vhost: rewrite enqueue
Quoted 39 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Friday, August 19, 2016 10:39 AM
To: Wang, Zhihong <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v2 1/6] vhost: rewrite enqueueQuoted 6 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 212++++++++++++++++++++++++++++++++++++++++--
lib/librte_vhost/vhost_rxtx.c is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t
Quoted 7 lines
1 file changed, 205 insertions(+), 7 deletions(-)lib/librte_vhost/vhost_rxtx.c +0
-0@@ -91,7 +91,7 @@ is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t
qp_nb)lib/librte_vhost/vhost_rxtx.c is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c +1
-1return (is_tx ^ (idx &1 )) ==0 && idx < qp_nb *VIRTIO_QNUM ; } -static void +static inline void __attribute__((always_inline)) virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr
*net_hdr)lib/librte_vhost/vhost_rxtx.c virtio_dev_merge_rx(struct virtio_net *dev,
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +0
-0{if (m_buf->ol_flags &PKT_TX_L4_MASK ) {@@ -533,19 +533,217 @@ virtio_dev_merge_rx(struct virtio_net *dev,
uint16_tqueue_id,Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +0
-0return pkt_idx; }
Quoted 11 lines
lib/librte_vhost/vhost_rxtx.c virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id, · line 543
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c +8
-0+static inline uint32_t __attribute__((always_inline)) +loop_check (struct vhost_virtqueue *vq,uint16_t avail_idx,uint32_t pkt_left) +{ +if (pkt_left ==0 || avail_idx == vq->last_used_idx) +return 1 ; + +return 0 ; +}Hmmm, I don't see any benifit from making such simple check into a function.
It's for prefetch code later to be merged into this function.
Quoted 13 lines
Quoted 13 lines
lib/librte_vhost/vhost_rxtx.c +12
-0+static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) +{ +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr; +struct vring_desc *desc; +uint64_t desc_host_write_addr =0 ; +uint32_t desc_chain_head =0 ; +uint32_t desc_chain_len =0 ; +uint32_t desc_current =0 ; +uint32_t desc_write_offset =0 ;
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id, · line 559
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +3
-0+uint32_t mbuf_len =0 ; +uint32_t mbuf_len_left =0 ; +uint32_t copy_len =0 ;The dequeue function uses var like
desc_addr,desc_avail,desc_offset,mbuf_avail, ..., I see no reason to use something different here. This breaks the code consistency. Besides that, var name likedesc_host_write_addrlooks redundant;desc_addris much cleaner.
Okay.
Quoted 2 lines
--yliu
PATCH v2 2/6 vhost: remove obsolete
In reply to Zhihong WangThis patch removes obsolete functions.
--- lib/librte_vhost/vhost_rxtx.c | 408 ------------------------------------------ 1 file changed, 408 deletions(-)
lib/librte_vhost/vhost_rxtx.c +0 -408
@@ -125,414 +125,6 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) } } -static inline void -copy_virtio_net_hdr (struct virtio_net *dev,uint64_t desc_addr, -struct virtio_net_hdr_mrg_rxbuf hdr) -{ -if (dev->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 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); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ; - -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, desc->addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - - desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; - - 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; - - desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - 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 ; -} - -/** -* 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 -* count is returned to indicate the number of packets that are succesfully -* added to the RX queue. This function works when the mbuf is scattered, but -* it doesn't support the mergeable feature. -*/ -static inline uint32_t __attribute__((always_inline)) -virtio_dev_rx (struct virtio_net *dev,uint16_t queue_id, -struct rte_mbuf **pkts,uint32_t count) -{ -struct vhost_virtqueue *vq; -uint16_t avail_idx, free_entries, start_idx; -uint16_t desc_indexes[MAX_PKT_BURST ]; -uint16_t used_idx; -uint32_t i; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); -return 0 ; - } - - vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) -return 0 ; - - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - start_idx = vq->last_used_idx; - free_entries = avail_idx - start_idx; - count =RTE_MIN (count, free_entries); - count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); -if (count ==0 ) -return 0 ; - -LOG_DEBUG (VHOST_DATA ,"(%d) start_idx %d | end_idx %d\n" , - dev->vid, start_idx, start_idx + count); - -/* Retrieve all of the desc indexes first to avoid caching issues. */ -rte_prefetch0 (&vq->avail->ring[start_idx & (vq->size -1 )]); -for (i =0 ; i < count; i++) { - used_idx = (start_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; - vq->used->ring[used_idx].id = desc_indexes[i]; - vq->used->ring[used_idx].len = pkts[i]->pkt_len + - dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } - -rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; - - err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx); -if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } - -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } - -rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ -rte_mb (); - -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; -} - -static inline int -fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, -uint32_t *allocated,uint32_t *vec_idx, -struct buf_vector *buf_vec) -{ -uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; -uint32_t vec_id = *vec_idx; -uint32_t len = *allocated; - -while (1 ) { -if (unlikely (vec_id >=BUF_VECTOR_MAX || idx >= vq->size)) -return -1 ; - - len += vq->desc[idx].len; - buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - buf_vec[vec_id].buf_len = vq->desc[idx].len; - 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 ; -} - -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, -uint16_t *end,struct buf_vector *buf_vec) -{ -uint16_t cur_idx; -uint16_t avail_idx; -uint32_t allocated =0 ; -uint32_t vec_idx =0 ; -uint16_t tries =0 ; - - cur_idx = vq->last_used_idx; - -while (1 ) { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (cur_idx == avail_idx)) -return -1 ; - -if (unlikely (fill_vec_buf (vq, cur_idx, &allocated, - &vec_idx, buf_vec) <0 )) -return -1 ; - - 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 (unlikely (tries >= vq->size)) -return -1 ; - } - - *end = cur_idx; -return 0 ; -} - -static inline uint32_t __attribute__((always_inline)) -copy_mbuf_to_desc_mergeable (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint16_t end_idx,struct rte_mbuf *m, -struct buf_vector *buf_vec) -{ -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint32_t vec_idx =0 ; -uint16_t start_idx = vq->last_used_idx; -uint16_t cur_idx = 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; - -if (unlikely (m ==NULL )) -return 0 ; - -LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, cur_idx, end_idx); - - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) -return 0 ; - -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - - virtio_hdr.num_buffers = end_idx - start_idx; -LOG_DEBUG (VHOST_DATA ,"(%d) RX: num merge buffers %d\n" , - dev->vid, virtio_hdr.num_buffers); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - - desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; - desc_offset = dev->vhost_hlen; - - 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 = buf_vec[vec_idx].desc_idx; - -if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) { -/* 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; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } - - vec_idx++; - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (unlikely (!desc_addr)) -return 0 ; - -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - desc_offset =0 ; - desc_avail = buf_vec[vec_idx].buf_len; - } - -/* done with current mbuf, get the next one */ -if (mbuf_avail ==0 ) { - m = m->next; - - mbuf_offset =0 ; - mbuf_avail =rte_pktmbuf_data_len (m); - } - - 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, buf_vec[vec_idx].buf_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; - } - - used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = 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 end_idx - start_idx; -} - -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 , nr_used =0 ; -uint16_t end; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); -return 0 ; - } - - vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) -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 + dev->vhost_hlen; - -if (unlikely (reserve_avail_buf_mergeable (vq, pkt_len, - &end, buf_vec) <0 )) { -LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); -break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - - *(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 += nr_used; - } - -if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); - -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); - } - -return pkt_idx; -} -static inline uint32_t __attribute__((always_inline))loop_check (struct vhost_virtqueue *vq,uint16_t avail_idx,uint32_t pkt_left) {
Signature · 1 line
2.7.4
Re: [PATCH v2 2/6] vhost: remove obsolete
Quoted 1 line · Zhihong Wang
This patch removes obsolete functions.
Splitting patches doesn't work in this way: this should be in the first patch. Otherwise, build breaks in the first patch, as some functions are defined but not used.
--yliu
Re: [PATCH v2 2/6] vhost: remove obsolete
Quoted 9 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Friday, August 19, 2016 10:33 AM
To: Wang, Zhihong <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v2 2/6] vhost: remove obsolete
Quoted 6 lines
Quoted 1 line · Zhihong Wang
This patch removes obsolete functions.
Splitting patches doesn't work in this way: this should be in the first patch. Otherwise, build breaks in the first patch, as some functions are defined but not used.
Thanks. I'll send out v3 soon, also to fix a small glitch while running in old platform like snb and ivb.
Quoted 2 lines
--yliu
PATCH v2 3/6 vhost: remove useless volatile
In reply to Zhihong WangThis patch removes useless volatile attribute to allow compiler optimization.
--- lib/librte_vhost/vhost-net.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost-net.h +1 -1
@@ -71,7 +71,7 @@ struct vhost_virtqueue {uint32_t size;/* Last index used on the available ring */ -volatile uint16_t last_used_idx; +uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
Signature · 1 line
2.7.4
PATCH v2 4/6 vhost: add desc prefetch
In reply to Zhihong WangThis patch adds descriptor prefetch to hide cache access latency.
--- lib/librte_vhost/vhost_rxtx.c | 5 +++++ 1 file changed, 5 insertions(+)
lib/librte_vhost/vhost_rxtx.c +5 -0
@@ -131,6 +131,11 @@ loop_check(struct vhost_virtqueue *vq, uint16_t avail_idx, uint32_t pkt_left)if (pkt_left ==0 || avail_idx == vq->last_used_idx)return 1 ; +/* prefetch the next desc */ +if (pkt_left >1 && avail_idx != vq->last_used_idx +1 ) +rte_prefetch0 (&vq->desc[vq->avail->ring[ + (vq->last_used_idx +1 ) & (vq->size -1 )]]); +return 0 ; }
Signature · 1 line
2.7.4
PATCH v2 5/6 vhost: batch update used ring
In reply to Zhihong WangThis patch enables batch update of the used ring for better efficiency.
--- lib/librte_vhost/vhost-net.h | 4 +++ lib/librte_vhost/vhost_rxtx.c | 68 +++++++++++++++++++++++++++++++++---------- lib/librte_vhost/virtio-net.c | 15 ++++++++-- 3 files changed, 68 insertions(+), 19 deletions(-)
lib/librte_vhost/vhost-net.h +4 -0
@@ -85,6 +85,10 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +/* Shadow used ring for performance */ +struct vring_used_elem *shadow_used_ring; +uint32_t shadow_used_idx; } __rte_cache_aligned;/* Old kernels have no such macro defined */
lib/librte_vhost/vhost_rxtx.c +52 -16
112 lines not on this page · raw message
lib/librte_vhost/virtio-net.c +12 -3
@@ -152,10 +152,14 @@ cleanup_device(struct virtio_net *dev, int destroy)static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq = dev->virtqueue[i *VIRTIO_QNUM ]; +rte_free (vq->shadow_used_ring); +rte_free (vq); + }rte_free (dev); }
@@ -418,13 +422,18 @@ int vhost_set_vring_num(int vid, struct vhost_vring_state *state) { struct virtio_net *dev; +struct vhost_virtqueue *vq; dev =get_device (vid);if (dev ==NULL )return -1 ;/* State->index refers to the queue index. The txq is 1, rxq is 0. */ - dev->virtqueue[state->index]->size = state->num; + vq = dev->virtqueue[state->index]; + vq->size = state->num; + vq->shadow_used_ring =rte_malloc ("" , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE );return 0 ; }
Signature · 1 line
2.7.4
PATCH v2 6/6 vhost: optimize cache access
In reply to Zhihong WangThis patch reorders the code to delay virtio header write to optimize cache access efficiency for cases where the mrg_rxbuf feature is turned on. It reduces CPU pipeline stall cycles significantly.
--- lib/librte_vhost/vhost_rxtx.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
lib/librte_vhost/vhost_rxtx.c +16 -7
@@ -154,6 +154,7 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t mbuf_len =0 ;uint32_t mbuf_len_left =0 ;uint32_t copy_len =0 ; +uint32_t copy_virtio_hdr =0 ;uint32_t extra_buffers =0 ;/* start with the first mbuf of the packet */
@@ -168,18 +169,17 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,if (unlikely (!desc_host_write_addr)) goto error; -/* handle virtio header */ +/* +* handle virtio header, the actual write operation +* is delayed for cache optimization. +*/ virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *) (uintptr_t )desc_host_write_addr; -memset ((void *)(uintptr_t )&(virtio_hdr->hdr), -0 , dev->vhost_hlen); -virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); + copy_virtio_hdr =1 ;vhost_log_write (dev, desc->addr, dev->vhost_hlen); desc_write_offset = dev->vhost_hlen; desc_chain_len = desc_write_offset; desc_host_write_addr += desc_write_offset; -if (is_mrg_rxbuf) - virtio_hdr->num_buffers =1 ;/* start copy from mbuf to desc */ while (1 ) {
@@ -233,9 +233,18 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq, goto rollback; } -/* copy mbuf data */ +/* copy virtio header and mbuf data */ copy_len =RTE_MIN (desc->len - desc_write_offset, mbuf_len_left); +if (copy_virtio_hdr) { + copy_virtio_hdr =0 ; +memset ((void *)(uintptr_t )&(virtio_hdr->hdr), +0 , dev->vhost_hlen); +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers = extra_buffers +1 ; + } + rte_memcpy((void *)(uintptr_t)desc_host_write_addr, rte_pktmbuf_mtod_offset(mbuf, void *, mbuf_len - mbuf_len_left),
Signature · 1 line
2.7.4
v3 cover vhost: optimize enqueue
In reply to Zhihong WangThis patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed for high performance and good maintainability, and improves CPU efficiency significantly by optimizing cache access, which means:
* For fast frontends (eg. DPDK virtio pmd), higher performance (maximum throughput) can be achieved.
* For slow frontends (eg. kernel virtio-net), better scalability can be achieved, each vhost core can support more connections since it takes less cycles to handle each single frontend.
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Code reordering and batch used ring update bring most of the performance improvements.
In the existing code there're 2 callbacks for vhost enqueue:
* virtio_dev_merge_rx for mrg_rxbuf turned on cases.
* virtio_dev_rx for mrg_rxbuf turned off cases.
The performance of the existing code is not optimal, especially when the mrg_rxbuf feature turned on. Also, having 2 separated functions increases maintenance efforts.
---
Changes in v3:
1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
3. Rewrite enqueue and delete the obsolete in the same patch.
---
Changes in v2:
1. Split the big function into several small ones.
2. Use multiple patches to explain each optimization.
3. Add comments.
Zhihong Wang (5):
vhost: rewrite enqueue
vhost: remove useless volatile
vhost: add desc prefetch
vhost: batch update used ring
vhost: optimize cache access
lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 573 +++++++++++++++--------------------------- lib/librte_vhost/virtio-net.c | 15 +- 3 files changed, 220 insertions(+), 374 deletions(-)
Signature · 1 line
2.7.4
PATCH v3 1/5 vhost: rewrite enqueue
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
---
Changes in v3:
1. Rewrite enqueue and delete the obsolete in the same patch.
--- lib/librte_vhost/vhost_rxtx.c | 537 +++++++++++++----------------------------- 1 file changed, 160 insertions(+), 377 deletions(-)
lib/librte_vhost/vhost_rxtx.c +160 -377
@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {
@@ -125,427 +125,210 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) } } -static inline void -copy_virtio_net_hdr (struct virtio_net *dev,uint64_t desc_addr, -struct virtio_net_hdr_mrg_rxbuf hdr) +static inline uint32_t __attribute__((always_inline)) +loop_check (struct vhost_virtqueue *vq,uint16_t avail_idx,uint32_t pkt_left) { -if (dev->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; +if (pkt_left ==0 || avail_idx == vq->last_used_idx) +return 1 ; + +return 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) +static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;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]; +uint64_t desc_addr =0 ; +uint32_t desc_chain_head =0 ; +uint32_t desc_chain_len =0 ; +uint32_t desc_current =0 ; +uint32_t desc_offset =0 ; +uint32_t mbuf_len =0 ; +uint32_t mbuf_avail =0 ; +uint32_t copy_len =0 ; +uint32_t extra_buffers =0 ; +uint32_t used_idx_round =0 ; + +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ; - -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +if (unlikely (!desc_addr)) + goto error; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr));vhost_log_write (dev, desc->addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; + desc_chain_len = desc_offset; + desc_addr += desc_offset; +if (is_mrg_rxbuf) + virtio_hdr->num_buffers =1 ; - 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); +/* start copy from mbuf to desc */ +while (1 ) { +/* get the next mbuf if the current done */ +if (!mbuf_avail) { +if (mbuf->next) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + }else +break ; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; - - desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; +/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */ + desc_offset =0 ; + desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto rollback; + }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ + used_idx_round = vq->last_used_idx + & (vq->size -1 ); + vq->used->ring[used_idx_round].id = + desc_chain_head; + vq->used->ring[used_idx_round].len = + desc_chain_len; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_round]), +sizeof (vq->used->ring[ + used_idx_round])); + vq->last_used_idx++; + extra_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto rollback; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto rollback; + + desc_chain_len =0 ; + desc_offset =0 ; + }else + goto rollback; } - 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 ; -} - -/** -* 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 -* count is returned to indicate the number of packets that are succesfully -* added to the RX queue. This function works when the mbuf is scattered, but -* it doesn't support the mergeable feature. -*/ -static inline uint32_t __attribute__((always_inline)) -virtio_dev_rx (struct virtio_net *dev,uint16_t queue_id, -struct rte_mbuf **pkts,uint32_t count) -{ -struct vhost_virtqueue *vq; -uint16_t avail_idx, free_entries, start_idx; -uint16_t desc_indexes[MAX_PKT_BURST ]; -uint16_t used_idx; -uint32_t i; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); -return 0 ; +/* copy mbuf data */ + copy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +rte_memcpy ((void *)(uintptr_t )desc_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_avail), + copy_len); +vhost_log_write (dev, desc->addr + desc_offset, copy_len); + mbuf_avail -= copy_len; + desc_offset += copy_len; + desc_addr += copy_len; + desc_chain_len += copy_len; } - vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) -return 0 ; - - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - start_idx = vq->last_used_idx; - free_entries = avail_idx - start_idx; - count =RTE_MIN (count, free_entries); - count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); -if (count ==0 ) -return 0 ; - -LOG_DEBUG (VHOST_DATA ,"(%d) start_idx %d | end_idx %d\n" , - dev->vid, start_idx, start_idx + count); - -/* Retrieve all of the desc indexes first to avoid caching issues. */ -rte_prefetch0 (&vq->avail->ring[start_idx & (vq->size -1 )]); -for (i =0 ; i < count; i++) { - used_idx = (start_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; - vq->used->ring[used_idx].id = desc_indexes[i]; - vq->used->ring[used_idx].len = pkts[i]->pkt_len + - dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } + used_idx_round = vq->last_used_idx & (vq->size -1 ); + vq->used->ring[used_idx_round].id = desc_chain_head; + vq->used->ring[used_idx_round].len = desc_chain_len; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used, ring[used_idx_round]), + sizeof(vq->used->ring[used_idx_round])); + vq->last_used_idx++; -rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; +return 0 ; - err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx); -if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } +rollback: +/* rollback on any error if last_used_idx update on-the-fly */ +if (is_mrg_rxbuf) + vq->last_used_idx -= extra_buffers; -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } +error: +return 1 ; +} +static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ + vq->used->idx = vq->last_used_idx; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; -} - -static inline int -fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, -uint32_t *allocated,uint32_t *vec_idx, -struct buf_vector *buf_vec) -{ -uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; -uint32_t vec_id = *vec_idx; -uint32_t len = *allocated; - -while (1 ) { -if (unlikely (vec_id >=BUF_VECTOR_MAX || idx >= vq->size)) -return -1 ; - - len += vq->desc[idx].len; - buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - buf_vec[vec_id].buf_len = vq->desc[idx].len; - 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 ; -} - -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, -uint16_t *end,struct buf_vector *buf_vec) -{ -uint16_t cur_idx; -uint16_t avail_idx; -uint32_t allocated =0 ; -uint32_t vec_idx =0 ; -uint16_t tries =0 ; - - cur_idx = vq->last_used_idx; - -while (1 ) { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (cur_idx == avail_idx)) -return -1 ; - -if (unlikely (fill_vec_buf (vq, cur_idx, &allocated, - &vec_idx, buf_vec) <0 )) -return -1 ; - - 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 (unlikely (tries >= vq->size)) -return -1 ; - } - - *end = cur_idx; -return 0 ; } -static inline uint32_t __attribute__((always_inline)) -copy_mbuf_to_desc_mergeable (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint16_t end_idx,struct rte_mbuf *m, -struct buf_vector *buf_vec) +uint16_t +rte_vhost_enqueue_burst (int vid,uint16_t queue_id, +struct rte_mbuf **pkts,uint16_t count) { -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint32_t vec_idx =0 ; -uint16_t start_idx = vq->last_used_idx; -uint16_t cur_idx = 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; - -if (unlikely (m ==NULL )) +struct vhost_virtqueue *vq; +struct virtio_net *dev; +uint32_t pkt_idx =0 ; +uint32_t pkt_left =0 ; +uint32_t pkt_sent =0 ; +uint32_t is_mrg_rxbuf =0 ; +uint16_t avail_idx =0 ; + +/* precheck */ +if (unlikely (count ==0 ))return 0 ; -LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, cur_idx, end_idx); + count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) + dev =get_device (vid); +if (unlikely (!dev))return 0 ; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - - virtio_hdr.num_buffers = end_idx - start_idx; -LOG_DEBUG (VHOST_DATA ,"(%d) RX: num merge buffers %d\n" , - dev->vid, virtio_hdr.num_buffers); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - - desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; - desc_offset = dev->vhost_hlen; - - 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 = buf_vec[vec_idx].desc_idx; - -if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) { -/* 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; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } - - vec_idx++; - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (unlikely (!desc_addr)) -return 0 ; - -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - desc_offset =0 ; - desc_avail = buf_vec[vec_idx].buf_len; - } - -/* done with current mbuf, get the next one */ -if (mbuf_avail ==0 ) { - m = m->next; - - mbuf_offset =0 ; - mbuf_avail =rte_pktmbuf_data_len (m); - } - - 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, buf_vec[vec_idx].buf_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; - } - - used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = 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 end_idx - start_idx; -} - -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 , nr_used =0 ; -uint16_t end; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); +if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb)))return 0 ; - } vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) +if (unlikely (!vq->enabled))return 0 ; - count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); -if (count ==0 ) -return 0 ; +if (dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF )) + is_mrg_rxbuf =1 ; -for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; - -if (unlikely (reserve_avail_buf_mergeable (vq, pkt_len, - &end, buf_vec) <0 )) { -LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); +/* start enqueuing packets 1 by 1 */ + pkt_idx =0 ; + pkt_left = count; + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (1 ) { +if (loop_check (vq, avail_idx, pkt_left))break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - *(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 += nr_used; - } - -if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], + is_mrg_rxbuf)) +break ; -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); + pkt_idx++; + pkt_sent++; + pkt_left--; } -return pkt_idx; -} - -uint16_t -rte_vhost_enqueue_burst (int vid,uint16_t queue_id, -struct rte_mbuf **pkts,uint16_t count) -{ -struct virtio_net *dev =get_device (vid); - -if (!dev) -return 0 ; +/* update used idx and kick the guest if necessary */ +if (pkt_sent) +notify_guest (dev, vq); -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); +return pkt_sent; } static void
Signature · 1 line
2.7.4
Re: [PATCH v3 1/5] vhost: rewrite enqueue
Quoted 27 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
---
Changes in v3:1. Rewrite enqueue and delete the obsolete in the same patch.
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 537 +++++++++++++----------------------------- 1 file changed, 160 insertions(+), 377 deletions(-)lib/librte_vhost/vhost_rxtx.c +1
-1@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 129
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c +2
-3@@ -125,427 +125,210 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) } } -static inline void -copy_virtio_net_hdr (struct virtio_net *dev,uint64_t desc_addr, -struct virtio_net_hdr_mrg_rxbuf hdr) +static inline uint32_t __attribute__((always_inline)) +loop_check (struct vhost_virtqueue *vq,uint16_t avail_idx,uint32_t pkt_left)
Creating a function just for doing this doesn't make much sense. And the function name doesn't help.
I think you should just remove this function.
Quoted 28 lines
lib/librte_vhost/vhost_rxtx.c +9
-14{ -if (dev->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; +if (pkt_left ==0 || avail_idx == vq->last_used_idx) +return 1 ; + +return 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) +static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;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];
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 153
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+uint64_t desc_addr =0 ; +uint32_t desc_chain_head =0 ; +uint32_t desc_chain_len =0 ; +uint32_t desc_current =0 ; +uint32_t desc_offset =0 ; +uint32_t mbuf_len =0 ; +uint32_t mbuf_avail =0 ; +uint32_t copy_len =0 ; +uint32_t extra_buffers =0 ; +uint32_t used_idx_round =0 ;
Most of these variables don't need to be initialized.
Quoted 17 lines
lib/librte_vhost/vhost_rxtx.c +9
-7+ +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ;
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 169
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +5
-4- -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +if (unlikely (!desc_addr)) + goto error; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr));
Parenthesis around virtio_hdr->hdr shouldn't be needed.
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 164
Quoted 2 lines
lib/librte_vhost/vhost_rxtx.c +0
-1vhost_log_write (dev, desc->addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 );
Looks like you remove the PRINT_PACKET calls.
Does it impact performance?
In any case, it should be mentionned in the commit message.
Quoted 9 lines
lib/librte_vhost/vhost_rxtx.c +4
-3- desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; + desc_chain_len = desc_offset; + desc_addr += desc_offset; +if (is_mrg_rxbuf) + virtio_hdr->num_buffers =1 ; - mbuf_avail =rte_pktmbuf_data_len (m);
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 178
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +2
-8- 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); +/* start copy from mbuf to desc */ +while (1 ) {
Please avoid while(1) when you can check for a real condition:
while (mbuf_avail || mbuf->next) ?
Compiler should optimize this properly, no?
Quoted 26 lines
lib/librte_vhost/vhost_rxtx.c +8
-16+/* get the next mbuf if the current done */ +if (!mbuf_avail) { +if (mbuf->next) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + }else +break ; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; - - desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len;
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 198
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */ + desc_offset =0 ; + desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto rollback;
you could goto directly to error, and decrement last_used_idx directly under "error"'s goto since extra_buffers will be zero otherwise.
Also, except desc_current affectation, all the above code is common with mergeable case, so you should avoid duplication.
Quoted 110 lines
lib/librte_vhost/vhost_rxtx.c +43
-63+ }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ + used_idx_round = vq->last_used_idx + & (vq->size -1 ); + vq->used->ring[used_idx_round].id = + desc_chain_head; + vq->used->ring[used_idx_round].len = + desc_chain_len; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_round]), +sizeof (vq->used->ring[ + used_idx_round])); + vq->last_used_idx++; + extra_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto rollback; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto rollback; + + desc_chain_len =0 ; + desc_offset =0 ; + }else + goto rollback; } - 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 ; -} - -/** -* 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 -* count is returned to indicate the number of packets that are succesfully -* added to the RX queue. This function works when the mbuf is scattered, but -* it doesn't support the mergeable feature. -*/ -static inline uint32_t __attribute__((always_inline)) -virtio_dev_rx (struct virtio_net *dev,uint16_t queue_id, -struct rte_mbuf **pkts,uint32_t count) -{ -struct vhost_virtqueue *vq; -uint16_t avail_idx, free_entries, start_idx; -uint16_t desc_indexes[MAX_PKT_BURST ]; -uint16_t used_idx; -uint32_t i; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); -return 0 ; +/* copy mbuf data */ + copy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +rte_memcpy ((void *)(uintptr_t )desc_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_avail), + copy_len); +vhost_log_write (dev, desc->addr + desc_offset, copy_len); + mbuf_avail -= copy_len; + desc_offset += copy_len; + desc_addr += copy_len; + desc_chain_len += copy_len; } - vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) -return 0 ; - - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - start_idx = vq->last_used_idx; - free_entries = avail_idx - start_idx; - count =RTE_MIN (count, free_entries); - count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); -if (count ==0 ) -return 0 ; - -LOG_DEBUG (VHOST_DATA ,"(%d) start_idx %d | end_idx %d\n" , - dev->vid, start_idx, start_idx + count); - -/* Retrieve all of the desc indexes first to avoid caching issues. */ -rte_prefetch0 (&vq->avail->ring[start_idx & (vq->size -1 )]); -for (i =0 ; i < count; i++) { - used_idx = (start_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; - vq->used->ring[used_idx].id = desc_indexes[i]; - vq->used->ring[used_idx].len = pkts[i]->pkt_len + - dev->vhost_hlen; - vhost_log_used_vring(dev, vq,
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 252
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +7
-3- offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } + used_idx_round = vq->last_used_idx & (vq->size -1 ); + vq->used->ring[used_idx_round].id = desc_chain_head; + vq->used->ring[used_idx_round].len = desc_chain_len; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used, ring[used_idx_round]), + sizeof(vq->used->ring[used_idx_round])); + vq->last_used_idx++;
All this code is duplicatedd from the rx_mergeable base. I think a dedicated inline function would really make sense here.
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c +1
-5-rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; +return 0 ; - err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx);
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 258
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +3
-7-if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } +rollback: +/* rollback on any error if last_used_idx update on-the-fly */ +if (is_mrg_rxbuf)
If (!is_mrg_rxbuf), extra_buffers will be zero, so just remove the test, and place belw line directly under error: as explained above.
Quoted 125 lines
lib/librte_vhost/vhost_rxtx.c +13
-102+ vq->last_used_idx -= extra_buffers; -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } +error: +return 1 ; +} +static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ + vq->used->idx = vq->last_used_idx; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; -} - -static inline int -fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, -uint32_t *allocated,uint32_t *vec_idx, -struct buf_vector *buf_vec) -{ -uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; -uint32_t vec_id = *vec_idx; -uint32_t len = *allocated; - -while (1 ) { -if (unlikely (vec_id >=BUF_VECTOR_MAX || idx >= vq->size)) -return -1 ; - - len += vq->desc[idx].len; - buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - buf_vec[vec_id].buf_len = vq->desc[idx].len; - 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 ; -} - -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, -uint16_t *end,struct buf_vector *buf_vec) -{ -uint16_t cur_idx; -uint16_t avail_idx; -uint32_t allocated =0 ; -uint32_t vec_idx =0 ; -uint16_t tries =0 ; - - cur_idx = vq->last_used_idx; - -while (1 ) { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (cur_idx == avail_idx)) -return -1 ; - -if (unlikely (fill_vec_buf (vq, cur_idx, &allocated, - &vec_idx, buf_vec) <0 )) -return -1 ; - - 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 (unlikely (tries >= vq->size)) -return -1 ; - } - - *end = cur_idx; -return 0 ; } -static inline uint32_t __attribute__((always_inline)) -copy_mbuf_to_desc_mergeable (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint16_t end_idx,struct rte_mbuf *m, -struct buf_vector *buf_vec) +uint16_t +rte_vhost_enqueue_burst (int vid,uint16_t queue_id, +struct rte_mbuf **pkts,uint16_t count) { -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint32_t vec_idx =0 ; -uint16_t start_idx = vq->last_used_idx; -uint16_t cur_idx = 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; -
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 290
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +9
-1-if (unlikely (m ==NULL )) +struct vhost_virtqueue *vq; +struct virtio_net *dev; +uint32_t pkt_idx =0 ; +uint32_t pkt_left =0 ; +uint32_t pkt_sent =0 ; +uint32_t is_mrg_rxbuf =0 ; +uint16_t avail_idx =0 ; + +/* precheck */
Comment not very informative here.
Quoted 121 lines
lib/librte_vhost/vhost_rxtx.c +8
-102+if (unlikely (count ==0 ))return 0 ; -LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, cur_idx, end_idx); + count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) + dev =get_device (vid); +if (unlikely (!dev))return 0 ; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - - virtio_hdr.num_buffers = end_idx - start_idx; -LOG_DEBUG (VHOST_DATA ,"(%d) RX: num merge buffers %d\n" , - dev->vid, virtio_hdr.num_buffers); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - - desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; - desc_offset = dev->vhost_hlen; - - 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 = buf_vec[vec_idx].desc_idx; - -if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) { -/* 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; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } - - vec_idx++; - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (unlikely (!desc_addr)) -return 0 ; - -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - desc_offset =0 ; - desc_avail = buf_vec[vec_idx].buf_len; - } - -/* done with current mbuf, get the next one */ -if (mbuf_avail ==0 ) { - m = m->next; - - mbuf_offset =0 ; - mbuf_avail =rte_pktmbuf_data_len (m); - } - - 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, buf_vec[vec_idx].buf_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; - } - - used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = 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 end_idx - start_idx; -} - -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 , nr_used =0 ; -uint16_t end; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); +if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb)))return 0 ; - } vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) +if (unlikely (!vq->enabled))return 0 ; - count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); -if (count ==0 ) -return 0 ; +if (dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF )) + is_mrg_rxbuf =1 ; -for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; - - if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 315
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +6
-4- &end, buf_vec) < 0)) { -LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); +/* start enqueuing packets 1 by 1 */ + pkt_idx =0 ; + pkt_left = count; + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (1 ) { +if (loop_check (vq, avail_idx, pkt_left))
What about:
while (pkt_left && avail_idx != vq->last_used_idx) {
Quoted 53 lines
lib/librte_vhost/vhost_rxtx.c +10
-33break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - *(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 += nr_used; - } - -if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], + is_mrg_rxbuf)) +break ; -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); + pkt_idx++; + pkt_sent++; + pkt_left--; } -return pkt_idx; -} - -uint16_t -rte_vhost_enqueue_burst (int vid,uint16_t queue_id, -struct rte_mbuf **pkts,uint16_t count) -{ -struct virtio_net *dev =get_device (vid); - -if (!dev) -return 0 ; +/* update used idx and kick the guest if necessary */ +if (pkt_sent) +notify_guest (dev, vq); -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); +return pkt_sent; } static void
Re: [PATCH v3 1/5] vhost: rewrite enqueue
Hi Maxime,
Thanks very much for the detailed review.
Quoted 43 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, August 22, 2016 5:36 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v3 1/5] vhost: rewrite enqueuelib/librte_vhost/vhost_rxtx.c is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t
Quoted 18 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
---
Changes in v3:1. Rewrite enqueue and delete the obsolete in the same patch.
Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 537 +++++++++++++----------------------------- 1 file changed, 160 insertions(+), 377 deletions(-)lib/librte_vhost/vhost_rxtx.c +0
-0@@ -91,7 +91,7 @@ is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t
qp_nb)lib/librte_vhost/vhost_rxtx.c is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c +1
-1return (is_tx ^ (idx &1 )) ==0 && idx < qp_nb *VIRTIO_QNUM ; } -static void +static inline void __attribute__((always_inline)) virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr
*net_hdr)lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf,
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +0
-0{if (m_buf->ol_flags &PKT_TX_L4_MASK ) {@@ -125,427 +125,210 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf,struct
virtio_net_hdr*net_hdr)Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +0
-0} }
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 129
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +2
-3-static inline void -copy_virtio_net_hdr (struct virtio_net *dev,uint64_t desc_addr, -struct virtio_net_hdr_mrg_rxbuf hdr) +static inline uint32_t __attribute__((always_inline)) +loop_check (struct vhost_virtqueue *vq,uint16_t avail_idx,uint32_t pkt_left)Creating a function just for doing this doesn't make much sense. And the function name doesn't help.
I think you should just remove this function.
Okay.
Quoted 32 lines
Quoted 32 lines
lib/librte_vhost/vhost_rxtx.c +12
-14{ -if (dev->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; +if (pkt_left ==0 || avail_idx == vq->last_used_idx) +return 1 ; + +return 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) +static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;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]; +uint64_t desc_addr =0 ; +uint32_t desc_chain_head =0 ; +uint32_t desc_chain_len =0 ;
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 153
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +7
-0+uint32_t desc_current =0 ; +uint32_t desc_offset =0 ; +uint32_t mbuf_len =0 ; +uint32_t mbuf_avail =0 ; +uint32_t copy_len =0 ; +uint32_t extra_buffers =0 ; +uint32_t used_idx_round =0 ;Most of these variables don't need to be initialized.
Okay.
Quoted 29 lines
Quoted 18 lines
lib/librte_vhost/vhost_rxtx.c +9
-7+ +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ;lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 169
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +5
-4- -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +if (unlikely (!desc_addr)) + goto error; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr));Parenthesis around
virtio_hdr->hdrshouldn't be needed.
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 164
Quoted 2 lines
lib/librte_vhost/vhost_rxtx.c +0
-1vhost_log_write (dev, desc->addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 );Looks like you remove the
PRINT_PACKETcalls.
Does it impact performance?
In any case, it should be mentionned in the commit message.
Will add this.
Quoted 9 lines
Quoted 9 lines
lib/librte_vhost/vhost_rxtx.c +4
-2- desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; + desc_chain_len = desc_offset; + desc_addr += desc_offset; +if (is_mrg_rxbuf) + virtio_hdr->num_buffers =1 ;
Quoted 13 lines
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 178
Quoted 11 lines
lib/librte_vhost/vhost_rxtx.c +2
-9- 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); +/* start copy from mbuf to desc */ +while (1 ) {Please avoid while(1) when you can check for a real condition:
while (mbuf_avail||mbuf->next) ?
Will rewrite this logic.
Quoted 32 lines
Compiler should optimize this properly, no?
Quoted 30 lines
lib/librte_vhost/vhost_rxtx.c +12
-16+/* get the next mbuf if the current done */ +if (!mbuf_avail) { +if (mbuf->next) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + }else +break ; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; - - desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; +/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 198
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c +6
-0+ desc_offset =0 ; + desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto rollback;you could goto directly to error, and decrement
last_used_idxdirectly under "error"'s goto sinceextra_bufferswill be zero otherwise.
Good call.
Quoted 117 lines
Also, except
desc_currentaffectation, all the above code is common with mergeable case, so you should avoid duplication.Quoted 114 lines
lib/librte_vhost/vhost_rxtx.c +44
-66+ }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ + used_idx_round = vq->last_used_idx + & (vq->size -1 ); + vq->used->ring[used_idx_round].id = + desc_chain_head; + vq->used->ring[used_idx_round].len = + desc_chain_len; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_round]), +sizeof (vq->used->ring[ + used_idx_round])); + vq->last_used_idx++; + extra_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto rollback; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto rollback; + + desc_chain_len =0 ; + desc_offset =0 ; + }else + goto rollback; } - 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 ; -} - -/** -* 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 -* count is returned to indicate the number of packets that are succesfully -* added to the RX queue. This function works when the mbuf is scattered, but -* it doesn't support the mergeable feature. -*/ -static inline uint32_t __attribute__((always_inline)) -virtio_dev_rx (struct virtio_net *dev,uint16_t queue_id, -struct rte_mbuf **pkts,uint32_t count) -{ -struct vhost_virtqueue *vq; -uint16_t avail_idx, free_entries, start_idx; -uint16_t desc_indexes[MAX_PKT_BURST ]; -uint16_t used_idx; -uint32_t i; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); -return 0 ; +/* copy mbuf data */ + copy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +rte_memcpy ((void *)(uintptr_t )desc_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_avail), + copy_len); +vhost_log_write (dev, desc->addr + desc_offset, copy_len); + mbuf_avail -= copy_len; + desc_offset += copy_len; + desc_addr += copy_len; + desc_chain_len += copy_len; } - vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) -return 0 ; - - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - start_idx = vq->last_used_idx; - free_entries = avail_idx - start_idx; - count =RTE_MIN (count, free_entries); - count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); -if (count ==0 ) -return 0 ; - -LOG_DEBUG (VHOST_DATA ,"(%d) start_idx %d | end_idx %d\n" , - dev->vid, start_idx, start_idx + count); - -/* Retrieve all of the desc indexes first to avoid caching issues. */ -rte_prefetch0 (&vq->avail->ring[start_idx & (vq->size -1 )]); -for (i =0 ; i < count; i++) { - used_idx = (start_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; - vq->used->ring[used_idx].id = desc_indexes[i]; - vq->used->ring[used_idx].len = pkts[i]->pkt_len + - dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } + used_idx_round = vq->last_used_idx & (vq->size -1 );
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 252
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c +6
-0+ vq->used->ring[used_idx_round].id = desc_chain_head; + vq->used->ring[used_idx_round].len = desc_chain_len; +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used, ring[used_idx_round]), + sizeof(vq->used->ring[used_idx_round])); + vq->last_used_idx++;All this code is duplicatedd from the
rx_mergeablebase. I think a dedicated inline function would really make sense here.
Good catch. Will make a function for this.
Quoted 8 lines
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c +1
-4-rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; +return 0 ;
Quoted 13 lines
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 258
Quoted 11 lines
lib/librte_vhost/vhost_rxtx.c +3
-8- err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx); -if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } +rollback: +/* rollback on any error if last_used_idx update on-the-fly */ +if (is_mrg_rxbuf)If (!is_mrg_rxbuf),
extra_bufferswill be zero, so just remove the test, and place belw line directly under error: as explained above.
Sure. Thanks.
Quoted 129 lines
Quoted 99 lines
lib/librte_vhost/vhost_rxtx.c +10
-82+ vq->last_used_idx -= extra_buffers; -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } +error: +return 1 ; +} +static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ + vq->used->idx = vq->last_used_idx; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; -} - -static inline int -fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, -uint32_t *allocated,uint32_t *vec_idx, -struct buf_vector *buf_vec) -{ -uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; -uint32_t vec_id = *vec_idx; -uint32_t len = *allocated; - -while (1 ) { -if (unlikely (vec_id >=BUF_VECTOR_MAX || idx >= vq->size)) -return -1 ; - - len += vq->desc[idx].len; - buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - buf_vec[vec_id].buf_len = vq->desc[idx].len; - 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 ; -} - -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, -uint16_t *end,struct buf_vector *buf_vec) -{ -uint16_t cur_idx; -uint16_t avail_idx; -uint32_t allocated =0 ; -uint32_t vec_idx =0 ; -uint16_t tries =0 ; - - cur_idx = vq->last_used_idx; - -while (1 ) { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (cur_idx == avail_idx)) -return -1 ; - -if (unlikely (fill_vec_buf (vq, cur_idx, &allocated, - &vec_idx, buf_vec) <0 )) -return -1 ; - - 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. -*/ lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +0
-8-if (unlikely (tries >= vq->size)) -return -1 ; - } - - *end = cur_idx; -return 0 ; } -static inline uint32_t __attribute__((always_inline)) -copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct
vhost_virtqueue*vq,Quoted 19 lines
lib/librte_vhost/vhost_rxtx.c +5
-13- uint16_t end_idx, struct rte_mbuf *m, - struct buf_vector *buf_vec) +uint16_t +rte_vhost_enqueue_burst (int vid,uint16_t queue_id, +struct rte_mbuf **pkts,uint16_t count) { - struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0}; -uint32_t vec_idx =0 ; -uint16_t start_idx = vq->last_used_idx; -uint16_t cur_idx = 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; - -if (unlikely (m ==NULL )) +struct vhost_virtqueue *vq; +struct virtio_net *dev;
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 290
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +7
-0+uint32_t pkt_idx =0 ; +uint32_t pkt_left =0 ; +uint32_t pkt_sent =0 ; +uint32_t is_mrg_rxbuf =0 ; +uint16_t avail_idx =0 ; + +/* precheck */ Comment not very informative here.
Okay.
Quoted 117 lines
Quoted 117 lines
lib/librte_vhost/vhost_rxtx.c +8
-98+if (unlikely (count ==0 ))return 0 ; -LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, cur_idx, end_idx); + count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) + dev =get_device (vid); +if (unlikely (!dev))return 0 ; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - - virtio_hdr.num_buffers = end_idx - start_idx; -LOG_DEBUG (VHOST_DATA ,"(%d) RX: num merge buffers %d\n" , - dev->vid, virtio_hdr.num_buffers); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - - desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; - desc_offset = dev->vhost_hlen; - - 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 = buf_vec[vec_idx].desc_idx; - -if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) { -/* 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; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } - - vec_idx++; - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (unlikely (!desc_addr)) -return 0 ; - -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - desc_offset =0 ; - desc_avail = buf_vec[vec_idx].buf_len; - } - -/* done with current mbuf, get the next one */ -if (mbuf_avail ==0 ) { - m = m->next; - - mbuf_offset =0 ; - mbuf_avail =rte_pktmbuf_data_len (m); - } - - 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, buf_vec[vec_idx].buf_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; - } - - used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = 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 end_idx - start_idx; -} - -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 , nr_used =0 ; -uint16_t end; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); +if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb)))return 0 ; - } vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) +if (unlikely (!vq->enabled))return 0 ; - count =RTE_MIN ((uint32_t )MAX_PKT_BURST , count); -if (count ==0 ) -return 0 ; +if (dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF )) + is_mrg_rxbuf =1 ;
Quoted 16 lines
Quoted 4 lines
lib/librte_vhost/vhost_rxtx.c +0
-4-for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; - - if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 315
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +6
-4- &end, buf_vec) < 0)) { -LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); +/* start enqueuing packets 1 by 1 */ + pkt_idx =0 ; + pkt_left = count; + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (1 ) { +if (loop_check (vq, avail_idx, pkt_left))What about:
while (pkt_left&&avail_idx!=vq->last_used_idx) {
Will rewrite it.
Quoted 54 lines
Quoted 54 lines
lib/librte_vhost/vhost_rxtx.c +10
-33break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - *(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 += nr_used; - } - -if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], + is_mrg_rxbuf)) +break ; -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); + pkt_idx++; + pkt_sent++; + pkt_left--; } -return pkt_idx; -} - -uint16_t -rte_vhost_enqueue_burst (int vid,uint16_t queue_id, -struct rte_mbuf **pkts,uint16_t count) -{ -struct virtio_net *dev =get_device (vid); - -if (!dev) -return 0 ; +/* update used idx and kick the guest if necessary */ +if (pkt_sent) +notify_guest (dev, vq); -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); +return pkt_sent; } static void
Re: [PATCH v3 1/5] vhost: rewrite enqueue
In reply to Maxime CoquelinQuoted 10 lines · Maxime Coquelin
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 169
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +3
-2-virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr));Parenthesis around
virtio_hdr->hdrshouldn't be needed.Review
Quoted 2 lines
vhost_log_write(dev, desc->addr, dev->vhost_hlen);(quoted diff) +0
-1- PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);Looks like you remove the
PRINT_PACKETcalls.
Does it impact performance?
Yes, it does. But it's only enabled for debug mode. Besides that, it's just a NOOP.
Quoted 1 line
In any case, it should be mentionned in the commit message.
Agreed. But for this case, we should not remove it: it breaks the debug-ability.
--yliu
PATCH v3 2/5 vhost: remove useless volatile
In reply to Zhihong WangThis patch removes useless volatile attribute to allow compiler optimization.
--- lib/librte_vhost/vhost-net.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost-net.h +1 -1
@@ -71,7 +71,7 @@ struct vhost_virtqueue {uint32_t size;/* Last index used on the available ring */ -volatile uint16_t last_used_idx; +uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
Signature · 1 line
2.7.4
PATCH v3 3/5 vhost: add desc prefetch
In reply to Zhihong WangThis patch adds descriptor prefetch to hide cache access latency.
--- lib/librte_vhost/vhost_rxtx.c | 5 +++++ 1 file changed, 5 insertions(+)
lib/librte_vhost/vhost_rxtx.c +5 -0
@@ -131,6 +131,11 @@ loop_check(struct vhost_virtqueue *vq, uint16_t avail_idx, uint32_t pkt_left)if (pkt_left ==0 || avail_idx == vq->last_used_idx)return 1 ; +/* prefetch the next desc */ +if (pkt_left >1 && avail_idx != vq->last_used_idx +1 ) +rte_prefetch0 (&vq->desc[vq->avail->ring[ + (vq->last_used_idx +1 ) & (vq->size -1 )]]); +return 0 ; }
Signature · 1 line
2.7.4
PATCH v3 4/5 vhost: batch update used ring
In reply to Zhihong WangThis patch enables batch update of the used ring for better efficiency.
--- lib/librte_vhost/vhost-net.h | 4 +++ lib/librte_vhost/vhost_rxtx.c | 68 +++++++++++++++++++++++++++++++++---------- lib/librte_vhost/virtio-net.c | 15 ++++++++-- 3 files changed, 68 insertions(+), 19 deletions(-)
lib/librte_vhost/vhost-net.h +4 -0
@@ -85,6 +85,10 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +/* Shadow used ring for performance */ +struct vring_used_elem *shadow_used_ring; +uint32_t shadow_used_idx; } __rte_cache_aligned;/* Old kernels have no such macro defined */
lib/librte_vhost/vhost_rxtx.c +52 -16
112 lines not on this page · raw message
lib/librte_vhost/virtio-net.c +12 -3
@@ -152,10 +152,14 @@ cleanup_device(struct virtio_net *dev, int destroy)static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq = dev->virtqueue[i *VIRTIO_QNUM ]; +rte_free (vq->shadow_used_ring); +rte_free (vq); + }rte_free (dev); }
@@ -418,13 +422,18 @@ int vhost_set_vring_num(int vid, struct vhost_vring_state *state) { struct virtio_net *dev; +struct vhost_virtqueue *vq; dev =get_device (vid);if (dev ==NULL )return -1 ;/* State->index refers to the queue index. The txq is 1, rxq is 0. */ - dev->virtqueue[state->index]->size = state->num; + vq = dev->virtqueue[state->index]; + vq->size = state->num; + vq->shadow_used_ring =rte_malloc ("" , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE );return 0 ; }
Signature · 1 line
2.7.4
Re: [PATCH v3 4/5] vhost: batch update used ring
Quoted 3 lines · Zhihong Wang
This patch enables batch update of the used ring for better efficiency.
Signed-off-by Zhihong Wang <[email protected]>
...
Quoted 27 lines
lib/librte_vhost/virtio-net.c +7
-2@@ -152,10 +152,14 @@ cleanup_device(struct virtio_net *dev, int destroy)static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq = dev->virtqueue[i *VIRTIO_QNUM ]; +rte_free (vq->shadow_used_ring); +rte_free (vq); + }rte_free (dev); }@@ -418,13 +422,18 @@ int vhost_set_vring_num(int vid, struct vhost_vring_state *state) { struct virtio_net *dev; +struct vhost_virtqueue *vq; dev =get_device (vid);
lib/librte_vhost/virtio-net.c int · line 436
Quoted 10 lines
lib/librte_vhost/virtio-net.c +5
-1if (dev ==NULL )return -1 ;/* State->index refers to the queue index. The txq is 1, rxq is 0. */ - dev->virtqueue[state->index]->size = state->num; + vq = dev->virtqueue[state->index]; + vq->size = state->num; + vq->shadow_used_ring =rte_malloc ("" , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE );
Few notes here:
- I think the typical way to not specific a string type is using NULL, but not "".
- You should check the return value of rte_malloc: it could fail.
- Note that free_device() is invoked only when the vhost-user connection is broken (say the guest is halt). However, vhost_set_vring_num() could be invoked many times for a connection, say when you restart testpmd many times. This would lead to memory leak.
The right way is to free it on get_vring_base().
--yliu
Re: [PATCH v3 4/5] vhost: batch update used ring
Quoted 58 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Thursday, August 25, 2016 11:48 AM
To: Wang, Zhihong <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v3 4/5] vhost: batch update used ringQuoted 3 lines · Zhihong Wang
This patch enables batch update of the used ring for better efficiency.
Signed-off-by Zhihong Wang <[email protected]>...
lib/librte_vhost/virtio-net.c cleanup_device(struct virtio_net *dev, int
Quoted 5 lines
lib/librte_vhost/virtio-net.c +0
-0@@ -152,10 +152,14 @@ cleanup_device(struct virtio_net *dev, intdestroy)
Quoted 22 lines
lib/librte_vhost/virtio-net.c +7
-2static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq = dev->virtqueue[i *VIRTIO_QNUM ]; +rte_free (vq->shadow_used_ring); +rte_free (vq); + }rte_free (dev); }@@ -418,13 +422,18 @@ int vhost_set_vring_num(int vid, struct vhost_vring_state *state) { struct virtio_net *dev; +struct vhost_virtqueue *vq; dev =get_device (vid);lib/librte_vhost/virtio-net.c int · line 436
Quoted 10 lines
lib/librte_vhost/virtio-net.c +5
-1if (dev ==NULL )return -1 ;/* State->index refers to the queue index. The txq is 1, rxq is 0. */ - dev->virtqueue[state->index]->size = state->num; + vq = dev->virtqueue[state->index]; + vq->size = state->num; + vq->shadow_used_ring =rte_malloc ("" , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE );Few notes here:
- I think the typical way to not specific a string type is using NULL, but not "".
Quoted 8 lines
- You should check the return value of
rte_malloc: it could fail.- Note that
free_device()is invoked only when the vhost-user connection is broken (say the guest is halt). However,vhost_set_vring_num()could be invoked many times for a connection, say when you restart testpmd many times. This would lead to memory leak.The right way is to free it on
get_vring_base().
Good catch! Thanks.
Quoted 2 lines
--yliu
PATCH v3 5/5 vhost: optimize cache access
In reply to Zhihong WangThis patch reorders the code to delay virtio header write to optimize cache access efficiency for cases where the mrg_rxbuf feature is turned on. It reduces CPU pipeline stall cycles significantly.
---
Changes in v3:
1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
--- lib/librte_vhost/vhost_rxtx.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
lib/librte_vhost/vhost_rxtx.c +14 -5
@@ -154,6 +154,7 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t mbuf_len =0 ;uint32_t mbuf_avail =0 ;uint32_t copy_len =0 ; +uint32_t copy_virtio_hdr =0 ;uint32_t extra_buffers =0 ;/* start with the first mbuf of the packet */
@@ -168,15 +169,16 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,if (unlikely (!desc_addr)) goto error; -/* handle virtio header */ +/* +* handle virtio header, the actual write operation +* is delayed for cache optimization. +*/ virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; -virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); + copy_virtio_hdr =1 ;vhost_log_write (dev, desc->addr, dev->vhost_hlen); desc_offset = dev->vhost_hlen; desc_chain_len = desc_offset; desc_addr += desc_offset; -if (is_mrg_rxbuf) - virtio_hdr->num_buffers =1 ;/* start copy from mbuf to desc */ while (1 ) {
@@ -228,8 +230,15 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq, goto rollback; } -/* copy mbuf data */ +/* copy virtio header and mbuf data */ copy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +if (copy_virtio_hdr) { + copy_virtio_hdr =0 ; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers = extra_buffers +1 ; + } + rte_memcpy((void *)(uintptr_t)desc_addr, rte_pktmbuf_mtod_offset(mbuf, void *, mbuf_len - mbuf_avail),
Signature · 1 line
2.7.4
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Zhihong WangHi Zhihong,
Quoted 15 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed for high performance and good maintainability, and improves CPU efficiency significantly by optimizing cache access, which means:
* For fast frontends (eg. DPDK virtio pmd), higher performance (maximum throughput) can be achieved.
* For slow frontends (eg. kernel virtio-net), better scalability can be achieved, each vhost core can support more connections since it takes less cycles to handle each single frontend.
The main optimization techniques are:
Quoted 7 lines
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Thanks for these details, this is helpful to understand where the perf gain comes from.
I would suggest to add these information as comments in the code where/if it makes sense. If more a general comment, at least add it in the commit message of the patch introducing it.
Indeed, adding it to the cover letter is fine, but the information is lost as soon as the series is applied.
You don't mention any figures, so I set up a benchmark on my side to evaluate your series. It indeed shows an interesting performance gain.
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible using pktgen-dpdk. The hosts forwards received packets back to the guest using testpmd on vhost pmd interface. Guest's vCPUs are pinned to physical CPUs.
I tested it with and without your v1 patch, with and without rx-mergeable feature turned ON.
Results are the average of 8 runs of 60 seconds:
Rx-Mergeable ON : 7.72Mpps
Rx-Mergeable ON + "vhost: optimize enqueue" v1: 9.19Mpps Rx-Mergeable OFF: 10.52Mpps
Rx-Mergeable OFF + "vhost: optimize enqueue" v1: 10.60Mpps
Regards,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 45 lines · Maxime Coquelin
Hi Zhihong,
Quoted 17 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed
for high performance and good maintainability, and improves CPU efficiency
significantly by optimizing cache access, which means:* For fast frontends (eg. DPDK virtio pmd), higher performance (maximum throughput) can be achieved.
* For slow frontends (eg. kernel virtio-net), better scalability can be achieved, each vhost core can support more connections since it takes less cycles to handle each single frontend.
The main optimization techniques are:
Quoted 7 lines
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Thanks for these details, this is helpful to understand where the perf gain comes from.
I would suggest to add these information as comments in the code where/if it makes sense. If more a general comment, at least add it in the commit message of the patch introducing it.
Indeed, adding it to the cover letter is fine, but the information is lost as soon as the series is applied.You don't mention any figures, so I set up a benchmark on my side to evaluate your series. It indeed shows an interesting performance gain.
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible using pktgen-dpdk. The hosts forwards received packets back to the guest using testpmd on vhost pmd interface. Guest's vCPUs are pinned to physical CPUs.
Quoted 9 lines
I tested it with and without your v1 patch, with and without rx-mergeable feature turned ON.
Results are the average of 8 runs of 60 seconds:Rx-Mergeable ON : 7.72Mpps
Rx-Mergeable ON + "vhost: optimize enqueue" v1: 9.19Mpps Rx-Mergeable OFF: 10.52Mpps
Rx-Mergeable OFF + "vhost: optimize enqueue" v1: 10.60Mpps
I forgot to add that before this series, I think we should first fix the windows bug. Else we will need a dedicated fix for the stable branch.
Regards,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
2016-08-22 12:01, Maxime Coquelin:
Quoted 2 lines
I forgot to add that before this series, I think we should first fix the windows bug. Else we will need a dedicated fix for the stable branch.
This is a funny situation :)
If Zhihong had reworked the code without mentioning it is fixing a scenario with Windows guests, maybe that nobody would have notice ;) That's probably why it is not written in v2/v3. But thanks to the v1, we all know it:
"It also fixes the issue working with Windows VMs."
So yes, it would be a lot better to find the root cause and try to have a minimal fix for 16.07, then rework the code for performance in 16.11. I think we must avoid silent fixes, and even more, avoid writing specific fixes for stable branches without validating them in the master branch and its large users base.
Thanks for your good works guys, DPDK vhost is improving very well.
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 12 lines
-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Monday, August 22, 2016 6:35 PM
To: Maxime Coquelin <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueue2016-08-22 12:01, Maxime Coquelin:
Quoted 1 line
I forgot to add that before this series, I think we should first fix the
Quoted 8 lines
windows bug.
Quoted 1 line
Else we will need a dedicated fix for the stable branch.
This is a funny situation :)
If Zhihong had reworked the code without mentioning it is fixing a scenario with Windows guests, maybe that nobody would have notice ;) That's probably why it is not written in v2/v3. But thanks to the v1, we all know it:
"It also fixes the issue working with Windows VMs."
I thought it'd be more appropriate to send a dedicated fix for stable branch. So I removed this info.
Quoted 6 lines
So yes, it would be a lot better to find the root cause and try to have a minimal fix for 16.07, then rework the code for performance in 16.11. I think we must avoid silent fixes, and even more, avoid writing specific fixes for stable branches without validating them in the master branch and its large users base.
Okay, that's also what Maxime and Yuanhan suggest.
BTW the root cause has been identified and fix will be in v4.
Quoted 2 lines
Thanks for your good works guys, DPDK vhost is improving very well.
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Maxime CoquelinQuoted 61 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, August 22, 2016 6:02 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v3 0/5] vhost: optimize enqueueQuoted 12 lines · Maxime Coquelin
Hi Zhihong,
Quoted 9 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed
for high performance and good maintainability, and improves CPU efficiency
significantly by optimizing cache access, which means:* For fast frontends (eg. DPDK virtio pmd), higher performance
(maximum
Quoted 37 lines
Quoted 8 lines
throughput) can be achieved.
* For slow frontends (eg. kernel virtio-net), better scalability can be achieved, each vhost core can support more connections since it takes less cycles to handle each single frontend.
The main optimization techniques are:
Quoted 7 lines
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Thanks for these details, this is helpful to understand where the perf gain comes from.
I would suggest to add these information as comments in the code where/if it makes sense. If more a general comment, at least add it in the commit message of the patch introducing it.
Indeed, adding it to the cover letter is fine, but the information is lost as soon as the series is applied.You don't mention any figures, so I set up a benchmark on my side to evaluate your series. It indeed shows an interesting performance gain.
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible using pktgen-dpdk. The hosts forwards received packets back to the guest using testpmd on vhost pmd interface. Guest's vCPUs are pinned to physical CPUs.I tested it with and without your v1 patch, with and without rx-mergeable feature turned ON.
Results are the average of 8 runs of 60 seconds:
Quoted 7 lines
Quoted 5 lines
Rx-Mergeable ON : 7.72Mpps
Rx-Mergeable ON + "vhost: optimize enqueue" v1: 9.19Mpps Rx-Mergeable OFF: 10.52Mpps
Rx-Mergeable OFF + "vhost: optimize enqueue" v1: 10.60MppsI forgot to add that before this series, I think we should first fix the windows bug. Else we will need a dedicated fix for the stable branch.
Okay I'll try to fix it, though I can't make any promises at present.
Have tried once but stopped since we don't have enough debug info from the frontend side so basically I was debugging the backend based on guesses.
Quoted 3 lines
Regards,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 75 lines
-----Original Message-----
From: Wang, Zhihong
Sent: Tuesday, August 23, 2016 10:31 AM
To: Maxime Coquelin <[email protected]>; [email protected]
Cc: [email protected]
Subject: RE: [PATCH v3 0/5] vhost: optimize enqueueQuoted 64 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, August 22, 2016 6:02 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v3 0/5] vhost: optimize enqueueQuoted 12 lines · Maxime Coquelin
Hi Zhihong,
Quoted 9 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed
for high performance and good maintainability, and improves CPU efficiency
significantly by optimizing cache access, which means:* For fast frontends (eg. DPDK virtio pmd), higher performance
(maximum
Quoted 42 lines
Quoted 8 lines
throughput) can be achieved.
* For slow frontends (eg. kernel virtio-net), better scalability can be achieved, each vhost core can support more connections since it takes less cycles to handle each single frontend.
The main optimization techniques are:
Quoted 7 lines
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Thanks for these details, this is helpful to understand where the perf gain comes from.
I would suggest to add these information as comments in the code where/if it makes sense. If more a general comment, at least add it in the commit message of the patch introducing it.
Indeed, adding it to the cover letter is fine, but the information is lost as soon as the series is applied.You don't mention any figures, so I set up a benchmark on my side to evaluate your series. It indeed shows an interesting performance gain.
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible using pktgen-dpdk. The hosts forwards received packets back to the guest using testpmd on vhost pmd interface. Guest's vCPUs are pinned to physical CPUs.I tested it with and without your v1 patch, with and without rx-mergeable feature turned ON.
Results are the average of 8 runs of 60 seconds:Rx-Mergeable ON : 7.72Mpps
Rx-Mergeable ON + "vhost: optimize enqueue" v1: 9.19Mpps Rx-Mergeable OFF: 10.52Mpps
Rx-Mergeable OFF + "vhost: optimize enqueue" v1: 10.60Mpps
Quoted 8 lines
Quoted 1 line
I forgot to add that before this series, I think we should first fix the windows
bug.
Quoted 1 line
Else we will need a dedicated fix for the stable branch.
Okay I'll try to fix it, though I can't make any promises at present.
Have tried once but stopped since we don't have enough debug info from the frontend side so basically I was debugging the backend based on guesses.
Hi Maxime, Yuanhan,
I've identified the root cause, do you think it makes sense to put the fix in the same patch set? Or send it as a separated patch?
Thanks
Zhihong
Quoted 5 lines
Quoted 5 lines
Regards,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 6 lines · Zhihong
Quoted 6 lines
Quoted 1 line
I forgot to add that before this series, I think we should first fix the windows
bug.
Quoted 1 line
Else we will need a dedicated fix for the stable branch.
Okay I'll try to fix it, though I can't make any promises at present.
Quoted 7 lines
Quoted 2 lines
Have tried once but stopped since we don't have enough debug info from the frontend side so basically I was debugging the backend based on guesses.
Hi Maxime, Yuanhan,
I've identified the root cause, do you think it makes sense to put the fix in the same patch set? Or send it as a separated patch?
Great!
Yes, it's okay to put it in the patch set (normally, as the first patch, before the rewrite).
Please also add following line before your Signed-off-by in the commit log:
Cc: <[email protected]> --yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Wang, ZhihongQuoted 17 lines · Zhihong
Quoted 17 lines
-----Original Message-----
From: Wang, Zhihong
Sent: Tuesday, August 23, 2016 10:31 AM
To: Maxime Coquelin <[email protected]>; [email protected]
Cc: [email protected]
Subject: RE: [PATCH v3 0/5] vhost: optimize enqueueQuoted 6 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, August 22, 2016 6:02 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v3 0/5] vhost: optimize enqueue
..
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 1 line
I forgot to add that before this series, I think we should first fix the windows
bug.
Quoted 1 line
Else we will need a dedicated fix for the stable branch.
Okay I'll try to fix it, though I can't make any promises at present.
Quoted 7 lines
Quoted 2 lines
Have tried once but stopped since we don't have enough debug info from the frontend side so basically I was debugging the backend based on guesses.
Hi Maxime, Yuanhan,
I've identified the root cause, do you think it makes sense to put the fix in the same patch set? Or send it as a separated patch?
Good work!
Agree with Yuanhan, send it before the optimization series.
Thanks,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Maxime CoquelinQuoted 4 lines
Subject: Re: [PATCH v3 0/5] vhost: optimize enqueue
Hi Zhihong,
[...]
Quoted 10 lines
Quoted 9 lines
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Quoted 7 lines
Thanks for these details, this is helpful to understand where the perf gain comes from.
I would suggest to add these information as comments in the code where/if it makes sense. If more a general comment, at least add it in the commit message of the patch introducing it.
Indeed, adding it to the cover letter is fine, but the information is lost as soon as the series is applied.
Hi Maxime,
I did add these info in the later optimization patches to explain each optimization techniques. The v1 was indeed hard to read.
Quoted 10 lines
You don't mention any figures, so I set up a benchmark on my side to evaluate your series. It indeed shows an interesting performance gain.
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible using pktgen-dpdk. The hosts forwards received packets back to the guest using testpmd on vhost pmd interface. Guest's vCPUs are pinned to physical CPUs.
Thanks for doing the test!
I didn't publish any numbers since the gain varies in different platforms and test setups.
In my phy to vm test on both IVB and HSW, where testpmd in the host rx from the nic and enqueue to the guest, the enqueue efficiency (cycles per packet) is 2.4x and 1.4x as fast as the current code for mergeable on and mergeable off respectively, for v3 patch.
Quoted 11 lines
I tested it with and without your v1 patch, with and without rx-mergeable feature turned ON.
Results are the average of 8 runs of 60 seconds:Rx-Mergeable ON : 7.72Mpps
Rx-Mergeable ON + "vhost: optimize enqueue" v1: 9.19Mpps Rx-Mergeable OFF: 10.52Mpps
Rx-Mergeable OFF + "vhost: optimize enqueue" v1: 10.60MppsRegards,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Maxime CoquelinHi Maxime,
Quoted 6 lines · Maxime Coquelin
Hi Zhihong,
Quoted 3 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
...
Quoted 3 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible using
Have you tested with other different packet size?
My testing shows that performance is dropping when packet size is more than 256.
Quoted 15 lines
pktgen-dpdk. The hosts forwards received packets back to the guest using testpmd on vhost pmd interface. Guest's vCPUs are pinned to physical CPUs.
I tested it with and without your v1 patch, with and without rx-mergeable feature turned ON.
Results are the average of 8 runs of 60 seconds:Rx-Mergeable ON : 7.72Mpps
Rx-Mergeable ON + "vhost: optimize enqueue" v1: 9.19Mpps Rx-Mergeable OFF: 10.52Mpps
Rx-Mergeable OFF + "vhost: optimize enqueue" v1: 10.60MppsRegards,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 23 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Wednesday, September 21, 2016 4:50 PM
To: Maxime Coquelin <[email protected]>
Cc: Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueHi Maxime,
Quoted 6 lines · Maxime Coquelin
Hi Zhihong,
Quoted 3 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
...
Quoted 1 line
Quoted 6 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size is more than 256.
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
3. How many percentage of drop are you seeing?
This is expected by me because I've already found the root cause and the way to optimize it, but since it missed the v0 deadline and requires changes in eal/memcpy, I postpone it to the next release.
After the upcoming optimization the performance for packets larger than 256 will be improved, and the new code will be much faster than the current code.
Thanks
Zhihong
Quoted 16 lines
Quoted 16 lines
pktgen-dpdk. The hosts forwards received packets back to the guest using testpmd on vhost pmd interface. Guest's vCPUs are pinned to physical CPUs.
I tested it with and without your v1 patch, with and without rx-mergeable feature turned ON.
Results are the average of 8 runs of 60 seconds:Rx-Mergeable ON : 7.72Mpps
Rx-Mergeable ON + "vhost: optimize enqueue" v1: 9.19Mpps Rx-Mergeable OFF: 10.52Mpps
Rx-Mergeable OFF + "vhost: optimize enqueue" v1: 10.60MppsRegards,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 42 lines · Zhihong
Quoted 23 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Wednesday, September 21, 2016 4:50 PM
To: Maxime Coquelin <[email protected]>
Cc: Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueHi Maxime,
Quoted 6 lines · Maxime Coquelin
Hi Zhihong,
Quoted 3 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
...
Quoted 1 line
Quoted 6 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size is more than 256.Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
3. How many percentage of drop are you seeing?
Quoted 8 lines
This is expected by me because I've already found the root cause and the way to optimize it, but since it missed the v0 deadline and requires changes in eal/memcpy, I postpone it to the next release.
After the upcoming optimization the performance for packets larger than 256 will be improved, and the new code will be much faster than the current code.
Sorry, I tested on an ARM server, but I wonder if there is the same issue for x86 platform.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
pktgen-dpdk. The hosts forwards received packets back to the guest using testpmd on vhost pmd interface. Guest's vCPUs are pinned to physical CPUs.
I tested it with and without your v1 patch, with and without rx-mergeable feature turned ON.
Results are the average of 8 runs of 60 seconds:Rx-Mergeable ON : 7.72Mpps
Rx-Mergeable ON + "vhost: optimize enqueue" v1: 9.19Mpps Rx-Mergeable OFF: 10.52Mpps
Rx-Mergeable OFF + "vhost: optimize enqueue" v1: 10.60Mpps
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 58 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Wednesday, September 21, 2016 8:54 PM
To: Wang, Zhihong <[email protected]>
Cc: Maxime Coquelin <[email protected]>; [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 46 lines · Zhihong
Quoted 23 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Wednesday, September 21, 2016 4:50 PM
To: Maxime Coquelin <[email protected]>
Cc: Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueHi Maxime,
Quoted 6 lines · Maxime Coquelin
Hi Zhihong,
Quoted 3 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
...
Quoted 1 line
Quoted 6 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size is more than 256.Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
3. How many percentage of drop are you seeing?
This is expected by me because I've already found the root cause and the way to optimize it, but since it missed the v0 deadline and requires changes in eal/memcpy, I postpone it to the next release.
Quoted 7 lines
Quoted 4 lines
After the upcoming optimization the performance for packets larger than 256 will be improved, and the new code will be much faster than the current code.
Sorry, I tested on an ARM server, but I wonder if there is the same issue for x86 platform.
For mrg_rxbuf=off path it might be slight drop for packets larger than 256B (~3% for 512B and ~1% for 1024B), no drop for other cases.
This is not a bug or issue, only we need to enhance memcpy to complete the whole optimization, which should be done in a separated patch, unfortunately it misses this release window.
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
pktgen-dpdk. The hosts forwards received packets back to the guest using testpmd on vhost pmd interface. Guest's vCPUs are pinned to physical CPUs.
I tested it with and without your v1 patch, with and without rx-mergeable feature turned ON.
Results are the average of 8 runs of 60 seconds:Rx-Mergeable ON : 7.72Mpps
Rx-Mergeable ON + "vhost: optimize enqueue" v1: 9.19Mpps Rx-Mergeable OFF: 10.52Mpps
Rx-Mergeable OFF + "vhost: optimize enqueue" v1: 10.60Mpps
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Jianbo LiuQuoted 23 lines · Jianbo Liu
Quoted 23 lines
Quoted 6 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size is more than 256.Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
3. How many percentage of drop are you seeing?
This is expected by me because I've already found the root cause and the way to optimize it, but since it missed the v0 deadline and requires changes in eal/memcpy, I postpone it to the next release.
Quoted 7 lines
Quoted 4 lines
After the upcoming optimization the performance for packets larger than 256 will be improved, and the new code will be much faster than the current code.
Sorry, I tested on an ARM server, but I wonder if there is the same issue for x86 platform.
Would you please provide more details? Say, answer the two left questions from Zhihong?
Thanks.
--yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 9 lines · Yuanhan Liu
Quoted 8 lines · Jianbo Liu
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size is more than 256.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.
Quoted 19 lines
Quoted 5 lines
Quoted 5 lines
This is expected by me because I've already found the root cause and the way to optimize it, but since it missed the v0 deadline and requires changes in eal/memcpy, I postpone it to the next release.
Quoted 7 lines
Quoted 4 lines
After the upcoming optimization the performance for packets larger than 256 will be improved, and the new code will be much faster than the current code.
Sorry, I tested on an ARM server, but I wonder if there is the same issue for x86 platform.
Would you please provide more details? Say, answer the two left questions from Zhihong?
Thanks.
--yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 36 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 1:48 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 6 lines · Yuanhan Liu
Quoted 5 lines · Jianbo Liu
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size ismore
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
than 256.
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.
Jianbo,
Could you please verify does this patch really cause enqueue perf to drop?
You can test the enqueue path only by set guest to do rxonly, and compare the mpps by show port stats all in the guest.
Thanks
Zhihong
Quoted 20 lines
Quoted 20 lines
Quoted 6 lines
Quoted 6 lines
This is expected by me because I've already found the root cause and the way to optimize it, but since it missed the v0 deadline and requires changes in eal/memcpy, I postpone it to the next release.
Quoted 7 lines
Quoted 4 lines
After the upcoming optimization the performance for packets larger than 256 will be improved, and the new code will be much faster than the current code.
Sorry, I tested on an ARM server, but I wonder if there is the same issue for x86 platform.
Would you please provide more details? Say, answer the two left questions from Zhihong?
Thanks.
--yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 47 lines · Zhihong
Quoted 36 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 1:48 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 6 lines · Yuanhan Liu
Quoted 5 lines · Jianbo Liu
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size ismore
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
than 256.
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.
Quoted 8 lines
Jianbo,
Could you please verify does this patch really cause enqueue perf to drop?
You can test the enqueue path only by set guest to do rxonly, and compare the mpps by show port stats all in the guest.
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)
64 4.12 128 6 256 2.65 512 -1.12
1024 -7.02
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 68 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 5:02 PM
To: Wang, Zhihong <[email protected]>
Cc: Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 11 lines · Zhihong
Quoted 11 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 1:48 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueOn 22 September 2016 at 10:29, Yuanhan Liu
Quoted 36 lines
Quoted 25 lines
Quoted 6 lines · wrote:
Quoted 5 lines · Jianbo Liu
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size ismore
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
than 256.
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.Quoted 8 lines
Jianbo,
Could you please verify does this patch really cause enqueue perf to drop?
You can test the enqueue path only by set guest to do rxonly, and compare the mpps by show port stats all in the guest.
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
I think your number is little bit hard to understand for me, this patch's optimization contains 2 parts:
1. ring operation: works for both mrg_rxbuf on and off
2. remote write ordering: works for mrg_rxbuf=on only
So, for mrg_rxbuf=off, if this patch is good for 64B packets, then it shouldn't do anything bad for larger packets.
This is the gain on x86 platform: host iofwd between nic and vhost, guest rxonly.
nic2vm enhancement
64 21.83%
128 16.97%
256 6.34%
512 0.01%
1024 0.00%
I suspect there's some complication in ARM's micro-arch.
Could you try v6 and apply all patches except the the last one:
[PATCH v6 6/6] vhost: optimize cache access
And see if there's still perf drop?
Thanks
Zhihong
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 91 lines · Zhihong
Quoted 68 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 5:02 PM
To: Wang, Zhihong <[email protected]>
Cc: Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 11 lines · Zhihong
Quoted 11 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 1:48 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueOn 22 September 2016 at 10:29, Yuanhan Liu
Quoted 36 lines
Quoted 25 lines
Quoted 6 lines · wrote:
Quoted 5 lines · Jianbo Liu
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size ismore
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
than 256.
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.Quoted 8 lines
Jianbo,
Could you please verify does this patch really cause enqueue perf to drop?
You can test the enqueue path only by set guest to do rxonly, and compare the mpps by show port stats all in the guest.
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
I think your number is little bit hard to understand for me, this patch's optimization contains 2 parts:
1. ring operation: works for both
mrg_rxbufon and off2. remote write ordering: works for mrg_rxbuf=on only
So, for mrg_rxbuf=off, if this patch is good for 64B packets, then it shouldn't do anything bad for larger packets.
This is the gain on x86 platform: host iofwd between nic and vhost, guest rxonly.
Quoted 7 lines
nic2vm enhancement 64 21.83% 128 16.97% 256 6.34% 512 0.01% 1024 0.00%
I bootup a VM with 2 virtual port, and stress the traffic between them. First, I stressed with pktgen-dpdk in VM, and did iofwd in host. Then, as you told, I did rxonly in VM, and txonly in host.
Quoted 7 lines
I suspect there's some complication in ARM's micro-arch.
Could you try v6 and apply all patches except the the last one:
[PATCH v6 6/6] vhost: optimize cache accessAnd see if there's still perf drop?
The last patch can improve the performance. The drop is actually caused by the second patch.
Jianbo
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 116 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 10:42 PM
To: Wang, Zhihong <[email protected]>
Cc: Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 91 lines · Zhihong
Quoted 68 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 5:02 PM
To: Wang, Zhihong <[email protected]>
Cc: Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 11 lines · Zhihong
Quoted 11 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 1:48 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueOn 22 September 2016 at 10:29, Yuanhan Liu
Quoted 36 lines
Quoted 25 lines
Quoted 6 lines · wrote:
Quoted 5 lines · Jianbo Liu
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size ismore
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
than 256.
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.Quoted 8 lines
Jianbo,
Could you please verify does this patch really cause enqueue perf to drop?
You can test the enqueue path only by set guest to do rxonly, and compare the mpps by show port stats all in the guest.
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
I think your number is little bit hard to understand for me, this patch's optimization contains 2 parts:
1. ring operation: works for both
mrg_rxbufon and off2. remote write ordering: works for mrg_rxbuf=on only
So, for mrg_rxbuf=off, if this patch is good for 64B packets, then it shouldn't do anything bad for larger packets.
This is the gain on x86 platform: host iofwd between nic and vhost, guest rxonly.
Quoted 7 lines
nic2vm enhancement 64 21.83% 128 16.97% 256 6.34% 512 0.01% 1024 0.00%I bootup a VM with 2 virtual port, and stress the traffic between them. First, I stressed with pktgen-dpdk in VM, and did iofwd in host. Then, as you told, I did rxonly in VM, and txonly in host.
Quoted 2 lines
I suspect there's some complication in ARM's micro-arch.
Quoted 7 lines
Quoted 5 lines
Could you try v6 and apply all patches except the the last one:
[PATCH v6 6/6] vhost: optimize cache accessAnd see if there's still perf drop?
The last patch can improve the performance. The drop is actually caused by the second patch.
This is expected because the 2nd patch is just a baseline and all optimization patches are organized in the rest of this patch set.
I think you can do bottleneck analysis on ARM to see what's slowing down the perf, there might be some micro-arch complications there, mostly likely in memcpy.
Do you use glibc's memcpy? I suggest to hand-crafted it on your own.
Could you publish the mrg_rxbuf=on data also? Since it's more widely used in terms of spec integrity.
Thanks
Zhihong
Quoted 2 lines
Jianbo
Re: [PATCH v3 0/5] vhost: optimize enqueue
On 23 September 2016 at 10:56, Wang, Zhihong <[email protected]> wrote:
.....
Quoted 12 lines
This is expected because the 2nd patch is just a baseline and all optimization patches are organized in the rest of this patch set.
I think you can do bottleneck analysis on ARM to see what's slowing down the perf, there might be some micro-arch complications there, mostly likely in memcpy.
Do you use glibc's memcpy? I suggest to hand-crafted it on your own.
Could you publish the mrg_rxbuf=on data also? Since it's more widely used in terms of spec integrity.
I don't think it will be helpful for you, considering the differences between x86 and arm.
So please move on with this patchset...
Thanks!
Jianbo
Re: [PATCH v3 0/5] vhost: optimize enqueue
2016-09-23 18:41, Jianbo Liu:
Quoted 9 lines · Zhihong
.....
Quoted 7 lines
This is expected because the 2nd patch is just a baseline and all optimization patches are organized in the rest of this patch set.
I think you can do bottleneck analysis on ARM to see what's slowing down the perf, there might be some micro-arch complications there, mostly likely in memcpy.
Quoted 8 lines
Quoted 5 lines
Do you use glibc's memcpy? I suggest to hand-crafted it on your own.
Could you publish the mrg_rxbuf=on data also? Since it's more widely used in terms of spec integrity.
I don't think it will be helpful for you, considering the differences between x86 and arm.
So please move on with this patchset...
Jianbo,
I don't understand.
You said that the 2nd patch is a regression:
- volatile uint16_t last_used_idx; + uint16_t last_used_idx;
And the overrall series lead to performance regression for packets > 512 B, right?
But we don't know wether you have tested the v6 or not.
Zhihong talked about some improvements possible in rte_memcpy. ARM64 is using libc memcpy in rte_memcpy.
Now you seem to give up.
Does it mean you accept having a regression in 16.11 release? Are you working on rte_memcpy?
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 11 lines
-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Friday, September 23, 2016 9:41 PM
To: Jianbo Liu <[email protected]>
Cc: [email protected]; Wang, Zhihong <[email protected]>; Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueue
Quoted 18 lines
2016-09-23 18:41, Jianbo Liu:
Quoted 1 line
On 23 September 2016 at 10:56, Wang, Zhihong <[email protected]>
Quoted 15 lines · wrote:
.....
Quoted 12 lines
This is expected because the 2nd patch is just a baseline and all optimization patches are organized in the rest of this patch set.
I think you can do bottleneck analysis on ARM to see what's slowing down the perf, there might be some micro-arch complications there, mostly likely in memcpy.
Do you use glibc's memcpy? I suggest to hand-crafted it on your own.
Could you publish the mrg_rxbuf=on data also? Since it's more widely used in terms of spec integrity.
I don't think it will be helpful for you, considering the differences between x86 and arm.
Hi Jianbo,
This patch does help in ARM for small packets like 64B sized ones, this actually proves the similarity between x86 and ARM in terms of caching optimization in this patch.
My estimation is based on:
1. The last patch are for mrg_rxbuf=on, and since you said it helps perf, we can ignore it for now when we discuss mrg_rxbuf=off
2. Vhost enqueue perf =
Ring overhead + Virtio header overhead + Data memcpy overhead
3. This patch helps small packets traffic, which means it helps ring + virtio header operations
4. So, when you say perf drop when packet size larger than 512B, this is most likely caused by memcpy in ARM not working well with this patch
I'm not saying glibc's memcpy is not good enough, it's just that this is a rather special use case. And since we see specialized memcpy + this patch give better performance than other combinations significantly on x86, we suggest to hand-craft a specialized memcpy for it.
Of course on ARM this is still just my speculation, and we need to either prove it or find the actual root cause.
It can be **REALLY HELPFUL** if you could help to test this patch on ARM for mrg_rxbuf=on cases to see if this patch is in fact helpful to ARM at all, since mrg_rxbuf=on the more widely used cases.
Thanks
Zhihong
Quoted 18 lines
Quoted 1 line
So please move on with this patchset...
Jianbo,
I don't understand.
You said that the 2nd patch is a regression:(quoted diff) +1
-1- volatile uint16_t last_used_idx; + uint16_t last_used_idx;And the overrall series lead to performance regression for packets > 512 B, right?
But we don't know wether you have tested the v6 or not.Zhihong talked about some improvements possible in
rte_memcpy. ARM64 is using libc memcpy inrte_memcpy.Now you seem to give up.
Does it mean you accept having a regression in 16.11 release? Are you working onrte_memcpy?
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 9 lines · Zhihong
Quoted 9 lines
-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Friday, September 23, 2016 9:41 PM
To: Jianbo Liu <[email protected]>
Cc: [email protected]; Wang, Zhihong <[email protected]>; Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>
....
Quoted 25 lines
This patch does help in ARM for small packets like 64B sized ones, this actually proves the similarity between x86 and ARM in terms of caching optimization in this patch.
My estimation is based on:
1. The last patch are for mrg_rxbuf=on, and since you said it helps perf, we can ignore it for now when we discuss mrg_rxbuf=off
2. Vhost enqueue perf =
Ring overhead + Virtio header overhead + Data memcpy overhead3. This patch helps small packets traffic, which means it helps ring + virtio header operations
4. So, when you say perf drop when packet size larger than 512B, this is most likely caused by memcpy in ARM not working well with this patch
I'm not saying glibc's memcpy is not good enough, it's just that this is a rather special use case. And since we see specialized memcpy + this patch give better performance than other combinations significantly on x86, we suggest to hand-craft a specialized memcpy for it.
Quoted 7 lines
Of course on ARM this is still just my speculation, and we need to either prove it or find the actual root cause.
It can be **REALLY HELPFUL** if you could help to test this patch on ARM for mrg_rxbuf=on cases to see if this patch is in fact helpful to ARM at all, since mrg_rxbuf=on the more widely used cases.
Actually it's worse than mrg_rxbuf=off.
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 48 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Monday, September 26, 2016 1:13 PM
To: Wang, Zhihong <[email protected]>
Cc: Thomas Monjalon <[email protected]>; [email protected]; Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 9 lines · Zhihong
Quoted 9 lines
-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Friday, September 23, 2016 9:41 PM
To: Jianbo Liu <[email protected]>
Cc: [email protected]; Wang, Zhihong <[email protected]>; Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>....
Quoted 25 lines
This patch does help in ARM for small packets like 64B sized ones, this actually proves the similarity between x86 and ARM in terms of caching optimization in this patch.
My estimation is based on:
1. The last patch are for mrg_rxbuf=on, and since you said it helps perf, we can ignore it for now when we discuss mrg_rxbuf=off
2. Vhost enqueue perf =
Ring overhead + Virtio header overhead + Data memcpy overhead3. This patch helps small packets traffic, which means it helps ring + virtio header operations
4. So, when you say perf drop when packet size larger than 512B, this is most likely caused by memcpy in ARM not working well with this patch
I'm not saying glibc's memcpy is not good enough, it's just that this is a rather special use case. And since we see specialized memcpy + this patch give better performance than other combinations significantly on x86, we suggest to hand-craft a specialized memcpy for it.
Quoted 8 lines
Quoted 7 lines
Of course on ARM this is still just my speculation, and we need to either prove it or find the actual root cause.
It can be **REALLY HELPFUL** if you could help to test this patch on ARM for mrg_rxbuf=on cases to see if this patch is in fact helpful to ARM at all, since mrg_rxbuf=on the more widely used cases.
Actually it's worse than mrg_rxbuf=off.
I mean compare the perf of original vs. original + patch with mrg_rxbuf turned on. Is there any perf improvement?
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 51 lines · Zhihong
Quoted 51 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Monday, September 26, 2016 1:13 PM
To: Wang, Zhihong <[email protected]>
Cc: Thomas Monjalon <[email protected]>; [email protected]; Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 9 lines · Zhihong
Quoted 9 lines
-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Friday, September 23, 2016 9:41 PM
To: Jianbo Liu <[email protected]>
Cc: [email protected]; Wang, Zhihong <[email protected]>; Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>....
Quoted 28 lines
This patch does help in ARM for small packets like 64B sized ones, this actually proves the similarity between x86 and ARM in terms of caching optimization in this patch.
My estimation is based on:
1. The last patch are for mrg_rxbuf=on, and since you said it helps perf, we can ignore it for now when we discuss mrg_rxbuf=off
2. Vhost enqueue perf =
Ring overhead + Virtio header overhead + Data memcpy overhead3. This patch helps small packets traffic, which means it helps ring + virtio header operations
4. So, when you say perf drop when packet size larger than 512B, this is most likely caused by memcpy in ARM not working well with this patch
I'm not saying glibc's memcpy is not good enough, it's just that this is a rather special use case. And since we see specialized memcpy + this patch give better performance than other combinations significantly on x86, we suggest to hand-craft a specialized memcpy for it.
Of course on ARM this is still just my speculation, and we need to either prove it or find the actual root cause.
Quoted 9 lines
Quoted 5 lines
Quoted 4 lines
It can be **REALLY HELPFUL** if you could help to test this patch on ARM for mrg_rxbuf=on cases to see if this patch is in fact helpful to ARM at all, since mrg_rxbuf=on the more widely used cases.
Actually it's worse than mrg_rxbuf=off.
I mean compare the perf of original vs. original + patch with
mrg_rxbufturned on. Is there any perf improvement?
Yes, orig + patch + on is better than orig + on, but orig + patch + on is worse than orig + patch + off.
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 70 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Monday, September 26, 2016 1:39 PM
To: Wang, Zhihong <[email protected]>
Cc: Thomas Monjalon <[email protected]>; [email protected]; Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 7 lines · Zhihong
Quoted 7 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Monday, September 26, 2016 1:13 PM
To: Wang, Zhihong <[email protected]>
Cc: Thomas Monjalon <[email protected]>; [email protected];Yuanhan
Quoted 8 lines
Quoted 8 lines
Liu <[email protected]>; Maxime Coquelin <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 2 lines · Zhihong
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Friday, September 23, 2016 9:41 PM
To: Jianbo Liu <[email protected]>
Cc: [email protected]; Wang, Zhihong <[email protected]>; YuanhanLiu
Quoted 35 lines
Quoted 35 lines
Quoted 2 lines
Quoted 2 lines
<[email protected]>; Maxime Coquelin
<[email protected]>....
Quoted 32 lines
This patch does help in ARM for small packets like 64B sized ones, this actually proves the similarity between x86 and ARM in terms of caching optimization in this patch.
My estimation is based on:
1. The last patch are for mrg_rxbuf=on, and since you said it helps perf, we can ignore it for now when we discuss mrg_rxbuf=off
2. Vhost enqueue perf =
Ring overhead + Virtio header overhead + Data memcpy overhead3. This patch helps small packets traffic, which means it helps ring + virtio header operations
4. So, when you say perf drop when packet size larger than 512B, this is most likely caused by memcpy in ARM not working well with this patch
I'm not saying glibc's memcpy is not good enough, it's just that this is a rather special use case. And since we see specialized memcpy + this patch give better performance than other combinations significantly on x86, we suggest to hand-craft a specialized memcpy for it.
Of course on ARM this is still just my speculation, and we need to either prove it or find the actual root cause.
It can be **REALLY HELPFUL** if you could help to test this patch on ARM for mrg_rxbuf=on cases to see if this patch is in fact helpful to ARM at all, since mrg_rxbuf=on the more widely used cases.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Actually it's worse than mrg_rxbuf=off.
I mean compare the perf of original vs. original + patch with
mrg_rxbufturned on. Is there any perf improvement?Yes, orig + patch + on is better than orig + on, but orig + patch + on is worse than orig + patch + off.
Hi Jianbo,
That's the way it is for virtio, if you compare the current enqueue, the mrg on perf is even slower.
We should compare:
1. mrg on: orig vs. orig + patch
2. mrg off: orig vs. orig + patch
There's more memory touch and in the frontend that brings down the performance when mrg is on.
Finally, even though mrg on is slower, it's still the mainstream use case as far as I know.
Thanks
Zhihong
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Thomas MonjalonHi Thomas,
Quoted 23 lines · Thomas Monjalon
2016-09-23 18:41, Jianbo Liu:
Quoted 9 lines · Zhihong
.....
Quoted 7 lines
This is expected because the 2nd patch is just a baseline and all optimization patches are organized in the rest of this patch set.
I think you can do bottleneck analysis on ARM to see what's slowing down the perf, there might be some micro-arch complications there, mostly likely in memcpy.
Quoted 8 lines
Quoted 5 lines
Do you use glibc's memcpy? I suggest to hand-crafted it on your own.
Could you publish the mrg_rxbuf=on data also? Since it's more widely used in terms of spec integrity.
I don't think it will be helpful for you, considering the differences between x86 and arm.
So please move on with this patchset...Jianbo,
I don't understand.
You said that the 2nd patch is a regression:
Review
Quoted 2 lines
(quoted diff) +1
-1- volatile uint16_t last_used_idx; + uint16_t last_used_idx;
No, I meant "vhost: rewrite enqueue".
Quoted 3 lines
And the overrall series lead to performance regression for packets > 512 B, right?
But we don't know wether you have tested the v6 or not.
Yes, I tested v6, and found performance regression for size >=512B.
Quoted 7 lines
Zhihong talked about some improvements possible in
rte_memcpy. ARM64 is using libc memcpy inrte_memcpy.Now you seem to give up.
Does it mean you accept having a regression in 16.11 release? Are you working onrte_memcpy?
This patchset actually improves performance according to Zhihong's result on x86 platfrom. And I also get improvement as least with small-size packet on ARM.
I don't want to give up, but I need more time to find out the reason for the regression. I think rte_memcpy definitely is one of the ways to improve performance, but it could be the reason?
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Jianbo LiuQuoted 8 lines · Jianbo Liu
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
Have you considered testing with more diverse workloads e.g. mixed packet sizes that are not always multiples of the cache line & register sizes?
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 11 lines · Luke Gorrie
Quoted 8 lines · Jianbo Liu
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
Quoted 3 lines
Have you considered testing with more diverse workloads e.g. mixed packet sizes that are not always multiples of the cache line & register sizes?
No. Does testpmd can stress performance with mixed size?
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Jianbo LiuQuoted 56 lines · Jianbo Liu
Quoted 47 lines · Zhihong
Quoted 36 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 1:48 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 6 lines · Yuanhan Liu
Quoted 5 lines · Jianbo Liu
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size ismore
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
than 256.
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.Quoted 8 lines
Jianbo,
Could you please verify does this patch really cause enqueue perf to drop?
You can test the enqueue path only by set guest to do rxonly, and compare the mpps by show port stats all in the guest.
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
There is a difference between Zhihong's code and the old I spotted in the first time: Zhihong removed the avail_idx prefetch. I understand the prefetch becomes a bit tricky when mrg-rx code path is considered; thus, I didn't comment on that.
That's one of the difference that, IMO, could drop a regression. I then finally got a chance to add it back.
A rough test shows it improves the performance of 1400B packet size greatly in the "txonly in host and rxonly in guest" case: +33% is the number I get with my test server (Ivybridge).
I guess this might/would help your case as well. Mind to have a test and tell me the results?
BTW, I made it in rush; I haven't tested the mrg-rx code path yet.
Thanks.
--yliu commit e5852d04bf87c02d6d0d8e6d8ded4c33030b9c9e Author: Yuanhan Liu <[email protected]> Date: Tue Sep 27 17:51:15 2016 +0800
xxxx
lib/librte_vhost/vhost.h +6 -1
@@ -61,6 +61,8 @@ struct buf_vector {uint32_t desc_idx; }; +#define NR_AVAIL_IDX_PREFETCH 32 +/** * Structure contains variables relevant to RX/TX virtqueues. */
@@ -70,7 +72,7 @@ struct vhost_virtqueue {struct vring_used *used;uint32_t size; -/* Last index used on the available ring */ +uint16_t last_avail_idx;uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
@@ -89,6 +91,9 @@ struct vhost_virtqueue {/* Shadow used ring for performance */ struct vring_used_elem *shadow_used_ring;uint32_t shadow_used_idx; + +uint16_t next_avail_idx; +uint16_t avail_idx_buf[NR_AVAIL_IDX_PREFETCH ]; } __rte_cache_aligned;/* Old kernels have no such macro defined */
lib/librte_vhost/virtio_net.c +44 -8
92 lines not on this page · raw message
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 81 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Tuesday, September 27, 2016 6:21 PM
To: Jianbo Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 1 line · Jianbo Liu
On 22 September 2016 at 14:58, Wang, Zhihong <[email protected]>
Quoted 55 lines · wrote:
Quoted 47 lines
Quoted 36 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 1:48 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 6 lines · Yuanhan Liu
Quoted 5 lines · Jianbo Liu
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size ismore
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
than 256.
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.Quoted 8 lines
Jianbo,
Could you please verify does this patch really cause enqueue perf to drop?
You can test the enqueue path only by set guest to do rxonly, and compare the mpps by show port stats all in the guest.
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
There is a difference between Zhihong's code and the old I spotted in the first time: Zhihong removed the
avail_idxprefetch. I understand the prefetch becomes a bit tricky when mrg-rx code path is considered; thus, I didn't comment on that.
Quoted 6 lines
That's one of the difference that, IMO, could drop a regression. I then finally got a chance to add it back.
A rough test shows it improves the performance of 1400B packet size greatly in the "txonly in host and rxonly in guest" case: +33% is the number I get with my test server (Ivybridge).
Thanks Yuanhan! I'll validate this on x86.
Quoted 9 lines
I guess this might/would help your case as well. Mind to have a test and tell me the results?
BTW, I made it in rush; I haven't tested the mrg-rx code path yet.
Thanks.
--yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 102 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Wang, Zhihong
Sent: Wednesday, September 28, 2016 12:45 AM
To: Yuanhan Liu <[email protected]>; Jianbo Liu <[email protected]>
Cc: Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 10 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Tuesday, September 27, 2016 6:21 PM
To: Jianbo Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 1 line · Jianbo Liu
On 22 September 2016 at 14:58, Wang, Zhihong
Quoted 11 lines
Quoted 10 lines · wrote:
Quoted 10 lines
Quoted 10 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 1:48 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
On 22 September 2016 at 10:29, Yuanhan Liu
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines · wrote:
Quoted 2 lines · Jianbo Liu
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
My setup consists of one host running a guest. The guest generates as much 64bytes packets as possible
using
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Have you tested with other different packet size? My testing shows that performance is dropping when packet
size is
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
more
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
than 256.
Hi Jianbo,
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 1 line
Quoted 1 line
Quoted 1 line
2. Could you please specify what CPU you're running? Is it
Haswell
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than512.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Jianbo,
Could you please verify does this patch really cause enqueue perf to
drop?
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
You can test the enqueue path only by set guest to do rxonly, and
compare
Quoted 19 lines
Quoted 10 lines
Quoted 3 lines
the mpps by show port stats all in the guest.
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
There is a difference between Zhihong's code and the old I spotted in the first time: Zhihong removed the
avail_idxprefetch. I understand the prefetch becomes a bit tricky when mrg-rx code path is considered; thus, I didn't comment on that.That's one of the difference that, IMO, could drop a regression. I then finally got a chance to add it back.
Quoted 6 lines
Quoted 1 line
A rough test shows it improves the performance of 1400B packet size
greatly
Quoted 2 lines
in the "txonly in host and rxonly in guest" case: +33% is the number I get with my test server (Ivybridge).
Thanks Yuanhan! I'll validate this on x86.
Hi Yuanhan,
Seems your code doesn't perform correctly. I write a new version of avail idx prefetch but didn't see any perf benefit.
To be honest I doubt the benefit of this idea. The previous mrg_off code has this method but doesn't give any benefits.
Even if this is useful, the benefits should be more significant for small packets, it's unlikely this simple idx prefetch could bring over 30% perf gain for large packets like 1400B ones.
But if you really do work it out like that I'll be very glad to see.
Thanks
Zhihong
Quoted 10 lines
Quoted 10 lines
I guess this might/would help your case as well. Mind to have a test and tell me the results?
BTW, I made it in rush; I haven't tested the mrg-rx code path yet.
Thanks.
--yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 23 lines · Zhihong
Quoted 16 lines
Quoted 16 lines
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
There is a difference between Zhihong's code and the old I spotted in the first time: Zhihong removed the
avail_idxprefetch. I understand the prefetch becomes a bit tricky when mrg-rx code path is considered; thus, I didn't comment on that.That's one of the difference that, IMO, could drop a regression. I then finally got a chance to add it back.
Quoted 6 lines
Quoted 1 line
A rough test shows it improves the performance of 1400B packet size
greatly
Quoted 2 lines
in the "txonly in host and rxonly in guest" case: +33% is the number I get with my test server (Ivybridge).
Thanks Yuanhan! I'll validate this on x86.
Quoted 7 lines
Hi Yuanhan,
Seems your code doesn't perform correctly. I write a new version of avail idx prefetch but didn't see any perf benefit.
To be honest I doubt the benefit of this idea. The previous
mrg_offcode has this method but doesn't give any benefits.
Good point. I thought of that before, too. But you know that I made it in rush, that I didn't think further and test more.
I looked the code a bit closer this time, and spotted a bug: the prefetch actually didn't happen, due to following code piece:
if (vq->next_avail_idx >= NR_AVAIL_IDX_PREFETCH) {
prefetch_avail_idx(vq);
...
}Since vq->next_avail_idx is set to 0 at the entrance of enqueue path, prefetch_avail_idx() will be called. The fix is easy though: just put prefetch_avail_idx before invoking enqueue_packet.
In summary, Zhihong is right, I see no more gains with that fix :(
However, as stated, that's kind of the only difference I found between yours and the old code, that maybe it's still worthwhile to have a test on ARM, Jianbo?
--yliu
Quoted 19 lines
Even if this is useful, the benefits should be more significant for small packets, it's unlikely this simple idx prefetch could bring over 30% perf gain for large packets like 1400B ones.
But if you really do work it out like that I'll be very glad to see.
Thanks
ZhihongQuoted 10 lines
Quoted 10 lines
I guess this might/would help your case as well. Mind to have a test and tell me the results?
BTW, I made it in rush; I haven't tested the mrg-rx code path yet.
Thanks.
--yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 47 lines · Yuanhan Liu
Quoted 23 lines · Zhihong
Quoted 16 lines
Quoted 16 lines
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
There is a difference between Zhihong's code and the old I spotted in the first time: Zhihong removed the
avail_idxprefetch. I understand the prefetch becomes a bit tricky when mrg-rx code path is considered; thus, I didn't comment on that.That's one of the difference that, IMO, could drop a regression. I then finally got a chance to add it back.
Quoted 6 lines
Quoted 1 line
A rough test shows it improves the performance of 1400B packet size
greatly
Quoted 2 lines
in the "txonly in host and rxonly in guest" case: +33% is the number I get with my test server (Ivybridge).
Thanks Yuanhan! I'll validate this on x86.
Quoted 7 lines
Hi Yuanhan,
Seems your code doesn't perform correctly. I write a new version of avail idx prefetch but didn't see any perf benefit.
To be honest I doubt the benefit of this idea. The previous
mrg_offcode has this method but doesn't give any benefits.Good point. I thought of that before, too. But you know that I made it in rush, that I didn't think further and test more.
I looked the code a bit closer this time, and spotted a bug: the prefetch actually didn't happen, due to following code piece:
if (vq->next_avail_idx >= NR_AVAIL_IDX_PREFETCH) { prefetch_avail_idx(vq); ... }Since
vq->next_avail_idxis set to 0 at the entrance of enqueue path,prefetch_avail_idx()will be called. The fix is easy though: just putprefetch_avail_idxbefore invokingenqueue_packet.
Quoted 6 lines
In summary, Zhihong is right, I see no more gains with that fix :(
However, as stated, that's kind of the only difference I found between yours and the old code, that maybe it's still worthwhile to have a test on ARM, Jianbo?
I haven't tested it, but I think it could be no improvement for ARM either.
A smalll suggestion for enqueue_packet:
.....
+ /* start copy from mbuf to desc */
+ while (mbuf_avail || mbuf->next) {
.....
Considering pkt_len is in the first cache line (same as data_len), while next pointer is in the second cache line,
is it better to check the total packet len, instead of the last mbuf's next pointer to jump out of while loop and avoid possible cache miss?
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 76 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Monday, October 10, 2016 1:32 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 9 lines · Yuanhan Liu
Quoted 8 lines · Zhihong
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
There is a difference between Zhihong's code and the old I spotted in the first time: Zhihong removed the
avail_idxprefetch. I understand the prefetch becomes a bit tricky when mrg-rx code path isconsidered;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
thus, I didn't comment on that.
That's one of the difference that, IMO, could drop a regression. I then finally got a chance to add it back.
A rough test shows it improves the performance of 1400B packet size
greatly
Quoted 1 line
in the "txonly in host and rxonly in guest" case: +33% is the number I
get
Quoted 27 lines
Quoted 11 lines
Quoted 3 lines
Quoted 1 line
with my test server (Ivybridge).
Thanks Yuanhan! I'll validate this on x86.
Hi Yuanhan,
Seems your code doesn't perform correctly. I write a new version of avail idx prefetch but didn't see any perf benefit.
To be honest I doubt the benefit of this idea. The previous
mrg_offcode has this method but doesn't give any benefits.Good point. I thought of that before, too. But you know that I made it in rush, that I didn't think further and test more.
I looked the code a bit closer this time, and spotted a bug: the prefetch actually didn't happen, due to following code piece:
if (vq->next_avail_idx >= NR_AVAIL_IDX_PREFETCH) { prefetch_avail_idx(vq); ... }Since
vq->next_avail_idxis set to 0 at the entrance of enqueue path,prefetch_avail_idx()will be called. The fix is easy though: just putprefetch_avail_idxbefore invokingenqueue_packet.Quoted 6 lines
In summary, Zhihong is right, I see no more gains with that fix :(
However, as stated, that's kind of the only difference I found between yours and the old code, that maybe it's still worthwhile to have a test on ARM, Jianbo?
I haven't tested it, but I think it could be no improvement for ARM either.
A smalll suggestion for
enqueue_packet:.....
(quoted diff) +2
-0+ /* start copy from mbuf to desc */ + while (mbuf_avail || mbuf->next) {
Quoted 4 lines
Considering
pkt_lenis in the first cache line (same asdata_len), while next pointer is in the second cache line,
is it better to check the total packet len, instead of the last mbuf's next pointer to jump out of while loop and avoid possible cache miss?
Jianbo,
Thanks for the reply!
This idea sounds good, but it won't help the general perf in my opinion, since the 2nd cache line is accessed anyway prior in virtio_enqueue_offload.
Also this would bring a NULL check when actually access mbuf->next.
BTW, could you please publish the number of:
1. mrg_rxbuf=on, comparison between original and original + this patch
2. mrg_rxbuf=off, comparison between original and original + this patch
So we can have a whole picture of how this patch impact on ARM platform.
Thanks
Zhihong
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 81 lines · Zhihong
Quoted 76 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Monday, October 10, 2016 1:32 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 9 lines · Yuanhan Liu
Quoted 8 lines · Zhihong
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
There is a difference between Zhihong's code and the old I spotted in the first time: Zhihong removed the
avail_idxprefetch. I understand the prefetch becomes a bit tricky when mrg-rx code path isconsidered;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
thus, I didn't comment on that.
That's one of the difference that, IMO, could drop a regression. I then finally got a chance to add it back.
A rough test shows it improves the performance of 1400B packet size
greatly
Quoted 1 line
in the "txonly in host and rxonly in guest" case: +33% is the number I
get
Quoted 27 lines
Quoted 11 lines
Quoted 3 lines
Quoted 1 line
with my test server (Ivybridge).
Thanks Yuanhan! I'll validate this on x86.
Hi Yuanhan,
Seems your code doesn't perform correctly. I write a new version of avail idx prefetch but didn't see any perf benefit.
To be honest I doubt the benefit of this idea. The previous
mrg_offcode has this method but doesn't give any benefits.Good point. I thought of that before, too. But you know that I made it in rush, that I didn't think further and test more.
I looked the code a bit closer this time, and spotted a bug: the prefetch actually didn't happen, due to following code piece:
if (vq->next_avail_idx >= NR_AVAIL_IDX_PREFETCH) { prefetch_avail_idx(vq); ... }Since
vq->next_avail_idxis set to 0 at the entrance of enqueue path,prefetch_avail_idx()will be called. The fix is easy though: just putprefetch_avail_idxbefore invokingenqueue_packet.Quoted 6 lines
In summary, Zhihong is right, I see no more gains with that fix :(
However, as stated, that's kind of the only difference I found between yours and the old code, that maybe it's still worthwhile to have a test on ARM, Jianbo?
I haven't tested it, but I think it could be no improvement for ARM either.
A smalll suggestion for
enqueue_packet:.....
(quoted diff) +2
-0+ /* start copy from mbuf to desc */ + while (mbuf_avail || mbuf->next) {Quoted 4 lines
Considering
pkt_lenis in the first cache line (same asdata_len), while next pointer is in the second cache line,
is it better to check the total packet len, instead of the last mbuf's next pointer to jump out of while loop and avoid possible cache miss?
Quoted 8 lines
Jianbo,
Thanks for the reply!
This idea sounds good, but it won't help the general perf in my opinion, since the 2nd cache line is accessed anyway prior in
virtio_enqueue_offload.
Yes, you are right. I'm thinking of prefetching beforehand. And if it's a chained mbuf, virtio_enqueue_offload will not be called in next loop.
Quoted 10 lines
Also this would bring a NULL check when actually access
mbuf->next.BTW, could you please publish the number of:
1. mrg_rxbuf=on, comparison between original and original + this patch
2. mrg_rxbuf=off, comparison between original and original + this patch
So we can have a whole picture of how this patch impact on ARM platform.
I think you already have got many results in my previous emails. Sorry I can't test right now and busy with other things.
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 114 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Monday, October 10, 2016 2:58 PM
To: Wang, Zhihong <[email protected]>
Cc: Yuanhan Liu <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 21 lines · Zhihong
Quoted 21 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Monday, October 10, 2016 1:32 PM
To: Yuanhan Liu <[email protected]>
Cc: Wang, Zhihong <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 9 lines · Yuanhan Liu
Quoted 8 lines · Zhihong
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Tested with testpmd, host: txonly, guest: rxonly
size (bytes) improvement (%)64 4.12 128 6 256 2.65 512 -1.121024 -7.02
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
There is a difference between Zhihong's code and the old I spotted
in
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
the first time: Zhihong removed the
avail_idxprefetch. Iunderstand
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
the prefetch becomes a bit tricky when mrg-rx code path is
considered;
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
thus, I didn't comment on that.
That's one of the difference that, IMO, could drop a regression. I
then
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
finally got a chance to add it back.
A rough test shows it improves the performance of 1400B packet
size
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
greatly
Quoted 1 line
in the "txonly in host and rxonly in guest" case: +33% is the number
I
Quoted 48 lines
Quoted 43 lines
get
Quoted 27 lines
Quoted 11 lines
Quoted 3 lines
Quoted 1 line
with my test server (Ivybridge).
Thanks Yuanhan! I'll validate this on x86.
Hi Yuanhan,
Seems your code doesn't perform correctly. I write a new version of avail idx prefetch but didn't see any perf benefit.
To be honest I doubt the benefit of this idea. The previous
mrg_offcode has this method but doesn't give any benefits.Good point. I thought of that before, too. But you know that I made it in rush, that I didn't think further and test more.
I looked the code a bit closer this time, and spotted a bug: the prefetch actually didn't happen, due to following code piece:
if (vq->next_avail_idx >= NR_AVAIL_IDX_PREFETCH) { prefetch_avail_idx(vq); ... }Since
vq->next_avail_idxis set to 0 at the entrance of enqueue path,prefetch_avail_idx()will be called. The fix is easy though: just putprefetch_avail_idxbefore invokingenqueue_packet.Quoted 6 lines
In summary, Zhihong is right, I see no more gains with that fix :(
However, as stated, that's kind of the only difference I found between yours and the old code, that maybe it's still worthwhile to have a test on ARM, Jianbo?
I haven't tested it, but I think it could be no improvement for ARM either.
A smalll suggestion for
enqueue_packet:.....
(quoted diff) +2
-0+ /* start copy from mbuf to desc */ + while (mbuf_avail || mbuf->next) {Quoted 4 lines
Considering
pkt_lenis in the first cache line (same asdata_len), while next pointer is in the second cache line,
is it better to check the total packet len, instead of the last mbuf's next pointer to jump out of while loop and avoid possible cache miss?Quoted 8 lines
Jianbo,
Thanks for the reply!
This idea sounds good, but it won't help the general perf in my opinion, since the 2nd cache line is accessed anyway prior in
virtio_enqueue_offload.Yes, you are right. I'm thinking of prefetching beforehand. And if it's a chained mbuf,
virtio_enqueue_offloadwill not be called in next loop.Quoted 4 lines
Also this would bring a NULL check when actually access
mbuf->next.BTW, could you please publish the number of:
Quoted 8 lines
Quoted 6 lines
1. mrg_rxbuf=on, comparison between original and original + this patch
2. mrg_rxbuf=off, comparison between original and original + this patch
So we can have a whole picture of how this patch impact on ARM platform.
I think you already have got many results in my previous emails. Sorry I can't test right now and busy with other things.
We're still missing mrg on data.
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Jianbo LiuQuoted 24 lines · Jianbo Liu
Quoted 9 lines · Yuanhan Liu
Quoted 8 lines · Jianbo Liu
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size is more than 256.Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.
I have thought of this twice. Unfortunately, I think I may need NACK this series.
Merging two code path into one is really good: as you stated, it improves the maintainability. But only if we see no performance regression on both path after the refactor. Unfortunately, that's not the case here: it hurts the performance for one code path (non-mrg Rx).
That makes me think we may should not do the code path merge at all. I think that also aligns with what you have said before (internally): we could do the merge if it gives comparable performance before and after that.
Besides that, I don't quite like the way you did in patch 2 (rewrite enqueue):
you made a lot of changes in one patch. That means if something wrong happened, it is hard to narrow down which change introduces that regression. Badly, that's exactly what we met here. Weeks have been passed, I see no progress.
That's the reason we like the idea of "one patch only does one thing, an atomic thing".
So I will apply the first patch (it's a bug fixing patch) and ask you to refactor the rest, without the code path merge.
I think we could still have a good maintainability code base if we introduce more common helper functions that can be used on both Rx path, or even on Tx path (such as update_used_ring, or shadow_used_ring).
It's a bit late for too many changes for v16.11. I think you could just grab patch 6 (vhost: optimize cache access) to the old mrg-Rx code path, if that also helps the performance? Let us handle the left in next release, such as shadow used ring.
Thanks.
--yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 58 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Wednesday, October 12, 2016 10:53 AM
To: Wang, Zhihong <[email protected]>; Jianbo Liu <[email protected]>
Cc: Maxime Coquelin <[email protected]>; [email protected]; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 1 line · Jianbo Liu
On 22 September 2016 at 10:29, Yuanhan Liu <[email protected]>
Quoted 23 lines · wrote:
Quoted 9 lines · Jianbo Liu
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
My setup consists of one host running a guest.
The guest generates as much 64bytes packets as possible usingHave you tested with other different packet size?
My testing shows that performance is dropping when packet size is more than 256.Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Hi Jianbo,
Thanks for reporting this.
1. Are you running the vector frontend with mrg_rxbuf=off?
Yes, my testing is mrg_rxbuf=off, but not vector frontend PMD.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
2. Could you please specify what CPU you're running? Is it Haswell or Ivy Bridge?
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
Quoted 9 lines
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.I have thought of this twice. Unfortunately, I think I may need NACK this series.
Merging two code path into one is really good: as you stated, it improves the maintainability. But only if we see no performance regression on both path after the refactor. Unfortunately, that's not the case here: it hurts the performance for one code path (non-mrg Rx).
That makes me think we may should not do the code path merge at all. I think that also aligns with what you have said before (internally): we could do the merge if it gives comparable performance before and after that.
Quoted 8 lines
Besides that, I don't quite like the way you did in patch 2 (rewrite enqueue):
you made a lot of changes in one patch. That means if something wrong happened,
it is hard to narrow down which change introduces that regression. Badly, that's exactly what we met here. Weeks have been passed, I see no progress.That's the reason we like the idea of "one patch only does one thing, an atomic thing".
Yuanhan, folks,
Thanks for the analysis. I disagree here though.
I analyze, develop, benchmark on x86 platforms, where this patch works great.
I've been trying to analyze on ARM too but it takes time and I've had a schedule. Also since the ARM perf issue comes when it's v6 already, I might not be able to make it in time. However that's what I have to do for this patch to be merged in this or the next release.
In the meantime, may I suggest we consider the possibility to have dedicated codes for **perf critical paths** for different kinds of architecture?
It can be hard for a person to have both the knowledge and the development environment for multiple archs at the same time.
Moreover, different optimization techniques might be required for different archs, so it's hard and unnecessary to make a function works for all archs, sometimes it's just not the right thing to do.
Thanks
Zhihong
Quoted 16 lines
So I will apply the first patch (it's a bug fixing patch) and ask you to refactor the rest, without the code path merge.
I think we could still have a good maintainability code base if we introduce more common helper functions that can be used on both Rx path, or even on Tx path (such as
update_used_ring, orshadow_used_ring).It's a bit late for too many changes for v16.11. I think you could just grab patch 6 (vhost: optimize cache access) to the old mrg-Rx code path, if that also helps the performance? Let us handle the left in next release, such as shadow used ring.
Thanks.
--yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
Sorry guys, you lost me in the discussion.
Is there some regression only on ARM?
Does it need some work specifically on memcpy for ARM, or vhost for ARM?
Who can work on ARM optimization?
More comments below.
2016-10-12 12:22, Wang, Zhihong:
Quoted 21 lines
From: Yuanhan Liu [mailto:[email protected]]
Quoted 20 lines
Quoted 12 lines
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.I have thought of this twice. Unfortunately, I think I may need NACK this series.
Merging two code path into one is really good: as you stated, it improves the maintainability. But only if we see no performance regression on both path after the refactor. Unfortunately, that's not the case here: it hurts the performance for one code path (non-mrg Rx).
+1
Quoted 9 lines
Quoted 9 lines
That makes me think we may should not do the code path merge at all. I think that also aligns with what you have said before (internally): we could do the merge if it gives comparable performance before and after that.
Besides that, I don't quite like the way you did in patch 2 (rewrite enqueue):
you made a lot of changes in one patch. That means if something wrong happened,
it is hard to narrow down which change introduces that regression. Badly, that's exactly what we met here. Weeks have been passed, I see no progress.
+1, it is important to have simple patches making changes step by step.
Quoted 17 lines
Quoted 2 lines
That's the reason we like the idea of "one patch only does one thing, an atomic thing".
Yuanhan, folks,
Thanks for the analysis. I disagree here though.
I analyze, develop, benchmark on x86 platforms, where this patch works great.
I've been trying to analyze on ARM too but it takes time and I've had a schedule. Also since the ARM perf issue comes when it's v6 already, I might not be able to make it in time. However that's what I have to do for this patch to be merged in this or the next release.
Quoted 3 lines
In the meantime, may I suggest we consider the possibility to have dedicated codes for **perf critical paths** for different kinds of architecture?
Yes that's what we do in several parts of DPDK.
Quoted 2 lines
It can be hard for a person to have both the knowledge and the development environment for multiple archs at the same time.
Yes we do not expect you work on ARM.
So if nobody work on the ARM issue, you could make 2 code paths in order to allow your optimization for x86 only.
But that's not the preferred way.
And you must split your rework to better identify which part is a regression on ARM.
Quoted 3 lines
Moreover, different optimization techniques might be required for different archs, so it's hard and unnecessary to make a function works for all archs, sometimes it's just not the right thing to do.
Yes sometimes. Please help us to be convinced for this case.
Quoted 6 lines
Quoted 6 lines
So I will apply the first patch (it's a bug fixing patch) and ask you to refactor the rest, without the code path merge.
I think we could still have a good maintainability code base if we introduce more common helper functions that can be used on both Rx path, or even on Tx path (such as
update_used_ring, orshadow_used_ring).
Yes it is a good step.
And the code path merge could be reconsidered later.
Quoted 4 lines
Quoted 4 lines
It's a bit late for too many changes for v16.11. I think you could just grab patch 6 (vhost: optimize cache access) to the old mrg-Rx code path, if that also helps the performance? Let us handle the left in next release, such as shadow used ring.
Thank you
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 11 lines
-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Wednesday, October 12, 2016 11:31 PM
To: Wang, Zhihong <[email protected]>
Cc: Yuanhan Liu <[email protected]>; Jianbo Liu <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueue
Quoted 3 lines
Sorry guys, you lost me in the discussion.
Is there some regression only on ARM?
ARM is what we see, no info on ppc yet.
Quoted 3 lines
Does it need some work specifically on memcpy for ARM, or vhost for ARM?
Who can work on ARM optimization?
These are still open questions, Jiaobo who reported this doesn't have time for more testing now according to the reply.
I'm trying to do some test in the hope to identify the root cause.
Quoted 101 lines
More comments below.
2016-10-12 12:22, Wang, Zhihong:
Quoted 13 lines
From: Yuanhan Liu [mailto:[email protected]]
Quoted 12 lines
Quoted 12 lines
It's an ARM server.
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than512.
Quoted 8 lines
Quoted 8 lines
I have thought of this twice. Unfortunately, I think I may need NACK this series.
Merging two code path into one is really good: as you stated, it improves the maintainability. But only if we see no performance regression on both path after the refactor. Unfortunately, that's not the case here: it hurts the performance for one code path (non-mrg Rx).
+1
Quoted 1 line
Quoted 1 line
That makes me think we may should not do the code path merge at all. I
think
Quoted 1 line
Quoted 1 line
that also aligns with what you have said before (internally): we could do
the
Quoted 3 lines
Quoted 3 lines
merge if it gives comparable performance before and after that.
Besides that, I don't quite like the way you did in patch 2 (rewrite
enqueue):
Quoted 4 lines
Quoted 4 lines
you made a lot of changes in one patch. That means if something wrong happened,
it is hard to narrow down which change introduces that regression. Badly, that's exactly what we met here. Weeks have been passed, I see noprogress.
+1, it is important to have simple patches making changes step by step.
Quoted 17 lines
Quoted 2 lines
That's the reason we like the idea of "one patch only does one thing, an atomic thing".
Yuanhan, folks,
Thanks for the analysis. I disagree here though.
I analyze, develop, benchmark on x86 platforms, where this patch works great.
I've been trying to analyze on ARM too but it takes time and I've had a schedule. Also since the ARM perf issue comes when it's v6 already, I might not be able to make it in time. However that's what I have to do for this patch to be merged in this or the next release.
Quoted 3 lines
In the meantime, may I suggest we consider the possibility to have dedicated codes for **perf critical paths** for different kinds of architecture?
Yes that's what we do in several parts of DPDK.
Quoted 2 lines
It can be hard for a person to have both the knowledge and the development environment for multiple archs at the same time.
Yes we do not expect you work on ARM.
So if nobody work on the ARM issue, you could make 2 code paths in order to allow your optimization for x86 only.
But that's not the preferred way.
And you must split your rework to better identify which part is a regression on ARM.Quoted 3 lines
Moreover, different optimization techniques might be required for different archs, so it's hard and unnecessary to make a function works for all archs, sometimes it's just not the right thing to do.
Yes sometimes. Please help us to be convinced for this case.
Quoted 5 lines
Quoted 5 lines
So I will apply the first patch (it's a bug fixing patch) and ask you to refactor the rest, without the code path merge.
I think we could still have a good maintainability code base if we introduce more common helper functions that can be used on both Rx path, or
even on
Quoted 1 line
Quoted 1 line
Tx path (such as
update_used_ring, orshadow_used_ring).Yes it is a good step.
And the code path merge could be reconsidered later.Quoted 4 lines
Quoted 4 lines
It's a bit late for too many changes for v16.11. I think you could just grab patch 6 (vhost: optimize cache access) to the old mrg-Rx code path, if that also helps the performance? Let us handle the left in next release, such as shadow used ring.
Thank you
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Thomas MonjalonHi Thomas,
Quoted 4 lines · Thomas Monjalon
Sorry guys, you lost me in the discussion.
Is there some regression only on ARM?
Does it need some work specifically on memcpy for ARM,
I don't know if there is common way to improve memcpy on different ARM hardware. Even there is, it could take times.
I have tried do that using neon (like sse) instructions, but without success.
Quoted 3 lines
or vhost for ARM?
Who can work on ARM optimization?
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Wang, ZhihongQuoted 33 lines · Zhihong
Quoted 23 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than 512.I have thought of this twice. Unfortunately, I think I may need NACK this series.
Merging two code path into one is really good: as you stated, it improves the maintainability. But only if we see no performance regression on both path after the refactor. Unfortunately, that's not the case here: it hurts the performance for one code path (non-mrg Rx).
That makes me think we may should not do the code path merge at all. I think that also aligns with what you have said before (internally): we could do the merge if it gives comparable performance before and after that.
Quoted 8 lines
Besides that, I don't quite like the way you did in patch 2 (rewrite enqueue):
you made a lot of changes in one patch. That means if something wrong happened,
it is hard to narrow down which change introduces that regression. Badly, that's exactly what we met here. Weeks have been passed, I see no progress.That's the reason we like the idea of "one patch only does one thing, an atomic thing".
Quoted 6 lines
Yuanhan, folks,
Thanks for the analysis. I disagree here though.
I analyze, develop, benchmark on x86 platforms, where this patch works great.
Yes, that's great effort! With your hardwork, we know what the bottleneck is and how it could be improved.
However, you don't have to do code refactor (merge two code path to one) to apply those improvements. From what I know, in this patchset, there are two factors could improve the performance:
- copy hdr together with packet data
- shadow used ring update and update at once
The overall performance boost I got with your v6 patchset with mrg-Rx code path is about 27% (in PVP case). And I have just applied the 1st optimization, it yields about 20% boosts. The left could be covered if we apply the 2nd optimization (I guess).
That would be a clean way to optimize vhost mergeable Rx path:
- you don't touch non-mrg Rx path (well, you may could apply the shadow_used_ring trick to it as wel)
This would at least make sure we will have no such performance regression issue reported by ARM guys.
- you don't refactor the code
The rewrite from scratch could introduce other issues, besides the performance regression. We may just don't know it yet.
Make sense to you? If you agree, I think we could still make it in this release: they would be some small changes after all. For example, below is the patch applies the 1st optimization tip on top of dpdk-next-virtio
--yliu
---------------------------------------------------------------
lib/librte_vhost/virtio_net.c +15 -9
@@ -379,7 +379,7 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, uint16_t end_idx, struct rte_mbuf *m, struct buf_vector *buf_vec) { - struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0}; + struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;uint32_t vec_idx =0 ;uint16_t start_idx = vq->last_used_idx;uint16_t cur_idx = start_idx;
@@ -388,6 +388,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t desc_offset, desc_avail;uint32_t cpy_len;uint16_t desc_idx, used_idx; +uint16_t num_buffers = end_idx - start_idx; +int hdr_copied =0 ;if (unlikely (m ==NULL ))return 0 ;
@@ -399,16 +401,11 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)return 0 ; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +rte_prefetch0 (virtio_hdr); - virtio_hdr.num_buffers = end_idx - start_idx;LOG_DEBUG (VHOST_DATA ,"(%d) RX: num merge buffers %d\n" , - dev->vid, virtio_hdr.num_buffers); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); + dev->vid, num_buffers); desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; desc_offset = dev->vhost_hlen;
@@ -450,6 +447,15 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, mbuf_avail =rte_pktmbuf_data_len (m); } +if (hdr_copied ==0 ) { + virtio_hdr->num_buffers = num_buffers; +virtio_enqueue_offload (m, &virtio_hdr->hdr); +vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); +PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); + + hdr_copied =1 ; + } + 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),
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 4 lines · Yuanhan Liu
That would be a clean way to optimize vhost mergeable Rx path:
- you don't touch non-mrg Rx path (well, you may could apply the
shadow_used_ringtrick to it as wel)
I meant "to non-mrg Rx as well".
--yliu
Re: [PATCH v3 0/5] vhost: optimize enqueue
In reply to Yuanhan LiuQuoted 83 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Thursday, October 13, 2016 1:33 PM
To: Wang, Zhihong <[email protected]>
Cc: Jianbo Liu <[email protected]>; Thomas Monjalon <[email protected]>; Maxime Coquelin <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 0/5] vhost: optimize enqueueQuoted 10 lines · Zhihong
Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
3. How many percentage of drop are you seeing?
The testing result:
size (bytes) improvement (%)
64 3.92
128 11.51
256 24.16
512 -13.79
1024 -22.51
1500 -12.22
A correction is that performance is dropping if byte size is larger than512.
Quoted 10 lines
Quoted 10 lines
I have thought of this twice. Unfortunately, I think I may need NACK this series.
Merging two code path into one is really good: as you stated, it improves the maintainability. But only if we see no performance regression on both path after the refactor. Unfortunately, that's not the case here: it hurts the performance for one code path (non-mrg Rx).
That makes me think we may should not do the code path merge at all. I
think
Quoted 1 line
Quoted 1 line
that also aligns with what you have said before (internally): we could do
the
Quoted 3 lines
Quoted 3 lines
merge if it gives comparable performance before and after that.
Besides that, I don't quite like the way you did in patch 2 (rewrite
enqueue):
Quoted 4 lines
Quoted 4 lines
you made a lot of changes in one patch. That means if something wrong happened,
it is hard to narrow down which change introduces that regression. Badly, that's exactly what we met here. Weeks have been passed, I see noprogress.
Quoted 11 lines
Quoted 3 lines
That's the reason we like the idea of "one patch only does one thing, an atomic thing".
Yuanhan, folks,
Thanks for the analysis. I disagree here though.
I analyze, develop, benchmark on x86 platforms, where this patch works great.
Yes, that's great effort! With your hardwork, we know what the bottleneck is and how it could be improved.
However, you don't have to do code refactor (merge two code path to one) to apply those improvements. From what I know, in this patchset, there are two factors could improve the performance:
- copy hdr together with packet data
- shadow used ring update and update at once
The overall performance boost I got with your v6 patchset with mrg-Rx code path is about 27% (in PVP case). And I have just applied the 1st optimization, it yields about 20% boosts. The left could be covered if we apply the 2nd optimization (I guess).
That would be a clean way to optimize vhost mergeable Rx path:
- you don't touch non-mrg Rx path (well, you may could apply the
shadow_used_ringtrick to it as wel)This would at least make sure we will have no such performance regression issue reported by ARM guys.
- you don't refactor the code
Quoted 8 lines
The rewrite from scratch could introduce other issues, besides the performance regression. We may just don't know it yet.
Make sense to you? If you agree, I think we could still make it in this release: they would be some small changes after all. For example, below is the patch applies the 1st optimization tip on top of dpdk-next-virtio
Thanks for this great idea. I think it's a better way to do it. I'll start to make the patch then.
Quoted 69 lines
--yliu---------------------------------------------------------------
lib/librte_vhost/virtio_net.c +1
-1@@ -379,7 +379,7 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, uint16_t end_idx, struct rte_mbuf *m, struct buf_vector *buf_vec) { -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;uint32_t vec_idx =0 ;uint16_t start_idx = vq->last_used_idx;lib/librte_vhost/virtio_net.c +2
-0uint16_t cur_idx = start_idx;@@ -388,6 +388,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, uint32_t desc_offset, desc_avail;uint32_t cpy_len;uint16_t desc_idx, used_idx; +uint16_t num_buffers = end_idx - start_idx; +int hdr_copied =0 ;if (unlikely (m ==NULL ))lib/librte_vhost/virtio_net.c +3
-8return 0 ;@@ -399,16 +401,11 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)return 0 ; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +rte_prefetch0 (virtio_hdr); - virtio_hdr.num_buffers = end_idx - start_idx;LOG_DEBUG (VHOST_DATA ,"(%d) RX: num merge buffers %d\n" , - dev->vid, virtio_hdr.num_buffers); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); + dev->vid, num_buffers);lib/librte_vhost/virtio_net.c +9
-0desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; desc_offset = dev->vhost_hlen;@@ -450,6 +447,15 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, mbuf_avail =rte_pktmbuf_data_len (m); } +if (hdr_copied ==0 ) { + virtio_hdr->num_buffers = num_buffers; +virtio_enqueue_offload (m, &virtio_hdr->hdr); +vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); +PRINT_PACKET (dev, (uintptr_t )desc_addr, dev- >vhost_hlen, 0); + + hdr_copied =1 ; + } +lib/librte_vhost/virtio_net.c +0
-0cpy_len =RTE_MIN (desc_avail, mbuf_avail); rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)), rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 30 lines · Zhihong
Quoted 30 lines
Quoted 30 lines
Yes, that's great effort! With your hardwork, we know what the bottleneck is and how it could be improved.
However, you don't have to do code refactor (merge two code path to one) to apply those improvements. From what I know, in this patchset, there are two factors could improve the performance:
- copy hdr together with packet data
- shadow used ring update and update at once
The overall performance boost I got with your v6 patchset with mrg-Rx code path is about 27% (in PVP case). And I have just applied the 1st optimization, it yields about 20% boosts. The left could be covered if we apply the 2nd optimization (I guess).
That would be a clean way to optimize vhost mergeable Rx path:
- you don't touch non-mrg Rx path (well, you may could apply the
shadow_used_ringtrick to it as wel)This would at least make sure we will have no such performance regression issue reported by ARM guys.
- you don't refactor the code
The rewrite from scratch could introduce other issues, besides the performance regression. We may just don't know it yet.
Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
Make sense to you? If you agree, I think we could still make it in this release: they would be some small changes after all. For example, below is the patch applies the 1st optimization tip on top of dpdk-next-virtio
Thanks for this great idea. I think it's a better way to do it. I'll start to make the patch then.
I personally find having two paths better for maintenance as it is easier to understand (IMHO).
So if we can have the performance gain while keeping the two paths, I definitely support the idea.
Thanks,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 45 lines · Maxime Coquelin
Quoted 32 lines · Zhihong
Quoted 32 lines
Quoted 1 line
Yes, that's great effort! With your hardwork, we know what the
bottleneck
Quoted 3 lines
is and how it could be improved.
However, you don't have to do code refactor (merge two code path to
one)
Quoted 26 lines
to apply those improvements. From what I know, in this patchset, there are two factors could improve the performance:
- copy hdr together with packet data
- shadow used ring update and update at once
The overall performance boost I got with your v6 patchset with mrg-Rx code path is about 27% (in PVP case). And I have just applied the 1st optimization, it yields about 20% boosts. The left could be covered if we apply the 2nd optimization (I guess).
That would be a clean way to optimize vhost mergeable Rx path:
- you don't touch non-mrg Rx path (well, you may could apply the
shadow_used_ringtrick to it as wel)This would at least make sure we will have no such performance regression issue reported by ARM guys.
- you don't refactor the code
The rewrite from scratch could introduce other issues, besides the performance regression. We may just don't know it yet.
Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
Make sense to you? If you agree, I think we could still make it in this release: they would be some small changes after all. For example, below is the patch applies the 1st optimization tip on top of dpdk-next-virtio
Thanks for this great idea. I think it's a better way to do it. I'll start to make the patch then.
Quoted 4 lines
I personally find having two paths better for maintenance as it is easier to understand (IMHO).
So if we can have the performance gain while keeping the two paths, I definitely support the idea.
I was going to re-run some PVP benchmark with 0% pkt loss, as I had some strange results last week.
Problem is that your series no more apply cleanly due to next-virtio's master branch history rewrite.
Any chance you send me a rebased version so that I can apply the series?
Thanks,
Maxime
Re: [PATCH v3 0/5] vhost: optimize enqueue
Quoted 6 lines · Maxime Coquelin
I was going to re-run some PVP benchmark with 0% pkt loss, as I had some strange results last week.
Problem is that your series no more apply cleanly due to next-virtio's master branch history rewrite.
Any chance you send me a rebased version so that I can apply the series?
I think it's pointless to do that now: it won't be merged after all. We have refactored out the new series, please help review if you got time :)
BTW, apologize that I forgot to include your Reviewed-by for the first patch. I intended to do that ...
--yliu
v4 cover vhost: optimize enqueue
In reply to Zhihong WangThis patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed for high performance and good maintainability, and improves CPU efficiency significantly by optimizing cache access, which means:
* Higher maximum throughput can be achieved for fast frontends like DPDK virtio pmd.
* Better scalability can be achieved that each vhost core can support more connections because it takes less cycles to handle each single frontend.
This patch set contains:
1. A Windows VM compatibility fix for vhost enqueue in 16.07 release.
2. A baseline patch to rewrite the vhost logic.
3. A series of optimization patches added upon the baseline.
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Code reordering and batch used ring update bring most of the performance improvements.
In the existing code there're 2 callbacks for vhost enqueue:
* virtio_dev_merge_rx for mrg_rxbuf turned on cases.
* virtio_dev_rx for mrg_rxbuf turned off cases.
The performance of the existing code is not optimal, especially when the mrg_rxbuf feature turned on. Besides, having 2 callback paths increases maintenance efforts.
Also, there's a compatibility issue in the existing code which causes Windows VM to hang when the mrg_rxbuf feature turned on.
---
Changes in v4:
1. Fix a Windows VM compatibility issue.
2. Free shadow used ring in the right place.
3. Add failure check for shadow used ring malloc.
4. Refactor the code for clearer logic.
5. Add PRINT_PACKET for debugging.
---
Changes in v3:
1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
3. Rewrite enqueue and delete the obsolete in the same patch.
---
Changes in v2:
1. Split the big function into several small ones.
2. Use multiple patches to explain each optimization.
3. Add comments.
Zhihong Wang (6):
vhost: fix windows vm hang
vhost: rewrite enqueue
vhost: remove useless volatile
vhost: add desc prefetch
vhost: batch update used ring
vhost: optimize cache access
lib/librte_vhost/vhost-net.h | 6 +- lib/librte_vhost/vhost_rxtx.c | 572 +++++++++++++++--------------------------- lib/librte_vhost/virtio-net.c | 42 +++- 3 files changed, 244 insertions(+), 376 deletions(-)
Signature · 1 line
2.7.4
PATCH v4 1/6 vhost: fix windows vm hang
This patch fixes a Windows VM compatibility issue in DPDK 16.07 vhost code, which causes the guest to hang once any packets are enqueued when mrg_rxbuf is turned on.
How to test?
1. Start testpmd in the host with a vhost port.
2. Start a Windows VM image with qemu and connect to the vhost port.
3. Start io forwarding with tx_first in host testpmd.
For 16.07 code, the Windows VM will hang once any packets are enqueued.
--- lib/librte_vhost/vhost_rxtx.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
lib/librte_vhost/vhost_rxtx.c +12 -5
@@ -384,6 +384,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,uint16_t start_idx = vq->last_used_idx;uint16_t cur_idx = start_idx;uint64_t desc_addr; +uint32_t desc_chain_head; +uint32_t desc_chain_len;uint32_t mbuf_offset, mbuf_avail;uint32_t desc_offset, desc_avail;uint32_t cpy_len;
@@ -412,6 +414,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; desc_offset = dev->vhost_hlen; + desc_chain_head = buf_vec[vec_idx].desc_idx; + desc_chain_len = desc_offset; mbuf_avail =rte_pktmbuf_data_len (m); mbuf_offset =0 ;
@@ -419,19 +423,21 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,/* done with current desc buf, get the next one */ if (desc_avail ==0 ) { desc_idx = buf_vec[vec_idx].desc_idx; + vec_idx++;if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) {/* 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; + vq->used->ring[used_idx].id = desc_chain_head; + vq->used->ring[used_idx].len = desc_chain_len;vhost_log_used_vring (dev, vq, offsetof(struct vring_used , ring[used_idx]), sizeof(vq->used->ring[used_idx])); + desc_chain_head = buf_vec[vec_idx].desc_idx; + desc_chain_len =0 ; } - vec_idx++; desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr);if (unlikely (!desc_addr))return 0 ;
@@ -463,11 +469,12 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, mbuf_offset += cpy_len; desc_avail -= cpy_len; desc_offset += cpy_len; + desc_chain_len += cpy_len; } used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = buf_vec[vec_idx].desc_idx; - vq->used->ring[used_idx].len = desc_offset; + vq->used->ring[used_idx].id = desc_chain_head; + vq->used->ring[used_idx].len = desc_chain_len;vhost_log_used_vring (dev, vq, offsetof(struct vring_used, ring[used_idx]), sizeof(vq->used->ring[used_idx]));
Signature · 1 line
2.7.4
Re: [dpdk-stable] [PATCH v4 1/6] vhost: fix windows vm hang
Quoted 3 lines · Zhihong Wang
This patch fixes a Windows VM compatibility issue in DPDK 16.07 vhost code, which causes the guest to hang once any packets are enqueued when
mrg_rxbufis turned on.
This commit log lacks two important pieces: why does the hang happen and how does your patch fix it.
Quoted 21 lines
How to test?
1. Start testpmd in the host with a vhost port.
2. Start a Windows VM image with qemu and connect to the vhost port.
3. Start io forwarding with
tx_firstin host testpmd.For 16.07 code, the Windows VM will hang once any packets are enqueued.
Cc <[email protected]>Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)lib/librte_vhost/vhost_rxtx.c +0
-0
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 388
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +2
-0@@ -384,6 +384,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,uint16_t start_idx = vq->last_used_idx;uint16_t cur_idx = start_idx;uint64_t desc_addr; +uint32_t desc_chain_head; +uint32_t desc_chain_len;
What's the point of introducing "desc_chain_len"? It has the same value of desc_offset.
--yliu
Re: [dpdk-stable] [PATCH v4 1/6] vhost: fix windows vm hang
Quoted 12 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Monday, September 5, 2016 1:25 PM
To: Wang, Zhihong <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [dpdk-stable] [PATCH v4 1/6] vhost: fix windows vm hangOn Mon, Aug 29, 2016 at 11:35:59PM -0400, Zhihong Wang wrote:
Quoted 8 lines
Quoted 1 line
This patch fixes a Windows VM compatibility issue in DPDK 16.07 vhost
code,
Quoted 1 line
which causes the guest to hang once any packets are enqueued when
mrg_rxbufQuoted 1 line
is turned on.
This commit log lacks two important pieces: why does the hang happen and how does your patch fix it.
Okay, I'll add it in v5.
Quoted 23 lines
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc_mergeable(struct virtio_net
Quoted 22 lines
How to test?
1. Start testpmd in the host with a vhost port.
2. Start a Windows VM image with qemu and connect to the vhost port.
3. Start io forwarding with
tx_firstin host testpmd.For 16.07 code, the Windows VM will hang once any packets are enqueued.
Cc <[email protected]>Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)lib/librte_vhost/vhost_rxtx.c +0
-0@@ -384,6 +384,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net*dev, struct
vhost_virtqueue*vq,
Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 388
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +2
-0uint16_t start_idx = vq->last_used_idx;uint16_t cur_idx = start_idx;uint64_t desc_addr; +uint32_t desc_chain_head; +uint32_t desc_chain_len;What's the point of introducing "
desc_chain_len"? It has the same value ofdesc_offset.
No it's not, desc_offset is the offset of the current desc only. That's where the old code goes wrong.
If you take a look at the virtio spec:
/* le32 is used here for ids for padding reasons. */ struct vring_used_elem {
/* Index of start of used descriptor chain. */
le32 id;
/* Total length of the descriptor chain which was written to. */ le32 len;
};
Quoted 2 lines
--yliu
Re: [dpdk-stable] [PATCH v4 1/6] vhost: fix windows vm hang
Quoted 55 lines · Zhihong
Quoted 12 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Monday, September 5, 2016 1:25 PM
To: Wang, Zhihong <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [dpdk-stable] [PATCH v4 1/6] vhost: fix windows vm hangOn Mon, Aug 29, 2016 at 11:35:59PM -0400, Zhihong Wang wrote:
Quoted 8 lines
Quoted 1 line
This patch fixes a Windows VM compatibility issue in DPDK 16.07 vhost
code,
Quoted 1 line
which causes the guest to hang once any packets are enqueued when
mrg_rxbufQuoted 1 line
is turned on.
This commit log lacks two important pieces: why does the hang happen and how does your patch fix it.
Okay, I'll add it in v5.
Quoted 23 lines
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc_mergeable(struct virtio_net
Quoted 22 lines
How to test?
1. Start testpmd in the host with a vhost port.
2. Start a Windows VM image with qemu and connect to the vhost port.
3. Start io forwarding with
tx_firstin host testpmd.For 16.07 code, the Windows VM will hang once any packets are enqueued.
Cc <[email protected]>Signed-off-by Zhihong Wang <[email protected]>--- lib/librte_vhost/vhost_rxtx.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)lib/librte_vhost/vhost_rxtx.c +0
-0@@ -384,6 +384,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net*dev, struct
vhost_virtqueue*vq,Quoted 8 lines
lib/librte_vhost/vhost_rxtx.c copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 388
Quoted 5 lines
lib/librte_vhost/vhost_rxtx.c +2
-0uint16_t start_idx = vq->last_used_idx;uint16_t cur_idx = start_idx;uint64_t desc_addr; +uint32_t desc_chain_head; +uint32_t desc_chain_len;What's the point of introducing "
desc_chain_len"? It has the same value ofdesc_offset.
Quoted 2 lines
No it's not,
desc_offsetis the offset of the current desc only. That's where the old code goes wrong.
Oh, right.
--yliu
PATCH v4 2/6 vhost: rewrite enqueue
In reply to Zhihong WangThis patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
---
Changes in v4:
1. Refactor the code for clearer logic.
2. Add PRINT_PACKET for debugging.
---
Changes in v3:
1. Rewrite enqueue and delete the obsolete in the same patch.
--- lib/librte_vhost/vhost_rxtx.c | 525 ++++++++++++------------------------------ 1 file changed, 145 insertions(+), 380 deletions(-)
lib/librte_vhost/vhost_rxtx.c +145 -380
@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {
@@ -112,6 +112,10 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) cksum));break ; } + }else { + net_hdr->flags =0 ; + net_hdr->csum_start =0 ; + net_hdr->csum_offset =0 ; }if (m_buf->ol_flags &PKT_TX_TCP_SEG ) {
@@ -122,437 +126,198 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) net_hdr->gso_size = m_buf->tso_segsz; net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len + m_buf->l4_len; + }else { + net_hdr->gso_type =0 ; + net_hdr->hdr_len =0 ; + net_hdr->gso_size =0 ; } } -static inline void -copy_virtio_net_hdr (struct virtio_net *dev,uint64_t desc_addr, -struct virtio_net_hdr_mrg_rxbuf hdr) +static inline void __attribute__((always_inline)) +update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t desc_chain_head,uint32_t desc_chain_len) { -if (dev->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; +uint32_t used_idx_round = vq->last_used_idx & (vq->size -1 ); + + vq->used->ring[used_idx_round].id = desc_chain_head; + vq->used->ring[used_idx_round].len = desc_chain_len; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , + ring[used_idx_round]), + sizeof(vq->used->ring[used_idx_round])); } -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) +static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;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]; +uint32_t desc_chain_head; +uint32_t desc_chain_len; +uint32_t desc_current; +uint32_t desc_offset; +uint32_t mbuf_len; +uint32_t mbuf_avail; +uint32_t copy_len; +uint32_t extra_buffers =0 ; + +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ; +if (unlikely (!desc_addr)) + goto error; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers = extra_buffers +1 ; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr);vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; - - 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); + desc_chain_len = desc_offset; + desc_addr += desc_offset; + +/* start copy from mbuf to desc */ +while (mbuf_avail || mbuf->next) { +/* get the next mbuf if the current done */ +if (!mbuf_avail) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; +/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */ + desc_offset =0 ; + desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; + }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ +update_used_ring (dev, vq, desc_chain_head, + desc_chain_len); + vq->last_used_idx++; + extra_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto error; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; - desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; + desc_chain_len =0 ; + desc_offset =0 ; + }else + goto error; } - 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; +/* copy mbuf data */ + copy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +rte_memcpy ((void *)(uintptr_t )desc_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_avail), + copy_len); +vhost_log_write (dev, desc->addr + desc_offset, copy_len); +PRINT_PACKET (dev, (uintptr_t )desc_addr, copy_len,0 ); + mbuf_avail -= copy_len; + desc_offset += copy_len; + desc_addr += copy_len; + desc_chain_len += copy_len; } -return 0 ; -} +update_used_ring (dev, vq, desc_chain_head, desc_chain_len); + vq->last_used_idx++; -/** -* 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 -* count is returned to indicate the number of packets that are succesfully -* added to the RX queue. This function works when the mbuf is scattered, but -* it doesn't support the mergeable feature. -*/ -static inline uint32_t __attribute__((always_inline)) -virtio_dev_rx (struct virtio_net *dev,uint16_t queue_id, -struct rte_mbuf **pkts,uint32_t count) -{ -struct vhost_virtqueue *vq; -uint16_t avail_idx, free_entries, start_idx; -uint16_t desc_indexes[MAX_PKT_BURST ]; -uint16_t used_idx; -uint32_t i; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); -return 0 ; - } - - vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) -return 0 ; - - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - start_idx = vq->last_used_idx; - free_entries = avail_idx - start_idx; - count =RTE_MIN (count, free_entries); - count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); -if (count ==0 ) -return 0 ; - -LOG_DEBUG (VHOST_DATA ,"(%d) start_idx %d | end_idx %d\n" , - dev->vid, start_idx, start_idx + count); - -/* Retrieve all of the desc indexes first to avoid caching issues. */ -rte_prefetch0 (&vq->avail->ring[start_idx & (vq->size -1 )]); -for (i =0 ; i < count; i++) { - used_idx = (start_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; - vq->used->ring[used_idx].id = desc_indexes[i]; - vq->used->ring[used_idx].len = pkts[i]->pkt_len + - dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } - -rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; +return 0 ; - err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx); -if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } +error: +/* rollback on any error if last_used_idx update on-the-fly */ + vq->last_used_idx -= extra_buffers; -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } +return 1 ; +} +static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ + vq->used->idx = vq->last_used_idx; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; } -static inline int -fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, -uint32_t *allocated,uint32_t *vec_idx, -struct buf_vector *buf_vec) -{ -uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; -uint32_t vec_id = *vec_idx; -uint32_t len = *allocated; - -while (1 ) { -if (unlikely (vec_id >=BUF_VECTOR_MAX || idx >= vq->size)) -return -1 ; - - len += vq->desc[idx].len; - buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - buf_vec[vec_id].buf_len = vq->desc[idx].len; - 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 ; -} - -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, -uint16_t *end,struct buf_vector *buf_vec) +uint16_t +rte_vhost_enqueue_burst (int vid,uint16_t queue_id, +struct rte_mbuf **pkts,uint16_t count) { -uint16_t cur_idx; +struct vhost_virtqueue *vq; +struct virtio_net *dev; +uint32_t pkt_left = count; +uint32_t pkt_idx =0 ; +uint32_t pkt_sent =0 ; +uint32_t is_mrg_rxbuf =0 ;uint16_t avail_idx; -uint32_t allocated =0 ; -uint32_t vec_idx =0 ; -uint16_t tries =0 ; - cur_idx = vq->last_used_idx; - -while (1 ) { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (cur_idx == avail_idx)) -return -1 ; - -if (unlikely (fill_vec_buf (vq, cur_idx, &allocated, - &vec_idx, buf_vec) <0 )) -return -1 ; - - 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 (unlikely (tries >= vq->size)) -return -1 ; - } - - *end = cur_idx; -return 0 ; -} - -static inline uint32_t __attribute__((always_inline)) -copy_mbuf_to_desc_mergeable (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint16_t end_idx,struct rte_mbuf *m, -struct buf_vector *buf_vec) -{ -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint32_t vec_idx =0 ; -uint16_t start_idx = vq->last_used_idx; -uint16_t cur_idx = start_idx; -uint64_t desc_addr; -uint32_t desc_chain_head; -uint32_t desc_chain_len; -uint32_t mbuf_offset, mbuf_avail; -uint32_t desc_offset, desc_avail; -uint32_t cpy_len; -uint16_t desc_idx, used_idx; - -if (unlikely (m ==NULL )) +if (unlikely (!pkt_left))return 0 ; -LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, cur_idx, end_idx); + pkt_left =RTE_MIN ((uint32_t )MAX_PKT_BURST , pkt_left); - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) + dev =get_device (vid); +if (unlikely (!dev))return 0 ; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - - virtio_hdr.num_buffers = end_idx - start_idx; -LOG_DEBUG (VHOST_DATA ,"(%d) RX: num merge buffers %d\n" , - dev->vid, virtio_hdr.num_buffers); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - - desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; - desc_offset = dev->vhost_hlen; - desc_chain_head = buf_vec[vec_idx].desc_idx; - desc_chain_len = desc_offset; - - 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 = buf_vec[vec_idx].desc_idx; - vec_idx++; - -if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) { -/* Update used ring with desc information */ - used_idx = cur_idx++ & (vq->size -1 ); - vq->used->ring[used_idx].id = desc_chain_head; - vq->used->ring[used_idx].len = desc_chain_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - desc_chain_head = buf_vec[vec_idx].desc_idx; - desc_chain_len =0 ; - } - - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (unlikely (!desc_addr)) -return 0 ; - -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - desc_offset =0 ; - desc_avail = buf_vec[vec_idx].buf_len; - } - -/* done with current mbuf, get the next one */ -if (mbuf_avail ==0 ) { - m = m->next; - - mbuf_offset =0 ; - mbuf_avail =rte_pktmbuf_data_len (m); - } - - 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, buf_vec[vec_idx].buf_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; - desc_chain_len += cpy_len; - } - - used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = desc_chain_head; - vq->used->ring[used_idx].len = desc_chain_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - -return end_idx - start_idx; -} - -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 , nr_used =0 ; -uint16_t end; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); +if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb)))return 0 ; - } vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) +if (unlikely (!vq->enabled))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 + dev->vhost_hlen; +if (dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF )) + is_mrg_rxbuf =1 ; -if (unlikely (reserve_avail_buf_mergeable (vq, pkt_len, - &end, buf_vec) <0 )) { -LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); +/* start enqueuing packets 1 by 1 */ + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (pkt_left && avail_idx != vq->last_used_idx) { +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], + is_mrg_rxbuf))break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - - *(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 += nr_used; - } - -if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); + pkt_idx++; + pkt_sent++; + pkt_left--; } -return pkt_idx; -} - -uint16_t -rte_vhost_enqueue_burst (int vid,uint16_t queue_id, -struct rte_mbuf **pkts,uint16_t count) -{ -struct virtio_net *dev =get_device (vid); - -if (!dev) -return 0 ; +/* update used idx and kick the guest if necessary */ +if (pkt_sent) +notify_guest (dev, vq); -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); +return pkt_sent; } static void
Signature · 1 line
2.7.4
Re: [PATCH v4 2/6] vhost: rewrite enqueue
Quoted 9 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
---
Changes in v4:
Quoted 8 lines
1. Refactor the code for clearer logic.
2. Add
PRINT_PACKETfor debugging.---
Changes in v3:1. Rewrite enqueue and delete the obsolete in the same patch.
Change log should go ---->
Quoted 2 lines
Signed-off-by Zhihong Wang <[email protected]>---
... here, after the SoB.
Quoted 40 lines
lib/librte_vhost/vhost_rxtx.c | 525 ++++++++++++------------------------------ 1 file changed, 145 insertions(+), 380 deletions(-)lib/librte_vhost/vhost_rxtx.c +9
-3@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {@@ -112,6 +112,10 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) cksum));break ; } + }else { + net_hdr->flags =0 ; + net_hdr->csum_start =0 ; + net_hdr->csum_offset =0 ; }if (m_buf->ol_flags &PKT_TX_TCP_SEG ) {@@ -122,437 +126,198 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) net_hdr->gso_size = m_buf->tso_segsz; net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len + m_buf->l4_len; + }else { + net_hdr->gso_type =0 ; + net_hdr->hdr_len =0 ; + net_hdr->gso_size =0 ; } } -static inline void -copy_virtio_net_hdr(struct virtio_net *dev, uint64_t desc_addr,
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 140
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +4
-5- struct virtio_net_hdr_mrg_rxbuf hdr) +static inline void __attribute__((always_inline)) +update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t desc_chain_head,uint32_t desc_chain_len) { -if (dev->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; +uint32_t used_idx_round = vq->last_used_idx & (vq->size -1 );
I'd suggest to use "used_idx", instead of "used_idx_round".
Quoted 23 lines
lib/librte_vhost/vhost_rxtx.c +11
-7+ + vq->used->ring[used_idx_round].id = desc_chain_head; + vq->used->ring[used_idx_round].len = desc_chain_len; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , + ring[used_idx_round]), + sizeof(vq->used->ring[used_idx_round])); } -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) +static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;struct vring_desc *desc;uint64_t desc_addr; -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 };
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 164
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +8
-2- - desc = &vq->desc[desc_idx]; +uint32_t desc_chain_head; +uint32_t desc_chain_len; +uint32_t desc_current; +uint32_t desc_offset; +uint32_t mbuf_len; +uint32_t mbuf_avail; +uint32_t copy_len; +uint32_t extra_buffers =0 ;
I'd name it to "num_buffers", to keep consistent with the virito hdr naming style.
Quoted 69 lines
lib/librte_vhost/vhost_rxtx.c +31
-30+ +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ; +if (unlikely (!desc_addr)) + goto error; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers = extra_buffers +1 ; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr);vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; - - 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); + desc_chain_len = desc_offset; + desc_addr += desc_offset; + +/* start copy from mbuf to desc */ +while (mbuf_avail || mbuf->next) { +/* get the next mbuf if the current done */ +if (!mbuf_avail) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; +/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */ + desc_offset =0 ;
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 213
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+ desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; + }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ +update_used_ring (dev, vq, desc_chain_head, + desc_chain_len); + vq->last_used_idx++;
Why not putting "vq->last_used_idx++" into update_used_ring()?
Quoted 31 lines
lib/librte_vhost/vhost_rxtx.c +17
-11+ extra_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto error; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; - desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; + desc_chain_len =0 ; + desc_offset =0 ; + }else + goto error; } - 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);
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 235
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +2
-8-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; +/* copy mbuf data */ + copy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail);
TBH, I'm okay with copy_len (actually, I prefer it slightly). However, the old code uses cpy_len, the current dequeue function also uses cpy_len, I then see no good reason to use copy_len here. It's really not a good idea to me to use two different naming styles in one source file.
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 238
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +3
-0+ rte_memcpy((void *)(uintptr_t)desc_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_avail),
I would keep the old var "mbuf_offset" and do not introduce "mbuf_len". This could avoid above calculation and make it straightforward.
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 245
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +7
-0+ copy_len); +vhost_log_write (dev, desc->addr + desc_offset, copy_len); +PRINT_PACKET (dev, (uintptr_t )desc_addr, copy_len,0 ); + mbuf_avail -= copy_len; + desc_offset += copy_len; + desc_addr += copy_len; + desc_chain_len += copy_len;
Vertical alighment[0] is not a must, but as you can see, it's a style I prefer. Meaning, if possible, please follow it.
[0]: https://en.wikipedia.org/wiki/Programming_style#Vertical_alignment
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +2
-2} -return 0 ; -} +update_used_ring (dev, vq, desc_chain_head, desc_chain_len); + vq->last_used_idx++;
...
Quoted 12 lines
lib/librte_vhost/vhost_rxtx.c +1
-10-rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; +return 0 ; - err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx); -if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]),
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 257
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +4
-5- sizeof(vq->used->ring[used_idx])); - } +error: +/* rollback on any error if last_used_idx update on-the-fly */ + vq->last_used_idx -= extra_buffers; -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } +return 1 ;
We normally returns -1 (but not 1) on error.
Quoted 4 lines
lib/librte_vhost/vhost_rxtx.c +3
-0+static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 264
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +1
-8rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ + vq->used->idx = vq->last_used_idx;
I will not drop the "volatile" cast here, sliently. You know this kind of stuff is tricky and would be painful to debug if it cause any issue. Such removal deserves a patch, as well as some explanations.
Quoted 11 lines
lib/librte_vhost/vhost_rxtx.c +2
-3+vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; }
...
Quoted 18 lines
lib/librte_vhost/vhost_rxtx.c +5
-12+/* start enqueuing packets 1 by 1 */ + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (pkt_left && avail_idx != vq->last_used_idx) { +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], + is_mrg_rxbuf))break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - - *(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 += nr_used; - } -
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 312
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +2
-7-if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); + pkt_idx++; + pkt_sent++;
pkt_idx and pkt_sent is duplicate here.
--yliu
Quoted 28 lines
lib/librte_vhost/vhost_rxtx.c +5
-15+ pkt_left--; } -return pkt_idx; -} - -uint16_t -rte_vhost_enqueue_burst (int vid,uint16_t queue_id, -struct rte_mbuf **pkts,uint16_t count) -{ -struct virtio_net *dev =get_device (vid); - -if (!dev) -return 0 ; +/* update used idx and kick the guest if necessary */ +if (pkt_sent) +notify_guest (dev, vq); -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); +return pkt_sent; } static voidSignature · 1 line
2.7.4
Re: [PATCH v4 2/6] vhost: rewrite enqueue
Hmmm, yet another email didn't send out successfully. Resend.
BTW, please work out v5 on top of the latest next-virtio tree.
Thanks.
--yliu
On Mon, Sep 05, 2016 at 02:39:25PM +0800, Yuanhan Liu wrote:
----
Quoted 9 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
---
Changes in v4:
Quoted 8 lines
1. Refactor the code for clearer logic.
2. Add
PRINT_PACKETfor debugging.---
Changes in v3:1. Rewrite enqueue and delete the obsolete in the same patch.
Change log should go ---->
Quoted 2 lines
Signed-off-by Zhihong Wang <[email protected]>---
... here, after the SoB.
Quoted 40 lines
lib/librte_vhost/vhost_rxtx.c | 525 ++++++++++++------------------------------ 1 file changed, 145 insertions(+), 380 deletions(-)lib/librte_vhost/vhost_rxtx.c +9
-3@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {@@ -112,6 +112,10 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) cksum));break ; } + }else { + net_hdr->flags =0 ; + net_hdr->csum_start =0 ; + net_hdr->csum_offset =0 ; }if (m_buf->ol_flags &PKT_TX_TCP_SEG ) {@@ -122,437 +126,198 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) net_hdr->gso_size = m_buf->tso_segsz; net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len + m_buf->l4_len; + }else { + net_hdr->gso_type =0 ; + net_hdr->hdr_len =0 ; + net_hdr->gso_size =0 ; } } -static inline void -copy_virtio_net_hdr(struct virtio_net *dev, uint64_t desc_addr,
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 140
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +4
-5- struct virtio_net_hdr_mrg_rxbuf hdr) +static inline void __attribute__((always_inline)) +update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t desc_chain_head,uint32_t desc_chain_len) { -if (dev->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; +uint32_t used_idx_round = vq->last_used_idx & (vq->size -1 );
I'd suggest to use "used_idx", instead of "used_idx_round".
Quoted 23 lines
lib/librte_vhost/vhost_rxtx.c +11
-7+ + vq->used->ring[used_idx_round].id = desc_chain_head; + vq->used->ring[used_idx_round].len = desc_chain_len; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , + ring[used_idx_round]), + sizeof(vq->used->ring[used_idx_round])); } -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) +static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;struct vring_desc *desc;uint64_t desc_addr; -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 };
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 164
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +8
-2- - desc = &vq->desc[desc_idx]; +uint32_t desc_chain_head; +uint32_t desc_chain_len; +uint32_t desc_current; +uint32_t desc_offset; +uint32_t mbuf_len; +uint32_t mbuf_avail; +uint32_t copy_len; +uint32_t extra_buffers =0 ;
I'd name it to "num_buffers", to keep consistent with the virito hdr naming style.
Quoted 69 lines
lib/librte_vhost/vhost_rxtx.c +31
-30+ +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ; +if (unlikely (!desc_addr)) + goto error; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers = extra_buffers +1 ; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr);vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; - - 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); + desc_chain_len = desc_offset; + desc_addr += desc_offset; + +/* start copy from mbuf to desc */ +while (mbuf_avail || mbuf->next) { +/* get the next mbuf if the current done */ +if (!mbuf_avail) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; +/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */ + desc_offset =0 ;
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 213
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+ desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; + }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ +update_used_ring (dev, vq, desc_chain_head, + desc_chain_len); + vq->last_used_idx++;
Why not putting "vq->last_used_idx++" into update_used_ring()?
Quoted 31 lines
lib/librte_vhost/vhost_rxtx.c +17
-11+ extra_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto error; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; - desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; + desc_chain_len =0 ; + desc_offset =0 ; + }else + goto error; } - 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);
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 235
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +2
-8-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; +/* copy mbuf data */ + copy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail);
TBH, I'm okay with copy_len (actually, I prefer it slightly). However, the old code uses cpy_len, the current dequeue function also uses cpy_len, I then see no good reason to use copy_len here. It's really not a good idea to me to use two different naming styles in one source file.
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 238
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +3
-0+ rte_memcpy((void *)(uintptr_t)desc_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_avail),
I would keep the old var "mbuf_offset" and do not introduce "mbuf_len". This could avoid above calculation and make it straightforward.
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 245
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +7
-0+ copy_len); +vhost_log_write (dev, desc->addr + desc_offset, copy_len); +PRINT_PACKET (dev, (uintptr_t )desc_addr, copy_len,0 ); + mbuf_avail -= copy_len; + desc_offset += copy_len; + desc_addr += copy_len; + desc_chain_len += copy_len;
Vertical alighment[0] is not a must, but as you can see, it's a style I prefer. Meaning, if possible, please follow it.
[0]: https://en.wikipedia.org/wiki/Programming_style#Vertical_alignment
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +2
-2} -return 0 ; -} +update_used_ring (dev, vq, desc_chain_head, desc_chain_len); + vq->last_used_idx++;
...
Quoted 12 lines
lib/librte_vhost/vhost_rxtx.c +1
-10-rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; +return 0 ; - err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx); -if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]),
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 257
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +4
-5- sizeof(vq->used->ring[used_idx])); - } +error: +/* rollback on any error if last_used_idx update on-the-fly */ + vq->last_used_idx -= extra_buffers; -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } +return 1 ;
We normally returns -1 (but not 1) on error.
Quoted 4 lines
lib/librte_vhost/vhost_rxtx.c +3
-0+static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 264
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +1
-8rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ + vq->used->idx = vq->last_used_idx;
I will not drop the "volatile" cast here, sliently. You know this kind of stuff is tricky and would be painful to debug if it cause any issue. Such removal deserves a patch, as well as some explanations.
Quoted 11 lines
lib/librte_vhost/vhost_rxtx.c +2
-3+vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; }
...
Quoted 18 lines
lib/librte_vhost/vhost_rxtx.c +5
-12+/* start enqueuing packets 1 by 1 */ + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (pkt_left && avail_idx != vq->last_used_idx) { +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], + is_mrg_rxbuf))break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - - *(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 += nr_used; - } -
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 312
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +2
-7-if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); + pkt_idx++; + pkt_sent++;
pkt_idx and pkt_sent is duplicate here.
--yliu
Quoted 28 lines
lib/librte_vhost/vhost_rxtx.c +5
-15+ pkt_left--; } -return pkt_idx; -} - -uint16_t -rte_vhost_enqueue_burst (int vid,uint16_t queue_id, -struct rte_mbuf **pkts,uint16_t count) -{ -struct virtio_net *dev =get_device (vid); - -if (!dev) -return 0 ; +/* update used idx and kick the guest if necessary */ +if (pkt_sent) +notify_guest (dev, vq); -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); +return pkt_sent; } static voidSignature · 1 line
2.7.4
Re: [PATCH v4 2/6] vhost: rewrite enqueue
Quoted 10 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Wednesday, September 7, 2016 1:33 PM
To: Wang, Zhihong <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v4 2/6] vhost: rewrite enqueue
Quoted 5 lines
Hmmm, yet another email didn't send out successfully. Resend.
BTW, please work out v5 on top of the latest next-virtio tree.
Thanks.
Okay. Thanks.
Quoted 407 lines
--yliuOn Mon, Sep 05, 2016 at 02:39:25PM +0800, Yuanhan Liu wrote:
----Quoted 9 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
---
Changes in v4:Quoted 8 lines
1. Refactor the code for clearer logic.
2. Add
PRINT_PACKETfor debugging.---
Changes in v3:1. Rewrite enqueue and delete the obsolete in the same patch.
Change log should go ---->
Quoted 2 lines
Signed-off-by Zhihong Wang <[email protected]>---
... here, after the SoB.
Quoted 1 line
lib/librte_vhost/vhost_rxtx.c | 525 ++++++++++++------------------------------
lib/librte_vhost/vhost_rxtx.c is_valid_virt_queue_idx(uint32_t idx, int is_tx,
Quoted 7 lines
1 file changed, 145 insertions(+), 380 deletions(-)lib/librte_vhost/vhost_rxtx.c +0
-0@@ -91,7 +91,7 @@ is_valid_virt_queue_idx(uint32_t idx, int is_tx,
uint32_tqp_nb)lib/librte_vhost/vhost_rxtx.c is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
Quoted 6 lines
lib/librte_vhost/vhost_rxtx.c +1
-1return (is_tx ^ (idx &1 )) ==0 && idx < qp_nb *VIRTIO_QNUM ; } -static void +static inline void __attribute__((always_inline)) virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr
*net_hdr)lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf,
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +0
-0{if (m_buf->ol_flags &PKT_TX_L4_MASK ) {@@ -112,6 +112,10 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf,struct
virtio_net_hdr*net_hdr)lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf
Quoted 11 lines
lib/librte_vhost/vhost_rxtx.c +4
-0cksum));break ; } + }else { + net_hdr->flags =0 ; + net_hdr->csum_start =0 ; + net_hdr->csum_offset =0 ; }if (m_buf->ol_flags &PKT_TX_TCP_SEG ) {@@ -122,437 +126,198 @@ virtio_enqueue_offload(struct rte_mbuf
*m_buf, structvirtio_net_hdr*net_hdr)Quoted 9 lines
lib/librte_vhost/vhost_rxtx.c +4
-0net_hdr->gso_size = m_buf->tso_segsz; net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len + m_buf->l4_len; + }else { + net_hdr->gso_type =0 ; + net_hdr->hdr_len =0 ; + net_hdr->gso_size =0 ; } }lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +3
-5-static inline void -copy_virtio_net_hdr (struct virtio_net *dev,uint64_t desc_addr, -struct virtio_net_hdr_mrg_rxbuf hdr) +static inline void __attribute__((always_inline)) +update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t desc_chain_head,uint32_t desc_chain_len) { -if (dev->vhost_hlen ==sizeof (struct virtio_net_hdr_mrg_rxbuf )) - *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc_addr =hdr;
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 140
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +1
-2-else - *(struct virtio_net_hdr *)(uintptr_t )desc_addr = hdr.hdr; +uint32_t used_idx_round = vq->last_used_idx & (vq->size -1 );I'd suggest to use "
used_idx", instead of "used_idx_round".Quoted 23 lines
lib/librte_vhost/vhost_rxtx.c +11
-7+ + vq->used->ring[used_idx_round].id = desc_chain_head; + vq->used->ring[used_idx_round].len = desc_chain_len; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , + ring[used_idx_round]), + sizeof(vq->used->ring[used_idx_round])); } -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) +static inline uint32_t __attribute__((always_inline)) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;struct vring_desc *desc;uint64_t desc_addr; -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 };lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 164
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +8
-2- - desc = &vq->desc[desc_idx]; +uint32_t desc_chain_head; +uint32_t desc_chain_len; +uint32_t desc_current; +uint32_t desc_offset; +uint32_t mbuf_len; +uint32_t mbuf_avail; +uint32_t copy_len; +uint32_t extra_buffers =0 ;I'd name it to "
num_buffers", to keep consistent with the virito hdr naming style.lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 13 lines
lib/librte_vhost/vhost_rxtx.c +9
-3+ +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which
lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +4
-5-* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ; +if (unlikely (!desc_addr)) + goto error; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf*)(uintptr_t)desc_addr;
Quoted 44 lines
lib/librte_vhost/vhost_rxtx.c +16
-22+virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers = extra_buffers +1 ; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr);vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; - - 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); + desc_chain_len = desc_offset; + desc_addr += desc_offset; + +/* start copy from mbuf to desc */ +while (mbuf_avail || mbuf->next) { +/* get the next mbuf if the current done */ +if (!mbuf_avail) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; +/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) {lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +10
-0+/* go on with the current desc chain */ + desc_offset =0 ; + desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; + }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ + update_used_ring(dev, vq,
desc_chain_head,lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf · line 213
Quoted 2 lines
lib/librte_vhost/vhost_rxtx.c +2
-0+ desc_chain_len); + vq->last_used_idx++;Why not putting "vq->last_used_idx++" into
update_used_ring()?lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +7
-0+ extra_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto error; + + desc_current = + vq->avail->ring[(vq->last_used_idx)&
Quoted 24 lines
lib/librte_vhost/vhost_rxtx.c +10
-11+ (vq->size - 1)]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; - desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; + desc_chain_len =0 ; + desc_offset =0 ; + }else + goto error; } - 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);lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 235
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +2
-8-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; +/* copy mbuf data */ + copy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail);TBH, I'm okay with
copy_len(actually, I prefer it slightly). However, the old code usescpy_len, the current dequeue function also usescpy_len, I then see no good reason to usecopy_lenhere. It's really not a good idea to me to use two different naming styles in one source file.lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 238
Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +3
-0+ rte_memcpy((void *)(uintptr_t)desc_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_avail),I would keep the old var "
mbuf_offset" and do not introduce "mbuf_len". This could avoid above calculation and make it straightforward.lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 245
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +7
-0+ copy_len); +vhost_log_write (dev, desc->addr + desc_offset, copy_len); +PRINT_PACKET (dev, (uintptr_t )desc_addr, copy_len,0 ); + mbuf_avail -= copy_len; + desc_offset += copy_len; + desc_addr += copy_len; + desc_chain_len += copy_len;Vertical alighment[0] is not a must, but as you can see, it's a style I prefer. Meaning, if possible, please follow it.
[0]: https://en.wikipedia.org/wiki/Programming_style#Vertical_alignment
Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +2
-2} -return 0 ; -} +update_used_ring (dev, vq, desc_chain_head, desc_chain_len); + vq->last_used_idx++;...
Quoted 12 lines
lib/librte_vhost/vhost_rxtx.c +1
-10-rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; +return 0 ; - err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx); -if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]),lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 257
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +4
-5- sizeof(vq->used->ring[used_idx])); - } +error: +/* rollback on any error if last_used_idx update on-the-fly */ + vq->last_used_idx -= extra_buffers; -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } +return 1 ;We normally returns -1 (but not 1) on error.
Quoted 4 lines
lib/librte_vhost/vhost_rxtx.c +3
-0+static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 264
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +1
-8rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ + vq->used->idx = vq->last_used_idx;I will not drop the "volatile" cast here, sliently. You know this kind of stuff is tricky and would be painful to debug if it cause any issue. Such removal deserves a patch, as well as some explanations.
Quoted 11 lines
lib/librte_vhost/vhost_rxtx.c +2
-3+vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; }...
Quoted 4 lines
lib/librte_vhost/vhost_rxtx.c +4
-0+/* start enqueuing packets 1 by 1 */ + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (pkt_left && avail_idx != vq->last_used_idx) { + if (enqueue_packet(dev, vq, avail_idx, pkts[pkt_idx],lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +1
-8+ is_mrg_rxbuf))break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += nr_used; - vhost_log_used_vring(dev, vq, offsetof(struct vring_used,idx),
Quoted 4 lines
lib/librte_vhost/vhost_rxtx.c +0
-4- sizeof(vq->used->idx)); - vq->last_used_idx += nr_used; - } -lib/librte_vhost/vhost_rxtx.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 312
Quoted 10 lines
lib/librte_vhost/vhost_rxtx.c +2
-7-if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); + pkt_idx++; + pkt_sent++;
pkt_idxandpkt_sentis duplicate here.--yliuQuoted 28 lines
lib/librte_vhost/vhost_rxtx.c +5
-16+ pkt_left--; } -return pkt_idx; -} - -uint16_t -rte_vhost_enqueue_burst (int vid,uint16_t queue_id, -struct rte_mbuf **pkts,uint16_t count) -{ -struct virtio_net *dev =get_device (vid); - -if (!dev) -return 0 ; +/* update used idx and kick the guest if necessary */ +if (pkt_sent) +notify_guest (dev, vq); -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); +return pkt_sent; } static void --2.7.4
PATCH v4 3/6 vhost: remove useless volatile
In reply to Zhihong WangThis patch removes useless volatile attribute to allow compiler optimization.
--- lib/librte_vhost/vhost-net.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost-net.h +1 -1
@@ -71,7 +71,7 @@ struct vhost_virtqueue {uint32_t size;/* Last index used on the available ring */ -volatile uint16_t last_used_idx; +uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
Signature · 1 line
2.7.4
PATCH v4 4/6 vhost: add desc prefetch
In reply to Zhihong WangThis patch adds descriptor prefetch to hide cache access latency.
--- lib/librte_vhost/vhost_rxtx.c | 6 ++++++ 1 file changed, 6 insertions(+)
lib/librte_vhost/vhost_rxtx.c +6 -0
@@ -304,6 +304,12 @@ rte_vhost_enqueue_burst(int vid, uint16_t queue_id,/* start enqueuing packets 1 by 1 */ avail_idx = *((volatile uint16_t *)&vq->avail->idx);while (pkt_left && avail_idx != vq->last_used_idx) { +/* prefetch the next desc */ +if (pkt_left >1 && avail_idx != vq->last_used_idx +1 ) +rte_prefetch0 (&vq->desc[vq->avail->ring[ + (vq->last_used_idx +1 ) & + (vq->size -1 )]]); +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], is_mrg_rxbuf))break ;
Signature · 1 line
2.7.4
PATCH v4 5/6 vhost: batch update used ring
In reply to Zhihong WangThis patch enables batch update of the used ring for better efficiency.
---
Changes in v4:
1. Free shadow used ring in the right place.
2. Add failure check for shadow used ring malloc.
--- lib/librte_vhost/vhost-net.h | 4 +++ lib/librte_vhost/vhost_rxtx.c | 62 ++++++++++++++++++++++++++++++++++++------- lib/librte_vhost/virtio-net.c | 42 ++++++++++++++++++++++++++--- 3 files changed, 95 insertions(+), 13 deletions(-)
lib/librte_vhost/vhost-net.h +4 -0
@@ -85,6 +85,10 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +/* Shadow used ring for performance */ +struct vring_used_elem *shadow_used_ring; +uint32_t shadow_used_idx; } __rte_cache_aligned;/* Old kernels have no such macro defined */
lib/librte_vhost/vhost_rxtx.c +52 -10
100 lines not on this page · raw message
lib/librte_vhost/virtio-net.c +39 -3
74 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v4 6/6 vhost: optimize cache access
In reply to Zhihong WangThis patch reorders the code to delay virtio header write to optimize cache access efficiency for cases where the mrg_rxbuf feature is turned on. It reduces CPU pipeline stall cycles significantly.
---
Changes in v3:
1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
--- lib/librte_vhost/vhost_rxtx.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
lib/librte_vhost/vhost_rxtx.c +14 -6
@@ -196,6 +196,7 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t mbuf_len;uint32_t mbuf_avail;uint32_t copy_len; +uint32_t copy_virtio_hdr;uint32_t extra_buffers =0 ;/* start with the first mbuf of the packet */
@@ -210,12 +211,12 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,if (unlikely (!desc_addr)) goto error; -/* handle virtio header */ +/* +* handle virtio header, the actual write operation is delayed +* for cache optimization, to reduce CPU pipeline stall cycles. +*/ virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; -virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); -if (is_mrg_rxbuf) - virtio_hdr->num_buffers = extra_buffers +1 ; - + copy_virtio_hdr =1 ;vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); desc_offset = dev->vhost_hlen;
@@ -266,8 +267,15 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq, goto error; } -/* copy mbuf data */ +/* copy virtio header and mbuf data */ copy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +if (copy_virtio_hdr) { + copy_virtio_hdr =0 ; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers = extra_buffers +1 ; + } + rte_memcpy((void *)(uintptr_t)desc_addr, rte_pktmbuf_mtod_offset(mbuf, void *, mbuf_len - mbuf_avail),
Signature · 1 line
2.7.4
v5 cover vhost: optimize enqueue
In reply to Zhihong WangThis patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed for high performance and good maintainability, and improves CPU efficiency significantly by optimizing cache access, which means:
* Higher maximum throughput can be achieved for fast frontends like DPDK virtio pmd.
* Better scalability can be achieved that each vhost core can support more connections because it takes less cycles to handle each single frontend.
This patch set contains:
1. A Windows VM compatibility fix for vhost enqueue in 16.07 release.
2. A baseline patch to rewrite the vhost logic.
3. A series of optimization patches added upon the baseline.
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Code reordering and batch used ring update bring most of the performance improvements.
In the existing code there're 2 callbacks for vhost enqueue:
* virtio_dev_merge_rx for mrg_rxbuf turned on cases.
* virtio_dev_rx for mrg_rxbuf turned off cases.
The performance of the existing code is not optimal, especially when the mrg_rxbuf feature turned on. Besides, having 2 callback paths increases maintenance efforts.
Also, there's a compatibility issue in the existing code which causes Windows VM to hang when the mrg_rxbuf feature turned on.
---
Changes in v5:
1. Rebase to the latest branch.
2. Rename variables to keep consistent in naming style.
3. Small changes like return value adjustment and vertical alignment.
4. Add details in commit log.
---
Changes in v4:
1. Fix a Windows VM compatibility issue.
2. Free shadow used ring in the right place.
3. Add failure check for shadow used ring malloc.
4. Refactor the code for clearer logic.
5. Add PRINT_PACKET for debugging.
---
Changes in v3:
1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
3. Rewrite enqueue and delete the obsolete in the same patch.
---
Changes in v2:
1. Split the big function into several small ones.
2. Use multiple patches to explain each optimization.
3. Add comments.
Zhihong Wang (6):
vhost: fix windows vm hang
vhost: rewrite enqueue
vhost: remove useless volatile
vhost: add desc prefetch
vhost: batch update used ring
vhost: optimize cache access
lib/librte_vhost/vhost.c | 20 +- lib/librte_vhost/vhost.h | 6 +- lib/librte_vhost/vhost_user.c | 31 ++- lib/librte_vhost/virtio_net.c | 561 +++++++++++++++--------------------------- 4 files changed, 242 insertions(+), 376 deletions(-)
Signature · 1 line
2.7.4
PATCH v5 1/6 vhost: fix windows vm hang
This patch fixes a Windows VM compatibility issue in DPDK 16.07 vhost code which causes the guest to hang once any packets are enqueued when mrg_rxbuf is turned on by setting the right id and len in the used ring.
As defined in virtio spec 0.95 and 1.0, in each used ring element, id means index of start of used descriptor chain, and len means total length of the descriptor chain which was written to. While in 16.07 code, index of the last descriptor is assigned to id, and the length of the last descriptor is assigned to len.
How to test?
1. Start testpmd in the host with a vhost port.
2. Start a Windows VM image with qemu and connect to the vhost port.
3. Start io forwarding with tx_first in host testpmd.
For 16.07 code, the Windows VM will hang once any packets are enqueued.
---
Changes in v5:
1. Add details in commit log.
lib/librte_vhost/virtio_net.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
lib/librte_vhost/virtio_net.c +12 -5
@@ -384,6 +384,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,uint16_t start_idx = vq->last_used_idx;uint16_t cur_idx = start_idx;uint64_t desc_addr; +uint32_t desc_chain_head; +uint32_t desc_chain_len;uint32_t mbuf_offset, mbuf_avail;uint32_t desc_offset, desc_avail;uint32_t cpy_len;
@@ -412,6 +414,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; desc_offset = dev->vhost_hlen; + desc_chain_head = buf_vec[vec_idx].desc_idx; + desc_chain_len = desc_offset; mbuf_avail =rte_pktmbuf_data_len (m); mbuf_offset =0 ;
@@ -419,19 +423,21 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,/* done with current desc buf, get the next one */ if (desc_avail ==0 ) { desc_idx = buf_vec[vec_idx].desc_idx; + vec_idx++;if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) {/* 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; + vq->used->ring[used_idx].id = desc_chain_head; + vq->used->ring[used_idx].len = desc_chain_len;vhost_log_used_vring (dev, vq, offsetof(struct vring_used , ring[used_idx]), sizeof(vq->used->ring[used_idx])); + desc_chain_head = buf_vec[vec_idx].desc_idx; + desc_chain_len =0 ; } - vec_idx++; desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr);if (unlikely (!desc_addr))return 0 ;
@@ -463,11 +469,12 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, mbuf_offset += cpy_len; desc_avail -= cpy_len; desc_offset += cpy_len; + desc_chain_len += cpy_len; } used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = buf_vec[vec_idx].desc_idx; - vq->used->ring[used_idx].len = desc_offset; + vq->used->ring[used_idx].id = desc_chain_head; + vq->used->ring[used_idx].len = desc_chain_len;vhost_log_used_vring (dev, vq, offsetof(struct vring_used, ring[used_idx]), sizeof(vq->used->ring[used_idx]));
Signature · 1 line
2.7.4
PATCH v5 2/6 vhost: rewrite enqueue
In reply to Zhihong WangThis patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
---
Changes in v5:
1. Rebase to the latest branch.
2. Rename variables to keep consistent in naming style.
3. Small changes like return value adjustment and vertical alignment.
---
Changes in v4:
1. Refactor the code for clearer logic.
2. Add PRINT_PACKET for debugging.
---
Changes in v3:
1. Rewrite enqueue and delete the obsolete in the same patch.
lib/librte_vhost/virtio_net.c | 514 ++++++++++++------------------------------ 1 file changed, 138 insertions(+), 376 deletions(-)
lib/librte_vhost/virtio_net.c +138 -376
@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {
@@ -112,6 +112,10 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) cksum));break ; } + }else { + net_hdr->flags =0 ; + net_hdr->csum_start =0 ; + net_hdr->csum_offset =0 ; }if (m_buf->ol_flags &PKT_TX_TCP_SEG ) {
@@ -122,439 +126,197 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) net_hdr->gso_size = m_buf->tso_segsz; net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len + m_buf->l4_len; + }else { + net_hdr->gso_type =0 ; + net_hdr->hdr_len =0 ; + net_hdr->gso_size =0 ; } } -static inline void -copy_virtio_net_hdr (struct virtio_net *dev,uint64_t desc_addr, -struct virtio_net_hdr_mrg_rxbuf hdr) +static inline void __attribute__((always_inline)) +update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t desc_chain_head,uint32_t desc_chain_len) { -if (dev->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; +uint32_t used_idx = vq->last_used_idx & (vq->size -1 ); + + vq->used->ring[used_idx].id = desc_chain_head; + vq->used->ring[used_idx].len = desc_chain_len; + vq->last_used_idx++; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , + ring[used_idx]), + sizeof(vq->used->ring[used_idx])); }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) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;struct vring_desc *desc;uint64_t desc_addr; -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; +uint32_t desc_chain_head; +uint32_t desc_chain_len; +uint32_t desc_current; +uint32_t desc_offset; +uint32_t mbuf_len; +uint32_t mbuf_avail; +uint32_t cpy_len; +uint32_t num_buffers =0 ; - desc = &vq->desc[desc_idx]; +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ; +if (unlikely (!desc_addr)) + goto error; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers =1 ; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr);vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; - - 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); + desc_chain_len = desc_offset; + desc_addr += desc_offset; + +/* start copy from mbuf to desc */ +while (mbuf_avail || mbuf->next) { +/* get the next mbuf if the current done */ +if (!mbuf_avail) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; +/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */ + desc_offset =0 ; + desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; + }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ +update_used_ring (dev, vq, desc_chain_head, + desc_chain_len); + num_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto error; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; - desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; + desc_chain_len =0 ; + desc_offset =0 ; + }else + goto error; } - 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); +/* copy mbuf data */ + cpy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +rte_memcpy ((void *)(uintptr_t )desc_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_avail), + 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 ; -} - -/** -* 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 -* count is returned to indicate the number of packets that are succesfully -* added to the RX queue. This function works when the mbuf is scattered, but -* it doesn't support the mergeable feature. -*/ -static inline uint32_t __attribute__((always_inline)) -virtio_dev_rx (struct virtio_net *dev,uint16_t queue_id, -struct rte_mbuf **pkts,uint32_t count) -{ -struct vhost_virtqueue *vq; -uint16_t avail_idx, free_entries, start_idx; -uint16_t desc_indexes[MAX_PKT_BURST ]; -uint16_t used_idx; -uint32_t i; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); -return 0 ; +PRINT_PACKET (dev, (uintptr_t )desc_addr, cpy_len,0 ); + mbuf_avail -= cpy_len; + desc_addr += cpy_len; + desc_offset += cpy_len; + desc_chain_len += cpy_len; } - vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) -return 0 ; +update_used_ring (dev, vq, desc_chain_head, desc_chain_len); - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - start_idx = vq->last_used_idx; - free_entries = avail_idx - start_idx; - count =RTE_MIN (count, free_entries); - count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); -if (count ==0 ) -return 0 ; - -LOG_DEBUG (VHOST_DATA ,"(%d) start_idx %d | end_idx %d\n" , - dev->vid, start_idx, start_idx + count); - -/* Retrieve all of the desc indexes first to avoid caching issues. */ -rte_prefetch0 (&vq->avail->ring[start_idx & (vq->size -1 )]); -for (i =0 ; i < count; i++) { - used_idx = (start_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; - vq->used->ring[used_idx].id = desc_indexes[i]; - vq->used->ring[used_idx].len = pkts[i]->pkt_len + - dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } - -rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; +return 0 ; - err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx); -if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } +error: +/* rollback on any error if last_used_idx update on-the-fly */ + vq->last_used_idx -= num_buffers; -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } +return -1 ; +} +static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ + *(volatile uint16_t *)&vq->used->idx = vq->last_used_idx; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; } -static inline int -fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, -uint32_t *allocated,uint32_t *vec_idx, -struct buf_vector *buf_vec) -{ -uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; -uint32_t vec_id = *vec_idx; -uint32_t len = *allocated; - -while (1 ) { -if (unlikely (vec_id >=BUF_VECTOR_MAX || idx >= vq->size)) -return -1 ; - - len += vq->desc[idx].len; - buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - buf_vec[vec_id].buf_len = vq->desc[idx].len; - 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 ; -} - -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, -uint16_t *end,struct buf_vector *buf_vec) +uint16_t +rte_vhost_enqueue_burst (int vid,uint16_t queue_id, +struct rte_mbuf **pkts,uint16_t count) { -uint16_t cur_idx; +struct vhost_virtqueue *vq; +struct virtio_net *dev; +uint32_t is_mrg_rxbuf =0 ; +uint32_t pkt_idx =0 ; +uint32_t pkt_left = count;uint16_t avail_idx; -uint32_t allocated =0 ; -uint32_t vec_idx =0 ; -uint16_t tries =0 ; - - cur_idx = vq->last_used_idx; - -while (1 ) { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (cur_idx == avail_idx)) -return -1 ; - -if (unlikely (fill_vec_buf (vq, cur_idx, &allocated, - &vec_idx, buf_vec) <0 )) -return -1 ; - - 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 (unlikely (tries >= vq->size)) -return -1 ; - } - *end = cur_idx; -return 0 ; -} - -static inline uint32_t __attribute__((always_inline)) -copy_mbuf_to_desc_mergeable (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint16_t end_idx,struct rte_mbuf *m, -struct buf_vector *buf_vec) -{ -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint32_t vec_idx =0 ; -uint16_t start_idx = vq->last_used_idx; -uint16_t cur_idx = start_idx; -uint64_t desc_addr; -uint32_t desc_chain_head; -uint32_t desc_chain_len; -uint32_t mbuf_offset, mbuf_avail; -uint32_t desc_offset, desc_avail; -uint32_t cpy_len; -uint16_t desc_idx, used_idx; - -if (unlikely (m ==NULL )) +if (unlikely (!pkt_left))return 0 ; -LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, cur_idx, end_idx); + pkt_left =RTE_MIN ((uint32_t )MAX_PKT_BURST , pkt_left); - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) + dev =get_device (vid); +if (unlikely (!dev))return 0 ; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - - virtio_hdr.num_buffers = end_idx - start_idx; -LOG_DEBUG (VHOST_DATA ,"(%d) RX: num merge buffers %d\n" , - dev->vid, virtio_hdr.num_buffers); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - - desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; - desc_offset = dev->vhost_hlen; - desc_chain_head = buf_vec[vec_idx].desc_idx; - desc_chain_len = desc_offset; - - 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 = buf_vec[vec_idx].desc_idx; - vec_idx++; - -if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) { -/* Update used ring with desc information */ - used_idx = cur_idx++ & (vq->size -1 ); - vq->used->ring[used_idx].id = desc_chain_head; - vq->used->ring[used_idx].len = desc_chain_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - desc_chain_head = buf_vec[vec_idx].desc_idx; - desc_chain_len =0 ; - } - - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (unlikely (!desc_addr)) -return 0 ; - -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - desc_offset =0 ; - desc_avail = buf_vec[vec_idx].buf_len; - } - -/* done with current mbuf, get the next one */ -if (mbuf_avail ==0 ) { - m = m->next; - - mbuf_offset =0 ; - mbuf_avail =rte_pktmbuf_data_len (m); - } - - 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, buf_vec[vec_idx].buf_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; - desc_chain_len += cpy_len; - } - - used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = desc_chain_head; - vq->used->ring[used_idx].len = desc_chain_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - -return end_idx - start_idx; -} - -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 , nr_used =0 ; -uint16_t end; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); +if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb)))return 0 ; - } vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) +if (unlikely (!vq->enabled))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 + dev->vhost_hlen; +if (dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF )) + is_mrg_rxbuf =1 ; -if (unlikely (reserve_avail_buf_mergeable (vq, pkt_len, - &end, buf_vec) <0 )) { -LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); +/* start enqueuing packets 1 by 1 */ + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (pkt_left && avail_idx != vq->last_used_idx) { +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], + is_mrg_rxbuf))break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - *(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 += nr_used; + pkt_idx++; + pkt_left--; } -if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); - -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); - } +/* update used idx and kick the guest if necessary */ +if (pkt_idx) +notify_guest (dev, vq);return pkt_idx; } -uint16_t -rte_vhost_enqueue_burst (int vid,uint16_t queue_id, -struct rte_mbuf **pkts,uint16_t count) -{ -struct virtio_net *dev =get_device (vid); - -if (!dev) -return 0 ; - -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); -} -static void parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) {
Signature · 1 line
2.7.4
Re: [PATCH v5 2/6] vhost: rewrite enqueue
Hi,
Quoted 201 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
Signed-off-by Zhihong Wang <[email protected]>---
Changes in v5:1. Rebase to the latest branch.
2. Rename variables to keep consistent in naming style.
3. Small changes like return value adjustment and vertical alignment.
---
Changes in v4:1. Refactor the code for clearer logic.
2. Add
PRINT_PACKETfor debugging.---
Changes in v3:1. Rewrite enqueue and delete the obsolete in the same patch.
lib/librte_vhost/virtio_net.c | 514 ++++++++++++------------------------------ 1 file changed, 138 insertions(+), 376 deletions(-)lib/librte_vhost/virtio_net.c +84
-45@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {@@ -112,6 +112,10 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) cksum));break ; } + }else { + net_hdr->flags =0 ; + net_hdr->csum_start =0 ; + net_hdr->csum_offset =0 ; }if (m_buf->ol_flags &PKT_TX_TCP_SEG ) {@@ -122,439 +126,197 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) net_hdr->gso_size = m_buf->tso_segsz; net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len + m_buf->l4_len; + }else { + net_hdr->gso_type =0 ; + net_hdr->hdr_len =0 ; + net_hdr->gso_size =0 ; } } -static inline void -copy_virtio_net_hdr (struct virtio_net *dev,uint64_t desc_addr, -struct virtio_net_hdr_mrg_rxbuf hdr) +static inline void __attribute__((always_inline)) +update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t desc_chain_head,uint32_t desc_chain_len) { -if (dev->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; +uint32_t used_idx = vq->last_used_idx & (vq->size -1 ); + + vq->used->ring[used_idx].id = desc_chain_head; + vq->used->ring[used_idx].len = desc_chain_len; + vq->last_used_idx++; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , + ring[used_idx]), + sizeof(vq->used->ring[used_idx])); }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) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;struct vring_desc *desc;uint64_t desc_addr; -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; +uint32_t desc_chain_head; +uint32_t desc_chain_len; +uint32_t desc_current; +uint32_t desc_offset; +uint32_t mbuf_len; +uint32_t mbuf_avail; +uint32_t cpy_len; +uint32_t num_buffers =0 ; - desc = &vq->desc[desc_idx]; +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ; +if (unlikely (!desc_addr)) + goto error; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers =1 ; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr);vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; - - 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); + desc_chain_len = desc_offset; + desc_addr += desc_offset; + +/* start copy from mbuf to desc */ +while (mbuf_avail || mbuf->next) { +/* get the next mbuf if the current done */ +if (!mbuf_avail) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; +/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */ + desc_offset =0 ; + desc_current = desc->next; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error; + }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ +update_used_ring (dev, vq, desc_chain_head, + desc_chain_len); + num_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto error; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error;
lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 229
Quoted 10 lines
lib/librte_vhost/virtio_net.c +2
-7- desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; + desc_chain_len =0 ; + desc_offset =0 ;
As I commented on v3, there is code duplication between next flag, and mrg buf cases:desc_offset = 0;
and:
desc = &vq->desc[desc_current];desc_addr = gpa_to_vva(dev, desc->addr);
if (unlikely(!desc_addr))
goto error;
Regards,
Maxime
Re: [PATCH v5 2/6] vhost: rewrite enqueue
Quoted 23 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ desc_current = + vq->avail->ring[(vq->last_used_idx)&
Quoted 6 lines
lib/librte_vhost/virtio_net.c +6
-0+ (vq->size - 1)]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error;lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 229
Quoted 10 lines
lib/librte_vhost/virtio_net.c +2
-7- desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; + desc_chain_len =0 ; + desc_offset =0 ;As I commented on v3, there is code duplication between next flag, and mrg buf cases:
desc_offset= 0;
Quoted 7 lines
and:
desc = &vq->desc[desc_current];
desc_addr=gpa_to_vva(dev, desc->addr);
if (unlikely(!desc_addr))goto error;
Do you mean to add something like:
static inline int __attribute__((always_inline)) get_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
uint32_t desc_idx, struct vring_desc **desc, uint64_t *desc_addr)
{
*desc = &vq->desc[desc_idx];
*desc_addr = gpa_to_vva(dev, (*desc)->addr);
if (unlikely(!(*desc_addr)))
return -1;
return 0;
}Quoted 2 lines
Regards,
Maxime
Re: [PATCH v5 2/6] vhost: rewrite enqueue
Hi,
Quoted 37 lines · Zhihong
Quoted 23 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ desc_current = + vq->avail->ring[(vq->last_used_idx)&
Quoted 6 lines
lib/librte_vhost/virtio_net.c +6
-0+ (vq->size - 1)]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; + desc_addr =gpa_to_vva (dev, desc->addr); +if (unlikely (!desc_addr)) + goto error;lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 229
Quoted 10 lines
lib/librte_vhost/virtio_net.c +2
-7- desc = &vq->desc[desc->next]; - desc_addr =gpa_to_vva (dev, desc->addr); -if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; + desc_chain_len =0 ; + desc_offset =0 ;As I commented on v3, there is code duplication between next flag, and mrg buf cases:
desc_offset= 0;Quoted 7 lines
and:
desc = &vq->desc[desc_current];
desc_addr=gpa_to_vva(dev, desc->addr);
if (unlikely(!desc_addr))goto error;Do you mean to add something like:
static inline int __attribute__((always_inline)) get_desc(struct
virtio_net*dev, structvhost_virtqueue*vq,
uint32_tdesc_idx, structvring_desc**desc,uint64_t*desc_addr)
Quoted 8 lines
{
*desc = &vq->desc[desc_idx];
*desc_addr = gpa_to_vva(dev, (*desc)->addr);
if (unlikely(!(*desc_addr)))
return -1;
return 0;
}I meant, move this code after the if/else.
You can do it in a function if it is done elsewhere in the file.
Re: [PATCH v5 2/6] vhost: rewrite enqueue
In reply to Zhihong Wanglib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 293
Quoted 13 lines · Zhihong Wang
lib/librte_vhost/virtio_net.c +3
-8+static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */
Please don't remove comments if not justified.
Here the comment is important, as it explains why the barrier is needed.
Quoted 11 lines
lib/librte_vhost/virtio_net.c +3
-3+ *(volatile uint16_t *)&vq->used->idx = vq->last_used_idx; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; }
Re: [PATCH v5 2/6] vhost: rewrite enqueue
Quoted 13 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Tuesday, September 13, 2016 12:27 AM
To: Wang, Zhihong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v5 2/6] vhost: rewrite enqueueQuoted 1 line · Zhihong Wang
Quoted 14 lines
lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 293
Quoted 12 lines
lib/librte_vhost/virtio_net.c +3
-8+static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ Please don't remove comments if not justified.
Here the comment is important, as it explains why the barrier is needed.
Okay.
Quoted 11 lines
Quoted 11 lines
lib/librte_vhost/virtio_net.c +3
-3+ *(volatile uint16_t *)&vq->used->idx = vq->last_used_idx; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx));rte_mb (); - -/* Kick the guest if necessary. */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; }
Re: [PATCH v5 2/6] vhost: rewrite enqueue
In reply to Zhihong WangQuoted 2 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
As always, your commit log just states what have been done, but doesn't tell why such changes have been made. For example, you said "it's designed for high performance", then you'd better explain why your version would introduce high performance. You need a reason, as well as some numbers (percent change) to prove it: it's not that right to keep the numbers
inside: I'm sure people outside intel are also willing and happy to know those numbers.
For this patch, I think it's more about the maintainability improvement but not performance: the performance tunning patches are done later after all.
Another example is, in patch 6, you said "It reduces CPU pipeline stall cycles significantly", but you didn't say why there is pipeline stall before and why your patch reduces it.
All those are important things that deserves some explanation. So, I'd ask you to re-visit all your patches in this set, to think what you could add to make the commit better and more informative.
Besides that, I think this patchset looks fine to me. I may just need another time to look it more carefully, then I think I can merge (v6).
BTW, thanks for the great work!
--yliu
Quoted 5 lines
This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
Signed-off-by Zhihong Wang <[email protected]>---
Re: [PATCH v5 2/6] vhost: rewrite enqueue
Quoted 26 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Sunday, September 18, 2016 10:19 PM
To: Wang, Zhihong <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 2/6] vhost: rewrite enqueueQuoted 2 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function designed for high performance and better maintainability.
As always, your commit log just states what have been done, but doesn't tell why such changes have been made. For example, you said "it's designed for high performance", then you'd better explain why your version would introduce high performance. You need a reason, as well as some numbers (percent change) to prove it: it's not that right to keep the numbers
inside: I'm sure people outside intel are also willing and happy to know those numbers.For this patch, I think it's more about the maintainability improvement but not performance: the performance tunning patches are done later after all.
Quoted 7 lines
Another example is, in patch 6, you said "It reduces CPU pipeline stall cycles significantly", but you didn't say why there is pipeline stall before and why your patch reduces it.
All those are important things that deserves some explanation. So, I'd ask you to re-visit all your patches in this set, to think what you could add to make the commit better and more informative.
Okay. I'll add more detailed commit log.
Quoted 13 lines
Besides that, I think this patchset looks fine to me. I may just need another time to look it more carefully, then I think I can merge (v6).
BTW, thanks for the great work!
--yliuQuoted 5 lines
This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
Signed-off-by Zhihong Wang <[email protected]>---
PATCH v5 3/6 vhost: remove useless volatile
In reply to Zhihong WangThis patch removes useless volatile attribute to allow compiler optimization.
--- lib/librte_vhost/vhost.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost.h +1 -1
@@ -71,7 +71,7 @@ struct vhost_virtqueue {uint32_t size;/* Last index used on the available ring */ -volatile uint16_t last_used_idx; +uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
Signature · 1 line
2.7.4
PATCH v5 4/6 vhost: add desc prefetch
In reply to Zhihong WangThis patch adds descriptor prefetch to hide cache access latency.
--- lib/librte_vhost/virtio_net.c | 6 ++++++ 1 file changed, 6 insertions(+)
lib/librte_vhost/virtio_net.c +6 -0
@@ -302,6 +302,12 @@ rte_vhost_enqueue_burst(int vid, uint16_t queue_id,/* start enqueuing packets 1 by 1 */ avail_idx = *((volatile uint16_t *)&vq->avail->idx);while (pkt_left && avail_idx != vq->last_used_idx) { +/* prefetch the next desc */ +if (pkt_left >1 && avail_idx != vq->last_used_idx +1 ) +rte_prefetch0 (&vq->desc[vq->avail->ring[ + (vq->last_used_idx +1 ) & + (vq->size -1 )]]); +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], is_mrg_rxbuf))break ;
Signature · 1 line
2.7.4
PATCH v5 5/6 vhost: batch update used ring
In reply to Zhihong WangThis patch enables batch update of the used ring for better efficiency.
---
Changes in v4:
1. Free shadow used ring in the right place.
2. Add failure check for shadow used ring malloc.
lib/librte_vhost/vhost.c | 20 ++++++++++++-- lib/librte_vhost/vhost.h | 4 +++ lib/librte_vhost/vhost_user.c | 31 +++++++++++++++++---- lib/librte_vhost/virtio_net.c | 64 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 101 insertions(+), 18 deletions(-)
lib/librte_vhost/vhost.c +18 -2
@@ -119,10 +119,26 @@ cleanup_device(struct virtio_net *dev, int destroy)static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq_0; +struct vhost_virtqueue *vq_1;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq_0 = dev->virtqueue[i *VIRTIO_QNUM ]; +if (vq_0->shadow_used_ring) { +rte_free (vq_0->shadow_used_ring); + vq_0->shadow_used_ring =NULL ; + } + + vq_1 = dev->virtqueue[i *VIRTIO_QNUM +1 ]; +if (vq_1->shadow_used_ring) { +rte_free (vq_1->shadow_used_ring); + vq_1->shadow_used_ring =NULL ; + } + +/* malloc together, free together */ +rte_free (vq_0); + }rte_free (dev); }
lib/librte_vhost/vhost.h +4 -0
@@ -85,6 +85,10 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +/* Shadow used ring for performance */ +struct vring_used_elem *shadow_used_ring; +uint32_t shadow_used_idx; } __rte_cache_aligned;/* Old kernels have no such macro defined */
lib/librte_vhost/vhost_user.c +26 -5
@@ -193,7 +193,21 @@ static int vhost_user_set_vring_num(struct virtio_net *dev, struct vhost_vring_state *state) { - dev->virtqueue[state->index]->size = state->num; + struct vhost_virtqueue *vq; + + vq = dev->virtqueue[state->index]; + vq->size = state->num; +if (!vq->shadow_used_ring) { + vq->shadow_used_ring =rte_malloc (NULL , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE ); +if (!vq->shadow_used_ring) { +RTE_LOG (ERR ,VHOST_CONFIG , +"Failed to allocate memory" +" for shadow used ring.\n" ); +return -1 ; + } + }return 0 ; }
@@ -611,14 +625,21 @@ static int vhost_user_get_vring_base(struct virtio_net *dev, struct vhost_vring_state *state) { + struct vhost_virtqueue *vq; +/* We have to stop the queue (virtio) if it is running. */ if (dev->flags &VIRTIO_DEV_RUNNING ) { dev->flags &= ~VIRTIO_DEV_RUNNING ; notify_ops->destroy_device (dev->vid); } + vq = dev->virtqueue[state->index];/* Here we are safe to get the last used index */ - state->num = dev->virtqueue[state->index]->last_used_idx; + state->num = vq->last_used_idx; +if (vq->shadow_used_ring) { +rte_free (vq->shadow_used_ring); + vq->shadow_used_ring =NULL ; + }RTE_LOG (INFO ,VHOST_CONFIG ,"vring base idx:%d file:%d\n" , state->index, state->num);
@@ -627,10 +648,10 @@ vhost_user_get_vring_base(struct virtio_net *dev,* sent and only sent in vhost_vring_stop. * TODO: cleanup the vring, it isn't usable since here. */ -if (dev->virtqueue[state->index]->kickfd >=0 ) -close (dev->virtqueue[state->index]->kickfd); +if (vq->kickfd >=0 ) +close (vq->kickfd); - dev->virtqueue[state->index]->kickfd =VIRTIO_UNINITIALIZED_EVENTFD ; + vq->kickfd =VIRTIO_UNINITIALIZED_EVENTFD ;return 0 ; }
lib/librte_vhost/virtio_net.c +53 -11
102 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v5 5/6] vhost: batch update used ring
Quoted 182 lines · Zhihong Wang
This patch enables batch update of the used ring for better efficiency.
Signed-off-by Zhihong Wang <[email protected]>---
Changes in v4:1. Free shadow used ring in the right place.
2. Add failure check for shadow used ring malloc.
lib/librte_vhost/vhost.c | 20 ++++++++++++-- lib/librte_vhost/vhost.h | 4 +++ lib/librte_vhost/vhost_user.c | 31 +++++++++++++++++---- lib/librte_vhost/virtio_net.c | 64 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 101 insertions(+), 18 deletions(-)lib/librte_vhost/vhost.c +18
-2@@ -119,10 +119,26 @@ cleanup_device(struct virtio_net *dev, int destroy)static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq_0; +struct vhost_virtqueue *vq_1;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq_0 = dev->virtqueue[i *VIRTIO_QNUM ]; +if (vq_0->shadow_used_ring) { +rte_free (vq_0->shadow_used_ring); + vq_0->shadow_used_ring =NULL ; + } + + vq_1 = dev->virtqueue[i *VIRTIO_QNUM +1 ]; +if (vq_1->shadow_used_ring) { +rte_free (vq_1->shadow_used_ring); + vq_1->shadow_used_ring =NULL ; + } + +/* malloc together, free together */ +rte_free (vq_0); + }rte_free (dev); }lib/librte_vhost/vhost.h +4
-0@@ -85,6 +85,10 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +/* Shadow used ring for performance */ +struct vring_used_elem *shadow_used_ring; +uint32_t shadow_used_idx; } __rte_cache_aligned;/* Old kernels have no such macro defined */ lib/librte_vhost/vhost_user.c +26
-5@@ -193,7 +193,21 @@ static int vhost_user_set_vring_num(struct virtio_net *dev, struct vhost_vring_state *state) { - dev->virtqueue[state->index]->size = state->num; + struct vhost_virtqueue *vq; + + vq = dev->virtqueue[state->index]; + vq->size = state->num; +if (!vq->shadow_used_ring) { + vq->shadow_used_ring =rte_malloc (NULL , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE ); +if (!vq->shadow_used_ring) { +RTE_LOG (ERR ,VHOST_CONFIG , +"Failed to allocate memory" +" for shadow used ring.\n" ); +return -1 ; + } + }return 0 ; }@@ -611,14 +625,21 @@ static int vhost_user_get_vring_base(struct virtio_net *dev, struct vhost_vring_state *state) { + struct vhost_virtqueue *vq; +/* We have to stop the queue (virtio) if it is running. */ if (dev->flags &VIRTIO_DEV_RUNNING ) { dev->flags &= ~VIRTIO_DEV_RUNNING ; notify_ops->destroy_device (dev->vid); } + vq = dev->virtqueue[state->index];/* Here we are safe to get the last used index */ - state->num = dev->virtqueue[state->index]->last_used_idx; + state->num = vq->last_used_idx; +if (vq->shadow_used_ring) { +rte_free (vq->shadow_used_ring); + vq->shadow_used_ring =NULL ; + }RTE_LOG (INFO ,VHOST_CONFIG ,"vring base idx:%d file:%d\n" , state->index, state->num);@@ -627,10 +648,10 @@ vhost_user_get_vring_base(struct virtio_net *dev,* sent and only sent in vhost_vring_stop. * TODO: cleanup the vring, it isn't usable since here. */ -if (dev->virtqueue[state->index]->kickfd >=0 ) -close (dev->virtqueue[state->index]->kickfd); +if (vq->kickfd >=0 ) +close (vq->kickfd); - dev->virtqueue[state->index]->kickfd =VIRTIO_UNINITIALIZED_EVENTFD ; + vq->kickfd =VIRTIO_UNINITIALIZED_EVENTFD ;return 0 ; }lib/librte_vhost/virtio_net.c +35
-9@@ -134,17 +134,52 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) }static inline void __attribute__((always_inline)) -update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint32_t desc_chain_head,uint32_t desc_chain_len) +update_used_ring (struct vhost_virtqueue *vq,uint32_t desc_chain_head, +uint32_t desc_chain_len) { -uint32_t used_idx = vq->last_used_idx & (vq->size -1 ); - - vq->used->ring[used_idx].id = desc_chain_head; - vq->used->ring[used_idx].len = desc_chain_len; + vq->shadow_used_ring[vq->shadow_used_idx].id = desc_chain_head; + vq->shadow_used_ring[vq->shadow_used_idx].len = desc_chain_len; + vq->shadow_used_idx++; vq->last_used_idx++; -vhost_log_used_vring (dev, vq, offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); +} + +static inline void __attribute__((always_inline)) +flush_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t used_idx_start) +{ +if (used_idx_start + vq->shadow_used_idx < vq->size) { +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + vq->shadow_used_idx * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + vq->shadow_used_idx * + sizeof(struct vring_used_elem)); + }else { +uint32_t part_1 = vq->size - used_idx_start; +uint32_t part_2 = vq->shadow_used_idx - part_1; + +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + part_1 * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + part_1 * + sizeof(struct vring_used_elem)); + rte_memcpy(&vq->used->ring[0],
lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 183
Quoted 10 lines
lib/librte_vhost/virtio_net.c +9
-0+ &vq->shadow_used_ring[part_1], + part_2 * + sizeof(struct vring_used_elem)); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[0]), + part_2 * + sizeof(struct vring_used_elem)); + } }
Is expanding the code done for performance purpose? Or maybe we could have a loop to do that? Something like this (not compiled, not tested):
static inline void __attribute__((always_inline)) flush_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
{
uint32_t to = used_idx_start;
uint32_t from = 0;
uint32_t count;
if (used_idx_start + vq->shadow_used_idx < vq->size)
count = vq->shadow_used_idx;
else
count = vq->size - used_idx_start;
do {
rte_memcpy(&vq->used->ring[to],
&vq->shadow_used_ring[from],
count * sizeof(struct vring_used_elem));
vhost_log_used_vring(dev, vq,
offsetof(struct vring_used, ring[to]),
count * sizeof(struct vring_used_elem));
to = (to + count) & (vq->size - 1);
from += count;
count = vq->shadow_used_idx - count;
} while (count);
}Regards,
Maxime
Re: [PATCH v5 5/6] vhost: batch update used ring
Quoted 205 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, September 12, 2016 11:46 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v5 5/6] vhost: batch update used ringQuoted 6 lines · Zhihong Wang
This patch enables batch update of the used ring for better efficiency.
Signed-off-by Zhihong Wang <[email protected]>---
Changes in v4:Quoted 8 lines
1. Free shadow used ring in the right place.
2. Add failure check for shadow used ring malloc.
lib/librte_vhost/vhost.c | 20 ++++++++++++-- lib/librte_vhost/vhost.h | 4 +++ lib/librte_vhost/vhost_user.c | 31 +++++++++++++++++---- lib/librte_vhost/virtio_net.c | 64+++++++++++++++++++++++++++++++++++--------
lib/librte_vhost/vhost.c cleanup_device(struct virtio_net *dev, int
Quoted 7 lines
4 files changed, 101 insertions(+), 18 deletions(-)lib/librte_vhost/vhost.c +0
-0@@ -119,10 +119,26 @@ cleanup_device(struct virtio_net *dev, intdestroy)
lib/librte_vhost/vhost_user.c vhost_user_get_vring_base(struct virtio_net
Quoted 94 lines
lib/librte_vhost/vhost.c +18
-2static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq_0; +struct vhost_virtqueue *vq_1;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq_0 = dev->virtqueue[i *VIRTIO_QNUM ]; +if (vq_0->shadow_used_ring) { +rte_free (vq_0->shadow_used_ring); + vq_0->shadow_used_ring =NULL ; + } + + vq_1 = dev->virtqueue[i *VIRTIO_QNUM +1 ]; +if (vq_1->shadow_used_ring) { +rte_free (vq_1->shadow_used_ring); + vq_1->shadow_used_ring =NULL ; + } + +/* malloc together, free together */ +rte_free (vq_0); + }rte_free (dev); }lib/librte_vhost/vhost.h +4
-0@@ -85,6 +85,10 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +/* Shadow used ring for performance */ +struct vring_used_elem *shadow_used_ring; +uint32_t shadow_used_idx; } __rte_cache_aligned;/* Old kernels have no such macro defined */ lib/librte_vhost/vhost_user.c +23
-2@@ -193,7 +193,21 @@ static int vhost_user_set_vring_num(struct virtio_net *dev, struct vhost_vring_state *state) { - dev->virtqueue[state->index]->size = state->num; + struct vhost_virtqueue *vq; + + vq = dev->virtqueue[state->index]; + vq->size = state->num; +if (!vq->shadow_used_ring) { + vq->shadow_used_ring =rte_malloc (NULL , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE ); +if (!vq->shadow_used_ring) { +RTE_LOG (ERR ,VHOST_CONFIG , +"Failed to allocate memory" +" for shadow used ring.\n" ); +return -1 ; + } + }return 0 ; }@@ -611,14 +625,21 @@ static int vhost_user_get_vring_base(struct virtio_net *dev, struct vhost_vring_state *state) { + struct vhost_virtqueue *vq; +/* We have to stop the queue (virtio) if it is running. */ if (dev->flags &VIRTIO_DEV_RUNNING ) { dev->flags &= ~VIRTIO_DEV_RUNNING ; notify_ops->destroy_device (dev->vid); } + vq = dev->virtqueue[state->index];/* Here we are safe to get the last used index */ - state->num = dev->virtqueue[state->index]->last_used_idx; + state->num = vq->last_used_idx; +if (vq->shadow_used_ring) { +rte_free (vq->shadow_used_ring); + vq->shadow_used_ring =NULL ; + }RTE_LOG (INFO ,VHOST_CONFIG ,"vring base idx:%d file:%d\n" , state->index, state->num);@@ -627,10 +648,10 @@ vhost_user_get_vring_base(struct virtio_net*dev,
lib/librte_vhost/vhost_user.c vhost_user_get_vring_base(struct virtio_net *dev,
Quoted 9 lines
lib/librte_vhost/vhost_user.c +2
-3* sent and only sent in vhost_vring_stop. * TODO: cleanup the vring, it isn't usable since here. */ -if (dev->virtqueue[state->index]->kickfd >=0 ) -close (dev->virtqueue[state->index]->kickfd); +if (vq->kickfd >=0 ) +close (vq->kickfd); - dev->virtqueue[state->index]->kickfd =
VIRTIO_UNINITIALIZED_EVENTFD;lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf
Quoted 9 lines
lib/librte_vhost/vhost_user.c +1
-0+ vq->kickfd =VIRTIO_UNINITIALIZED_EVENTFD ;return 0 ; }lib/librte_vhost/virtio_net.c +0
-0@@ -134,17 +134,52 @@ virtio_enqueue_offload(struct rte_mbuf
*m_buf, structvirtio_net_hdr*net_hdr)lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 6 lines
lib/librte_vhost/virtio_net.c +1
-2}static inline void __attribute__((always_inline)) -update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint32_t desc_chain_head,uint32_t desc_chain_len) +update_used_ring(struct vhost_virtqueue *vq, uint32_t
desc_chain_head,lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 7 lines
lib/librte_vhost/virtio_net.c +2
-4+ uint32_t desc_chain_len) { -uint32_t used_idx = vq->last_used_idx & (vq->size -1 ); - - vq->used->ring[used_idx].id = desc_chain_head; - vq->used->ring[used_idx].len = desc_chain_len; + vq->shadow_used_ring[vq->shadow_used_idx].id =
desc_chain_head;lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf · line 141
Quoted 1 line
lib/librte_vhost/virtio_net.c +1
-0+ vq->shadow_used_ring[vq->shadow_used_idx].len =
desc_chain_len;Quoted 38 lines
lib/librte_vhost/virtio_net.c +34
-3+ vq->shadow_used_idx++; vq->last_used_idx++; -vhost_log_used_vring (dev, vq, offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); +} + +static inline void __attribute__((always_inline)) +flush_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t used_idx_start) +{ +if (used_idx_start + vq->shadow_used_idx < vq->size) { +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + vq->shadow_used_idx * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + vq->shadow_used_idx * + sizeof(struct vring_used_elem)); + }else { +uint32_t part_1 = vq->size - used_idx_start; +uint32_t part_2 = vq->shadow_used_idx - part_1; + +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + part_1 * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + part_1 * + sizeof(struct vring_used_elem)); +rte_memcpy (&vq->used->ring[0 ], + &vq->shadow_used_ring[part_1], + part_2 * +sizeof (struct vring_used_elem ));
Quoted 8 lines
lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 183
Quoted 7 lines
lib/librte_vhost/virtio_net.c +6
-0+vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[0]), + part_2 * + sizeof(struct vring_used_elem)); + } }Is expanding the code done for performance purpose?
Hi Maxime,
Yes theoretically this has the least branch number. And I think the logic is simpler this way.
Thanks
Zhihong
Quoted 31 lines
Or maybe we could have a loop to do that? Something like this (not compiled, not tested):
static inline void __attribute__((always_inline)) flush_used_ring(struct
virtio_net*dev, structvhost_virtqueue*vq,{ uint32_t to = used_idx_start; uint32_t from = 0; uint32_t count; if (used_idx_start + vq->shadow_used_idx < vq->size) count = vq->shadow_used_idx; else count = vq->size - used_idx_start; do { rte_memcpy(&vq->used->ring[to], &vq->shadow_used_ring[from], count * sizeof(struct vring_used_elem)); vhost_log_used_vring(dev, vq, offsetof(struct vring_used, ring[to]), count * sizeof(struct vring_used_elem)); to = (to + count) & (vq->size - 1); from += count; count = vq->shadow_used_idx - count; } while (count); }Regards,
Maxime
Re: [PATCH v5 5/6] vhost: batch update used ring
Quoted 214 lines · Zhihong
Quoted 205 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, September 12, 2016 11:46 PM
To: Wang, Zhihong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v5 5/6] vhost: batch update used ringQuoted 6 lines · Zhihong Wang
This patch enables batch update of the used ring for better efficiency.
Signed-off-by Zhihong Wang <[email protected]>---
Changes in v4:Quoted 8 lines
1. Free shadow used ring in the right place.
2. Add failure check for shadow used ring malloc.
lib/librte_vhost/vhost.c | 20 ++++++++++++-- lib/librte_vhost/vhost.h | 4 +++ lib/librte_vhost/vhost_user.c | 31 +++++++++++++++++---- lib/librte_vhost/virtio_net.c | 64+++++++++++++++++++++++++++++++++++--------
lib/librte_vhost/vhost.c cleanup_device(struct virtio_net *dev, int
Quoted 7 lines
4 files changed, 101 insertions(+), 18 deletions(-)lib/librte_vhost/vhost.c +0
-0@@ -119,10 +119,26 @@ cleanup_device(struct virtio_net *dev, intdestroy)
lib/librte_vhost/vhost_user.c vhost_user_get_vring_base(struct virtio_net
Quoted 94 lines
lib/librte_vhost/vhost.c +18
-2static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq_0; +struct vhost_virtqueue *vq_1;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq_0 = dev->virtqueue[i *VIRTIO_QNUM ]; +if (vq_0->shadow_used_ring) { +rte_free (vq_0->shadow_used_ring); + vq_0->shadow_used_ring =NULL ; + } + + vq_1 = dev->virtqueue[i *VIRTIO_QNUM +1 ]; +if (vq_1->shadow_used_ring) { +rte_free (vq_1->shadow_used_ring); + vq_1->shadow_used_ring =NULL ; + } + +/* malloc together, free together */ +rte_free (vq_0); + }rte_free (dev); }lib/librte_vhost/vhost.h +4
-0@@ -85,6 +85,10 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +/* Shadow used ring for performance */ +struct vring_used_elem *shadow_used_ring; +uint32_t shadow_used_idx; } __rte_cache_aligned;/* Old kernels have no such macro defined */ lib/librte_vhost/vhost_user.c +23
-2@@ -193,7 +193,21 @@ static int vhost_user_set_vring_num(struct virtio_net *dev, struct vhost_vring_state *state) { - dev->virtqueue[state->index]->size = state->num; + struct vhost_virtqueue *vq; + + vq = dev->virtqueue[state->index]; + vq->size = state->num; +if (!vq->shadow_used_ring) { + vq->shadow_used_ring =rte_malloc (NULL , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE ); +if (!vq->shadow_used_ring) { +RTE_LOG (ERR ,VHOST_CONFIG , +"Failed to allocate memory" +" for shadow used ring.\n" ); +return -1 ; + } + }return 0 ; }@@ -611,14 +625,21 @@ static int vhost_user_get_vring_base(struct virtio_net *dev, struct vhost_vring_state *state) { + struct vhost_virtqueue *vq; +/* We have to stop the queue (virtio) if it is running. */ if (dev->flags &VIRTIO_DEV_RUNNING ) { dev->flags &= ~VIRTIO_DEV_RUNNING ; notify_ops->destroy_device (dev->vid); } + vq = dev->virtqueue[state->index];/* Here we are safe to get the last used index */ - state->num = dev->virtqueue[state->index]->last_used_idx; + state->num = vq->last_used_idx; +if (vq->shadow_used_ring) { +rte_free (vq->shadow_used_ring); + vq->shadow_used_ring =NULL ; + }RTE_LOG (INFO ,VHOST_CONFIG ,"vring base idx:%d file:%d\n" , state->index, state->num);@@ -627,10 +648,10 @@ vhost_user_get_vring_base(struct virtio_net*dev,
lib/librte_vhost/vhost_user.c vhost_user_get_vring_base(struct virtio_net *dev,
Quoted 9 lines
lib/librte_vhost/vhost_user.c +2
-3* sent and only sent in vhost_vring_stop. * TODO: cleanup the vring, it isn't usable since here. */ -if (dev->virtqueue[state->index]->kickfd >=0 ) -close (dev->virtqueue[state->index]->kickfd); +if (vq->kickfd >=0 ) +close (vq->kickfd); - dev->virtqueue[state->index]->kickfd =
VIRTIO_UNINITIALIZED_EVENTFD;lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf
Quoted 9 lines
lib/librte_vhost/vhost_user.c +1
-0+ vq->kickfd =VIRTIO_UNINITIALIZED_EVENTFD ;return 0 ; }lib/librte_vhost/virtio_net.c +0
-0@@ -134,17 +134,52 @@ virtio_enqueue_offload(struct rte_mbuf
*m_buf, structvirtio_net_hdr*net_hdr)lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 6 lines
lib/librte_vhost/virtio_net.c +1
-2}static inline void __attribute__((always_inline)) -update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint32_t desc_chain_head,uint32_t desc_chain_len) +update_used_ring(struct vhost_virtqueue *vq, uint32_t
desc_chain_head,lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
Quoted 7 lines
lib/librte_vhost/virtio_net.c +2
-4+ uint32_t desc_chain_len) { -uint32_t used_idx = vq->last_used_idx & (vq->size -1 ); - - vq->used->ring[used_idx].id = desc_chain_head; - vq->used->ring[used_idx].len = desc_chain_len; + vq->shadow_used_ring[vq->shadow_used_idx].id =
desc_chain_head;lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf · line 141
Quoted 1 line
lib/librte_vhost/virtio_net.c +1
-0+ vq->shadow_used_ring[vq->shadow_used_idx].len =
desc_chain_len;Quoted 38 lines
lib/librte_vhost/virtio_net.c +34
-3+ vq->shadow_used_idx++; vq->last_used_idx++; -vhost_log_used_vring (dev, vq, offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); +} + +static inline void __attribute__((always_inline)) +flush_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t used_idx_start) +{ +if (used_idx_start + vq->shadow_used_idx < vq->size) { +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + vq->shadow_used_idx * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + vq->shadow_used_idx * + sizeof(struct vring_used_elem)); + }else { +uint32_t part_1 = vq->size - used_idx_start; +uint32_t part_2 = vq->shadow_used_idx - part_1; + +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + part_1 * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + part_1 * + sizeof(struct vring_used_elem)); +rte_memcpy (&vq->used->ring[0 ], + &vq->shadow_used_ring[part_1], + part_2 * +sizeof (struct vring_used_elem ));Quoted 8 lines
lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 183
Quoted 7 lines
lib/librte_vhost/virtio_net.c +6
-0+vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[0]), + part_2 * + sizeof(struct vring_used_elem)); + } }Is expanding the code done for performance purpose?
Quoted 4 lines
Hi Maxime,
Yes theoretically this has the least branch number. And I think the logic is simpler this way.
Ok, in that case, maybe you could create a function to do the rte_memcpy and the vhost_log_used on a given range.
I don't have a strong opinion on this, if Yuanhan is fine with current code, that's ok for me.
Thanks,
Maxime
Quoted 35 lines
Thanks
ZhihongQuoted 31 lines
Or maybe we could have a loop to do that? Something like this (not compiled, not tested):
static inline void __attribute__((always_inline)) flush_used_ring(struct
virtio_net*dev, structvhost_virtqueue*vq,{ uint32_t to = used_idx_start; uint32_t from = 0; uint32_t count; if (used_idx_start + vq->shadow_used_idx < vq->size) count = vq->shadow_used_idx; else count = vq->size - used_idx_start; do { rte_memcpy(&vq->used->ring[to], &vq->shadow_used_ring[from], count * sizeof(struct vring_used_elem)); vhost_log_used_vring(dev, vq, offsetof(struct vring_used, ring[to]), count * sizeof(struct vring_used_elem)); to = (to + count) & (vq->size - 1); from += count; count = vq->shadow_used_idx - count; } while (count); }Regards,
Maxime
Re: [PATCH v5 5/6] vhost: batch update used ring
Quoted 40 lines · Maxime Coquelin
Quoted 40 lines
Quoted 39 lines
Quoted 37 lines
lib/librte_vhost/virtio_net.c +37
-0+static inline void __attribute__((always_inline)) +flush_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t used_idx_start) +{ +if (used_idx_start + vq->shadow_used_idx < vq->size) { +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + vq->shadow_used_idx * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + vq->shadow_used_idx * + sizeof(struct vring_used_elem)); + }else { +uint32_t part_1 = vq->size - used_idx_start; +uint32_t part_2 = vq->shadow_used_idx - part_1; + +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + part_1 * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + part_1 * + sizeof(struct vring_used_elem)); +rte_memcpy (&vq->used->ring[0 ], + &vq->shadow_used_ring[part_1], + part_2 * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[0]), + part_2 * + sizeof(struct vring_used_elem)); + }Quoted 1 line
}
Is expanding the code done for performance purpose?
Quoted 6 lines
Quoted 4 lines
Hi Maxime,
Yes theoretically this has the least branch number. And I think the logic is simpler this way.
Ok, in that case, maybe you could create a function to do the
rte_memcpyand thevhost_log_usedon a given range.
Agreed, that will be better; it could avoid repeating similar code block 3 times.
Quoted 4 lines
I don't have a strong opinion on this, if Yuanhan is fine with current code, that's ok for me.
From what I know, that's kind of DPDK prefered way, to expand code
when necessary. For example, 9ec201f5d6e7 ("mbuf: provide bulk allocation").
So I'm fine with it.
--yliu
Re: [PATCH v5 5/6] vhost: batch update used ring
Quoted 53 lines
-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: Sunday, September 18, 2016 10:56 AM
To: Maxime Coquelin <[email protected]>
Cc: Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v5 5/6] vhost: batch update used ringQuoted 42 lines · Maxime Coquelin
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines
lib/librte_vhost/virtio_net.c +31
-0+static inline void __attribute__((always_inline)) +flush_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t used_idx_start) +{ +if (used_idx_start + vq->shadow_used_idx < vq->size) { +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + vq->shadow_used_idx * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + vq->shadow_used_idx * + sizeof(struct vring_used_elem)); + }else { +uint32_t part_1 = vq->size - used_idx_start; +uint32_t part_2 = vq->shadow_used_idx - part_1; + +rte_memcpy (&vq->used->ring[used_idx_start], + &vq->shadow_used_ring[0 ], + part_1 * +sizeof (struct vring_used_elem )); +vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[used_idx_start]), + part_1 * + sizeof(struct vring_used_elem)); +rte_memcpy (&vq->used->ring[0 ], + &vq->shadow_used_ring[part_1], + part_2 * +sizeof (struct vring_used_elem ));Quoted 8 lines
Quoted 7 lines
lib/librte_vhost/virtio_net.c +6
-0+vhost_log_used_vring (dev, vq, + offsetof(struct vring_used , + ring[0]), + part_2 * + sizeof(struct vring_used_elem)); + }}
Is expanding the code done for performance purpose?
Hi Maxime,
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Yes theoretically this has the least branch number. And I think the logic is simpler this way.
Ok, in that case, maybe you could create a function to do the
rte_memcpyand thevhost_log_usedon a given range.Agreed, that will be better; it could avoid repeating similar code block 3 times.
Okay. Thanks for the suggestion, Maxime and Yuanhan.
Quoted 11 lines
Quoted 3 lines
I don't have a strong opinion on this, if Yuanhan is fine with current code, that's ok for me.
From what I know, that's kind of DPDK prefered way, to expand code when necessary. For example,
9ec201f5d6e7("mbuf: provide bulk allocation").So I'm fine with it.
--yliu
PATCH v5 6/6 vhost: optimize cache access
In reply to Zhihong WangThis patch reorders the code to delay virtio header write to optimize cache access efficiency for cases where the mrg_rxbuf feature is turned on. It reduces CPU pipeline stall cycles significantly.
---
Changes in v3:
1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
lib/librte_vhost/virtio_net.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
lib/librte_vhost/virtio_net.c +14 -6
@@ -197,6 +197,7 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t mbuf_len;uint32_t mbuf_avail;uint32_t cpy_len; +uint32_t copy_virtio_hdr;uint32_t num_buffers =0 ;/* start with the first mbuf of the packet */
@@ -211,12 +212,12 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,if (unlikely (!desc_addr)) goto error; -/* handle virtio header */ +/* +* handle virtio header, the actual write operation is delayed +* for cache optimization, to reduce CPU pipeline stall cycles. +*/ virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; -virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); -if (is_mrg_rxbuf) - virtio_hdr->num_buffers =1 ; - + copy_virtio_hdr =1 ;vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); desc_offset = dev->vhost_hlen;
@@ -266,8 +267,15 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq, goto error; } -/* copy mbuf data */ +/* copy virtio header and mbuf data */ cpy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +if (copy_virtio_hdr) { + copy_virtio_hdr =0 ; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers = num_buffers +1 ; + } + rte_memcpy((void *)(uintptr_t)desc_addr, rte_pktmbuf_mtod_offset(mbuf, void *, mbuf_len - mbuf_avail),
Signature · 1 line
2.7.4
Re: [PATCH v5 0/6] vhost: optimize enqueue
In reply to Zhihong WangHi,
Quoted 50 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed for high performance and good maintainability, and improves CPU efficiency significantly by optimizing cache access, which means:
* Higher maximum throughput can be achieved for fast frontends like DPDK virtio pmd.
* Better scalability can be achieved that each vhost core can support more connections because it takes less cycles to handle each single frontend.
This patch set contains:
1. A Windows VM compatibility fix for vhost enqueue in 16.07 release.
2. A baseline patch to rewrite the vhost logic.
3. A series of optimization patches added upon the baseline.
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Code reordering and batch used ring update bring most of the performance improvements.
In the existing code there're 2 callbacks for vhost enqueue:
*
virtio_dev_merge_rxformrg_rxbufturned on cases.*
virtio_dev_rxformrg_rxbufturned off cases.The performance of the existing code is not optimal, especially when the
mrg_rxbuffeature turned on. Besides, having 2 callback paths increases maintenance efforts.Also, there's a compatibility issue in the existing code which causes Windows VM to hang when the
mrg_rxbuffeature turned on.---
Changes in v5:
Quoted 7 lines
1. Rebase to the latest branch.
2. Rename variables to keep consistent in naming style.
3. Small changes like return value adjustment and vertical alignment.
4. Add details in commit log.
Just tried to apply your series without success. Apparently, it is not based directly on master branch, as it lacks some SHA-1 information.
Could you rebase it against master please?
Thanks,
Maxime
Re: [PATCH v5 0/6] vhost: optimize enqueue
Quoted 60 lines · Maxime Coquelin
Hi,
Quoted 57 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed
for high performance and good maintainability, and improves CPU efficiency
significantly by optimizing cache access, which means:* Higher maximum throughput can be achieved for fast frontends like
DPDK
virtio pmd.* Better scalability can be achieved that each vhost core can support more connections because it takes less cycles to handle each single frontend.
This patch set contains:
1. A Windows VM compatibility fix for vhost enqueue in 16.07 release.
2. A baseline patch to rewrite the vhost logic.
3. A series of optimization patches added upon the baseline.
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Code reordering and batch used ring update bring most of the performance improvements.
In the existing code there're 2 callbacks for vhost enqueue:
*
virtio_dev_merge_rxformrg_rxbufturned on cases.*
virtio_dev_rxformrg_rxbufturned off cases.The performance of the existing code is not optimal, especially when the
mrg_rxbuffeature turned on. Besides, having 2 callback paths increases maintenance efforts.Also, there's a compatibility issue in the existing code which causes Windows VM to hang when the
mrg_rxbuffeature turned on.---
Changes in v5:1. Rebase to the latest branch.
2. Rename variables to keep consistent in naming style.
Quoted 8 lines
Quoted 3 lines
3. Small changes like return value adjustment and vertical alignment.
4. Add details in commit log.
Just tried to apply your series without success. Apparently, it is not based directly on master branch, as it lacks some SHA-1 information.
Could you rebase it against master please?
Ok, it is in fact based on top of:git://dpdk.org/next/dpdk-next-virtio master
For v6, if any, could you add this info to the cover letter please?
Thanks,
Maxime
Re: [PATCH v5 0/6] vhost: optimize enqueue
In reply to Maxime CoquelinQuoted 5 lines · Maxime Coquelin
Just tried to apply your series without success. Apparently, it is not based directly on master branch, as it lacks some SHA-1 information.
Could you rebase it against master please?
It's rebased against the dpdk-next-virtio tree [0], where all the virtio/vhost patches are applied first.
[0]: http://dpdk.org/browse/next/dpdk-next-virtio/
--yliu
v6 cover vhost: optimize enqueue
In reply to Zhihong WangThis patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed for high performance and good maintainability, and improves CPU efficiency significantly by optimizing cache access, which means:
* Higher maximum throughput can be achieved for fast frontends like DPDK virtio pmd.
* Better scalability can be achieved that each vhost core can support more connections because it takes less cycles to handle each single frontend.
This patch set contains:
1. A Windows VM compatibility fix for vhost enqueue in 16.07 release.
2. A baseline patch to rewrite the vhost logic.
3. A series of optimization patches added upon the baseline.
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Code reordering and batch used ring update bring most of the performance improvements.
In the existing code there're 2 callbacks for vhost enqueue:
* virtio_dev_merge_rx for mrg_rxbuf turned on cases.
* virtio_dev_rx for mrg_rxbuf turned off cases.
The performance of the existing code is not optimal, especially when the mrg_rxbuf feature turned on. Besides, having 2 callback paths increases maintenance efforts.
Also, there's a compatibility issue in the existing code which causes Windows VM to hang when the mrg_rxbuf feature turned on.
---
Changes in v6:
1. Merge duplicated code.
2. Introduce a function for used ring write.
3. Add necessary comments.
---
Changes in v5:
1. Rebase to dpdk-next-virtio master.
2. Rename variables to keep consistent in naming style.
3. Small changes like return value adjustment and vertical alignment.
4. Add details in commit log.
---
Changes in v4:
1. Fix a Windows VM compatibility issue.
2. Free shadow used ring in the right place.
3. Add failure check for shadow used ring malloc.
4. Refactor the code for clearer logic.
5. Add PRINT_PACKET for debugging.
---
Changes in v3:
1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
3. Rewrite enqueue and delete the obsolete in the same patch.
---
Changes in v2:
1. Split the big function into several small ones.
2. Use multiple patches to explain each optimization.
3. Add comments.
Zhihong Wang (6):
vhost: fix windows vm hang
vhost: rewrite enqueue
vhost: remove useless volatile
vhost: add desc prefetch
vhost: batch update used ring
vhost: optimize cache access
lib/librte_vhost/vhost.c | 20 +- lib/librte_vhost/vhost.h | 6 +- lib/librte_vhost/vhost_user.c | 31 ++- lib/librte_vhost/virtio_net.c | 541 ++++++++++++++---------------------------- 4 files changed, 225 insertions(+), 373 deletions(-)
Signature · 1 line
2.7.4
PATCH v6 1/6 vhost: fix windows vm hang
This patch fixes a Windows VM compatibility issue in DPDK 16.07 vhost code which causes the guest to hang once any packets are enqueued when mrg_rxbuf is turned on by setting the right id and len in the used ring.
As defined in virtio spec 0.95 and 1.0, in each used ring element, id means index of start of used descriptor chain, and len means total length of the descriptor chain which was written to. While in 16.07 code, index of the last descriptor is assigned to id, and the length of the last descriptor is assigned to len.
How to test?
1. Start testpmd in the host with a vhost port.
2. Start a Windows VM image with qemu and connect to the vhost port.
3. Start io forwarding with tx_first in host testpmd.
For 16.07 code, the Windows VM will hang once any packets are enqueued.
---
Changes in v5:
1. Add details in commit log.
lib/librte_vhost/virtio_net.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
lib/librte_vhost/virtio_net.c +12 -5
@@ -384,6 +384,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,uint16_t start_idx = vq->last_used_idx;uint16_t cur_idx = start_idx;uint64_t desc_addr; +uint32_t desc_chain_head; +uint32_t desc_chain_len;uint32_t mbuf_offset, mbuf_avail;uint32_t desc_offset, desc_avail;uint32_t cpy_len;
@@ -412,6 +414,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; desc_offset = dev->vhost_hlen; + desc_chain_head = buf_vec[vec_idx].desc_idx; + desc_chain_len = desc_offset; mbuf_avail =rte_pktmbuf_data_len (m); mbuf_offset =0 ;
@@ -419,19 +423,21 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,/* done with current desc buf, get the next one */ if (desc_avail ==0 ) { desc_idx = buf_vec[vec_idx].desc_idx; + vec_idx++;if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) {/* 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; + vq->used->ring[used_idx].id = desc_chain_head; + vq->used->ring[used_idx].len = desc_chain_len;vhost_log_used_vring (dev, vq, offsetof(struct vring_used , ring[used_idx]), sizeof(vq->used->ring[used_idx])); + desc_chain_head = buf_vec[vec_idx].desc_idx; + desc_chain_len =0 ; } - vec_idx++; desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr);if (unlikely (!desc_addr))return 0 ;
@@ -463,11 +469,12 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, mbuf_offset += cpy_len; desc_avail -= cpy_len; desc_offset += cpy_len; + desc_chain_len += cpy_len; } used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = buf_vec[vec_idx].desc_idx; - vq->used->ring[used_idx].len = desc_offset; + vq->used->ring[used_idx].id = desc_chain_head; + vq->used->ring[used_idx].len = desc_chain_len;vhost_log_used_vring (dev, vq, offsetof(struct vring_used, ring[used_idx]), sizeof(vq->used->ring[used_idx]));
Signature · 1 line
2.7.4
Yuanhan Liu
AppliedRe: [dpdk-stable] [PATCH v6 1/6] vhost: fix windows vm hang
Quoted 14 lines · Zhihong Wang
This patch fixes a Windows VM compatibility issue in DPDK 16.07 vhost code which causes the guest to hang once any packets are enqueued when
mrg_rxbufis turned on by setting the right id and len in the used ring.As defined in virtio spec 0.95 and 1.0, in each used ring element, id means index of start of used descriptor chain, and len means total length of the descriptor chain which was written to. While in 16.07 code, index of the last descriptor is assigned to id, and the length of the last descriptor is assigned to len.
How to test?
1. Start testpmd in the host with a vhost port.
Quoted 8 lines
2. Start a Windows VM image with qemu and connect to the vhost port.
3. Start io forwarding with
tx_firstin host testpmd.For 16.07 code, the Windows VM will hang once any packets are enqueued.
Cc <[email protected]>Signed-off-by Zhihong Wang <[email protected]>
Applied to dpdk-next-virtio (this patch only).
Thanks.
--yliu
PATCH v6 2/6 vhost: rewrite enqueue
In reply to Zhihong WangThis patch implements the vhost logic from scratch into a single function to improve maintainability. This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
In the existing code there're 2 callbacks for vhost enqueue:
* virtio_dev_merge_rx for mrg_rxbuf turned on cases.
* virtio_dev_rx for mrg_rxbuf turned off cases.
Having 2 callback paths increases maintenance effort. Also, the performance of the existing code is not optimal, especially when the mrg_rxbuf feature turned on.
---
Changes in v6:
1. Merge duplicated code.
2. Add necessary comments.
---
Changes in v5:
1. Rebase to dpdk-next-virtio master.
2. Rename variables to keep consistent in naming style.
3. Small changes like return value adjustment and vertical alignment.
---
Changes in v4:
1. Refactor the code for clearer logic.
2. Add PRINT_PACKET for debugging.
---
Changes in v3:
1. Rewrite enqueue and delete the obsolete in the same patch.
lib/librte_vhost/virtio_net.c | 508 +++++++++++------------------------------- 1 file changed, 134 insertions(+), 374 deletions(-)
lib/librte_vhost/virtio_net.c +134 -374
@@ -91,7 +91,7 @@ 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 void +static inline void __attribute__((always_inline))virtio_enqueue_offload (struct rte_mbuf *m_buf,struct virtio_net_hdr *net_hdr) {if (m_buf->ol_flags &PKT_TX_L4_MASK ) {
@@ -112,6 +112,10 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) cksum));break ; } + }else { + net_hdr->flags =0 ; + net_hdr->csum_start =0 ; + net_hdr->csum_offset =0 ; }if (m_buf->ol_flags &PKT_TX_TCP_SEG ) {
@@ -122,439 +126,195 @@ virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) net_hdr->gso_size = m_buf->tso_segsz; net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len + m_buf->l4_len; + }else { + net_hdr->gso_type =0 ; + net_hdr->hdr_len =0 ; + net_hdr->gso_size =0 ; } } -static inline void -copy_virtio_net_hdr (struct virtio_net *dev,uint64_t desc_addr, -struct virtio_net_hdr_mrg_rxbuf hdr) +static inline void __attribute__((always_inline)) +update_used_ring (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint32_t desc_chain_head,uint32_t desc_chain_len) { -if (dev->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; +uint32_t used_idx = vq->last_used_idx & (vq->size -1 ); + + vq->used->ring[used_idx].id = desc_chain_head; + vq->used->ring[used_idx].len = desc_chain_len; + vq->last_used_idx++; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , + ring[used_idx]), + sizeof(vq->used->ring[used_idx])); }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) +enqueue_packet (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t avail_idx,struct rte_mbuf *mbuf, +uint32_t is_mrg_rxbuf) { -uint32_t desc_avail, desc_offset; -uint32_t mbuf_avail, mbuf_offset; -uint32_t cpy_len; +struct virtio_net_hdr_mrg_rxbuf *virtio_hdr;struct vring_desc *desc;uint64_t desc_addr; -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; +uint32_t desc_chain_head; +uint32_t desc_chain_len; +uint32_t desc_current; +uint32_t desc_offset; +uint32_t mbuf_len; +uint32_t mbuf_avail; +uint32_t cpy_len; +uint32_t num_buffers =0 ; - desc = &vq->desc[desc_idx]; +/* start with the first mbuf of the packet */ + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; + +/* get the current desc */ + desc_current = vq->avail->ring[(vq->last_used_idx) & (vq->size -1 )]; + desc_chain_head = desc_current; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr); -/* -* Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid -* performance issue with some versions of gcc (4.8.4 and 5.3.0) which -* otherwise stores offset on the stack instead of in a register. -*/ -if (unlikely (desc->len < dev->vhost_hlen) || !desc_addr) -return -1 ; +if (unlikely (!desc_addr)) + goto error; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); +/* handle virtio header */ + virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers =1 ; -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr);vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - desc_offset = dev->vhost_hlen; - desc_avail = desc->len - dev->vhost_hlen; - - 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); + desc_chain_len = desc_offset; + desc_addr += desc_offset; + +/* start copy from mbuf to desc */ +while (mbuf_avail || mbuf->next) { +/* get the next mbuf if the current done */ +if (!mbuf_avail) { + mbuf = mbuf->next; + mbuf_len =rte_pktmbuf_data_len (mbuf); + mbuf_avail = mbuf_len; } -/* 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 ; - } -if (unlikely (desc->next >= vq->size)) -return -1 ; +/* get the next desc if the current done */ +if (desc->len <= desc_offset) { +if (desc->flags &VRING_DESC_F_NEXT ) { +/* go on with the current desc chain */ + desc_current = desc->next; + }else if (is_mrg_rxbuf) { +/* start with the next desc chain */ +update_used_ring (dev, vq, desc_chain_head, + desc_chain_len); + num_buffers++; + virtio_hdr->num_buffers++; +if (avail_idx == vq->last_used_idx) + goto error; + + desc_current = + vq->avail->ring[(vq->last_used_idx) & + (vq->size -1 )]; + desc_chain_head = desc_current; + desc_chain_len =0 ; + }else + goto error; - desc = &vq->desc[desc->next]; + desc_offset =0 ; + desc = &vq->desc[desc_current]; desc_addr =gpa_to_vva (dev, desc->addr);if (unlikely (!desc_addr)) -return -1 ; - - desc_offset =0 ; - desc_avail = desc->len; + goto error; } - 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); +/* copy mbuf data */ + cpy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +rte_memcpy ((void *)(uintptr_t )desc_addr, + rte_pktmbuf_mtod_offset(mbuf, void *, + mbuf_len - mbuf_avail), + 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 ; -} - -/** -* 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 -* count is returned to indicate the number of packets that are succesfully -* added to the RX queue. This function works when the mbuf is scattered, but -* it doesn't support the mergeable feature. -*/ -static inline uint32_t __attribute__((always_inline)) -virtio_dev_rx (struct virtio_net *dev,uint16_t queue_id, -struct rte_mbuf **pkts,uint32_t count) -{ -struct vhost_virtqueue *vq; -uint16_t avail_idx, free_entries, start_idx; -uint16_t desc_indexes[MAX_PKT_BURST ]; -uint16_t used_idx; -uint32_t i; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); -return 0 ; +PRINT_PACKET (dev, (uintptr_t )desc_addr, cpy_len,0 ); + mbuf_avail -= cpy_len; + desc_addr += cpy_len; + desc_offset += cpy_len; + desc_chain_len += cpy_len; } - vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) -return 0 ; +update_used_ring (dev, vq, desc_chain_head, desc_chain_len); - avail_idx = *((volatile uint16_t *)&vq->avail->idx); - start_idx = vq->last_used_idx; - free_entries = avail_idx - start_idx; - count =RTE_MIN (count, free_entries); - count =RTE_MIN (count, (uint32_t )MAX_PKT_BURST ); -if (count ==0 ) -return 0 ; - -LOG_DEBUG (VHOST_DATA ,"(%d) start_idx %d | end_idx %d\n" , - dev->vid, start_idx, start_idx + count); - -/* Retrieve all of the desc indexes first to avoid caching issues. */ -rte_prefetch0 (&vq->avail->ring[start_idx & (vq->size -1 )]); -for (i =0 ; i < count; i++) { - used_idx = (start_idx + i) & (vq->size -1 ); - desc_indexes[i] = vq->avail->ring[used_idx]; - vq->used->ring[used_idx].id = desc_indexes[i]; - vq->used->ring[used_idx].len = pkts[i]->pkt_len + - dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } - -rte_prefetch0 (&vq->desc[desc_indexes[0 ]]); -for (i =0 ; i < count; i++) { -uint16_t desc_idx = desc_indexes[i]; -int err; +return 0 ; - err =copy_mbuf_to_desc (dev, vq, pkts[i], desc_idx); -if (unlikely (err)) { - used_idx = (start_idx + i) & (vq->size -1 ); - vq->used->ring[used_idx].len = dev->vhost_hlen; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - } +error: +/* rollback on any error if last_used_idx update on-the-fly */ + vq->last_used_idx -= num_buffers; -if (i +1 < count) -rte_prefetch0 (&vq->desc[desc_indexes[i+1 ]]); - } +return -1 ; +} +static inline void __attribute__((always_inline)) +notify_guest (struct virtio_net *dev,struct vhost_virtqueue *vq) +{ +/* flush changes before updating used->idx */ rte_smp_wmb (); - - *(volatile uint16_t *)&vq->used->idx += count; - vq->last_used_idx += count; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , idx), -sizeof (vq->used->idx)); - -/* flush used->idx update before we read avail->flags. */ + *(volatile uint16_t *)&vq->used->idx = vq->last_used_idx; +vhost_log_used_vring (dev, vq, offsetof(struct vring_used , idx), +sizeof (vq->used->idx)); +/* flush used->idx update before reading avail->flags */ rte_mb (); - -/* Kick the guest if necessary. */ +/* kick the guest if necessary */ if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) && (vq->callfd >=0 ))eventfd_write (vq->callfd, (eventfd_t )1 ); -return count; -} - -static inline int -fill_vec_buf (struct vhost_virtqueue *vq,uint32_t avail_idx, -uint32_t *allocated,uint32_t *vec_idx, -struct buf_vector *buf_vec) -{ -uint16_t idx = vq->avail->ring[avail_idx & (vq->size -1 )]; -uint32_t vec_id = *vec_idx; -uint32_t len = *allocated; - -while (1 ) { -if (unlikely (vec_id >=BUF_VECTOR_MAX || idx >= vq->size)) -return -1 ; - - len += vq->desc[idx].len; - buf_vec[vec_id].buf_addr = vq->desc[idx].addr; - buf_vec[vec_id].buf_len = vq->desc[idx].len; - 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 ; } -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, -uint16_t *end,struct buf_vector *buf_vec) +uint16_t +rte_vhost_enqueue_burst (int vid,uint16_t queue_id, +struct rte_mbuf **pkts,uint16_t count) { -uint16_t cur_idx; +struct vhost_virtqueue *vq; +struct virtio_net *dev; +uint32_t is_mrg_rxbuf =0 ; +uint32_t pkt_idx =0 ; +uint32_t pkt_left = count;uint16_t avail_idx; -uint32_t allocated =0 ; -uint32_t vec_idx =0 ; -uint16_t tries =0 ; - - cur_idx = vq->last_used_idx; - -while (1 ) { - avail_idx = *((volatile uint16_t *)&vq->avail->idx); -if (unlikely (cur_idx == avail_idx)) -return -1 ; - -if (unlikely (fill_vec_buf (vq, cur_idx, &allocated, - &vec_idx, buf_vec) <0 )) -return -1 ; - - 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 (unlikely (tries >= vq->size)) -return -1 ; - } - *end = cur_idx; -return 0 ; -} - -static inline uint32_t __attribute__((always_inline)) -copy_mbuf_to_desc_mergeable (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint16_t end_idx,struct rte_mbuf *m, -struct buf_vector *buf_vec) -{ -struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0 ,0 ,0 ,0 ,0 ,0 },0 }; -uint32_t vec_idx =0 ; -uint16_t start_idx = vq->last_used_idx; -uint16_t cur_idx = start_idx; -uint64_t desc_addr; -uint32_t desc_chain_head; -uint32_t desc_chain_len; -uint32_t mbuf_offset, mbuf_avail; -uint32_t desc_offset, desc_avail; -uint32_t cpy_len; -uint16_t desc_idx, used_idx; - -if (unlikely (m ==NULL )) +if (unlikely (!pkt_left))return 0 ; -LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, cur_idx, end_idx); + pkt_left =RTE_MIN ((uint32_t )MAX_PKT_BURST , pkt_left); - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) + dev =get_device (vid); +if (unlikely (!dev))return 0 ; -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - - virtio_hdr.num_buffers = end_idx - start_idx; -LOG_DEBUG (VHOST_DATA ,"(%d) RX: num merge buffers %d\n" , - dev->vid, virtio_hdr.num_buffers); - -virtio_enqueue_offload (m, &virtio_hdr.hdr); -copy_virtio_net_hdr (dev, desc_addr, virtio_hdr); -vhost_log_write (dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen); -PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); - - desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen; - desc_offset = dev->vhost_hlen; - desc_chain_head = buf_vec[vec_idx].desc_idx; - desc_chain_len = desc_offset; - - 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 = buf_vec[vec_idx].desc_idx; - vec_idx++; - -if (!(vq->desc[desc_idx].flags &VRING_DESC_F_NEXT )) { -/* Update used ring with desc information */ - used_idx = cur_idx++ & (vq->size -1 ); - vq->used->ring[used_idx].id = desc_chain_head; - vq->used->ring[used_idx].len = desc_chain_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used , - ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - desc_chain_head = buf_vec[vec_idx].desc_idx; - desc_chain_len =0 ; - } - - desc_addr =gpa_to_vva (dev, buf_vec[vec_idx].buf_addr); -if (unlikely (!desc_addr)) -return 0 ; - -/* Prefetch buffer address. */ -rte_prefetch0 ((void *)(uintptr_t )desc_addr); - desc_offset =0 ; - desc_avail = buf_vec[vec_idx].buf_len; - } - -/* done with current mbuf, get the next one */ -if (mbuf_avail ==0 ) { - m = m->next; - - mbuf_offset =0 ; - mbuf_avail =rte_pktmbuf_data_len (m); - } - - 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, buf_vec[vec_idx].buf_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; - desc_chain_len += cpy_len; - } - - used_idx = cur_idx & (vq->size -1 ); - vq->used->ring[used_idx].id = desc_chain_head; - vq->used->ring[used_idx].len = desc_chain_len; -vhost_log_used_vring (dev, vq, - offsetof(struct vring_used, ring[used_idx]), - sizeof(vq->used->ring[used_idx])); - -return end_idx - start_idx; -} - -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 , nr_used =0 ; -uint16_t end; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; - -LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); -if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb))) { -RTE_LOG (ERR ,VHOST_DATA ,"(%d) %s: invalid virtqueue idx %d.\n" , - dev->vid, __func__, queue_id); +if (unlikely (!is_valid_virt_queue_idx (queue_id,0 , dev->virt_qp_nb)))return 0 ; - } vq = dev->virtqueue[queue_id]; -if (unlikely (vq->enabled ==0 )) +if (unlikely (!vq->enabled))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 + dev->vhost_hlen; +if (dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF )) + is_mrg_rxbuf =1 ; -if (unlikely (reserve_avail_buf_mergeable (vq, pkt_len, - &end, buf_vec) <0 )) { -LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); +/* start enqueuing packets 1 by 1 */ + avail_idx = *((volatile uint16_t *)&vq->avail->idx); +while (pkt_left && avail_idx != vq->last_used_idx) { +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], + is_mrg_rxbuf))break ; - } - - nr_used =copy_mbuf_to_desc_mergeable (dev, vq, end, - pkts[pkt_idx], buf_vec); -rte_smp_wmb (); - *(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 += nr_used; + pkt_idx++; + pkt_left--; } -if (likely (pkt_idx)) { -/* flush used->idx update before we read avail->flags. */ -rte_mb (); - -/* Kick the guest if necessary. */ -if (!(vq->avail->flags &VRING_AVAIL_F_NO_INTERRUPT ) - && (vq->callfd >=0 )) -eventfd_write (vq->callfd, (eventfd_t )1 ); - } +/* update used idx and kick the guest if necessary */ +if (pkt_idx) +notify_guest (dev, vq);return pkt_idx; } -uint16_t -rte_vhost_enqueue_burst (int vid,uint16_t queue_id, -struct rte_mbuf **pkts,uint16_t count) -{ -struct virtio_net *dev =get_device (vid); - -if (!dev) -return 0 ; - -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); -} -static void parse_ethernet (struct rte_mbuf *m,uint16_t *l4_proto,void **l4_hdr) {
Signature · 1 line
2.7.4
Re: [PATCH v6 2/6] vhost: rewrite enqueue
Quoted 17 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function to improve maintainability. This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
In the existing code there're 2 callbacks for vhost enqueue:
*
virtio_dev_merge_rxformrg_rxbufturned on cases.*
virtio_dev_rxformrg_rxbufturned off cases.Having 2 callback paths increases maintenance effort. Also, the performance of the existing code is not optimal, especially when the
mrg_rxbuffeature turned on.Signed-off-by Zhihong Wang <[email protected]>---
Changes in v6:
.....
Quoted 6 lines
lib/librte_vhost/virtio_net.c +0
-6-/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, -uint16_t *end,struct buf_vector *buf_vec)
lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 278
Quoted 10 lines
lib/librte_vhost/virtio_net.c +8
-1+uint16_t +rte_vhost_enqueue_burst (int vid,uint16_t queue_id, +struct rte_mbuf **pkts,uint16_t count) { -uint16_t cur_idx; +struct vhost_virtqueue *vq; +struct virtio_net *dev; +uint32_t is_mrg_rxbuf =0 ; +uint32_t pkt_idx =0 ; +uint32_t pkt_left = count;
Is pkt_left really needed?
Quoted 4 lines
uint16_t avail_idx;lib/librte_vhost/virtio_net.c +0
-3-uint32_t allocated =0 ; -uint32_t vec_idx =0 ; -uint16_t tries =0 ;
....
Re: [PATCH v6 2/6] vhost: rewrite enqueue
Quoted 43 lines
-----Original Message-----
From: Jianbo Liu [mailto:[email protected]]
Sent: Thursday, September 22, 2016 5:58 PM
To: Wang, Zhihong <[email protected]>
Cc: [email protected]; Maxime Coquelin <[email protected]>; Yuanhan Liu <[email protected]>; Thomas Monjalon <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v6 2/6] vhost: rewrite enqueueQuoted 11 lines · Zhihong Wang
This patch implements the vhost logic from scratch into a single function to improve maintainability. This is the baseline version of the new code, more optimization will be added in the following patches in this patch set.
In the existing code there're 2 callbacks for vhost enqueue:
*
virtio_dev_merge_rxformrg_rxbufturned on cases.*
virtio_dev_rxformrg_rxbufturned off cases.Having 2 callback paths increases maintenance effort. Also, the
performance
Quoted 6 lines
of the existing code is not optimal, especially when the
mrg_rxbuffeature turned on.Signed-off-by Zhihong Wang <[email protected]>---
Changes in v6:.....
Quoted 10 lines
lib/librte_vhost/virtio_net.c +3
-6-/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_mergeable (struct vhost_virtqueue *vq,uint32_t size, -uint16_t *end,struct buf_vector *buf_vec) +uint16_t +rte_vhost_enqueue_burst (int vid,uint16_t queue_id, +struct rte_mbuf **pkts,uint16_t count) {
Quoted 8 lines
lib/librte_vhost/virtio_net.c virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr) · line 278
Quoted 6 lines
lib/librte_vhost/virtio_net.c +5
-1-uint16_t cur_idx; +struct vhost_virtqueue *vq; +struct virtio_net *dev; +uint32_t is_mrg_rxbuf =0 ; +uint32_t pkt_idx =0 ; +uint32_t pkt_left = count;Is
pkt_leftreally needed?
It's a matter of coding style since there's no underlying difference. I prefer this way personally.
Quoted 6 lines
Quoted 5 lines
uint16_t avail_idx;lib/librte_vhost/virtio_net.c +0
-3-uint32_t allocated =0 ; -uint32_t vec_idx =0 ; -uint16_t tries =0 ;....
PATCH v6 3/6 vhost: remove useless volatile
In reply to Zhihong WangThis patch removes useless volatile attribute to allow compiler optimization.
--- lib/librte_vhost/vhost.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost.h +1 -1
@@ -71,7 +71,7 @@ struct vhost_virtqueue {uint32_t size;/* Last index used on the available ring */ -volatile uint16_t last_used_idx; +uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
Signature · 1 line
2.7.4
PATCH v6 4/6 vhost: add desc prefetch
In reply to Zhihong WangThis patch adds descriptor prefetch to hide cache access latency.
--- lib/librte_vhost/virtio_net.c | 6 ++++++ 1 file changed, 6 insertions(+)
lib/librte_vhost/virtio_net.c +6 -0
@@ -300,6 +300,12 @@ rte_vhost_enqueue_burst(int vid, uint16_t queue_id,/* start enqueuing packets 1 by 1 */ avail_idx = *((volatile uint16_t *)&vq->avail->idx);while (pkt_left && avail_idx != vq->last_used_idx) { +/* prefetch the next desc */ +if (pkt_left >1 && avail_idx != vq->last_used_idx +1 ) +rte_prefetch0 (&vq->desc[vq->avail->ring[ + (vq->last_used_idx +1 ) & + (vq->size -1 )]]); +if (enqueue_packet (dev, vq, avail_idx, pkts[pkt_idx], is_mrg_rxbuf))break ;
Signature · 1 line
2.7.4
PATCH v6 5/6 vhost: batch update used ring
In reply to Zhihong WangThis patch enables batch update of the used ring for better efficiency.
---
Changes in v6:
1. Introduce a function for used ring write.
---
Changes in v4:
1. Free shadow used ring in the right place.
2. Add failure check for shadow used ring malloc.
lib/librte_vhost/vhost.c | 20 +++++++++++++++-- lib/librte_vhost/vhost.h | 4 ++++ lib/librte_vhost/vhost_user.c | 31 +++++++++++++++++++++----- lib/librte_vhost/virtio_net.c | 52 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 89 insertions(+), 18 deletions(-)
lib/librte_vhost/vhost.c +18 -2
@@ -119,10 +119,26 @@ cleanup_device(struct virtio_net *dev, int destroy)static void free_device (struct virtio_net *dev) { +struct vhost_virtqueue *vq_0; +struct vhost_virtqueue *vq_1;uint32_t i; -for (i =0 ; i < dev->virt_qp_nb; i++) -rte_free (dev->virtqueue[i *VIRTIO_QNUM ]); +for (i =0 ; i < dev->virt_qp_nb; i++) { + vq_0 = dev->virtqueue[i *VIRTIO_QNUM ]; +if (vq_0->shadow_used_ring) { +rte_free (vq_0->shadow_used_ring); + vq_0->shadow_used_ring =NULL ; + } + + vq_1 = dev->virtqueue[i *VIRTIO_QNUM +1 ]; +if (vq_1->shadow_used_ring) { +rte_free (vq_1->shadow_used_ring); + vq_1->shadow_used_ring =NULL ; + } + +/* malloc together, free together */ +rte_free (vq_0); + }rte_free (dev); }
lib/librte_vhost/vhost.h +4 -0
@@ -85,6 +85,10 @@ struct vhost_virtqueue {/* Physical address of used ring, for logging */ uint64_t log_guest_addr; + +/* Shadow used ring for performance */ +struct vring_used_elem *shadow_used_ring; +uint32_t shadow_used_idx; } __rte_cache_aligned;/* Old kernels have no such macro defined */
lib/librte_vhost/vhost_user.c +26 -5
@@ -193,7 +193,21 @@ static int vhost_user_set_vring_num(struct virtio_net *dev, struct vhost_vring_state *state) { - dev->virtqueue[state->index]->size = state->num; + struct vhost_virtqueue *vq; + + vq = dev->virtqueue[state->index]; + vq->size = state->num; +if (!vq->shadow_used_ring) { + vq->shadow_used_ring =rte_malloc (NULL , + vq->size *sizeof (struct vring_used_elem ), +RTE_CACHE_LINE_SIZE ); +if (!vq->shadow_used_ring) { +RTE_LOG (ERR ,VHOST_CONFIG , +"Failed to allocate memory" +" for shadow used ring.\n" ); +return -1 ; + } + }return 0 ; }
@@ -611,14 +625,21 @@ static int vhost_user_get_vring_base(struct virtio_net *dev, struct vhost_vring_state *state) { + struct vhost_virtqueue *vq; +/* We have to stop the queue (virtio) if it is running. */ if (dev->flags &VIRTIO_DEV_RUNNING ) { dev->flags &= ~VIRTIO_DEV_RUNNING ; notify_ops->destroy_device (dev->vid); } + vq = dev->virtqueue[state->index];/* Here we are safe to get the last used index */ - state->num = dev->virtqueue[state->index]->last_used_idx; + state->num = vq->last_used_idx; +if (vq->shadow_used_ring) { +rte_free (vq->shadow_used_ring); + vq->shadow_used_ring =NULL ; + }RTE_LOG (INFO ,VHOST_CONFIG ,"vring base idx:%d file:%d\n" , state->index, state->num);
@@ -627,10 +648,10 @@ vhost_user_get_vring_base(struct virtio_net *dev,* sent and only sent in vhost_vring_stop. * TODO: cleanup the vring, it isn't usable since here. */ -if (dev->virtqueue[state->index]->kickfd >=0 ) -close (dev->virtqueue[state->index]->kickfd); +if (vq->kickfd >=0 ) +close (vq->kickfd); - dev->virtqueue[state->index]->kickfd =VIRTIO_UNINITIALIZED_EVENTFD ; + vq->kickfd =VIRTIO_UNINITIALIZED_EVENTFD ;return 0 ; }
lib/librte_vhost/virtio_net.c +41 -11
90 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v6 6/6 vhost: optimize cache access
In reply to Zhihong WangThis patch reorders the code to delay virtio header write to improve cache access efficiency for cases where the mrg_rxbuf feature is turned on. CPU pipeline stall cycles can be significantly reduced.
Virtio header write and mbuf data copy are all remote store operations which takes a long time to finish. It's a good idea to put them together to remove bubbles in between, to let as many remote store instructions as possible go into store buffer at the same time to hide latency, and to let the H/W prefetcher goes to work as early as possible.
On a Haswell machine, about 100 cycles can be saved per packet by this patch alone. Taking 64B packets traffic for example, this means about 60% efficiency improvement for the enqueue operation.
---
Changes in v3:
1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
lib/librte_vhost/virtio_net.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
lib/librte_vhost/virtio_net.c +14 -6
@@ -185,6 +185,7 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t mbuf_len;uint32_t mbuf_avail;uint32_t cpy_len; +uint32_t copy_virtio_hdr;uint32_t num_buffers =0 ;/* start with the first mbuf of the packet */
@@ -199,12 +200,12 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq,if (unlikely (!desc_addr)) goto error; -/* handle virtio header */ +/* +* handle virtio header, the actual write operation is delayed +* for cache optimization, to reduce CPU pipeline stall cycles. +*/ virtio_hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t )desc_addr; -virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); -if (is_mrg_rxbuf) - virtio_hdr->num_buffers =1 ; - + copy_virtio_hdr =1 ;vhost_log_write (dev, desc->addr, dev->vhost_hlen);PRINT_PACKET (dev, (uintptr_t )desc_addr, dev->vhost_hlen,0 ); desc_offset = dev->vhost_hlen;
@@ -249,8 +250,15 @@ enqueue_packet(struct virtio_net *dev, struct vhost_virtqueue *vq, goto error; } -/* copy mbuf data */ +/* copy virtio header and mbuf data */ cpy_len =RTE_MIN (desc->len - desc_offset, mbuf_avail); +if (copy_virtio_hdr) { + copy_virtio_hdr =0 ; +virtio_enqueue_offload (mbuf, &(virtio_hdr->hdr)); +if (is_mrg_rxbuf) + virtio_hdr->num_buffers = num_buffers +1 ; + } + rte_memcpy((void *)(uintptr_t)desc_addr, rte_pktmbuf_mtod_offset(mbuf, void *, mbuf_len - mbuf_avail),
Signature · 1 line
2.7.4
Re: [PATCH v6 6/6] vhost: optimize cache access
Quoted 10 lines · Zhihong Wang
This patch reorders the code to delay virtio header write to improve cache access efficiency for cases where the
mrg_rxbuffeature is turned on. CPU pipeline stall cycles can be significantly reduced.Virtio header write and mbuf data copy are all remote store operations which takes a long time to finish. It's a good idea to put them together to remove bubbles in between, to let as many remote store instructions as possible go into store buffer at the same time to hide latency, and to let the H/W prefetcher goes to work as early as possible.
Quoted 3 lines
On a Haswell machine, about 100 cycles can be saved per packet by this patch alone. Taking 64B packets traffic for example, this means about 60% efficiency improvement for the enqueue operation.
Thanks for the detailed information, I appreciate it.
Maxime
Re: [PATCH v6 0/6] vhost: optimize enqueue
In reply to Zhihong WangHi Maxime,
Do you have more comments about this set? If no, I think I could merge it shortly.
Thanks.
--yliu
Quoted 114 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed for high performance and good maintainability, and improves CPU efficiency significantly by optimizing cache access, which means:
* Higher maximum throughput can be achieved for fast frontends like DPDK virtio pmd.
* Better scalability can be achieved that each vhost core can support more connections because it takes less cycles to handle each single frontend.
This patch set contains:
1. A Windows VM compatibility fix for vhost enqueue in 16.07 release.
2. A baseline patch to rewrite the vhost logic.
3. A series of optimization patches added upon the baseline.
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Code reordering and batch used ring update bring most of the performance improvements.
In the existing code there're 2 callbacks for vhost enqueue:
*
virtio_dev_merge_rxformrg_rxbufturned on cases.*
virtio_dev_rxformrg_rxbufturned off cases.The performance of the existing code is not optimal, especially when the
mrg_rxbuffeature turned on. Besides, having 2 callback paths increases maintenance efforts.Also, there's a compatibility issue in the existing code which causes Windows VM to hang when the
mrg_rxbuffeature turned on.---
Changes in v6:1. Merge duplicated code.
2. Introduce a function for used ring write.
3. Add necessary comments.
---
Changes in v5:1. Rebase to dpdk-next-virtio master.
2. Rename variables to keep consistent in naming style.
3. Small changes like return value adjustment and vertical alignment.
4. Add details in commit log.
---
Changes in v4:1. Fix a Windows VM compatibility issue.
2. Free shadow used ring in the right place.
3. Add failure check for shadow used ring malloc.
4. Refactor the code for clearer logic.
5. Add
PRINT_PACKETfor debugging.---
Changes in v3:1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
3. Rewrite enqueue and delete the obsolete in the same patch.
---
Changes in v2:1. Split the big function into several small ones.
2. Use multiple patches to explain each optimization.
3. Add comments.
Zhihong Wang (6):
vhost: fix windows vm hang
vhost: rewrite enqueue
vhost: remove useless volatile
vhost: add desc prefetch
vhost: batch update used ring
vhost: optimize cache accesslib/librte_vhost/vhost.c | 20 +- lib/librte_vhost/vhost.h | 6 +- lib/librte_vhost/vhost_user.c | 31 ++- lib/librte_vhost/virtio_net.c | 541 ++++++++++++++---------------------------- 4 files changed, 225 insertions(+), 373 deletions(-)Signature · 1 line
2.7.4
Maxime Coquelin
Reviewed-byRe: [PATCH v6 0/6] vhost: optimize enqueue
Hi Yuanhan,
Quoted 4 lines · Yuanhan Liu
Hi Maxime,
Do you have more comments about this set? If no, I think I could merge it shortly.
No more comments, this is good to me.
Feel free to add:
Thanks,
Maxime
Quoted 119 lines
Thanks.
--yliuQuoted 114 lines · Zhihong Wang
This patch set optimizes the vhost enqueue function.
It implements the vhost logic from scratch into a single function designed for high performance and good maintainability, and improves CPU efficiency significantly by optimizing cache access, which means:
* Higher maximum throughput can be achieved for fast frontends like DPDK virtio pmd.
* Better scalability can be achieved that each vhost core can support more connections because it takes less cycles to handle each single frontend.
This patch set contains:
1. A Windows VM compatibility fix for vhost enqueue in 16.07 release.
2. A baseline patch to rewrite the vhost logic.
3. A series of optimization patches added upon the baseline.
The main optimization techniques are:
1. Reorder code to reduce CPU pipeline stall cycles.
2. Batch update the used ring for better efficiency.
3. Prefetch descriptor to hide cache latency.
4. Remove useless volatile attribute to allow compiler optimization.
Code reordering and batch used ring update bring most of the performance improvements.
In the existing code there're 2 callbacks for vhost enqueue:
*
virtio_dev_merge_rxformrg_rxbufturned on cases.*
virtio_dev_rxformrg_rxbufturned off cases.The performance of the existing code is not optimal, especially when the
mrg_rxbuffeature turned on. Besides, having 2 callback paths increases maintenance efforts.Also, there's a compatibility issue in the existing code which causes Windows VM to hang when the
mrg_rxbuffeature turned on.---
Changes in v6:1. Merge duplicated code.
2. Introduce a function for used ring write.
3. Add necessary comments.
---
Changes in v5:1. Rebase to dpdk-next-virtio master.
2. Rename variables to keep consistent in naming style.
3. Small changes like return value adjustment and vertical alignment.
4. Add details in commit log.
---
Changes in v4:1. Fix a Windows VM compatibility issue.
2. Free shadow used ring in the right place.
3. Add failure check for shadow used ring malloc.
4. Refactor the code for clearer logic.
5. Add
PRINT_PACKETfor debugging.---
Changes in v3:1. Remove unnecessary memset which causes frontend stall on SNB & IVB.
2. Rename variables to follow naming convention.
3. Rewrite enqueue and delete the obsolete in the same patch.
---
Changes in v2:1. Split the big function into several small ones.
2. Use multiple patches to explain each optimization.
3. Add comments.
Zhihong Wang (6):
vhost: fix windows vm hang
vhost: rewrite enqueue
vhost: remove useless volatile
vhost: add desc prefetch
vhost: batch update used ring
vhost: optimize cache accesslib/librte_vhost/vhost.c | 20 +- lib/librte_vhost/vhost.h | 6 +- lib/librte_vhost/vhost_user.c | 31 ++- lib/librte_vhost/virtio_net.c | 541 ++++++++++++++---------------------------- 4 files changed, 225 insertions(+), 373 deletions(-)--
2.7.4
v7 cover vhost: optimize mergeable Rx path
In reply to Zhihong WangThis is a new set of patches to optimize the mergeable Rx code path. No refactoring (rewrite) was made this time. It just applies some findings from Zhihong (kudos to him!) that could improve the mergeable Rx path on the old code.
The two major factors that could improve the performance greatly are:
- copy virtio header together with packet data. This could remove the buubbles between the two copy to optimize the cache access.
This is implemented in patch 2 "vhost: optimize cache access"
- shadow used ring update and update them at once
The basic idea is to update used ring in a local buffer and flush them to the virtio used ring at once in the end. Again, this is for optimizing the cache access.
This is implemented in patch 5 "vhost: shadow used ring update"
The two optimizations could yield 40+% performance in micro testing and 20+% in PVP case testing with 64B packet size.
Besides that, there are some tiny optimizations, such as prefetch avail ring (patch 6) and retrieve avail head once (patch 7).
Note: the shadow used ring tech could also be applied to the non-mrg Rx path (and even the dequeu) path. I didn't do that for two reasons:
- we already update used ring in batch in both path: it's not shadowed first though.
- it's a bit too late too make many changes at this stage: RC1 is out.
Please help testing.
Thanks.
--yliu
---
Yuanhan Liu (4):
vhost: simplify mergeable Rx vring reservation
vhost: use last avail idx for avail ring reservation
vhost: prefetch avail ring
vhost: retrieve avail head once
Zhihong Wang (3):
vhost: remove useless volatile
vhost: optimize cache access
vhost: shadow used ring update
lib/librte_vhost/vhost.c | 13 ++- lib/librte_vhost/vhost.h | 5 +- lib/librte_vhost/vhost_user.c | 23 +++-- lib/librte_vhost/virtio_net.c | 193 +++++++++++++++++++++++++----------------- 4 files changed, 149 insertions(+), 85 deletions(-)
Signature · 1 line
1.9.0
PATCH v7 1/7 vhost: remove useless volatile
From: Zhihong Wang <[email protected]>
last_used_idx is a local var, there is no need to decorate it by "volatile".
--- lib/librte_vhost/vhost.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_vhost/vhost.h +1 -1
@@ -85,7 +85,7 @@ struct vhost_virtqueue {uint32_t size;uint16_t last_avail_idx; -volatile uint16_t last_used_idx; +uint16_t last_used_idx;#define VIRTIO_INVALID_EVENTFD (-1)#define VIRTIO_UNINITIALIZED_EVENTFD (-2)
Signature · 1 line
1.9.0