Skip to content

Commit 0ed005c

Browse files
jasowangdavem330
authored andcommitted
vhost_net: tx batching
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 <[email protected]> Signed-off-by: Jason Wang <[email protected]> Acked-by: Michael S. Tsirkin <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 275bf96 commit 0ed005c

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

drivers/vhost/net.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,15 @@ static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
351351
return r;
352352
}
353353

354+
static bool vhost_exceeds_maxpend(struct vhost_net *net)
355+
{
356+
struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
357+
struct vhost_virtqueue *vq = &nvq->vq;
358+
359+
return (nvq->upend_idx + vq->num - VHOST_MAX_PEND) % UIO_MAXIOV
360+
== nvq->done_idx;
361+
}
362+
354363
/* Expects to be always run from workqueue - which acts as
355364
* read-size critical section for our kind of RCU. */
356365
static void handle_tx(struct vhost_net *net)
@@ -394,8 +403,7 @@ static void handle_tx(struct vhost_net *net)
394403
/* If more outstanding DMAs, queue the work.
395404
* Handle upend_idx wrap around
396405
*/
397-
if (unlikely((nvq->upend_idx + vq->num - VHOST_MAX_PEND)
398-
% UIO_MAXIOV == nvq->done_idx))
406+
if (unlikely(vhost_exceeds_maxpend(net)))
399407
break;
400408

401409
head = vhost_net_tx_get_vq_desc(net, vq, vq->iov,
@@ -454,6 +462,16 @@ static void handle_tx(struct vhost_net *net)
454462
msg.msg_control = NULL;
455463
ubufs = NULL;
456464
}
465+
466+
total_len += len;
467+
if (total_len < VHOST_NET_WEIGHT &&
468+
!vhost_vq_avail_empty(&net->dev, vq) &&
469+
likely(!vhost_exceeds_maxpend(net))) {
470+
msg.msg_flags |= MSG_MORE;
471+
} else {
472+
msg.msg_flags &= ~MSG_MORE;
473+
}
474+
457475
/* TODO: Check specific error and bomb out unless ENOBUFS? */
458476
err = sock->ops->sendmsg(sock, &msg, len);
459477
if (unlikely(err < 0)) {
@@ -472,7 +490,6 @@ static void handle_tx(struct vhost_net *net)
472490
vhost_add_used_and_signal(&net->dev, vq, head, 0);
473491
else
474492
vhost_zerocopy_signal_used(net, vq);
475-
total_len += len;
476493
vhost_net_tx_packet(net);
477494
if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
478495
vhost_poll_queue(&vq->poll);

0 commit comments

Comments
 (0)