Re: [PATCH net-next V5 2/3] vhost_net: tx batching

2017-01-18 Thread Michael S. Tsirkin
On Wed, Jan 18, 2017 at 03:02:02PM +0800, Jason Wang wrote:
> This patch tries to utilize tuntap rx batching by peeking the tx
> virtqueue during transmission, if there's more available buffers in
> the virtqueue, set MSG_MORE flag for a hint for backend (e.g tuntap)
> to batch the packets.
> 
> Reviewed-by: Stefan Hajnoczi 
> Signed-off-by: Jason Wang 

Acked-by: Michael S. Tsirkin 

> ---
>  drivers/vhost/net.c | 23 ---
>  1 file changed, 20 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
> index 5dc3465..c42e9c3 100644
> --- a/drivers/vhost/net.c
> +++ b/drivers/vhost/net.c
> @@ -351,6 +351,15 @@ static int vhost_net_tx_get_vq_desc(struct vhost_net 
> *net,
>   return r;
>  }
>  
> +static bool vhost_exceeds_maxpend(struct vhost_net *net)
> +{
> + struct vhost_net_virtqueue *nvq = >vqs[VHOST_NET_VQ_TX];
> + struct vhost_virtqueue *vq = >vq;
> +
> + return (nvq->upend_idx + vq->num - VHOST_MAX_PEND) % UIO_MAXIOV
> + == nvq->done_idx;
> +}
> +
>  /* Expects to be always run from workqueue - which acts as
>   * read-size critical section for our kind of RCU. */
>  static void handle_tx(struct vhost_net *net)
> @@ -394,8 +403,7 @@ static void handle_tx(struct vhost_net *net)
>   /* If more outstanding DMAs, queue the work.
>* Handle upend_idx wrap around
>*/
> - if (unlikely((nvq->upend_idx + vq->num - VHOST_MAX_PEND)
> -   % UIO_MAXIOV == nvq->done_idx))
> + if (unlikely(vhost_exceeds_maxpend(net)))
>   break;
>  
>   head = vhost_net_tx_get_vq_desc(net, vq, vq->iov,
> @@ -454,6 +462,16 @@ static void handle_tx(struct vhost_net *net)
>   msg.msg_control = NULL;
>   ubufs = NULL;
>   }
> +
> + total_len += len;
> + if (total_len < VHOST_NET_WEIGHT &&
> + !vhost_vq_avail_empty(>dev, vq) &&
> + likely(!vhost_exceeds_maxpend(net))) {
> + msg.msg_flags |= MSG_MORE;
> + } else {
> + msg.msg_flags &= ~MSG_MORE;
> + }
> +
>   /* TODO: Check specific error and bomb out unless ENOBUFS? */
>   err = sock->ops->sendmsg(sock, , len);
>   if (unlikely(err < 0)) {
> @@ -472,7 +490,6 @@ static void handle_tx(struct vhost_net *net)
>   vhost_add_used_and_signal(>dev, vq, head, 0);
>   else
>   vhost_zerocopy_signal_used(net, vq);
> - total_len += len;
>   vhost_net_tx_packet(net);
>   if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
>   vhost_poll_queue(>poll);
> -- 
> 2.7.4


[PATCH net-next V5 2/3] vhost_net: tx batching

2017-01-17 Thread Jason Wang
This patch tries to utilize tuntap rx batching by peeking the tx
virtqueue during transmission, if there's more available buffers in
the virtqueue, set MSG_MORE flag for a hint for backend (e.g tuntap)
to batch the packets.

Reviewed-by: Stefan Hajnoczi 
Signed-off-by: Jason Wang 
---
 drivers/vhost/net.c | 23 ---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
index 5dc3465..c42e9c3 100644
--- a/drivers/vhost/net.c
+++ b/drivers/vhost/net.c
@@ -351,6 +351,15 @@ static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
return r;
 }
 
+static bool vhost_exceeds_maxpend(struct vhost_net *net)
+{
+   struct vhost_net_virtqueue *nvq = >vqs[VHOST_NET_VQ_TX];
+   struct vhost_virtqueue *vq = >vq;
+
+   return (nvq->upend_idx + vq->num - VHOST_MAX_PEND) % UIO_MAXIOV
+   == nvq->done_idx;
+}
+
 /* Expects to be always run from workqueue - which acts as
  * read-size critical section for our kind of RCU. */
 static void handle_tx(struct vhost_net *net)
@@ -394,8 +403,7 @@ static void handle_tx(struct vhost_net *net)
/* If more outstanding DMAs, queue the work.
 * Handle upend_idx wrap around
 */
-   if (unlikely((nvq->upend_idx + vq->num - VHOST_MAX_PEND)
- % UIO_MAXIOV == nvq->done_idx))
+   if (unlikely(vhost_exceeds_maxpend(net)))
break;
 
head = vhost_net_tx_get_vq_desc(net, vq, vq->iov,
@@ -454,6 +462,16 @@ static void handle_tx(struct vhost_net *net)
msg.msg_control = NULL;
ubufs = NULL;
}
+
+   total_len += len;
+   if (total_len < VHOST_NET_WEIGHT &&
+   !vhost_vq_avail_empty(>dev, vq) &&
+   likely(!vhost_exceeds_maxpend(net))) {
+   msg.msg_flags |= MSG_MORE;
+   } else {
+   msg.msg_flags &= ~MSG_MORE;
+   }
+
/* TODO: Check specific error and bomb out unless ENOBUFS? */
err = sock->ops->sendmsg(sock, , len);
if (unlikely(err < 0)) {
@@ -472,7 +490,6 @@ static void handle_tx(struct vhost_net *net)
vhost_add_used_and_signal(>dev, vq, head, 0);
else
vhost_zerocopy_signal_used(net, vq);
-   total_len += len;
vhost_net_tx_packet(net);
if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
vhost_poll_queue(>poll);
-- 
2.7.4