Skip to content

Commit d85b758

Browse files
committed
virtio_net: fix support for small rings
When ring size is small (<32 entries) making buffers smaller means a full ring might not be able to hold enough buffers to fit a single large packet. Make sure a ring full of buffers is large enough to allow at least one packet of max size. Fixes: 2613af0 ("virtio_net: migrate mergeable rx buffers to page frag allocators") Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent e377fcc commit d85b758

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

drivers/net/virtio_net.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/slab.h>
3030
#include <linux/cpu.h>
3131
#include <linux/average.h>
32+
#include <net/route.h>
3233

3334
static int napi_weight = NAPI_POLL_WEIGHT;
3435
module_param(napi_weight, int, 0444);
@@ -98,6 +99,9 @@ struct receive_queue {
9899
/* RX: fragments + linear part + virtio header */
99100
struct scatterlist sg[MAX_SKB_FRAGS + 2];
100101

102+
/* Min single buffer size for mergeable buffers case. */
103+
unsigned int min_buf_len;
104+
101105
/* Name of this receive queue: input.$index */
102106
char name[40];
103107
};
@@ -831,13 +835,14 @@ static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
831835
return err;
832836
}
833837

834-
static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
838+
static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
839+
struct ewma_pkt_len *avg_pkt_len)
835840
{
836841
const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
837842
unsigned int len;
838843

839844
len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
840-
GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
845+
rq->min_buf_len - hdr_len, PAGE_SIZE - hdr_len);
841846
return ALIGN(len, L1_CACHE_BYTES);
842847
}
843848

@@ -851,7 +856,7 @@ static int add_recvbuf_mergeable(struct virtnet_info *vi,
851856
int err;
852857
unsigned int len, hole;
853858

854-
len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
859+
len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len);
855860
if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp)))
856861
return -ENOMEM;
857862

@@ -2023,6 +2028,21 @@ static void virtnet_del_vqs(struct virtnet_info *vi)
20232028
virtnet_free_queues(vi);
20242029
}
20252030

2031+
/* How large should a single buffer be so a queue full of these can fit at
2032+
* least one full packet?
2033+
* Logic below assumes the mergeable buffer header is used.
2034+
*/
2035+
static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2036+
{
2037+
const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2038+
unsigned int rq_size = virtqueue_get_vring_size(vq);
2039+
unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2040+
unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2041+
unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2042+
2043+
return max(min_buf_len, hdr_len);
2044+
}
2045+
20262046
static int virtnet_find_vqs(struct virtnet_info *vi)
20272047
{
20282048
vq_callback_t **callbacks;
@@ -2088,6 +2108,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi)
20882108

20892109
for (i = 0; i < vi->max_queue_pairs; i++) {
20902110
vi->rq[i].vq = vqs[rxq2vq(i)];
2111+
vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
20912112
vi->sq[i].vq = vqs[txq2vq(i)];
20922113
}
20932114

@@ -2174,7 +2195,8 @@ static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
21742195

21752196
BUG_ON(queue_index >= vi->max_queue_pairs);
21762197
avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2177-
return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2198+
return sprintf(buf, "%u\n",
2199+
get_mergeable_buf_len(&vi->rq[queue_index], avg));
21782200
}
21792201

21802202
static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =

0 commit comments

Comments
 (0)