* [PATCH net-next v24 01/14] virtio_pci: Remove supported_caps cache and build assert
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-02 15:04 ` [PATCH net-next v24 02/14] virtio_pci: Fix sleeping under spinlock in admin command path Shahar Shitrit
` (13 subsequent siblings)
14 siblings, 0 replies; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
Following patches introduce capability IDs above 63 (e.g. 0x800),
making the capability ID list larger than a single u64. Remove the
cached supported_caps field so the code uses data->supported_caps
directly, transparently handling a bitmap of any size. Remove the
build assert that enforced the list fit in 64 bits.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
---
drivers/virtio/virtio_pci_common.h | 1 -
drivers/virtio/virtio_pci_modern.c | 10 +++-------
2 files changed, 3 insertions(+), 8 deletions(-)
diff --git a/drivers/virtio/virtio_pci_common.h b/drivers/virtio/virtio_pci_common.h
index 4ce903e7d3a99..b8b8da9d0bab0 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -51,7 +51,6 @@ struct virtio_pci_admin_vq {
/* Protects virtqueue access. */
spinlock_t lock;
u64 supported_cmds;
- u64 supported_caps;
u8 max_dev_parts_objects;
struct ida dev_parts_ida;
/* Name of the admin queue: avq.$vq_index. */
diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
index 922e2df027de9..3a97d0471dd27 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -304,10 +304,10 @@ virtio_pci_admin_cmd_dev_parts_objects_enable(struct virtio_device *virtio_dev)
static void virtio_pci_admin_cmd_cap_init(struct virtio_device *virtio_dev)
{
- struct virtio_pci_device *vp_dev = to_vp_device(virtio_dev);
struct virtio_admin_cmd_query_cap_id_result *data;
struct virtio_admin_cmd cmd = {};
struct scatterlist result_sg;
+ u64 caps;
int ret;
data = kzalloc_obj(*data);
@@ -323,12 +323,8 @@ static void virtio_pci_admin_cmd_cap_init(struct virtio_device *virtio_dev)
if (ret)
goto end;
- /* Max number of caps fits into a single u64 */
- BUILD_BUG_ON(sizeof(data->supported_caps) > sizeof(u64));
-
- vp_dev->admin_vq.supported_caps = le64_to_cpu(data->supported_caps[0]);
-
- if (!(vp_dev->admin_vq.supported_caps & (1 << VIRTIO_DEV_PARTS_CAP)))
+ caps = le64_to_cpu(data->supported_caps[0]);
+ if (!(caps & BIT_ULL(VIRTIO_DEV_PARTS_CAP)))
goto end;
virtio_pci_admin_cmd_dev_parts_objects_enable(virtio_dev);
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* [PATCH net-next v24 02/14] virtio_pci: Fix sleeping under spinlock in admin command path
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
2026-12-02 15:04 ` [PATCH net-next v24 01/14] virtio_pci: Remove supported_caps cache and build assert Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:04 ` [PATCH net-next v24 03/14] virtio: Add config_op for admin commands Shahar Shitrit
` (12 subsequent siblings)
14 siblings, 1 reply; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
virtqueue_exec_admin_cmd() calls virtqueue_add_sgs() with GFP_KERNEL
while holding a spinlock with interrupts disabled. GFP_KERNEL allocations
can sleep, which is illegal in atomic context.
This is reachable today: when the device negotiates
VIRTIO_RING_F_INDIRECT_DESC, an admin command with more than one
scatterlist entry makes virtqueue_add_split() call kmalloc(GFP_KERNEL)
via alloc_indirect_split(), under the spinlock.
The next commit exposes vp_modern_admin_cmd_exec() through
virtio_config_ops, which makes the path available to more callers.
Fix this by replacing the spinlock with a mutex, which allows GFP_KERNEL
to sleep as needed. Since the virtqueue done callback runs in interrupt
context and cannot take a mutex, defer its work to a work item that runs
in process context.
Fixes: 4c3b54af907e ("virtio_pci_modern: use completion instead of busy loop to wait on admin cmd result")
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
---
drivers/virtio/virtio_pci_common.c | 3 +++
drivers/virtio/virtio_pci_common.h | 5 +++-
drivers/virtio/virtio_pci_modern.c | 40 +++++++++++++++++++++---------
3 files changed, 35 insertions(+), 13 deletions(-)
diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
index 79d3395f37050..0a15b9332c474 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -264,6 +264,9 @@ static void vp_del_vq(struct virtqueue *vq, struct virtio_pci_vq_info *info)
spin_unlock_irqrestore(&vp_dev->lock, flags);
}
+ if (vp_is_avq(vq->vdev, vq->index))
+ cancel_work_sync(&vp_dev->admin_vq.work);
+
vp_dev->del_vq(info);
kfree(info);
}
diff --git a/drivers/virtio/virtio_pci_common.h b/drivers/virtio/virtio_pci_common.h
index b8b8da9d0bab0..e4f674ff42ab3 100644
--- a/drivers/virtio/virtio_pci_common.h
+++ b/drivers/virtio/virtio_pci_common.h
@@ -30,6 +30,7 @@
#include <linux/highmem.h>
#include <linux/spinlock.h>
#include <linux/mutex.h>
+#include <linux/workqueue.h>
struct virtio_pci_vq_info {
/* the actual virtqueue */
@@ -49,7 +50,9 @@ struct virtio_pci_admin_vq {
/* Virtqueue info associated with this admin queue. */
struct virtio_pci_vq_info *info;
/* Protects virtqueue access. */
- spinlock_t lock;
+ struct mutex lock;
+ /* Admin command completion work. */
+ struct work_struct work;
u64 supported_cmds;
u8 max_dev_parts_objects;
struct ida dev_parts_ida;
diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
index 3a97d0471dd27..d5ab2c11d0842 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -47,13 +47,22 @@ static int vp_avq_index(struct virtio_device *vdev, u16 *index, u16 *num)
void vp_modern_avq_done(struct virtqueue *vq)
{
struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
- struct virtio_pci_admin_vq *admin_vq = &vp_dev->admin_vq;
+
+ schedule_work(&vp_dev->admin_vq.work);
+}
+
+static void vp_modern_avq_work(struct work_struct *work)
+{
unsigned int status_size = sizeof(struct virtio_admin_cmd_status);
+ struct virtio_pci_admin_vq *admin_vq =
+ container_of(work, struct virtio_pci_admin_vq, work);
struct virtio_admin_cmd *cmd;
- unsigned long flags;
+ struct virtqueue *vq;
unsigned int len;
- spin_lock_irqsave(&admin_vq->lock, flags);
+ vq = admin_vq->info->vq;
+
+ mutex_lock(&admin_vq->lock);
do {
virtqueue_disable_cb(vq);
while ((cmd = virtqueue_get_buf(vq, &len))) {
@@ -71,7 +80,7 @@ void vp_modern_avq_done(struct virtqueue *vq)
complete(&cmd->completion);
}
} while (!virtqueue_enable_cb(vq));
- spin_unlock_irqrestore(&admin_vq->lock, flags);
+ mutex_unlock(&admin_vq->lock);
}
static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq,
@@ -82,7 +91,6 @@ static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq,
struct virtio_admin_cmd *cmd)
{
struct virtqueue *vq;
- unsigned long flags;
int ret;
vq = admin_vq->info->vq;
@@ -100,26 +108,26 @@ static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq,
if (virtqueue_is_broken(vq))
return -EIO;
- spin_lock_irqsave(&admin_vq->lock, flags);
+ mutex_lock(&admin_vq->lock);
ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_KERNEL);
if (ret < 0) {
if (ret == -ENOSPC) {
- spin_unlock_irqrestore(&admin_vq->lock, flags);
- cpu_relax();
+ mutex_unlock(&admin_vq->lock);
+ cond_resched();
goto again;
}
goto unlock_err;
}
if (!virtqueue_kick(vq))
goto unlock_err;
- spin_unlock_irqrestore(&admin_vq->lock, flags);
+ mutex_unlock(&admin_vq->lock);
wait_for_completion(&cmd->completion);
return cmd->ret;
unlock_err:
- spin_unlock_irqrestore(&admin_vq->lock, flags);
+ mutex_unlock(&admin_vq->lock);
return -EIO;
}
@@ -344,20 +352,27 @@ static void vp_modern_avq_activate(struct virtio_device *vdev)
static void vp_modern_avq_cleanup(struct virtio_device *vdev)
{
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
+ struct virtio_pci_admin_vq *admin_vq;
struct virtio_admin_cmd *cmd;
struct virtqueue *vq;
+ admin_vq = &vp_dev->admin_vq;
+
if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
return;
- vq = vp_dev->admin_vq.info->vq;
+ vq = admin_vq->info->vq;
if (!vq)
return;
+ cancel_work_sync(&admin_vq->work);
+
+ mutex_lock(&admin_vq->lock);
while ((cmd = virtqueue_detach_unused_buf(vq))) {
cmd->ret = -EIO;
complete(&cmd->completion);
}
+ mutex_unlock(&admin_vq->lock);
}
static void vp_modern_del_vqs(struct virtio_device *vdev)
@@ -1285,8 +1300,9 @@ int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
vp_dev->avq_index = vp_avq_index;
vp_dev->isr = mdev->isr;
vp_dev->vdev.id = mdev->id;
+ mutex_init(&vp_dev->admin_vq.lock);
+ INIT_WORK(&vp_dev->admin_vq.work, vp_modern_avq_work);
- spin_lock_init(&vp_dev->admin_vq.lock);
return 0;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 02/14] virtio_pci: Fix sleeping under spinlock in admin command path
2026-12-02 15:04 ` [PATCH net-next v24 02/14] virtio_pci: Fix sleeping under spinlock in admin command path Shahar Shitrit
@ 2026-12-03 15:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] When virtqueue_add_sgs() returns -ENOSPC,
virtqueue_exec_admin_cmd() keeps polling: mutex_unlock(),…
- [Low] Suppose the device has finished an admin command: it has written
the status/result buffers and the used-ring entry, and the IRQ has…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is a high-
impact race on the path this patch reworks.
--
virtio_pci: Fix sleeping under spinlock in admin command path
This patch replaces the admin virtqueue spinlock with a mutex, so that
virtqueue_exec_admin_cmd() can call virtqueue_add_sgs() with GFP_KERNEL.
The virtqueue callback runs in interrupt context, so the used-buffer
processing in vp_modern_avq_done() moves to a work item,
vp_modern_avq_work(). The teardown paths cancel this work before the
admin vq is drained and deleted.
> diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
> index 3a97d0471dd27..d5ab2c11d0842 100644
> --- a/drivers/virtio/virtio_pci_modern.c
> +++ b/drivers/virtio/virtio_pci_modern.c
> @@ -47,13 +47,22 @@ static int vp_avq_index(struct virtio_device *vdev, u16 *index, u16 *num)
> void vp_modern_avq_done(struct virtqueue *vq)
> {
> struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
> - struct virtio_pci_admin_vq *admin_vq = &vp_dev->admin_vq;
> +
> + schedule_work(&vp_dev->admin_vq.work);
> +}
[ ... ]
> @@ -100,26 +108,26 @@ static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq,
> if (virtqueue_is_broken(vq))
> return -EIO;
>
> - spin_lock_irqsave(&admin_vq->lock, flags);
> + mutex_lock(&admin_vq->lock);
> ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_KERNEL);
> if (ret < 0) {
> if (ret == -ENOSPC) {
> - spin_unlock_irqrestore(&admin_vq->lock, flags);
> - cpu_relax();
> + mutex_unlock(&admin_vq->lock);
> + cond_resched();
> goto again;
> }
[Severity: Low]
Used descriptors are now reclaimed only in vp_modern_avq_work(). Could
this -ENOSPC retry loop end up busy-waiting on the same kworker it is
competing with?
Before this patch, vp_modern_avq_done() ran in hard IRQ context and
always preempted the poller. Now schedule_work() queues a SCHED_NORMAL
per-CPU work item on the CPU that took the interrupt. That work item
also needs admin_vq->lock, which the poller keeps taking and dropping.
If the poller runs on that CPU and never sleeps, progress depends on the
scheduler preempting it:
- a SCHED_FIFO/RR poller lets the kworker run only through RT
throttling or the fair dl_server
- a poller that is itself a work item in the same per-CPU pool holds
off the avq work until the CPU-intensive watchdog fires (about 10ms)
- with PREEMPT_NONE, the poller yields only once need_resched is set
Nothing wakes the submitter when descriptors are freed. This isn't a
hard deadlock, since tick preemption, the workqueue watchdog and RT
throttling all eventually let the kworker run.
Should the submitter sleep until vp_modern_avq_work() frees descriptors,
instead of polling with cond_resched()?
> goto unlock_err;
> }
> if (!virtqueue_kick(vq))
> goto unlock_err;
> - spin_unlock_irqrestore(&admin_vq->lock, flags);
> + mutex_unlock(&admin_vq->lock);
>
> wait_for_completion(&cmd->completion);
[Severity: High]
This is a pre-existing issue and was not introduced by this patch. Since
this path is being reworked, though: what stops a command from being
posted after vp_modern_avq_cleanup() has drained the queue?
virtqueue_exec_admin_cmd() reads admin_vq->info->vq and checks
virtqueue_is_broken() without holding admin_vq->lock. After it takes
the mutex, it never checks whether teardown has started.
Admin commands can also come from VF context. The vfio virtio variant
driver calls virtio_pci_admin_legacy_io_write() and the migration
helpers, and these go through virtio_pci_vf_get_pf_dev(). That helper
only checks that the PF's PCI driver is virtio_pci:
pf_vp_dev = pci_iov_get_pf_drvdata(pdev, &virtio_pci_driver);
It does not cover the virtio driver being unbound from the PF's virtio
device through sysfs, or a freeze. pci_disable_sriov() in
virtio_pci_remove() only covers PCI-level removal.
A possible interleaving:
CPU1 (PF teardown) CPU0 (VF admin command)
vp_modern_del_vqs()
vp_modern_avq_cleanup()
mutex_lock()
detach all buffers, -EIO
mutex_unlock()
virtqueue_exec_admin_cmd()
mutex_lock()
virtqueue_add_sgs()
virtqueue_kick()
mutex_unlock()
wait_for_completion()
vp_del_vqs()
vp_del_vq()
del_vq()
vring_del_virtqueue()
If CPU0 posts the buffer before vp_del_vq(), nothing ever completes it.
wait_for_completion() then blocks forever in TASK_UNINTERRUPTIBLE with
no timeout.
vring_del_virtqueue() does not take admin_vq->lock. If CPU0's add/kick
overlaps it, can CPU0 access freed vring memory?
Also, vp_del_vqs() never clears vp_dev->admin_vq.info. Would a VF
command issued after the unbind dereference the freed info through
admin_vq->info->vq?
The mutex added to vp_modern_avq_cleanup() serializes the detach loop
against the add. However, no shutting-down state is checked under the
lock. Should the teardown check and virtqueue_add_sgs() be in the same
critical section?
[ ... ]
> @@ -344,20 +352,27 @@ static void vp_modern_avq_activate(struct virtio_device *vdev)
> static void vp_modern_avq_cleanup(struct virtio_device *vdev)
> {
[ ... ]
> - vq = vp_dev->admin_vq.info->vq;
> + vq = admin_vq->info->vq;
> if (!vq)
> return;
>
> + cancel_work_sync(&admin_vq->work);
> +
> + mutex_lock(&admin_vq->lock);
> while ((cmd = virtqueue_detach_unused_buf(vq))) {
> cmd->ret = -EIO;
> complete(&cmd->completion);
> }
> + mutex_unlock(&admin_vq->lock);
[Severity: Low]
Can a command that the device already completed be reported as -EIO
here?
Suppose the device has written the status and the used-ring entry, and
the interrupt has queued vp_modern_avq_work(), but the work has not run
yet. cancel_work_sync() then throws that pending processing away instead
of running it.
virtqueue_detach_unused_buf_split() returns every desc_state[] entry
that has data set, including entries already in the used ring:
for (i = 0; i < vq->split.vring.num; i++) {
if (!vq->split.desc_state[i].data)
continue;
The completed command therefore gets cmd->ret = -EIO, and
vp_modern_admin_cmd_exec() skips reading the device's status.
Before this patch, the window was IRQ latency. It is now workqueue
latency.
Would flush_work(), or draining with virtqueue_get_buf() under the mutex
before the detach loop, let these commands complete with their real
result?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 03/14] virtio: Add config_op for admin commands
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
2026-12-02 15:04 ` [PATCH net-next v24 01/14] virtio_pci: Remove supported_caps cache and build assert Shahar Shitrit
2026-12-02 15:04 ` [PATCH net-next v24 02/14] virtio_pci: Fix sleeping under spinlock in admin command path Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:04 ` [PATCH net-next v24 04/14] virtio: Expose generic device capability operations Shahar Shitrit
` (11 subsequent siblings)
14 siblings, 1 reply; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
This will allow device drivers to issue administration commands.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
---
drivers/virtio/virtio_pci_modern.c | 2 ++
include/linux/virtio_config.h | 10 ++++++++++
2 files changed, 12 insertions(+)
diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
index d5ab2c11d0842..ceb386ab3a214 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -1254,6 +1254,7 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
.get_shm_region = vp_get_shm_region,
.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
+ .admin_cmd_exec = vp_modern_admin_cmd_exec,
};
static const struct virtio_config_ops virtio_pci_config_ops = {
@@ -1274,6 +1275,7 @@ static const struct virtio_config_ops virtio_pci_config_ops = {
.get_shm_region = vp_get_shm_region,
.disable_vq_and_reset = vp_modern_disable_vq_and_reset,
.enable_vq_after_reset = vp_modern_enable_vq_after_reset,
+ .admin_cmd_exec = vp_modern_admin_cmd_exec,
};
/* the PCI probing function */
diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
index 69f84ea85d71a..c816bb6ddb474 100644
--- a/include/linux/virtio_config.h
+++ b/include/linux/virtio_config.h
@@ -108,6 +108,14 @@ struct virtqueue_info {
* Returns 0 on success or error status
* If disable_vq_and_reset is set, then enable_vq_after_reset must also be
* set.
+ * @admin_cmd_exec: Execute an admin VQ command (optional).
+ * vdev: the virtio_device
+ * cmd: the command to execute
+ * Returns 0 on success or error status
+ * May sleep, so process context only. Blocks until the device completes
+ * the command, or until del_vqs() fails it with -EIO. Returns -EIO if
+ * the admin VQ is broken. Must only be called after find_vqs() and
+ * before del_vqs(), as the admin VQ does not exist otherwise.
*/
struct virtio_config_ops {
void (*get)(struct virtio_device *vdev, unsigned offset,
@@ -137,6 +145,8 @@ struct virtio_config_ops {
struct virtio_shm_region *region, u8 id);
int (*disable_vq_and_reset)(struct virtqueue *vq);
int (*enable_vq_after_reset)(struct virtqueue *vq);
+ int (*admin_cmd_exec)(struct virtio_device *vdev,
+ struct virtio_admin_cmd *cmd);
};
/**
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 03/14] virtio: Add config_op for admin commands
2026-12-02 15:04 ` [PATCH net-next v24 03/14] virtio: Add config_op for admin commands Shahar Shitrit
@ 2026-12-03 15:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 (2) · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The commit message says the new op 'will allow device drivers
to issue administration commands'.
- [Low] virtqueue_exec_admin_cmd() returns cmd->ret after
wait_for_completion() (drivers/virtio/virtio_pci_modern.c:125-127),…
- [Low] The new kerneldoc says admin_cmd_exec 'Must only be called after
find_vqs() and before del_vqs()'.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch
exposes it to every virtio driver and documents a contract it breaks.
- [High] This problem wasn't introduced by this patch, but the new
kerneldoc misdescribes it and the series' new callers make it a…
--
virtio: Add config_op for admin commands
This commit adds an optional admin_cmd_exec() callback to struct
virtio_config_ops, with kerneldoc describing its contract. It wires the
callback to vp_modern_admin_cmd_exec() in both virtio-pci modern config ops
tables, so that virtio device drivers can issue admin VQ commands.
> diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c
> index d5ab2c11d0842..ceb386ab3a214 100644
> --- a/drivers/virtio/virtio_pci_modern.c
> +++ b/drivers/virtio/virtio_pci_modern.c
> @@ -1254,6 +1254,7 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
> .get_shm_region = vp_get_shm_region,
> .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
> .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
> + .admin_cmd_exec = vp_modern_admin_cmd_exec,
> };
[Severity: Medium]
The commit message says:
This will allow device drivers to issue administration commands.
Does the transport behind this op match that description?
virtqueue_exec_admin_cmd() rejects every opcode other than LIST_QUERY and
LIST_USE that is missing from supported_cmds:
if (opcode != VIRTIO_ADMIN_CMD_LIST_QUERY &&
opcode != VIRTIO_ADMIN_CMD_LIST_USE &&
!((1ULL << opcode) & admin_vq->supported_cmds))
return -EOPNOTSUPP;
Only virtio_pci_admin_cmd_list_init() writes supported_cmds. It queries and
enables commands for the SR-IOV group only, and masks the result with the
transport-private VIRTIO_ADMIN_CMD_BITMAP:
cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
...
*data &= cpu_to_le64(VIRTIO_ADMIN_CMD_BITMAP);
...
vp_dev->admin_vq.supported_cmds = le64_to_cpu(*data);
Later in the series, the virtio_net flow-filter user sends all of its
commands with VIRTIO_ADMIN_GROUP_TYPE_SELF. That covers
virtio_admin_cap_get(), virtio_admin_cap_set(), virtio_admin_obj_create()
and virtio_admin_obj_destroy(). The SELF group is never queried, and never
enabled with LIST_USE.
Take a device that has an admin VQ but no SR-IOV group, such as a VF or a
non-SR-IOV device assigned to a guest. If the SRIOV LIST_QUERY fails or
returns an empty list, supported_cmds stays 0. Wouldn't every SELF-group
command then fail with -EOPNOTSUPP?
One example is virtnet_ff_init()->virtio_admin_cap_id_list_query().
virtio_net treats -EOPNOTSUPP as "not supported" and drops the feature
without any message.
The op is also installed unconditionally, so a non-NULL admin_cmd_exec
doesn't mean admin commands work. vp_modern_admin_cmd_exec() returns
-EOPNOTSUPP when VIRTIO_F_ADMIN_VQ isn't negotiated.
Neither the commit message nor the new kerneldoc mentions this gating or
the -EOPNOTSUPP return. Could both be documented?
[ ... ]
> diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
> index 69f84ea85d71a..c816bb6ddb474 100644
> --- a/include/linux/virtio_config.h
> +++ b/include/linux/virtio_config.h
> @@ -108,6 +108,14 @@ struct virtqueue_info {
> * Returns 0 on success or error status
> * If disable_vq_and_reset is set, then enable_vq_after_reset must also be
> * set.
> + * @admin_cmd_exec: Execute an admin VQ command (optional).
> + * vdev: the virtio_device
> + * cmd: the command to execute
> + * Returns 0 on success or error status
[Severity: Low]
Is a return of 0 on success actually guaranteed? virtqueue_exec_admin_cmd()
initializes cmd->completion itself, but it returns cmd->ret, and nothing
writes that field on the success path:
init_completion(&cmd->completion);
...
wait_for_completion(&cmd->completion);
return cmd->ret;
vp_modern_avq_work() only sets result_sg_size and calls complete(). The only
writer of cmd->ret is vp_modern_avq_cleanup(), which sets it to -EIO.
Suppose a caller kmalloc()s or stack-allocates cmd without zeroing it. A
successful command would then be reported as a random error.
vp_modern_admin_cmd_exec() would also take the err_cmd_exec path and log
"Failed to execute command on admin vq".
All current callers use struct virtio_admin_cmd cmd = {}, including the
in-series virtio_admin_commands.c helpers, so none is affected today.
Should virtqueue_exec_admin_cmd() set cmd->ret = 0 next to
init_completion(), or should the kerneldoc require a zeroed cmd?
> + * May sleep, so process context only. Blocks until the device completes
> + * the command, or until del_vqs() fails it with -EIO. Returns -EIO if
[Severity: High]
This isn't a bug introduced by this patch, but the patch exposes it to every
virtio driver and documents a contract that the kick failure path doesn't
follow.
In virtqueue_exec_admin_cmd(), once virtqueue_add_sgs() succeeds, cmd and the
hdr, status, data and result buffers are on the ring. If virtqueue_kick()
then fails, the function returns without detaching that buffer:
ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_KERNEL);
...
if (!virtqueue_kick(vq))
goto unlock_err;
...
unlock_err:
mutex_unlock(&admin_vq->lock);
return -EIO;
virtqueue_kick() fails when vq->broken is set. One way that happens is
virtio_pci_remove() calling virtio_break_device() on surprise removal.
After the failure, vp_modern_admin_cmd_exec() frees va_hdr and va_status.
The caller's stack-allocated struct virtio_admin_cmd also goes out of scope,
for example in virtio_admin_obj_create().
vp_modern_avq_work() can't reap the token, because virtqueue_get_buf()
returns NULL on a broken vq. virtqueue_detach_unused_buf_split() doesn't
check broken, though, so del_vqs()->vp_modern_avq_cleanup() later runs:
while ((cmd = virtqueue_detach_unused_buf(vq))) {
cmd->ret = -EIO;
complete(&cmd->completion);
}
Can this write into a dead stack frame? complete() would also take a
spinlock and walk a wait list in that stale memory.
The final series has a concrete trigger. An ethtool -N or -U request goes
through virtio_admin_obj_create() or virtio_admin_obj_destroy() under rtnl.
If the device is surprise-removed while that request is between add_sgs and
kick, this path follows:
virtio_pci_remove()
virtio_break_device()
unregister_virtio_device()
virtnet_remove()
remove_vq_common()
del_vqs()
vp_modern_avq_cleanup()
This behaviour dates back to commit 4c3b54af907e ("virtio_pci_modern: use
completion instead of busy loop to wait on admin cmd result").
> + * the admin VQ is broken. Must only be called after find_vqs() and
[Severity: High]
This is a pre-existing issue, but does "Returns -EIO if the admin VQ is
broken" hold for commands that are already in flight?
virtqueue_exec_admin_cmd() checks virtqueue_is_broken() only before it
submits the command. After that it waits uninterruptibly with no timeout:
again:
if (virtqueue_is_broken(vq))
return -EIO;
...
wait_for_completion(&cmd->completion);
virtio_break_device() only sets vq->broken and doesn't complete outstanding
admin commands. The only wakeup is vp_modern_avq_cleanup() during del_vqs().
That seems to conflict with this comment in virtio_pci_remove():
* Device is marked broken on surprise removal so that virtio upper
* layers can abort any ongoing operation.
For comparison, the virtio_net control VQ wait polls virtqueue_is_broken().
In the final series, virtio_net issues admin commands under rtnl_lock: from
ethtool set_rxnfc, and from virtnet_ff_init() in probe and restore. If the
device stops completing a command, the caller blocks forever while holding
rtnl. That can happen on surprise removal, or with a hung device or
firmware. On surprise removal:
virtio_pci_remove()
virtio_break_device() <- no wakeup for the waiter
unregister_virtio_device()
virtnet_remove()
unregister_netdev() <- needs rtnl, held by the waiter
...
remove_vq_common() <- del_vqs(), never reached
Can this deadlock the removal path along with every other rtnl user? The
untimed wait also dates back to commit 4c3b54af907e.
> + * before del_vqs(), as the admin VQ does not exist otherwise.
[Severity: Low]
Is the find_vqs() to del_vqs() window the right precondition to document?
The comment above virtio_device_ready() says:
* Driver must call this to use vqs in the probe function.
The transport also fills in supported_cmds only at DRIVER_OK:
vp_set_status()
vp_modern_avq_activate()
virtio_pci_admin_cmd_list_init()
Suppose a driver follows this doc and calls the op in probe, between
find_vqs() and virtio_device_ready(). On first probe supported_cmds is still
0, so commands the device supports would fail with -EOPNOTSUPP.
supported_cmds is never cleared on reset or del_vqs(), so on restore it
still holds the value from before suspend. A command could then pass the
gate and be kicked before DRIVER_OK. The device isn't required to process
it, so could the restore thread block forever in wait_for_completion()
before it gets to set DRIVER_OK?
The in-series virtio_net callers run virtnet_ff_init() only after
virtio_device_ready(), so they aren't affected. Would "after DRIVER_OK" be
a more accurate precondition?
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 04/14] virtio: Expose generic device capability operations
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (2 preceding siblings ...)
2026-12-02 15:04 ` [PATCH net-next v24 03/14] virtio: Add config_op for admin commands Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:04 ` [PATCH net-next v24 05/14] virtio: Expose object create and destroy API Shahar Shitrit
` (10 subsequent siblings)
14 siblings, 1 reply; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
Currently querying and setting capabilities is restricted to a single
capability and contained within the virtio PCI driver. However, each
device type has generic and device specific capabilities, that may be
queried and set. In subsequent patches virtio_net will query and set
flow filter capabilities.
This changes the size of virtio_admin_cmd_query_cap_id_result. It's safe
to do because this data is written by DMA, so a newer controller can't
overrun the size on an older kernel. Since the API does not report how
much of the result was written, callers must zero-initialize the
structure before use. Any portion not written by the device then remains
zero and indicates that those capabilities are not supported.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
---
drivers/virtio/Makefile | 2 +-
drivers/virtio/virtio_admin_commands.c | 100 +++++++++++++++++++++++++
include/linux/virtio_admin.h | 89 ++++++++++++++++++++++
include/uapi/linux/virtio_pci.h | 6 +-
4 files changed, 194 insertions(+), 3 deletions(-)
create mode 100644 drivers/virtio/virtio_admin_commands.c
create mode 100644 include/linux/virtio_admin.h
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
index eefcfe90d6b8b..2b4a204dde331 100644
--- a/drivers/virtio/Makefile
+++ b/drivers/virtio/Makefile
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
-obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o
+obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o virtio_admin_commands.o
obj-$(CONFIG_VIRTIO_ANCHOR) += virtio_anchor.o
obj-$(CONFIG_VIRTIO_PCI_LIB) += virtio_pci_modern_dev.o
obj-$(CONFIG_VIRTIO_PCI_LIB_LEGACY) += virtio_pci_legacy_dev.o
diff --git a/drivers/virtio/virtio_admin_commands.c b/drivers/virtio/virtio_admin_commands.c
new file mode 100644
index 0000000000000..d4bf8c48e1d91
--- /dev/null
+++ b/drivers/virtio/virtio_admin_commands.c
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/virtio.h>
+#include <linux/virtio_config.h>
+#include <linux/virtio_admin.h>
+#include <linux/overflow.h>
+#include <uapi/linux/virtio_pci.h>
+
+int virtio_admin_cap_id_list_query(struct virtio_device *vdev,
+ struct virtio_admin_cmd_query_cap_id_result *data)
+{
+ struct virtio_admin_cmd cmd = {};
+ struct scatterlist result_sg;
+
+ if (!vdev->config->admin_cmd_exec)
+ return -EOPNOTSUPP;
+
+ sg_init_one(&result_sg, data, sizeof(*data));
+ cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY);
+ cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SELF);
+ cmd.result_sg = &result_sg;
+
+ return vdev->config->admin_cmd_exec(vdev, &cmd);
+}
+EXPORT_SYMBOL_GPL(virtio_admin_cap_id_list_query);
+
+int virtio_admin_cap_get(struct virtio_device *vdev,
+ u16 id,
+ void *caps,
+ size_t cap_size)
+{
+ struct virtio_admin_cmd_cap_get_data *data;
+ struct virtio_admin_cmd cmd = {};
+ struct scatterlist result_sg;
+ struct scatterlist data_sg;
+ int err;
+
+ if (!vdev->config->admin_cmd_exec)
+ return -EOPNOTSUPP;
+
+ data = kzalloc_obj(*data);
+ if (!data)
+ return -ENOMEM;
+
+ data->id = cpu_to_le16(id);
+ sg_init_one(&data_sg, data, sizeof(*data));
+ sg_init_one(&result_sg, caps, cap_size);
+ cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_DEVICE_CAP_GET);
+ cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SELF);
+ cmd.data_sg = &data_sg;
+ cmd.result_sg = &result_sg;
+
+ err = vdev->config->admin_cmd_exec(vdev, &cmd);
+ kfree(data);
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(virtio_admin_cap_get);
+
+int virtio_admin_cap_set(struct virtio_device *vdev,
+ u16 id,
+ const void *caps,
+ size_t cap_size)
+{
+ struct virtio_admin_cmd_cap_set_data *data;
+ struct virtio_admin_cmd cmd = {};
+ struct scatterlist data_sg;
+ size_t data_size;
+ int err;
+
+ if (!vdev->config->admin_cmd_exec)
+ return -EOPNOTSUPP;
+
+ if (check_add_overflow(sizeof(*data), cap_size, &data_size))
+ return -EOVERFLOW;
+
+ data = kzalloc(data_size, GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;
+
+ data->id = cpu_to_le16(id);
+ memcpy(data->cap_specific_data, caps, cap_size);
+ sg_init_one(&data_sg, data, data_size);
+ cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_DRIVER_CAP_SET);
+ cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SELF);
+ cmd.data_sg = &data_sg;
+ cmd.result_sg = NULL;
+
+ err = vdev->config->admin_cmd_exec(vdev, &cmd);
+ kfree(data);
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(virtio_admin_cap_set);
+
+MODULE_DESCRIPTION("Virtio admin command helpers");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/virtio_admin.h b/include/linux/virtio_admin.h
new file mode 100644
index 0000000000000..43ea2dbd48b7e
--- /dev/null
+++ b/include/linux/virtio_admin.h
@@ -0,0 +1,89 @@
+/* SPDX-License-Identifier: GPL-2.0-only
+ *
+ * Header file for virtio admin operations
+ */
+
+#ifndef _LINUX_VIRTIO_ADMIN_H
+#define _LINUX_VIRTIO_ADMIN_H
+
+#include <linux/bug.h>
+#include <linux/types.h>
+#include <linux/byteorder/generic.h>
+#include <uapi/linux/virtio_pci.h>
+
+struct virtio_device;
+
+/**
+ * virtio_cap_in_list - Check if a capability is supported in the capability list
+ * @cap_list: Pointer to capability list structure containing supported_caps array
+ * @cap: Capability ID to check
+ *
+ * The cap_list contains a supported_caps array of little-endian 64-bit integers
+ * where each bit represents a capability. Bit 0 of the first element represents
+ * capability ID 0, bit 1 represents capability ID 1, and so on.
+ *
+ * Return: true if capability is supported, false otherwise
+ */
+static inline bool virtio_cap_in_list(
+ const struct virtio_admin_cmd_query_cap_id_result *cap_list, u16 cap)
+{
+ BUILD_BUG_ON(cap > VIRTIO_ADMIN_MAX_CAP);
+ return !!(1 & (le64_to_cpu(cap_list->supported_caps[cap / 64]) >>
+ (cap % 64)));
+}
+
+/**
+ * virtio_admin_cap_id_list_query - Query the list of available capability IDs
+ * @vdev: The virtio device to query
+ * @data: Pointer to result structure (must be zero-initialized and heap allocated)
+ *
+ * This function queries the virtio device for the list of available capability
+ * IDs that can be used with virtio_admin_cap_get() and virtio_admin_cap_set().
+ * The result is stored in the provided data structure.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the device doesn't support admin
+ * operations or capability queries, or a negative error code on other failures.
+ */
+int virtio_admin_cap_id_list_query(struct virtio_device *vdev,
+ struct virtio_admin_cmd_query_cap_id_result *data);
+
+/**
+ * virtio_admin_cap_get - Get capability data for a specific capability ID
+ * @vdev: The virtio device
+ * @id: Capability ID to retrieve
+ * @caps: Pointer to capability data structure (must be zero-initialized and heap allocated)
+ * @cap_size: Size of the capability data structure
+ *
+ * This function retrieves a specific capability from the virtio device.
+ * The capability data is stored in the provided buffer. The caller must
+ * ensure the buffer is large enough to hold the capability data.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the device doesn't support admin
+ * operations or capability retrieval, or a negative error code on other failures.
+ */
+int virtio_admin_cap_get(struct virtio_device *vdev,
+ u16 id,
+ void *caps,
+ size_t cap_size);
+
+/**
+ * virtio_admin_cap_set - Set capability data for a specific capability ID
+ * @vdev: The virtio device
+ * @id: Capability ID to set
+ * @caps: Pointer to capability data structure (must be heap allocated)
+ * @cap_size: Size of the capability data structure
+ *
+ * This function sets a specific capability on the virtio device.
+ * The capability data is read from the provided buffer and applied
+ * to the device. The device may validate the capability data before
+ * applying it.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the device doesn't support admin
+ * operations or capability setting, or a negative error code on other failures.
+ */
+int virtio_admin_cap_set(struct virtio_device *vdev,
+ u16 id,
+ const void *caps,
+ size_t cap_size);
+
+#endif /* _LINUX_VIRTIO_ADMIN_H */
diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index e732e3456e275..96d097d3757e4 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -315,15 +315,17 @@ struct virtio_admin_cmd_notify_info_result {
#define VIRTIO_DEV_PARTS_CAP 0x0000
+#define VIRTIO_ADMIN_MAX_CAP 0x0fff
+
struct virtio_dev_parts_cap {
__u8 get_parts_resource_objects_limit;
__u8 set_parts_resource_objects_limit;
};
-#define MAX_CAP_ID __KERNEL_DIV_ROUND_UP(VIRTIO_DEV_PARTS_CAP + 1, 64)
+#define VIRTIO_ADMIN_CAP_ID_ARRAY_SIZE __KERNEL_DIV_ROUND_UP(VIRTIO_ADMIN_MAX_CAP + 1, 64)
struct virtio_admin_cmd_query_cap_id_result {
- __le64 supported_caps[MAX_CAP_ID];
+ __le64 supported_caps[VIRTIO_ADMIN_CAP_ID_ARRAY_SIZE];
};
struct virtio_admin_cmd_cap_get_data {
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 04/14] virtio: Expose generic device capability operations
2026-12-02 15:04 ` [PATCH net-next v24 04/14] virtio: Expose generic device capability operations Shahar Shitrit
@ 2026-12-03 15:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 3
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] This patch exports new admin helpers for every virtio driver:
virtio_admin_cap_id_list_query(), virtio_admin_cap_get() and…
- [Low] virtio_cap_in_list() in include/linux/virtio_admin.h is a plain
'static inline' function.
- [Low] In include/uapi/linux/virtio_pci.h the patch deletes the exported
UAPI macro MAX_CAP_ID ('#define MAX_CAP_ID…
- [Low] The kernel-doc for virtio_admin_cap_set() in
include/linux/virtio_admin.h (line 73) says '@caps: Pointer to…
--
virtio: Expose generic device capability operations
This commit adds generic helpers for virtio admin capability commands:
virtio_admin_cap_id_list_query(), virtio_admin_cap_get() and
virtio_admin_cap_set(). It also adds a virtio_cap_in_list() helper. The
UAPI struct virtio_admin_cmd_query_cap_id_result grows so that it covers
capability IDs up to VIRTIO_ADMIN_MAX_CAP.
> diff --git a/drivers/virtio/virtio_admin_commands.c b/drivers/virtio/virtio_admin_commands.c
> new file mode 100644
> index 0000000000000..d4bf8c48e1d91
> --- /dev/null
> +++ b/drivers/virtio/virtio_admin_commands.c
[ ... ]
> +int virtio_admin_cap_id_list_query(struct virtio_device *vdev,
> + struct virtio_admin_cmd_query_cap_id_result *data)
> +{
> + struct virtio_admin_cmd cmd = {};
> + struct scatterlist result_sg;
> +
> + if (!vdev->config->admin_cmd_exec)
> + return -EOPNOTSUPP;
> +
> + sg_init_one(&result_sg, data, sizeof(*data));
> + cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY);
> + cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SELF);
[Severity: Medium]
How does this SELF-group command get past the opcode check on virtio-pci?
virtio_admin_cap_get() and virtio_admin_cap_set() have the same pattern
with DEVICE_CAP_GET and DRIVER_CAP_SET.
On virtio-pci, virtqueue_exec_admin_cmd() checks every opcode except
LIST_QUERY and LIST_USE against a single bitmap. It does not look at
cmd->group_type:
drivers/virtio/virtio_pci_modern.c:virtqueue_exec_admin_cmd() {
...
if (opcode != VIRTIO_ADMIN_CMD_LIST_QUERY &&
opcode != VIRTIO_ADMIN_CMD_LIST_USE &&
!((1ULL << opcode) & admin_vq->supported_cmds))
return -EOPNOTSUPP;
...
}
The only place that fills supported_cmds is virtio_pci_admin_cmd_list_init(),
called from vp_modern_avq_activate() at DRIVER_OK. It negotiates only the
SRIOV group:
drivers/virtio/virtio_pci_modern.c:virtio_pci_admin_cmd_list_init() {
...
cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_LIST_QUERY);
cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
...
vp_dev->admin_vq.supported_cmds = le64_to_cpu(*data);
...
}
Take a device with an admin VQ that is not an SR-IOV group owner, such as
a VF or a standalone virtio-net device. LIST_QUERY for the SRIOV group
would fail or come back empty there, so supported_cmds stays 0. Would all
three new helpers then return -EOPNOTSUPP, even when the device supports
these commands for the SELF group?
The later virtio_net patch, "virtio_net: Query and set flow filter caps",
has virtnet_ff_init() treat -EOPNOTSUPP as "not supported". So flow
filtering would be silently disabled on those devices.
On a PF, these SELF-group commands are sent without ever being declared
via LIST_USE for the SELF group. Is it intended to rely on the device
accepting that?
No later patch in the series appears to change this negotiation. Should
LIST_QUERY and LIST_USE also be done for VIRTIO_ADMIN_GROUP_TYPE_SELF,
with the check in virtqueue_exec_admin_cmd() taking group_type into
account?
> + cmd.result_sg = &result_sg;
> +
> + return vdev->config->admin_cmd_exec(vdev, &cmd);
> +}
> +EXPORT_SYMBOL_GPL(virtio_admin_cap_id_list_query);
[ ... ]
> diff --git a/include/linux/virtio_admin.h b/include/linux/virtio_admin.h
> new file mode 100644
> index 0000000000000..43ea2dbd48b7e
> --- /dev/null
> +++ b/include/linux/virtio_admin.h
[ ... ]
> +static inline bool virtio_cap_in_list(
> + const struct virtio_admin_cmd_query_cap_id_result *cap_list, u16 cap)
> +{
> + BUILD_BUG_ON(cap > VIRTIO_ADMIN_MAX_CAP);
[Severity: Low]
Can this BUILD_BUG_ON() break the build when virtio_cap_in_list() is not
inlined?
cap is a runtime u16 parameter. In the kernel, a plain inline is not
__always_inline:
include/linux/compiler_types.h:
#define inline inline __gnu_inline __inline_maybe_unused notrace
If the compiler emits an out-of-line copy, for example with
CONFIG_CC_OPTIMIZE_FOR_SIZE or with instrumentation, cap can be anything
from 0 to 0xffff there. The __compiletime_assert() error call would then
survive, and the build would fail with "BUILD_BUG_ON failed: cap >
VIRTIO_ADMIN_MAX_CAP".
Any caller that passes a cap ID that is not a compile-time constant would
also fail to build. With no runtime bounds check, the helper cannot be
used for runtime IDs at all.
The callers in virtnet_ff_init() pass constants, so normal -O2 builds work
today. Would __always_inline, or a runtime range check, be more robust
here?
> + return !!(1 & (le64_to_cpu(cap_list->supported_caps[cap / 64]) >>
> + (cap % 64)));
> +}
[ ... ]
> +/**
> + * virtio_admin_cap_set - Set capability data for a specific capability ID
> + * @vdev: The virtio device
> + * @id: Capability ID to set
> + * @caps: Pointer to capability data structure (must be heap allocated)
[Severity: Low]
This isn't a bug, but is the "must be heap allocated" requirement accurate
for virtio_admin_cap_set()?
The implementation never DMA-maps caps. It copies caps into its own
kzalloc'd buffer and maps only that buffer:
drivers/virtio/virtio_admin_commands.c:virtio_admin_cap_set() {
...
memcpy(data->cap_specific_data, caps, cap_size);
sg_init_one(&data_sg, data, data_size);
...
}
So stack or const buffers would work. In virtio_admin_cap_get(), the
caller's buffer really is DMA-mapped, so the two helpers have different
contracts while the docs describe them the same way.
Also, virtio_pci_modern.c keeps open-coded copies of the same
CAP_ID_LIST_QUERY, DEVICE_CAP_GET and DRIVER_CAP_SET sequences in
virtio_pci_admin_cmd_cap_init() and
virtio_pci_admin_cmd_dev_parts_objects_enable(). Could those be converted
to the new helpers?
> + * @cap_size: Size of the capability data structure
[ ... ]
> diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
> index e732e3456e275..96d097d3757e4 100644
> --- a/include/uapi/linux/virtio_pci.h
> +++ b/include/uapi/linux/virtio_pci.h
> @@ -315,15 +315,17 @@ struct virtio_admin_cmd_notify_info_result {
>
> #define VIRTIO_DEV_PARTS_CAP 0x0000
>
> +#define VIRTIO_ADMIN_MAX_CAP 0x0fff
> +
> struct virtio_dev_parts_cap {
> __u8 get_parts_resource_objects_limit;
> __u8 set_parts_resource_objects_limit;
> };
>
> -#define MAX_CAP_ID __KERNEL_DIV_ROUND_UP(VIRTIO_DEV_PARTS_CAP + 1, 64)
> +#define VIRTIO_ADMIN_CAP_ID_ARRAY_SIZE __KERNEL_DIV_ROUND_UP(VIRTIO_ADMIN_MAX_CAP + 1, 64)
[Severity: Low]
Could removing MAX_CAP_ID break userspace builds?
It is an exported UAPI macro, and it is dropped here without a
compatibility alias. Userspace that syncs the kernel UAPI headers and uses
MAX_CAP_ID, such as VMMs or device emulators, would stop compiling.
The commit message explains the size change of
struct virtio_admin_cmd_query_cap_id_result, but not the removal of this
name. Would it make sense to keep MAX_CAP_ID defined, or at least mention
the removal in the commit message?
>
> struct virtio_admin_cmd_query_cap_id_result {
> - __le64 supported_caps[MAX_CAP_ID];
> + __le64 supported_caps[VIRTIO_ADMIN_CAP_ID_ARRAY_SIZE];
> };
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 05/14] virtio: Expose object create and destroy API
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (3 preceding siblings ...)
2026-12-02 15:04 ` [PATCH net-next v24 04/14] virtio: Expose generic device capability operations Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:04 ` [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps Shahar Shitrit
` (9 subsequent siblings)
14 siblings, 1 reply; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
Object create and destroy were implemented specifically for dev parts
device objects. Create general purpose APIs for use by upper layer
drivers.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
---
drivers/virtio/virtio_admin_commands.c | 77 ++++++++++++++++++++++++++
include/linux/virtio_admin.h | 44 +++++++++++++++
2 files changed, 121 insertions(+)
diff --git a/drivers/virtio/virtio_admin_commands.c b/drivers/virtio/virtio_admin_commands.c
index d4bf8c48e1d91..c53c26de91975 100644
--- a/drivers/virtio/virtio_admin_commands.c
+++ b/drivers/virtio/virtio_admin_commands.c
@@ -96,5 +96,82 @@ int virtio_admin_cap_set(struct virtio_device *vdev,
}
EXPORT_SYMBOL_GPL(virtio_admin_cap_set);
+int virtio_admin_obj_create(struct virtio_device *vdev,
+ u16 obj_type,
+ u32 obj_id,
+ u16 group_type,
+ u64 group_member_id,
+ const void *obj_specific_data,
+ size_t obj_specific_data_size)
+{
+ size_t data_size = sizeof(struct virtio_admin_cmd_resource_obj_create_data);
+ struct virtio_admin_cmd_resource_obj_create_data *obj_create_data;
+ struct virtio_admin_cmd cmd = {};
+ struct scatterlist data_sg;
+ void *data;
+ int err;
+
+ if (!vdev->config->admin_cmd_exec)
+ return -EOPNOTSUPP;
+
+ if (check_add_overflow(data_size, obj_specific_data_size, &data_size))
+ return -EOVERFLOW;
+
+ data = kzalloc(data_size, GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;
+
+ obj_create_data = data;
+ obj_create_data->hdr.type = cpu_to_le16(obj_type);
+ obj_create_data->hdr.id = cpu_to_le32(obj_id);
+ memcpy(obj_create_data->resource_obj_specific_data, obj_specific_data,
+ obj_specific_data_size);
+ sg_init_one(&data_sg, data, data_size);
+
+ cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE);
+ cmd.group_type = cpu_to_le16(group_type);
+ cmd.group_member_id = cpu_to_le64(group_member_id);
+ cmd.data_sg = &data_sg;
+
+ err = vdev->config->admin_cmd_exec(vdev, &cmd);
+ kfree(data);
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(virtio_admin_obj_create);
+
+int virtio_admin_obj_destroy(struct virtio_device *vdev,
+ u16 obj_type,
+ u32 obj_id,
+ u16 group_type,
+ u64 group_member_id)
+{
+ struct virtio_admin_cmd_resource_obj_cmd_hdr *data;
+ struct virtio_admin_cmd cmd = {};
+ struct scatterlist data_sg;
+ int err;
+
+ if (!vdev->config->admin_cmd_exec)
+ return -EOPNOTSUPP;
+
+ data = kzalloc_obj(*data);
+ if (!data)
+ return -ENOMEM;
+
+ data->type = cpu_to_le16(obj_type);
+ data->id = cpu_to_le32(obj_id);
+ sg_init_one(&data_sg, data, sizeof(*data));
+ cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_DESTROY);
+ cmd.group_type = cpu_to_le16(group_type);
+ cmd.group_member_id = cpu_to_le64(group_member_id);
+ cmd.data_sg = &data_sg;
+
+ err = vdev->config->admin_cmd_exec(vdev, &cmd);
+ kfree(data);
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(virtio_admin_obj_destroy);
+
MODULE_DESCRIPTION("Virtio admin command helpers");
MODULE_LICENSE("GPL");
diff --git a/include/linux/virtio_admin.h b/include/linux/virtio_admin.h
index 43ea2dbd48b7e..087987ebaa0cc 100644
--- a/include/linux/virtio_admin.h
+++ b/include/linux/virtio_admin.h
@@ -86,4 +86,48 @@ int virtio_admin_cap_set(struct virtio_device *vdev,
const void *caps,
size_t cap_size);
+/**
+ * virtio_admin_obj_create - Create an object on a virtio device
+ * @vdev: the virtio device
+ * @obj_type: type of object to create
+ * @obj_id: ID for the new object
+ * @group_type: administrative group type for the operation
+ * @group_member_id: member identifier within the administrative group
+ * @obj_specific_data: object-specific data for creation
+ * @obj_specific_data_size: size of the object-specific data in bytes
+ *
+ * Creates a new object on the virtio device with the specified type and ID.
+ * The object may require object-specific data for proper initialization.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the device doesn't support admin
+ * operations or object creation, or a negative error code on other failures.
+ */
+int virtio_admin_obj_create(struct virtio_device *vdev,
+ u16 obj_type,
+ u32 obj_id,
+ u16 group_type,
+ u64 group_member_id,
+ const void *obj_specific_data,
+ size_t obj_specific_data_size);
+
+/**
+ * virtio_admin_obj_destroy - Destroy an object on a virtio device
+ * @vdev: the virtio device
+ * @obj_type: type of object to destroy
+ * @obj_id: ID of the object to destroy
+ * @group_type: administrative group type for the operation
+ * @group_member_id: member identifier within the administrative group
+ *
+ * Destroys an existing object on the virtio device with the specified type
+ * and ID.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the device doesn't support admin
+ * operations, or a negative error code on other failures.
+ */
+int virtio_admin_obj_destroy(struct virtio_device *vdev,
+ u16 obj_type,
+ u32 obj_id,
+ u16 group_type,
+ u64 group_member_id);
+
#endif /* _LINUX_VIRTIO_ADMIN_H */
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 05/14] virtio: Expose object create and destroy API
2026-12-02 15:04 ` [PATCH net-next v24 05/14] virtio: Expose object create and destroy API Shahar Shitrit
@ 2026-12-03 15:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The new helpers virtio_admin_obj_create() and
virtio_admin_obj_destroy() in drivers/virtio/virtio_admin_commands.c…
- [Low] virtio_admin_obj_create()/destroy() accept any obj_type and
obj_id.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the new
helpers add two more exposed callers.
--
virtio: Expose object create and destroy API
This commit adds virtio_admin_obj_create() and virtio_admin_obj_destroy()
to drivers/virtio/virtio_admin_commands.c. They build the
RESOURCE_OBJ_CREATE and RESOURCE_OBJ_DESTROY admin commands from an object
type, id and group fields passed in by the caller. This lets upper layer
drivers use them outside the existing dev parts code.
> diff --git a/drivers/virtio/virtio_admin_commands.c b/drivers/virtio/virtio_admin_commands.c
> index d4bf8c48e1d91..c53c26de91975 100644
> --- a/drivers/virtio/virtio_admin_commands.c
> +++ b/drivers/virtio/virtio_admin_commands.c
> @@ -96,5 +96,82 @@ int virtio_admin_cap_set(struct virtio_device *vdev,
> }
> EXPORT_SYMBOL_GPL(virtio_admin_cap_set);
>
> +int virtio_admin_obj_create(struct virtio_device *vdev,
> + u16 obj_type,
> + u32 obj_id,
> + u16 group_type,
> + u64 group_member_id,
> + const void *obj_specific_data,
> + size_t obj_specific_data_size)
> +{
[ ... ]
> + obj_create_data = data;
> + obj_create_data->hdr.type = cpu_to_le16(obj_type);
> + obj_create_data->hdr.id = cpu_to_le32(obj_id);
[Severity: Low]
Should these helpers reject VIRTIO_RESOURCE_OBJ_DEV_PARTS, or at least
document that it must not be passed?
virtio_pci_admin_obj_create() and virtio_pci_admin_obj_destroy() in
drivers/virtio/virtio_pci_modern.c track ids for that object type in
admin_vq.dev_parts_ida:
virtio_pci_admin_obj_create() {
...
id = ida_alloc_range(&avq->dev_parts_ida, 0,
avq->max_dev_parts_objects - 1, GFP_KERNEL);
...
}
virtio_pci_admin_obj_destroy() {
...
if (!ret) {
vp_dev = to_vp_device(virtio_dev);
ida_free(&vp_dev->admin_vq.dev_parts_ida, id);
}
...
}
The new generic helpers accept any obj_type and obj_id. If a caller used
them with VIRTIO_RESOURCE_OBJ_DEV_PARTS, dev_parts_ida could fall out of
sync. A duplicate id could be handed out later, or ida_free() could warn
about an id it never allocated.
No caller in this series does this. The virtio_net users pass the
VIRTIO_NET_RESOURCE_OBJ_FF_* types with VIRTIO_ADMIN_GROUP_TYPE_SELF, so
this only affects the API contract.
> + memcpy(obj_create_data->resource_obj_specific_data, obj_specific_data,
> + obj_specific_data_size);
> + sg_init_one(&data_sg, data, data_size);
> +
> + cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE);
> + cmd.group_type = cpu_to_le16(group_type);
> + cmd.group_member_id = cpu_to_le64(group_member_id);
> + cmd.data_sg = &data_sg;
[Severity: Low]
This isn't a bug, but this builds the same RESOURCE_OBJ_CREATE payload
that virtio_pci_admin_obj_create() in drivers/virtio/virtio_pci_modern.c
already builds:
virtio_pci_admin_obj_create() {
...
cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE);
cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
cmd.group_member_id = cpu_to_le64(vf_id + 1);
...
}
virtio_pci_admin_obj_destroy() overlaps with virtio_admin_obj_destroy() in
the same way. The only extra work it does is the ida_free().
Could the PCI dev parts helpers call the new generic ones, passing
VIRTIO_ADMIN_GROUP_TYPE_SRIOV and vf_id + 1? At the end of the series,
both copies of the command-building code are still there.
> +
> + err = vdev->config->admin_cmd_exec(vdev, &cmd);
> + kfree(data);
[Severity: High]
This is a pre-existing issue, but these helpers add two more callers that
can hit it. What happens to cmd and data if virtqueue_kick() fails after
virtqueue_add_sgs() has succeeded?
drivers/virtio/virtio_pci_modern.c:virtqueue_exec_admin_cmd() {
...
if (!virtqueue_kick(vq))
goto unlock_err;
...
unlock_err:
mutex_unlock(&admin_vq->lock);
return -EIO;
}
On this path the descriptors stay published in the admin vq. They still
point at data, at va_hdr and va_status (which vp_modern_admin_cmd_exec()
frees), and at the token cmd, which lives on this function's stack.
virtqueue_kick() returns false when vq->broken is set. virtio_break_device()
sets that flag without taking admin_vq->lock. One example is
virtio_pci_remove() on surprise removal, so the flag can be set between
the add and the kick.
After the -EIO return, this function frees data and returns, so the stack
frame holding cmd is gone. Later, on the remove path:
virtio_pci_remove()
unregister_virtio_device()
driver remove -> del_vqs()
vp_modern_del_vqs()
vp_modern_avq_cleanup()
vp_modern_avq_cleanup() {
...
while ((cmd = virtqueue_detach_unused_buf(vq))) {
cmd->ret = -EIO;
complete(&cmd->completion);
}
...
}
Doesn't this write to a stack frame that has already returned, and take
the completion spinlock inside it?
If the device is still alive after the break, could it also read or write
descriptors that now point at freed memory?
virtio_admin_obj_destroy() follows the same pattern. Should the kick
failure path in virtqueue_exec_admin_cmd() detach the buffer it just added
before returning -EIO?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (4 preceding siblings ...)
2026-12-02 15:04 ` [PATCH net-next v24 05/14] virtio: Expose object create and destroy API Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-03 13:16 ` Michael S. Tsirkin
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:04 ` [PATCH net-next v24 07/14] virtio_net: Create a FF group for ethtool steering Shahar Shitrit
` (8 subsequent siblings)
14 siblings, 2 replies; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
When probing a virtnet device, attempt to read the flow filter
capabilities. In order to use the feature the caps must also
be set. For now setting what was read is sufficient.
This patch adds uapi definitions virtio_net flow filters define in
version 1.4 of the VirtIO spec.
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
---
drivers/net/virtio_net.c | 237 +++++++++++++++++++++++++++++
include/uapi/linux/virtio_net_ff.h | 91 +++++++++++
2 files changed, 328 insertions(+)
create mode 100644 include/uapi/linux/virtio_net_ff.h
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 5eef2c3159638..03aea14da492e 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -27,6 +27,10 @@
#include <net/netdev_queues.h>
#include <net/xdp_sock_drv.h>
#include <net/page_pool/helpers.h>
+#include <linux/virtio_admin.h>
+#include <net/ipv6.h>
+#include <net/ip.h>
+#include <uapi/linux/virtio_net_ff.h>
static int napi_weight = NAPI_POLL_WEIGHT;
module_param(napi_weight, int, 0444);
@@ -284,6 +288,14 @@ static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
};
+struct virtnet_ff {
+ struct virtio_device *vdev;
+ bool ff_supported;
+ struct virtio_net_ff_cap_data *ff_caps;
+ struct virtio_net_ff_cap_mask_data *ff_mask;
+ struct virtio_net_ff_actions *ff_actions;
+};
+
#define VIRTNET_Q_TYPE_RX 0
#define VIRTNET_Q_TYPE_TX 1
#define VIRTNET_Q_TYPE_CQ 2
@@ -475,6 +487,8 @@ struct virtnet_info {
struct virtio_net_rss_config_hdr *rss_hdr;
+ struct virtnet_ff ff;
+
/* Must be last as it ends in a flexible-array member. */
TRAILING_OVERLAP(struct virtio_net_rss_config_trailer, rss_trailer, hash_key_data,
u8 rss_hash_key_data[NETDEV_RSS_KEY_LEN];
@@ -518,6 +532,7 @@ static struct sk_buff *virtnet_skb_append_frag(struct receive_queue *rq,
static void virtnet_xsk_completed(struct send_queue *sq, int num);
static void free_unused_bufs(struct virtnet_info *vi);
static void virtnet_del_vqs(struct virtnet_info *vi);
+static void remove_vq_common(struct virtnet_info *vi);
enum virtnet_xmit_type {
VIRTNET_XMIT_TYPE_SKB,
@@ -5794,6 +5809,199 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
.get_base_stats = virtnet_get_base_stats,
};
+static size_t get_mask_size(u16 type)
+{
+ switch (type) {
+ case VIRTIO_NET_FF_MASK_TYPE_ETH:
+ return sizeof(struct ethhdr);
+ case VIRTIO_NET_FF_MASK_TYPE_IPV4:
+ return sizeof(struct iphdr);
+ case VIRTIO_NET_FF_MASK_TYPE_IPV6:
+ return sizeof(struct ipv6hdr);
+ case VIRTIO_NET_FF_MASK_TYPE_TCP:
+ return sizeof(struct tcphdr);
+ case VIRTIO_NET_FF_MASK_TYPE_UDP:
+ return sizeof(struct udphdr);
+ }
+
+ return 0;
+}
+
+static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
+{
+ size_t ff_mask_size = sizeof(struct virtio_net_ff_cap_mask_data) +
+ sizeof(struct virtio_net_ff_selector) *
+ VIRTIO_NET_FF_MASK_TYPE_MAX;
+ struct virtio_admin_cmd_query_cap_id_result *cap_id_list;
+ struct virtio_net_ff_selector *sel;
+ unsigned long sel_types = 0;
+ size_t real_ff_mask_size;
+ int err;
+ int i;
+
+ if (!vdev->config->admin_cmd_exec)
+ return -EOPNOTSUPP;
+
+ cap_id_list = kzalloc(sizeof(*cap_id_list), GFP_KERNEL);
+ if (!cap_id_list)
+ return -ENOMEM;
+
+ err = virtio_admin_cap_id_list_query(vdev, cap_id_list);
+ if (err)
+ goto err_cap_list;
+
+ if (!(virtio_cap_in_list(cap_id_list, VIRTIO_NET_FF_RESOURCE_CAP) &&
+ virtio_cap_in_list(cap_id_list, VIRTIO_NET_FF_SELECTOR_CAP) &&
+ virtio_cap_in_list(cap_id_list, VIRTIO_NET_FF_ACTION_CAP))) {
+ err = -EOPNOTSUPP;
+ goto err_cap_list;
+ }
+
+ ff->ff_caps = kzalloc(sizeof(*ff->ff_caps), GFP_KERNEL);
+ if (!ff->ff_caps) {
+ err = -ENOMEM;
+ goto err_cap_list;
+ }
+
+ err = virtio_admin_cap_get(vdev,
+ VIRTIO_NET_FF_RESOURCE_CAP,
+ ff->ff_caps,
+ sizeof(*ff->ff_caps));
+
+ if (err)
+ goto err_ff;
+
+ if (!le32_to_cpu(ff->ff_caps->groups_limit) ||
+ !le32_to_cpu(ff->ff_caps->classifiers_limit) ||
+ !le32_to_cpu(ff->ff_caps->rules_limit) ||
+ !le32_to_cpu(ff->ff_caps->rules_per_group_limit) ||
+ !ff->ff_caps->selectors_per_classifier_limit) {
+ err = -EINVAL;
+ goto err_ff;
+ }
+
+ /* VIRTIO_NET_FF_MASK_TYPE start at 1 */
+ for (i = 1; i <= VIRTIO_NET_FF_MASK_TYPE_MAX; i++)
+ ff_mask_size += get_mask_size(i);
+
+ ff->ff_mask = kzalloc(ff_mask_size, GFP_KERNEL);
+ if (!ff->ff_mask) {
+ err = -ENOMEM;
+ goto err_ff;
+ }
+
+ err = virtio_admin_cap_get(vdev,
+ VIRTIO_NET_FF_SELECTOR_CAP,
+ ff->ff_mask,
+ ff_mask_size);
+
+ if (err)
+ goto err_ff_mask;
+
+ ff->ff_mask->count = min_t(u8, ff->ff_mask->count,
+ VIRTIO_NET_FF_MASK_TYPE_MAX);
+
+ ff->ff_actions = kzalloc(sizeof(*ff->ff_actions) +
+ VIRTIO_NET_FF_ACTION_MAX,
+ GFP_KERNEL);
+ if (!ff->ff_actions) {
+ err = -ENOMEM;
+ goto err_ff_mask;
+ }
+
+ err = virtio_admin_cap_get(vdev,
+ VIRTIO_NET_FF_ACTION_CAP,
+ ff->ff_actions,
+ sizeof(*ff->ff_actions) + VIRTIO_NET_FF_ACTION_MAX);
+
+ if (err)
+ goto err_ff_action;
+
+ ff->ff_actions->count = min_t(u8, ff->ff_actions->count,
+ VIRTIO_NET_FF_ACTION_MAX);
+ if (!ff->ff_actions->count) {
+ err = -EINVAL;
+ goto err_ff_action;
+ }
+
+ err = virtio_admin_cap_set(vdev,
+ VIRTIO_NET_FF_RESOURCE_CAP,
+ ff->ff_caps,
+ sizeof(*ff->ff_caps));
+ if (err)
+ goto err_ff_action;
+
+ real_ff_mask_size = sizeof(struct virtio_net_ff_cap_mask_data);
+ sel = (void *)&ff->ff_mask->selectors;
+
+ for (i = 0; i < ff->ff_mask->count; i++) {
+ /* If the selector type is unknown it may indicate the spec
+ * has been revised to include new types of selectors
+ */
+ if (!sel->type || sel->type > VIRTIO_NET_FF_MASK_TYPE_MAX)
+ break;
+
+ if (sel->length != get_mask_size(sel->type) ||
+ test_and_set_bit(sel->type, &sel_types)) {
+ err = -EPROTO;
+ goto err_ff_action;
+ }
+ real_ff_mask_size += sizeof(*sel) + sel->length;
+ if (real_ff_mask_size > ff_mask_size) {
+ err = -EPROTO;
+ goto err_ff_action;
+ }
+ sel = (void *)sel + sizeof(*sel) + sel->length;
+ }
+ ff->ff_mask->count = i;
+
+ err = virtio_admin_cap_set(vdev,
+ VIRTIO_NET_FF_SELECTOR_CAP,
+ ff->ff_mask,
+ real_ff_mask_size);
+ if (err)
+ goto err_ff_action;
+
+ err = virtio_admin_cap_set(vdev,
+ VIRTIO_NET_FF_ACTION_CAP,
+ ff->ff_actions,
+ sizeof(*ff->ff_actions) + ff->ff_actions->count);
+ if (err)
+ goto err_ff_action;
+
+ ff->vdev = vdev;
+ ff->ff_supported = true;
+
+ kfree(cap_id_list);
+
+ return 0;
+
+err_ff_action:
+ kfree(ff->ff_actions);
+ ff->ff_actions = NULL;
+err_ff_mask:
+ kfree(ff->ff_mask);
+ ff->ff_mask = NULL;
+err_ff:
+ kfree(ff->ff_caps);
+ ff->ff_caps = NULL;
+err_cap_list:
+ kfree(cap_id_list);
+
+ return err;
+}
+
+static void virtnet_ff_cleanup(struct virtnet_ff *ff)
+{
+ if (!ff->ff_supported)
+ return;
+
+ kfree(ff->ff_actions);
+ kfree(ff->ff_mask);
+ kfree(ff->ff_caps);
+ ff->ff_supported = false;
+}
+
static void virtnet_freeze_down(struct virtio_device *vdev)
{
struct virtnet_info *vi = vdev->priv;
@@ -5811,6 +6019,10 @@ static void virtnet_freeze_down(struct virtio_device *vdev)
netif_tx_lock_bh(vi->dev);
netif_device_detach(vi->dev);
netif_tx_unlock_bh(vi->dev);
+
+ rtnl_lock();
+ virtnet_ff_cleanup(&vi->ff);
+ rtnl_unlock();
}
static int init_vqs(struct virtnet_info *vi);
@@ -5838,6 +6050,20 @@ static int virtnet_restore_up(struct virtio_device *vdev)
goto err_destroy_pools;
}
+ /*
+ * Initialize flow filters. Not supported is an acceptable and common
+ * return code
+ */
+ rtnl_lock();
+ err = virtnet_ff_init(&vi->ff, vi->vdev);
+ if (err && err != -EOPNOTSUPP) {
+ rtnl_unlock();
+ virtnet_freeze_down(vi->vdev);
+ remove_vq_common(vi);
+ return err;
+ }
+ rtnl_unlock();
+
netif_tx_lock_bh(vi->dev);
netif_device_attach(vi->dev);
netif_tx_unlock_bh(vi->dev);
@@ -7142,6 +7368,15 @@ static int virtnet_probe(struct virtio_device *vdev)
virtio_device_ready(vdev);
+ /* Initialize flow filters. Not supported is an acceptable and common
+ * return code
+ */
+ err = virtnet_ff_init(&vi->ff, vi->vdev);
+ if (err && err != -EOPNOTSUPP) {
+ rtnl_unlock();
+ goto free_unregister_netdev;
+ }
+
if (vi->has_rss || vi->has_rss_hash_report) {
if (!virtnet_commit_rss_command(vi)) {
dev_warn(&vdev->dev, "RSS disabled because committing failed.\n");
@@ -7226,6 +7461,7 @@ static int virtnet_probe(struct virtio_device *vdev)
free_unregister_netdev:
unregister_netdev(dev);
+ virtnet_ff_cleanup(&vi->ff);
free_failover:
net_failover_destroy(vi->failover);
free_page_pools:
@@ -7275,6 +7511,7 @@ static void virtnet_remove(struct virtio_device *vdev)
virtnet_free_irq_moder(vi);
unregister_netdev(vi->dev);
+ virtnet_ff_cleanup(&vi->ff);
net_failover_destroy(vi->failover);
diff --git a/include/uapi/linux/virtio_net_ff.h b/include/uapi/linux/virtio_net_ff.h
new file mode 100644
index 0000000000000..f9e771a0baff1
--- /dev/null
+++ b/include/uapi/linux/virtio_net_ff.h
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
+ *
+ * Header file for virtio_net flow filters
+ */
+#ifndef _LINUX_VIRTIO_NET_FF_H
+#define _LINUX_VIRTIO_NET_FF_H
+
+#include <linux/types.h>
+#include <linux/stddef.h>
+
+#define VIRTIO_NET_FF_RESOURCE_CAP 0x800
+#define VIRTIO_NET_FF_SELECTOR_CAP 0x801
+#define VIRTIO_NET_FF_ACTION_CAP 0x802
+
+/**
+ * struct virtio_net_ff_cap_data - Flow filter resource capability limits
+ * @groups_limit: maximum number of flow filter groups supported by the device
+ * @classifiers_limit: maximum number of classifiers supported by the device
+ * @rules_limit: maximum number of rules supported device-wide across all groups
+ * @rules_per_group_limit: maximum number of rules allowed in a single group
+ * @last_rule_priority: priority value associated with the lowest-priority rule
+ * @selectors_per_classifier_limit: maximum selectors allowed in one classifier
+ */
+struct virtio_net_ff_cap_data {
+ __le32 groups_limit;
+ __le32 classifiers_limit;
+ __le32 rules_limit;
+ __le32 rules_per_group_limit;
+ __u8 last_rule_priority;
+ __u8 selectors_per_classifier_limit;
+ /* private: */
+ __u8 reserved[2];
+};
+
+/**
+ * struct virtio_net_ff_selector - Selector mask descriptor
+ * @type: selector type, one of VIRTIO_NET_FF_MASK_TYPE_* constants
+ * @flags: selector flags, see VIRTIO_NET_FF_MASK_F_* constants
+ * @reserved: must be set to 0 by the driver and ignored by the device
+ * @length: size in bytes of @mask
+ * @reserved1: must be set to 0 by the driver and ignored by the device
+ * @mask: variable-length mask payload for @type, length given by @length
+ *
+ * A selector describes a header mask that a classifier can apply. The format
+ * of @mask depends on @type.
+ */
+struct virtio_net_ff_selector {
+ __u8 type;
+ __u8 flags;
+ __u8 reserved[2];
+ __u8 length;
+ __u8 reserved1[3];
+ __u8 mask[] __counted_by(length);
+};
+
+#define VIRTIO_NET_FF_MASK_TYPE_ETH 1
+#define VIRTIO_NET_FF_MASK_TYPE_IPV4 2
+#define VIRTIO_NET_FF_MASK_TYPE_IPV6 3
+#define VIRTIO_NET_FF_MASK_TYPE_TCP 4
+#define VIRTIO_NET_FF_MASK_TYPE_UDP 5
+#define VIRTIO_NET_FF_MASK_TYPE_MAX VIRTIO_NET_FF_MASK_TYPE_UDP
+
+/**
+ * struct virtio_net_ff_cap_mask_data - Supported selector mask formats
+ * @count: number of entries in @selectors
+ * @reserved: must be set to 0 by the driver and ignored by the device
+ * @selectors: packed array of struct virtio_net_ff_selector.
+ */
+struct virtio_net_ff_cap_mask_data {
+ __u8 count;
+ __u8 reserved[7];
+ __u8 selectors[];
+};
+
+#define VIRTIO_NET_FF_MASK_F_PARTIAL_MASK (1 << 0)
+
+#define VIRTIO_NET_FF_ACTION_DROP 1
+#define VIRTIO_NET_FF_ACTION_RX_VQ 2
+#define VIRTIO_NET_FF_ACTION_MAX VIRTIO_NET_FF_ACTION_RX_VQ
+/**
+ * struct virtio_net_ff_actions - Supported flow actions
+ * @count: number of supported actions in @actions
+ * @reserved: must be set to 0 by the driver and ignored by the device
+ * @actions: array of action identifiers (VIRTIO_NET_FF_ACTION_*)
+ */
+struct virtio_net_ff_actions {
+ __u8 count;
+ __u8 reserved[7];
+ __u8 actions[] __counted_by(count);
+};
+#endif
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps
2026-12-02 15:04 ` [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps Shahar Shitrit
@ 2026-12-03 13:16 ` Michael S. Tsirkin
2026-12-03 15:51 ` netdev-bot+sashiko
1 sibling, 0 replies; 30+ messages in thread
From: Michael S. Tsirkin @ 2026-12-03 13:16 UTC (permalink / raw)
To: Shahar Shitrit
Cc: netdev, jasowang, pabeni, virtualization, parav, yohadt, xuanzhuo,
eperezma, jgg, kevin.tian, kuba, andrew+netdev, edumazet, danielj
On Wed, Dec 02, 2026 at 05:04:54PM +0200, Shahar Shitrit wrote:
> When probing a virtnet device, attempt to read the flow filter
> capabilities. In order to use the feature the caps must also
> be set. For now setting what was read is sufficient.
>
> This patch adds uapi definitions virtio_net flow filters define in
> version 1.4 of the VirtIO spec.
>
> Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
> Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
> Reviewed-by: Parav Pandit <parav@nvidia.com>
> ---
> drivers/net/virtio_net.c | 237 +++++++++++++++++++++++++++++
> include/uapi/linux/virtio_net_ff.h | 91 +++++++++++
> 2 files changed, 328 insertions(+)
> create mode 100644 include/uapi/linux/virtio_net_ff.h
>
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 5eef2c3159638..03aea14da492e 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -27,6 +27,10 @@
> #include <net/netdev_queues.h>
> #include <net/xdp_sock_drv.h>
> #include <net/page_pool/helpers.h>
> +#include <linux/virtio_admin.h>
> +#include <net/ipv6.h>
> +#include <net/ip.h>
> +#include <uapi/linux/virtio_net_ff.h>
>
> static int napi_weight = NAPI_POLL_WEIGHT;
> module_param(napi_weight, int, 0444);
> @@ -284,6 +288,14 @@ static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
> VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
> };
>
> +struct virtnet_ff {
> + struct virtio_device *vdev;
> + bool ff_supported;
> + struct virtio_net_ff_cap_data *ff_caps;
> + struct virtio_net_ff_cap_mask_data *ff_mask;
> + struct virtio_net_ff_actions *ff_actions;
> +};
> +
> #define VIRTNET_Q_TYPE_RX 0
> #define VIRTNET_Q_TYPE_TX 1
> #define VIRTNET_Q_TYPE_CQ 2
> @@ -475,6 +487,8 @@ struct virtnet_info {
>
> struct virtio_net_rss_config_hdr *rss_hdr;
>
> + struct virtnet_ff ff;
> +
> /* Must be last as it ends in a flexible-array member. */
> TRAILING_OVERLAP(struct virtio_net_rss_config_trailer, rss_trailer, hash_key_data,
> u8 rss_hash_key_data[NETDEV_RSS_KEY_LEN];
> @@ -518,6 +532,7 @@ static struct sk_buff *virtnet_skb_append_frag(struct receive_queue *rq,
> static void virtnet_xsk_completed(struct send_queue *sq, int num);
> static void free_unused_bufs(struct virtnet_info *vi);
> static void virtnet_del_vqs(struct virtnet_info *vi);
> +static void remove_vq_common(struct virtnet_info *vi);
>
> enum virtnet_xmit_type {
> VIRTNET_XMIT_TYPE_SKB,
> @@ -5794,6 +5809,199 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
> .get_base_stats = virtnet_get_base_stats,
> };
>
> +static size_t get_mask_size(u16 type)
> +{
> + switch (type) {
> + case VIRTIO_NET_FF_MASK_TYPE_ETH:
> + return sizeof(struct ethhdr);
> + case VIRTIO_NET_FF_MASK_TYPE_IPV4:
> + return sizeof(struct iphdr);
> + case VIRTIO_NET_FF_MASK_TYPE_IPV6:
> + return sizeof(struct ipv6hdr);
> + case VIRTIO_NET_FF_MASK_TYPE_TCP:
> + return sizeof(struct tcphdr);
> + case VIRTIO_NET_FF_MASK_TYPE_UDP:
> + return sizeof(struct udphdr);
> + }
> +
> + return 0;
> +}
> +
> +static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
> +{
> + size_t ff_mask_size = sizeof(struct virtio_net_ff_cap_mask_data) +
> + sizeof(struct virtio_net_ff_selector) *
> + VIRTIO_NET_FF_MASK_TYPE_MAX;
> + struct virtio_admin_cmd_query_cap_id_result *cap_id_list;
> + struct virtio_net_ff_selector *sel;
> + unsigned long sel_types = 0;
> + size_t real_ff_mask_size;
> + int err;
> + int i;
> +
> + if (!vdev->config->admin_cmd_exec)
> + return -EOPNOTSUPP;
> +
> + cap_id_list = kzalloc(sizeof(*cap_id_list), GFP_KERNEL);
> + if (!cap_id_list)
> + return -ENOMEM;
> +
> + err = virtio_admin_cap_id_list_query(vdev, cap_id_list);
> + if (err)
> + goto err_cap_list;
> +
> + if (!(virtio_cap_in_list(cap_id_list, VIRTIO_NET_FF_RESOURCE_CAP) &&
> + virtio_cap_in_list(cap_id_list, VIRTIO_NET_FF_SELECTOR_CAP) &&
> + virtio_cap_in_list(cap_id_list, VIRTIO_NET_FF_ACTION_CAP))) {
> + err = -EOPNOTSUPP;
> + goto err_cap_list;
> + }
> +
> + ff->ff_caps = kzalloc(sizeof(*ff->ff_caps), GFP_KERNEL);
> + if (!ff->ff_caps) {
> + err = -ENOMEM;
> + goto err_cap_list;
> + }
> +
> + err = virtio_admin_cap_get(vdev,
> + VIRTIO_NET_FF_RESOURCE_CAP,
> + ff->ff_caps,
> + sizeof(*ff->ff_caps));
> +
> + if (err)
> + goto err_ff;
> +
> + if (!le32_to_cpu(ff->ff_caps->groups_limit) ||
> + !le32_to_cpu(ff->ff_caps->classifiers_limit) ||
> + !le32_to_cpu(ff->ff_caps->rules_limit) ||
> + !le32_to_cpu(ff->ff_caps->rules_per_group_limit) ||
> + !ff->ff_caps->selectors_per_classifier_limit) {
> + err = -EINVAL;
> + goto err_ff;
> + }
> +
> + /* VIRTIO_NET_FF_MASK_TYPE start at 1 */
> + for (i = 1; i <= VIRTIO_NET_FF_MASK_TYPE_MAX; i++)
> + ff_mask_size += get_mask_size(i);
> +
> + ff->ff_mask = kzalloc(ff_mask_size, GFP_KERNEL);
> + if (!ff->ff_mask) {
> + err = -ENOMEM;
> + goto err_ff;
> + }
> +
> + err = virtio_admin_cap_get(vdev,
> + VIRTIO_NET_FF_SELECTOR_CAP,
> + ff->ff_mask,
> + ff_mask_size);
> +
> + if (err)
> + goto err_ff_mask;
> +
> + ff->ff_mask->count = min_t(u8, ff->ff_mask->count,
> + VIRTIO_NET_FF_MASK_TYPE_MAX);
> +
> + ff->ff_actions = kzalloc(sizeof(*ff->ff_actions) +
> + VIRTIO_NET_FF_ACTION_MAX,
> + GFP_KERNEL);
> + if (!ff->ff_actions) {
> + err = -ENOMEM;
> + goto err_ff_mask;
> + }
> +
> + err = virtio_admin_cap_get(vdev,
> + VIRTIO_NET_FF_ACTION_CAP,
> + ff->ff_actions,
> + sizeof(*ff->ff_actions) + VIRTIO_NET_FF_ACTION_MAX);
> +
> + if (err)
> + goto err_ff_action;
> +
> + ff->ff_actions->count = min_t(u8, ff->ff_actions->count,
> + VIRTIO_NET_FF_ACTION_MAX);
> + if (!ff->ff_actions->count) {
> + err = -EINVAL;
> + goto err_ff_action;
> + }
> +
> + err = virtio_admin_cap_set(vdev,
> + VIRTIO_NET_FF_RESOURCE_CAP,
> + ff->ff_caps,
> + sizeof(*ff->ff_caps));
> + if (err)
> + goto err_ff_action;
> +
> + real_ff_mask_size = sizeof(struct virtio_net_ff_cap_mask_data);
> + sel = (void *)&ff->ff_mask->selectors;
> +
> + for (i = 0; i < ff->ff_mask->count; i++) {
> + /* If the selector type is unknown it may indicate the spec
> + * has been revised to include new types of selectors
> + */
> + if (!sel->type || sel->type > VIRTIO_NET_FF_MASK_TYPE_MAX)
> + break;
> +
> + if (sel->length != get_mask_size(sel->type) ||
> + test_and_set_bit(sel->type, &sel_types)) {
> + err = -EPROTO;
> + goto err_ff_action;
> + }
> + real_ff_mask_size += sizeof(*sel) + sel->length;
> + if (real_ff_mask_size > ff_mask_size) {
> + err = -EPROTO;
> + goto err_ff_action;
> + }
> + sel = (void *)sel + sizeof(*sel) + sel->length;
> + }
> + ff->ff_mask->count = i;
> +
> + err = virtio_admin_cap_set(vdev,
> + VIRTIO_NET_FF_SELECTOR_CAP,
> + ff->ff_mask,
> + real_ff_mask_size);
> + if (err)
> + goto err_ff_action;
> +
> + err = virtio_admin_cap_set(vdev,
> + VIRTIO_NET_FF_ACTION_CAP,
> + ff->ff_actions,
> + sizeof(*ff->ff_actions) + ff->ff_actions->count);
> + if (err)
> + goto err_ff_action;
> +
> + ff->vdev = vdev;
> + ff->ff_supported = true;
> +
> + kfree(cap_id_list);
> +
> + return 0;
> +
> +err_ff_action:
> + kfree(ff->ff_actions);
> + ff->ff_actions = NULL;
> +err_ff_mask:
> + kfree(ff->ff_mask);
> + ff->ff_mask = NULL;
> +err_ff:
> + kfree(ff->ff_caps);
> + ff->ff_caps = NULL;
> +err_cap_list:
> + kfree(cap_id_list);
> +
> + return err;
> +}
> +
> +static void virtnet_ff_cleanup(struct virtnet_ff *ff)
> +{
> + if (!ff->ff_supported)
> + return;
> +
> + kfree(ff->ff_actions);
> + kfree(ff->ff_mask);
> + kfree(ff->ff_caps);
> + ff->ff_supported = false;
> +}
> +
> static void virtnet_freeze_down(struct virtio_device *vdev)
> {
> struct virtnet_info *vi = vdev->priv;
> @@ -5811,6 +6019,10 @@ static void virtnet_freeze_down(struct virtio_device *vdev)
> netif_tx_lock_bh(vi->dev);
> netif_device_detach(vi->dev);
> netif_tx_unlock_bh(vi->dev);
> +
> + rtnl_lock();
> + virtnet_ff_cleanup(&vi->ff);
> + rtnl_unlock();
> }
So do we need rtnl_lock around virtnet_ff_cleanup?
Because below it is called without rtnl_lock.
>
> static int init_vqs(struct virtnet_info *vi);
> @@ -5838,6 +6050,20 @@ static int virtnet_restore_up(struct virtio_device *vdev)
> goto err_destroy_pools;
> }
>
> + /*
> + * Initialize flow filters. Not supported is an acceptable and common
> + * return code
> + */
> + rtnl_lock();
> + err = virtnet_ff_init(&vi->ff, vi->vdev);
> + if (err && err != -EOPNOTSUPP) {
> + rtnl_unlock();
> + virtnet_freeze_down(vi->vdev);
> + remove_vq_common(vi);
> + return err;
> + }
> + rtnl_unlock();
> +
> netif_tx_lock_bh(vi->dev);
> netif_device_attach(vi->dev);
> netif_tx_unlock_bh(vi->dev);
> @@ -7142,6 +7368,15 @@ static int virtnet_probe(struct virtio_device *vdev)
>
> virtio_device_ready(vdev);
>
> + /* Initialize flow filters. Not supported is an acceptable and common
> + * return code
> + */
> + err = virtnet_ff_init(&vi->ff, vi->vdev);
> + if (err && err != -EOPNOTSUPP) {
> + rtnl_unlock();
> + goto free_unregister_netdev;
> + }
> +
> if (vi->has_rss || vi->has_rss_hash_report) {
> if (!virtnet_commit_rss_command(vi)) {
> dev_warn(&vdev->dev, "RSS disabled because committing failed.\n");
> @@ -7226,6 +7461,7 @@ static int virtnet_probe(struct virtio_device *vdev)
>
> free_unregister_netdev:
> unregister_netdev(dev);
> + virtnet_ff_cleanup(&vi->ff);
> free_failover:
> net_failover_destroy(vi->failover);
> free_page_pools:
> @@ -7275,6 +7511,7 @@ static void virtnet_remove(struct virtio_device *vdev)
> virtnet_free_irq_moder(vi);
>
> unregister_netdev(vi->dev);
> + virtnet_ff_cleanup(&vi->ff);
>
> net_failover_destroy(vi->failover);
>
> diff --git a/include/uapi/linux/virtio_net_ff.h b/include/uapi/linux/virtio_net_ff.h
> new file mode 100644
> index 0000000000000..f9e771a0baff1
> --- /dev/null
> +++ b/include/uapi/linux/virtio_net_ff.h
> @@ -0,0 +1,91 @@
> +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
> + *
> + * Header file for virtio_net flow filters
> + */
> +#ifndef _LINUX_VIRTIO_NET_FF_H
> +#define _LINUX_VIRTIO_NET_FF_H
> +
> +#include <linux/types.h>
> +#include <linux/stddef.h>
> +
> +#define VIRTIO_NET_FF_RESOURCE_CAP 0x800
> +#define VIRTIO_NET_FF_SELECTOR_CAP 0x801
> +#define VIRTIO_NET_FF_ACTION_CAP 0x802
> +
> +/**
> + * struct virtio_net_ff_cap_data - Flow filter resource capability limits
> + * @groups_limit: maximum number of flow filter groups supported by the device
> + * @classifiers_limit: maximum number of classifiers supported by the device
> + * @rules_limit: maximum number of rules supported device-wide across all groups
> + * @rules_per_group_limit: maximum number of rules allowed in a single group
> + * @last_rule_priority: priority value associated with the lowest-priority rule
> + * @selectors_per_classifier_limit: maximum selectors allowed in one classifier
> + */
> +struct virtio_net_ff_cap_data {
> + __le32 groups_limit;
> + __le32 classifiers_limit;
> + __le32 rules_limit;
> + __le32 rules_per_group_limit;
> + __u8 last_rule_priority;
> + __u8 selectors_per_classifier_limit;
> + /* private: */
> + __u8 reserved[2];
> +};
> +
> +/**
> + * struct virtio_net_ff_selector - Selector mask descriptor
> + * @type: selector type, one of VIRTIO_NET_FF_MASK_TYPE_* constants
> + * @flags: selector flags, see VIRTIO_NET_FF_MASK_F_* constants
> + * @reserved: must be set to 0 by the driver and ignored by the device
> + * @length: size in bytes of @mask
> + * @reserved1: must be set to 0 by the driver and ignored by the device
> + * @mask: variable-length mask payload for @type, length given by @length
> + *
> + * A selector describes a header mask that a classifier can apply. The format
> + * of @mask depends on @type.
> + */
> +struct virtio_net_ff_selector {
> + __u8 type;
> + __u8 flags;
> + __u8 reserved[2];
> + __u8 length;
> + __u8 reserved1[3];
> + __u8 mask[] __counted_by(length);
> +};
> +
> +#define VIRTIO_NET_FF_MASK_TYPE_ETH 1
> +#define VIRTIO_NET_FF_MASK_TYPE_IPV4 2
> +#define VIRTIO_NET_FF_MASK_TYPE_IPV6 3
> +#define VIRTIO_NET_FF_MASK_TYPE_TCP 4
> +#define VIRTIO_NET_FF_MASK_TYPE_UDP 5
> +#define VIRTIO_NET_FF_MASK_TYPE_MAX VIRTIO_NET_FF_MASK_TYPE_UDP
> +
> +/**
> + * struct virtio_net_ff_cap_mask_data - Supported selector mask formats
> + * @count: number of entries in @selectors
> + * @reserved: must be set to 0 by the driver and ignored by the device
> + * @selectors: packed array of struct virtio_net_ff_selector.
> + */
> +struct virtio_net_ff_cap_mask_data {
> + __u8 count;
> + __u8 reserved[7];
> + __u8 selectors[];
> +};
> +
> +#define VIRTIO_NET_FF_MASK_F_PARTIAL_MASK (1 << 0)
> +
> +#define VIRTIO_NET_FF_ACTION_DROP 1
> +#define VIRTIO_NET_FF_ACTION_RX_VQ 2
> +#define VIRTIO_NET_FF_ACTION_MAX VIRTIO_NET_FF_ACTION_RX_VQ
> +/**
> + * struct virtio_net_ff_actions - Supported flow actions
> + * @count: number of supported actions in @actions
> + * @reserved: must be set to 0 by the driver and ignored by the device
> + * @actions: array of action identifiers (VIRTIO_NET_FF_ACTION_*)
> + */
> +struct virtio_net_ff_actions {
> + __u8 count;
> + __u8 reserved[7];
> + __u8 actions[] __counted_by(count);
> +};
> +#endif
> --
> 2.49.0
>
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps
2026-12-02 15:04 ` [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps Shahar Shitrit
2026-12-03 13:16 ` Michael S. Tsirkin
@ 2026-12-03 15:51 ` netdev-bot+sashiko
1 sibling, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 (1) · Medium: 3 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] Flow filters are optional and nothing in this patch uses them,
yet a failed flow-filter init now aborts probe and resume.
- [Medium] In virtnet_ff_init() (drivers/net/virtio_net.c:5904-5925,
5965-5968) the action capability list from the device is echoed back…
- [Medium] virtio_net: unbalanced disable_rx_mode_work() in
virtnet_restore_up()
- [Low] virtnet_ff_init() reads the three FF capability structures with
DEVICE_CAP_GET into ff->ff_caps, ff->ff_mask and ff->ff_actions.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch adds
a new path that a device can trigger.
--
virtio_net: Query and set flow filter caps
This commit adds virtnet_ff_init(). During probe and restore it reads the
flow filter resource, selector and action capabilities with virtio admin
commands, then sets the same capabilities back on the device. It also adds
the uapi header virtio_net_ff.h, with the flow filter definitions from
version 1.4 of the VirtIO spec.
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 5eef2c3159638..03aea14da492e 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
[ ... ]
> @@ -5794,6 +5809,199 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
[ ... ]
> + err = virtio_admin_cap_get(vdev,
> + VIRTIO_NET_FF_ACTION_CAP,
> + ff->ff_actions,
> + sizeof(*ff->ff_actions) + VIRTIO_NET_FF_ACTION_MAX);
> +
> + if (err)
> + goto err_ff_action;
> +
> + ff->ff_actions->count = min_t(u8, ff->ff_actions->count,
> + VIRTIO_NET_FF_ACTION_MAX);
[Severity: Medium]
Is this clamp checking the right thing in virtnet_ff_init()?
VIRTIO_NET_FF_ACTION_MAX is an action identifier, but here it is used as a
count. The entries are cut off by position, and nothing checks that each
entry is VIRTIO_NET_FF_ACTION_DROP or VIRTIO_NET_FF_ACTION_RX_VQ.
Say a newer device reports actions [3, 1, 2]. The driver keeps [3, 1] and
later sends that back with virtio_admin_cap_set(). It claims action 3,
which it doesn't implement, and drops RX_VQ, which it does implement.
Duplicate action IDs also get through.
Later in the series, RX_VQ rules look in ff_actions->actions[] for the
action they need. Would those rules be refused in this case?
The selector loop further down has a similar problem:
if (!sel->type || sel->type > VIRTIO_NET_FF_MASK_TYPE_MAX)
break;
The comment above it expects spec revisions that add new selector types.
However, the loop stops at the first unknown entry instead of skipping it
by sel->length. If an unknown type comes first, ff_mask->count becomes 0.
An empty selector set is then sent to the device, and ff_supported is
still set to true.
Should unknown actions and selectors be filtered out individually, and
should an empty selector set be rejected? This code is the same at the end
of the series.
> + if (!ff->ff_actions->count) {
> + err = -EINVAL;
> + goto err_ff_action;
> + }
[ ... ]
> + ff->ff_mask->count = i;
> +
> + err = virtio_admin_cap_set(vdev,
> + VIRTIO_NET_FF_SELECTOR_CAP,
> + ff->ff_mask,
> + real_ff_mask_size);
[Severity: Low]
The buffers filled by virtio_admin_cap_get() go back to the device
unchanged through virtio_admin_cap_set(), which does:
memcpy(data->cap_specific_data, caps, cap_size);
So any value the device wrote into ff_mask->reserved[], each
sel->reserved[] and sel->reserved1[], ff_actions->reserved[] and
ff_caps->reserved[] is echoed back. The new uapi header says these fields
"must be set to 0 by the driver". Should they be cleared before the set
commands?
sel->flags is also echoed back without masking it to
VIRTIO_NET_FF_MASK_F_PARTIAL_MASK. If a device reports a future flag bit,
would the driver end up claiming support for it?
The resource and action cap sets in virtnet_ff_init() follow the same
pattern. This code is the same at the end of the series.
> + if (err)
> + goto err_ff_action;
> +
> + err = virtio_admin_cap_set(vdev,
> + VIRTIO_NET_FF_ACTION_CAP,
> + ff->ff_actions,
> + sizeof(*ff->ff_actions) + ff->ff_actions->count);
> + if (err)
> + goto err_ff_action;
[ ... ]
> @@ -5838,6 +6050,20 @@ static int virtnet_restore_up(struct virtio_device *vdev)
> goto err_destroy_pools;
> }
>
> + /*
> + * Initialize flow filters. Not supported is an acceptable and common
> + * return code
> + */
> + rtnl_lock();
> + err = virtnet_ff_init(&vi->ff, vi->vdev);
> + if (err && err != -EOPNOTSUPP) {
> + rtnl_unlock();
> + virtnet_freeze_down(vi->vdev);
> + remove_vq_common(vi);
> + return err;
> + }
[Severity: Medium]
Does calling virtnet_freeze_down() on this error path leave
vi->rx_mode_work disabled twice?
virtnet_freeze() has already run virtnet_freeze_down(). That called
disable_rx_mode_work(), which does disable_work_sync() on
vi->rx_mode_work. The matching enable_rx_mode_work() only runs at the
end of virtnet_restore_up(), after this new block. So when
virtnet_ff_init() fails, the work is still disabled from the freeze,
and it gets disabled again:
virtnet_freeze()
virtnet_freeze_down()
disable_rx_mode_work() <- disable count 0 -> 1
virtnet_restore()
virtnet_restore_up()
virtnet_open()
virtnet_ff_init() fails
virtnet_freeze_down()
disable_rx_mode_work() <- disable count 1 -> 2
Nothing removes that extra disable. A later enable_rx_mode_work() only
brings the count back to 1. Queueing a disabled work item does nothing,
so rx mode changes requested through ndo_set_rx_mode would be dropped
silently from then on.
virtnet_freeze_down() also repeats netif_device_detach() and
virtnet_ff_cleanup(), which already ran at freeze time. It was written
to quiesce a running device. It was not written to undo a partial
restore.
Could virtnet_ff_init() be called before the netif_running() and
virtnet_open() block instead, with failures unwinding through
err_destroy_pools? virtnet_probe() already does this right after
virtio_device_ready(). The netdev would then not be opened and closed
again here, and the disable/enable of rx_mode_work would stay
balanced.
[Severity: High]
This is a pre-existing issue and was not introduced by this patch, but
this new error path reaches it. remove_vq_common() ends up in
virtnet_del_vqs()->virtnet_free_queues(), which does this without
clearing the pointers:
kfree(vi->rq);
kfree(vi->sq);
kfree(vi->ctrl);
virtnet_restore() then returns the error. virtio_device_restore_priv()
only marks the device as failed, so the driver stays bound:
err:
virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
return ret;
On a later unbind, rmmod or hot-unplug:
virtnet_remove()
remove_vq_common()
free_unused_bufs()
struct virtqueue *vq = vi->sq[i].vq; <- vi->sq already freed
virtnet_del_vqs()
virtnet_free_queues() <- second kfree()
Can this cause a use-after-free and then a double free of vi->sq, vi->rq
and vi->ctrl?
The restore failure paths for virtnet_cpu_notif_add() and for the open
and page pool failures already have this problem. With this patch, any
flow filter init failure on resume also reaches it, for example -ENOMEM,
a device error status, -EIO, zero limits or malformed selectors. The
problem is still there at the end of the series.
> + rtnl_unlock();
[ ... ]
> @@ -7142,6 +7368,15 @@ static int virtnet_probe(struct virtio_device *vdev)
>
> virtio_device_ready(vdev);
>
> + /* Initialize flow filters. Not supported is an acceptable and common
> + * return code
> + */
> + err = virtnet_ff_init(&vi->ff, vi->vdev);
> + if (err && err != -EOPNOTSUPP) {
> + rtnl_unlock();
> + goto free_unregister_netdev;
> + }
[Severity: Medium]
Flow filters are optional, and nothing in this patch uses them yet.
Should a failure here make the whole probe fail?
Only -EOPNOTSUPP is treated as harmless. virtnet_ff_init() can return
other errors when the feature just can't be used.
vp_modern_admin_cmd_exec() turns any non-OK device status into an error
with "ret = -status". A device that rejects the command therefore gives
-EINVAL, not -EOPNOTSUPP. A broken vq or an add/kick failure gives -EIO.
The -EOPNOTSUPP check in the admin path looks at admin_vq.supported_cmds.
That list comes from the SR-IOV group query in
virtio_pci_admin_cmd_list_init(). The flow filter cap commands use
VIRTIO_ADMIN_GROUP_TYPE_SELF instead, so a device can pass that check and
still reject the command.
The sanity checks in virtnet_ff_init() return -EINVAL for zero limits or
zero actions, and -EPROTO for a bad selector length or a duplicate
selector type. Allocation failures return -ENOMEM.
virtio_pci_admin_cmd_cap_init() sends the same SELF group cap id list
query and treats a failure as non-fatal:
ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
if (ret)
goto end;
Could a device that probed fine before this patch now fail probe here?
virtnet_restore_up() has the same problem: resume fails and the vqs are
torn down.
Later in the series this handling stays the same and gains more fatal
cases: -ENOSPC when groups_limit < VIRTNET_FF_MAX_GROUPS, and a failure of
virtio_admin_obj_create() for the ethtool group.
Would it be better to log the failure and continue without flow filters?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 07/14] virtio_net: Create a FF group for ethtool steering
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (5 preceding siblings ...)
2026-12-02 15:04 ` [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:04 ` [PATCH net-next v24 08/14] ethtool: Introduce ethtool_flow_type_mask() Shahar Shitrit
` (7 subsequent siblings)
14 siblings, 1 reply; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
All ethtool steering rules will go in one group, create it during
initialization.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
---
drivers/net/virtio_net.c | 32 ++++++++++++++++++++++++++++++
include/uapi/linux/virtio_net_ff.h | 15 ++++++++++++++
2 files changed, 47 insertions(+)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 03aea14da492e..d5ca5a4d4378c 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -288,6 +288,9 @@ static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
};
+#define VIRTNET_FF_ETHTOOL_GROUP_PRIORITY 0
+#define VIRTNET_FF_MAX_GROUPS 1
+
struct virtnet_ff {
struct virtio_device *vdev;
bool ff_supported;
@@ -5832,6 +5835,7 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
size_t ff_mask_size = sizeof(struct virtio_net_ff_cap_mask_data) +
sizeof(struct virtio_net_ff_selector) *
VIRTIO_NET_FF_MASK_TYPE_MAX;
+ struct virtio_net_resource_obj_ff_group ethtool_group = {};
struct virtio_admin_cmd_query_cap_id_result *cap_id_list;
struct virtio_net_ff_selector *sel;
unsigned long sel_types = 0;
@@ -5924,6 +5928,12 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
goto err_ff_action;
}
+ if (le32_to_cpu(ff->ff_caps->groups_limit) < VIRTNET_FF_MAX_GROUPS) {
+ err = -ENOSPC;
+ goto err_ff_action;
+ }
+ ff->ff_caps->groups_limit = cpu_to_le32(VIRTNET_FF_MAX_GROUPS);
+
err = virtio_admin_cap_set(vdev,
VIRTIO_NET_FF_RESOURCE_CAP,
ff->ff_caps,
@@ -5969,6 +5979,19 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
if (err)
goto err_ff_action;
+ ethtool_group.group_priority = cpu_to_le16(VIRTNET_FF_ETHTOOL_GROUP_PRIORITY);
+
+ /* Use priority for the object ID. */
+ err = virtio_admin_obj_create(vdev,
+ VIRTIO_NET_RESOURCE_OBJ_FF_GROUP,
+ VIRTNET_FF_ETHTOOL_GROUP_PRIORITY,
+ VIRTIO_ADMIN_GROUP_TYPE_SELF,
+ 0,
+ ðtool_group,
+ sizeof(ethtool_group));
+ if (err)
+ goto err_ff_action;
+
ff->vdev = vdev;
ff->ff_supported = true;
@@ -5993,9 +6016,18 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
static void virtnet_ff_cleanup(struct virtnet_ff *ff)
{
+ int err;
+
if (!ff->ff_supported)
return;
+ err = virtio_admin_obj_destroy(ff->vdev,
+ VIRTIO_NET_RESOURCE_OBJ_FF_GROUP,
+ VIRTNET_FF_ETHTOOL_GROUP_PRIORITY,
+ VIRTIO_ADMIN_GROUP_TYPE_SELF,
+ 0);
+ WARN_ON_ONCE(err);
+
kfree(ff->ff_actions);
kfree(ff->ff_mask);
kfree(ff->ff_caps);
diff --git a/include/uapi/linux/virtio_net_ff.h b/include/uapi/linux/virtio_net_ff.h
index f9e771a0baff1..b0780e49df9f0 100644
--- a/include/uapi/linux/virtio_net_ff.h
+++ b/include/uapi/linux/virtio_net_ff.h
@@ -12,6 +12,8 @@
#define VIRTIO_NET_FF_SELECTOR_CAP 0x801
#define VIRTIO_NET_FF_ACTION_CAP 0x802
+#define VIRTIO_NET_RESOURCE_OBJ_FF_GROUP 0x0200
+
/**
* struct virtio_net_ff_cap_data - Flow filter resource capability limits
* @groups_limit: maximum number of flow filter groups supported by the device
@@ -88,4 +90,17 @@ struct virtio_net_ff_actions {
__u8 reserved[7];
__u8 actions[] __counted_by(count);
};
+
+/**
+ * struct virtio_net_resource_obj_ff_group - Flow filter group object
+ * @group_priority: priority of the group used to order evaluation
+ *
+ * This structure is the payload for the VIRTIO_NET_RESOURCE_OBJ_FF_GROUP
+ * administrative object. Devices use @group_priority to order flow filter
+ * groups. Multi-byte fields are little-endian.
+ */
+struct virtio_net_resource_obj_ff_group {
+ __le16 group_priority;
+};
+
#endif
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 07/14] virtio_net: Create a FF group for ethtool steering
2026-12-02 15:04 ` [PATCH net-next v24 07/14] virtio_net: Create a FF group for ethtool steering Shahar Shitrit
@ 2026-12-03 15:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] virtnet_ff_cleanup() in drivers/net/virtio_net.c wraps the
result of virtio_admin_obj_destroy() in WARN_ON_ONCE(err).
- [Medium] This patch adds a new step to virtnet_ff_init():
virtio_admin_obj_create() for the ethtool FF group.
- [Low] The commit message only says the ethtool group is created during
initialization.
--
virtio_net: Create a FF group for ethtool steering
This commit creates one flow filter group for ethtool steering rules in
virtnet_ff_init() and destroys it in virtnet_ff_cleanup(). It also clamps
the groups_limit programmed into the device to VIRTNET_FF_MAX_GROUPS. It
adds the VIRTIO_NET_RESOURCE_OBJ_FF_GROUP uapi definitions as well.
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 03aea14da492e..d5ca5a4d4378c 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
[ ... ]
> @@ -5924,6 +5928,12 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
> goto err_ff_action;
> }
>
> + if (le32_to_cpu(ff->ff_caps->groups_limit) < VIRTNET_FF_MAX_GROUPS) {
> + err = -ENOSPC;
> + goto err_ff_action;
> + }
> + ff->ff_caps->groups_limit = cpu_to_le32(VIRTNET_FF_MAX_GROUPS);
> +
[Severity: Low]
The commit message only says the group is created during initialization.
This hunk also changes what gets programmed into the device with
VIRTIO_NET_FF_RESOURCE_CAP. groups_limit is now overwritten with
VIRTNET_FF_MAX_GROUPS instead of keeping the value read from the device.
That reverses the earlier "virtio_net: Query and set flow filter caps"
patch in this series ("For now setting what was read is sufficient").
Could the commit message mention this?
Also, can the -ENOSPC branch ever be taken? With VIRTNET_FF_MAX_GROUPS
set to 1, the only value below it is 0. An earlier check in
virtnet_ff_init() already rejects 0:
if (!le32_to_cpu(ff->ff_caps->groups_limit) ||
...
err = -EINVAL;
goto err_ff;
}
> err = virtio_admin_cap_set(vdev,
> VIRTIO_NET_FF_RESOURCE_CAP,
> ff->ff_caps,
> @@ -5969,6 +5979,19 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
> if (err)
> goto err_ff_action;
>
> + ethtool_group.group_priority = cpu_to_le16(VIRTNET_FF_ETHTOOL_GROUP_PRIORITY);
> +
> + /* Use priority for the object ID. */
> + err = virtio_admin_obj_create(vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_GROUP,
> + VIRTNET_FF_ETHTOOL_GROUP_PRIORITY,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0,
> + ðtool_group,
> + sizeof(ethtool_group));
> + if (err)
> + goto err_ff_action;
[Severity: Medium]
Unlike the earlier CAP_SET calls, this one asks the device to allocate
an object, so a device-side resource error is a realistic result. One
example is a resource exhaustion status when FF resources are shared
between functions. Should that error make the whole interface fail?
Both callers of virtnet_ff_init() accept only -EOPNOTSUPP.
virtnet_probe():
err = virtnet_ff_init(&vi->ff, vi->vdev);
if (err && err != -EOPNOTSUPP) {
rtnl_unlock();
goto free_unregister_netdev;
}
virtnet_restore_up():
err = virtnet_ff_init(&vi->ff, vi->vdev);
if (err && err != -EOPNOTSUPP) {
rtnl_unlock();
virtnet_freeze_down(vi->vdev);
remove_vq_common(vi);
return err;
}
If the device fails to create this group, which is only needed for the
optional ethtool steering feature, the virtio-net interface is lost at
probe or after resume. The device does not just run without flow
steering. The commit message does not mention this new failure mode.
> +
> ff->vdev = vdev;
> ff->ff_supported = true;
>
> @@ -5993,9 +6016,18 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
>
> static void virtnet_ff_cleanup(struct virtnet_ff *ff)
> {
> + int err;
> +
> if (!ff->ff_supported)
> return;
>
> + err = virtio_admin_obj_destroy(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_GROUP,
> + VIRTNET_FF_ETHTOOL_GROUP_PRIORITY,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0);
> + WARN_ON_ONCE(err);
[Severity: Medium]
Is WARN_ON_ONCE() the right choice here? The device and the transport
control this return value, and cleanup continues the same way whatever
it is.
On a surprise hot-unplug, virtio_pci_remove() marks the device broken
before the driver's remove runs. The admin vq is broken too:
virtio_pci_remove()
if (!pci_device_is_present(pci_dev))
virtio_break_device(&vp_dev->vdev);
unregister_virtio_device()
virtnet_remove()
virtnet_ff_cleanup()
virtio_admin_obj_destroy()
vp_modern_admin_cmd_exec()
virtqueue_exec_admin_cmd()
if (virtqueue_is_broken(vq))
return -EIO;
The same thing happens in these cases:
- The device advertises RESOURCE_OBJ_CREATE but not
RESOURCE_OBJ_DESTROY in supported_cmds, so
virtqueue_exec_admin_cmd() returns -EOPNOTSUPP.
- The device returns any non-OK admin status.
- The kzalloc() in virtio_admin_obj_destroy() fails.
Each of these gives a WARN splat, or a panic with panic_on_warn.
vp_modern_admin_cmd_exec() already logs the failure with dev_err().
A later patch in the series adds the same WARN_ON_ONCE(err) around
classifier destroy.
> +
> kfree(ff->ff_actions);
> kfree(ff->ff_mask);
> kfree(ff->ff_caps);
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 08/14] ethtool: Introduce ethtool_flow_type_mask()
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (6 preceding siblings ...)
2026-12-02 15:04 ` [PATCH net-next v24 07/14] virtio_net: Create a FF group for ethtool steering Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-02 15:04 ` [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules Shahar Shitrit
` (6 subsequent siblings)
14 siblings, 0 replies; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
Add a common ethtool_flow_type_mask() helper to mask extension bits
from an ethtool flow type.
Move the helper to include/linux/ethtool.h and convert mlx5e and
mlx5i to use it, removing duplicate implementations.
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
---
.../ethernet/mellanox/mlx5/core/en_fs_ethtool.c | 17 ++++++-----------
.../ethernet/mellanox/mlx5/core/ipoib/ethtool.c | 7 +------
include/linux/ethtool.h | 6 ++++++
3 files changed, 13 insertions(+), 17 deletions(-)
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_fs_ethtool.c b/drivers/net/ethernet/mellanox/mlx5/core/en_fs_ethtool.c
index aecfba7deeb03..7247f6cbdb32d 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_fs_ethtool.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_fs_ethtool.c
@@ -53,11 +53,6 @@ struct mlx5e_ethtool_steering {
static int flow_type_to_traffic_type(u32 flow_type);
-static u32 flow_type_mask(u32 flow_type)
-{
- return flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
-}
-
struct mlx5e_ethtool_rule {
struct list_head list;
struct ethtool_rx_flow_spec flow_spec;
@@ -91,7 +86,7 @@ static struct mlx5e_ethtool_table *get_flow_table(struct mlx5e_priv *priv,
int table_size;
int prio;
- switch (flow_type_mask(fs->flow_type)) {
+ switch (ethtool_flow_type_mask(fs->flow_type)) {
case TCP_V4_FLOW:
case UDP_V4_FLOW:
case TCP_V6_FLOW:
@@ -350,7 +345,7 @@ static int set_flow_attrs(u32 *match_c, u32 *match_v,
outer_headers);
void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
outer_headers);
- u32 flow_type = flow_type_mask(fs->flow_type);
+ u32 flow_type = ethtool_flow_type_mask(fs->flow_type);
switch (flow_type) {
case TCP_V4_FLOW:
@@ -435,7 +430,7 @@ static int flow_get_tirn(struct mlx5e_priv *priv,
if (!rss)
return -ENOENT;
- flow_type = flow_type_mask(fs->flow_type);
+ flow_type = ethtool_flow_type_mask(fs->flow_type);
tt = flow_type_to_traffic_type(flow_type);
if (tt < 0)
return -EINVAL;
@@ -673,7 +668,7 @@ static int validate_flow(struct mlx5e_priv *priv,
if (fs->ring_cookie >= priv->channels.params.num_channels)
return -EINVAL;
- switch (flow_type_mask(fs->flow_type)) {
+ switch (ethtool_flow_type_mask(fs->flow_type)) {
case ETHER_FLOW:
num_tuples += validate_ethter(fs);
break;
@@ -906,7 +901,7 @@ int mlx5e_ethtool_set_rxfh_fields(struct mlx5e_priv *priv,
rss_idx = nfc->rss_context;
- flow_type = flow_type_mask(nfc->flow_type);
+ flow_type = ethtool_flow_type_mask(nfc->flow_type);
tt = flow_type_to_traffic_type(flow_type);
if (tt < 0)
return tt;
@@ -951,7 +946,7 @@ int mlx5e_ethtool_get_rxfh_fields(struct mlx5e_priv *priv,
rss_idx = nfc->rss_context;
- flow_type = flow_type_mask(nfc->flow_type);
+ flow_type = ethtool_flow_type_mask(nfc->flow_type);
tt = flow_type_to_traffic_type(flow_type);
if (tt < 0)
return tt;
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/ipoib/ethtool.c b/drivers/net/ethernet/mellanox/mlx5/core/ipoib/ethtool.c
index 01ddc3def9ac0..83eaceddc4375 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/ipoib/ethtool.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/ipoib/ethtool.c
@@ -233,11 +233,6 @@ static int mlx5i_get_link_ksettings(struct net_device *netdev,
return 0;
}
-static u32 mlx5i_flow_type_mask(u32 flow_type)
-{
- return flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
-}
-
static int mlx5i_set_rxfh_fields(struct net_device *dev,
const struct ethtool_rxfh_fields *cmd,
struct netlink_ext_ack *extack)
@@ -260,7 +255,7 @@ static int mlx5i_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
struct mlx5e_priv *priv = mlx5i_epriv(dev);
struct ethtool_rx_flow_spec *fs = &cmd->fs;
- if (mlx5i_flow_type_mask(fs->flow_type) == ETHER_FLOW)
+ if (ethtool_flow_type_mask(fs->flow_type) == ETHER_FLOW)
return -EINVAL;
return mlx5e_ethtool_set_rxnfc(priv, cmd);
diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h
index c3a41fbd5426d..d43c0e8981979 100644
--- a/include/linux/ethtool.h
+++ b/include/linux/ethtool.h
@@ -1577,4 +1577,10 @@ struct ethtool_forced_speed_map {
void
ethtool_forced_speed_maps_init(struct ethtool_forced_speed_map *maps, u32 size);
+
+static inline u32 ethtool_flow_type_mask(u32 flow_type)
+{
+ return flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
+}
+
#endif /* _LINUX_ETHTOOL_H */
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (7 preceding siblings ...)
2026-12-02 15:04 ` [PATCH net-next v24 08/14] ethtool: Introduce ethtool_flow_type_mask() Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-03 13:19 ` Michael S. Tsirkin
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:04 ` [PATCH net-next v24 10/14] virtio_net: Use existing classifier if possible Shahar Shitrit
` (5 subsequent siblings)
14 siblings, 2 replies; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
Filtering a flow requires a classifier to match the packets, and a rule
to filter on the matches.
A classifier consists of one or more selectors. There is one selector
per header type. A selector must only use fields set in the selector
capability. If partial matching is supported, the classifier mask for a
particular field can be a subset of the mask for that field in the
capability.
The rule consists of a priority, an action and a key. The key is a byte
array containing headers corresponding to the selectors in the
classifier.
This patch implements ethtool rules for ethernet headers.
Example:
$ ethtool -U ens9 flow-type ether dst 08:11:22:33:44:54 action 30
Added rule with ID 1
The rule in the example directs received packets with the specified
destination MAC address to rq 30.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio_net.c | 535 +++++++++++++++++++++++++++--
include/uapi/linux/virtio_net_ff.h | 50 +++
2 files changed, 557 insertions(+), 28 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index d5ca5a4d4378c..e9caed7f0ef66 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -31,6 +31,7 @@
#include <net/ipv6.h>
#include <net/ip.h>
#include <uapi/linux/virtio_net_ff.h>
+#include <linux/xarray.h>
static int napi_weight = NAPI_POLL_WEIGHT;
module_param(napi_weight, int, 0444);
@@ -288,6 +289,11 @@ static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
};
+struct virtnet_ethtool_ff {
+ struct xarray rules;
+ int num_rules;
+};
+
#define VIRTNET_FF_ETHTOOL_GROUP_PRIORITY 0
#define VIRTNET_FF_MAX_GROUPS 1
@@ -297,6 +303,8 @@ struct virtnet_ff {
struct virtio_net_ff_cap_data *ff_caps;
struct virtio_net_ff_cap_mask_data *ff_mask;
struct virtio_net_ff_actions *ff_actions;
+ struct xarray classifiers;
+ struct virtnet_ethtool_ff ethtool;
};
#define VIRTNET_Q_TYPE_RX 0
@@ -5688,34 +5696,6 @@ static u32 virtnet_get_rx_ring_count(struct net_device *dev)
return vi->curr_queue_pairs;
}
-static const struct ethtool_ops virtnet_ethtool_ops = {
- .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
- ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
- .get_drvinfo = virtnet_get_drvinfo,
- .get_link = ethtool_op_get_link,
- .get_ringparam = virtnet_get_ringparam,
- .set_ringparam = virtnet_set_ringparam,
- .get_strings = virtnet_get_strings,
- .get_sset_count = virtnet_get_sset_count,
- .get_ethtool_stats = virtnet_get_ethtool_stats,
- .set_channels = virtnet_set_channels,
- .get_channels = virtnet_get_channels,
- .get_ts_info = ethtool_op_get_ts_info,
- .get_link_ksettings = virtnet_get_link_ksettings,
- .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,
- .set_rxfh = virtnet_set_rxfh,
- .get_rxfh_fields = virtnet_get_hashflow,
- .set_rxfh_fields = virtnet_set_hashflow,
- .get_rx_ring_count = virtnet_get_rx_ring_count,
-};
-
static void virtnet_get_queue_stats_rx(struct net_device *dev, int i,
struct netdev_queue_stats_rx *stats)
{
@@ -5812,6 +5792,451 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
.get_base_stats = virtnet_get_base_stats,
};
+struct virtnet_ethtool_rule {
+ struct ethtool_rx_flow_spec flow_spec;
+ u32 classifier_id;
+};
+
+/* The classifier struct must be the last field in this struct */
+struct virtnet_classifier {
+ size_t size;
+ u32 id;
+ struct virtio_net_resource_obj_ff_classifier classifier;
+};
+
+static_assert(sizeof(struct virtnet_classifier) ==
+ ALIGN(offsetofend(struct virtnet_classifier, classifier),
+ __alignof__(struct virtnet_classifier)),
+ "virtnet_classifier: classifier must be the last member");
+
+static bool check_mask_vs_cap(const void *m, const void *c,
+ u16 len, bool partial)
+{
+ const u8 *mask = m;
+ const u8 *cap = c;
+ int i;
+
+ for (i = 0; i < len; i++) {
+ if (partial && ((mask[i] & cap[i]) != mask[i]))
+ return false;
+ if (!partial && mask[i] != cap[i])
+ return false;
+ }
+
+ return true;
+}
+
+static
+struct virtio_net_ff_selector *get_selector_cap(const struct virtnet_ff *ff,
+ u8 selector_type)
+{
+ struct virtio_net_ff_selector *sel;
+ void *buf;
+ int i;
+
+ buf = &ff->ff_mask->selectors;
+ sel = buf;
+
+ for (i = 0; i < ff->ff_mask->count; i++) {
+ if (sel->type == selector_type)
+ return sel;
+
+ buf += sizeof(struct virtio_net_ff_selector) + sel->length;
+ sel = buf;
+ }
+
+ return NULL;
+}
+
+static bool validate_eth_mask(const struct virtnet_ff *ff,
+ const struct virtio_net_ff_selector *sel,
+ const struct virtio_net_ff_selector *sel_cap)
+{
+ bool partial_mask = !!(sel_cap->flags & VIRTIO_NET_FF_MASK_F_PARTIAL_MASK);
+ struct ethhdr *cap, *mask;
+ struct ethhdr zeros = {};
+
+ cap = (struct ethhdr *)&sel_cap->mask;
+ mask = (struct ethhdr *)&sel->mask;
+
+ if (memcmp(&zeros.h_dest, mask->h_dest, sizeof(zeros.h_dest)) &&
+ !check_mask_vs_cap(mask->h_dest, cap->h_dest,
+ sizeof(mask->h_dest), partial_mask))
+ return false;
+
+ if (memcmp(&zeros.h_source, mask->h_source, sizeof(zeros.h_source)) &&
+ !check_mask_vs_cap(mask->h_source, cap->h_source,
+ sizeof(mask->h_source), partial_mask))
+ return false;
+
+ if (mask->h_proto &&
+ !check_mask_vs_cap(&mask->h_proto, &cap->h_proto,
+ sizeof(__be16), partial_mask))
+ return false;
+
+ return true;
+}
+
+static bool validate_mask(const struct virtnet_ff *ff,
+ const struct virtio_net_ff_selector *sel)
+{
+ struct virtio_net_ff_selector *sel_cap = get_selector_cap(ff, sel->type);
+
+ if (!sel_cap)
+ return false;
+
+ switch (sel->type) {
+ case VIRTIO_NET_FF_MASK_TYPE_ETH:
+ return validate_eth_mask(ff, sel, sel_cap);
+ }
+
+ return false;
+}
+
+static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c)
+{
+ int err;
+
+ err = xa_alloc(&ff->classifiers, &c->id, c,
+ XA_LIMIT(0, le32_to_cpu(ff->ff_caps->classifiers_limit) - 1),
+ GFP_KERNEL);
+ if (err)
+ return err;
+
+ err = virtio_admin_obj_create(ff->vdev,
+ VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
+ c->id,
+ VIRTIO_ADMIN_GROUP_TYPE_SELF,
+ 0,
+ &c->classifier,
+ c->size);
+ if (err)
+ goto err_xarray;
+
+ return 0;
+
+err_xarray:
+ xa_erase(&ff->classifiers, c->id);
+
+ return err;
+}
+
+static void destroy_classifier(struct virtnet_ff *ff,
+ u32 classifier_id)
+{
+ struct virtnet_classifier *c;
+ int err;
+
+ c = xa_load(&ff->classifiers, classifier_id);
+ if (c) {
+ err = virtio_admin_obj_destroy(ff->vdev,
+ VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
+ c->id,
+ VIRTIO_ADMIN_GROUP_TYPE_SELF,
+ 0);
+ WARN_ON_ONCE(err);
+
+ xa_erase(&ff->classifiers, c->id);
+ kfree(c);
+ }
+}
+
+static int destroy_ethtool_rule(struct virtnet_ff *ff,
+ struct virtnet_ethtool_rule *eth_rule)
+{
+ int err;
+
+ ff->ethtool.num_rules--;
+
+ err = virtio_admin_obj_destroy(ff->vdev,
+ VIRTIO_NET_RESOURCE_OBJ_FF_RULE,
+ eth_rule->flow_spec.location,
+ VIRTIO_ADMIN_GROUP_TYPE_SELF,
+ 0);
+
+ xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location);
+ destroy_classifier(ff, eth_rule->classifier_id);
+ kfree(eth_rule);
+
+ return err;
+}
+
+static int insert_rule(struct virtnet_ff *ff,
+ struct virtnet_ethtool_rule *eth_rule,
+ u32 classifier_id,
+ const u8 *key,
+ u8 key_size)
+{
+ struct ethtool_rx_flow_spec *fs = ð_rule->flow_spec;
+ struct virtio_net_resource_obj_ff_rule *ff_rule;
+ int err;
+
+ ff_rule = kzalloc(sizeof(*ff_rule) + key_size, GFP_KERNEL);
+ if (!ff_rule)
+ return -ENOMEM;
+
+ /* Intentionally leave the priority as 0. All rules have the same
+ * priority.
+ */
+ ff_rule->group_id = cpu_to_le32(VIRTNET_FF_ETHTOOL_GROUP_PRIORITY);
+ ff_rule->classifier_id = cpu_to_le32(classifier_id);
+ ff_rule->key_length = key_size;
+ ff_rule->action = fs->ring_cookie == RX_CLS_FLOW_DISC ?
+ VIRTIO_NET_FF_ACTION_DROP :
+ VIRTIO_NET_FF_ACTION_RX_VQ;
+ ff_rule->vq_index = fs->ring_cookie != RX_CLS_FLOW_DISC ?
+ cpu_to_le16(rxq2vq(fs->ring_cookie)) : 0;
+ memcpy(&ff_rule->keys, key, key_size);
+
+ err = virtio_admin_obj_create(ff->vdev,
+ VIRTIO_NET_RESOURCE_OBJ_FF_RULE,
+ fs->location,
+ VIRTIO_ADMIN_GROUP_TYPE_SELF,
+ 0,
+ ff_rule,
+ sizeof(*ff_rule) + key_size);
+ if (err)
+ goto err_ff_rule;
+
+ eth_rule->classifier_id = classifier_id;
+ ff->ethtool.num_rules++;
+ kfree(ff_rule);
+ kfree(key);
+
+ return 0;
+
+err_ff_rule:
+ kfree(ff_rule);
+
+ return err;
+}
+
+static bool supported_flow_type(const struct ethtool_rx_flow_spec *fs)
+{
+ switch (fs->flow_type) {
+ case ETHER_FLOW:
+ return true;
+ }
+
+ return false;
+}
+
+static int validate_flow_input(struct virtnet_ff *ff,
+ const struct ethtool_rx_flow_spec *fs,
+ u16 curr_queue_pairs)
+{
+ u8 required_action = fs->ring_cookie == RX_CLS_FLOW_DISC ?
+ VIRTIO_NET_FF_ACTION_DROP :
+ VIRTIO_NET_FF_ACTION_RX_VQ;
+ int i;
+
+ /* Force users to use RX_CLS_LOC_ANY - don't allow specific locations */
+ if (fs->location != RX_CLS_LOC_ANY)
+ return -EOPNOTSUPP;
+
+ if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
+ fs->ring_cookie >= curr_queue_pairs)
+ return -EINVAL;
+
+ for (i = 0; i < ff->ff_actions->count; i++)
+ if (ff->ff_actions->actions[i] == required_action)
+ goto action_ok;
+ return -EOPNOTSUPP;
+
+action_ok:
+ if (fs->flow_type != ethtool_flow_type_mask(fs->flow_type))
+ return -EOPNOTSUPP;
+
+ if (!supported_flow_type(fs))
+ return -EOPNOTSUPP;
+
+ return 0;
+}
+
+static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs,
+ u8 *key_size, size_t *classifier_size,
+ int *num_hdrs)
+{
+ *num_hdrs = 1;
+ *key_size = sizeof(struct ethhdr);
+ /*
+ * The classifier size is the size of the classifier header, a selector
+ * header for each type of header in the match criteria, and each header
+ * providing the mask for matching against.
+ */
+ *classifier_size = *key_size +
+ sizeof(struct virtio_net_resource_obj_ff_classifier) +
+ sizeof(struct virtio_net_ff_selector) * (*num_hdrs);
+}
+
+static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector,
+ u8 *key,
+ const struct ethtool_rx_flow_spec *fs)
+{
+ struct ethhdr *eth_m = (struct ethhdr *)&selector->mask;
+ struct ethhdr *eth_k = (struct ethhdr *)key;
+
+ selector->type = VIRTIO_NET_FF_MASK_TYPE_ETH;
+ selector->length = sizeof(struct ethhdr);
+
+ memcpy(eth_m, &fs->m_u.ether_spec, sizeof(*eth_m));
+ memcpy(eth_k, &fs->h_u.ether_spec, sizeof(*eth_k));
+}
+
+static int
+validate_classifier_selectors(struct virtnet_ff *ff,
+ struct virtio_net_resource_obj_ff_classifier *classifier,
+ int num_hdrs)
+{
+ struct virtio_net_ff_selector *selector = (void *)classifier->selectors;
+ int i;
+
+ if (num_hdrs > ff->ff_caps->selectors_per_classifier_limit)
+ return -EINVAL;
+
+ for (i = 0; i < num_hdrs; i++) {
+ if (!validate_mask(ff, selector))
+ return -EINVAL;
+
+ selector = (((void *)selector) + sizeof(*selector) +
+ selector->length);
+ }
+
+ return 0;
+}
+
+static int build_and_insert(struct virtnet_ff *ff,
+ struct virtnet_ethtool_rule *eth_rule)
+{
+ struct virtio_net_resource_obj_ff_classifier *classifier;
+ struct ethtool_rx_flow_spec *fs = ð_rule->flow_spec;
+ struct virtio_net_ff_selector *selector;
+ struct virtnet_classifier *c;
+ size_t classifier_size;
+ int num_hdrs;
+ u8 key_size;
+ u8 *key;
+ int err;
+
+ calculate_flow_sizes(fs, &key_size, &classifier_size, &num_hdrs);
+
+ key = kzalloc(key_size, GFP_KERNEL);
+ if (!key)
+ return -ENOMEM;
+
+ /*
+ * virtio_net_ff_obj_ff_classifier is already included in the
+ * classifier_size.
+ */
+ c = kzalloc(classifier_size +
+ sizeof(struct virtnet_classifier) -
+ sizeof(struct virtio_net_resource_obj_ff_classifier),
+ GFP_KERNEL);
+ if (!c) {
+ kfree(key);
+ return -ENOMEM;
+ }
+
+ c->size = classifier_size;
+ classifier = &c->classifier;
+ classifier->count = num_hdrs;
+ selector = (void *)&classifier->selectors[0];
+
+ setup_eth_hdr_key_mask(selector, key, fs);
+
+ err = validate_classifier_selectors(ff, classifier, num_hdrs);
+ if (err)
+ goto err_classifier;
+
+ err = setup_classifier(ff, c);
+ if (err)
+ goto err_classifier;
+
+ err = insert_rule(ff, eth_rule, c->id, key, key_size);
+ if (err) {
+ /* destroy_classifier will free the classifier */
+ destroy_classifier(ff, c->id);
+ goto err_key;
+ }
+
+ return 0;
+
+err_classifier:
+ kfree(c);
+err_key:
+ kfree(key);
+
+ return err;
+}
+
+static u32 virtnet_ff_rules_limit(const struct virtnet_ff *ff)
+{
+ return min(le32_to_cpu(ff->ff_caps->rules_limit),
+ le32_to_cpu(ff->ff_caps->rules_per_group_limit));
+}
+
+static int virtnet_ethtool_flow_insert(struct virtnet_ff *ff,
+ struct ethtool_rx_flow_spec *fs,
+ u16 curr_queue_pairs)
+{
+ struct virtnet_ethtool_rule *eth_rule;
+ int err;
+
+ if (!ff->ff_supported)
+ return -EOPNOTSUPP;
+
+ err = validate_flow_input(ff, fs, curr_queue_pairs);
+ if (err)
+ return err;
+
+ eth_rule = kzalloc(sizeof(*eth_rule), GFP_KERNEL);
+ if (!eth_rule)
+ return -ENOMEM;
+
+ err = xa_alloc(&ff->ethtool.rules, &fs->location, eth_rule,
+ XA_LIMIT(0, virtnet_ff_rules_limit(ff) - 1),
+ GFP_KERNEL);
+ if (err)
+ goto err_rule;
+
+ eth_rule->flow_spec = *fs;
+
+ err = build_and_insert(ff, eth_rule);
+ if (err)
+ goto err_xa;
+
+ return err;
+
+err_xa:
+ xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location);
+
+err_rule:
+ fs->location = RX_CLS_LOC_ANY;
+ kfree(eth_rule);
+
+ return err;
+}
+
+static int virtnet_ethtool_flow_remove(struct virtnet_ff *ff, int location)
+{
+ struct virtnet_ethtool_rule *eth_rule;
+ int err = 0;
+
+ if (!ff->ff_supported)
+ return -EOPNOTSUPP;
+
+ eth_rule = xa_load(&ff->ethtool.rules, location);
+ if (!eth_rule) {
+ err = -ENOENT;
+ goto out;
+ }
+
+ err = destroy_ethtool_rule(ff, eth_rule);
+
+out:
+ return err;
+}
+
static size_t get_mask_size(u16 type)
{
switch (type) {
@@ -5830,6 +6255,50 @@ static size_t get_mask_size(u16 type)
return 0;
}
+static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
+{
+ struct virtnet_info *vi = netdev_priv(dev);
+
+ switch (info->cmd) {
+ case ETHTOOL_SRXCLSRLINS:
+ return virtnet_ethtool_flow_insert(&vi->ff, &info->fs,
+ vi->curr_queue_pairs);
+ case ETHTOOL_SRXCLSRLDEL:
+ return virtnet_ethtool_flow_remove(&vi->ff, info->fs.location);
+ }
+
+ return -EOPNOTSUPP;
+}
+
+static const struct ethtool_ops virtnet_ethtool_ops = {
+ .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
+ ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
+ .get_drvinfo = virtnet_get_drvinfo,
+ .get_link = ethtool_op_get_link,
+ .get_ringparam = virtnet_get_ringparam,
+ .set_ringparam = virtnet_set_ringparam,
+ .get_strings = virtnet_get_strings,
+ .get_sset_count = virtnet_get_sset_count,
+ .get_ethtool_stats = virtnet_get_ethtool_stats,
+ .set_channels = virtnet_set_channels,
+ .get_channels = virtnet_get_channels,
+ .get_ts_info = ethtool_op_get_ts_info,
+ .get_link_ksettings = virtnet_get_link_ksettings,
+ .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,
+ .set_rxfh = virtnet_set_rxfh,
+ .get_rxfh_fields = virtnet_get_hashflow,
+ .set_rxfh_fields = virtnet_set_hashflow,
+ .get_rx_ring_count = virtnet_get_rx_ring_count,
+ .set_rxnfc = virtnet_set_rxnfc,
+};
+
static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
{
size_t ff_mask_size = sizeof(struct virtio_net_ff_cap_mask_data) +
@@ -5992,6 +6461,8 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
if (err)
goto err_ff_action;
+ xa_init_flags(&ff->classifiers, XA_FLAGS_ALLOC);
+ xa_init_flags(&ff->ethtool.rules, XA_FLAGS_ALLOC);
ff->vdev = vdev;
ff->ff_supported = true;
@@ -6016,11 +6487,19 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
static void virtnet_ff_cleanup(struct virtnet_ff *ff)
{
+ struct virtnet_ethtool_rule *eth_rule;
+ unsigned long i;
int err;
if (!ff->ff_supported)
return;
+ xa_for_each(&ff->ethtool.rules, i, eth_rule)
+ WARN_ON_ONCE(destroy_ethtool_rule(ff, eth_rule));
+
+ xa_destroy(&ff->ethtool.rules);
+ xa_destroy(&ff->classifiers);
+
err = virtio_admin_obj_destroy(ff->vdev,
VIRTIO_NET_RESOURCE_OBJ_FF_GROUP,
VIRTNET_FF_ETHTOOL_GROUP_PRIORITY,
diff --git a/include/uapi/linux/virtio_net_ff.h b/include/uapi/linux/virtio_net_ff.h
index b0780e49df9f0..9152021c99baa 100644
--- a/include/uapi/linux/virtio_net_ff.h
+++ b/include/uapi/linux/virtio_net_ff.h
@@ -13,6 +13,8 @@
#define VIRTIO_NET_FF_ACTION_CAP 0x802
#define VIRTIO_NET_RESOURCE_OBJ_FF_GROUP 0x0200
+#define VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER 0x0201
+#define VIRTIO_NET_RESOURCE_OBJ_FF_RULE 0x0202
/**
* struct virtio_net_ff_cap_data - Flow filter resource capability limits
@@ -103,4 +105,52 @@ struct virtio_net_resource_obj_ff_group {
__le16 group_priority;
};
+/**
+ * struct virtio_net_resource_obj_ff_classifier - Flow filter classifier object
+ * @count: number of selector entries in @selectors
+ * @reserved: must be set to 0 by the driver and ignored by the device
+ * @selectors: array of selector descriptors that define match masks
+ *
+ * Payload for the VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER administrative object.
+ * Each selector describes a header mask used to match packets
+ * (see struct virtio_net_ff_selector). Selectors appear in the order they are
+ * to be applied.
+ */
+struct virtio_net_resource_obj_ff_classifier {
+ __u8 count;
+ __u8 reserved[7];
+ __u8 selectors[];
+};
+
+/**
+ * struct virtio_net_resource_obj_ff_rule - Flow filter rule object
+ * @group_id: identifier of the target flow filter group
+ * @classifier_id: identifier of the classifier referenced by this rule
+ * @rule_priority: relative priority of this rule within the group
+ * @key_length: number of bytes in @keys
+ * @action: action to perform, one of VIRTIO_NET_FF_ACTION_*
+ * @reserved: must be set to 0 by the driver and ignored by the device
+ * @vq_index: RX virtqueue index for VIRTIO_NET_FF_ACTION_RX_VQ, 0 otherwise
+ * @reserved1: must be set to 0 by the driver and ignored by the device
+ * @keys: concatenated key bytes matching the classifier's selectors order
+ *
+ * Payload for the VIRTIO_NET_RESOURCE_OBJ_FF_RULE administrative object.
+ * @group_id and @classifier_id refer to previously created objects of types
+ * VIRTIO_NET_RESOURCE_OBJ_FF_GROUP and VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER
+ * respectively. The key bytes are compared against packet headers using the
+ * masks provided by the classifier's selectors. Multi-byte fields are
+ * little-endian.
+ */
+struct virtio_net_resource_obj_ff_rule {
+ __le32 group_id;
+ __le32 classifier_id;
+ __u8 rule_priority;
+ __u8 key_length; /* length of key in bytes */
+ __u8 action;
+ __u8 reserved;
+ __le16 vq_index;
+ __u8 reserved1[2];
+ __u8 keys[];
+};
+
#endif
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules
2026-12-02 15:04 ` [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules Shahar Shitrit
@ 2026-12-03 13:19 ` Michael S. Tsirkin
2026-12-03 15:51 ` netdev-bot+sashiko
1 sibling, 0 replies; 30+ messages in thread
From: Michael S. Tsirkin @ 2026-12-03 13:19 UTC (permalink / raw)
To: Shahar Shitrit
Cc: netdev, jasowang, pabeni, virtualization, parav, yohadt, xuanzhuo,
eperezma, jgg, kevin.tian, kuba, andrew+netdev, edumazet, danielj
On Wed, Dec 02, 2026 at 05:04:57PM +0200, Shahar Shitrit wrote:
> From: Daniel Jurgens <danielj@nvidia.com>
>
> Filtering a flow requires a classifier to match the packets, and a rule
> to filter on the matches.
>
> A classifier consists of one or more selectors. There is one selector
> per header type. A selector must only use fields set in the selector
> capability. If partial matching is supported, the classifier mask for a
> particular field can be a subset of the mask for that field in the
> capability.
>
> The rule consists of a priority, an action and a key. The key is a byte
> array containing headers corresponding to the selectors in the
> classifier.
>
> This patch implements ethtool rules for ethernet headers.
>
> Example:
> $ ethtool -U ens9 flow-type ether dst 08:11:22:33:44:54 action 30
> Added rule with ID 1
>
> The rule in the example directs received packets with the specified
> destination MAC address to rq 30.
>
> Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
> Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
> Reviewed-by: Parav Pandit <parav@nvidia.com>
> Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> ---
> drivers/net/virtio_net.c | 535 +++++++++++++++++++++++++++--
> include/uapi/linux/virtio_net_ff.h | 50 +++
> 2 files changed, 557 insertions(+), 28 deletions(-)
>
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index d5ca5a4d4378c..e9caed7f0ef66 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -31,6 +31,7 @@
> #include <net/ipv6.h>
> #include <net/ip.h>
> #include <uapi/linux/virtio_net_ff.h>
> +#include <linux/xarray.h>
>
> static int napi_weight = NAPI_POLL_WEIGHT;
> module_param(napi_weight, int, 0444);
> @@ -288,6 +289,11 @@ static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
> VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
> };
>
> +struct virtnet_ethtool_ff {
> + struct xarray rules;
> + int num_rules;
> +};
> +
> #define VIRTNET_FF_ETHTOOL_GROUP_PRIORITY 0
> #define VIRTNET_FF_MAX_GROUPS 1
>
> @@ -297,6 +303,8 @@ struct virtnet_ff {
> struct virtio_net_ff_cap_data *ff_caps;
> struct virtio_net_ff_cap_mask_data *ff_mask;
> struct virtio_net_ff_actions *ff_actions;
> + struct xarray classifiers;
> + struct virtnet_ethtool_ff ethtool;
> };
>
> #define VIRTNET_Q_TYPE_RX 0
> @@ -5688,34 +5696,6 @@ static u32 virtnet_get_rx_ring_count(struct net_device *dev)
> return vi->curr_queue_pairs;
> }
>
> -static const struct ethtool_ops virtnet_ethtool_ops = {
> - .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
> - ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
> - .get_drvinfo = virtnet_get_drvinfo,
> - .get_link = ethtool_op_get_link,
> - .get_ringparam = virtnet_get_ringparam,
> - .set_ringparam = virtnet_set_ringparam,
> - .get_strings = virtnet_get_strings,
> - .get_sset_count = virtnet_get_sset_count,
> - .get_ethtool_stats = virtnet_get_ethtool_stats,
> - .set_channels = virtnet_set_channels,
> - .get_channels = virtnet_get_channels,
> - .get_ts_info = ethtool_op_get_ts_info,
> - .get_link_ksettings = virtnet_get_link_ksettings,
> - .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,
> - .set_rxfh = virtnet_set_rxfh,
> - .get_rxfh_fields = virtnet_get_hashflow,
> - .set_rxfh_fields = virtnet_set_hashflow,
> - .get_rx_ring_count = virtnet_get_rx_ring_count,
> -};
> -
> static void virtnet_get_queue_stats_rx(struct net_device *dev, int i,
> struct netdev_queue_stats_rx *stats)
> {
> @@ -5812,6 +5792,451 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
> .get_base_stats = virtnet_get_base_stats,
> };
>
> +struct virtnet_ethtool_rule {
> + struct ethtool_rx_flow_spec flow_spec;
> + u32 classifier_id;
> +};
> +
> +/* The classifier struct must be the last field in this struct */
> +struct virtnet_classifier {
> + size_t size;
> + u32 id;
> + struct virtio_net_resource_obj_ff_classifier classifier;
> +};
> +
> +static_assert(sizeof(struct virtnet_classifier) ==
> + ALIGN(offsetofend(struct virtnet_classifier, classifier),
> + __alignof__(struct virtnet_classifier)),
> + "virtnet_classifier: classifier must be the last member");
> +
> +static bool check_mask_vs_cap(const void *m, const void *c,
> + u16 len, bool partial)
> +{
> + const u8 *mask = m;
> + const u8 *cap = c;
> + int i;
> +
> + for (i = 0; i < len; i++) {
> + if (partial && ((mask[i] & cap[i]) != mask[i]))
> + return false;
> + if (!partial && mask[i] != cap[i])
> + return false;
> + }
> +
> + return true;
> +}
> +
> +static
> +struct virtio_net_ff_selector *get_selector_cap(const struct virtnet_ff *ff,
> + u8 selector_type)
> +{
> + struct virtio_net_ff_selector *sel;
> + void *buf;
> + int i;
> +
> + buf = &ff->ff_mask->selectors;
> + sel = buf;
> +
> + for (i = 0; i < ff->ff_mask->count; i++) {
> + if (sel->type == selector_type)
> + return sel;
> +
> + buf += sizeof(struct virtio_net_ff_selector) + sel->length;
> + sel = buf;
> + }
> +
> + return NULL;
> +}
> +
> +static bool validate_eth_mask(const struct virtnet_ff *ff,
> + const struct virtio_net_ff_selector *sel,
> + const struct virtio_net_ff_selector *sel_cap)
> +{
> + bool partial_mask = !!(sel_cap->flags & VIRTIO_NET_FF_MASK_F_PARTIAL_MASK);
> + struct ethhdr *cap, *mask;
> + struct ethhdr zeros = {};
> +
> + cap = (struct ethhdr *)&sel_cap->mask;
> + mask = (struct ethhdr *)&sel->mask;
> +
> + if (memcmp(&zeros.h_dest, mask->h_dest, sizeof(zeros.h_dest)) &&
> + !check_mask_vs_cap(mask->h_dest, cap->h_dest,
> + sizeof(mask->h_dest), partial_mask))
> + return false;
> +
> + if (memcmp(&zeros.h_source, mask->h_source, sizeof(zeros.h_source)) &&
> + !check_mask_vs_cap(mask->h_source, cap->h_source,
> + sizeof(mask->h_source), partial_mask))
> + return false;
> +
> + if (mask->h_proto &&
> + !check_mask_vs_cap(&mask->h_proto, &cap->h_proto,
> + sizeof(__be16), partial_mask))
> + return false;
> +
> + return true;
> +}
> +
> +static bool validate_mask(const struct virtnet_ff *ff,
> + const struct virtio_net_ff_selector *sel)
> +{
> + struct virtio_net_ff_selector *sel_cap = get_selector_cap(ff, sel->type);
> +
> + if (!sel_cap)
> + return false;
> +
> + switch (sel->type) {
> + case VIRTIO_NET_FF_MASK_TYPE_ETH:
> + return validate_eth_mask(ff, sel, sel_cap);
> + }
> +
> + return false;
> +}
> +
> +static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c)
> +{
> + int err;
> +
> + err = xa_alloc(&ff->classifiers, &c->id, c,
> + XA_LIMIT(0, le32_to_cpu(ff->ff_caps->classifiers_limit) - 1),
> + GFP_KERNEL);
> + if (err)
> + return err;
> +
> + err = virtio_admin_obj_create(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
> + c->id,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0,
> + &c->classifier,
> + c->size);
> + if (err)
> + goto err_xarray;
> +
> + return 0;
> +
> +err_xarray:
> + xa_erase(&ff->classifiers, c->id);
> +
> + return err;
> +}
> +
> +static void destroy_classifier(struct virtnet_ff *ff,
> + u32 classifier_id)
> +{
> + struct virtnet_classifier *c;
> + int err;
> +
> + c = xa_load(&ff->classifiers, classifier_id);
> + if (c) {
> + err = virtio_admin_obj_destroy(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
> + c->id,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0);
> + WARN_ON_ONCE(err);
> +
> + xa_erase(&ff->classifiers, c->id);
> + kfree(c);
> + }
> +}
> +
> +static int destroy_ethtool_rule(struct virtnet_ff *ff,
> + struct virtnet_ethtool_rule *eth_rule)
> +{
> + int err;
> +
> + ff->ethtool.num_rules--;
> +
> + err = virtio_admin_obj_destroy(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_RULE,
> + eth_rule->flow_spec.location,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0);
> +
> + xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location);
> + destroy_classifier(ff, eth_rule->classifier_id);
> + kfree(eth_rule);
> +
> + return err;
> +}
> +
> +static int insert_rule(struct virtnet_ff *ff,
> + struct virtnet_ethtool_rule *eth_rule,
> + u32 classifier_id,
> + const u8 *key,
> + u8 key_size)
> +{
> + struct ethtool_rx_flow_spec *fs = ð_rule->flow_spec;
> + struct virtio_net_resource_obj_ff_rule *ff_rule;
> + int err;
> +
> + ff_rule = kzalloc(sizeof(*ff_rule) + key_size, GFP_KERNEL);
> + if (!ff_rule)
> + return -ENOMEM;
> +
> + /* Intentionally leave the priority as 0. All rules have the same
> + * priority.
> + */
> + ff_rule->group_id = cpu_to_le32(VIRTNET_FF_ETHTOOL_GROUP_PRIORITY);
> + ff_rule->classifier_id = cpu_to_le32(classifier_id);
> + ff_rule->key_length = key_size;
> + ff_rule->action = fs->ring_cookie == RX_CLS_FLOW_DISC ?
> + VIRTIO_NET_FF_ACTION_DROP :
> + VIRTIO_NET_FF_ACTION_RX_VQ;
> + ff_rule->vq_index = fs->ring_cookie != RX_CLS_FLOW_DISC ?
> + cpu_to_le16(rxq2vq(fs->ring_cookie)) : 0;
> + memcpy(&ff_rule->keys, key, key_size);
> +
> + err = virtio_admin_obj_create(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_RULE,
> + fs->location,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0,
> + ff_rule,
> + sizeof(*ff_rule) + key_size);
> + if (err)
> + goto err_ff_rule;
> +
> + eth_rule->classifier_id = classifier_id;
> + ff->ethtool.num_rules++;
> + kfree(ff_rule);
> + kfree(key);
> +
> + return 0;
> +
> +err_ff_rule:
> + kfree(ff_rule);
> +
> + return err;
> +}
> +
> +static bool supported_flow_type(const struct ethtool_rx_flow_spec *fs)
> +{
> + switch (fs->flow_type) {
> + case ETHER_FLOW:
> + return true;
> + }
> +
> + return false;
> +}
> +
> +static int validate_flow_input(struct virtnet_ff *ff,
> + const struct ethtool_rx_flow_spec *fs,
> + u16 curr_queue_pairs)
> +{
> + u8 required_action = fs->ring_cookie == RX_CLS_FLOW_DISC ?
> + VIRTIO_NET_FF_ACTION_DROP :
> + VIRTIO_NET_FF_ACTION_RX_VQ;
> + int i;
> +
> + /* Force users to use RX_CLS_LOC_ANY - don't allow specific locations */
> + if (fs->location != RX_CLS_LOC_ANY)
> + return -EOPNOTSUPP;
> +
> + if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
> + fs->ring_cookie >= curr_queue_pairs)
> + return -EINVAL;
> +
> + for (i = 0; i < ff->ff_actions->count; i++)
> + if (ff->ff_actions->actions[i] == required_action)
> + goto action_ok;
> + return -EOPNOTSUPP;
> +
> +action_ok:
> + if (fs->flow_type != ethtool_flow_type_mask(fs->flow_type))
> + return -EOPNOTSUPP;
> +
> + if (!supported_flow_type(fs))
> + return -EOPNOTSUPP;
> +
> + return 0;
> +}
> +
> +static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs,
> + u8 *key_size, size_t *classifier_size,
> + int *num_hdrs)
> +{
> + *num_hdrs = 1;
> + *key_size = sizeof(struct ethhdr);
> + /*
> + * The classifier size is the size of the classifier header, a selector
> + * header for each type of header in the match criteria, and each header
> + * providing the mask for matching against.
> + */
> + *classifier_size = *key_size +
> + sizeof(struct virtio_net_resource_obj_ff_classifier) +
> + sizeof(struct virtio_net_ff_selector) * (*num_hdrs);
> +}
> +
> +static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector,
> + u8 *key,
> + const struct ethtool_rx_flow_spec *fs)
> +{
> + struct ethhdr *eth_m = (struct ethhdr *)&selector->mask;
> + struct ethhdr *eth_k = (struct ethhdr *)key;
> +
> + selector->type = VIRTIO_NET_FF_MASK_TYPE_ETH;
> + selector->length = sizeof(struct ethhdr);
> +
> + memcpy(eth_m, &fs->m_u.ether_spec, sizeof(*eth_m));
> + memcpy(eth_k, &fs->h_u.ether_spec, sizeof(*eth_k));
> +}
> +
> +static int
> +validate_classifier_selectors(struct virtnet_ff *ff,
> + struct virtio_net_resource_obj_ff_classifier *classifier,
> + int num_hdrs)
> +{
> + struct virtio_net_ff_selector *selector = (void *)classifier->selectors;
> + int i;
> +
> + if (num_hdrs > ff->ff_caps->selectors_per_classifier_limit)
> + return -EINVAL;
> +
> + for (i = 0; i < num_hdrs; i++) {
> + if (!validate_mask(ff, selector))
> + return -EINVAL;
> +
> + selector = (((void *)selector) + sizeof(*selector) +
> + selector->length);
> + }
> +
> + return 0;
> +}
> +
> +static int build_and_insert(struct virtnet_ff *ff,
> + struct virtnet_ethtool_rule *eth_rule)
> +{
> + struct virtio_net_resource_obj_ff_classifier *classifier;
> + struct ethtool_rx_flow_spec *fs = ð_rule->flow_spec;
> + struct virtio_net_ff_selector *selector;
> + struct virtnet_classifier *c;
> + size_t classifier_size;
> + int num_hdrs;
> + u8 key_size;
> + u8 *key;
> + int err;
> +
> + calculate_flow_sizes(fs, &key_size, &classifier_size, &num_hdrs);
> +
> + key = kzalloc(key_size, GFP_KERNEL);
> + if (!key)
> + return -ENOMEM;
> +
> + /*
> + * virtio_net_ff_obj_ff_classifier is already included in the
> + * classifier_size.
> + */
> + c = kzalloc(classifier_size +
> + sizeof(struct virtnet_classifier) -
> + sizeof(struct virtio_net_resource_obj_ff_classifier),
> + GFP_KERNEL);
> + if (!c) {
> + kfree(key);
> + return -ENOMEM;
> + }
> +
> + c->size = classifier_size;
> + classifier = &c->classifier;
> + classifier->count = num_hdrs;
> + selector = (void *)&classifier->selectors[0];
> +
> + setup_eth_hdr_key_mask(selector, key, fs);
> +
> + err = validate_classifier_selectors(ff, classifier, num_hdrs);
> + if (err)
> + goto err_classifier;
> +
> + err = setup_classifier(ff, c);
> + if (err)
> + goto err_classifier;
> +
> + err = insert_rule(ff, eth_rule, c->id, key, key_size);
> + if (err) {
> + /* destroy_classifier will free the classifier */
> + destroy_classifier(ff, c->id);
> + goto err_key;
> + }
> +
> + return 0;
> +
> +err_classifier:
> + kfree(c);
> +err_key:
> + kfree(key);
> +
> + return err;
> +}
> +
> +static u32 virtnet_ff_rules_limit(const struct virtnet_ff *ff)
> +{
> + return min(le32_to_cpu(ff->ff_caps->rules_limit),
> + le32_to_cpu(ff->ff_caps->rules_per_group_limit));
> +}
> +
> +static int virtnet_ethtool_flow_insert(struct virtnet_ff *ff,
> + struct ethtool_rx_flow_spec *fs,
> + u16 curr_queue_pairs)
> +{
> + struct virtnet_ethtool_rule *eth_rule;
> + int err;
> +
> + if (!ff->ff_supported)
> + return -EOPNOTSUPP;
> +
> + err = validate_flow_input(ff, fs, curr_queue_pairs);
> + if (err)
> + return err;
> +
> + eth_rule = kzalloc(sizeof(*eth_rule), GFP_KERNEL);
> + if (!eth_rule)
> + return -ENOMEM;
> +
> + err = xa_alloc(&ff->ethtool.rules, &fs->location, eth_rule,
> + XA_LIMIT(0, virtnet_ff_rules_limit(ff) - 1),
> + GFP_KERNEL);
> + if (err)
> + goto err_rule;
> +
> + eth_rule->flow_spec = *fs;
> +
> + err = build_and_insert(ff, eth_rule);
> + if (err)
> + goto err_xa;
> +
> + return err;
> +
> +err_xa:
> + xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location);
> +
> +err_rule:
> + fs->location = RX_CLS_LOC_ANY;
> + kfree(eth_rule);
> +
> + return err;
> +}
> +
> +static int virtnet_ethtool_flow_remove(struct virtnet_ff *ff, int location)
> +{
> + struct virtnet_ethtool_rule *eth_rule;
> + int err = 0;
> +
> + if (!ff->ff_supported)
> + return -EOPNOTSUPP;
> +
> + eth_rule = xa_load(&ff->ethtool.rules, location);
> + if (!eth_rule) {
> + err = -ENOENT;
> + goto out;
> + }
> +
> + err = destroy_ethtool_rule(ff, eth_rule);
> +
> +out:
> + return err;
> +}
> +
> static size_t get_mask_size(u16 type)
> {
> switch (type) {
> @@ -5830,6 +6255,50 @@ static size_t get_mask_size(u16 type)
> return 0;
> }
>
> +static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
> +{
> + struct virtnet_info *vi = netdev_priv(dev);
> +
> + switch (info->cmd) {
> + case ETHTOOL_SRXCLSRLINS:
> + return virtnet_ethtool_flow_insert(&vi->ff, &info->fs,
> + vi->curr_queue_pairs);
> + case ETHTOOL_SRXCLSRLDEL:
> + return virtnet_ethtool_flow_remove(&vi->ff, info->fs.location);
> + }
> +
> + return -EOPNOTSUPP;
> +}
> +
> +static const struct ethtool_ops virtnet_ethtool_ops = {
> + .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
> + ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
> + .get_drvinfo = virtnet_get_drvinfo,
> + .get_link = ethtool_op_get_link,
> + .get_ringparam = virtnet_get_ringparam,
> + .set_ringparam = virtnet_set_ringparam,
> + .get_strings = virtnet_get_strings,
> + .get_sset_count = virtnet_get_sset_count,
> + .get_ethtool_stats = virtnet_get_ethtool_stats,
> + .set_channels = virtnet_set_channels,
> + .get_channels = virtnet_get_channels,
> + .get_ts_info = ethtool_op_get_ts_info,
> + .get_link_ksettings = virtnet_get_link_ksettings,
> + .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,
> + .set_rxfh = virtnet_set_rxfh,
> + .get_rxfh_fields = virtnet_get_hashflow,
> + .set_rxfh_fields = virtnet_set_hashflow,
> + .get_rx_ring_count = virtnet_get_rx_ring_count,
> + .set_rxnfc = virtnet_set_rxnfc,
> +};
> +
> static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
> {
> size_t ff_mask_size = sizeof(struct virtio_net_ff_cap_mask_data) +
> @@ -5992,6 +6461,8 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
> if (err)
> goto err_ff_action;
>
> + xa_init_flags(&ff->classifiers, XA_FLAGS_ALLOC);
> + xa_init_flags(&ff->ethtool.rules, XA_FLAGS_ALLOC);
> ff->vdev = vdev;
> ff->ff_supported = true;
>
> @@ -6016,11 +6487,19 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
>
> static void virtnet_ff_cleanup(struct virtnet_ff *ff)
> {
> + struct virtnet_ethtool_rule *eth_rule;
> + unsigned long i;
> int err;
>
> if (!ff->ff_supported)
> return;
>
> + xa_for_each(&ff->ethtool.rules, i, eth_rule)
> + WARN_ON_ONCE(destroy_ethtool_rule(ff, eth_rule));
This is not safe because destroy_ethtool_rule calls xa_erase.
You want xa_for_each_safe maybe.
> +
> + xa_destroy(&ff->ethtool.rules);
> + xa_destroy(&ff->classifiers);
> +
> err = virtio_admin_obj_destroy(ff->vdev,
> VIRTIO_NET_RESOURCE_OBJ_FF_GROUP,
> VIRTNET_FF_ETHTOOL_GROUP_PRIORITY,
> diff --git a/include/uapi/linux/virtio_net_ff.h b/include/uapi/linux/virtio_net_ff.h
> index b0780e49df9f0..9152021c99baa 100644
> --- a/include/uapi/linux/virtio_net_ff.h
> +++ b/include/uapi/linux/virtio_net_ff.h
> @@ -13,6 +13,8 @@
> #define VIRTIO_NET_FF_ACTION_CAP 0x802
>
> #define VIRTIO_NET_RESOURCE_OBJ_FF_GROUP 0x0200
> +#define VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER 0x0201
> +#define VIRTIO_NET_RESOURCE_OBJ_FF_RULE 0x0202
>
> /**
> * struct virtio_net_ff_cap_data - Flow filter resource capability limits
> @@ -103,4 +105,52 @@ struct virtio_net_resource_obj_ff_group {
> __le16 group_priority;
> };
>
> +/**
> + * struct virtio_net_resource_obj_ff_classifier - Flow filter classifier object
> + * @count: number of selector entries in @selectors
> + * @reserved: must be set to 0 by the driver and ignored by the device
> + * @selectors: array of selector descriptors that define match masks
> + *
> + * Payload for the VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER administrative object.
> + * Each selector describes a header mask used to match packets
> + * (see struct virtio_net_ff_selector). Selectors appear in the order they are
> + * to be applied.
> + */
> +struct virtio_net_resource_obj_ff_classifier {
> + __u8 count;
> + __u8 reserved[7];
> + __u8 selectors[];
> +};
> +
> +/**
> + * struct virtio_net_resource_obj_ff_rule - Flow filter rule object
> + * @group_id: identifier of the target flow filter group
> + * @classifier_id: identifier of the classifier referenced by this rule
> + * @rule_priority: relative priority of this rule within the group
> + * @key_length: number of bytes in @keys
> + * @action: action to perform, one of VIRTIO_NET_FF_ACTION_*
> + * @reserved: must be set to 0 by the driver and ignored by the device
> + * @vq_index: RX virtqueue index for VIRTIO_NET_FF_ACTION_RX_VQ, 0 otherwise
> + * @reserved1: must be set to 0 by the driver and ignored by the device
> + * @keys: concatenated key bytes matching the classifier's selectors order
> + *
> + * Payload for the VIRTIO_NET_RESOURCE_OBJ_FF_RULE administrative object.
> + * @group_id and @classifier_id refer to previously created objects of types
> + * VIRTIO_NET_RESOURCE_OBJ_FF_GROUP and VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER
> + * respectively. The key bytes are compared against packet headers using the
> + * masks provided by the classifier's selectors. Multi-byte fields are
> + * little-endian.
> + */
> +struct virtio_net_resource_obj_ff_rule {
> + __le32 group_id;
> + __le32 classifier_id;
> + __u8 rule_priority;
> + __u8 key_length; /* length of key in bytes */
> + __u8 action;
> + __u8 reserved;
> + __le16 vq_index;
> + __u8 reserved1[2];
> + __u8 keys[];
> +};
> +
> #endif
> --
> 2.49.0
>
^ permalink raw reply [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules
2026-12-02 15:04 ` [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules Shahar Shitrit
2026-12-03 13:19 ` Michael S. Tsirkin
@ 2026-12-03 15:51 ` netdev-bot+sashiko
1 sibling, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 3 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] virtio_net: __counted_by counter set after referencing mask in
setup_eth_hdr_key_mask()
- [Medium] Ethtool flow rules point the device at a fixed RX virtqueue
(insert_rule() programs vq_index = rxq2vq(fs->ring_cookie)).
- [Medium] User-configured ethtool flow rules are thrown away on every
freeze/restore (suspend/resume, hibernation) and never reinstalled.
- [Medium] destroy_ethtool_rule() drops the driver's record of a rule
even when the device fails to destroy it.
- [Low] The comment in build_and_insert() names the type
'virtio_net_ff_obj_ff_classifier', which does not exist anywhere in…
- [Low] The new kernel-doc for struct virtio_net_resource_obj_ff_rule in
include/uapi/linux/virtio_net_ff.h reads: "The key bytes are compared…
--
virtio_net: Implement layer 2 ethtool flow rules
This commit adds ETHTOOL_SRXCLSRLINS and ETHTOOL_SRXCLSRLDEL support to
virtio_net for ethernet header flow rules. Each rule creates a classifier
object and a rule object on the device through admin commands, and both
are tracked in xarrays. It also adds the classifier and rule object
layouts to the virtio_net_ff.h uAPI header.
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index d5ca5a4d4378c..e9caed7f0ef66 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
[ ... ]
> @@ -5812,6 +5792,451 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
[ ... ]
> +static void destroy_classifier(struct virtnet_ff *ff,
> + u32 classifier_id)
> +{
> + struct virtnet_classifier *c;
> + int err;
> +
> + c = xa_load(&ff->classifiers, classifier_id);
> + if (c) {
> + err = virtio_admin_obj_destroy(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
> + c->id,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0);
> + WARN_ON_ONCE(err);
> +
> + xa_erase(&ff->classifiers, c->id);
> + kfree(c);
> + }
> +}
> +
> +static int destroy_ethtool_rule(struct virtnet_ff *ff,
> + struct virtnet_ethtool_rule *eth_rule)
> +{
> + int err;
> +
> + ff->ethtool.num_rules--;
> +
> + err = virtio_admin_obj_destroy(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_RULE,
> + eth_rule->flow_spec.location,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0);
> +
> + xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location);
> + destroy_classifier(ff, eth_rule->classifier_id);
> + kfree(eth_rule);
[Severity: Medium]
What happens here if virtio_admin_obj_destroy() fails? The error goes
into err, but the xarray entry, the classifier and eth_rule are released
anyway, even though the device may still hold the rule.
destroy_classifier() then tries to destroy a classifier that the
surviving device rule may still reference. If that fails it only warns,
then erases the ID and frees the classifier.
virtnet_ethtool_flow_remove() returns the error to userspace, but a
retry finds nothing:
eth_rule = xa_load(&ff->ethtool.rules, location);
if (!eth_rule) {
err = -ENOENT;
So the rule is orphaned on the device and keeps steering or dropping
traffic.
Both xa_alloc() calls hand out the lowest free ID: ethtool.rules in
virtnet_ethtool_flow_insert() and ff->classifiers in setup_classifier().
That means the next insert reuses the stale rule and classifier IDs.
If the device rejects duplicate object IDs, could virtio_admin_obj_create()
fail for every later insert until the device is reset?
The destroy can fail with -ENOMEM from the allocation inside
virtio_admin_obj_destroy(), or with an admin queue or device error. The
same logic is still present at the end of the series in
try_destroy_classifier().
> +
> + return err;
> +}
[ ... ]
> + ff_rule->vq_index = fs->ring_cookie != RX_CLS_FLOW_DISC ?
> + cpu_to_le16(rxq2vq(fs->ring_cookie)) : 0;
[ ... ]
> +static int validate_flow_input(struct virtnet_ff *ff,
> + const struct ethtool_rx_flow_spec *fs,
> + u16 curr_queue_pairs)
> +{
[ ... ]
> + if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
> + fs->ring_cookie >= curr_queue_pairs)
> + return -EINVAL;
[Severity: Medium]
Is checking ring_cookie only at insert time enough? insert_rule()
programs a fixed vq_index = rxq2vq(fs->ring_cookie) into the device.
Nothing re-checks the installed rules if the number of queue pairs
shrinks later.
At this commit there is no .get_rxnfc, so ethtool_check_max_channel()
gets -EOPNOTSUPP from ethtool_get_max_rxnfc_channel() and treats the
highest ring in use as 0. That allows ethtool -L combined 1 after a
rule with action 30. The later "virtio_net: Add get ethtool flow rules
ops" commit adds get_rxnfc, which closes this path.
The XDP path is still open at the end of the series. While XDP is
attached, curr_queue_pairs is curr_qp + xdp_qp, so a rule that targets
one of the extra XDP rings passes this check. On XDP detach,
virtnet_xdp_set() does:
err = virtnet_set_queues(vi, curr_qp + xdp_qp);
with xdp_qp == 0 and no check against the installed rules.
For example, take 4 online CPUs and max_queue_pairs >= 8. Attaching XDP
makes curr_queue_pairs 8, so ethtool -U ... action 6 is accepted.
Detaching XDP then drops back to 4 queue pairs.
Would the device keep steering matching flows to an RX vq the driver
has disabled, while ethtool -u still lists the rule?
[ ... ]
> + /*
> + * virtio_net_ff_obj_ff_classifier is already included in the
> + * classifier_size.
> + */
[Severity: Low]
This isn't a bug, but no type named virtio_net_ff_obj_ff_classifier
exists. Should this comment refer to struct
virtio_net_resource_obj_ff_classifier, which is what gets subtracted
below? The wrong name is still there at the end of the series.
> + c = kzalloc(classifier_size +
> + sizeof(struct virtnet_classifier) -
> + sizeof(struct virtio_net_resource_obj_ff_classifier),
> + GFP_KERNEL);
[ ... ]
> @@ -6016,11 +6487,19 @@ static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev)
>
> static void virtnet_ff_cleanup(struct virtnet_ff *ff)
> {
> + struct virtnet_ethtool_rule *eth_rule;
> + unsigned long i;
> int err;
>
> if (!ff->ff_supported)
> return;
>
> + xa_for_each(&ff->ethtool.rules, i, eth_rule)
> + WARN_ON_ONCE(destroy_ethtool_rule(ff, eth_rule));
> +
> + xa_destroy(&ff->ethtool.rules);
> + xa_destroy(&ff->classifiers);
[Severity: Medium]
Does this loop throw away the user's ethtool flow rules on every
suspend/resume or hibernation cycle?
virtnet_freeze_down() calls virtnet_ff_cleanup(). That function now
destroys and frees every rule, then calls xa_destroy() on both xarrays.
On resume, virtnet_restore_up() calls virtnet_ff_init(). That only
re-reads the caps, recreates the group and initializes empty xarrays:
virtnet_freeze()
virtnet_freeze_down()
virtnet_ff_cleanup()
virtnet_restore()
virtnet_restore_up()
virtnet_ff_init()
Nothing replays the saved ethtool_rx_flow_spec entries. After resume,
all steering and drop rules are gone and nothing is logged. Traffic
that should be dropped is accepted, and steered flows fall back to RSS.
This is still the case at the end of the series.
[ ... ]
> diff --git a/include/uapi/linux/virtio_net_ff.h b/include/uapi/linux/virtio_net_ff.h
> index b0780e49df9f0..9152021c99baa 100644
> --- a/include/uapi/linux/virtio_net_ff.h
> +++ b/include/uapi/linux/virtio_net_ff.h
[ ... ]
> @@ -103,4 +105,52 @@ struct virtio_net_resource_obj_ff_group {
[ ... ]
> + * Payload for the VIRTIO_NET_RESOURCE_OBJ_FF_RULE administrative object.
> + * @group_id and @classifier_id refer to previously created objects of types
> + * VIRTIO_NET_RESOURCE_OBJ_FF_GROUP and VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER
> + * respectively. The key bytes are compared against packet headers using the
> + * masks provided by the classifier's selectors. Multi-byte fields are
> + * little-endian.
> + */
[Severity: Low]
This isn't a bug, but "Multi-byte fields are little-endian" comes right
after the sentence about key bytes. It reads as if it also covers
keys[] and the selector masks.
setup_eth_hdr_key_mask() copies ethtool's network-order header images
(for example the __be16 h_proto) into keys[] and selector->mask
unchanged. Only group_id, classifier_id and vq_index are little-endian.
Could the comment say which fields it means? Otherwise a device
implementation written from this header might match EtherTypes
byte-swapped.
[ ... ]
> @@ -5812,6 +5792,451 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
> .get_base_stats = virtnet_get_base_stats,
> };
>
> +struct virtnet_ethtool_rule {
> + struct ethtool_rx_flow_spec flow_spec;
> + u32 classifier_id;
> +};
> +
> +/* The classifier struct must be the last field in this struct */
> +struct virtnet_classifier {
> + size_t size;
> + u32 id;
> + struct virtio_net_resource_obj_ff_classifier classifier;
> +};
> +
> +static_assert(sizeof(struct virtnet_classifier) ==
> + ALIGN(offsetofend(struct virtnet_classifier, classifier),
> + __alignof__(struct virtnet_classifier)),
> + "virtnet_classifier: classifier must be the last member");
> +
> +static bool check_mask_vs_cap(const void *m, const void *c,
> + u16 len, bool partial)
> +{
> + const u8 *mask = m;
> + const u8 *cap = c;
> + int i;
> +
> + for (i = 0; i < len; i++) {
> + if (partial && ((mask[i] & cap[i]) != mask[i]))
> + return false;
> + if (!partial && mask[i] != cap[i])
> + return false;
> + }
> +
> + return true;
> +}
> +
> +static
> +struct virtio_net_ff_selector *get_selector_cap(const struct virtnet_ff *ff,
> + u8 selector_type)
> +{
> + struct virtio_net_ff_selector *sel;
> + void *buf;
> + int i;
> +
> + buf = &ff->ff_mask->selectors;
> + sel = buf;
> +
> + for (i = 0; i < ff->ff_mask->count; i++) {
> + if (sel->type == selector_type)
> + return sel;
> +
> + buf += sizeof(struct virtio_net_ff_selector) + sel->length;
> + sel = buf;
> + }
> +
> + return NULL;
> +}
> +
> +static bool validate_eth_mask(const struct virtnet_ff *ff,
> + const struct virtio_net_ff_selector *sel,
> + const struct virtio_net_ff_selector *sel_cap)
> +{
> + bool partial_mask = !!(sel_cap->flags & VIRTIO_NET_FF_MASK_F_PARTIAL_MASK);
> + struct ethhdr *cap, *mask;
> + struct ethhdr zeros = {};
> +
> + cap = (struct ethhdr *)&sel_cap->mask;
> + mask = (struct ethhdr *)&sel->mask;
> +
> + if (memcmp(&zeros.h_dest, mask->h_dest, sizeof(zeros.h_dest)) &&
> + !check_mask_vs_cap(mask->h_dest, cap->h_dest,
> + sizeof(mask->h_dest), partial_mask))
> + return false;
> +
> + if (memcmp(&zeros.h_source, mask->h_source, sizeof(zeros.h_source)) &&
> + !check_mask_vs_cap(mask->h_source, cap->h_source,
> + sizeof(mask->h_source), partial_mask))
> + return false;
> +
> + if (mask->h_proto &&
> + !check_mask_vs_cap(&mask->h_proto, &cap->h_proto,
> + sizeof(__be16), partial_mask))
> + return false;
> +
> + return true;
> +}
> +
> +static bool validate_mask(const struct virtnet_ff *ff,
> + const struct virtio_net_ff_selector *sel)
> +{
> + struct virtio_net_ff_selector *sel_cap = get_selector_cap(ff, sel->type);
> +
> + if (!sel_cap)
> + return false;
> +
> + switch (sel->type) {
> + case VIRTIO_NET_FF_MASK_TYPE_ETH:
> + return validate_eth_mask(ff, sel, sel_cap);
> + }
> +
> + return false;
> +}
> +
> +static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c)
> +{
> + int err;
> +
> + err = xa_alloc(&ff->classifiers, &c->id, c,
> + XA_LIMIT(0, le32_to_cpu(ff->ff_caps->classifiers_limit) - 1),
> + GFP_KERNEL);
> + if (err)
> + return err;
> +
> + err = virtio_admin_obj_create(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
> + c->id,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0,
> + &c->classifier,
> + c->size);
> + if (err)
> + goto err_xarray;
> +
> + return 0;
> +
> +err_xarray:
> + xa_erase(&ff->classifiers, c->id);
> +
> + return err;
> +}
> +
> +static void destroy_classifier(struct virtnet_ff *ff,
> + u32 classifier_id)
> +{
> + struct virtnet_classifier *c;
> + int err;
> +
> + c = xa_load(&ff->classifiers, classifier_id);
> + if (c) {
> + err = virtio_admin_obj_destroy(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
> + c->id,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0);
> + WARN_ON_ONCE(err);
> +
> + xa_erase(&ff->classifiers, c->id);
> + kfree(c);
> + }
> +}
> +
> +static int destroy_ethtool_rule(struct virtnet_ff *ff,
> + struct virtnet_ethtool_rule *eth_rule)
> +{
> + int err;
> +
> + ff->ethtool.num_rules--;
> +
> + err = virtio_admin_obj_destroy(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_RULE,
> + eth_rule->flow_spec.location,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0);
> +
> + xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location);
> + destroy_classifier(ff, eth_rule->classifier_id);
> + kfree(eth_rule);
> +
> + return err;
> +}
> +
> +static int insert_rule(struct virtnet_ff *ff,
> + struct virtnet_ethtool_rule *eth_rule,
> + u32 classifier_id,
> + const u8 *key,
> + u8 key_size)
> +{
> + struct ethtool_rx_flow_spec *fs = ð_rule->flow_spec;
> + struct virtio_net_resource_obj_ff_rule *ff_rule;
> + int err;
> +
> + ff_rule = kzalloc(sizeof(*ff_rule) + key_size, GFP_KERNEL);
> + if (!ff_rule)
> + return -ENOMEM;
> +
> + /* Intentionally leave the priority as 0. All rules have the same
> + * priority.
> + */
> + ff_rule->group_id = cpu_to_le32(VIRTNET_FF_ETHTOOL_GROUP_PRIORITY);
> + ff_rule->classifier_id = cpu_to_le32(classifier_id);
> + ff_rule->key_length = key_size;
> + ff_rule->action = fs->ring_cookie == RX_CLS_FLOW_DISC ?
> + VIRTIO_NET_FF_ACTION_DROP :
> + VIRTIO_NET_FF_ACTION_RX_VQ;
> + ff_rule->vq_index = fs->ring_cookie != RX_CLS_FLOW_DISC ?
> + cpu_to_le16(rxq2vq(fs->ring_cookie)) : 0;
> + memcpy(&ff_rule->keys, key, key_size);
> +
> + err = virtio_admin_obj_create(ff->vdev,
> + VIRTIO_NET_RESOURCE_OBJ_FF_RULE,
> + fs->location,
> + VIRTIO_ADMIN_GROUP_TYPE_SELF,
> + 0,
> + ff_rule,
> + sizeof(*ff_rule) + key_size);
> + if (err)
> + goto err_ff_rule;
> +
> + eth_rule->classifier_id = classifier_id;
> + ff->ethtool.num_rules++;
> + kfree(ff_rule);
> + kfree(key);
> +
> + return 0;
> +
> +err_ff_rule:
> + kfree(ff_rule);
> +
> + return err;
> +}
> +
> +static bool supported_flow_type(const struct ethtool_rx_flow_spec *fs)
> +{
> + switch (fs->flow_type) {
> + case ETHER_FLOW:
> + return true;
> + }
> +
> + return false;
> +}
> +
> +static int validate_flow_input(struct virtnet_ff *ff,
> + const struct ethtool_rx_flow_spec *fs,
> + u16 curr_queue_pairs)
> +{
> + u8 required_action = fs->ring_cookie == RX_CLS_FLOW_DISC ?
> + VIRTIO_NET_FF_ACTION_DROP :
> + VIRTIO_NET_FF_ACTION_RX_VQ;
> + int i;
> +
> + /* Force users to use RX_CLS_LOC_ANY - don't allow specific locations */
> + if (fs->location != RX_CLS_LOC_ANY)
> + return -EOPNOTSUPP;
> +
> + if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
> + fs->ring_cookie >= curr_queue_pairs)
> + return -EINVAL;
> +
> + for (i = 0; i < ff->ff_actions->count; i++)
> + if (ff->ff_actions->actions[i] == required_action)
> + goto action_ok;
> + return -EOPNOTSUPP;
> +
> +action_ok:
> + if (fs->flow_type != ethtool_flow_type_mask(fs->flow_type))
> + return -EOPNOTSUPP;
> +
> + if (!supported_flow_type(fs))
> + return -EOPNOTSUPP;
> +
> + return 0;
> +}
> +
> +static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs,
> + u8 *key_size, size_t *classifier_size,
> + int *num_hdrs)
> +{
> + *num_hdrs = 1;
> + *key_size = sizeof(struct ethhdr);
> + /*
> + * The classifier size is the size of the classifier header, a selector
> + * header for each type of header in the match criteria, and each header
> + * providing the mask for matching against.
> + */
> + *classifier_size = *key_size +
> + sizeof(struct virtio_net_resource_obj_ff_classifier) +
> + sizeof(struct virtio_net_ff_selector) * (*num_hdrs);
> +}
> +
> +static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector,
> + u8 *key,
> + const struct ethtool_rx_flow_spec *fs)
> +{
> + struct ethhdr *eth_m = (struct ethhdr *)&selector->mask;
> + struct ethhdr *eth_k = (struct ethhdr *)key;
> +
> + selector->type = VIRTIO_NET_FF_MASK_TYPE_ETH;
> + selector->length = sizeof(struct ethhdr);
> +
> + memcpy(eth_m, &fs->m_u.ether_spec, sizeof(*eth_m));
> + memcpy(eth_k, &fs->h_u.ether_spec, sizeof(*eth_k));
> +}
> +
> +static int
> +validate_classifier_selectors(struct virtnet_ff *ff,
> + struct virtio_net_resource_obj_ff_classifier *classifier,
> + int num_hdrs)
> +{
> + struct virtio_net_ff_selector *selector = (void *)classifier->selectors;
> + int i;
> +
> + if (num_hdrs > ff->ff_caps->selectors_per_classifier_limit)
> + return -EINVAL;
> +
> + for (i = 0; i < num_hdrs; i++) {
> + if (!validate_mask(ff, selector))
> + return -EINVAL;
> +
> + selector = (((void *)selector) + sizeof(*selector) +
> + selector->length);
> + }
> +
> + return 0;
> +}
> +
> +static int build_and_insert(struct virtnet_ff *ff,
> + struct virtnet_ethtool_rule *eth_rule)
> +{
> + struct virtio_net_resource_obj_ff_classifier *classifier;
> + struct ethtool_rx_flow_spec *fs = ð_rule->flow_spec;
> + struct virtio_net_ff_selector *selector;
> + struct virtnet_classifier *c;
> + size_t classifier_size;
> + int num_hdrs;
> + u8 key_size;
> + u8 *key;
> + int err;
> +
> + calculate_flow_sizes(fs, &key_size, &classifier_size, &num_hdrs);
> +
> + key = kzalloc(key_size, GFP_KERNEL);
> + if (!key)
> + return -ENOMEM;
> +
> + /*
> + * virtio_net_ff_obj_ff_classifier is already included in the
> + * classifier_size.
> + */
> + c = kzalloc(classifier_size +
> + sizeof(struct virtnet_classifier) -
> + sizeof(struct virtio_net_resource_obj_ff_classifier),
> + GFP_KERNEL);
> + if (!c) {
> + kfree(key);
> + return -ENOMEM;
> + }
> +
> + c->size = classifier_size;
> + classifier = &c->classifier;
> + classifier->count = num_hdrs;
> + selector = (void *)&classifier->selectors[0];
> +
> + setup_eth_hdr_key_mask(selector, key, fs);
> +
> + err = validate_classifier_selectors(ff, classifier, num_hdrs);
> + if (err)
> + goto err_classifier;
> +
> + err = setup_classifier(ff, c);
> + if (err)
> + goto err_classifier;
> +
> + err = insert_rule(ff, eth_rule, c->id, key, key_size);
> + if (err) {
> + /* destroy_classifier will free the classifier */
> + destroy_classifier(ff, c->id);
> + goto err_key;
> + }
> +
> + return 0;
> +
> +err_classifier:
> + kfree(c);
> +err_key:
> + kfree(key);
> +
> + return err;
> +}
> +
> +static u32 virtnet_ff_rules_limit(const struct virtnet_ff *ff)
> +{
> + return min(le32_to_cpu(ff->ff_caps->rules_limit),
> + le32_to_cpu(ff->ff_caps->rules_per_group_limit));
> +}
> +
> +static int virtnet_ethtool_flow_insert(struct virtnet_ff *ff,
> + struct ethtool_rx_flow_spec *fs,
> + u16 curr_queue_pairs)
> +{
> + struct virtnet_ethtool_rule *eth_rule;
> + int err;
> +
> + if (!ff->ff_supported)
> + return -EOPNOTSUPP;
> +
> + err = validate_flow_input(ff, fs, curr_queue_pairs);
> + if (err)
> + return err;
> +
> + eth_rule = kzalloc(sizeof(*eth_rule), GFP_KERNEL);
> + if (!eth_rule)
> + return -ENOMEM;
> +
> + err = xa_alloc(&ff->ethtool.rules, &fs->location, eth_rule,
> + XA_LIMIT(0, virtnet_ff_rules_limit(ff) - 1),
> + GFP_KERNEL);
> + if (err)
> + goto err_rule;
> +
> + eth_rule->flow_spec = *fs;
> +
> + err = build_and_insert(ff, eth_rule);
> + if (err)
> + goto err_xa;
> +
> + return err;
> +
> +err_xa:
> + xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location);
> +
> +err_rule:
> + fs->location = RX_CLS_LOC_ANY;
> + kfree(eth_rule);
> +
> + return err;
> +}
> +
> +static int virtnet_ethtool_flow_remove(struct virtnet_ff *ff, int location)
> +{
> + struct virtnet_ethtool_rule *eth_rule;
> + int err = 0;
> +
> + if (!ff->ff_supported)
> + return -EOPNOTSUPP;
> +
> + eth_rule = xa_load(&ff->ethtool.rules, location);
> + if (!eth_rule) {
> + err = -ENOENT;
> + goto out;
> + }
> +
> + err = destroy_ethtool_rule(ff, eth_rule);
> +
> +out:
> + return err;
> +}
> +
> static size_t get_mask_size(u16 type)
> {
> switch (type) {
[Severity: High]
Is this ordering safe given the __counted_by(length) annotation on
mask in struct virtio_net_ff_selector?
eth_m is taken from &selector->mask while selector->length is still 0.
The zero comes from the kzalloc() of the classifier in
build_and_insert(). length is only assigned two lines later.
GCC 15+ and recent Clang support counted_by. With such a compiler and
CONFIG_FORTIFY_SOURCE=y, the object size of eth_m can be derived from
the value of length at the point where the pointer is formed. The
fortified memcpy(eth_m, &fs->m_u.ether_spec, sizeof(*eth_m)) would
then see a 0 byte destination for a 14 byte copy, and hit
fortify_panic().
On those builds, every ETHTOOL_SRXCLSRLINS with flow-type ether would
turn into a BUG() instead of adding a rule.
Could selector->type and selector->length be set before the mask
pointer is derived, for example by assigning eth_m after the length
store?
setup_transport_key_mask() later in the series has the same pattern.
It initializes tcp_m and udp_m from &selector->mask in their
declarations, before selector->length is set. Does it need the same
reordering?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 10/14] virtio_net: Use existing classifier if possible
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (8 preceding siblings ...)
2026-12-02 15:04 ` [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-03 13:20 ` Michael S. Tsirkin
2026-12-02 15:04 ` [PATCH net-next v24 11/14] virtio_net: Implement IPv4 ethtool flow rules Shahar Shitrit
` (4 subsequent siblings)
14 siblings, 1 reply; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
Classifiers can be used by more than one rule. If there is an existing
classifier, use it instead of creating a new one. If duplicate
classifiers are created it would artificially limit the number of rules
to the classifier limit, which is likely less than the rules limit.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
---
drivers/net/virtio_net.c | 50 +++++++++++++++++++++++++++-------------
1 file changed, 34 insertions(+), 16 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index e9caed7f0ef66..43c3e6750ad01 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -32,6 +32,7 @@
#include <net/ip.h>
#include <uapi/linux/virtio_net_ff.h>
#include <linux/xarray.h>
+#include <linux/refcount.h>
static int napi_weight = NAPI_POLL_WEIGHT;
module_param(napi_weight, int, 0444);
@@ -5800,12 +5801,13 @@ struct virtnet_ethtool_rule {
/* The classifier struct must be the last field in this struct */
struct virtnet_classifier {
size_t size;
+ refcount_t refcount;
u32 id;
- struct virtio_net_resource_obj_ff_classifier classifier;
+ struct virtio_net_resource_obj_ff_classifier obj;
};
static_assert(sizeof(struct virtnet_classifier) ==
- ALIGN(offsetofend(struct virtnet_classifier, classifier),
+ ALIGN(offsetofend(struct virtnet_classifier, obj),
__alignof__(struct virtnet_classifier)),
"virtnet_classifier: classifier must be the last member");
@@ -5893,11 +5895,24 @@ static bool validate_mask(const struct virtnet_ff *ff,
return false;
}
-static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c)
+static int setup_classifier(struct virtnet_ff *ff,
+ struct virtnet_classifier **c)
{
+ struct virtnet_classifier *tmp;
+ unsigned long i;
int err;
- err = xa_alloc(&ff->classifiers, &c->id, c,
+ xa_for_each(&ff->classifiers, i, tmp) {
+ if ((*c)->size == tmp->size &&
+ !memcmp(&tmp->obj, &(*c)->obj, tmp->size)) {
+ refcount_inc(&tmp->refcount);
+ kfree(*c);
+ *c = tmp;
+ goto out;
+ }
+ }
+
+ err = xa_alloc(&ff->classifiers, &(*c)->id, *c,
XA_LIMIT(0, le32_to_cpu(ff->ff_caps->classifiers_limit) - 1),
GFP_KERNEL);
if (err)
@@ -5905,30 +5920,31 @@ static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c)
err = virtio_admin_obj_create(ff->vdev,
VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
- c->id,
+ (*c)->id,
VIRTIO_ADMIN_GROUP_TYPE_SELF,
0,
- &c->classifier,
- c->size);
+ &(*c)->obj,
+ (*c)->size);
if (err)
goto err_xarray;
+ refcount_set(&(*c)->refcount, 1);
+out:
return 0;
err_xarray:
- xa_erase(&ff->classifiers, c->id);
+ xa_erase(&ff->classifiers, (*c)->id);
return err;
}
-static void destroy_classifier(struct virtnet_ff *ff,
- u32 classifier_id)
+static void try_destroy_classifier(struct virtnet_ff *ff, u32 classifier_id)
{
struct virtnet_classifier *c;
int err;
c = xa_load(&ff->classifiers, classifier_id);
- if (c) {
+ if (c && refcount_dec_and_test(&c->refcount)) {
err = virtio_admin_obj_destroy(ff->vdev,
VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
c->id,
@@ -5955,7 +5971,7 @@ static int destroy_ethtool_rule(struct virtnet_ff *ff,
0);
xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location);
- destroy_classifier(ff, eth_rule->classifier_id);
+ try_destroy_classifier(ff, eth_rule->classifier_id);
kfree(eth_rule);
return err;
@@ -6138,7 +6154,7 @@ static int build_and_insert(struct virtnet_ff *ff,
}
c->size = classifier_size;
- classifier = &c->classifier;
+ classifier = &c->obj;
classifier->count = num_hdrs;
selector = (void *)&classifier->selectors[0];
@@ -6148,14 +6164,16 @@ static int build_and_insert(struct virtnet_ff *ff,
if (err)
goto err_classifier;
- err = setup_classifier(ff, c);
+ err = setup_classifier(ff, &c);
if (err)
goto err_classifier;
err = insert_rule(ff, eth_rule, c->id, key, key_size);
if (err) {
- /* destroy_classifier will free the classifier */
- destroy_classifier(ff, c->id);
+ /* try_destroy_classifier will decrement the refcount on the
+ * classifier and free it if needed.
+ */
+ try_destroy_classifier(ff, c->id);
goto err_key;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 10/14] virtio_net: Use existing classifier if possible
2026-12-02 15:04 ` [PATCH net-next v24 10/14] virtio_net: Use existing classifier if possible Shahar Shitrit
@ 2026-12-03 13:20 ` Michael S. Tsirkin
0 siblings, 0 replies; 30+ messages in thread
From: Michael S. Tsirkin @ 2026-12-03 13:20 UTC (permalink / raw)
To: Shahar Shitrit
Cc: netdev, jasowang, pabeni, virtualization, parav, yohadt, xuanzhuo,
eperezma, jgg, kevin.tian, kuba, andrew+netdev, edumazet, danielj
On Wed, Dec 02, 2026 at 05:04:58PM +0200, Shahar Shitrit wrote:
> From: Daniel Jurgens <danielj@nvidia.com>
>
> Classifiers can be used by more than one rule. If there is an existing
> classifier, use it instead of creating a new one. If duplicate
> classifiers are created it would artificially limit the number of rules
> to the classifier limit, which is likely less than the rules limit.
>
> Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
> Reviewed-by: Parav Pandit <parav@nvidia.com>
> Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
> ---
> drivers/net/virtio_net.c | 50 +++++++++++++++++++++++++++-------------
> 1 file changed, 34 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index e9caed7f0ef66..43c3e6750ad01 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -32,6 +32,7 @@
> #include <net/ip.h>
> #include <uapi/linux/virtio_net_ff.h>
> #include <linux/xarray.h>
> +#include <linux/refcount.h>
>
> static int napi_weight = NAPI_POLL_WEIGHT;
> module_param(napi_weight, int, 0444);
> @@ -5800,12 +5801,13 @@ struct virtnet_ethtool_rule {
> /* The classifier struct must be the last field in this struct */
> struct virtnet_classifier {
> size_t size;
> + refcount_t refcount;
> u32 id;
> - struct virtio_net_resource_obj_ff_classifier classifier;
> + struct virtio_net_resource_obj_ff_classifier obj;
> };
>
> static_assert(sizeof(struct virtnet_classifier) ==
> - ALIGN(offsetofend(struct virtnet_classifier, classifier),
> + ALIGN(offsetofend(struct virtnet_classifier, obj),
> __alignof__(struct virtnet_classifier)),
> "virtnet_classifier: classifier must be the last member");
>
> @@ -5893,11 +5895,24 @@ static bool validate_mask(const struct virtnet_ff *ff,
> return false;
> }
>
> -static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c)
> +static int setup_classifier(struct virtnet_ff *ff,
> + struct virtnet_classifier **c)
> {
> + struct virtnet_classifier *tmp;
> + unsigned long i;
> int err;
>
> - err = xa_alloc(&ff->classifiers, &c->id, c,
> + xa_for_each(&ff->classifiers, i, tmp) {
So adding N classifiers is O(N^2).
What kind of N can we expect?
> + if ((*c)->size == tmp->size &&
> + !memcmp(&tmp->obj, &(*c)->obj, tmp->size)) {
> + refcount_inc(&tmp->refcount);
> + kfree(*c);
> + *c = tmp;
> + goto out;
> + }
> + }
> +
> + err = xa_alloc(&ff->classifiers, &(*c)->id, *c,
> XA_LIMIT(0, le32_to_cpu(ff->ff_caps->classifiers_limit) - 1),
> GFP_KERNEL);
> if (err)
> @@ -5905,30 +5920,31 @@ static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c)
>
> err = virtio_admin_obj_create(ff->vdev,
> VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
> - c->id,
> + (*c)->id,
> VIRTIO_ADMIN_GROUP_TYPE_SELF,
> 0,
> - &c->classifier,
> - c->size);
> + &(*c)->obj,
> + (*c)->size);
> if (err)
> goto err_xarray;
>
> + refcount_set(&(*c)->refcount, 1);
> +out:
> return 0;
>
> err_xarray:
> - xa_erase(&ff->classifiers, c->id);
> + xa_erase(&ff->classifiers, (*c)->id);
>
> return err;
> }
>
> -static void destroy_classifier(struct virtnet_ff *ff,
> - u32 classifier_id)
> +static void try_destroy_classifier(struct virtnet_ff *ff, u32 classifier_id)
> {
> struct virtnet_classifier *c;
> int err;
>
> c = xa_load(&ff->classifiers, classifier_id);
> - if (c) {
> + if (c && refcount_dec_and_test(&c->refcount)) {
> err = virtio_admin_obj_destroy(ff->vdev,
> VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER,
> c->id,
> @@ -5955,7 +5971,7 @@ static int destroy_ethtool_rule(struct virtnet_ff *ff,
> 0);
>
> xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location);
> - destroy_classifier(ff, eth_rule->classifier_id);
> + try_destroy_classifier(ff, eth_rule->classifier_id);
> kfree(eth_rule);
>
> return err;
> @@ -6138,7 +6154,7 @@ static int build_and_insert(struct virtnet_ff *ff,
> }
>
> c->size = classifier_size;
> - classifier = &c->classifier;
> + classifier = &c->obj;
> classifier->count = num_hdrs;
> selector = (void *)&classifier->selectors[0];
>
> @@ -6148,14 +6164,16 @@ static int build_and_insert(struct virtnet_ff *ff,
> if (err)
> goto err_classifier;
>
> - err = setup_classifier(ff, c);
> + err = setup_classifier(ff, &c);
> if (err)
> goto err_classifier;
>
> err = insert_rule(ff, eth_rule, c->id, key, key_size);
> if (err) {
> - /* destroy_classifier will free the classifier */
> - destroy_classifier(ff, c->id);
> + /* try_destroy_classifier will decrement the refcount on the
> + * classifier and free it if needed.
> + */
> + try_destroy_classifier(ff, c->id);
> goto err_key;
> }
>
> --
> 2.49.0
>
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 11/14] virtio_net: Implement IPv4 ethtool flow rules
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (9 preceding siblings ...)
2026-12-02 15:04 ` [PATCH net-next v24 10/14] virtio_net: Use existing classifier if possible Shahar Shitrit
@ 2026-12-02 15:04 ` Shahar Shitrit
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:05 ` [PATCH net-next v24 12/14] virtio_net: Add support for IPv6 ethtool steering Shahar Shitrit
` (3 subsequent siblings)
14 siblings, 1 reply; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:04 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
Add support for IP_USER type rules from ethtool.
Example:
$ ethtool -U ens9 flow-type ip4 src-ip 192.168.51.101 action -1
Added rule with ID 1
The example rule will drop packets with the source IP specified.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
---
drivers/net/virtio_net.c | 129 +++++++++++++++++++++++++++++++++++++--
1 file changed, 124 insertions(+), 5 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 43c3e6750ad01..8456ae3010200 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -33,6 +33,7 @@
#include <uapi/linux/virtio_net_ff.h>
#include <linux/xarray.h>
#include <linux/refcount.h>
+#include <linux/unaligned.h>
static int napi_weight = NAPI_POLL_WEIGHT;
module_param(napi_weight, int, 0444);
@@ -5879,6 +5880,39 @@ static bool validate_eth_mask(const struct virtnet_ff *ff,
return true;
}
+static bool validate_ip4_mask(const struct virtnet_ff *ff,
+ const struct virtio_net_ff_selector *sel,
+ const struct virtio_net_ff_selector *sel_cap)
+{
+ bool partial_mask = !!(sel_cap->flags & VIRTIO_NET_FF_MASK_F_PARTIAL_MASK);
+ struct iphdr *cap, *mask;
+
+ cap = (struct iphdr *)&sel_cap->mask;
+ mask = (struct iphdr *)&sel->mask;
+
+ if (get_unaligned(&mask->saddr) &&
+ !check_mask_vs_cap(&mask->saddr, &cap->saddr,
+ sizeof(__be32), partial_mask))
+ return false;
+
+ if (get_unaligned(&mask->daddr) &&
+ !check_mask_vs_cap(&mask->daddr, &cap->daddr,
+ sizeof(__be32), partial_mask))
+ return false;
+
+ if (mask->protocol &&
+ !check_mask_vs_cap(&mask->protocol, &cap->protocol,
+ sizeof(u8), partial_mask))
+ return false;
+
+ if (mask->tos &&
+ !check_mask_vs_cap(&mask->tos, &cap->tos,
+ sizeof(u8), partial_mask))
+ return false;
+
+ return true;
+}
+
static bool validate_mask(const struct virtnet_ff *ff,
const struct virtio_net_ff_selector *sel)
{
@@ -5890,11 +5924,41 @@ static bool validate_mask(const struct virtnet_ff *ff,
switch (sel->type) {
case VIRTIO_NET_FF_MASK_TYPE_ETH:
return validate_eth_mask(ff, sel, sel_cap);
+
+ case VIRTIO_NET_FF_MASK_TYPE_IPV4:
+ return validate_ip4_mask(ff, sel, sel_cap);
}
return false;
}
+static void parse_ip4(struct iphdr *mask, struct iphdr *key,
+ const struct ethtool_rx_flow_spec *fs)
+{
+ const struct ethtool_usrip4_spec *l3_mask = &fs->m_u.usr_ip4_spec;
+ const struct ethtool_usrip4_spec *l3_val = &fs->h_u.usr_ip4_spec;
+
+ if (l3_mask->ip4src) {
+ put_unaligned(l3_mask->ip4src, &mask->saddr);
+ put_unaligned(l3_val->ip4src, &key->saddr);
+ }
+
+ if (l3_mask->ip4dst) {
+ put_unaligned(l3_mask->ip4dst, &mask->daddr);
+ put_unaligned(l3_val->ip4dst, &key->daddr);
+ }
+
+ if (l3_mask->tos) {
+ mask->tos = l3_mask->tos;
+ key->tos = l3_val->tos;
+ }
+}
+
+static bool has_ipv4(u32 flow_type)
+{
+ return flow_type == IP_USER_FLOW;
+}
+
static int setup_classifier(struct virtnet_ff *ff,
struct virtnet_classifier **c)
{
@@ -6031,6 +6095,7 @@ static bool supported_flow_type(const struct ethtool_rx_flow_spec *fs)
{
switch (fs->flow_type) {
case ETHER_FLOW:
+ case IP_USER_FLOW:
return true;
}
@@ -6073,8 +6138,18 @@ static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs,
u8 *key_size, size_t *classifier_size,
int *num_hdrs)
{
+ size_t size = sizeof(struct ethhdr);
+
*num_hdrs = 1;
- *key_size = sizeof(struct ethhdr);
+
+ if (fs->flow_type != ETHER_FLOW) {
+ ++(*num_hdrs);
+ if (has_ipv4(fs->flow_type))
+ size += sizeof(struct iphdr);
+ }
+
+ BUG_ON(size > 0xff);
+ *key_size = size;
/*
* The classifier size is the size of the classifier header, a selector
* header for each type of header in the match criteria, and each header
@@ -6087,7 +6162,8 @@ static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs,
static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector,
u8 *key,
- const struct ethtool_rx_flow_spec *fs)
+ const struct ethtool_rx_flow_spec *fs,
+ int num_hdrs)
{
struct ethhdr *eth_m = (struct ethhdr *)&selector->mask;
struct ethhdr *eth_k = (struct ethhdr *)key;
@@ -6095,8 +6171,36 @@ static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector,
selector->type = VIRTIO_NET_FF_MASK_TYPE_ETH;
selector->length = sizeof(struct ethhdr);
- memcpy(eth_m, &fs->m_u.ether_spec, sizeof(*eth_m));
- memcpy(eth_k, &fs->h_u.ether_spec, sizeof(*eth_k));
+ if (num_hdrs > 1) {
+ eth_m->h_proto = cpu_to_be16(0xffff);
+ eth_k->h_proto = cpu_to_be16(ETH_P_IP);
+ } else {
+ memcpy(eth_m, &fs->m_u.ether_spec, sizeof(*eth_m));
+ memcpy(eth_k, &fs->h_u.ether_spec, sizeof(*eth_k));
+ }
+}
+
+static int setup_ip_key_mask(struct virtio_net_ff_selector *selector,
+ u8 *key,
+ const struct ethtool_rx_flow_spec *fs)
+{
+ struct iphdr *v4_m = (struct iphdr *)&selector->mask;
+ struct iphdr *v4_k = (struct iphdr *)key;
+
+ selector->type = VIRTIO_NET_FF_MASK_TYPE_IPV4;
+ selector->length = sizeof(struct iphdr);
+
+ if (fs->h_u.usr_ip4_spec.l4_4_bytes ||
+ fs->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4 ||
+ fs->m_u.usr_ip4_spec.l4_4_bytes ||
+ fs->m_u.usr_ip4_spec.ip_ver ||
+ fs->h_u.usr_ip4_spec.proto ||
+ fs->m_u.usr_ip4_spec.proto)
+ return -EINVAL;
+
+ parse_ip4(v4_m, v4_k, fs);
+
+ return 0;
}
static int
@@ -6121,6 +6225,13 @@ validate_classifier_selectors(struct virtnet_ff *ff,
return 0;
}
+static
+struct virtio_net_ff_selector *next_selector(struct virtio_net_ff_selector *sel)
+{
+ return (void *)sel + sizeof(struct virtio_net_ff_selector) +
+ sel->length;
+}
+
static int build_and_insert(struct virtnet_ff *ff,
struct virtnet_ethtool_rule *eth_rule)
{
@@ -6158,7 +6269,15 @@ static int build_and_insert(struct virtnet_ff *ff,
classifier->count = num_hdrs;
selector = (void *)&classifier->selectors[0];
- setup_eth_hdr_key_mask(selector, key, fs);
+ setup_eth_hdr_key_mask(selector, key, fs, num_hdrs);
+
+ if (has_ipv4(fs->flow_type)) {
+ selector = next_selector(selector);
+
+ err = setup_ip_key_mask(selector, key + sizeof(struct ethhdr), fs);
+ if (err)
+ goto err_classifier;
+ }
err = validate_classifier_selectors(ff, classifier, num_hdrs);
if (err)
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 11/14] virtio_net: Implement IPv4 ethtool flow rules
2026-12-02 15:04 ` [PATCH net-next v24 11/14] virtio_net: Implement IPv4 ethtool flow rules Shahar Shitrit
@ 2026-12-03 15:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] parse_ip4() in drivers/net/virtio_net.c copies the raw ethtool
h_u.usr_ip4_spec values (ip4src, ip4dst, tos) into the device rule key…
- [Low] In drivers/net/virtio_net.c, setup_ip_key_mask() (lines
6193-6199) returns -EINVAL whenever h_u.usr_ip4_spec.proto or…
- [Low] calculate_flow_sizes() in drivers/net/virtio_net.c adds a runtime
`BUG_ON(size > 0xff)` (line 6151) to guard the u8 key_size, which…
--
virtio_net: Implement IPv4 ethtool flow rules
This commit adds support for IP_USER_FLOW ethtool rules to the virtio_net
flow filter code. It builds a classifier with two selectors (Ethernet and
IPv4) and fills the IPv4 key and mask from the ethtool usr_ip4_spec fields.
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 43c3e6750ad01..8456ae3010200 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
[ ... ]
> @@ -5890,11 +5924,41 @@ static bool validate_mask(const struct virtnet_ff *ff,
[ ... ]
> +static void parse_ip4(struct iphdr *mask, struct iphdr *key,
> + const struct ethtool_rx_flow_spec *fs)
> +{
> + const struct ethtool_usrip4_spec *l3_mask = &fs->m_u.usr_ip4_spec;
> + const struct ethtool_usrip4_spec *l3_val = &fs->h_u.usr_ip4_spec;
> +
> + if (l3_mask->ip4src) {
> + put_unaligned(l3_mask->ip4src, &mask->saddr);
> + put_unaligned(l3_val->ip4src, &key->saddr);
> + }
> +
> + if (l3_mask->ip4dst) {
> + put_unaligned(l3_mask->ip4dst, &mask->daddr);
> + put_unaligned(l3_val->ip4dst, &key->daddr);
> + }
> +
> + if (l3_mask->tos) {
> + mask->tos = l3_mask->tos;
> + key->tos = l3_val->tos;
> + }
> +}
[Severity: Medium]
Should the key values be ANDed with the mask here?
The ethtool uAPI doesn't require h_u bits outside of m_u to be zero, and the
ethtool CLI passes the value through unmasked. For example:
ethtool -U dev flow-type ip4 src-ip 192.168.51.101 m 0.0.0.255 action N
reaches the driver with ip4src mask 255.255.255.0 and value 192.168.51.101.
If the device advertises VIRTIO_NET_FF_MASK_F_PARTIAL_MASK,
validate_ip4_mask() accepts this partial mask. insert_rule() then sends a
key that has bits set where the classifier mask is zero.
The device contract in include/uapi/linux/virtio_net_ff.h only says:
The key bytes are compared against packet headers using the
masks provided by the classifier's selectors.
It doesn't say whether key bits outside the mask are ignored. If a device
matches with (pkt & mask) == key, would these rules silently never match?
Rules that are equivalent after masking would also be stored as distinct
keys.
The ETHER_FLOW path in setup_eth_hdr_key_mask(), added earlier in this
series, follows the same pattern with its memcpy() of fs->h_u.ether_spec.
Would writing (val & mask) into the key remove the dependency on device
behaviour? parse_ip4() is unchanged at the end of the series, so no later
patch in the series addresses this.
[ ... ]
> @@ -6073,8 +6138,18 @@ static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs,
> u8 *key_size, size_t *classifier_size,
> int *num_hdrs)
> {
> + size_t size = sizeof(struct ethhdr);
> +
> *num_hdrs = 1;
> - *key_size = sizeof(struct ethhdr);
> +
> + if (fs->flow_type != ETHER_FLOW) {
> + ++(*num_hdrs);
> + if (has_ipv4(fs->flow_type))
> + size += sizeof(struct iphdr);
> + }
> +
> + BUG_ON(size > 0xff);
[Severity: Low]
This isn't a bug, but does this need a runtime BUG_ON() on the ethtool
ioctl path?
The size comes only from compile-time constants: 14 bytes for ethhdr, plus
20 for iphdr. The largest case at the end of the series is eth + ipv6 + tcp
= 74 bytes, so the condition can't be true today.
Could this be a BUILD_BUG_ON() on the largest header combination instead?
Another option is WARN_ON_ONCE() with an -EINVAL return, so that a future
flow type pushing the size past 255 can't panic the kernel.
> + *key_size = size;
[ ... ]
> @@ -6095,8 +6171,36 @@ static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector,
[ ... ]
> +static int setup_ip_key_mask(struct virtio_net_ff_selector *selector,
> + u8 *key,
> + const struct ethtool_rx_flow_spec *fs)
> +{
> + struct iphdr *v4_m = (struct iphdr *)&selector->mask;
> + struct iphdr *v4_k = (struct iphdr *)key;
> +
> + selector->type = VIRTIO_NET_FF_MASK_TYPE_IPV4;
> + selector->length = sizeof(struct iphdr);
> +
> + if (fs->h_u.usr_ip4_spec.l4_4_bytes ||
> + fs->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4 ||
> + fs->m_u.usr_ip4_spec.l4_4_bytes ||
> + fs->m_u.usr_ip4_spec.ip_ver ||
> + fs->h_u.usr_ip4_spec.proto ||
> + fs->m_u.usr_ip4_spec.proto)
> + return -EINVAL;
[Severity: Low]
Is this stricter than the ethtool uAPI requires?
In the rxnfc uAPI a zero mask means "don't care". struct
ethtool_usrip4_spec only places a rule on the mask:
@proto: Transport protocol number; mask must be 0
l4_4_bytes has no rule that its value must be zero either.
Because of the checks on h_u.usr_ip4_spec.proto and
h_u.usr_ip4_spec.l4_4_bytes, a request like this returns -EINVAL:
ethtool -U dev flow-type ip4 src-ip X l4proto 6 m 0xff action N
ethtool inverts the user mask, so this arrives as m_u.proto = 0 and
h_u.proto = 6. The proto field should just be ignored. The same thing
happens with "l4data V m 0xffffffff".
Other drivers, such as mlx5 and ice, check only the mask fields plus the
ip_ver value. Could the h_u.proto and h_u.l4_4_bytes checks be dropped?
At the end of the series, the num_hdrs == 2 branch of setup_ip_key_mask()
still has the same check.
> +
> + parse_ip4(v4_m, v4_k, fs);
> +
> + return 0;
> }
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 12/14] virtio_net: Add support for IPv6 ethtool steering
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (10 preceding siblings ...)
2026-12-02 15:04 ` [PATCH net-next v24 11/14] virtio_net: Implement IPv4 ethtool flow rules Shahar Shitrit
@ 2026-12-02 15:05 ` Shahar Shitrit
2026-12-02 15:05 ` [PATCH net-next v24 13/14] virtio_net: Add support for TCP and UDP ethtool rules Shahar Shitrit
` (2 subsequent siblings)
14 siblings, 0 replies; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:05 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
Implement support for IPV6_USER_FLOW type rules.
Example:
$ ethtool -U ens9 flow-type ip6 src-ip fe80::2 dst-ip fe80::4 action 3
Added rule with ID 0
The example rule will forward packets with the specified source and
destination IP addresses to RX ring 3.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio_net.c | 108 ++++++++++++++++++++++++++++++++++-----
1 file changed, 96 insertions(+), 12 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 8456ae3010200..814576383514e 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -5913,6 +5913,37 @@ static bool validate_ip4_mask(const struct virtnet_ff *ff,
return true;
}
+static bool validate_ip6_mask(const struct virtnet_ff *ff,
+ const struct virtio_net_ff_selector *sel,
+ const struct virtio_net_ff_selector *sel_cap)
+{
+ bool partial_mask = !!(sel_cap->flags & VIRTIO_NET_FF_MASK_F_PARTIAL_MASK);
+ struct in6_addr tmp;
+ struct ipv6hdr *cap, *mask;
+
+ cap = (struct ipv6hdr *)&sel_cap->mask;
+ mask = (struct ipv6hdr *)&sel->mask;
+
+ tmp = get_unaligned(&mask->saddr);
+ if (!ipv6_addr_any(&tmp) &&
+ !check_mask_vs_cap(&mask->saddr, &cap->saddr,
+ sizeof(cap->saddr), partial_mask))
+ return false;
+
+ tmp = get_unaligned(&mask->daddr);
+ if (!ipv6_addr_any(&tmp) &&
+ !check_mask_vs_cap(&mask->daddr, &cap->daddr,
+ sizeof(cap->daddr), partial_mask))
+ return false;
+
+ if (mask->nexthdr &&
+ !check_mask_vs_cap(&mask->nexthdr, &cap->nexthdr,
+ sizeof(cap->nexthdr), partial_mask))
+ return false;
+
+ return true;
+}
+
static bool validate_mask(const struct virtnet_ff *ff,
const struct virtio_net_ff_selector *sel)
{
@@ -5927,6 +5958,9 @@ static bool validate_mask(const struct virtnet_ff *ff,
case VIRTIO_NET_FF_MASK_TYPE_IPV4:
return validate_ip4_mask(ff, sel, sel_cap);
+
+ case VIRTIO_NET_FF_MASK_TYPE_IPV6:
+ return validate_ip6_mask(ff, sel, sel_cap);
}
return false;
@@ -5954,11 +5988,37 @@ static void parse_ip4(struct iphdr *mask, struct iphdr *key,
}
}
+static void parse_ip6(struct ipv6hdr *mask, struct ipv6hdr *key,
+ const struct ethtool_rx_flow_spec *fs)
+{
+ const struct ethtool_usrip6_spec *l3_mask = &fs->m_u.usr_ip6_spec;
+ const struct ethtool_usrip6_spec *l3_val = &fs->h_u.usr_ip6_spec;
+
+ if (!ipv6_addr_any((struct in6_addr *)l3_mask->ip6src)) {
+ put_unaligned(*(const struct in6_addr *)l3_mask->ip6src,
+ &mask->saddr);
+ put_unaligned(*(const struct in6_addr *)l3_val->ip6src,
+ &key->saddr);
+ }
+
+ if (!ipv6_addr_any((struct in6_addr *)l3_mask->ip6dst)) {
+ put_unaligned(*(const struct in6_addr *)l3_mask->ip6dst,
+ &mask->daddr);
+ put_unaligned(*(const struct in6_addr *)l3_val->ip6dst,
+ &key->daddr);
+ }
+}
+
static bool has_ipv4(u32 flow_type)
{
return flow_type == IP_USER_FLOW;
}
+static bool has_ipv6(u32 flow_type)
+{
+ return flow_type == IPV6_USER_FLOW;
+}
+
static int setup_classifier(struct virtnet_ff *ff,
struct virtnet_classifier **c)
{
@@ -6096,6 +6156,7 @@ static bool supported_flow_type(const struct ethtool_rx_flow_spec *fs)
switch (fs->flow_type) {
case ETHER_FLOW:
case IP_USER_FLOW:
+ case IPV6_USER_FLOW:
return true;
}
@@ -6146,6 +6207,8 @@ static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs,
++(*num_hdrs);
if (has_ipv4(fs->flow_type))
size += sizeof(struct iphdr);
+ else if (has_ipv6(fs->flow_type))
+ size += sizeof(struct ipv6hdr);
}
BUG_ON(size > 0xff);
@@ -6173,7 +6236,10 @@ static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector,
if (num_hdrs > 1) {
eth_m->h_proto = cpu_to_be16(0xffff);
- eth_k->h_proto = cpu_to_be16(ETH_P_IP);
+ if (has_ipv4(fs->flow_type))
+ eth_k->h_proto = cpu_to_be16(ETH_P_IP);
+ else
+ eth_k->h_proto = cpu_to_be16(ETH_P_IPV6);
} else {
memcpy(eth_m, &fs->m_u.ether_spec, sizeof(*eth_m));
memcpy(eth_k, &fs->h_u.ether_spec, sizeof(*eth_k));
@@ -6184,21 +6250,39 @@ static int setup_ip_key_mask(struct virtio_net_ff_selector *selector,
u8 *key,
const struct ethtool_rx_flow_spec *fs)
{
+ struct ipv6hdr *v6_m = (struct ipv6hdr *)&selector->mask;
struct iphdr *v4_m = (struct iphdr *)&selector->mask;
+ struct ipv6hdr *v6_k = (struct ipv6hdr *)key;
struct iphdr *v4_k = (struct iphdr *)key;
- selector->type = VIRTIO_NET_FF_MASK_TYPE_IPV4;
- selector->length = sizeof(struct iphdr);
+ if (has_ipv6(fs->flow_type)) {
+ selector->type = VIRTIO_NET_FF_MASK_TYPE_IPV6;
+ selector->length = sizeof(struct ipv6hdr);
+
+ /* exclude tclass, it's not exposed directly in struct ipv6hdr */
+ if (fs->h_u.usr_ip6_spec.l4_4_bytes ||
+ fs->m_u.usr_ip6_spec.l4_4_bytes ||
+ fs->h_u.usr_ip6_spec.tclass ||
+ fs->m_u.usr_ip6_spec.tclass ||
+ fs->h_u.usr_ip6_spec.l4_proto ||
+ fs->m_u.usr_ip6_spec.l4_proto)
+ return -EINVAL;
- if (fs->h_u.usr_ip4_spec.l4_4_bytes ||
- fs->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4 ||
- fs->m_u.usr_ip4_spec.l4_4_bytes ||
- fs->m_u.usr_ip4_spec.ip_ver ||
- fs->h_u.usr_ip4_spec.proto ||
- fs->m_u.usr_ip4_spec.proto)
- return -EINVAL;
+ parse_ip6(v6_m, v6_k, fs);
+ } else {
+ selector->type = VIRTIO_NET_FF_MASK_TYPE_IPV4;
+ selector->length = sizeof(struct iphdr);
+
+ if (fs->h_u.usr_ip4_spec.l4_4_bytes ||
+ fs->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4 ||
+ fs->m_u.usr_ip4_spec.l4_4_bytes ||
+ fs->m_u.usr_ip4_spec.ip_ver ||
+ fs->h_u.usr_ip4_spec.proto ||
+ fs->m_u.usr_ip4_spec.proto)
+ return -EINVAL;
- parse_ip4(v4_m, v4_k, fs);
+ parse_ip4(v4_m, v4_k, fs);
+ }
return 0;
}
@@ -6271,7 +6355,7 @@ static int build_and_insert(struct virtnet_ff *ff,
setup_eth_hdr_key_mask(selector, key, fs, num_hdrs);
- if (has_ipv4(fs->flow_type)) {
+ if (has_ipv4(fs->flow_type) || has_ipv6(fs->flow_type)) {
selector = next_selector(selector);
err = setup_ip_key_mask(selector, key + sizeof(struct ethhdr), fs);
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* [PATCH net-next v24 13/14] virtio_net: Add support for TCP and UDP ethtool rules
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (11 preceding siblings ...)
2026-12-02 15:05 ` [PATCH net-next v24 12/14] virtio_net: Add support for IPv6 ethtool steering Shahar Shitrit
@ 2026-12-02 15:05 ` Shahar Shitrit
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:05 ` [PATCH net-next v24 14/14] virtio_net: Add get ethtool flow rules ops Shahar Shitrit
2026-12-02 15:08 ` [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support netdev-bot+sinfo
14 siblings, 1 reply; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:05 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
Implement TCP and UDP V4/V6 ethtool flow types.
Examples:
$ ethtool -U ens9 flow-type udp4 dst-ip 192.168.5.2 dst-port\
4321 action 20
Added rule with ID 4
This example directs IPv4 UDP traffic with the specified address and
port to queue 20.
$ ethtool -U ens9 flow-type tcp6 src-ip 2001:db8::1 src-port 1234 dst-ip\
2001:db8::2 dst-port 4321 action 12
Added rule with ID 5
This example directs IPv6 TCP traffic with the specified address and
port to queue 12.
Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>=
---
drivers/net/virtio_net.c | 225 ++++++++++++++++++++++++++++++++++++---
1 file changed, 210 insertions(+), 15 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 814576383514e..6fcc157464f3e 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -31,6 +31,8 @@
#include <net/ipv6.h>
#include <net/ip.h>
#include <uapi/linux/virtio_net_ff.h>
+#include <linux/tcp.h>
+#include <linux/udp.h>
#include <linux/xarray.h>
#include <linux/refcount.h>
#include <linux/unaligned.h>
@@ -5944,6 +5946,52 @@ static bool validate_ip6_mask(const struct virtnet_ff *ff,
return true;
}
+static bool validate_tcp_mask(const struct virtnet_ff *ff,
+ const struct virtio_net_ff_selector *sel,
+ const struct virtio_net_ff_selector *sel_cap)
+{
+ bool partial_mask = !!(sel_cap->flags & VIRTIO_NET_FF_MASK_F_PARTIAL_MASK);
+ struct tcphdr *cap, *mask;
+
+ cap = (struct tcphdr *)&sel_cap->mask;
+ mask = (struct tcphdr *)&sel->mask;
+
+ if (get_unaligned(&mask->source) &&
+ !check_mask_vs_cap(&mask->source, &cap->source,
+ sizeof(cap->source), partial_mask))
+ return false;
+
+ if (get_unaligned(&mask->dest) &&
+ !check_mask_vs_cap(&mask->dest, &cap->dest,
+ sizeof(cap->dest), partial_mask))
+ return false;
+
+ return true;
+}
+
+static bool validate_udp_mask(const struct virtnet_ff *ff,
+ const struct virtio_net_ff_selector *sel,
+ const struct virtio_net_ff_selector *sel_cap)
+{
+ bool partial_mask = !!(sel_cap->flags & VIRTIO_NET_FF_MASK_F_PARTIAL_MASK);
+ struct udphdr *cap, *mask;
+
+ cap = (struct udphdr *)&sel_cap->mask;
+ mask = (struct udphdr *)&sel->mask;
+
+ if (get_unaligned(&mask->source) &&
+ !check_mask_vs_cap(&mask->source, &cap->source,
+ sizeof(cap->source), partial_mask))
+ return false;
+
+ if (get_unaligned(&mask->dest) &&
+ !check_mask_vs_cap(&mask->dest, &cap->dest,
+ sizeof(cap->dest), partial_mask))
+ return false;
+
+ return true;
+}
+
static bool validate_mask(const struct virtnet_ff *ff,
const struct virtio_net_ff_selector *sel)
{
@@ -5961,11 +6009,47 @@ static bool validate_mask(const struct virtnet_ff *ff,
case VIRTIO_NET_FF_MASK_TYPE_IPV6:
return validate_ip6_mask(ff, sel, sel_cap);
+
+ case VIRTIO_NET_FF_MASK_TYPE_TCP:
+ return validate_tcp_mask(ff, sel, sel_cap);
+
+ case VIRTIO_NET_FF_MASK_TYPE_UDP:
+ return validate_udp_mask(ff, sel, sel_cap);
}
return false;
}
+static void set_tcp(struct tcphdr *mask, struct tcphdr *key,
+ __be16 psrc_m, __be16 psrc_k,
+ __be16 pdst_m, __be16 pdst_k)
+{
+ /* mask/key may be unaligned; use memcpy */
+ if (psrc_m) {
+ memcpy(&mask->source, &psrc_m, sizeof(mask->source));
+ memcpy(&key->source, &psrc_k, sizeof(key->source));
+ }
+ if (pdst_m) {
+ memcpy(&mask->dest, &pdst_m, sizeof(mask->dest));
+ memcpy(&key->dest, &pdst_k, sizeof(key->dest));
+ }
+}
+
+static void set_udp(struct udphdr *mask, struct udphdr *key,
+ __be16 psrc_m, __be16 psrc_k,
+ __be16 pdst_m, __be16 pdst_k)
+{
+ /* mask/key may be unaligned; use memcpy */
+ if (psrc_m) {
+ memcpy(&mask->source, &psrc_m, sizeof(mask->source));
+ memcpy(&key->source, &psrc_k, sizeof(key->source));
+ }
+ if (pdst_m) {
+ memcpy(&mask->dest, &pdst_m, sizeof(mask->dest));
+ memcpy(&key->dest, &pdst_k, sizeof(key->dest));
+ }
+}
+
static void parse_ip4(struct iphdr *mask, struct iphdr *key,
const struct ethtool_rx_flow_spec *fs)
{
@@ -6011,12 +6095,26 @@ static void parse_ip6(struct ipv6hdr *mask, struct ipv6hdr *key,
static bool has_ipv4(u32 flow_type)
{
- return flow_type == IP_USER_FLOW;
+ return flow_type == TCP_V4_FLOW ||
+ flow_type == UDP_V4_FLOW ||
+ flow_type == IP_USER_FLOW;
}
static bool has_ipv6(u32 flow_type)
{
- return flow_type == IPV6_USER_FLOW;
+ return flow_type == TCP_V6_FLOW ||
+ flow_type == UDP_V6_FLOW ||
+ flow_type == IPV6_USER_FLOW;
+}
+
+static bool has_tcp(u32 flow_type)
+{
+ return flow_type == TCP_V4_FLOW || flow_type == TCP_V6_FLOW;
+}
+
+static bool has_udp(u32 flow_type)
+{
+ return flow_type == UDP_V4_FLOW || flow_type == UDP_V6_FLOW;
}
static int setup_classifier(struct virtnet_ff *ff,
@@ -6157,6 +6255,10 @@ static bool supported_flow_type(const struct ethtool_rx_flow_spec *fs)
case ETHER_FLOW:
case IP_USER_FLOW:
case IPV6_USER_FLOW:
+ case TCP_V4_FLOW:
+ case TCP_V6_FLOW:
+ case UDP_V4_FLOW:
+ case UDP_V6_FLOW:
return true;
}
@@ -6209,6 +6311,12 @@ static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs,
size += sizeof(struct iphdr);
else if (has_ipv6(fs->flow_type))
size += sizeof(struct ipv6hdr);
+
+ if (has_tcp(fs->flow_type) || has_udp(fs->flow_type)) {
+ ++(*num_hdrs);
+ size += has_tcp(fs->flow_type) ? sizeof(struct tcphdr) :
+ sizeof(struct udphdr);
+ }
}
BUG_ON(size > 0xff);
@@ -6248,7 +6356,8 @@ static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector,
static int setup_ip_key_mask(struct virtio_net_ff_selector *selector,
u8 *key,
- const struct ethtool_rx_flow_spec *fs)
+ const struct ethtool_rx_flow_spec *fs,
+ int num_hdrs)
{
struct ipv6hdr *v6_m = (struct ipv6hdr *)&selector->mask;
struct iphdr *v4_m = (struct iphdr *)&selector->mask;
@@ -6260,28 +6369,100 @@ static int setup_ip_key_mask(struct virtio_net_ff_selector *selector,
selector->length = sizeof(struct ipv6hdr);
/* exclude tclass, it's not exposed directly in struct ipv6hdr */
- if (fs->h_u.usr_ip6_spec.l4_4_bytes ||
- fs->m_u.usr_ip6_spec.l4_4_bytes ||
- fs->h_u.usr_ip6_spec.tclass ||
+ if (fs->h_u.usr_ip6_spec.tclass ||
fs->m_u.usr_ip6_spec.tclass ||
- fs->h_u.usr_ip6_spec.l4_proto ||
- fs->m_u.usr_ip6_spec.l4_proto)
+ (num_hdrs == 2 && (fs->h_u.usr_ip6_spec.l4_4_bytes ||
+ fs->m_u.usr_ip6_spec.l4_4_bytes ||
+ fs->h_u.usr_ip6_spec.l4_proto ||
+ fs->m_u.usr_ip6_spec.l4_proto)))
return -EINVAL;
parse_ip6(v6_m, v6_k, fs);
+
+ if (num_hdrs > 2) {
+ v6_m->nexthdr = 0xff;
+ if (has_tcp(fs->flow_type))
+ v6_k->nexthdr = IPPROTO_TCP;
+ else
+ v6_k->nexthdr = IPPROTO_UDP;
+ }
} else {
selector->type = VIRTIO_NET_FF_MASK_TYPE_IPV4;
selector->length = sizeof(struct iphdr);
- if (fs->h_u.usr_ip4_spec.l4_4_bytes ||
- fs->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4 ||
- fs->m_u.usr_ip4_spec.l4_4_bytes ||
- fs->m_u.usr_ip4_spec.ip_ver ||
- fs->h_u.usr_ip4_spec.proto ||
- fs->m_u.usr_ip4_spec.proto)
+ if (num_hdrs == 2 &&
+ (fs->h_u.usr_ip4_spec.l4_4_bytes ||
+ fs->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4 ||
+ fs->m_u.usr_ip4_spec.l4_4_bytes ||
+ fs->m_u.usr_ip4_spec.ip_ver ||
+ fs->h_u.usr_ip4_spec.proto ||
+ fs->m_u.usr_ip4_spec.proto))
return -EINVAL;
parse_ip4(v4_m, v4_k, fs);
+
+ if (num_hdrs > 2) {
+ v4_m->protocol = 0xff;
+ if (has_tcp(fs->flow_type))
+ v4_k->protocol = IPPROTO_TCP;
+ else
+ v4_k->protocol = IPPROTO_UDP;
+ }
+ }
+
+ return 0;
+}
+
+static int setup_transport_key_mask(struct virtio_net_ff_selector *selector,
+ u8 *key,
+ struct ethtool_rx_flow_spec *fs)
+{
+ struct tcphdr *tcp_m = (struct tcphdr *)&selector->mask;
+ struct udphdr *udp_m = (struct udphdr *)&selector->mask;
+ const struct ethtool_tcpip6_spec *v6_l4_mask;
+ const struct ethtool_tcpip4_spec *v4_l4_mask;
+ const struct ethtool_tcpip6_spec *v6_l4_key;
+ const struct ethtool_tcpip4_spec *v4_l4_key;
+ struct tcphdr *tcp_k = (struct tcphdr *)key;
+ struct udphdr *udp_k = (struct udphdr *)key;
+
+ if (has_tcp(fs->flow_type)) {
+ selector->type = VIRTIO_NET_FF_MASK_TYPE_TCP;
+ selector->length = sizeof(struct tcphdr);
+
+ if (has_ipv6(fs->flow_type)) {
+ v6_l4_mask = &fs->m_u.tcp_ip6_spec;
+ v6_l4_key = &fs->h_u.tcp_ip6_spec;
+
+ set_tcp(tcp_m, tcp_k, v6_l4_mask->psrc, v6_l4_key->psrc,
+ v6_l4_mask->pdst, v6_l4_key->pdst);
+ } else {
+ v4_l4_mask = &fs->m_u.tcp_ip4_spec;
+ v4_l4_key = &fs->h_u.tcp_ip4_spec;
+
+ set_tcp(tcp_m, tcp_k, v4_l4_mask->psrc, v4_l4_key->psrc,
+ v4_l4_mask->pdst, v4_l4_key->pdst);
+ }
+
+ } else if (has_udp(fs->flow_type)) {
+ selector->type = VIRTIO_NET_FF_MASK_TYPE_UDP;
+ selector->length = sizeof(struct udphdr);
+
+ if (has_ipv6(fs->flow_type)) {
+ v6_l4_mask = &fs->m_u.udp_ip6_spec;
+ v6_l4_key = &fs->h_u.udp_ip6_spec;
+
+ set_udp(udp_m, udp_k, v6_l4_mask->psrc, v6_l4_key->psrc,
+ v6_l4_mask->pdst, v6_l4_key->pdst);
+ } else {
+ v4_l4_mask = &fs->m_u.udp_ip4_spec;
+ v4_l4_key = &fs->h_u.udp_ip4_spec;
+
+ set_udp(udp_m, udp_k, v4_l4_mask->psrc, v4_l4_key->psrc,
+ v4_l4_mask->pdst, v4_l4_key->pdst);
+ }
+ } else {
+ return -EOPNOTSUPP;
}
return 0;
@@ -6324,6 +6505,7 @@ static int build_and_insert(struct virtnet_ff *ff,
struct virtio_net_ff_selector *selector;
struct virtnet_classifier *c;
size_t classifier_size;
+ size_t key_offset;
int num_hdrs;
u8 key_size;
u8 *key;
@@ -6356,11 +6538,24 @@ static int build_and_insert(struct virtnet_ff *ff,
setup_eth_hdr_key_mask(selector, key, fs, num_hdrs);
if (has_ipv4(fs->flow_type) || has_ipv6(fs->flow_type)) {
+ key_offset = selector->length;
selector = next_selector(selector);
- err = setup_ip_key_mask(selector, key + sizeof(struct ethhdr), fs);
+ err = setup_ip_key_mask(selector, key + key_offset,
+ fs, num_hdrs);
if (err)
goto err_classifier;
+
+ if (has_udp(fs->flow_type) || has_tcp(fs->flow_type)) {
+ key_offset += selector->length;
+ selector = next_selector(selector);
+
+ err = setup_transport_key_mask(selector,
+ key + key_offset,
+ fs);
+ if (err)
+ goto err_classifier;
+ }
}
err = validate_classifier_selectors(ff, classifier, num_hdrs);
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 13/14] virtio_net: Add support for TCP and UDP ethtool rules
2026-12-02 15:05 ` [PATCH net-next v24 13/14] virtio_net: Add support for TCP and UDP ethtool rules Shahar Shitrit
@ 2026-12-03 15:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The last Signed-off-by trailer in commit "virtio_net: Add support
for TCP and UDP ethtool rules" is malformed.
--
virtio_net: Add support for TCP and UDP ethtool rules
This commit adds the TCP_V4_FLOW, TCP_V6_FLOW, UDP_V4_FLOW and UDP_V6_FLOW
ethtool flow types to the virtio_net flow filter support. For these flow
types, the classifier gets a third selector for the transport header,
matching on source and destination ports. The IP protocol or nexthdr field
is set to match TCP or UDP.
> Signed-off-by: Daniel Jurgens <danielj@nvidia.com>
> Reviewed-by: Parav Pandit <parav@nvidia.com>
> Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>=
[Severity: Low]
This isn't a bug, but the last Signed-off-by trailer has a stray "=" after
the closing angle bracket. It looks like a quoted-printable soft line break
left over from mail transport.
Could the trailer be fixed to end at the closing angle bracket when this
is applied or resent?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH net-next v24 14/14] virtio_net: Add get ethtool flow rules ops
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (12 preceding siblings ...)
2026-12-02 15:05 ` [PATCH net-next v24 13/14] virtio_net: Add support for TCP and UDP ethtool rules Shahar Shitrit
@ 2026-12-02 15:05 ` Shahar Shitrit
2026-12-03 15:51 ` netdev-bot+sashiko
2026-12-02 15:08 ` [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support netdev-bot+sinfo
14 siblings, 1 reply; 30+ messages in thread
From: Shahar Shitrit @ 2026-12-02 15:05 UTC (permalink / raw)
To: netdev, mst, jasowang, pabeni
Cc: virtualization, parav, shshitrit, yohadt, xuanzhuo, eperezma, jgg,
kevin.tian, kuba, andrew+netdev, edumazet, danielj
From: Daniel Jurgens <danielj@nvidia.com>
- Get total number of rules via ETHTOOL_GRXCLSRLCNT. It is
used to allocate an appropriately sized buffer for getting all the
rules.
- Get specific rule
$ ethtool -u ens9 rule 0
Filter: 0
Rule Type: UDP over IPv4
Src IP addr: 0.0.0.0 mask: 255.255.255.255
Dest IP addr: 192.168.5.2 mask: 0.0.0.0
TOS: 0x0 mask: 0xff
Src port: 0 mask: 0xffff
Dest port: 4321 mask: 0x0
Action: Direct to queue 16
- Get all rules:
$ ethtool -u ens9
31 RX rings available
Total 2 rules
Filter: 0
Rule Type: UDP over IPv4
Src IP addr: 0.0.0.0 mask: 255.255.255.255
Dest IP addr: 192.168.5.2 mask: 0.0.0.0
...
Filter: 1
Flow Type: Raw Ethernet
Src MAC addr: 00:00:00:00:00:00 mask: FF:FF:FF:FF:FF:FF
Dest MAC addr: 08:11:22:33:44:54 mask: 00:00:00:00:00:00
Signed-off-by: Dniel Jurgens <danielj@nvidia.com>
Signed-off-by: Shahar Shitrit <shshitrit@nvidia.com>
Reviewed-by: Parav Pandit <parav@nvidia.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio_net.c | 75 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 75 insertions(+)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 6fcc157464f3e..a5fe4a5b383ed 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -6653,6 +6653,58 @@ static int virtnet_ethtool_flow_remove(struct virtnet_ff *ff, int location)
return err;
}
+static int virtnet_ethtool_get_flow_count(struct virtnet_ff *ff,
+ struct ethtool_rxnfc *info)
+{
+ if (!ff->ff_supported)
+ return -EOPNOTSUPP;
+
+ info->rule_cnt = ff->ethtool.num_rules;
+ info->data = virtnet_ff_rules_limit(ff) | RX_CLS_LOC_SPECIAL;
+
+ return 0;
+}
+
+static int virtnet_ethtool_get_flow(struct virtnet_ff *ff,
+ struct ethtool_rxnfc *info)
+{
+ struct virtnet_ethtool_rule *eth_rule;
+
+ if (!ff->ff_supported)
+ return -EOPNOTSUPP;
+
+ eth_rule = xa_load(&ff->ethtool.rules, info->fs.location);
+ if (!eth_rule)
+ return -ENOENT;
+
+ info->fs = eth_rule->flow_spec;
+
+ return 0;
+}
+
+static int
+virtnet_ethtool_get_all_flows(struct virtnet_ff *ff,
+ struct ethtool_rxnfc *info, u32 *rule_locs)
+{
+ struct virtnet_ethtool_rule *eth_rule;
+ unsigned long i = 0;
+ int idx = 0;
+
+ if (!ff->ff_supported)
+ return -EOPNOTSUPP;
+
+ xa_for_each(&ff->ethtool.rules, i, eth_rule) {
+ if (idx == info->rule_cnt)
+ return -EMSGSIZE;
+ rule_locs[idx++] = i;
+ }
+
+ info->data = virtnet_ff_rules_limit(ff);
+ info->rule_cnt = idx;
+
+ return 0;
+}
+
static size_t get_mask_size(u16 type)
{
switch (type) {
@@ -6671,6 +6723,28 @@ static size_t get_mask_size(u16 type)
return 0;
}
+static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
+{
+ struct virtnet_info *vi = netdev_priv(dev);
+ int rc;
+
+ switch (info->cmd) {
+ case ETHTOOL_GRXCLSRLCNT:
+ rc = virtnet_ethtool_get_flow_count(&vi->ff, info);
+ break;
+ case ETHTOOL_GRXCLSRULE:
+ rc = virtnet_ethtool_get_flow(&vi->ff, info);
+ break;
+ case ETHTOOL_GRXCLSRLALL:
+ rc = virtnet_ethtool_get_all_flows(&vi->ff, info, rule_locs);
+ break;
+ default:
+ rc = -EOPNOTSUPP;
+ }
+
+ return rc;
+}
+
static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
{
struct virtnet_info *vi = netdev_priv(dev);
@@ -6712,6 +6786,7 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
.get_rxfh_fields = virtnet_get_hashflow,
.set_rxfh_fields = virtnet_set_hashflow,
.get_rx_ring_count = virtnet_get_rx_ring_count,
+ .get_rxnfc = virtnet_get_rxnfc,
.set_rxnfc = virtnet_set_rxnfc,
};
--
2.49.0
^ permalink raw reply related [flat|nested] 30+ messages in thread* Re: [PATCH net-next v24 14/14] virtio_net: Add get ethtool flow rules ops
2026-12-02 15:05 ` [PATCH net-next v24 14/14] virtio_net: Add get ethtool flow rules ops Shahar Shitrit
@ 2026-12-03 15:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 30+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 15:51 UTC (permalink / raw)
To: shshitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The author's DCO sign-off in commit "virtio_net: Add get ethtool
flow rules ops" misspells the author's name.
- [Low] Registering `.get_rxnfc = virtnet_get_rxnfc` in
`virtnet_ethtool_ops` also changes channel-count behaviour, and the…
--
virtio_net: Add get ethtool flow rules ops
This adds a get_rxnfc ethtool op to virtio_net. It reports the rule count
(ETHTOOL_GRXCLSRLCNT), a single rule (ETHTOOL_GRXCLSRULE) and all rule
locations (ETHTOOL_GRXCLSRLALL), taken from the driver's ethtool flow
filter xarray.
> Signed-off-by: Dniel Jurgens <danielj@nvidia.com>
[Severity: Low]
This isn't a bug, but the author's sign-off has a typo: "Dniel" instead
of "Daniel". It doesn't match the Author: line, and checkpatch.pl reports
it as a From:/Signed-off-by: name mismatch.
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 6fcc157464f3e..a5fe4a5b383ed 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
[ ... ]
> @@ -6712,6 +6786,7 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
> .get_rxfh_fields = virtnet_get_hashflow,
> .set_rxfh_fields = virtnet_set_hashflow,
> .get_rx_ring_count = virtnet_get_rx_ring_count,
> + .get_rxnfc = virtnet_get_rxnfc,
> .set_rxnfc = virtnet_set_rxnfc,
> };
[Severity: Low]
Should the commit message mention that registering .get_rxnfc also
changes how ethtool -L behaves?
Before this patch, ethtool_get_max_rxnfc_channel() in
net/ethtool/common.c returned early:
if (!ops->get_rxnfc)
return -EOPNOTSUPP;
As a result, ethtool_check_max_channel() fell back to max_rxnfc_in_use = 0,
and flow rules never blocked a channel reduction.
With this op registered, the core now walks the installed rules, takes the
largest ring_cookie, and rejects the request in ethtool_check_max_channel():
if (channels.combined_count + channels.rx_count <= max_rxnfc_in_use) {
if (info)
GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing ntuple filter settings");
return -EINVAL;
}
The ioctl path, ethtool_set_channels(), and the netlink channels path
both apply this check.
The new behaviour looks correct. validate_flow_input() only checks
ring_cookie against curr_queue_pairs when a rule is inserted. Without
this op, a later "ethtool -L <dev> combined M" with M at or below a rule's
target queue would succeed and leave that rule steering to a queue the
driver no longer uses.
The commit message describes the patch only as read-only rule reporting.
Could it also say that the patch turns on the core's ntuple-aware channel
validation for virtio_net? Anyone backporting only the set_rxnfc patches
would then know they need this one as well.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support
2026-12-02 15:04 [PATCH net-next v24 00/14] virtio_net: Add ethtool flow rules support Shahar Shitrit
` (13 preceding siblings ...)
2026-12-02 15:05 ` [PATCH net-next v24 14/14] virtio_net: Add get ethtool flow rules ops Shahar Shitrit
@ 2026-12-02 15:08 ` netdev-bot+sinfo
[not found] ` <BL0PR12MB5505B05C081539D249C49DCCDA4B2@BL0PR12MB5505.namprd12.prod.outlook.com>
14 siblings, 1 reply; 30+ messages in thread
From: netdev-bot+sinfo @ 2026-12-02 15:08 UTC (permalink / raw)
To: Shahar Shitrit
Cc: netdev, mst, jasowang, pabeni, virtualization, parav, yohadt,
xuanzhuo, eperezma, jgg, kevin.tian, kuba, andrew+netdev,
edumazet, danielj
Hi!
This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical
(e.g. found by LLM and not proven at runtime). If it was triggered
please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
--
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
^ permalink raw reply [flat|nested] 30+ messages in thread