virtio_net: Add ethtool flow rules support
Subsystems (7)
v24 cover virtio_net: Add ethtool flow rules support
This series implements ethtool flow rules support for virtio_net using the virtio flow filter (FF) specification. The implementation allows users to configure packet filtering rules through ethtool commands, directing packets to specific receive queues, or dropping them based on various header fields.
The series starts with infrastructure changes to expose virtio PCI admin capabilities and object management APIs. It then creates the virtio_net directory structure and implements the flow filter functionality with support for:
- Layer 2 (Ethernet) flow rules
- IPv4 and IPv6 flow rules
- TCP and UDP flow rules (both IPv4 and IPv6)
- Rule querying and management operations
Setting, deleting and viewing flow filters, -1 action is drop, positive integers steer to that RQ:
$ ethtool -u ens9 4 RX rings available Total 0 rules
$ ethtool -U ens9 flow-type ether src 1c:34:da:4a:33:dd action 0 Added rule with ID 0 $ ethtool -U ens9 flow-type udp4 dst-port 5001 action 3 Added rule with ID 1 $ ethtool -U ens9 flow-type tcp6 src-ip fc00::2 dst-port 5001 action 2 Added rule with ID 2 $ ethtool -U ens9 flow-type ip4 src-ip 192.168.51.101 action 1 Added rule with ID 3 $ ethtool -U ens9 flow-type ip6 dst-ip fc00::1 action -1 Added rule with ID 4 $ ethtool -U ens9 flow-type ip6 src-ip fc00::2 action -1 Added rule with ID 5 $ ethtool -U ens9 delete 4 $ ethtool -u ens9 4 RX rings available Total 5 rules
Filter: 0
Flow Type: Raw Ethernet
Src MAC addr: 1C:34:DA:4A:33:DD mask: 00:00:00:00:00:00
Dest MAC addr: 00:00:00:00:00:00 mask: FF:FF:FF:FF:FF:FF
Ethertype: 0x0 mask: 0xFFFF
Action: Direct to queue 0Filter: 1
Rule Type: UDP over IPv4
Src IP addr: 0.0.0.0 mask: 255.255.255.255
Dest IP addr: 0.0.0.0 mask: 255.255.255.255
TOS: 0x0 mask: 0xff
Src port: 0 mask: 0xffff
Dest port: 5001 mask: 0x0
Action: Direct to queue 3Filter: 2
Rule Type: TCP over IPv6
Src IP addr: fc00::2 mask: ::
Dest IP addr: :: mask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
Traffic Class: 0x0 mask: 0xff
Src port: 0 mask: 0xffff
Dest port: 5001 mask: 0x0
Action: Direct to queue 2Filter: 3
Rule Type: Raw IPv4
Src IP addr: 192.168.51.101 mask: 0.0.0.0
Dest IP addr: 0.0.0.0 mask: 255.255.255.255
TOS: 0x0 mask: 0xff
Protocol: 0 mask: 0xff
L4 bytes: 0x0 mask: 0xffffffff
Action: Direct to queue 1Filter: 5
Rule Type: Raw IPv6
Src IP addr: fc00::2 mask: ::
Dest IP addr: :: mask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
Traffic Class: 0x0 mask: 0xff
Protocol: 0 mask: 0xff
L4 bytes: 0x0 mask: 0xffffffff
Action: Drop---
v2: https://lore.kernel.org/netdev/[email protected]/
- Fix sparse warnings
- Fix memory leak on subsequent failure to allocate
- Fix some Typos
v3: https://lore.kernel.org/netdev/[email protected]/
- Added admin_ops to virtio_device kdoc.
v4:
- Fixed double free bug inserting flows
- Fixed incorrect protocol field check parsing ip4 headers.
- (u8 *) changed to (void *)
- Added kdoc comments to UAPI changes.
- No longer split up virtio_net.c
- Added config op to execute admin commands.
- virtio_pci assigns vp_modern_admin_cmd_exec to this callback.
- Moved admin command API to new core file virtio_admin_commands.c
v5:
- Fixed compile error
- Fixed static analysis warning on () after macro
- Added missing fields to kdoc comments
- Aligned parameter name between prototype and kdoc
v6:
- Fix sparse warning "array of flexible structures" Jakub K/Simon H
- Use new variable and validate ff_mask_size before set_cap. MST
v7:
- Change virtnet_ff_init to return a value. Allow -EOPNOTSUPP. Xuan
- Set ff->ff_{caps, mask, actions} NULL in error path. Paolo Abini
- Move for (int i removal hung back a patch. Paolo Abini
v8
- Removed unused num_classifiers. Jason Wang
- Use real_ff_mask_size when setting the selector caps. Jason Wang
v9:
- Set err to -ENOMEM after alloc failures in virtnet_ff_init. Simon H
v10:
- Return -EOPNOTSUPP in virnet_ff_init before allocing any memory. Jason Wang/Paolo Abeni
v11:
- Return -EINVAL if any resource limit is 0. Simon Horman
- Ensure we don't overrun alloced space of ff->ff_mask by moving the real_ff_mask_size > ff_mask_size check into the loop. Simon Horman
v12: Many comments by MST, thanks Michael. Only the most significant
listed here:
- Fixed leak of key in build_and_insert.
- Fixed setting ethhdr proto for IPv6.
- Added 2 byte pad to struct virtio_net_ff_cap_data.
- Use and set rule_cnt when querying all flows.
- Cleanup and reinit in freeze/restore path.
v13:
- Add private comment for reserved field in kdoc. Jakub
- Serveral comments from MST details in patches. Most significant:
- Fixed bug in ip4, check l3_mask vs mask when setting addrs.
- Changed ff_mask cap checking to not break on expanded selector types
- Changed virtio_admin_obj_destroy to return void.
- Check tos field for ip4.
- Don't accept tclass field for ip6.
- If ip6 only flow check that l4_proto isn't set.
v14:
- Handle virtio_ff_init errors in freeze/restore. MST
- Don't set proto in parse_ip4/6. The casted struct may not have that field, and the proto field was set explicitly anyway. Simon H/AI.
v15:
- In virtnet_restore_up only call virtnet_close in err path if netif_running. AI
v16:
- Return 0 from virtnet_restore_up if virtnet_init_ff return not supported. AI
- Rebased over removing series to remove delayed refill.
v17:
- Properly handle unaligned reads/writes. MST
- Fix use after free if init fails during virtnet_restor. AI
- Fix memory leak when validating the classifer vs caps fails. AI
- Added missing includes. MSTA
v18:
- Validate selector cap lengths, instead of just checking they don't exceed a max. AI
- Add __count_by attribute to flexible arrays in UAPI definitions. Paolo A.
v19:
- Style fixes. AI
v20:
- Added missing include
v21:
- Use le64_to_cpu() and BIT_ULL() instead of cpu_to_le64() for cap checking.
- Don't use __counted_by on flexible array of flexible structs.
- Replace UAPI header includes with kernel header includes.
- Add missing includes for linux/types.h and linux/byteorder/generic.h.
- Clamp the reported action count to the driver-supported maximum.
- Clamp the reported selector count to the driver-supported maximum.
- Validate sel->type is not 0.
- Reduce selectors' count in case selector's type is invalid.
- Move virtio_device_ready() before virtnet_ff_init() as the flow filter initialization requires the device to be in ready state to issue admin commands.
- Remove forward declarations.
- Validate action is supported before inserting rule.
- Convert ring_cookie to vq before assigning ff_rule->vq_index.
- reword a comment.
- Introduce a new patch that moves flow_type_mask() to include/linux/ethtool.h.
- Wrap __le32 limit fields in le32_to_cpu() to avoid sparse warnings.
- Use put_unaligned() in parse_ip4() to avoid misaligned 32-bit stores on strict-alignment architectures.
v22:
- Reword/fix typo in commit messages.
- Remove include <uapi/linux/virtio_pci.h> from virtio_net.c.
- Verify also selectors_per_classifier_limit in virtnet_ff_init().
- Validate ff->ff_actions->count != 0.
- Remove WARN_ON_ONCE() and replace -EINVAL with -EPROTO for errors on device side.
- Use ff->ff_mask->count after it was initialized.
- Add a patch to fix sleeping under spinlock in the admin command path.
- Document that callers must zero-initialize the capability structure.
- Convert macro VIRTIO_CAP_IN_LIST to be inline function.
- Add WARN_ON_ONCE if allocation fails in virtio_admin_obj_destroy().
- Change VIRTNET_FF_ETHTOOL_GROUP_PRIORITY to be 0.
- Reject flow rules that require more selectors than the device supports (selectors_per_classifier_limit).
- Report min(rules_limit, rules_per_group_limit) as the effective rule limit, since all rules reside in a single group.
v23:
- Make virtio_admin_obj_destroy() return int instead of void + internal WARN_ON_ONCE, so callers can propagate the failure themselves.
v24:
- Rebase on top of virtio_pci/virtio patches
("virtio_pci_modern: move avq cleanup from reset to del_vqs", "virtio_pci: drop callback sync on reset" and
"virtio: synchronize callbacks after device reset").
- Use cond_resched() instead of cpu_relax() when the admin vq is full.
- Extend doc of @admin_cmd_exec in include/linux/virtio_config.h.
- Add module metadata to virtio_admin_commands.c to fix the build with CONFIG_VIRTIO=m.
- Document that caps in virtio_admin_cap_get() need to be zero-initialized.
- Return -EINVAL instead of 0 from virtnet_ff_init() when actions' count is 0.
- Bound the rule location range by min(rules_limit, rules_per_group_limit).
- Add virtnet_ff_rules_limit() helper for min(rules_limit, rules_per_group_limit).
- Fix indentation in setup_eth_hdr_key_mask().
- Use get_unaligned()/put_unaligned() for the IPv6 saddr/daddr copies, as the IPv4 path does, since they sit at 2 mod 4 in the byte-packed key/mask buffers.
- Reject a non-zero h_u.usr_ip4_spec.proto for ip4 rules with -EINVAL.
A comment for Sashiko review in mailing list:
- Pre-existing issues are not fixed as part of this series.
[Severity: Low]
This isn't a bug, but should the callback suppress further notifications
before deferring?
[SS] The work already disables callbacks and re-enables them under admin_vq.lock. Disabling in the callback would leave them disabled if the work is cancelled in cleanup or del_vq, so I'd rather not add it for a Low-severity optimisation.
[Severity: High]
Should values reported by the device be fatal to probe here?virtnet_ff_init() returns -EINVAL whenever the device reports any ofgroups_limit, classifiers_limit, rules_limit, rules_per_group_limit orselectors_per_classifier_limit as zero, and -EPROTO from the selector
walk further down when a known selector type has a length other thanget_mask_size(type) or the reply is truncated. The three kzalloc()s can
also return -ENOMEM.
[SS] device might not support flow filters. However, a device that advertises flow filters with zero limits or a mismatched selector size is broken and we fail probe.
[Severity: Medium]
What happens on the device side when the second or third capability set
fails after this one succeeded?virtnet_ff_init() commits three capabilities in sequence (resource,
selector, action), but the error labels only free the driver side copies:
[SS] There's no admin command to revoke a driver capability, so there's nothing to roll back with. It's harmless: after a failure ff_supported stays false, so the driver never uses what was granted, and the device clears it on reset.
[Severity: Medium]
Does this need to run with RTNL held?virtnet_ff_init() issues up to seven admin commands (one cap id list
query, three cap gets, three cap sets), and each one ends in an
uninterruptible wait with no timeout
[SS] RTNL is what serializes vi->ff: it has no lock of its own, and the ethtool rxnfc ops in the later patches access it under RTNL. In probe the netdev is already registered, and virtnet_freeze_down() takes RTNL around ff_cleanup, so the lock in virtnet_restore_up() pairs with it. Dropping it would let ethtool race with ff_init.
[Severity: Medium]
Is WARN_ON_ONCE() the right reporting mechanism for this error, given that
err here is not a kernel invariant? Looking at virtio_admin_obj_destroy()
in drivers/virtio/virtio_admin_commands.c, err comes from either an
allocation failure or the device-supplied admin command status
[SS] WARN_ON_ONCE() was requested by MST during review. A failed destroy means the driver and device disagree about the object's state, which is what we want to flag. A reset follows teardown, which discards device-side state, so a leftover object shouldn't affect the next probe.
[Severity: Medium]
Can the driver and the device diverge here when the destroy fails? err is
saved but the xa_erase(), destroy_classifier() and kfree(eth_rule) run
unconditionally, so on failure:
[SS] Teardown is best effort by design: a failed destroy means the device is misbehaving, so we warn and release the driver state instead of keeping records that can't be recovered. Keeping the rule would also complicate virtnet_ff_cleanup(), which has to free everything unconditionally. Any device state left behind is discarded on reset.
[Severity: Medium]
Where is rules_per_group_limit enforced? Every ethtool rule is created in the single ethtool group:
ff_rule->group_id = cpu_to_le32(VIRTNET_FF_ETHTOOL_GROUP_PRIORITY);
but the id allocation here is bounded only by rules_limit. On a device that
reports rules_per_group_limit smaller than rules_limit, repeated ethtool -U
insertions keep allocating locations and issuing OBJ_CREATE past the
per-group limit, and the failure then comes back from the device as an opaque
error instead of -ENOSPC.
[SS] The driver only accepts RX_CLS_LOC_ANY (validate_flow_input() rejects explicit locations), so users can't specify rule order and the location-based ordering guarantee doesn't apply. Overlapping rules are resolved by the device.
[Severity: Medium]
Are the installed rules meant to survive a freeze/restore cycle? This teardown is reached from the suspend path:
virtnet_freeze_down()
rtnl_lock(); virtnet_ff_cleanup(&vi->ff); rtnl_unlock();
[SS] Rules are not preserved across suspend and resume in this series. This could be a follow up.
Comments from internal Sashiko review:
Quoted 8 lines
(quoted diff) +8
-0+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) +{
[ ... ]
Review
Quoted 4 lines
(quoted diff) +4
-0+ 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);
Can this memcpy trigger undefined behavior if callers pass NULL for obj_specific_data and 0 for obj_specific_data_size? In C, passing a NULL pointer to memcpy is undefined behavior even if the size is 0, which could cause UBSAN splats.
[SS] It's the caller responsibly not to pass NULL. Current callers don't pass NULL.
Review
Quoted 8 lines
(quoted diff) +8
-0+ 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);
This isn't a bug introduced by this patch, but does this execution path sleep while holding a spinlock with interrupts disabled?
If this calls into virtqueue_exec_admin_cmd() (for example via vp_modern_admin_cmd_exec()), the call chain looks like:virtqueue_exec_admin_cmd()
spin_lock_irqsave(&admin_vq->lock, flags);
virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_KERNEL);Since GFP_KERNEL allows direct reclaim and sleeping, can this cause a deadlock or trigger a BUG if the virtqueue falls back to allocating indirect descriptors?
[SS] Right, but not related to this series. Can introduce a fix in a different patch.
Quoted 7 lines
(quoted diff) +7
-0+void 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;
Review
Quoted 10 lines
(quoted diff) +10
-0+ struct virtio_admin_cmd cmd = {}; + struct scatterlist data_sg; + int err; + + if (!vdev->config->admin_cmd_exec) + return; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return;
If this kzalloc() fails, does this function return without sending the destroy command to the hardware?
Since the return type is void, the caller has no way to detect this failure and will likely free its local software tracking objects. Will this leave the physical device and software tracking structures permanently desynchronized, leaking the object on the virtio device?
[SS] Not much to do here. There could be a firmware error also,
and then what we do? Destroy flow is best effort.
Review
Quoted 5 lines
(quoted diff) +5
-0+ break; + + if (sel->length != get_mask_size(sel->type) || + test_and_set_bit(sel->type, &sel_types)) { + WARN_ON_ONCE(true);
Is it safe to use WARN_ON_ONCE() for validating device input? A malicious or buggy virtio device could intentionally trigger this by providing an invalid length or duplicate selector types, potentially leading to a kernel panic if panic_on_warn is enabled. Would it be better to just return the error without the warning?
Review
Quoted 6 lines
(quoted diff) +6
-0+ err = -EINVAL; + goto err_ff_action; + } + real_ff_mask_size += sizeof(struct virtio_net_ff_selector) + sel->length; + if (real_ff_mask_size > ff_mask_size) { + WARN_ON_ONCE(true);
Similarly, could this WARN_ON_ONCE() be triggered by a malicious device providing a larger size than expected, leading to a panic if panic_on_warn is enabled?
[SS] These WARN_ON_ONCE() were requested by MST during review.
[ ... ]
Quoted 1 line
+static const struct
ethtool_opsvirtnet_ethtool_ops= {
[ ... ]
Review
Quoted 5 lines
(quoted diff) +5
-0+ .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, +};
Does this also need an implementation of .get_rxnfc to prevent invalid channel configurations?
When .get_rxnfc is missing, the ethtool core function ethtool_get_max_rxnfc_channel() returns -EOPNOTSUPP. This causes ethtool_check_max_channel() to silently skip validating the new channel count against active flow rules.
If a user adds a flow rule targeting a specific RX queue, and then reduces the active RX queues via ethtool -L, virtnet_set_channels() will update the queue count without checking the active rules stored in vi->ff.ethtool.rules.
Could this leave hardware rules pointing to disabled queues and cause silent packet drops?
[SS] .get_rxnfc exists (introduced in a later patch).
Review static const struct virtio_config_ops virtio_pci_config_ops = { · line 1263
Quoted 6 lines
(quoted diff) +1
-0@@ -1258,6 +1259,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, };
This isn't a bug introduced by this patch, but by exposing this function to general drivers, can a driver trigger a NULL pointer dereference or use-after-free?
If a driver calls the newly exposed config->admin_cmd_exec() early in probe before find_vqs(), or late in remove after del_vqs(), virtqueue_exec_admin_cmd() unconditionally dereferences admin_vq->info:
vq = admin_vq->info->vq;
In vp_del_vqs(), the info structure is freed via kfree(info), but vp_dev->admin_vq.info is never set to NULL, leaving a dangling pointer.
[SS] Current code is written in correct order. Future callers are responsible
to call this when VQs are initialized.
This isn't a bug introduced by this patch, but does this error path lead to stack corruption?
In virtqueue_exec_admin_cmd(), if virtqueue_kick() fails, it returns -EIO without waiting for completion or detaching the exposed buffer:
if (!virtqueue_kick(vq))
goto unlock_err;If the hardware later processes the descriptor and triggers vp_modern_avq_done(), it will call complete(&cmd->completion). Since the cmd structure was likely allocated on the caller's stack, this can overwrite out-of-scope stack memory.
[SS] The vq->broken flag set by virtqueue_notify() causes virtqueue_get_buf()
to return NULL in vp_modern_avq_done(), so complete() is never called on the freed cmd. No stack corruption.
This isn't a bug introduced by this patch, but can this function return an uninitialized variable?virtqueue_exec_admin_cmd() returns cmd->ret at the end of the function:
return cmd->ret;
However, on a successful command execution, vp_modern_avq_done() never explicitly sets cmd->ret to 0. If a driver allocates struct virtio_admin_cmd on the stack without zero-initializing it, could this return stack garbage and erroneously indicate failure?
[SS] cmd is initialized in all callers struct with virtio_admin_cmd cmd = {};
If the device responds with fewer bytes than the driver's provided data buffer, does this leave the rest of the output buffer uninitialized? The wrapper discards cmd.result_sg_size without zero-initializing data and returns 0, which could expose uninitialized memory to callers.
[SS] Caller's responsibility to initialize the buffer.
Second, if virtqueue_add_sgs() returns -ENOSPC, the code loops infinitely with cpu_relax() without a timeout or sleep:
drivers/virtio/virtio_pci_modern.c:virtqueue_exec_admin_cmd() {
...
if (ret == -ENOSPC) {
spin_unlock_irqrestore(&admin_vq->lock, flags);
cpu_relax();
goto again;
}...
}
Can this soft-lockup the CPU if the virtqueue remains full and the device is unresponsive?
[SS] Pre-existing, not introduced by this series, and bounded in practice by
the virtqueue_is_broken() check. Not something to fix here.
Third, if virtqueue_kick() fails, the code jumps to unlock_err without detaching the buffer from the virtqueue:
drivers/virtio/virtio_pci_modern.c:virtqueue_exec_admin_cmd() {
...
if (!virtqueue_kick(vq)) goto unlock_err; spin_unlock_irqrestore(&admin_vq->lock, flags); wait_for_completion(&cmd->completion); return cmd->ret; unlock_err: spin_unlock_irqrestore(&admin_vq->lock, flags); return -EIO;
...
}
Does this leave a dangling pointer on the virtqueue?
If the device later processes the command or the virtqueue is torn down, could it write to the freed heap memory and invoke complete() on a dangling stack pointer?
[SS] Not a bug. vq->broken prevents complete() from firing.
Review static const struct virtio_config_ops virtio_pci_config_ops = {
Quoted 8 lines
(quoted diff) +8
-0+ 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);
Similar to virtio_admin_cap_id_list_query above, does this execution discard cmd.result_sg_size without zeroing the caps buffer, potentially returning uninitialized memory to the caller on a short response?
[SS] Standard kernel API contract, caller owns the buffer, current callers
handle it correctly.
Review
Quoted 2 lines
(quoted diff) +2
-0+ data->id = cpu_to_le16(id); + memcpy(data->cap_specific_data, caps, cap_size);
If a caller invokes virtio_admin_cap_set() to enable a capability with no extra data (cap_size == 0 and caps == NULL), will this unconditionally pass a NULL pointer to memcpy(), invoking undefined behavior?
[SS] Caller's responsibility.
Review
Quoted 13 lines
(quoted diff) +13
-0+/** + * 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: 1 if capability is supported, 0 otherwise + */ +#define VIRTIO_CAP_IN_LIST(cap_list, cap) \ + (!!(1 & (le64_to_cpu(cap_list->supported_caps[(cap) / 64]) >> (cap) % 64)))
Could this macro trigger an out-of-bounds read on the supported_caps array if a caller passes a cap value greater than VIRTIO_ADMIN_MAX_CAP? Additionally, could evaluating cap multiple times cause unexpected side effects if passed an expression like cap++?
[SS] All callers use named cap constants defined in the uapi headers, which are all
within bounds by construction. Passing side-effecting expressions to macros is standard caller responsibility. No fix needed.
Review
Quoted 11 lines
static void
virtnet_ff_cleanup(struct virtnet_ff *ff){(quoted diff) +6
-0+ struct virtnet_ethtool_rule *eth_rule; + unsigned long i; + if (!ff->ff_supported) return; + xa_for_each(&ff->ethtool.rules, i, eth_rule) + destroy_ethtool_rule(ff, eth_rule); +
Will this cause user-configured flow filter rules to be permanently lost across system suspend/resume or device migration?
During suspend, device reset, or device freeze, virtnet_freeze_down() calls virtnet_ff_cleanup(). This loop iterates over all configured flow rules, sends the destroy commands to the hardware, and then calls kfree() via destroy_ethtool_rule(), permanently destroying the software representation of the rules.
Upon resume, virtnet_restore_up() invokes virtnet_ff_init(), which initializes the flow filters as completely empty. The driver appears to make no attempt to retain the software state of the rules during suspend or replay them to the device during restore, meaning users must manually recreate all flow filter rules every time the system resumes or the device is migrated.
[SS] This is intentional for now. Could be a follow up feature.
Signature · 1 line
2.49.0
PATCH v24 1/14 virtio_pci: Remove supported_caps cache and build assert
From: Daniel Jurgens <[email protected]>
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.
--- drivers/virtio/virtio_pci_common.h | 1 - drivers/virtio/virtio_pci_modern.c | 10 +++------- 2 files changed, 3 insertions(+), 8 deletions(-)
drivers/virtio/virtio_pci_common.h +0 -1
@@ -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. */
drivers/virtio/virtio_pci_modern.c +3 -7
@@ -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);
Signature · 1 line
2.49.0
PATCH v24 2/14 virtio_pci: Fix sleeping under spinlock in admin command path
In reply to Shahar Shitritvirtqueue_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.
--- 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(-)
drivers/virtio/virtio_pci_common.c +3 -0
@@ -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); }
drivers/virtio/virtio_pci_common.h +4 -1
@@ -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;
drivers/virtio/virtio_pci_modern.c +28 -12
@@ -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 ; }
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 02/14] virtio_pci: Fix sleeping under spinlock in admin command path
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] virtio_pci: race and premature work cancel in vp_modern_avq_cleanup() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] virtio_pci: race and premature work cancel in vp_modern_avq_cleanup()
--
commit dbe6d2ad6d64e5dddf1bddc940f5ef549b3aca9d
Author: Shahar Shitrit <[email protected]>
virtio_pci: Fix sleeping under spinlock in admin command path
This commit replaces the spinlock protecting the admin virtqueue with a mutex to allow GFP_KERNEL allocations in virtqueue_add_sgs(), and defers the completion callback from interrupt context to a work item.
Quoted 4 lines
drivers/virtio/virtio_pci_modern.c +0
-0
[ ... ]
Quoted 19 lines
drivers/virtio/virtio_pci_modern.c +5
-1@@ -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);
drivers/virtio/virtio_pci_modern.c static void vp_modern_avq_activate(struct virtio_device *vdev) · line 378
Quoted 10 lines
drivers/virtio/virtio_pci_modern.c +3
-0+ +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)
[Severity: Medium]
Can cancelling admin_vq->work in vp_modern_avq_cleanup() fail already completed commands with -EIO and trigger BAD_RING() in virtqueue_get_buf()?
Previously, vp_modern_avq_done() drained the used ring synchronously in vring_interrupt(), so virtio_synchronize_cbs() guaranteed all used entries were consumed and vq->last_used_idx was up to date before cleanup ran. Now vp_modern_avq_done() only schedules admin_vq->work, leaving used entries in the ring (more_used(vq) == true) when vring_interrupt() returns.
If admin_vq->work is pending when vp_modern_avq_cleanup() runs, cancel_work_sync() drops the work without draining the used ring. virtqueue_detach_unused_buf() then detaches those completed buffers, clearing desc_state[id].data = NULL and completing the commands with -EIO, while vq->last_used_idx remains unadvanced.
Because vp_modern_del_vqs() calls vp_modern_avq_cleanup() before vp_del_vqs() removes the admin virtqueue from vp_dev->virtqueues or frees its IRQ, an interrupt firing on a shared vector before vp_del_vq() is reached sees more_used(vq) == true and reschedules admin_vq->work:
CPU0 CPU1vp_modern_del_vqs()
vp_modern_avq_cleanup()
cancel_work_sync(&admin_vq->work)
virtqueue_detach_unused_buf(vq)
desc_state[id].data = NULL
<hardirq>
vring_interrupt()
more_used(vq) == true
vp_modern_avq_done()
schedule_work(&admin_vq->work)
vp_modern_avq_work()
virtqueue_get_buf(vq, &len)
!desc_state[id].data
BAD_RING()
vp_del_vqs()
...
vp_del_vq()Flushing admin_vq->work (or draining the used ring) instead of cancelling it, and doing so after the admin virtqueue is unhooked from interrupt dispatch, would avoid both issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH net-next v24 02/14] virtio_pci: Fix sleeping under spinlock in admin command path
In reply to Shahar ShitritThank 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…
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.
Quoted 12 lines
drivers/virtio/virtio_pci_modern.c +3
-1@@ -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); +}
[ ... ]
Quoted 5 lines
drivers/virtio/virtio_pci_modern.c +0
-1@@ -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);
drivers/virtio/virtio_pci_modern.c static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq, · line 118
Quoted 10 lines
drivers/virtio/virtio_pci_modern.c +3
-2+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()?
drivers/virtio/virtio_pci_modern.c static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq, · line 125
Quoted 8 lines
drivers/virtio/virtio_pci_modern.c +1
-1goto 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?
[ ... ]
Quoted 3 lines
drivers/virtio/virtio_pci_modern.c +0
-0@@ -344,20 +352,27 @@ static void vp_modern_avq_activate(struct virtio_device *vdev)static void vp_modern_avq_cleanup (struct virtio_device *vdev) {
[ ... ]
drivers/virtio/virtio_pci_modern.c static void vp_modern_avq_activate(struct virtio_device *vdev) · line 375
Quoted 13 lines
drivers/virtio/virtio_pci_modern.c +5
-1- 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?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
PATCH v24 3/14 virtio: Add config_op for admin commands
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
This will allow device drivers to issue administration commands.
--- drivers/virtio/virtio_pci_modern.c | 2 ++ include/linux/virtio_config.h | 10 ++++++++++ 2 files changed, 12 insertions(+)
drivers/virtio/virtio_pci_modern.c +2 -0
@@ -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 */
include/linux/virtio_config.h +10 -0
@@ -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); };/**
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 03/14] virtio: Add config_op for admin commands
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
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.
drivers/virtio/virtio_pci_modern.c static const struct virtio_config_ops virtio_pci_config_nodev_ops = { · line 1258
Quoted 10 lines
drivers/virtio/virtio_pci_modern.c +1
-0@@ -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?
[ ... ]
include/linux/virtio_config.h struct virtqueue_info { · line 114
Quoted 12 lines
include/linux/virtio_config.h +4
-0@@ -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?
include/linux/virtio_config.h struct virtqueue_info { · line 116
Quoted 2 lines
include/linux/virtio_config.h +2
-0+* 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").
include/linux/virtio_config.h struct virtqueue_info { · line 117
Quoted 1 line
include/linux/virtio_config.h +1
-0+* 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.
include/linux/virtio_config.h struct virtqueue_info { · line 118
Quoted 1 line
include/linux/virtio_config.h +1
-0+* 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?
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
PATCH v24 4/14 virtio: Expose generic device capability operations
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
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.
--- 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
drivers/virtio/Makefile +1 -1
@@ -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
drivers/virtio/virtio_admin_commands.c new file +100 -0
100 lines not on this page · raw message
include/linux/virtio_admin.h new file +89 -0
89 lines not on this page · raw message
include/uapi/linux/virtio_pci.h +4 -2
@@ -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 {
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 04/14] virtio: Expose generic device capability operations
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] virtio: duplicated capability command logic in virtio_admin_commands.c --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] virtio: duplicated capability command logic in virtio_admin_commands.c
--
commit 433f36930ddca8a1d9168aa95c377a032729ab34
Author: Daniel Jurgens <[email protected]>
virtio: Expose generic device capability operations
This commit adds generic helpers in virtio_admin_commands.c and virtio_admin.h to query capability ID lists and get or set device capabilities via admin commands, and expands the supported_caps bitmap in struct virtio_admin_cmd_query_cap_id_result to cover up to VIRTIO_ADMIN_MAX_CAP.
Quoted 93 lines
drivers/virtio/virtio_admin_commands.c new file +87
-0@@ -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 );
drivers/virtio/virtio_admin_commands.c line 97
Quoted 10 lines
drivers/virtio/virtio_admin_commands.c new file +10
-0+ 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);
[Severity: Low]
This isn't a bug, but could virtio_pci_admin_cmd_cap_init() and virtio_pci_admin_cmd_dev_parts_objects_enable() in drivers/virtio/virtio_pci_modern.c be converted to use virtio_admin_cap_id_list_query(), virtio_cap_in_list(), virtio_admin_cap_get(), and virtio_admin_cap_set()?
Both virtio_pci_config_ops and virtio_pci_config_nodev_ops already wire admin_cmd_exec to vp_modern_admin_cmd_exec(), so leaving the open-coded scatterlist setup and VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY, VIRTIO_ADMIN_CMD_DEVICE_CAP_GET, and VIRTIO_ADMIN_CMD_DRIVER_CAP_SET calls in virtio_pci_modern.c duplicates these helpers:
drivers/virtio/virtio_pci_modern.c:virtio_pci_admin_cmd_dev_parts_objects_enable() {
...
get_data->id = cpu_to_le16(VIRTIO_DEV_PARTS_CAP);
sg_init_one(&data_sg, get_data, sizeof(*get_data));
sg_init_one(&result_sg, result, sizeof(*result));
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;
ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
...
}drivers/virtio/virtio_pci_modern.c:virtio_pci_admin_cmd_cap_init() {
...
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;
ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
...
}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH net-next v24 04/14] virtio: Expose generic device capability operations
In reply to Shahar ShitritThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
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.
Quoted 5 lines
drivers/virtio/virtio_admin_commands.c new file +0
-0
[ ... ]
drivers/virtio/virtio_admin_commands.c line 23
Quoted 12 lines
drivers/virtio/virtio_admin_commands.c new file +12
-0+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?
Quoted 5 lines
drivers/virtio/virtio_admin_commands.c new file +5
-0+ cmd.result_sg = &result_sg; + +return vdev->config->admin_cmd_exec (vdev, &cmd); +} +EXPORT_SYMBOL_GPL (virtio_admin_cap_id_list_query);
[ ... ]
Quoted 5 lines
include/linux/virtio_admin.h new file +0
-0
[ ... ]
include/linux/virtio_admin.h line 30
Quoted 4 lines
include/linux/virtio_admin.h new file +4
-0+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?
Quoted 3 lines
include/linux/virtio_admin.h new file +3
-0+return !!(1 & (le64_to_cpu (cap_list->supported_caps[cap /64 ]) >> + (cap %64 ))); +}
[ ... ]
Quoted 5 lines
+/**
+ *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?
Quoted 1 line
+ * @cap_size: Size of the capability data structure
[ ... ]
Quoted 7 lines
include/uapi/linux/virtio_pci.h +0
-0@@ -315,15 +315,17 @@ struct virtio_admin_cmd_notify_info_result {#define VIRTIO_DEV_PARTS_CAP 0x0000
include/uapi/linux/virtio_pci.h struct virtio_admin_cmd_notify_info_result { · line 325
Quoted 10 lines
include/uapi/linux/virtio_pci.h +3
-1+#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?
Quoted 5 lines
include/uapi/linux/virtio_pci.h +1
-1struct virtio_admin_cmd_query_cap_id_result { -__le64 supported_caps[MAX_CAP_ID ]; +__le64 supported_caps[VIRTIO_ADMIN_CAP_ID_ARRAY_SIZE ]; };
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
PATCH v24 5/14 virtio: Expose object create and destroy API
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
Object create and destroy were implemented specifically for dev parts device objects. Create general purpose APIs for use by upper layer drivers.
--- drivers/virtio/virtio_admin_commands.c | 77 ++++++++++++++++++++++++++ include/linux/virtio_admin.h | 44 +++++++++++++++ 2 files changed, 121 insertions(+)
drivers/virtio/virtio_admin_commands.c +77 -0
@@ -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" );
include/linux/virtio_admin.h +44 -0
48 lines not on this page · raw message
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 05/14] virtio: Expose object create and destroy API
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…
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.
Quoted 16 lines
drivers/virtio/virtio_admin_commands.c +8
-0@@ -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) +{
[ ... ]
drivers/virtio/virtio_admin_commands.c int virtio_admin_cap_set(struct virtio_device *vdev, · line 126
Quoted 3 lines
drivers/virtio/virtio_admin_commands.c +3
-0+ 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.
drivers/virtio/virtio_admin_commands.c int virtio_admin_cap_set(struct virtio_device *vdev, · line 134
Quoted 8 lines
drivers/virtio/virtio_admin_commands.c +8
-0+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.
drivers/virtio/virtio_admin_commands.c int virtio_admin_cap_set(struct virtio_device *vdev, · line 137
Quoted 3 lines
drivers/virtio/virtio_admin_commands.c +3
-0+ + 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?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
PATCH v24 6/14 virtio_net: Query and set flow filter caps
In reply to Shahar ShitritWhen 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.
--- 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
drivers/net/virtio_net.c +237 -0
@@ -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);
include/uapi/linux/virtio_net_ff.h new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
2.49.0
Michael S. Tsirkin
maintainerRe: [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps
Quoted 264 lines · Shahar Shitrit
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_netflow filters define in version 1.4 of the VirtIO spec.Signed-off-by Shahar Shitrit <[email protected]>Signed-off-by Daniel Jurgens <[email protected]>Reviewed-by Parav Pandit <[email protected]>--- 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.hdrivers/net/virtio_net.c +208
-0@@ -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;
drivers/net/virtio_net.c static void virtnet_freeze_down(struct virtio_device *vdev) · line 6026
Quoted 8 lines
drivers/net/virtio_net.c +4
-0@@ -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.
Quoted 155 lines
drivers/net/virtio_net.c +25
-0static 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);include/uapi/linux/virtio_net_ff.h new file +91
-0@@ -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 Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps
In reply to Shahar ShitritThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] virtio_net: unbalanced disable_rx_mode_work() in virtnet_restore_up() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] virtio_net: unbalanced disable_rx_mode_work() in virtnet_restore_up()
--
commit b9f5c14eec9c7ae78f244671bac272d4a36c28f1
Author: Shahar Shitrit <[email protected]>
virtio_net: Query and set flow filter caps
This commit adds UAPI definitions for VirtIO 1.4 flow filters and queries and sets flow filter capabilities during device probe and restore.
Quoted 4 lines
drivers/net/virtio_net.c +0
-0
[ ... ]
Quoted 7 lines
drivers/net/virtio_net.c +3
-0@@ -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
drivers/net/virtio_net.c static int virtnet_restore_up(struct virtio_device *vdev) · line 6065
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+*/ +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 ();
[Severity: Medium]
Does calling virtnet_freeze_down() on this error path leave vi->rx_mode_work's disable depth unbalanced?
virtnet_freeze_down() calls disable_rx_mode_work() (disable_work_sync(&vi->rx_mode_work)), which already ran during freeze, and enable_rx_mode_work() is not reached until the end of virtnet_restore_up(). If virtnet_ff_init() fails, calling virtnet_freeze_down() increments the work item's disable count a second time without a matching enable_work().
Initializing flow filters right after virtio_device_ready() (as in virtnet_probe()) and jumping to err_destroy_pools on error would avoid calling virtnet_freeze_down() here.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH net-next v24 06/14] virtio_net: Query and set flow filter caps
In reply to Shahar ShitritThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
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.
Quoted 4 lines
drivers/net/virtio_net.c +0
-0
[ ... ]
Quoted 1 line
drivers/net/virtio_net.c +0
-0@@ -5794,6 +5809,199 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
[ ... ]
drivers/net/virtio_net.c static const struct netdev_stat_ops virtnet_stat_ops = { · line 5921
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+ 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.
Quoted 4 lines
drivers/net/virtio_net.c +4
-0+if (!ff->ff_actions->count) { + err = -EINVAL ; + goto err_ff_action; + }
[ ... ]
drivers/net/virtio_net.c static const struct netdev_stat_ops virtnet_stat_ops = { · line 5961
Quoted 6 lines
drivers/net/virtio_net.c +6
-0+ 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.
Quoted 9 lines
drivers/net/virtio_net.c +9
-0+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;
[ ... ]
Quoted 6 lines
drivers/net/virtio_net.c +2
-0@@ -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
drivers/net/virtio_net.c static int virtnet_restore_up(struct virtio_device *vdev) · line 6064
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+* 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 -> 2Nothing 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.
Quoted 1 line
drivers/net/virtio_net.c +1
-0+rtnl_unlock ();
[ ... ]
drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 7378
Quoted 12 lines
drivers/net/virtio_net.c +8
-0@@ -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?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
PATCH v24 7/14 virtio_net: Create a FF group for ethtool steering
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
All ethtool steering rules will go in one group, create it during initialization.
--- drivers/net/virtio_net.c | 32 ++++++++++++++++++++++++++++++ include/uapi/linux/virtio_net_ff.h | 15 ++++++++++++++ 2 files changed, 47 insertions(+)
drivers/net/virtio_net.c +32 -0
@@ -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);
include/uapi/linux/virtio_net_ff.h +15 -0
@@ -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
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 07/14] virtio_net: Create a FF group for ethtool steering
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] virtio_net: spurious WARN_ON_ONCE() in virtnet_ff_cleanup() on destroy error --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] virtio_net: spurious WARN_ON_ONCE() in virtnet_ff_cleanup() on destroy error
--
commit 5c257586f26d962c6d07dc4ada5dd7ba143b698a
Author: Daniel Jurgens <[email protected]>
virtio_net: Create a FF group for ethtool steering
This commit creates a flow filter group object for ethtool steering rules during flow filter initialization and destroys it during cleanup.
Quoted 4 lines
drivers/net/virtio_net.c +0
-0
[ ... ]
Quoted 5 lines
drivers/net/virtio_net.c +1
-0@@ -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;
drivers/net/virtio_net.c static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev) · line 6029
Quoted 10 lines
drivers/net/virtio_net.c +7
-0+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]
Can WARN_ON_ONCE(err) fire during PCI surprise removal?
On surprise hot-unplug, virtio_pci_remove() calls virtio_break_device() before unregister_virtio_device() -> virtnet_remove() -> virtnet_ff_cleanup(). Because the admin virtqueue is already marked broken, virtio_admin_obj_destroy() returns -EIO (and can also return -ENOMEM if its GFP_KERNEL buffer allocation fails), triggering a warning backtrace or a panic when panic_on_warn is set.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewRe: [PATCH net-next v24 07/14] virtio_net: Create a FF group for ethtool steering
In reply to Shahar ShitritThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
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.
Quoted 4 lines
drivers/net/virtio_net.c +0
-0
[ ... ]
drivers/net/virtio_net.c static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev) · line 5936
Quoted 10 lines
drivers/net/virtio_net.c +6
-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 ); +
[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; }
Quoted 9 lines
drivers/net/virtio_net.c +2
-0err = 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 ); +
drivers/net/virtio_net.c static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev) · line 5993
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+/* 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.
Quoted 9 lines
drivers/net/virtio_net.c +2
-0+ 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;
drivers/net/virtio_net.c static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev) · line 6029
Quoted 10 lines
drivers/net/virtio_net.c +7
-0+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.
Quoted 4 lines
drivers/net/virtio_net.c +1
-0+kfree (ff->ff_actions);kfree (ff->ff_mask);kfree (ff->ff_caps);
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
PATCH v24 8/14 ethtool: Introduce ethtool_flow_type_mask()
In reply to Shahar ShitritAdd 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.
--- .../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(-)
drivers/net/ethernet/mellanox/mlx5/core/en_fs_ethtool.c +6 -11
@@ -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;
drivers/net/ethernet/mellanox/mlx5/core/ipoib/ethtool.c +1 -6
@@ -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);
include/linux/ethtool.h +6 -0
@@ -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 */
Signature · 1 line
2.49.0
PATCH v24 9/14 virtio_net: Implement layer 2 ethtool flow rules
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
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.
--- drivers/net/virtio_net.c | 535 +++++++++++++++++++++++++++-- include/uapi/linux/virtio_net_ff.h | 50 +++ 2 files changed, 557 insertions(+), 28 deletions(-)
drivers/net/virtio_net.c +507 -28
@@ -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,
include/uapi/linux/virtio_net_ff.h +50 -0
60 lines not on this page · raw message
Signature · 1 line
2.49.0
Michael S. Tsirkin
maintainerRe: [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules
Quoted 616 lines · Shahar Shitrit
From: Daniel Jurgens <[email protected]>
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 1The rule in the example directs received packets with the specified destination MAC address to rq 30.
Signed-off-by Daniel Jurgens <[email protected]>Signed-off-by Shahar Shitrit <[email protected]>Reviewed-by Parav Pandit <[email protected]>Reviewed-by Xuan Zhuo <[email protected]>--- drivers/net/virtio_net.c | 535 +++++++++++++++++++++++++++-- include/uapi/linux/virtio_net_ff.h | 50 +++ 2 files changed, 557 insertions(+), 28 deletions(-)drivers/net/virtio_net.c +499
-28@@ -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)
drivers/net/virtio_net.c static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev) · line 6498
Quoted 10 lines
drivers/net/virtio_net.c +4
-0{ +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.
Quoted 76 lines
drivers/net/virtio_net.c +4
-0+ +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,include/uapi/linux/virtio_net_ff.h +50
-0@@ -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 Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules
In reply to Shahar ShitritThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] virtio_net: __counted_by counter set after referencing mask in setup_eth_hdr_key_mask() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] virtio_net: __counted_by counter set after referencing mask in setup_eth_hdr_key_mask()
--
commit 20499aa384cc033510155c4bf8a0df76cc437ef3
Author: Daniel Jurgens <[email protected]>
virtio_net: Implement layer 2 ethtool flow rules
This commit implements layer 2 ethtool flow steering rules for ethernet headers in virtio_net using flow filter classifiers and rules.
Quoted 4 lines
drivers/net/virtio_net.c +0
-0
[ ... ]
Quoted 1 line
drivers/net/virtio_net.c +0
-0@@ -5812,6 +5792,451 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
[ ... ]
drivers/net/virtio_net.c static const struct netdev_stat_ops virtnet_stat_ops = { · line 6084
Quoted 13 lines
drivers/net/virtio_net.c +13
-0+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)); +}
[Severity: High]
Does taking the address of selector->mask to initialize eth_m before setting selector->length trigger a fortify_panic() in memcpy()?
In struct virtio_net_ff_selector, mask is annotated with __counted_by(length):
include/uapi/linux/virtio_net_ff.h:
struct virtio_net_ff_selector {
__u8 type;
__u8 flags;
__u8 reserved[2];
__u8 length;
__u8 reserved1[3];
__u8 mask[] __counted_by(length);
};With CONFIG_CC_HAS_COUNTED_BY and CONFIG_FORTIFY_SOURCE enabled, the compiler evaluates the flexible array bound at the point selector->mask is referenced to initialize eth_m, while selector->length is still 0 from kzalloc(). As a result, __builtin_dynamic_object_size(eth_m, 0) evaluates to 0 and the fortified memcpy() panics when a rule is inserted. Assigning selector->length before referencing selector->mask avoids this.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
[email protected]
AI reviewRe: [PATCH net-next v24 09/14] virtio_net: Implement layer 2 ethtool flow rules
In reply to Shahar ShitritThank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.
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.
Quoted 4 lines
drivers/net/virtio_net.c +0
-0
[ ... ]
Quoted 1 line
drivers/net/virtio_net.c +0
-0@@ -5812,6 +5792,451 @@ static const struct netdev_stat_ops virtnet_stat_ops = {
[ ... ]
Quoted 26 lines
drivers/net/virtio_net.c +26
-0+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--;
drivers/net/virtio_net.c static const struct netdev_stat_ops virtnet_stat_ops = { · line 5959
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+ + 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().
Quoted 3 lines
drivers/net/virtio_net.c +3
-0+ +return err; +}
[ ... ]
Quoted 2 lines
drivers/net/virtio_net.c +2
-0+ ff_rule->vq_index = fs->ring_cookie !=RX_CLS_FLOW_DISC ? +cpu_to_le16 (rxq2vq (fs->ring_cookie)) :0 ;
[ ... ]
Quoted 4 lines
drivers/net/virtio_net.c +4
-0+static int validate_flow_input (struct virtnet_ff *ff, +const struct ethtool_rx_flow_spec *fs, +u16 curr_queue_pairs) +{
[ ... ]
drivers/net/virtio_net.c static const struct netdev_stat_ops virtnet_stat_ops = { · line 6039
Quoted 3 lines
drivers/net/virtio_net.c +3
-0+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?
[ ... ]
drivers/net/virtio_net.c static const struct netdev_stat_ops virtnet_stat_ops = { · line 6130
Quoted 4 lines
drivers/net/virtio_net.c +4
-0+/* +* 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.
Quoted 4 lines
drivers/net/virtio_net.c +4
-0+ c =kzalloc (classifier_size + +sizeof (struct virtnet_classifier ) - +sizeof (struct virtio_net_resource_obj_ff_classifier ), +GFP_KERNEL );
[ ... ]
Quoted 6 lines
drivers/net/virtio_net.c +2
-0@@ -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;
drivers/net/virtio_net.c static int virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev) · line 6501
Quoted 10 lines
drivers/net/virtio_net.c +5
-0int 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.
[ ... ]
Quoted 4 lines
include/uapi/linux/virtio_net_ff.h +0
-0
[ ... ]
Quoted 1 line
include/uapi/linux/virtio_net_ff.h +0
-0@@ -103,4 +105,52 @@ struct virtio_net_resource_obj_ff_group {
[ ... ]
Quoted 7 lines
+ * Payload for the
VIRTIO_NET_RESOURCE_OBJ_FF_RULEadministrative object.
+ * @group_id and @classifier_id refer to previously created objects of types
+ *VIRTIO_NET_RESOURCE_OBJ_FF_GROUPandVIRTIO_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.
[ ... ]
Quoted 442 lines
include/uapi/linux/virtio_net_ff.h +438
-0@@ -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; + }
include/uapi/linux/virtio_net_ff.h static const struct netdev_stat_ops virtnet_stat_ops = { · line 6242
Quoted 10 lines
include/uapi/linux/virtio_net_ff.h +7
-0+ + 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?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
PATCH v24 10/14 virtio_net: Use existing classifier if possible
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
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.
--- drivers/net/virtio_net.c | 50 +++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 16 deletions(-)
drivers/net/virtio_net.c +34 -16
@@ -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; }
Signature · 1 line
2.49.0
Michael S. Tsirkin
maintainerRe: [PATCH net-next v24 10/14] virtio_net: Use existing classifier if possible
Quoted 47 lines · Shahar Shitrit
From: Daniel Jurgens <[email protected]>
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 <[email protected]>Reviewed-by Parav Pandit <[email protected]>Reviewed-by Xuan Zhuo <[email protected]>Signed-off-by Shahar Shitrit <[email protected]>--- drivers/net/virtio_net.c | 50 +++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 16 deletions(-)drivers/net/virtio_net.c +4
-2@@ -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; }
drivers/net/virtio_net.c static bool validate_mask(const struct virtnet_ff *ff, · line 5905
Quoted 10 lines
drivers/net/virtio_net.c +5
-2-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?
Quoted 93 lines
drivers/net/virtio_net.c +25
-12+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; }Signature · 1 line
2.49.0
PATCH v24 11/14 virtio_net: Implement IPv4 ethtool flow rules
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
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.
--- drivers/net/virtio_net.c | 129 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 124 insertions(+), 5 deletions(-)
drivers/net/virtio_net.c +124 -5
@@ -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)
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 11/14] virtio_net: Implement IPv4 ethtool flow rules
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
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.
Quoted 4 lines
drivers/net/virtio_net.c +0
-0
[ ... ]
Quoted 1 line
drivers/net/virtio_net.c +0
-0@@ -5890,11 +5924,41 @@ static bool validate_mask(const struct virtnet_ff *ff,
[ ... ]
Quoted 11 lines
drivers/net/virtio_net.c +11
-0+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); + } +
drivers/net/virtio_net.c static bool validate_mask(const struct virtnet_ff *ff, · line 5955
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+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.
[ ... ]
Quoted 6 lines
drivers/net/virtio_net.c +2
-0@@ -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 ); +
drivers/net/virtio_net.c static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs, · line 6151
Quoted 10 lines
drivers/net/virtio_net.c +8
-1*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.
Quoted 1 line
drivers/net/virtio_net.c +1
-0+ *key_size = size;
[ ... ]
Quoted 1 line
drivers/net/virtio_net.c +0
-0@@ -6095,8 +6171,36 @@ static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector,
[ ... ]
Quoted 7 lines
drivers/net/virtio_net.c +7
-0+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; +
drivers/net/virtio_net.c static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector, · line 6199
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+ 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.
Quoted 5 lines
drivers/net/virtio_net.c +4
-0+ +parse_ip4 (v4_m, v4_k, fs); + +return 0 ; }
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
PATCH v24 12/14 virtio_net: Add support for IPv6 ethtool steering
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
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.
--- drivers/net/virtio_net.c | 108 ++++++++++++++++++++++++++++++++++----- 1 file changed, 96 insertions(+), 12 deletions(-)
drivers/net/virtio_net.c +96 -12
@@ -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);
Signature · 1 line
2.49.0
PATCH v24 13/14 virtio_net: Add support for TCP and UDP ethtool rules
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
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.
--- drivers/net/virtio_net.c | 225 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 210 insertions(+), 15 deletions(-)
drivers/net/virtio_net.c +210 -15
@@ -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);
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 13/14] virtio_net: Add support for TCP and UDP ethtool rules
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] virtio_net: malformed Signed-off-by trailer in commit message --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] virtio_net: malformed Signed-off-by trailer in commit message --
commit b15c571b89a0dd07dd32bb70081ade133aa32c70
Author: Daniel Jurgens <[email protected]>
Quoted 16 lines
virtio_net: Add support for TCP and UDP ethtool rulesImplement 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 4This 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
Quoted 7 lines
This example directs IPv6 TCP traffic with the specified address and port to queue 12.
Signed-off-by Daniel Jurgens <[email protected]>Reviewed-by Parav Pandit <[email protected]>Reviewed-by Xuan Zhuo <[email protected]>Signed-off-by Shahar Shitrit <[email protected]>=
[Severity: High]
The second Signed-off-by trailer has a stray trailing '=' character after the closing '>' of the email address.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
[email protected]
AI reviewRe: [PATCH net-next v24 13/14] virtio_net: Add support for TCP and UDP ethtool rules
In reply to Shahar ShitritThank 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…
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.
Quoted 4 lines
Signed-off-by Daniel Jurgens <[email protected]>Reviewed-by Parav Pandit <[email protected]>Reviewed-by Xuan Zhuo <[email protected]>Signed-off-by Shahar Shitrit <[email protected]>=
[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?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
PATCH v24 14/14 virtio_net: Add get ethtool flow rules ops
In reply to Shahar ShitritFrom: Daniel Jurgens <[email protected]>
- 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--- drivers/net/virtio_net.c | 75 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)
drivers/net/virtio_net.c +75 -0
@@ -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, };
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH net-next v24 14/14] virtio_net: Add get ethtool flow rules ops
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…
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.
Quoted 1 line
Signed-off-by Dniel Jurgens <[email protected]>
[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.
Quoted 4 lines
drivers/net/virtio_net.c +0
-0
[ ... ]
drivers/net/virtio_net.c static const struct ethtool_ops virtnet_ethtool_ops = { · line 6791
Quoted 7 lines
drivers/net/virtio_net.c +1
-0@@ -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.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202150502.3350030-1-shshitrit%40nvidia.com
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
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.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
A message that is not in the archive (sent off-list or not yet received): BL0PR12MB5505B05C081539D249C49DCCDA4B2@BL0PR12MB5505.namprd12.prod.outlook.com
Quoted 11 lines · On 03/12/2026 12:51, [email protected] wrote:
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Wednesday, December 2, 2026 5:09 PM
To: Shahar Shitrit <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Parav Pandit <[email protected]>; Yohad Tor <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Dan Jurgens <[email protected]>
Subject: Re: [PATCH net-next v24 00/14]virtio_net: Add ethtool flow rules supportHi!
Quoted 7 lines
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.
This series is not a fix but a new feature (tested). No info is missing
Quoted 4 lines
*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.
[email protected]
AI reviewSashiko has finished reviewing this 14-patch series:
Sashiko has finished reviewing this 14-patch series:
- Patches 1, 3, 5, 8, 10-12, and 14: no issues found.
- Patch 2 ("virtio_pci: Fix sleeping under spinlock in admin com..."): 1 potential issue (1 Medium)
- Patch 4 ("virtio: Expose generic device capability operations"): 1 potential issue (1 Low)
- Patch 6 ("virtio_net: Query and set flow filter caps"): 1 potential issue (1 Medium)
- Patch 7 ("virtio_net: Create a FF group for ethtool steering"): 1 potential issue (1 Medium)
- Patch 9 ("virtio_net: Implement layer 2 ethtool flow rules"): 1 potential issue (1 High)
- Patch 13 ("virtio_net: Add support for TCP and UDP ethtool rules"): 1 potential issue (1 High)
Detailed reviews have been sent as separate replies to patches 2, 4, 6, 7, 9, and 13.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]