diff mbox series

[net-next,V2,3/4] virtio_net: support per queue interrupt coalesce command

Message ID 20230717143037.21858-4-gavinl@nvidia.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series virtio_net: add per queue interrupt coalescing support | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1346 this patch: 1346
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 1365 this patch: 1365
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1369 this patch: 1369
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 82 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns WARNING: line length of 88 exceeds 80 columns WARNING: line length of 90 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns WARNING: line length of 95 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Gavin Li July 17, 2023, 2:30 p.m. UTC
Add interrupt_coalesce config in send_queue and receive_queue to cache user
config.

Send per virtqueue interrupt moderation config to underline device in order
to have more efficient interrupt moderation and cpu utilization of guest
VM.

Signed-off-by: Gavin Li <gavinl@nvidia.com>
Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
---
 drivers/net/virtio_net.c        | 123 ++++++++++++++++++++++++++++----
 include/uapi/linux/virtio_net.h |  14 ++++
 2 files changed, 125 insertions(+), 12 deletions(-)

Comments

Gavin Li July 17, 2023, 3:22 p.m. UTC | #1
On 7/17/2023 10:30 PM, Gavin Li wrote:
> Add interrupt_coalesce config in send_queue and receive_queue to cache user
> config.
> 
> Send per virtqueue interrupt moderation config to underline device in order
> to have more efficient interrupt moderation and cpu utilization of guest
> VM.
> 
> Signed-off-by: Gavin Li <gavinl@nvidia.com>
> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
> Reviewed-by: Jiri Pirko <jiri@nvidia.com>
> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>   drivers/net/virtio_net.c        | 123 ++++++++++++++++++++++++++++----
>   include/uapi/linux/virtio_net.h |  14 ++++
>   2 files changed, 125 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 802ed21453f5..1566c7de9436 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -144,6 +144,8 @@ struct send_queue {
>   
>   	struct virtnet_sq_stats stats;
>   
> +	struct virtnet_interrupt_coalesce intr_coal;
> +
>   	struct napi_struct napi;
>   
>   	/* Record whether sq is in reset state. */
> @@ -161,6 +163,8 @@ struct receive_queue {
>   
>   	struct virtnet_rq_stats stats;
>   
> +	struct virtnet_interrupt_coalesce intr_coal;
> +
>   	/* Chain pages by the private ptr. */
>   	struct page *pages;
>   
> @@ -3078,6 +3082,59 @@ static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
>   	return 0;
>   }
>   
> +static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
> +					 u16 vqn, u32 max_usecs, u32 max_packets)
> +{
> +	struct virtio_net_ctrl_coal_vq *coal_vq;
> +	struct scatterlist sgs;
> +
> +	coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);
> +	if (!coal_vq)
> +		return -ENOMEM;
> +	coal_vq->vqn = cpu_to_le16(vqn);
> +	coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
> +	coal_vq->coal.max_packets = cpu_to_le32(max_packets);
> +	sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
> +
> +	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
> +				  VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
> +				  &sgs))
> +		return -EINVAL;
Sorry, introduced mem leak here, ie. coal_vq. May I re-send the patch 
series or fix it in V3?
> +
> +	return 0;
> +}
> +
> +static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
> +					  struct ethtool_coalesce *ec,
> +					  u16 queue)
> +{
> +	int err;
> +
> +	if (ec->rx_coalesce_usecs || ec->rx_max_coalesced_frames) {
> +		err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
> +						    ec->rx_coalesce_usecs,
> +						    ec->rx_max_coalesced_frames);
> +		if (err)
> +			return err;
> +		/* Save parameters */
> +		vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
> +		vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames;
> +	}
> +
> +	if (ec->tx_coalesce_usecs || ec->tx_max_coalesced_frames) {
> +		err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
> +						    ec->tx_coalesce_usecs,
> +						    ec->tx_max_coalesced_frames);
> +		if (err)
> +			return err;
> +		/* Save parameters */
> +		vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
> +		vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames;
> +	}
> +
> +	return 0;
> +}
> +
>   static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>   {
>   	/* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
> @@ -3094,23 +3151,39 @@ static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>   }
>   
>   static int virtnet_set_coalesce_one(struct net_device *dev,
> -				    struct ethtool_coalesce *ec)
> +				    struct ethtool_coalesce *ec,
> +				    bool per_queue,
> +				    u32 queue)
>   {
>   	struct virtnet_info *vi = netdev_priv(dev);
> -	int ret, i, napi_weight;
> +	int queue_count = per_queue ? 1 : vi->max_queue_pairs;
> +	int queue_number = per_queue ? queue : 0;
>   	bool update_napi = false;
> +	int ret, i, napi_weight;
> +
> +	if (queue >= vi->max_queue_pairs)
> +		return -EINVAL;
>   
>   	/* Can't change NAPI weight if the link is up */
>   	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
> -	if (napi_weight ^ vi->sq[0].napi.weight) {
> -		if (dev->flags & IFF_UP)
> -			return -EBUSY;
> -		else
> +	for (i = queue_number; i < queue_count; i++) {
> +		if (napi_weight ^ vi->sq[i].napi.weight) {
> +			if (dev->flags & IFF_UP)
> +				return -EBUSY;
> +
>   			update_napi = true;
> +			/* All queues that belong to [queue_number, queue_count] will be
> +			 * updated for the sake of simplicity, which might not be necessary
> +			 */
> +			queue_number = i;
> +			break;
> +		}
>   	}
>   
> -	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
> +	if (!per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
>   		ret = virtnet_send_notf_coal_cmds(vi, ec);
> +	else if (per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
> +		ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
>   	else
>   		ret = virtnet_coal_params_supported(ec);
>   
> @@ -3118,7 +3191,7 @@ static int virtnet_set_coalesce_one(struct net_device *dev,
>   		return ret;
>   
>   	if (update_napi) {
> -		for (i = 0; i < vi->max_queue_pairs; i++)
> +		for (i = queue_number; i < queue_count; i++)
>   			vi->sq[i].napi.weight = napi_weight;
>   	}
>   
> @@ -3130,19 +3203,29 @@ static int virtnet_set_coalesce(struct net_device *dev,
>   				struct kernel_ethtool_coalesce *kernel_coal,
>   				struct netlink_ext_ack *extack)
>   {
> -	return virtnet_set_coalesce_one(dev, ec);
> +	return virtnet_set_coalesce_one(dev, ec, false, 0);
>   }
>   
>   static int virtnet_get_coalesce_one(struct net_device *dev,
> -				    struct ethtool_coalesce *ec)
> +				    struct ethtool_coalesce *ec,
> +				    bool per_queue,
> +				    u32 queue)
>   {
>   	struct virtnet_info *vi = netdev_priv(dev);
>   
> -	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
> +	if (queue >= vi->max_queue_pairs)
> +		return -EINVAL;
> +
> +	if (!per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
>   		ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
>   		ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
>   		ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
>   		ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
> +	} else if (per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
> +		ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
> +		ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
> +		ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
> +		ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
>   	} else {
>   		ec->rx_max_coalesced_frames = 1;
>   
> @@ -3158,7 +3241,21 @@ static int virtnet_get_coalesce(struct net_device *dev,
>   				struct kernel_ethtool_coalesce *kernel_coal,
>   				struct netlink_ext_ack *extack)
>   {
> -	return virtnet_get_coalesce_one(dev, ec);
> +	return virtnet_get_coalesce_one(dev, ec, false, 0);
> +}
> +
> +static int virtnet_set_per_queue_coalesce(struct net_device *dev,
> +					  u32 queue,
> +					  struct ethtool_coalesce *ec)
> +{
> +	return virtnet_set_coalesce_one(dev, ec, true, queue);
> +}
> +
> +static int virtnet_get_per_queue_coalesce(struct net_device *dev,
> +					  u32 queue,
> +					  struct ethtool_coalesce *ec)
> +{
> +	return virtnet_get_coalesce_one(dev, ec, true, queue);
>   }
>   
>   static void virtnet_init_settings(struct net_device *dev)
> @@ -3291,6 +3388,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
>   	.set_link_ksettings = virtnet_set_link_ksettings,
>   	.set_coalesce = virtnet_set_coalesce,
>   	.get_coalesce = virtnet_get_coalesce,
> +	.set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
> +	.get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
>   	.get_rxfh_key_size = virtnet_get_rxfh_key_size,
>   	.get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
>   	.get_rxfh = virtnet_get_rxfh,
> diff --git a/include/uapi/linux/virtio_net.h b/include/uapi/linux/virtio_net.h
> index 12c1c9699935..cc65ef0f3c3e 100644
> --- a/include/uapi/linux/virtio_net.h
> +++ b/include/uapi/linux/virtio_net.h
> @@ -56,6 +56,7 @@
>   #define VIRTIO_NET_F_MQ	22	/* Device supports Receive Flow
>   					 * Steering */
>   #define VIRTIO_NET_F_CTRL_MAC_ADDR 23	/* Set MAC address */
> +#define VIRTIO_NET_F_VQ_NOTF_COAL 52	/* Device supports virtqueue notification coalescing */
>   #define VIRTIO_NET_F_NOTF_COAL	53	/* Device supports notifications coalescing */
>   #define VIRTIO_NET_F_GUEST_USO4	54	/* Guest can handle USOv4 in. */
>   #define VIRTIO_NET_F_GUEST_USO6	55	/* Guest can handle USOv6 in. */
> @@ -391,5 +392,18 @@ struct virtio_net_ctrl_coal_rx {
>   };
>   
>   #define VIRTIO_NET_CTRL_NOTF_COAL_RX_SET		1
> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET		2
> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_GET		3
> +
> +struct virtio_net_ctrl_coal {
> +	__le32 max_packets;
> +	__le32 max_usecs;
> +};
> +
> +struct  virtio_net_ctrl_coal_vq {
> +	__le16 vqn;
> +	__le16 reserved;
> +	struct virtio_net_ctrl_coal coal;
> +};
>   
>   #endif /* _UAPI_LINUX_VIRTIO_NET_H */
Heng Qi July 18, 2023, 3:29 a.m. UTC | #2
在 2023/7/17 下午10:30, Gavin Li 写道:
> Add interrupt_coalesce config in send_queue and receive_queue to cache user
> config.
>
> Send per virtqueue interrupt moderation config to underline device in order
> to have more efficient interrupt moderation and cpu utilization of guest
> VM.
>
> Signed-off-by: Gavin Li <gavinl@nvidia.com>
> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
> Reviewed-by: Jiri Pirko <jiri@nvidia.com>
> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>   drivers/net/virtio_net.c        | 123 ++++++++++++++++++++++++++++----
>   include/uapi/linux/virtio_net.h |  14 ++++
>   2 files changed, 125 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 802ed21453f5..1566c7de9436 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -144,6 +144,8 @@ struct send_queue {
>   
>   	struct virtnet_sq_stats stats;
>   
> +	struct virtnet_interrupt_coalesce intr_coal;
> +
>   	struct napi_struct napi;
>   
>   	/* Record whether sq is in reset state. */
> @@ -161,6 +163,8 @@ struct receive_queue {
>   
>   	struct virtnet_rq_stats stats;
>   
> +	struct virtnet_interrupt_coalesce intr_coal;
> +
>   	/* Chain pages by the private ptr. */
>   	struct page *pages;
>   
> @@ -3078,6 +3082,59 @@ static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
>   	return 0;
>   }
>   
> +static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
> +					 u16 vqn, u32 max_usecs, u32 max_packets)
> +{
> +	struct virtio_net_ctrl_coal_vq *coal_vq;
> +	struct scatterlist sgs;
> +
> +	coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);

I think this should go in the structure control_buf, which serves two 
purposes, and that's on the heap in init_vqs():
1. We can have the same form as other control types, such as 
virtio_net_ctrl_coal_{tx, rx};
2. Avoid using heap memory here to cause the following memory leaks

> +	if (!coal_vq)
> +		return -ENOMEM;
> +	coal_vq->vqn = cpu_to_le16(vqn);
> +	coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
> +	coal_vq->coal.max_packets = cpu_to_le32(max_packets);
> +	sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
> +
> +	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
> +				  VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
> +				  &sgs))
> +		return -EINVAL;

If this fails, we should free coal_vq, so pls move coal_vq into control_buf.

Thanks.

> +
> +	return 0;
> +}
> +
> +static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
> +					  struct ethtool_coalesce *ec,
> +					  u16 queue)
> +{
> +	int err;
> +
> +	if (ec->rx_coalesce_usecs || ec->rx_max_coalesced_frames) {
> +		err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
> +						    ec->rx_coalesce_usecs,
> +						    ec->rx_max_coalesced_frames);
> +		if (err)
> +			return err;
> +		/* Save parameters */
> +		vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
> +		vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames;
> +	}
> +
> +	if (ec->tx_coalesce_usecs || ec->tx_max_coalesced_frames) {
> +		err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
> +						    ec->tx_coalesce_usecs,
> +						    ec->tx_max_coalesced_frames);
> +		if (err)
> +			return err;
> +		/* Save parameters */
> +		vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
> +		vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames;
> +	}
> +
> +	return 0;
> +}
> +
>   static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>   {
>   	/* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
> @@ -3094,23 +3151,39 @@ static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>   }
>   
>   static int virtnet_set_coalesce_one(struct net_device *dev,
> -				    struct ethtool_coalesce *ec)
> +				    struct ethtool_coalesce *ec,
> +				    bool per_queue,
> +				    u32 queue)
>   {
>   	struct virtnet_info *vi = netdev_priv(dev);
> -	int ret, i, napi_weight;
> +	int queue_count = per_queue ? 1 : vi->max_queue_pairs;
> +	int queue_number = per_queue ? queue : 0;
>   	bool update_napi = false;
> +	int ret, i, napi_weight;
> +
> +	if (queue >= vi->max_queue_pairs)
> +		return -EINVAL;
>   
>   	/* Can't change NAPI weight if the link is up */
>   	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
> -	if (napi_weight ^ vi->sq[0].napi.weight) {
> -		if (dev->flags & IFF_UP)
> -			return -EBUSY;
> -		else
> +	for (i = queue_number; i < queue_count; i++) {
> +		if (napi_weight ^ vi->sq[i].napi.weight) {
> +			if (dev->flags & IFF_UP)
> +				return -EBUSY;
> +
>   			update_napi = true;
> +			/* All queues that belong to [queue_number, queue_count] will be
> +			 * updated for the sake of simplicity, which might not be necessary
> +			 */
> +			queue_number = i;
> +			break;
> +		}
>   	}
>   
> -	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
> +	if (!per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
>   		ret = virtnet_send_notf_coal_cmds(vi, ec);
> +	else if (per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
> +		ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
>   	else
>   		ret = virtnet_coal_params_supported(ec);
>   
> @@ -3118,7 +3191,7 @@ static int virtnet_set_coalesce_one(struct net_device *dev,
>   		return ret;
>   
>   	if (update_napi) {
> -		for (i = 0; i < vi->max_queue_pairs; i++)
> +		for (i = queue_number; i < queue_count; i++)
>   			vi->sq[i].napi.weight = napi_weight;
>   	}
>   
> @@ -3130,19 +3203,29 @@ static int virtnet_set_coalesce(struct net_device *dev,
>   				struct kernel_ethtool_coalesce *kernel_coal,
>   				struct netlink_ext_ack *extack)
>   {
> -	return virtnet_set_coalesce_one(dev, ec);
> +	return virtnet_set_coalesce_one(dev, ec, false, 0);
>   }
>   
>   static int virtnet_get_coalesce_one(struct net_device *dev,
> -				    struct ethtool_coalesce *ec)
> +				    struct ethtool_coalesce *ec,
> +				    bool per_queue,
> +				    u32 queue)
>   {
>   	struct virtnet_info *vi = netdev_priv(dev);
>   
> -	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
> +	if (queue >= vi->max_queue_pairs)
> +		return -EINVAL;
> +
> +	if (!per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
>   		ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
>   		ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
>   		ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
>   		ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
> +	} else if (per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
> +		ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
> +		ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
> +		ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
> +		ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
>   	} else {
>   		ec->rx_max_coalesced_frames = 1;
>   
> @@ -3158,7 +3241,21 @@ static int virtnet_get_coalesce(struct net_device *dev,
>   				struct kernel_ethtool_coalesce *kernel_coal,
>   				struct netlink_ext_ack *extack)
>   {
> -	return virtnet_get_coalesce_one(dev, ec);
> +	return virtnet_get_coalesce_one(dev, ec, false, 0);
> +}
> +
> +static int virtnet_set_per_queue_coalesce(struct net_device *dev,
> +					  u32 queue,
> +					  struct ethtool_coalesce *ec)
> +{
> +	return virtnet_set_coalesce_one(dev, ec, true, queue);
> +}
> +
> +static int virtnet_get_per_queue_coalesce(struct net_device *dev,
> +					  u32 queue,
> +					  struct ethtool_coalesce *ec)
> +{
> +	return virtnet_get_coalesce_one(dev, ec, true, queue);
>   }
>   
>   static void virtnet_init_settings(struct net_device *dev)
> @@ -3291,6 +3388,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
>   	.set_link_ksettings = virtnet_set_link_ksettings,
>   	.set_coalesce = virtnet_set_coalesce,
>   	.get_coalesce = virtnet_get_coalesce,
> +	.set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
> +	.get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
>   	.get_rxfh_key_size = virtnet_get_rxfh_key_size,
>   	.get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
>   	.get_rxfh = virtnet_get_rxfh,
> diff --git a/include/uapi/linux/virtio_net.h b/include/uapi/linux/virtio_net.h
> index 12c1c9699935..cc65ef0f3c3e 100644
> --- a/include/uapi/linux/virtio_net.h
> +++ b/include/uapi/linux/virtio_net.h
> @@ -56,6 +56,7 @@
>   #define VIRTIO_NET_F_MQ	22	/* Device supports Receive Flow
>   					 * Steering */
>   #define VIRTIO_NET_F_CTRL_MAC_ADDR 23	/* Set MAC address */
> +#define VIRTIO_NET_F_VQ_NOTF_COAL 52	/* Device supports virtqueue notification coalescing */
>   #define VIRTIO_NET_F_NOTF_COAL	53	/* Device supports notifications coalescing */
>   #define VIRTIO_NET_F_GUEST_USO4	54	/* Guest can handle USOv4 in. */
>   #define VIRTIO_NET_F_GUEST_USO6	55	/* Guest can handle USOv6 in. */
> @@ -391,5 +392,18 @@ struct virtio_net_ctrl_coal_rx {
>   };
>   
>   #define VIRTIO_NET_CTRL_NOTF_COAL_RX_SET		1
> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET		2
> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_GET		3
> +
> +struct virtio_net_ctrl_coal {
> +	__le32 max_packets;
> +	__le32 max_usecs;
> +};
> +
> +struct  virtio_net_ctrl_coal_vq {
> +	__le16 vqn;
> +	__le16 reserved;
> +	struct virtio_net_ctrl_coal coal;
> +};
>   
>   #endif /* _UAPI_LINUX_VIRTIO_NET_H */
Heng Qi July 18, 2023, 3:37 a.m. UTC | #3
在 2023/7/17 下午10:30, Gavin Li 写道:
> Add interrupt_coalesce config in send_queue and receive_queue to cache user
> config.
>
> Send per virtqueue interrupt moderation config to underline device in order
> to have more efficient interrupt moderation and cpu utilization of guest
> VM.
>
> Signed-off-by: Gavin Li <gavinl@nvidia.com>
> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
> Reviewed-by: Jiri Pirko <jiri@nvidia.com>
> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>   drivers/net/virtio_net.c        | 123 ++++++++++++++++++++++++++++----
>   include/uapi/linux/virtio_net.h |  14 ++++
>   2 files changed, 125 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 802ed21453f5..1566c7de9436 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -144,6 +144,8 @@ struct send_queue {
>   
>   	struct virtnet_sq_stats stats;
>   
> +	struct virtnet_interrupt_coalesce intr_coal;
> +
>   	struct napi_struct napi;
>   
>   	/* Record whether sq is in reset state. */
> @@ -161,6 +163,8 @@ struct receive_queue {
>   
>   	struct virtnet_rq_stats stats;
>   
> +	struct virtnet_interrupt_coalesce intr_coal;
> +
>   	/* Chain pages by the private ptr. */
>   	struct page *pages;
>   
> @@ -3078,6 +3082,59 @@ static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
>   	return 0;
>   }
>   
> +static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
> +					 u16 vqn, u32 max_usecs, u32 max_packets)
> +{
> +	struct virtio_net_ctrl_coal_vq *coal_vq;
> +	struct scatterlist sgs;
> +
> +	coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);
> +	if (!coal_vq)
> +		return -ENOMEM;
> +	coal_vq->vqn = cpu_to_le16(vqn);
> +	coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
> +	coal_vq->coal.max_packets = cpu_to_le32(max_packets);
> +	sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
> +
> +	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
> +				  VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
> +				  &sgs))
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +
> +static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
> +					  struct ethtool_coalesce *ec,
> +					  u16 queue)
> +{
> +	int err;
> +
> +	if (ec->rx_coalesce_usecs || ec->rx_max_coalesced_frames) {
> +		err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
> +						    ec->rx_coalesce_usecs,
> +						    ec->rx_max_coalesced_frames);
> +		if (err)
> +			return err;
> +		/* Save parameters */
> +		vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
> +		vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames;
> +	}
> +
> +	if (ec->tx_coalesce_usecs || ec->tx_max_coalesced_frames) {
> +		err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
> +						    ec->tx_coalesce_usecs,
> +						    ec->tx_max_coalesced_frames);
> +		if (err)
> +			return err;
> +		/* Save parameters */
> +		vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
> +		vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames;
> +	}
> +
> +	return 0;
> +}
> +
>   static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>   {
>   	/* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
> @@ -3094,23 +3151,39 @@ static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>   }
>   
>   static int virtnet_set_coalesce_one(struct net_device *dev,
> -				    struct ethtool_coalesce *ec)
> +				    struct ethtool_coalesce *ec,
> +				    bool per_queue,
> +				    u32 queue)
>   {
>   	struct virtnet_info *vi = netdev_priv(dev);
> -	int ret, i, napi_weight;
> +	int queue_count = per_queue ? 1 : vi->max_queue_pairs;
> +	int queue_number = per_queue ? queue : 0;
>   	bool update_napi = false;
> +	int ret, i, napi_weight;
> +
> +	if (queue >= vi->max_queue_pairs)
> +		return -EINVAL;
>   
>   	/* Can't change NAPI weight if the link is up */
>   	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
> -	if (napi_weight ^ vi->sq[0].napi.weight) {
> -		if (dev->flags & IFF_UP)
> -			return -EBUSY;
> -		else
> +	for (i = queue_number; i < queue_count; i++) {
> +		if (napi_weight ^ vi->sq[i].napi.weight) {
> +			if (dev->flags & IFF_UP)
> +				return -EBUSY;
> +
>   			update_napi = true;
> +			/* All queues that belong to [queue_number, queue_count] will be
> +			 * updated for the sake of simplicity, which might not be necessary
> +			 */
> +			queue_number = i;
> +			break;
> +		}
>   	}
>   
> -	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
> +	if (!per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
>   		ret = virtnet_send_notf_coal_cmds(vi, ec);
> +	else if (per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
> +		ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
>   	else
>   		ret = virtnet_coal_params_supported(ec);
>   
> @@ -3118,7 +3191,7 @@ static int virtnet_set_coalesce_one(struct net_device *dev,
>   		return ret;
>   
>   	if (update_napi) {
> -		for (i = 0; i < vi->max_queue_pairs; i++)
> +		for (i = queue_number; i < queue_count; i++)
>   			vi->sq[i].napi.weight = napi_weight;
>   	}
>   
> @@ -3130,19 +3203,29 @@ static int virtnet_set_coalesce(struct net_device *dev,
>   				struct kernel_ethtool_coalesce *kernel_coal,
>   				struct netlink_ext_ack *extack)
>   {
> -	return virtnet_set_coalesce_one(dev, ec);
> +	return virtnet_set_coalesce_one(dev, ec, false, 0);
>   }
>   
>   static int virtnet_get_coalesce_one(struct net_device *dev,
> -				    struct ethtool_coalesce *ec)
> +				    struct ethtool_coalesce *ec,
> +				    bool per_queue,
> +				    u32 queue)
>   {
>   	struct virtnet_info *vi = netdev_priv(dev);
>   
> -	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
> +	if (queue >= vi->max_queue_pairs)
> +		return -EINVAL;
> +
> +	if (!per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
>   		ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
>   		ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
>   		ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
>   		ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
> +	} else if (per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
> +		ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
> +		ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
> +		ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
> +		ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
>   	} else {
>   		ec->rx_max_coalesced_frames = 1;
>   
> @@ -3158,7 +3241,21 @@ static int virtnet_get_coalesce(struct net_device *dev,
>   				struct kernel_ethtool_coalesce *kernel_coal,
>   				struct netlink_ext_ack *extack)
>   {
> -	return virtnet_get_coalesce_one(dev, ec);
> +	return virtnet_get_coalesce_one(dev, ec, false, 0);
> +}
> +
> +static int virtnet_set_per_queue_coalesce(struct net_device *dev,
> +					  u32 queue,
> +					  struct ethtool_coalesce *ec)

When \field{max_virtqueue_pairs} is the maximum value, and the user does 
not carry the queue_mask for 'ethtool -Q',
we will send same command for all vqs, and the device will receive a 
large number of the same VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET commands at 
this time.
Do we want to alleviate this situation?

Thanks.

> +{
> +	return virtnet_set_coalesce_one(dev, ec, true, queue);
> +}
> +
> +static int virtnet_get_per_queue_coalesce(struct net_device *dev,
> +					  u32 queue,
> +					  struct ethtool_coalesce *ec)
> +{
> +	return virtnet_get_coalesce_one(dev, ec, true, queue);
>   }
>   
>   static void virtnet_init_settings(struct net_device *dev)
> @@ -3291,6 +3388,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
>   	.set_link_ksettings = virtnet_set_link_ksettings,
>   	.set_coalesce = virtnet_set_coalesce,
>   	.get_coalesce = virtnet_get_coalesce,
> +	.set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
> +	.get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
>   	.get_rxfh_key_size = virtnet_get_rxfh_key_size,
>   	.get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
>   	.get_rxfh = virtnet_get_rxfh,
> diff --git a/include/uapi/linux/virtio_net.h b/include/uapi/linux/virtio_net.h
> index 12c1c9699935..cc65ef0f3c3e 100644
> --- a/include/uapi/linux/virtio_net.h
> +++ b/include/uapi/linux/virtio_net.h
> @@ -56,6 +56,7 @@
>   #define VIRTIO_NET_F_MQ	22	/* Device supports Receive Flow
>   					 * Steering */
>   #define VIRTIO_NET_F_CTRL_MAC_ADDR 23	/* Set MAC address */
> +#define VIRTIO_NET_F_VQ_NOTF_COAL 52	/* Device supports virtqueue notification coalescing */
>   #define VIRTIO_NET_F_NOTF_COAL	53	/* Device supports notifications coalescing */
>   #define VIRTIO_NET_F_GUEST_USO4	54	/* Guest can handle USOv4 in. */
>   #define VIRTIO_NET_F_GUEST_USO6	55	/* Guest can handle USOv6 in. */
> @@ -391,5 +392,18 @@ struct virtio_net_ctrl_coal_rx {
>   };
>   
>   #define VIRTIO_NET_CTRL_NOTF_COAL_RX_SET		1
> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET		2
> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_GET		3
> +
> +struct virtio_net_ctrl_coal {
> +	__le32 max_packets;
> +	__le32 max_usecs;
> +};
> +
> +struct  virtio_net_ctrl_coal_vq {
> +	__le16 vqn;
> +	__le16 reserved;
> +	struct virtio_net_ctrl_coal coal;
> +};
>   
>   #endif /* _UAPI_LINUX_VIRTIO_NET_H */
Gavin Li July 18, 2023, 6:28 a.m. UTC | #4
On 7/18/2023 11:29 AM, Heng Qi wrote:
> 
> 
> 在 2023/7/17 下午10:30, Gavin Li 写道:
>> Add interrupt_coalesce config in send_queue and receive_queue to cache 
>> user
>> config.
>>
>> Send per virtqueue interrupt moderation config to underline device in 
>> order
>> to have more efficient interrupt moderation and cpu utilization of guest
>> VM.
>>
>> Signed-off-by: Gavin Li <gavinl@nvidia.com>
>> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
>> Reviewed-by: Jiri Pirko <jiri@nvidia.com>
>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>> ---
>>   drivers/net/virtio_net.c        | 123 ++++++++++++++++++++++++++++----
>>   include/uapi/linux/virtio_net.h |  14 ++++
>>   2 files changed, 125 insertions(+), 12 deletions(-)
>>
>> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
>> index 802ed21453f5..1566c7de9436 100644
>> --- a/drivers/net/virtio_net.c
>> +++ b/drivers/net/virtio_net.c
>> @@ -144,6 +144,8 @@ struct send_queue {
>>       struct virtnet_sq_stats stats;
>> +    struct virtnet_interrupt_coalesce intr_coal;
>> +
>>       struct napi_struct napi;
>>       /* Record whether sq is in reset state. */
>> @@ -161,6 +163,8 @@ struct receive_queue {
>>       struct virtnet_rq_stats stats;
>> +    struct virtnet_interrupt_coalesce intr_coal;
>> +
>>       /* Chain pages by the private ptr. */
>>       struct page *pages;
>> @@ -3078,6 +3082,59 @@ static int virtnet_send_notf_coal_cmds(struct 
>> virtnet_info *vi,
>>       return 0;
>>   }
>> +static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
>> +                     u16 vqn, u32 max_usecs, u32 max_packets)
>> +{
>> +    struct virtio_net_ctrl_coal_vq *coal_vq;
>> +    struct scatterlist sgs;
>> +
>> +    coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);
> 
> I think this should go in the structure control_buf, which serves two 
> purposes, and that's on the heap in init_vqs():
> 1. We can have the same form as other control types, such as 
> virtio_net_ctrl_coal_{tx, rx};
> 2. Avoid using heap memory here to cause the following memory leaks
ACK
> 
>> +    if (!coal_vq)
>> +        return -ENOMEM;
>> +    coal_vq->vqn = cpu_to_le16(vqn);
>> +    coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
>> +    coal_vq->coal.max_packets = cpu_to_le32(max_packets);
>> +    sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
>> +
>> +    if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
>> +                  VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
>> +                  &sgs))
>> +        return -EINVAL;
> 
> If this fails, we should free coal_vq, so pls move coal_vq into 
> control_buf.
> 
> Thanks.
> ACK
>> +
>> +    return 0;
>> +}
>> +
>> +static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
>> +                      struct ethtool_coalesce *ec,
>> +                      u16 queue)
>> +{
>> +    int err;
>> +
>> +    if (ec->rx_coalesce_usecs || ec->rx_max_coalesced_frames) {
>> +        err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
>> +                            ec->rx_coalesce_usecs,
>> +                            ec->rx_max_coalesced_frames);
>> +        if (err)
>> +            return err;
>> +        /* Save parameters */
>> +        vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
>> +        vi->rq[queue].intr_coal.max_packets = 
>> ec->rx_max_coalesced_frames;
>> +    }
>> +
>> +    if (ec->tx_coalesce_usecs || ec->tx_max_coalesced_frames) {
>> +        err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
>> +                            ec->tx_coalesce_usecs,
>> +                            ec->tx_max_coalesced_frames);
>> +        if (err)
>> +            return err;
>> +        /* Save parameters */
>> +        vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
>> +        vi->sq[queue].intr_coal.max_packets = 
>> ec->tx_max_coalesced_frames;
>> +    }
>> +
>> +    return 0;
>> +}
>> +
>>   static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>>   {
>>       /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
>> @@ -3094,23 +3151,39 @@ static int 
>> virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>>   }
>>   static int virtnet_set_coalesce_one(struct net_device *dev,
>> -                    struct ethtool_coalesce *ec)
>> +                    struct ethtool_coalesce *ec,
>> +                    bool per_queue,
>> +                    u32 queue)
>>   {
>>       struct virtnet_info *vi = netdev_priv(dev);
>> -    int ret, i, napi_weight;
>> +    int queue_count = per_queue ? 1 : vi->max_queue_pairs;
>> +    int queue_number = per_queue ? queue : 0;
>>       bool update_napi = false;
>> +    int ret, i, napi_weight;
>> +
>> +    if (queue >= vi->max_queue_pairs)
>> +        return -EINVAL;
>>       /* Can't change NAPI weight if the link is up */
>>       napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
>> -    if (napi_weight ^ vi->sq[0].napi.weight) {
>> -        if (dev->flags & IFF_UP)
>> -            return -EBUSY;
>> -        else
>> +    for (i = queue_number; i < queue_count; i++) {
>> +        if (napi_weight ^ vi->sq[i].napi.weight) {
>> +            if (dev->flags & IFF_UP)
>> +                return -EBUSY;
>> +
>>               update_napi = true;
>> +            /* All queues that belong to [queue_number, queue_count] 
>> will be
>> +             * updated for the sake of simplicity, which might not be 
>> necessary
>> +             */
>> +            queue_number = i;
>> +            break;
>> +        }
>>       }
>> -    if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
>> +    if (!per_queue && virtio_has_feature(vi->vdev, 
>> VIRTIO_NET_F_NOTF_COAL))
>>           ret = virtnet_send_notf_coal_cmds(vi, ec);
>> +    else if (per_queue && virtio_has_feature(vi->vdev, 
>> VIRTIO_NET_F_VQ_NOTF_COAL))
>> +        ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
>>       else
>>           ret = virtnet_coal_params_supported(ec);
>> @@ -3118,7 +3191,7 @@ static int virtnet_set_coalesce_one(struct 
>> net_device *dev,
>>           return ret;
>>       if (update_napi) {
>> -        for (i = 0; i < vi->max_queue_pairs; i++)
>> +        for (i = queue_number; i < queue_count; i++)
>>               vi->sq[i].napi.weight = napi_weight;
>>       }
>> @@ -3130,19 +3203,29 @@ static int virtnet_set_coalesce(struct 
>> net_device *dev,
>>                   struct kernel_ethtool_coalesce *kernel_coal,
>>                   struct netlink_ext_ack *extack)
>>   {
>> -    return virtnet_set_coalesce_one(dev, ec);
>> +    return virtnet_set_coalesce_one(dev, ec, false, 0);
>>   }
>>   static int virtnet_get_coalesce_one(struct net_device *dev,
>> -                    struct ethtool_coalesce *ec)
>> +                    struct ethtool_coalesce *ec,
>> +                    bool per_queue,
>> +                    u32 queue)
>>   {
>>       struct virtnet_info *vi = netdev_priv(dev);
>> -    if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
>> +    if (queue >= vi->max_queue_pairs)
>> +        return -EINVAL;
>> +
>> +    if (!per_queue && virtio_has_feature(vi->vdev, 
>> VIRTIO_NET_F_NOTF_COAL)) {
>>           ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
>>           ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
>>           ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
>>           ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
>> +    } else if (per_queue && virtio_has_feature(vi->vdev, 
>> VIRTIO_NET_F_VQ_NOTF_COAL)) {
>> +        ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
>> +        ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
>> +        ec->tx_max_coalesced_frames = 
>> vi->sq[queue].intr_coal.max_packets;
>> +        ec->rx_max_coalesced_frames = 
>> vi->rq[queue].intr_coal.max_packets;
>>       } else {
>>           ec->rx_max_coalesced_frames = 1;
>> @@ -3158,7 +3241,21 @@ static int virtnet_get_coalesce(struct 
>> net_device *dev,
>>                   struct kernel_ethtool_coalesce *kernel_coal,
>>                   struct netlink_ext_ack *extack)
>>   {
>> -    return virtnet_get_coalesce_one(dev, ec);
>> +    return virtnet_get_coalesce_one(dev, ec, false, 0);
>> +}
>> +
>> +static int virtnet_set_per_queue_coalesce(struct net_device *dev,
>> +                      u32 queue,
>> +                      struct ethtool_coalesce *ec)
>> +{
>> +    return virtnet_set_coalesce_one(dev, ec, true, queue);
>> +}
>> +
>> +static int virtnet_get_per_queue_coalesce(struct net_device *dev,
>> +                      u32 queue,
>> +                      struct ethtool_coalesce *ec)
>> +{
>> +    return virtnet_get_coalesce_one(dev, ec, true, queue);
>>   }
>>   static void virtnet_init_settings(struct net_device *dev)
>> @@ -3291,6 +3388,8 @@ static const struct ethtool_ops 
>> virtnet_ethtool_ops = {
>>       .set_link_ksettings = virtnet_set_link_ksettings,
>>       .set_coalesce = virtnet_set_coalesce,
>>       .get_coalesce = virtnet_get_coalesce,
>> +    .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
>> +    .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
>>       .get_rxfh_key_size = virtnet_get_rxfh_key_size,
>>       .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
>>       .get_rxfh = virtnet_get_rxfh,
>> diff --git a/include/uapi/linux/virtio_net.h 
>> b/include/uapi/linux/virtio_net.h
>> index 12c1c9699935..cc65ef0f3c3e 100644
>> --- a/include/uapi/linux/virtio_net.h
>> +++ b/include/uapi/linux/virtio_net.h
>> @@ -56,6 +56,7 @@
>>   #define VIRTIO_NET_F_MQ    22    /* Device supports Receive Flow
>>                        * Steering */
>>   #define VIRTIO_NET_F_CTRL_MAC_ADDR 23    /* Set MAC address */
>> +#define VIRTIO_NET_F_VQ_NOTF_COAL 52    /* Device supports virtqueue 
>> notification coalescing */
>>   #define VIRTIO_NET_F_NOTF_COAL    53    /* Device supports 
>> notifications coalescing */
>>   #define VIRTIO_NET_F_GUEST_USO4    54    /* Guest can handle USOv4 
>> in. */
>>   #define VIRTIO_NET_F_GUEST_USO6    55    /* Guest can handle USOv6 
>> in. */
>> @@ -391,5 +392,18 @@ struct virtio_net_ctrl_coal_rx {
>>   };
>>   #define VIRTIO_NET_CTRL_NOTF_COAL_RX_SET        1
>> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET        2
>> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_GET        3
>> +
>> +struct virtio_net_ctrl_coal {
>> +    __le32 max_packets;
>> +    __le32 max_usecs;
>> +};
>> +
>> +struct  virtio_net_ctrl_coal_vq {
>> +    __le16 vqn;
>> +    __le16 reserved;
>> +    struct virtio_net_ctrl_coal coal;
>> +};
>>   #endif /* _UAPI_LINUX_VIRTIO_NET_H */
>
Gavin Li July 18, 2023, 6:45 a.m. UTC | #5
On 7/18/2023 11:37 AM, Heng Qi wrote:
> 
> 
> 在 2023/7/17 下午10:30, Gavin Li 写道:
>> Add interrupt_coalesce config in send_queue and receive_queue to cache 
>> user
>> config.
>>
>> Send per virtqueue interrupt moderation config to underline device in 
>> order
>> to have more efficient interrupt moderation and cpu utilization of guest
>> VM.
>>
>> Signed-off-by: Gavin Li <gavinl@nvidia.com>
>> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
>> Reviewed-by: Jiri Pirko <jiri@nvidia.com>
>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>> ---
>>   drivers/net/virtio_net.c        | 123 ++++++++++++++++++++++++++++----
>>   include/uapi/linux/virtio_net.h |  14 ++++
>>   2 files changed, 125 insertions(+), 12 deletions(-)
>>
>> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
>> index 802ed21453f5..1566c7de9436 100644
>> --- a/drivers/net/virtio_net.c
>> +++ b/drivers/net/virtio_net.c
>> @@ -144,6 +144,8 @@ struct send_queue {
>>       struct virtnet_sq_stats stats;
>> +    struct virtnet_interrupt_coalesce intr_coal;
>> +
>>       struct napi_struct napi;
>>       /* Record whether sq is in reset state. */
>> @@ -161,6 +163,8 @@ struct receive_queue {
>>       struct virtnet_rq_stats stats;
>> +    struct virtnet_interrupt_coalesce intr_coal;
>> +
>>       /* Chain pages by the private ptr. */
>>       struct page *pages;
>> @@ -3078,6 +3082,59 @@ static int virtnet_send_notf_coal_cmds(struct 
>> virtnet_info *vi,
>>       return 0;
>>   }
>> +static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
>> +                     u16 vqn, u32 max_usecs, u32 max_packets)
>> +{
>> +    struct virtio_net_ctrl_coal_vq *coal_vq;
>> +    struct scatterlist sgs;
>> +
>> +    coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);
>> +    if (!coal_vq)
>> +        return -ENOMEM;
>> +    coal_vq->vqn = cpu_to_le16(vqn);
>> +    coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
>> +    coal_vq->coal.max_packets = cpu_to_le32(max_packets);
>> +    sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
>> +
>> +    if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
>> +                  VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
>> +                  &sgs))
>> +        return -EINVAL;
>> +
>> +    return 0;
>> +}
>> +
>> +static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
>> +                      struct ethtool_coalesce *ec,
>> +                      u16 queue)
>> +{
>> +    int err;
>> +
>> +    if (ec->rx_coalesce_usecs || ec->rx_max_coalesced_frames) {
>> +        err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
>> +                            ec->rx_coalesce_usecs,
>> +                            ec->rx_max_coalesced_frames);
>> +        if (err)
>> +            return err;
>> +        /* Save parameters */
>> +        vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
>> +        vi->rq[queue].intr_coal.max_packets = 
>> ec->rx_max_coalesced_frames;
>> +    }
>> +
>> +    if (ec->tx_coalesce_usecs || ec->tx_max_coalesced_frames) {
>> +        err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
>> +                            ec->tx_coalesce_usecs,
>> +                            ec->tx_max_coalesced_frames);
>> +        if (err)
>> +            return err;
>> +        /* Save parameters */
>> +        vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
>> +        vi->sq[queue].intr_coal.max_packets = 
>> ec->tx_max_coalesced_frames;
>> +    }
>> +
>> +    return 0;
>> +}
>> +
>>   static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>>   {
>>       /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
>> @@ -3094,23 +3151,39 @@ static int 
>> virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>>   }
>>   static int virtnet_set_coalesce_one(struct net_device *dev,
>> -                    struct ethtool_coalesce *ec)
>> +                    struct ethtool_coalesce *ec,
>> +                    bool per_queue,
>> +                    u32 queue)
>>   {
>>       struct virtnet_info *vi = netdev_priv(dev);
>> -    int ret, i, napi_weight;
>> +    int queue_count = per_queue ? 1 : vi->max_queue_pairs;
>> +    int queue_number = per_queue ? queue : 0;
>>       bool update_napi = false;
>> +    int ret, i, napi_weight;
>> +
>> +    if (queue >= vi->max_queue_pairs)
>> +        return -EINVAL;
>>       /* Can't change NAPI weight if the link is up */
>>       napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
>> -    if (napi_weight ^ vi->sq[0].napi.weight) {
>> -        if (dev->flags & IFF_UP)
>> -            return -EBUSY;
>> -        else
>> +    for (i = queue_number; i < queue_count; i++) {
>> +        if (napi_weight ^ vi->sq[i].napi.weight) {
>> +            if (dev->flags & IFF_UP)
>> +                return -EBUSY;
>> +
>>               update_napi = true;
>> +            /* All queues that belong to [queue_number, queue_count] 
>> will be
>> +             * updated for the sake of simplicity, which might not be 
>> necessary
>> +             */
>> +            queue_number = i;
>> +            break;
>> +        }
>>       }
>> -    if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
>> +    if (!per_queue && virtio_has_feature(vi->vdev, 
>> VIRTIO_NET_F_NOTF_COAL))
>>           ret = virtnet_send_notf_coal_cmds(vi, ec);
>> +    else if (per_queue && virtio_has_feature(vi->vdev, 
>> VIRTIO_NET_F_VQ_NOTF_COAL))
>> +        ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
>>       else
>>           ret = virtnet_coal_params_supported(ec);
>> @@ -3118,7 +3191,7 @@ static int virtnet_set_coalesce_one(struct 
>> net_device *dev,
>>           return ret;
>>       if (update_napi) {
>> -        for (i = 0; i < vi->max_queue_pairs; i++)
>> +        for (i = queue_number; i < queue_count; i++)
>>               vi->sq[i].napi.weight = napi_weight;
>>       }
>> @@ -3130,19 +3203,29 @@ static int virtnet_set_coalesce(struct 
>> net_device *dev,
>>                   struct kernel_ethtool_coalesce *kernel_coal,
>>                   struct netlink_ext_ack *extack)
>>   {
>> -    return virtnet_set_coalesce_one(dev, ec);
>> +    return virtnet_set_coalesce_one(dev, ec, false, 0);
>>   }
>>   static int virtnet_get_coalesce_one(struct net_device *dev,
>> -                    struct ethtool_coalesce *ec)
>> +                    struct ethtool_coalesce *ec,
>> +                    bool per_queue,
>> +                    u32 queue)
>>   {
>>       struct virtnet_info *vi = netdev_priv(dev);
>> -    if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
>> +    if (queue >= vi->max_queue_pairs)
>> +        return -EINVAL;
>> +
>> +    if (!per_queue && virtio_has_feature(vi->vdev, 
>> VIRTIO_NET_F_NOTF_COAL)) {
>>           ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
>>           ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
>>           ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
>>           ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
>> +    } else if (per_queue && virtio_has_feature(vi->vdev, 
>> VIRTIO_NET_F_VQ_NOTF_COAL)) {
>> +        ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
>> +        ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
>> +        ec->tx_max_coalesced_frames = 
>> vi->sq[queue].intr_coal.max_packets;
>> +        ec->rx_max_coalesced_frames = 
>> vi->rq[queue].intr_coal.max_packets;
>>       } else {
>>           ec->rx_max_coalesced_frames = 1;
>> @@ -3158,7 +3241,21 @@ static int virtnet_get_coalesce(struct 
>> net_device *dev,
>>                   struct kernel_ethtool_coalesce *kernel_coal,
>>                   struct netlink_ext_ack *extack)
>>   {
>> -    return virtnet_get_coalesce_one(dev, ec);
>> +    return virtnet_get_coalesce_one(dev, ec, false, 0);
>> +}
>> +
>> +static int virtnet_set_per_queue_coalesce(struct net_device *dev,
>> +                      u32 queue,
>> +                      struct ethtool_coalesce *ec)
> 
> When \field{max_virtqueue_pairs} is the maximum value, and the user does 
> not carry the queue_mask for 'ethtool -Q',
> we will send same command for all vqs, and the device will receive a 
> large number of the same VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET commands at 
> this time.
> Do we want to alleviate this situation?
> 
> Thanks.
> 

May it be better to handle this senario in ethtool user space and call 
set_coalesce instead of set_per_queue_coalesce? I'm not sure.
>> +{
>> +    return virtnet_set_coalesce_one(dev, ec, true, queue);
>> +}
>> +
>> +static int virtnet_get_per_queue_coalesce(struct net_device *dev,
>> +                      u32 queue,
>> +                      struct ethtool_coalesce *ec)
>> +{
>> +    return virtnet_get_coalesce_one(dev, ec, true, queue);
>>   }
>>   static void virtnet_init_settings(struct net_device *dev)
>> @@ -3291,6 +3388,8 @@ static const struct ethtool_ops 
>> virtnet_ethtool_ops = {
>>       .set_link_ksettings = virtnet_set_link_ksettings,
>>       .set_coalesce = virtnet_set_coalesce,
>>       .get_coalesce = virtnet_get_coalesce,
>> +    .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
>> +    .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
>>       .get_rxfh_key_size = virtnet_get_rxfh_key_size,
>>       .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
>>       .get_rxfh = virtnet_get_rxfh,
>> diff --git a/include/uapi/linux/virtio_net.h 
>> b/include/uapi/linux/virtio_net.h
>> index 12c1c9699935..cc65ef0f3c3e 100644
>> --- a/include/uapi/linux/virtio_net.h
>> +++ b/include/uapi/linux/virtio_net.h
>> @@ -56,6 +56,7 @@
>>   #define VIRTIO_NET_F_MQ    22    /* Device supports Receive Flow
>>                        * Steering */
>>   #define VIRTIO_NET_F_CTRL_MAC_ADDR 23    /* Set MAC address */
>> +#define VIRTIO_NET_F_VQ_NOTF_COAL 52    /* Device supports virtqueue 
>> notification coalescing */
>>   #define VIRTIO_NET_F_NOTF_COAL    53    /* Device supports 
>> notifications coalescing */
>>   #define VIRTIO_NET_F_GUEST_USO4    54    /* Guest can handle USOv4 
>> in. */
>>   #define VIRTIO_NET_F_GUEST_USO6    55    /* Guest can handle USOv6 
>> in. */
>> @@ -391,5 +392,18 @@ struct virtio_net_ctrl_coal_rx {
>>   };
>>   #define VIRTIO_NET_CTRL_NOTF_COAL_RX_SET        1
>> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET        2
>> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_GET        3
>> +
>> +struct virtio_net_ctrl_coal {
>> +    __le32 max_packets;
>> +    __le32 max_usecs;
>> +};
>> +
>> +struct  virtio_net_ctrl_coal_vq {
>> +    __le16 vqn;
>> +    __le16 reserved;
>> +    struct virtio_net_ctrl_coal coal;
>> +};
>>   #endif /* _UAPI_LINUX_VIRTIO_NET_H */
>
Heng Qi July 19, 2023, 12:19 p.m. UTC | #6
在 2023/7/18 下午2:45, Gavin Li 写道:
>
>
> On 7/18/2023 11:37 AM, Heng Qi wrote:
>>
>>
>> 在 2023/7/17 下午10:30, Gavin Li 写道:
>>> Add interrupt_coalesce config in send_queue and receive_queue to 
>>> cache user
>>> config.
>>>
>>> Send per virtqueue interrupt moderation config to underline device 
>>> in order
>>> to have more efficient interrupt moderation and cpu utilization of 
>>> guest
>>> VM.
>>>
>>> Signed-off-by: Gavin Li <gavinl@nvidia.com>
>>> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
>>> Reviewed-by: Jiri Pirko <jiri@nvidia.com>
>>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>>> ---
>>>   drivers/net/virtio_net.c        | 123 
>>> ++++++++++++++++++++++++++++----
>>>   include/uapi/linux/virtio_net.h |  14 ++++
>>>   2 files changed, 125 insertions(+), 12 deletions(-)
>>>
>>> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
>>> index 802ed21453f5..1566c7de9436 100644
>>> --- a/drivers/net/virtio_net.c
>>> +++ b/drivers/net/virtio_net.c
>>> @@ -144,6 +144,8 @@ struct send_queue {
>>>       struct virtnet_sq_stats stats;
>>> +    struct virtnet_interrupt_coalesce intr_coal;
>>> +
>>>       struct napi_struct napi;
>>>       /* Record whether sq is in reset state. */
>>> @@ -161,6 +163,8 @@ struct receive_queue {
>>>       struct virtnet_rq_stats stats;
>>> +    struct virtnet_interrupt_coalesce intr_coal;
>>> +
>>>       /* Chain pages by the private ptr. */
>>>       struct page *pages;
>>> @@ -3078,6 +3082,59 @@ static int virtnet_send_notf_coal_cmds(struct 
>>> virtnet_info *vi,
>>>       return 0;
>>>   }
>>> +static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
>>> +                     u16 vqn, u32 max_usecs, u32 max_packets)
>>> +{
>>> +    struct virtio_net_ctrl_coal_vq *coal_vq;
>>> +    struct scatterlist sgs;
>>> +
>>> +    coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);
>>> +    if (!coal_vq)
>>> +        return -ENOMEM;
>>> +    coal_vq->vqn = cpu_to_le16(vqn);
>>> +    coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
>>> +    coal_vq->coal.max_packets = cpu_to_le32(max_packets);
>>> +    sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
>>> +
>>> +    if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
>>> +                  VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
>>> +                  &sgs))
>>> +        return -EINVAL;
>>> +
>>> +    return 0;
>>> +}
>>> +
>>> +static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
>>> +                      struct ethtool_coalesce *ec,
>>> +                      u16 queue)
>>> +{
>>> +    int err;
>>> +
>>> +    if (ec->rx_coalesce_usecs || ec->rx_max_coalesced_frames) {
>>> +        err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
>>> +                            ec->rx_coalesce_usecs,
>>> +                            ec->rx_max_coalesced_frames);
>>> +        if (err)
>>> +            return err;
>>> +        /* Save parameters */
>>> +        vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
>>> +        vi->rq[queue].intr_coal.max_packets = 
>>> ec->rx_max_coalesced_frames;
>>> +    }
>>> +
>>> +    if (ec->tx_coalesce_usecs || ec->tx_max_coalesced_frames) {
>>> +        err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
>>> +                            ec->tx_coalesce_usecs,
>>> +                            ec->tx_max_coalesced_frames);
>>> +        if (err)
>>> +            return err;
>>> +        /* Save parameters */
>>> +        vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
>>> +        vi->sq[queue].intr_coal.max_packets = 
>>> ec->tx_max_coalesced_frames;
>>> +    }
>>> +
>>> +    return 0;
>>> +}
>>> +
>>>   static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>>>   {
>>>       /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
>>> @@ -3094,23 +3151,39 @@ static int 
>>> virtnet_coal_params_supported(struct ethtool_coalesce *ec)
>>>   }
>>>   static int virtnet_set_coalesce_one(struct net_device *dev,
>>> -                    struct ethtool_coalesce *ec)
>>> +                    struct ethtool_coalesce *ec,
>>> +                    bool per_queue,
>>> +                    u32 queue)
>>>   {
>>>       struct virtnet_info *vi = netdev_priv(dev);
>>> -    int ret, i, napi_weight;
>>> +    int queue_count = per_queue ? 1 : vi->max_queue_pairs;
>>> +    int queue_number = per_queue ? queue : 0;
>>>       bool update_napi = false;
>>> +    int ret, i, napi_weight;
>>> +
>>> +    if (queue >= vi->max_queue_pairs)
>>> +        return -EINVAL;
>>>       /* Can't change NAPI weight if the link is up */
>>>       napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
>>> -    if (napi_weight ^ vi->sq[0].napi.weight) {
>>> -        if (dev->flags & IFF_UP)
>>> -            return -EBUSY;
>>> -        else
>>> +    for (i = queue_number; i < queue_count; i++) {
>>> +        if (napi_weight ^ vi->sq[i].napi.weight) {
>>> +            if (dev->flags & IFF_UP)
>>> +                return -EBUSY;
>>> +
>>>               update_napi = true;
>>> +            /* All queues that belong to [queue_number, 
>>> queue_count] will be
>>> +             * updated for the sake of simplicity, which might not 
>>> be necessary
>>> +             */
>>> +            queue_number = i;
>>> +            break;
>>> +        }
>>>       }
>>> -    if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
>>> +    if (!per_queue && virtio_has_feature(vi->vdev, 
>>> VIRTIO_NET_F_NOTF_COAL))
>>>           ret = virtnet_send_notf_coal_cmds(vi, ec);
>>> +    else if (per_queue && virtio_has_feature(vi->vdev, 
>>> VIRTIO_NET_F_VQ_NOTF_COAL))
>>> +        ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
>>>       else
>>>           ret = virtnet_coal_params_supported(ec);
>>> @@ -3118,7 +3191,7 @@ static int virtnet_set_coalesce_one(struct 
>>> net_device *dev,
>>>           return ret;
>>>       if (update_napi) {
>>> -        for (i = 0; i < vi->max_queue_pairs; i++)
>>> +        for (i = queue_number; i < queue_count; i++)
>>>               vi->sq[i].napi.weight = napi_weight;
>>>       }
>>> @@ -3130,19 +3203,29 @@ static int virtnet_set_coalesce(struct 
>>> net_device *dev,
>>>                   struct kernel_ethtool_coalesce *kernel_coal,
>>>                   struct netlink_ext_ack *extack)
>>>   {
>>> -    return virtnet_set_coalesce_one(dev, ec);
>>> +    return virtnet_set_coalesce_one(dev, ec, false, 0);
>>>   }
>>>   static int virtnet_get_coalesce_one(struct net_device *dev,
>>> -                    struct ethtool_coalesce *ec)
>>> +                    struct ethtool_coalesce *ec,
>>> +                    bool per_queue,
>>> +                    u32 queue)
>>>   {
>>>       struct virtnet_info *vi = netdev_priv(dev);
>>> -    if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
>>> +    if (queue >= vi->max_queue_pairs)
>>> +        return -EINVAL;
>>> +
>>> +    if (!per_queue && virtio_has_feature(vi->vdev, 
>>> VIRTIO_NET_F_NOTF_COAL)) {
>>>           ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
>>>           ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
>>>           ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
>>>           ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
>>> +    } else if (per_queue && virtio_has_feature(vi->vdev, 
>>> VIRTIO_NET_F_VQ_NOTF_COAL)) {
>>> +        ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
>>> +        ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
>>> +        ec->tx_max_coalesced_frames = 
>>> vi->sq[queue].intr_coal.max_packets;
>>> +        ec->rx_max_coalesced_frames = 
>>> vi->rq[queue].intr_coal.max_packets;
>>>       } else {
>>>           ec->rx_max_coalesced_frames = 1;
>>> @@ -3158,7 +3241,21 @@ static int virtnet_get_coalesce(struct 
>>> net_device *dev,
>>>                   struct kernel_ethtool_coalesce *kernel_coal,
>>>                   struct netlink_ext_ack *extack)
>>>   {
>>> -    return virtnet_get_coalesce_one(dev, ec);
>>> +    return virtnet_get_coalesce_one(dev, ec, false, 0);
>>> +}
>>> +
>>> +static int virtnet_set_per_queue_coalesce(struct net_device *dev,
>>> +                      u32 queue,
>>> +                      struct ethtool_coalesce *ec)
>>
>> When \field{max_virtqueue_pairs} is the maximum value, and the user 
>> does not carry the queue_mask for 'ethtool -Q',
>> we will send same command for all vqs, and the device will receive a 
>> large number of the same VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET commands at 
>> this time.
>> Do we want to alleviate this situation?
>>
>> Thanks.
>>
>
> May it be better to handle this senario in ethtool user space and call 
> set_coalesce instead of set_per_queue_coalesce? I'm not sure.

Maybe we need to force 'ethtool -Q' to carry queue_mask in userspace or 
instead call set_coalesce() interface when not carrying queue_mask
(this does not hinder this work and should be in another thread, please 
move on). But this I'm not sure either, since other NICs don't seem to
care much about this. Simply check that all drivers that implement the 
set_per_queue_coalesce() interface have implemented set_coalesce().

Thanks.

>>> +{
>>> +    return virtnet_set_coalesce_one(dev, ec, true, queue);
>>> +}
>>> +
>>> +static int virtnet_get_per_queue_coalesce(struct net_device *dev,
>>> +                      u32 queue,
>>> +                      struct ethtool_coalesce *ec)
>>> +{
>>> +    return virtnet_get_coalesce_one(dev, ec, true, queue);
>>>   }
>>>   static void virtnet_init_settings(struct net_device *dev)
>>> @@ -3291,6 +3388,8 @@ static const struct ethtool_ops 
>>> virtnet_ethtool_ops = {
>>>       .set_link_ksettings = virtnet_set_link_ksettings,
>>>       .set_coalesce = virtnet_set_coalesce,
>>>       .get_coalesce = virtnet_get_coalesce,
>>> +    .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
>>> +    .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
>>>       .get_rxfh_key_size = virtnet_get_rxfh_key_size,
>>>       .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
>>>       .get_rxfh = virtnet_get_rxfh,
>>> diff --git a/include/uapi/linux/virtio_net.h 
>>> b/include/uapi/linux/virtio_net.h
>>> index 12c1c9699935..cc65ef0f3c3e 100644
>>> --- a/include/uapi/linux/virtio_net.h
>>> +++ b/include/uapi/linux/virtio_net.h
>>> @@ -56,6 +56,7 @@
>>>   #define VIRTIO_NET_F_MQ    22    /* Device supports Receive Flow
>>>                        * Steering */
>>>   #define VIRTIO_NET_F_CTRL_MAC_ADDR 23    /* Set MAC address */
>>> +#define VIRTIO_NET_F_VQ_NOTF_COAL 52    /* Device supports 
>>> virtqueue notification coalescing */
>>>   #define VIRTIO_NET_F_NOTF_COAL    53    /* Device supports 
>>> notifications coalescing */
>>>   #define VIRTIO_NET_F_GUEST_USO4    54    /* Guest can handle USOv4 
>>> in. */
>>>   #define VIRTIO_NET_F_GUEST_USO6    55    /* Guest can handle USOv6 
>>> in. */
>>> @@ -391,5 +392,18 @@ struct virtio_net_ctrl_coal_rx {
>>>   };
>>>   #define VIRTIO_NET_CTRL_NOTF_COAL_RX_SET        1
>>> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET        2
>>> +#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_GET        3
>>> +
>>> +struct virtio_net_ctrl_coal {
>>> +    __le32 max_packets;
>>> +    __le32 max_usecs;
>>> +};
>>> +
>>> +struct  virtio_net_ctrl_coal_vq {
>>> +    __le16 vqn;
>>> +    __le16 reserved;
>>> +    struct virtio_net_ctrl_coal coal;
>>> +};
>>>   #endif /* _UAPI_LINUX_VIRTIO_NET_H */
>>
diff mbox series

Patch

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 802ed21453f5..1566c7de9436 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -144,6 +144,8 @@  struct send_queue {
 
 	struct virtnet_sq_stats stats;
 
+	struct virtnet_interrupt_coalesce intr_coal;
+
 	struct napi_struct napi;
 
 	/* Record whether sq is in reset state. */
@@ -161,6 +163,8 @@  struct receive_queue {
 
 	struct virtnet_rq_stats stats;
 
+	struct virtnet_interrupt_coalesce intr_coal;
+
 	/* Chain pages by the private ptr. */
 	struct page *pages;
 
@@ -3078,6 +3082,59 @@  static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
 	return 0;
 }
 
+static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
+					 u16 vqn, u32 max_usecs, u32 max_packets)
+{
+	struct virtio_net_ctrl_coal_vq *coal_vq;
+	struct scatterlist sgs;
+
+	coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);
+	if (!coal_vq)
+		return -ENOMEM;
+	coal_vq->vqn = cpu_to_le16(vqn);
+	coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
+	coal_vq->coal.max_packets = cpu_to_le32(max_packets);
+	sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
+
+	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
+				  VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
+				  &sgs))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
+					  struct ethtool_coalesce *ec,
+					  u16 queue)
+{
+	int err;
+
+	if (ec->rx_coalesce_usecs || ec->rx_max_coalesced_frames) {
+		err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
+						    ec->rx_coalesce_usecs,
+						    ec->rx_max_coalesced_frames);
+		if (err)
+			return err;
+		/* Save parameters */
+		vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
+		vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames;
+	}
+
+	if (ec->tx_coalesce_usecs || ec->tx_max_coalesced_frames) {
+		err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
+						    ec->tx_coalesce_usecs,
+						    ec->tx_max_coalesced_frames);
+		if (err)
+			return err;
+		/* Save parameters */
+		vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
+		vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames;
+	}
+
+	return 0;
+}
+
 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
 {
 	/* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
@@ -3094,23 +3151,39 @@  static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
 }
 
 static int virtnet_set_coalesce_one(struct net_device *dev,
-				    struct ethtool_coalesce *ec)
+				    struct ethtool_coalesce *ec,
+				    bool per_queue,
+				    u32 queue)
 {
 	struct virtnet_info *vi = netdev_priv(dev);
-	int ret, i, napi_weight;
+	int queue_count = per_queue ? 1 : vi->max_queue_pairs;
+	int queue_number = per_queue ? queue : 0;
 	bool update_napi = false;
+	int ret, i, napi_weight;
+
+	if (queue >= vi->max_queue_pairs)
+		return -EINVAL;
 
 	/* Can't change NAPI weight if the link is up */
 	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
-	if (napi_weight ^ vi->sq[0].napi.weight) {
-		if (dev->flags & IFF_UP)
-			return -EBUSY;
-		else
+	for (i = queue_number; i < queue_count; i++) {
+		if (napi_weight ^ vi->sq[i].napi.weight) {
+			if (dev->flags & IFF_UP)
+				return -EBUSY;
+
 			update_napi = true;
+			/* All queues that belong to [queue_number, queue_count] will be
+			 * updated for the sake of simplicity, which might not be necessary
+			 */
+			queue_number = i;
+			break;
+		}
 	}
 
-	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
+	if (!per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
 		ret = virtnet_send_notf_coal_cmds(vi, ec);
+	else if (per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
+		ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
 	else
 		ret = virtnet_coal_params_supported(ec);
 
@@ -3118,7 +3191,7 @@  static int virtnet_set_coalesce_one(struct net_device *dev,
 		return ret;
 
 	if (update_napi) {
-		for (i = 0; i < vi->max_queue_pairs; i++)
+		for (i = queue_number; i < queue_count; i++)
 			vi->sq[i].napi.weight = napi_weight;
 	}
 
@@ -3130,19 +3203,29 @@  static int virtnet_set_coalesce(struct net_device *dev,
 				struct kernel_ethtool_coalesce *kernel_coal,
 				struct netlink_ext_ack *extack)
 {
-	return virtnet_set_coalesce_one(dev, ec);
+	return virtnet_set_coalesce_one(dev, ec, false, 0);
 }
 
 static int virtnet_get_coalesce_one(struct net_device *dev,
-				    struct ethtool_coalesce *ec)
+				    struct ethtool_coalesce *ec,
+				    bool per_queue,
+				    u32 queue)
 {
 	struct virtnet_info *vi = netdev_priv(dev);
 
-	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
+	if (queue >= vi->max_queue_pairs)
+		return -EINVAL;
+
+	if (!per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
 		ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
 		ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
 		ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
 		ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
+	} else if (per_queue && virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
+		ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
+		ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
+		ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
+		ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
 	} else {
 		ec->rx_max_coalesced_frames = 1;
 
@@ -3158,7 +3241,21 @@  static int virtnet_get_coalesce(struct net_device *dev,
 				struct kernel_ethtool_coalesce *kernel_coal,
 				struct netlink_ext_ack *extack)
 {
-	return virtnet_get_coalesce_one(dev, ec);
+	return virtnet_get_coalesce_one(dev, ec, false, 0);
+}
+
+static int virtnet_set_per_queue_coalesce(struct net_device *dev,
+					  u32 queue,
+					  struct ethtool_coalesce *ec)
+{
+	return virtnet_set_coalesce_one(dev, ec, true, queue);
+}
+
+static int virtnet_get_per_queue_coalesce(struct net_device *dev,
+					  u32 queue,
+					  struct ethtool_coalesce *ec)
+{
+	return virtnet_get_coalesce_one(dev, ec, true, queue);
 }
 
 static void virtnet_init_settings(struct net_device *dev)
@@ -3291,6 +3388,8 @@  static const struct ethtool_ops virtnet_ethtool_ops = {
 	.set_link_ksettings = virtnet_set_link_ksettings,
 	.set_coalesce = virtnet_set_coalesce,
 	.get_coalesce = virtnet_get_coalesce,
+	.set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
+	.get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
 	.get_rxfh_key_size = virtnet_get_rxfh_key_size,
 	.get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
 	.get_rxfh = virtnet_get_rxfh,
diff --git a/include/uapi/linux/virtio_net.h b/include/uapi/linux/virtio_net.h
index 12c1c9699935..cc65ef0f3c3e 100644
--- a/include/uapi/linux/virtio_net.h
+++ b/include/uapi/linux/virtio_net.h
@@ -56,6 +56,7 @@ 
 #define VIRTIO_NET_F_MQ	22	/* Device supports Receive Flow
 					 * Steering */
 #define VIRTIO_NET_F_CTRL_MAC_ADDR 23	/* Set MAC address */
+#define VIRTIO_NET_F_VQ_NOTF_COAL 52	/* Device supports virtqueue notification coalescing */
 #define VIRTIO_NET_F_NOTF_COAL	53	/* Device supports notifications coalescing */
 #define VIRTIO_NET_F_GUEST_USO4	54	/* Guest can handle USOv4 in. */
 #define VIRTIO_NET_F_GUEST_USO6	55	/* Guest can handle USOv6 in. */
@@ -391,5 +392,18 @@  struct virtio_net_ctrl_coal_rx {
 };
 
 #define VIRTIO_NET_CTRL_NOTF_COAL_RX_SET		1
+#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET		2
+#define VIRTIO_NET_CTRL_NOTF_COAL_VQ_GET		3
+
+struct virtio_net_ctrl_coal {
+	__le32 max_packets;
+	__le32 max_usecs;
+};
+
+struct  virtio_net_ctrl_coal_vq {
+	__le16 vqn;
+	__le16 reserved;
+	struct virtio_net_ctrl_coal coal;
+};
 
 #endif /* _UAPI_LINUX_VIRTIO_NET_H */