Skip to content

Commit 92bab4e

Browse files
committed
Merge branch 'virtio-net-busy-poll'
Jason Wang says: ==================== rx busy polling support for virtio-net This series introduces the support for rx busy polling support. This was useful for reducing the latency for a kvm guest. Instead of introducing new states and spinlocks, this series re-uses NAPI state to synchonrize between NAPI and busy polling. This grealy simplified the codes and reduce the overheads of spinlocks for normal NAPI fast path. Test was done between a kvm guest and an external host. Two hosts were connected through 40gb mlx4 cards. With both busy_poll and busy_read are set to 50 in guest, 1 byte netperf tcp_rr shows 127% improvement: transaction rate was increased from 8353.33 to 18966.87. Changes from V2: - Avoid introducing new states and spinlocks by reusuing the NAPI state - Fix the budget calculation in virtnet_poll() - Drop patch 1/3 from V2 since it was useless Changes from V1: - split the patch info smaller ones - add more details about test setup/configuration ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 96b3bff + 9181563 commit 92bab4e

1 file changed

Lines changed: 61 additions & 6 deletions

File tree

‎drivers/net/virtio_net.c‎

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/slab.h>
2828
#include <linux/cpu.h>
2929
#include <linux/average.h>
30+
#include <net/busy_poll.h>
3031

3132
static int napi_weight = NAPI_POLL_WEIGHT;
3233
module_param(napi_weight, int, 0444);
@@ -521,6 +522,8 @@ static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
521522
skb_shinfo(skb)->gso_segs = 0;
522523
}
523524

525+
skb_mark_napi_id(skb, &rq->napi);
526+
524527
netif_receive_skb(skb);
525528
return;
526529

@@ -725,15 +728,12 @@ static void refill_work(struct work_struct *work)
725728
}
726729
}
727730

728-
static int virtnet_poll(struct napi_struct *napi, int budget)
731+
static int virtnet_receive(struct receive_queue *rq, int budget)
729732
{
730-
struct receive_queue *rq =
731-
container_of(napi, struct receive_queue, napi);
732733
struct virtnet_info *vi = rq->vq->vdev->priv;
734+
unsigned int len, received = 0;
733735
void *buf;
734-
unsigned int r, len, received = 0;
735736

736-
again:
737737
while (received < budget &&
738738
(buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
739739
receive_buf(rq, buf, len);
@@ -745,6 +745,18 @@ static int virtnet_poll(struct napi_struct *napi, int budget)
745745
schedule_delayed_work(&vi->refill, 0);
746746
}
747747

748+
return received;
749+
}
750+
751+
static int virtnet_poll(struct napi_struct *napi, int budget)
752+
{
753+
struct receive_queue *rq =
754+
container_of(napi, struct receive_queue, napi);
755+
unsigned int r, received = 0;
756+
757+
again:
758+
received += virtnet_receive(rq, budget - received);
759+
748760
/* Out of packets? */
749761
if (received < budget) {
750762
r = virtqueue_enable_cb_prepare(rq->vq);
@@ -760,6 +772,43 @@ static int virtnet_poll(struct napi_struct *napi, int budget)
760772
return received;
761773
}
762774

775+
#ifdef CONFIG_NET_RX_BUSY_POLL
776+
/* must be called with local_bh_disable()d */
777+
static int virtnet_busy_poll(struct napi_struct *napi)
778+
{
779+
struct receive_queue *rq =
780+
container_of(napi, struct receive_queue, napi);
781+
struct virtnet_info *vi = rq->vq->vdev->priv;
782+
int r, received = 0, budget = 4;
783+
784+
if (!(vi->status & VIRTIO_NET_S_LINK_UP))
785+
return LL_FLUSH_FAILED;
786+
787+
if (!napi_schedule_prep(napi))
788+
return LL_FLUSH_BUSY;
789+
790+
virtqueue_disable_cb(rq->vq);
791+
792+
again:
793+
received += virtnet_receive(rq, budget);
794+
795+
r = virtqueue_enable_cb_prepare(rq->vq);
796+
clear_bit(NAPI_STATE_SCHED, &napi->state);
797+
if (unlikely(virtqueue_poll(rq->vq, r)) &&
798+
napi_schedule_prep(napi)) {
799+
virtqueue_disable_cb(rq->vq);
800+
if (received < budget) {
801+
budget -= received;
802+
goto again;
803+
} else {
804+
__napi_schedule(napi);
805+
}
806+
}
807+
808+
return received;
809+
}
810+
#endif /* CONFIG_NET_RX_BUSY_POLL */
811+
763812
static int virtnet_open(struct net_device *dev)
764813
{
765814
struct virtnet_info *vi = netdev_priv(dev);
@@ -1347,6 +1396,9 @@ static const struct net_device_ops virtnet_netdev = {
13471396
#ifdef CONFIG_NET_POLL_CONTROLLER
13481397
.ndo_poll_controller = virtnet_netpoll,
13491398
#endif
1399+
#ifdef CONFIG_NET_RX_BUSY_POLL
1400+
.ndo_busy_poll = virtnet_busy_poll,
1401+
#endif
13501402
};
13511403

13521404
static void virtnet_config_changed_work(struct work_struct *work)
@@ -1552,6 +1604,7 @@ static int virtnet_alloc_queues(struct virtnet_info *vi)
15521604
vi->rq[i].pages = NULL;
15531605
netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
15541606
napi_weight);
1607+
napi_hash_add(&vi->rq[i].napi);
15551608

15561609
sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
15571610
ewma_init(&vi->rq[i].mrg_avg_pkt_len, 1, RECEIVE_AVG_WEIGHT);
@@ -1853,11 +1906,13 @@ static int virtnet_freeze(struct virtio_device *vdev)
18531906
netif_device_detach(vi->dev);
18541907
cancel_delayed_work_sync(&vi->refill);
18551908

1856-
if (netif_running(vi->dev))
1909+
if (netif_running(vi->dev)) {
18571910
for (i = 0; i < vi->max_queue_pairs; i++) {
18581911
napi_disable(&vi->rq[i].napi);
1912+
napi_hash_del(&vi->rq[i].napi);
18591913
netif_napi_del(&vi->rq[i].napi);
18601914
}
1915+
}
18611916

18621917
remove_vq_common(vi);
18631918

0 commit comments

Comments
 (0)