Skip to contentSkip to conversation

Improve virtio performance for 9k mtu

PATCH Superseded Gavin Li 3 participants 8 messages Aug 30, 2022 – Aug 31, 2022

Subsystems (4)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Gavin Li

author
#0 lore raw

v3 cover Improve virtio performance for 9k mtu

This small series contains two patches that improves virtio netdevice performance for 9K mtu when GRO/ guest TSO is disabled.

Gavin Li (2):
virtio-net: introduce and use helper function for guest gso support
checks
virtio-net: use mtu size as buffer length for big packets

 drivers/net/virtio_net.c | 48 ++++++++++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 16 deletions(-)
Signature · 6 lines
2.31.1


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
author
#1 lore raw

PATCH v3 1/2 virtio-net: introduce and use helper function for guest gso support checks

Probe routine is already several hundred lines.
Use helper function for guest gso support check.

---
changelog:
v1->v2
- Add new patch

---
 drivers/net/virtio_net.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/net/virtio_net.c +9 -4
index 9cce7dec7366..e1904877d461 100644
@@ -3682,6 +3682,14 @@ static int virtnet_validate(struct virtio_device *vdev)
 	return 0;
 }
 
+static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
+{
+	return (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
+		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
+		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
+		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO));
+}
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
 	int i, err = -ENOMEM;
@@ -3777,10 +3785,7 @@ static int virtnet_probe(struct virtio_device *vdev)
 	spin_lock_init(&vi->refill_lock);
 
 	/* If we can receive ANY GSO packets, we must allocate large ones. */
-	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
+	if (virtnet_check_guest_gso(vi))
 		vi->big_packets = true;
 
 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
Signature · 6 lines
2.31.1


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
author
#4 lore raw

PATCH v3 2/2 virtio-net: use mtu size as buffer length for big packets

In reply to Gavin Li

Currently add_recvbuf_big() allocates MAX_SKB_FRAGS segments for big packets even when GUEST_* offloads are not present on the device. However, if guest GSO is not supported, it would be sufficient to allocate segments to cover just up the MTU size and no further. Allocating the maximum amount of segments results in a large waste of buffer space in the queue, which limits the number of packets that can be buffered and can result in reduced performance.

Therefore, if guest GSO is not supported, use the MTU to calculate the optimal amount of segments required.

When guest offload is enabled at runtime, RQ already has packets of bytes less than 64K. So when packet of 64KB arrives, all the packets of such size will be dropped. and RQ is now not usable.

So this means that during set_guest_offloads() phase, RQs have to be destroyed and recreated, which requires almost driver reload.

If VIRTIO_NET_F_CTRL_GUEST_OFFLOADS has been negotiated, then it should always treat them as GSO enabled.

Below is the iperf TCP test results over a Mellanox NIC, using vDPA for 1 VQ, queue size 1024, before and after the change, with the iperf server running over the virtio-net interface.

MTU(Bytes)/Bandwidth (Gbit/s)
Before After

  1500        22.5     22.4
  9000        12.8     25.9

---
changelog:
v2->v3
- Addressed comments from Si-Wei
- Simplify the condition check to enable the optimization v1->v2
- Addressed comments from Jason, Michael, Si-Wei.
- Remove the flag of guest GSO support, set sg_num for big packets and use it directly
- Recalculate sg_num for big packets in virtnet_set_guest_offloads
- Replace the round up algorithm with DIV_ROUND_UP

---
 drivers/net/virtio_net.c | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +24 -13
index e1904877d461..d2721e71af18 100644
@@ -225,6 +225,9 @@ struct virtnet_info {
 	/* I like... big packets and I cannot lie! */
 	bool big_packets;
 
+	/* number of sg entries allocated for big packets */
+	unsigned int big_packets_sg_num;
+
 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
 	bool mergeable_rx_bufs;
 
@@ -1331,10 +1334,10 @@ static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
 	char *p;
 	int i, err, offset;
 
-	sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
+	sg_init_table(rq->sg, vi->big_packets_sg_num + 2);
 
-	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
-	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
+	/* page in rq->sg[vi->big_packets_sg_num + 1] is list tail */
+	for (i = vi->big_packets_sg_num + 1; i > 1; --i) {
 		first = get_a_page(rq, gfp);
 		if (!first) {
 			if (list)
@@ -1365,7 +1368,7 @@ static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
 
 	/* chain first in list head */
 	first->private = (unsigned long)list;
-	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
+	err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_sg_num + 2,
 				  first, gfp);
 	if (err < 0)
 		give_pages(rq, first);
@@ -3690,13 +3693,27 @@ static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO));
 }
 
+static void virtnet_set_big_packets_fields(struct virtnet_info *vi, const int mtu)
+{
+	bool guest_gso = virtnet_check_guest_gso(vi);
+
+	/* If device can receive ANY guest GSO packets, regardless of mtu,
+	 * allocate packets of maximum size, otherwise limit it to only
+	 * mtu size worth only.
+	 */
+	if (mtu > ETH_DATA_LEN || guest_gso) {
+		vi->big_packets = true;
+		vi->big_packets_sg_num = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
+	}
+}
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
 	int i, err = -ENOMEM;
 	struct net_device *dev;
 	struct virtnet_info *vi;
 	u16 max_queue_pairs;
-	int mtu;
+	int mtu = 0;
 
 	/* Find if host supports multiqueue/rss virtio_net device */
 	max_queue_pairs = 1;
@@ -3784,10 +3801,6 @@ static int virtnet_probe(struct virtio_device *vdev)
 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
 	spin_lock_init(&vi->refill_lock);
 
-	/* If we can receive ANY GSO packets, we must allocate large ones. */
-	if (virtnet_check_guest_gso(vi))
-		vi->big_packets = true;
-
 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
 		vi->mergeable_rx_bufs = true;
 
@@ -3853,12 +3866,10 @@ static int virtnet_probe(struct virtio_device *vdev)
 
 		dev->mtu = mtu;
 		dev->max_mtu = mtu;
-
-		/* TODO: size buffers correctly in this case. */
-		if (dev->mtu > ETH_DATA_LEN)
-			vi->big_packets = true;
 	}
 
+	virtnet_set_big_packets_fields(vi, mtu);
+
 	if (vi->any_header_sg)
 		dev->needed_headroom = vi->hdr_len;
 
Signature · 6 lines
2.31.1


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
reviewer Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] virtio-net: introduce and use helper function for guest gso support checks

In reply to Gavin Li
Quoted 6 lines · Gavin Li

Probe routine is already several hundred lines. Use helper function for guest gso support check.

Quoted 40 lines

---
changelog:
v1->v2
- Add new patch

---
 drivers/net/virtio_net.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/net/virtio_net.c +9 -5
index 9cce7dec7366..e1904877d461 100644
@@ -3682,6 +3682,14 @@ static int virtnet_validate(struct virtio_device *vdev)
 	return 0;
 }
 
+static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
+{
+	return (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
+		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
+		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
+		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO));
+}
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
 	int i, err = -ENOMEM;
@@ -3777,10 +3785,7 @@ static int virtnet_probe(struct virtio_device *vdev)
 	spin_lock_init(&vi->refill_lock);
 
 	/* If we can receive ANY GSO packets, we must allocate large ones. */
-	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
+	if (virtnet_check_guest_gso(vi))
 		vi->big_packets = true;
 
 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
--
Quoted 2 lines

2.31.1

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

reviewer Reviewed-by on v3 2/2
#5 lore raw

Re: [virtio-dev] [PATCH v3 2/2] virtio-net: use mtu size as buffer length for big packets

In reply to Gavin Li
Quoted 25 lines · Gavin Li

Currently add_recvbuf_big() allocates MAX_SKB_FRAGS segments for big packets even when GUEST_* offloads are not present on the device. However, if guest GSO is not supported, it would be sufficient to allocate segments to cover just up the MTU size and no further. Allocating the maximum amount of segments results in a large waste of buffer space in the queue, which limits the number of packets that can be buffered and can result in reduced performance.

Therefore, if guest GSO is not supported, use the MTU to calculate the optimal amount of segments required.

When guest offload is enabled at runtime, RQ already has packets of bytes less than 64K. So when packet of 64KB arrives, all the packets of such size will be dropped. and RQ is now not usable.

So this means that during set_guest_offloads() phase, RQs have to be destroyed and recreated, which requires almost driver reload.

If VIRTIO_NET_F_CTRL_GUEST_OFFLOADS has been negotiated, then it should always treat them as GSO enabled.

Below is the iperf TCP test results over a Mellanox NIC, using vDPA for 1 VQ, queue size 1024, before and after the change, with the iperf server running over the virtio-net interface.

Quoted 8 lines

MTU(Bytes)/Bandwidth (Gbit/s)
Before After

  1500        22.5     22.4
  9000        12.8     25.9
Quoted 111 lines

---
changelog:
v2->v3
- Addressed comments from Si-Wei
- Simplify the condition check to enable the optimization v1->v2
- Addressed comments from Jason, Michael, Si-Wei.
- Remove the flag of guest GSO support, set sg_num for big packets and use it directly
- Recalculate sg_num for big packets in virtnet_set_guest_offloads
- Replace the round up algorithm with DIV_ROUND_UP

---
 drivers/net/virtio_net.c | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +24 -14
index e1904877d461..d2721e71af18 100644
@@ -225,6 +225,9 @@ struct virtnet_info {
 	/* I like... big packets and I cannot lie! */
 	bool big_packets;
 
+	/* number of sg entries allocated for big packets */
+	unsigned int big_packets_sg_num;
+
 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
 	bool mergeable_rx_bufs;
 
@@ -1331,10 +1334,10 @@ static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
 	char *p;
 	int i, err, offset;
 
-	sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
+	sg_init_table(rq->sg, vi->big_packets_sg_num + 2);
 
-	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
-	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
+	/* page in rq->sg[vi->big_packets_sg_num + 1] is list tail */
+	for (i = vi->big_packets_sg_num + 1; i > 1; --i) {
 		first = get_a_page(rq, gfp);
 		if (!first) {
 			if (list)
@@ -1365,7 +1368,7 @@ static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
 
 	/* chain first in list head */
 	first->private = (unsigned long)list;
-	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
+	err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_sg_num + 2,
 				  first, gfp);
 	if (err < 0)
 		give_pages(rq, first);
@@ -3690,13 +3693,27 @@ static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO));
 }
 
+static void virtnet_set_big_packets_fields(struct virtnet_info *vi, const int mtu)
+{
+	bool guest_gso = virtnet_check_guest_gso(vi);
+
+	/* If device can receive ANY guest GSO packets, regardless of mtu,
+	 * allocate packets of maximum size, otherwise limit it to only
+	 * mtu size worth only.
+	 */
+	if (mtu > ETH_DATA_LEN || guest_gso) {
+		vi->big_packets = true;
+		vi->big_packets_sg_num = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
+	}
+}
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
 	int i, err = -ENOMEM;
 	struct net_device *dev;
 	struct virtnet_info *vi;
 	u16 max_queue_pairs;
-	int mtu;
+	int mtu = 0;
 
 	/* Find if host supports multiqueue/rss virtio_net device */
 	max_queue_pairs = 1;
@@ -3784,10 +3801,6 @@ static int virtnet_probe(struct virtio_device *vdev)
 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
 	spin_lock_init(&vi->refill_lock);
 
-	/* If we can receive ANY GSO packets, we must allocate large ones. */
-	if (virtnet_check_guest_gso(vi))
-		vi->big_packets = true;
-
 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
 		vi->mergeable_rx_bufs = true;
 
@@ -3853,12 +3866,10 @@ static int virtnet_probe(struct virtio_device *vdev)
 
 		dev->mtu = mtu;
 		dev->max_mtu = mtu;
-
-		/* TODO: size buffers correctly in this case. */
-		if (dev->mtu > ETH_DATA_LEN)
-			vi->big_packets = true;
 	}
 
+	virtnet_set_big_packets_fields(vi, mtu);
+
 	if (vi->any_header_sg)
 		dev->needed_headroom = vi->hdr_len;
 
--

2.31.1

Quoted 4 lines

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

Reviewed-by on v3 1/2
#3 lore raw

Re: [virtio-dev] [PATCH v3 1/2] virtio-net: introduce and use helper function for guest gso support checks

In reply to Gavin Li
Quoted 6 lines · Gavin Li

Probe routine is already several hundred lines. Use helper function for guest gso support check.

Quoted 29 lines

---
changelog:
v1->v2
- Add new patch

---
  drivers/net/virtio_net.c | 13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)
drivers/net/virtio_net.c +8 -0
index 9cce7dec7366..e1904877d461 100644
@@ -3682,6 +3682,14 @@ static int virtnet_validate(struct virtio_device *vdev)
  	return 0;
  }
 
+static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
+{
+	return (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
+		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
+		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
+		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO));
+}
+
  static int virtnet_probe(struct virtio_device *vdev)
  {
  	int i, err = -ENOMEM;
@@ -3777,10 +3785,7 @@ static int virtnet_probe(struct virtio_device *vdev)
  	spin_lock_init(&vi->refill_lock);

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 3791

Quoted 10 lines
drivers/net/virtio_net.c +1 -4
 
  	/* If we can receive ANY GSO packets, we must allocate large ones. */
-	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
+	if (virtnet_check_guest_gso(vi))
  		vi->big_packets = true;
 
  	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

Reviewed-by on v3 2/2
#6 lore raw

Re: [virtio-dev] [PATCH v3 2/2] virtio-net: use mtu size as buffer length for big packets

In reply to Gavin Li
Quoted 53 lines · Gavin Li

Currently add_recvbuf_big() allocates MAX_SKB_FRAGS segments for big packets even when GUEST_* offloads are not present on the device. However, if guest GSO is not supported, it would be sufficient to allocate segments to cover just up the MTU size and no further. Allocating the maximum amount of segments results in a large waste of buffer space in the queue, which limits the number of packets that can be buffered and can result in reduced performance.

Therefore, if guest GSO is not supported, use the MTU to calculate the optimal amount of segments required.

When guest offload is enabled at runtime, RQ already has packets of bytes less than 64K. So when packet of 64KB arrives, all the packets of such size will be dropped. and RQ is now not usable.

So this means that during set_guest_offloads() phase, RQs have to be destroyed and recreated, which requires almost driver reload.

If VIRTIO_NET_F_CTRL_GUEST_OFFLOADS has been negotiated, then it should always treat them as GSO enabled.

Below is the iperf TCP test results over a Mellanox NIC, using vDPA for 1 VQ, queue size 1024, before and after the change, with the iperf server running over the virtio-net interface.

MTU(Bytes)/Bandwidth (Gbit/s)
Before After

   1500        22.5     22.4
   9000        12.8     25.9

---
changelog:
v2->v3
- Addressed comments from Si-Wei
- Simplify the condition check to enable the optimization v1->v2
- Addressed comments from Jason, Michael, Si-Wei.
- Remove the flag of guest GSO support, set sg_num for big packets and use it directly
- Recalculate sg_num for big packets in virtnet_set_guest_offloads
- Replace the round up algorithm with DIV_ROUND_UP

---
  drivers/net/virtio_net.c | 37 ++++++++++++++++++++++++-------------
  1 file changed, 24 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +0 -0
index e1904877d461..d2721e71af18 100644

drivers/net/virtio_net.c struct virtnet_info { · line 229

Quoted 5 lines
drivers/net/virtio_net.c +2 -0
@@ -225,6 +225,9 @@ struct virtnet_info {
  	/* I like... big packets and I cannot lie! */
  	bool big_packets;
 
+	/* number of sg entries allocated for big packets */
+	unsigned int big_packets_sg_num;

Sorry for nit picking, but in my opinion big_packets_num_skbfrags might be a better name than big_packets_sg_num, where the comment could be written as "number of skb fragments for big packets" rather than "number of sg entries allocated for big packets".

Quoted 72 lines
drivers/net/virtio_net.c +20 -10
+
  	/* Host will merge rx buffers for big packets (shake it! shake it!) */
  	bool mergeable_rx_bufs;
 
@@ -1331,10 +1334,10 @@ static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
  	char *p;
  	int i, err, offset;
 
-	sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
+	sg_init_table(rq->sg, vi->big_packets_sg_num + 2);
 
-	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
-	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
+	/* page in rq->sg[vi->big_packets_sg_num + 1] is list tail */
+	for (i = vi->big_packets_sg_num + 1; i > 1; --i) {
  		first = get_a_page(rq, gfp);
  		if (!first) {
  			if (list)
@@ -1365,7 +1368,7 @@ static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
 
  	/* chain first in list head */
  	first->private = (unsigned long)list;
-	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
+	err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_sg_num + 2,
  				  first, gfp);
  	if (err < 0)
  		give_pages(rq, first);
@@ -3690,13 +3693,27 @@ static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
  		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO));
  }
 
+static void virtnet_set_big_packets_fields(struct virtnet_info *vi, const int mtu)
+{
+	bool guest_gso = virtnet_check_guest_gso(vi);
+
+	/* If device can receive ANY guest GSO packets, regardless of mtu,
+	 * allocate packets of maximum size, otherwise limit it to only
+	 * mtu size worth only.
+	 */
+	if (mtu > ETH_DATA_LEN || guest_gso) {
+		vi->big_packets = true;
+		vi->big_packets_sg_num = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
+	}
+}
+
  static int virtnet_probe(struct virtio_device *vdev)
  {
  	int i, err = -ENOMEM;
  	struct net_device *dev;
  	struct virtnet_info *vi;
  	u16 max_queue_pairs;
-	int mtu;
+	int mtu = 0;
 
  	/* Find if host supports multiqueue/rss virtio_net device */
  	max_queue_pairs = 1;
@@ -3784,10 +3801,6 @@ static int virtnet_probe(struct virtio_device *vdev)
  	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  	spin_lock_init(&vi->refill_lock);
 
-	/* If we can receive ANY GSO packets, we must allocate large ones. */
-	if (virtnet_check_guest_gso(vi))
-		vi->big_packets = true;
-
  	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  		vi->mergeable_rx_bufs = true;
 
@@ -3853,12 +3866,10 @@ static int virtnet_probe(struct virtio_device *vdev)
 
  		dev->mtu = mtu;
  		dev->max_mtu = mtu;
-

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 3875

Quoted 10 lines
drivers/net/virtio_net.c +2 -3
-		/* TODO: size buffers correctly in this case. */
-		if (dev->mtu > ETH_DATA_LEN)
-			vi->big_packets = true;
  	}
 
+	virtnet_set_big_packets_fields(vi, mtu);
+
  	if (vi->any_header_sg)
  		dev->needed_headroom = vi->hdr_len;
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

author
#7 lore raw

Re: [virtio-dev] [PATCH v3 2/2] virtio-net: use mtu size as buffer length for big packets

Quoted 60 lines · Si-Wei Liu

External email: Use caution opening links or attachments

Quoted 56 lines · Gavin Li

Currently add_recvbuf_big() allocates MAX_SKB_FRAGS segments for big packets even when GUEST_* offloads are not present on the device. However, if guest GSO is not supported, it would be sufficient to allocate segments to cover just up the MTU size and no further. Allocating the maximum amount of segments results in a large waste of buffer space in the queue, which limits the number of packets that can be buffered and can result in reduced performance.

Therefore, if guest GSO is not supported, use the MTU to calculate the optimal amount of segments required.

When guest offload is enabled at runtime, RQ already has packets of bytes less than 64K. So when packet of 64KB arrives, all the packets of such size will be dropped. and RQ is now not usable.

So this means that during set_guest_offloads() phase, RQs have to be destroyed and recreated, which requires almost driver reload.

If VIRTIO_NET_F_CTRL_GUEST_OFFLOADS has been negotiated, then it should always treat them as GSO enabled.

Below is the iperf TCP test results over a Mellanox NIC, using vDPA for 1 VQ, queue size 1024, before and after the change, with the iperf server running over the virtio-net interface.

MTU(Bytes)/Bandwidth (Gbit/s)
              Before   After
   1500        22.5     22.4
   9000        12.8     25.9

---
changelog:
v2->v3
- Addressed comments from Si-Wei
- Simplify the condition check to enable the optimization v1->v2
- Addressed comments from Jason, Michael, Si-Wei.
- Remove the flag of guest GSO support, set sg_num for big packets and use it directly
- Recalculate sg_num for big packets in virtnet_set_guest_offloads
- Replace the round up algorithm with DIV_ROUND_UP
---
  drivers/net/virtio_net.c | 37 ++++++++++++++++++++++++-------------

  1 file changed, 24 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +0 -0
index e1904877d461..d2721e71af18 100644
@@ -225,6 +225,9 @@ struct virtnet_info {
       /* I like... big packets and I cannot lie! */
       bool big_packets;
 
Quoted 6 lines

drivers/net/virtio_net.c struct virtnet_info { · line 229

Quoted 2 lines
drivers/net/virtio_net.c +2 -0
+     /* number of sg entries allocated for big packets */
+     unsigned int big_packets_sg_num;

Sorry for nit picking, but in my opinion big_packets_num_skbfrags might be a better name than big_packets_sg_num, where the comment could be written as "number of skb fragments for big packets" rather than "number of sg entries allocated for big packets".

ACK

Quoted 89 lines
Quoted 86 lines
drivers/net/virtio_net.c +4 -3
+
       /* Host will merge rx buffers for big packets (shake it! shake it!) */
       bool mergeable_rx_bufs;
 
@@ -1331,10 +1334,10 @@ static int add_recvbuf_big(struct 
 virtnet_info *vi, struct receive_queue *rq,
       char *p;
       int i, err, offset;
 
-     sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
+     sg_init_table(rq->sg, vi->big_packets_sg_num + 2);
 
-     /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
-     for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
+     /* page in rq->sg[vi->big_packets_sg_num + 1] is list tail */
+     for (i = vi->big_packets_sg_num + 1; i > 1; --i) {
               first = get_a_page(rq, gfp);
               if (!first) {

                      if (list)

drivers/net/virtio_net.c +1 -1
@@ -1365,7 +1368,7 @@ static int add_recvbuf_big(struct virtnet_info 
 *vi, struct receive_queue *rq,
 
       /* chain first in list head */
       first->private = (unsigned long)list;
-     err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
+     err = virtqueue_add_inbuf(rq->vq, rq->sg, 
 vi->big_packets_sg_num + 2,
                                 first, gfp);

      if (err < 0)
              give_pages(rq, first);

drivers/net/virtio_net.c +15 -1
@@ -3690,13 +3693,27 @@ static bool virtnet_check_guest_gso(const 
 struct virtnet_info *vi)
               virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO));
   }
 
+static void virtnet_set_big_packets_fields(struct virtnet_info *vi, 
 const int mtu)
+{
+     bool guest_gso = virtnet_check_guest_gso(vi);
+
+     /* If device can receive ANY guest GSO packets, regardless of mtu,
+      * allocate packets of maximum size, otherwise limit it to only
+      * mtu size worth only.
+      */
+     if (mtu > ETH_DATA_LEN || guest_gso) {
+             vi->big_packets = true;
+             vi->big_packets_sg_num = guest_gso ? MAX_SKB_FRAGS : 
 DIV_ROUND_UP(mtu, PAGE_SIZE);
+     }
+}
+
   static int virtnet_probe(struct virtio_device *vdev)
   {
       int i, err = -ENOMEM;
       struct net_device *dev;
       struct virtnet_info *vi;
       u16 max_queue_pairs;
-     int mtu;
+     int mtu = 0;

      /* Find if host supports multiqueue/rss virtio_net device */ max_queue_pairs = 1;

drivers/net/virtio_net.c +0 -1
@@ -3784,10 +3801,6 @@ static int virtnet_probe(struct virtio_device 
 *vdev)
       INIT_WORK(&vi->config_work, virtnet_config_changed_work);
       spin_lock_init(&vi->refill_lock);
 
-     /* If we can receive ANY GSO packets, we must allocate large 

ones. */
-     if (virtnet_check_guest_gso(vi))
-             vi->big_packets = true;
-
      if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) vi->mergeable_rx_bufs = true;

drivers/net/virtio_net.c +0 -4
@@ -3853,12 +3866,10 @@ static int virtnet_probe(struct virtio_device 
 *vdev)
 
               dev->mtu = mtu;
               dev->max_mtu = mtu;
-
-             /* TODO: size buffers correctly in this case. */
-             if (dev->mtu > ETH_DATA_LEN)
-                     vi->big_packets = true;
       }
 
Quoted 6 lines

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 3874

Quoted 5 lines
drivers/net/virtio_net.c +2 -0
+     virtnet_set_big_packets_fields(vi, mtu);
+
       if (vi->any_header_sg)
               dev->needed_headroom = vi->hdr_len;

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]