Skip to content

Commit

Permalink
Merge branch 'virtio_net-fixes-and-improvements'
Browse files Browse the repository at this point in the history
Akihiko Odaki says:

====================
virtio_net: Fixes and improvements

Jason Wang recently proposed an improvement to struct
virtio_net_rss_config:
https://lore.kernel.org/CACGkMEud0Ki8p=z299Q7b4qEDONpYDzbVqhHxCNVk_vo-KdP9A@mail.gmail.com

This patch series implements it and also fixes a few minor bugs I found
when writing patches.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Tested-by: Lei Yang <leiyang@redhat.com>

v1: https://lore.kernel.org/20250318-virtio-v1-0-344caf336ddd@daynix.com
====================

Link: https://patch.msgid.link/20250321-virtio-v2-0-33afb8f4640b@daynix.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Jakub Kicinski committed Mar 25, 2025
2 parents b2d1e4c + 4944be2 commit 5106876
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 76 deletions.
119 changes: 43 additions & 76 deletions drivers/net/virtio_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,24 +360,7 @@ struct receive_queue {
struct xdp_buff **xsk_buffs;
};

/* This structure can contain rss message with maximum settings for indirection table and keysize
* Note, that default structure that describes RSS configuration virtio_net_rss_config
* contains same info but can't handle table values.
* In any case, structure would be passed to virtio hw through sg_buf split by parts
* because table sizes may be differ according to the device configuration.
*/
#define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
struct virtio_net_ctrl_rss {
u32 hash_types;
u16 indirection_table_mask;
u16 unclassified_queue;
u16 hash_cfg_reserved; /* for HASH_CONFIG (see virtio_net_hash_config for details) */
u16 max_tx_vq;
u8 hash_key_length;
u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

u16 *indirection_table;
};

/* Control VQ buffers: protected by the rtnl lock */
struct control_buf {
Expand Down Expand Up @@ -421,7 +404,9 @@ struct virtnet_info {
u16 rss_indir_table_size;
u32 rss_hash_types_supported;
u32 rss_hash_types_saved;
struct virtio_net_ctrl_rss rss;
struct virtio_net_rss_config_hdr *rss_hdr;
struct virtio_net_rss_config_trailer rss_trailer;
u8 rss_hash_key_data[VIRTIO_NET_RSS_MAX_KEY_SIZE];

/* Has control virtqueue */
bool has_cvq;
Expand Down Expand Up @@ -523,23 +508,16 @@ enum virtnet_xmit_type {
VIRTNET_XMIT_TYPE_XSK,
};

static int rss_indirection_table_alloc(struct virtio_net_ctrl_rss *rss, u16 indir_table_size)
static size_t virtnet_rss_hdr_size(const struct virtnet_info *vi)
{
if (!indir_table_size) {
rss->indirection_table = NULL;
return 0;
}
u16 indir_table_size = vi->has_rss ? vi->rss_indir_table_size : 1;

rss->indirection_table = kmalloc_array(indir_table_size, sizeof(u16), GFP_KERNEL);
if (!rss->indirection_table)
return -ENOMEM;

return 0;
return struct_size(vi->rss_hdr, indirection_table, indir_table_size);
}

static void rss_indirection_table_free(struct virtio_net_ctrl_rss *rss)
static size_t virtnet_rss_trailer_size(const struct virtnet_info *vi)
{
kfree(rss->indirection_table);
return struct_size(&vi->rss_trailer, hash_key_data, vi->rss_key_size);
}

/* We use the last two bits of the pointer to distinguish the xmit type. */
Expand Down Expand Up @@ -3638,15 +3616,16 @@ static void virtnet_rss_update_by_qpairs(struct virtnet_info *vi, u16 queue_pair

for (; i < vi->rss_indir_table_size; ++i) {
indir_val = ethtool_rxfh_indir_default(i, queue_pairs);
vi->rss.indirection_table[i] = indir_val;
vi->rss_hdr->indirection_table[i] = cpu_to_le16(indir_val);
}
vi->rss.max_tx_vq = queue_pairs;
vi->rss_trailer.max_tx_vq = cpu_to_le16(queue_pairs);
}

static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
{
struct virtio_net_ctrl_mq *mq __free(kfree) = NULL;
struct virtio_net_ctrl_rss old_rss;
struct virtio_net_rss_config_hdr *old_rss_hdr;
struct virtio_net_rss_config_trailer old_rss_trailer;
struct net_device *dev = vi->dev;
struct scatterlist sg;

Expand All @@ -3661,24 +3640,28 @@ static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
* update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly.
*/
if (vi->has_rss && !netif_is_rxfh_configured(dev)) {
memcpy(&old_rss, &vi->rss, sizeof(old_rss));
if (rss_indirection_table_alloc(&vi->rss, vi->rss_indir_table_size)) {
vi->rss.indirection_table = old_rss.indirection_table;
old_rss_hdr = vi->rss_hdr;
old_rss_trailer = vi->rss_trailer;
vi->rss_hdr = devm_kzalloc(&dev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL);
if (!vi->rss_hdr) {
vi->rss_hdr = old_rss_hdr;
return -ENOMEM;
}

*vi->rss_hdr = *old_rss_hdr;
virtnet_rss_update_by_qpairs(vi, queue_pairs);

if (!virtnet_commit_rss_command(vi)) {
/* restore ctrl_rss if commit_rss_command failed */
rss_indirection_table_free(&vi->rss);
memcpy(&vi->rss, &old_rss, sizeof(old_rss));
devm_kfree(&dev->dev, vi->rss_hdr);
vi->rss_hdr = old_rss_hdr;
vi->rss_trailer = old_rss_trailer;

dev_warn(&dev->dev, "Fail to set num of queue pairs to %d, because committing RSS failed\n",
queue_pairs);
return -EINVAL;
}
rss_indirection_table_free(&old_rss);
devm_kfree(&dev->dev, old_rss_hdr);
goto succ;
}

Expand Down Expand Up @@ -4121,28 +4104,12 @@ static int virtnet_set_ringparam(struct net_device *dev,
static bool virtnet_commit_rss_command(struct virtnet_info *vi)
{
struct net_device *dev = vi->dev;
struct scatterlist sgs[4];
unsigned int sg_buf_size;
struct scatterlist sgs[2];

/* prepare sgs */
sg_init_table(sgs, 4);

sg_buf_size = offsetof(struct virtio_net_ctrl_rss, hash_cfg_reserved);
sg_set_buf(&sgs[0], &vi->rss, sg_buf_size);

if (vi->has_rss) {
sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
} else {
sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved, sizeof(uint16_t));
}

sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
- offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
sg_set_buf(&sgs[2], &vi->rss.max_tx_vq, sg_buf_size);

sg_buf_size = vi->rss_key_size;
sg_set_buf(&sgs[3], vi->rss.key, sg_buf_size);
sg_init_table(sgs, 2);
sg_set_buf(&sgs[0], vi->rss_hdr, virtnet_rss_hdr_size(vi));
sg_set_buf(&sgs[1], &vi->rss_trailer, virtnet_rss_trailer_size(vi));

if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
Expand All @@ -4159,17 +4126,17 @@ static bool virtnet_commit_rss_command(struct virtnet_info *vi)

static void virtnet_init_default_rss(struct virtnet_info *vi)
{
vi->rss.hash_types = vi->rss_hash_types_supported;
vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_supported);
vi->rss_hash_types_saved = vi->rss_hash_types_supported;
vi->rss.indirection_table_mask = vi->rss_indir_table_size
? vi->rss_indir_table_size - 1 : 0;
vi->rss.unclassified_queue = 0;
vi->rss_hdr->indirection_table_mask = vi->rss_indir_table_size
? cpu_to_le16(vi->rss_indir_table_size - 1) : 0;
vi->rss_hdr->unclassified_queue = 0;

virtnet_rss_update_by_qpairs(vi, vi->curr_queue_pairs);

vi->rss.hash_key_length = vi->rss_key_size;
vi->rss_trailer.hash_key_length = vi->rss_key_size;

netdev_rss_key_fill(vi->rss.key, vi->rss_key_size);
netdev_rss_key_fill(vi->rss_hash_key_data, vi->rss_key_size);
}

static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
Expand Down Expand Up @@ -4280,7 +4247,7 @@ static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *

if (new_hashtypes != vi->rss_hash_types_saved) {
vi->rss_hash_types_saved = new_hashtypes;
vi->rss.hash_types = vi->rss_hash_types_saved;
vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved);
if (vi->dev->features & NETIF_F_RXHASH)
return virtnet_commit_rss_command(vi);
}
Expand Down Expand Up @@ -5460,11 +5427,11 @@ static int virtnet_get_rxfh(struct net_device *dev,

if (rxfh->indir) {
for (i = 0; i < vi->rss_indir_table_size; ++i)
rxfh->indir[i] = vi->rss.indirection_table[i];
rxfh->indir[i] = le16_to_cpu(vi->rss_hdr->indirection_table[i]);
}

if (rxfh->key)
memcpy(rxfh->key, vi->rss.key, vi->rss_key_size);
memcpy(rxfh->key, vi->rss_hash_key_data, vi->rss_key_size);

rxfh->hfunc = ETH_RSS_HASH_TOP;

Expand All @@ -5488,7 +5455,7 @@ static int virtnet_set_rxfh(struct net_device *dev,
return -EOPNOTSUPP;

for (i = 0; i < vi->rss_indir_table_size; ++i)
vi->rss.indirection_table[i] = rxfh->indir[i];
vi->rss_hdr->indirection_table[i] = cpu_to_le16(rxfh->indir[i]);
update = true;
}

Expand All @@ -5500,7 +5467,7 @@ static int virtnet_set_rxfh(struct net_device *dev,
if (!vi->has_rss && !vi->has_rss_hash_report)
return -EOPNOTSUPP;

memcpy(vi->rss.key, rxfh->key, vi->rss_key_size);
memcpy(vi->rss_hash_key_data, rxfh->key, vi->rss_key_size);
update = true;
}

Expand Down Expand Up @@ -6109,9 +6076,9 @@ static int virtnet_set_features(struct net_device *dev,

if ((dev->features ^ features) & NETIF_F_RXHASH) {
if (features & NETIF_F_RXHASH)
vi->rss.hash_types = vi->rss_hash_types_saved;
vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved);
else
vi->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
vi->rss_hdr->hash_types = cpu_to_le32(VIRTIO_NET_HASH_REPORT_NONE);

if (!virtnet_commit_rss_command(vi))
return -EINVAL;
Expand Down Expand Up @@ -6801,9 +6768,11 @@ static int virtnet_probe(struct virtio_device *vdev)
virtio_cread16(vdev, offsetof(struct virtio_net_config,
rss_max_indirection_table_length));
}
err = rss_indirection_table_alloc(&vi->rss, vi->rss_indir_table_size);
if (err)
vi->rss_hdr = devm_kzalloc(&vdev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL);
if (!vi->rss_hdr) {
err = -ENOMEM;
goto free;
}

if (vi->has_rss || vi->has_rss_hash_report) {
vi->rss_key_size =
Expand Down Expand Up @@ -7082,8 +7051,6 @@ static void virtnet_remove(struct virtio_device *vdev)

remove_vq_common(vi);

rss_indirection_table_free(&vi->rss);

free_netdev(vi->dev);
}

Expand Down
13 changes: 13 additions & 0 deletions include/uapi/linux/virtio_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,19 @@ struct virtio_net_rss_config {
__u8 hash_key_data[/* hash_key_length */];
};

struct virtio_net_rss_config_hdr {
__le32 hash_types;
__le16 indirection_table_mask;
__le16 unclassified_queue;
__le16 indirection_table[/* 1 + indirection_table_mask */];
};

struct virtio_net_rss_config_trailer {
__le16 max_tx_vq;
__u8 hash_key_length;
__u8 hash_key_data[/* hash_key_length */];
};

#define VIRTIO_NET_CTRL_MQ_RSS_CONFIG 1

/*
Expand Down

0 comments on commit 5106876

Please sign in to comment.