tun: Introduce virtio-net hashing feature
Subsystems (9)
v11 cover tun: Introduce virtio-net hashing feature
virtio-net have two usage of hashes: one is RSS and another is hash reporting. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.
Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF.
Introduce the code to compute hashes to the kernel in order to overcome thse challenges.
An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but it is based on context rewrites, which is in feature freeze. We can adopt kfuncs, but they will not be UAPIs. We opt to ioctl to align with other relevant UAPIs (KVM and vhost_net).
The patches for QEMU to use this new feature was submitted as RFC and is available at:
https://patchew.org/QEMU/[email protected]/
This work was presented at LPC 2024:
https://lpc.events/event/18/contributions/1963/
V1 -> V2:
Changed to introduce a new BPF program type.
---
Changes in v11:
- Added the missing code to free vnet_hash in patch
"tap: Introduce virtio-net hash feature".
- Link to v10: https://lore.kernel.org/r/[email protected]
Changes in v10:
- Split common code and TUN/TAP-specific code into separate patches.
- Reverted a spurious style change in patch "tun: Introduce virtio-net hash feature".
- Added a comment explaining disable_ipv6 in tests.
- Used AF_PACKET for patch "selftest: tun: Add tests for
virtio-net hashing". I also added the usage of FIXTURE_VARIANT() as the testing function now needs access to more variant-specific variables.
- Corrected the message of patch "selftest: tun: Add tests for virtio-net hashing"; it mentioned validation of configuration but it is not scope of this patch.
- Expanded the description of patch "selftest: tun: Add tests for virtio-net hashing".
- Added patch "tun: Allow steering eBPF program to fall back".
- Changed to handle TUNGETVNETHASHCAP before taking the rtnl lock.
- Removed redundant tests for tun_vnet_ioctl().
- Added patch "selftest: tap: Add tests for virtio-net ioctls".
- Added a design explanation of ioctls for extensibility and migration.
- Removed a few branches in patch
"vhost/net: Support VIRTIO_NET_F_HASH_REPORT".
- Link to v9: https://lore.kernel.org/r/[email protected]
Changes in v9:
- Added a missing return statement in patch
"tun: Introduce virtio-net hash feature".
- Link to v8: https://lore.kernel.org/r/[email protected]
Changes in v8:
- Disabled IPv6 to eliminate noises in tests.
- Added a branch in tap to avoid unnecessary dissection when hash reporting is disabled.
- Removed unnecessary rtnl_lock().
- Extracted code to handle new ioctls into separate functions to avoid adding extra NULL checks to the code handling other ioctls.
- Introduced variable named "fd" to __tun_chr_ioctl().
- s/-/=/g in a patch message to avoid confusing Git.
- Link to v7: https://lore.kernel.org/r/[email protected]
Changes in v7:
- Ensured to set hash_report to VIRTIO_NET_HASH_REPORT_NONE for VHOST_NET_F_VIRTIO_NET_HDR.
- s/4/sizeof(u32)/ in patch "virtio_net: Add functions for hashing".
- Added tap_skb_cb type.
- Rebased.
- Link to v6: https://lore.kernel.org/r/[email protected]
Changes in v6:
- Extracted changes to fill vnet header holes into another series.
- Squashed patches "skbuff: Introduce SKB_EXT_TUN_VNET_HASH", "tun:
Introduce virtio-net hash reporting feature", and "tun: Introduce virtio-net RSS" into patch "tun: Introduce virtio-net hash feature".
- Dropped the RFC tag.
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Fixed a compilation error with CONFIG_TUN_VNET_CROSS_LE.
- Optimized the calculation of the hash value according to:
https://git.dpdk.org/dpdk/commit/?id=3fb1ea032bd6ff8317af5dac9af901f1f324cab4
- Added patch "tun: Unify vnet implementation".
- Dropped patch "tap: Pad virtio header with zero".
- Added patch "selftest: tun: Test vnet ioctls without device".
- Reworked selftests to skip for older kernels.
- Documented the case when the underlying device is deleted and packets have queue_mapping set by TC.
- Reordered test harness arguments.
- Added code to handle fragmented packets.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Moved tun_vnet_hash_ext to if_tun.h.
- Renamed virtio_net_toeplitz() to virtio_net_toeplitz_calc().
- Replaced htons() with cpu_to_be16().
- Changed virtio_net_hash_rss() to return void.
- Reordered variable declarations in virtio_net_hash_rss().
- Removed virtio_net_hdr_v1_hash_from_skb().
- Updated messages of "tap: Pad virtio header with zero" and
"tun: Pad virtio header with zero".
- Fixed vnet_hash allocation size.
- Ensured to free vnet_hash when destructing tun_struct.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Reverted back to add ioctl.
- Split patch "tun: Introduce virtio-net hashing feature" into
"tun: Introduce virtio-net hash reporting feature" and
"tun: Introduce virtio-net RSS".
- Changed to reuse hash values computed for automq instead of performing RSS hashing when hash reporting is requested but RSS is not.
- Extracted relevant data from struct tun_struct to keep it minimal.
- Added kernel-doc.
- Changed to allow calling TUNGETVNETHASHCAP before TUNSETIFF.
- Initialized num_buffers with 1.
- Added a test case for unclassified packets.
- Fixed error handling in tests.
- Changed tests to verify that the queue index will not overflow.
- Rebased.
- Link to v2: https://lore.kernel.org/r/[email protected]
---
Akihiko Odaki (10):
virtio_net: Add functions for hashing
net: flow_dissector: Export flow_keys_dissector_symmetric
tun: Allow steering eBPF program to fall back
tun: Add common virtio-net hash feature code
tun: Introduce virtio-net hash feature
tap: Introduce virtio-net hash feature
selftest: tun: Test vnet ioctls without device
selftest: tun: Add tests for virtio-net hashing
selftest: tap: Add tests for virtio-net ioctls vhost/net: Support VIRTIO_NET_F_HASH_REPORT
Documentation/networking/tuntap.rst | 7 + drivers/net/Kconfig | 1 + drivers/net/ipvlan/ipvtap.c | 2 +- drivers/net/macvtap.c | 2 +- drivers/net/tap.c | 78 +++++- drivers/net/tun.c | 90 +++++-- drivers/net/tun_vnet.h | 155 ++++++++++- drivers/vhost/net.c | 68 ++--- include/linux/if_tap.h | 4 +- include/linux/skbuff.h | 3 + include/linux/virtio_net.h | 188 ++++++++++++++ include/net/flow_dissector.h | 1 + include/uapi/linux/if_tun.h | 82 ++++++ net/core/flow_dissector.c | 3 +- net/core/skbuff.c | 4 + tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/tap.c | 97 ++++++- tools/testing/selftests/net/tun.c | 491 ++++++++++++++++++++++++++++++++++- 18 files changed, 1194 insertions(+), 84 deletions(-)
---
base-commit: dd83757f6e686a2188997cb58b5975f744bb7786
change-id: 20240403-rss-e737d89efa77
prerequisite-change-id: 20241230-tun-66e10a49b0c7:v6
prerequisite-patch-id: 871dc5f146fb6b0e3ec8612971a8e8190472c0fb
prerequisite-patch-id: 2797ed249d32590321f088373d4055ff3f430a0e
prerequisite-patch-id: ea3370c72d4904e2f0536ec76ba5d26784c0cede
prerequisite-patch-id: 837e4cf5d6b451424f9b1639455e83a260c4440d
prerequisite-patch-id: ea701076f57819e844f5a35efe5cbc5712d3080d
prerequisite-patch-id: 701646fb43ad04cc64dd2bf13c150ccbe6f828ce
prerequisite-patch-id: 53176dae0c003f5b6c114d43f936cf7140d31bb5
prerequisite-change-id: 20250116-buffers-96e14bf023fc:v2
prerequisite-patch-id: 25fd4f99d4236a05a5ef16ab79f3e85ee57e21cc
Best regards,
Signature · 1 line
Akihiko Odaki <[email protected]>
PATCH v11 1/10 virtio_net: Add functions for hashing
They are useful to implement VIRTIO_NET_F_RSS andVIRTIO_NET_F_HASH_REPORT.
--- include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+)
include/linux/virtio_net.h +188 -0
@@ -9,6 +9,194 @@#include <uapi/linux/tcp.h> #include <uapi/linux/virtio_net.h> +struct virtio_net_hash { +u32 value; +u16 report; +}; + +struct virtio_net_toeplitz_state { +u32 hash; +const u32 *key; +}; + +#define VIRTIO_NET_SUPPORTED_HASH_TYPES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \ + VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \ + VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \ + VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \ + VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \ + VIRTIO_NET_RSS_HASH_TYPE_UDPv6) + +#define VIRTIO_NET_RSS_MAX_KEY_SIZE 40 + +static inline void virtio_net_toeplitz_convert_key (u32 *input,size_t len) +{ +while (len >=sizeof (*input)) { + *input =be32_to_cpu ((__force __be32)*input); + input++; + len -=sizeof (*input); + } +} + +static inline void virtio_net_toeplitz_calc (struct virtio_net_toeplitz_state *state, +const __be32 *input,size_t len) +{ +while (len >=sizeof (*input)) { +for (u32 map =be32_to_cpu (*input); map; map &= (map -1 )) { +u32 i =ffs (map); + + state->hash ^= state->key[0 ] << (32 - i) | + (u32)((u64 )state->key[1 ] >> i); + } + + state->key++; + input++; + len -=sizeof (*input); + } +} + +static inline u8 virtio_net_hash_key_length (u32 types) +{ +size_t len =0 ; + +if (types & VIRTIO_NET_HASH_REPORT_IPv4) + len =max (len, +sizeof (struct flow_dissector_key_ipv4_addrs )); + +if (types & + (VIRTIO_NET_HASH_REPORT_TCPv4 | VIRTIO_NET_HASH_REPORT_UDPv4)) + len =max (len, +sizeof (struct flow_dissector_key_ipv4_addrs ) + +sizeof (struct flow_dissector_key_ports )); + +if (types & VIRTIO_NET_HASH_REPORT_IPv6) + len =max (len, +sizeof (struct flow_dissector_key_ipv6_addrs )); + +if (types & + (VIRTIO_NET_HASH_REPORT_TCPv6 | VIRTIO_NET_HASH_REPORT_UDPv6)) + len =max (len, +sizeof (struct flow_dissector_key_ipv6_addrs ) + +sizeof (struct flow_dissector_key_ports )); + +return len +sizeof (u32); +} + +static inline u32 virtio_net_hash_report (u32 types, +const struct flow_keys_basic *keys) +{ +switch (keys->basic.n_proto) { +case cpu_to_be16 (ETH_P_IP ): +if (!(keys->control.flags &FLOW_DIS_IS_FRAGMENT )) { +if (keys->basic.ip_proto ==IPPROTO_TCP && + (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) +return VIRTIO_NET_HASH_REPORT_TCPv4; + +if (keys->basic.ip_proto ==IPPROTO_UDP && + (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) +return VIRTIO_NET_HASH_REPORT_UDPv4; + } + +if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) +return VIRTIO_NET_HASH_REPORT_IPv4; + +return VIRTIO_NET_HASH_REPORT_NONE ; + +case cpu_to_be16 (ETH_P_IPV6 ): +if (!(keys->control.flags &FLOW_DIS_IS_FRAGMENT )) { +if (keys->basic.ip_proto ==IPPROTO_TCP && + (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv6)) +return VIRTIO_NET_HASH_REPORT_TCPv6; + +if (keys->basic.ip_proto ==IPPROTO_UDP && + (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv6)) +return VIRTIO_NET_HASH_REPORT_UDPv6; + } + +if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv6) +return VIRTIO_NET_HASH_REPORT_IPv6; + +return VIRTIO_NET_HASH_REPORT_NONE ; + +default : +return VIRTIO_NET_HASH_REPORT_NONE ; + } +} + +static inline void virtio_net_hash_rss (const struct sk_buff *skb, +u32 types,const u32 *key, +struct virtio_net_hash *hash) +{ +struct virtio_net_toeplitz_state toeplitz_state = { .key = key }; +struct flow_keys flow; +struct flow_keys_basic flow_basic; +u16 report; + +if (!skb_flow_dissect_flow_keys (skb, &flow,0 )) { + hash->report =VIRTIO_NET_HASH_REPORT_NONE ; +return ; + } + + flow_basic = (struct flow_keys_basic ) { + .control = flow.control, + .basic = flow.basic + }; + + report =virtio_net_hash_report (types, &flow_basic); + +switch (report) { +case VIRTIO_NET_HASH_REPORT_IPv4: +virtio_net_toeplitz_calc (&toeplitz_state, + (__be32 *)&flow.addrs.v4addrs, +sizeof (flow.addrs.v4addrs)); +break ; + +case VIRTIO_NET_HASH_REPORT_TCPv4: +virtio_net_toeplitz_calc (&toeplitz_state, + (__be32 *)&flow.addrs.v4addrs, +sizeof (flow.addrs.v4addrs)); +virtio_net_toeplitz_calc (&toeplitz_state, &flow.ports.ports, +sizeof (flow.ports.ports)); +break ; + +case VIRTIO_NET_HASH_REPORT_UDPv4: +virtio_net_toeplitz_calc (&toeplitz_state, + (__be32 *)&flow.addrs.v4addrs, +sizeof (flow.addrs.v4addrs)); +virtio_net_toeplitz_calc (&toeplitz_state, &flow.ports.ports, +sizeof (flow.ports.ports)); +break ; + +case VIRTIO_NET_HASH_REPORT_IPv6: +virtio_net_toeplitz_calc (&toeplitz_state, + (__be32 *)&flow.addrs.v6addrs, +sizeof (flow.addrs.v6addrs)); +break ; + +case VIRTIO_NET_HASH_REPORT_TCPv6: +virtio_net_toeplitz_calc (&toeplitz_state, + (__be32 *)&flow.addrs.v6addrs, +sizeof (flow.addrs.v6addrs)); +virtio_net_toeplitz_calc (&toeplitz_state, &flow.ports.ports, +sizeof (flow.ports.ports)); +break ; + +case VIRTIO_NET_HASH_REPORT_UDPv6: +virtio_net_toeplitz_calc (&toeplitz_state, + (__be32 *)&flow.addrs.v6addrs, +sizeof (flow.addrs.v6addrs)); +virtio_net_toeplitz_calc (&toeplitz_state, &flow.ports.ports, +sizeof (flow.ports.ports)); +break ; + +default : + hash->report =VIRTIO_NET_HASH_REPORT_NONE ; +return ; + } + + hash->value = toeplitz_state.hash; + hash->report = report; +} +static inline bool virtio_net_hdr_match_proto (__be16 protocol,__u8 gso_type) {switch (gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) {
Signature · 1 line
2.48.1
PATCH v11 2/10 net: flow_dissector: Export flow_keys_dissector_symmetric
In reply to Akihiko Odakiflow_keys_dissector_symmetric is useful to derive a symmetric hash and to know its source such as IPv4, IPv6, TCP, and UDP.
--- include/net/flow_dissector.h | 1 + net/core/flow_dissector.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
include/net/flow_dissector.h +1 -0
@@ -423,6 +423,7 @@ __be32 flow_get_u32_src(const struct flow_keys *flow);__be32 flow_get_u32_dst (const struct flow_keys *flow);extern struct flow_dissector flow_keys_dissector; +extern struct flow_dissector flow_keys_dissector_symmetric;extern struct flow_dissector flow_keys_basic_dissector;/* struct flow_keys_digest:
net/core/flow_dissector.c +2 -1
@@ -1862,7 +1862,8 @@ void make_flow_keys_digest(struct flow_keys_digest *digest, }EXPORT_SYMBOL (make_flow_keys_digest); -static struct flow_dissector flow_keys_dissector_symmetric__read_mostly ; +struct flow_dissector flow_keys_dissector_symmetric__read_mostly ; +EXPORT_SYMBOL (flow_keys_dissector_symmetric);u32 __skb_get_hash_symmetric_net (const struct net *net,const struct sk_buff *skb) {
Signature · 1 line
2.48.1
PATCH v11 3/10 tun: Allow steering eBPF program to fall back
In reply to Akihiko OdakiThis clarifies a steering eBPF program takes precedence over the other steering algorithms.
--- Documentation/networking/tuntap.rst | 7 +++++++ drivers/net/tun.c | 28 +++++++++++++++++----------- include/uapi/linux/if_tun.h | 9 +++++++++ 3 files changed, 33 insertions(+), 11 deletions(-)
Documentation/networking/tuntap.rst +7 -0
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it:: return ioctl(fd, TUNSETQUEUE, (void *)&ifr); } +3.4 Reference +------------- + +``linux/if_tun.h`` defines the interface described below: + +.. kernel-doc:: include/uapi/linux/if_tun.h + Universal TUN/TAP device driver Frequently Asked Question =========================================================
drivers/net/tun.c +17 -11
@@ -476,21 +476,29 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)return txq; } -static u16 tun_ebpf_select_queue (struct tun_struct *tun,struct sk_buff *skb) +static bool tun_ebpf_select_queue (struct tun_struct *tun,struct sk_buff *skb, +u16 *ret) {struct tun_prog *prog;u32 numqueues; -u16 ret =0 ; +u32 prog_ret; + + prog =rcu_dereference (tun->steering_prog); +if (!prog) +return false; numqueues =READ_ONCE (tun->numqueues); -if (!numqueues) -return 0 ; +if (!numqueues) { + *ret =0 ; +return true; + } - prog =rcu_dereference (tun->steering_prog); -if (prog) - ret =bpf_prog_run_clear_cb (prog->prog, skb); + prog_ret =bpf_prog_run_clear_cb (prog->prog, skb); +if (prog_ret ==TUN_STEERINGEBPF_FALLBACK ) +return false; -return ret % numqueues; + *ret = (u16 )prog_ret % numqueues; +return true; } static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
@@ -500,9 +508,7 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,u16 ret;rcu_read_lock (); -if (rcu_dereference (tun->steering_prog)) - ret =tun_ebpf_select_queue (tun, skb); -else +if (!tun_ebpf_select_queue (tun, skb, &ret)) ret =tun_automq_select_queue (tun, skb);rcu_read_unlock ();
include/uapi/linux/if_tun.h +9 -0
@@ -115,4 +115,13 @@ struct tun_filter { __u8 addr[][ETH_ALEN]; }; +/** +* define TUN_STEERINGEBPF_FALLBACK - A steering eBPF return value to fall back +* +* A steering eBPF program may return this value to fall back to the steering +* algorithm that should have been used if the program was not set. This allows +* selectively overriding the steering decision. +*/ +#define TUN_STEERINGEBPF_FALLBACK -1 +#endif /* _UAPI__IF_TUN_H */
Signature · 1 line
2.48.1
PATCH v11 4/10 tun: Add common virtio-net hash feature code
In reply to Akihiko OdakiAdd common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.
Added Features
==============
Hash reporting
--------------
Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.
Receive Side Scaling (RSS)
--------------------------
RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.
Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.
Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.
Added ioctls
============
They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.
TUNGETVNETHASHCAP
-----------------
This ioctl tells supported features and hash types. It is useful to check if a VM can be migrated to the current host.
TUNSETVNETHASH
--------------
This ioctl allows setting features and hash types to be enabled. It limits the features exposed to the guest to ensure proper migration. It also sets RSS parameters, depending on the enabled features and hash types.
--- drivers/net/tap.c | 10 ++- drivers/net/tun.c | 12 +++- drivers/net/tun_vnet.h | 155 +++++++++++++++++++++++++++++++++++++++++--- include/uapi/linux/if_tun.h | 73 +++++++++++++++++++++ 4 files changed, 236 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)sock_put (&q->sk); } +static const struct virtio_net_hash *tap_find_hash (const struct sk_buff *skb) +{ +return NULL ; +} +/* * Select a queue based on the rxq of the device on which this packet * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,int total;if (q->flags &IFF_VNET_HDR ) { -struct virtio_net_hdr vnet_hdr; +struct virtio_net_hdr_v1_hash vnet_hdr; vnet_hdr_len =READ_ONCE (q->vnet_hdr_sz); - ret =tun_vnet_hdr_from_skb (q->flags,NULL , skb, &vnet_hdr); + ret =tun_vnet_hdr_from_skb (vnet_hdr_len, q->flags,NULL , skb, + tap_find_hash, &vnet_hdr);if (ret)return ret;
drivers/net/tun.c +9 -3
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) e->rps_rxhash = hash; } +static const struct virtio_net_hash *tun_find_hash (const struct sk_buff *skb) +{ +return NULL ; +} +/* We try to identify a flow through its rxhash. The reason that * we do not check rxq no. is because some cards(e.g 82599), chooses * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,ssize_t ret;if (tun->flags &IFF_VNET_HDR ) { -struct virtio_net_hdr gso = {0 }; +struct virtio_net_hdr_v1_hash gso = {0 }; vnet_hdr_sz =READ_ONCE (tun->vnet_hdr_sz); ret =tun_vnet_hdr_put (vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun, }if (vnet_hdr_sz) { -struct virtio_net_hdr gso; +struct virtio_net_hdr_v1_hash gso; - ret =tun_vnet_hdr_from_skb (tun->flags, tun->dev, skb, &gso); + ret =tun_vnet_hdr_from_skb (vnet_hdr_sz, tun->flags, tun->dev, + skb, tun_find_hash, &gso);if (ret)return ret;
drivers/net/tun_vnet.h +146 -9
199 lines not on this page · raw message
include/uapi/linux/if_tun.h +73 -0
83 lines not on this page · raw message
Signature · 1 line
2.48.1
PATCH v11 5/10 tun: Introduce virtio-net hash feature
In reply to Akihiko OdakiAdd ioctls and storage required for the virtio-net hash feature to TUN.
--- drivers/net/Kconfig | 1 + drivers/net/tun.c | 54 ++++++++++++++++++++++++++++++++++++++++++-------- include/linux/skbuff.h | 3 +++ net/core/skbuff.c | 4 ++++ 4 files changed, 54 insertions(+), 8 deletions(-)
drivers/net/Kconfig +1 -0
@@ -395,6 +395,7 @@ config TUN tristate "Universal TUN/TAP device driver support" depends on INET select CRC32 + select SKB_EXTENSIONS help TUN/TAP provides packet reception and transmission for user space programs. It can be viewed as a simple Point-to-Point or Ethernet
drivers/net/tun.c +46 -8
117 lines not on this page · raw message
include/linux/skbuff.h +3 -0
@@ -4842,6 +4842,9 @@ enum skb_ext_id {#endif #if IS_ENABLED (CONFIG_MCTP_FLOWS ) SKB_EXT_MCTP, +#endif +#if IS_ENABLED (CONFIG_TUN ) +SKB_EXT_TUN_VNET_HASH , #endif SKB_EXT_NUM, /* must be last */ };
net/core/skbuff.c +4 -0
@@ -64,6 +64,7 @@#include <linux/mpls.h> #include <linux/kcov.h> #include <linux/iov_iter.h> +#include <linux/virtio_net.h> #include <net/protocol.h> #include <net/dst.h>
@@ -4969,6 +4970,9 @@ static const u8 skb_ext_type_len[] = {#if IS_ENABLED (CONFIG_MCTP_FLOWS ) [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), #endif +#if IS_ENABLED(CONFIG_TUN) + [SKB_EXT_TUN_VNET_HASH] = SKB_EXT_CHUNKSIZEOF(struct virtio_net_hash), +#endif }; static __always_inline unsigned int skb_ext_total_length(void)
Signature · 1 line
2.48.1
PATCH v11 6/10 tap: Introduce virtio-net hash feature
In reply to Akihiko OdakiAdd ioctls and storage required for the virtio-net hash feature to TAP.
--- drivers/net/ipvlan/ipvtap.c | 2 +- drivers/net/macvtap.c | 2 +- drivers/net/tap.c | 70 +++++++++++++++++++++++++++++++++++++++++---- include/linux/if_tap.h | 4 ++- 4 files changed, 69 insertions(+), 9 deletions(-)
drivers/net/ipvlan/ipvtap.c +1 -1
@@ -114,7 +114,7 @@ static void ipvtap_dellink(struct net_device *dev,struct ipvtap_dev *vlan =netdev_priv (dev);netdev_rx_handler_unregister (dev); -tap_del_queues (&vlan->tap); +tap_del (&vlan->tap);ipvlan_link_delete (dev, head); }
drivers/net/macvtap.c +1 -1
@@ -122,7 +122,7 @@ static void macvtap_dellink(struct net_device *dev,struct macvtap_dev *vlantap =netdev_priv (dev);netdev_rx_handler_unregister (dev); -tap_del_queues (&vlantap->tap); +tap_del (&vlantap->tap);macvlan_dellink (dev, head); }
drivers/net/tap.c +64 -6
126 lines not on this page · raw message
include/linux/if_tap.h +3 -1
@@ -31,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)#define MAX_TAP_QUEUES 256struct tap_queue ; +struct tun_vnet_hash_container ;struct tap_dev {struct net_device *dev;
@@ -43,6 +44,7 @@ struct tap_dev {int numqueues;netdev_features_t tap_features;int minor; +struct tun_vnet_hash_container __rcu *vnet_hash;void (*update_features)(struct tap_dev *tap,netdev_features_t features);void (*count_tx_dropped)(struct tap_dev *tap);
@@ -74,7 +76,7 @@ struct tap_queue { };rx_handler_result_t tap_handle_frame (struct sk_buff **pskb); -void tap_del_queues (struct tap_dev *tap); +void tap_del (struct tap_dev *tap);int tap_get_minor (dev_t major,struct tap_dev *tap);void tap_free_minor (dev_t major,struct tap_dev *tap);int tap_queue_resize (struct tap_dev *tap);
Signature · 1 line
2.48.1
PATCH v11 7/10 selftest: tun: Test vnet ioctls without device
In reply to Akihiko OdakiEnsure that vnet ioctls result in EBADFD when the underlying device is deleted.
--- tools/testing/selftests/net/tun.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
tools/testing/selftests/net/tun.c +38 -0
@@ -159,4 +159,42 @@ TEST_F(tun, reattach_close_delete) {EXPECT_EQ (tun_delete (self->ifname),0 ); } +FIXTURE (tun_deleted) +{ +char ifname[IFNAMSIZ ]; +int fd; +}; + +FIXTURE_SETUP (tun_deleted) +{ + self->ifname[0 ] =0 ; + self->fd =tun_alloc (self->ifname); +ASSERT_LE (0 , self->fd); + + ASSERT_EQ(0, tun_delete(self->ifname)) +EXPECT_EQ (0 ,close (self->fd)); +} + +FIXTURE_TEARDOWN (tun_deleted) +{ +EXPECT_EQ (0 ,close (self->fd)); +} + +TEST_F(tun_deleted, getvnethdrsz) +{ +ASSERT_EQ (-1 ,ioctl (self->fd,TUNGETVNETHDRSZ )); +EXPECT_EQ (EBADFD , errno); +} + +TEST_F(tun_deleted, getvnethashcap) +{ +struct tun_vnet_hash cap; +int i =ioctl (self->fd,TUNGETVNETHASHCAP , &cap); + +if (i ==-1 && errno ==EBADFD ) +SKIP (return,"TUNGETVNETHASHCAP not supported" ); + +EXPECT_EQ (0 , i); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.48.1
PATCH v11 8/10 selftest: tun: Add tests for virtio-net hashing
In reply to Akihiko OdakiThe added tests confirm tun can perform RSS for all supported hash types to select the receive queue and report hash values.
--- tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/tun.c | 455 ++++++++++++++++++++++++++++++++++- 2 files changed, 447 insertions(+), 10 deletions(-)
tools/testing/selftests/net/Makefile +1 -1
@@ -123,6 +123,6 @@ $(OUTPUT)/reuseport_bpf_numa: LDLIBS += -lnuma $(OUTPUT)/tcp_mmap: LDLIBS += -lpthread -lcrypto $(OUTPUT)/tcp_inq: LDLIBS += -lpthread $(OUTPUT)/bind_bhash: LDLIBS += -lpthread -$(OUTPUT)/io_uring_zerocopy_tx: CFLAGS += -I../../../include/ +$(OUTPUT)/io_uring_zerocopy_tx $(OUTPUT)/tun: CFLAGS += -I../../../include/ include bpf.mk
tools/testing/selftests/net/tun.c +446 -9
514 lines not on this page · raw message
Signature · 1 line
2.48.1
PATCH v11 9/10 selftest: tap: Add tests for virtio-net ioctls
In reply to Akihiko OdakiThey only test the ioctls are wired up to the implementation common with tun as it is already tested for tun.
--- tools/testing/selftests/net/tap.c | 97 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-)
tools/testing/selftests/net/tap.c +95 -2
@@ -363,6 +363,7 @@ size_t build_test_packet_crash_tap_invalid_eth_proto(uint8_t *buf,FIXTURE (tap) {int fd; +bool deleted; };FIXTURE_SETUP (tap)
@@ -387,8 +388,10 @@ FIXTURE_TEARDOWN(tap)if (self->fd !=-1 )close (self->fd); - ret =dev_delete (param_dev_tap_name); -EXPECT_EQ (ret,0 ); +if (!self->deleted) { + ret =dev_delete (param_dev_tap_name); +EXPECT_EQ (ret,0 ); + } ret =dev_delete (param_dev_dummy_name);EXPECT_EQ (ret,0 );
@@ -431,4 +434,94 @@ TEST_F(tap, test_packet_crash_tap_invalid_eth_proto)ASSERT_EQ (errno,EINVAL ); } +TEST_F(tap, test_vnethdrsz) +{ +int sz =sizeof (struct virtio_net_hdr_v1_hash ); + +ASSERT_FALSE (dev_delete (param_dev_tap_name)); + self->deleted = true; + +ASSERT_FALSE (ioctl (self->fd,TUNSETVNETHDRSZ , &sz)); + sz =0 ; +ASSERT_FALSE (ioctl (self->fd,TUNGETVNETHDRSZ , &sz)); +EXPECT_EQ (sizeof (struct virtio_net_hdr_v1_hash ), sz); +} + +TEST_F(tap, test_vnetle) +{ +int le =1 ; + +ASSERT_FALSE (dev_delete (param_dev_tap_name)); + self->deleted = true; + +ASSERT_FALSE (ioctl (self->fd,TUNSETVNETLE , &le)); + le =0 ; +ASSERT_FALSE (ioctl (self->fd,TUNGETVNETLE , &le)); +EXPECT_EQ (1 , le); +} + +TEST_F(tap, test_vnetbe) +{ +int be =1 ; +int ret; + +ASSERT_FALSE (dev_delete (param_dev_tap_name)); + self->deleted = true; + + ret =ioctl (self->fd,TUNSETVNETBE , &be); +if (ret ==-1 && errno ==EINVAL ) +SKIP (return,"TUNSETVNETBE not supported" ); + +ASSERT_FALSE (ret); + be =0 ; +ASSERT_FALSE (ioctl (self->fd,TUNGETVNETBE , &be)); +EXPECT_EQ (1 , be); +} + +TEST_F(tap, test_getvnethashcap) +{ +static const struct tun_vnet_hash expected = { + .flags =TUN_VNET_HASH_REPORT |TUN_VNET_HASH_RSS , + .types = VIRTIO_NET_RSS_HASH_TYPE_IPv4 | + VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | + VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | + VIRTIO_NET_RSS_HASH_TYPE_IPv6 | + VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | + VIRTIO_NET_RSS_HASH_TYPE_UDPv6 + }; +struct tun_vnet_hash seen; +int ret; + +ASSERT_FALSE (dev_delete (param_dev_tap_name)); + self->deleted = true; + + ret =ioctl (self->fd,TUNGETVNETHASHCAP , &seen); + +if (ret ==-1 && errno ==EINVAL ) +SKIP (return,"TUNGETVNETHASHCAP not supported" ); + +EXPECT_FALSE (ret); +EXPECT_FALSE (memcmp (&expected, &seen,sizeof (expected))); +} + +TEST_F(tap, test_setvnethash_alive) +{ +struct tun_vnet_hash hash = { .flags =0 }; + +EXPECT_FALSE (ioctl (self->fd,TUNSETVNETHASH , &hash)); +} + +TEST_F(tap, test_setvnethash_deleted) +{ +ASSERT_FALSE (dev_delete (param_dev_tap_name)); + self->deleted = true; + +ASSERT_EQ (-1 ,ioctl (self->fd,TUNSETVNETHASH )); + +if (errno ==EINVAL ) +SKIP (return,"TUNSETVNETHASH not supported" ); + +EXPECT_EQ (EBADFD , errno); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.48.1
Jakub Kicinski
maintainerRe: [PATCH net-next v11 09/10] selftest: tap: Add tests for virtio-net ioctls
Quoted 6 lines · On Mon, 17 Mar 2025 19:57:59 +0900 Akihiko Odaki wrote:
(quoted diff) +6
-0+TEST_F(tap, test_vnetbe) +{ + int be = 1; + int ret; + + ASSERT_FALSE(dev_delete(param_dev_tap_name));
Review
Quoted 10 lines
(quoted diff) +10
-0+ self->deleted = true; + + ret = ioctl(self->fd, TUNSETVNETBE, &be); + if (ret == -1 && errno == EINVAL) + SKIP(return, "TUNSETVNETBE not supported"); + + ASSERT_FALSE(ret); + be = 0; + ASSERT_FALSE(ioctl(self->fd, TUNGETVNETBE, &be)); + EXPECT_EQ(1, be);
The config options needed by selftests should be listed in the relevant config file, I think you need this snippet:
tools/testing/selftests/net/config +1 -0
@@ -101,6 +101,7 @@ CONFIG_IPV6_IOAM6_LWTUNNEL=y CONFIG_CRYPTO_SM4_GENERIC=y CONFIG_AMT=m CONFIG_TUN=y +CONFIG_TUN_VNET_CROSS_LE=y CONFIG_VXLAN=m CONFIG_IP_SCTP=m CONFIG_NETFILTER_XT_MATCH_POLICY=m
Signature · 1 line
pw-bot: cr
PATCH v11 10/10 vhost/net: Support VIRTIO_NET_F_HASH_REPORT
In reply to Akihiko OdakiVIRTIO_NET_F_HASH_REPORT allows to report hash values calculated on the host. When VHOST_NET_F_VIRTIO_NET_HDR is employed, it will report no hash values (i.e., the hash_report member is always set to VIRTIO_NET_HASH_REPORT_NONE). Otherwise, the values reported by the underlying socket will be reported.
VIRTIO_NET_F_HASH_REPORT requires VIRTIO_F_VERSION_1.
--- drivers/vhost/net.c | 68 +++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 33 deletions(-)
drivers/vhost/net.c +35 -33
@@ -73,6 +73,7 @@ enum {VHOST_NET_FEATURES =VHOST_FEATURES | (1ULL <<VHOST_NET_F_VIRTIO_NET_HDR ) | (1ULL <<VIRTIO_NET_F_MRG_RXBUF ) | + (1ULL <<VIRTIO_NET_F_HASH_REPORT ) | (1ULL <<VIRTIO_F_ACCESS_PLATFORM ) | (1ULL << VIRTIO_F_RING_RESET) };
@@ -1097,10 +1098,6 @@ static void handle_rx(struct vhost_net *net) .msg_controllen = 0, .msg_flags = MSG_DONTWAIT, }; -struct virtio_net_hdr hdr = { - .flags =0 , - .gso_type =VIRTIO_NET_HDR_GSO_NONE - };size_t total_len =0 ;int err, mergeable;s16 headcount;
@@ -1174,11 +1171,15 @@ static void handle_rx(struct vhost_net *net)/* We don't need to be notified again. */ iov_iter_init (&msg.msg_iter,ITER_DEST , vq->iov, in, vhost_len); fixup = msg.msg_iter; -if (unlikely ((vhost_hlen))) { -/* We will supply the header ourselves -* TODO: support TSO. -*/ -iov_iter_advance (&msg.msg_iter, vhost_hlen); +/* +* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR +* TODO: support TSO. +*/ +if (unlikely (vhost_hlen) && +iov_iter_zero (vhost_hlen, &msg.msg_iter) != vhost_hlen) { +vq_err (vq,"Unable to write vnet_hdr at addr %p\n" , + vq->iov->iov_base); + goto out; } err = sock->ops->recvmsg (sock, &msg, sock_len,MSG_DONTWAIT |MSG_TRUNC );
@@ -1191,30 +1192,24 @@ static void handle_rx(struct vhost_net *net)vhost_discard_vq_desc (vq, headcount);continue ; } -/* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */ -if (unlikely (vhost_hlen)) { -if (copy_to_iter (&hdr,sizeof (hdr), - &fixup) !=sizeof (hdr)) { -vq_err (vq,"Unable to write vnet_hdr " -"at addr %p\n" , vq->iov->iov_base); - goto out; - } - }else { -/* Header came from socket; we'll need to patch -* ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF -*/ -iov_iter_advance (&fixup,sizeof (hdr)); - }/* TODO: Should check and handle checksum. */ +/* +* We'll need to patch ->num_buffers over if +* VIRTIO_NET_F_MRG_RXBUF or VIRTIO_F_VERSION_1 +*/ num_buffers =cpu_to_vhost16 (vq, headcount); -if (likely (set_num_buffers) && -copy_to_iter (&num_buffers,sizeof num_buffers, - &fixup) !=sizeof num_buffers) { -vq_err (vq,"Failed num_buffers write" ); -vhost_discard_vq_desc (vq, headcount); - goto out; +if (likely (set_num_buffers)) { +iov_iter_advance (&fixup, offsetof(struct virtio_net_hdr_v1 , num_buffers)); + +if (copy_to_iter (&num_buffers,sizeof (num_buffers), + &fixup) !=sizeof (num_buffers)) { +vq_err (vq,"Failed num_buffers write" ); +vhost_discard_vq_desc (vq, headcount); + goto out; + } } + nvq->done_idx += headcount;if (nvq->done_idx >VHOST_NET_BATCH )vhost_net_signal_used (nvq);
@@ -1607,10 +1602,13 @@ static int vhost_net_set_features(struct vhost_net *n, u64 features)size_t vhost_hlen, sock_hlen, hdr_len;int i; - hdr_len = (features & ((1ULL <<VIRTIO_NET_F_MRG_RXBUF ) | - (1ULL <<VIRTIO_F_VERSION_1 ))) ? -sizeof (struct virtio_net_hdr_mrg_rxbuf ) : -sizeof (struct virtio_net_hdr ); +if (features & (1ULL <<VIRTIO_NET_F_HASH_REPORT )) + hdr_len =sizeof (struct virtio_net_hdr_v1_hash ); +else if (features & ((1ULL <<VIRTIO_NET_F_MRG_RXBUF ) | + (1ULL <<VIRTIO_F_VERSION_1 ))) + hdr_len =sizeof (struct virtio_net_hdr_mrg_rxbuf ); +else + hdr_len =sizeof (struct virtio_net_hdr );if (features & (1 <<VHOST_NET_F_VIRTIO_NET_HDR )) {/* vhost provides vnet_hdr */ vhost_hlen = hdr_len;
@@ -1691,6 +1689,10 @@ static long vhost_net_ioctl(struct file *f, unsigned int ioctl,return -EFAULT ;if (features & ~VHOST_NET_FEATURES )return -EOPNOTSUPP ; +if ((features & ((1ULL <<VIRTIO_F_VERSION_1 ) | + (1ULL <<VIRTIO_NET_F_HASH_REPORT ))) == + (1ULL <<VIRTIO_NET_F_HASH_REPORT )) +return -EINVAL ;return vhost_net_set_features (n, features);case VHOST_GET_BACKEND_FEATURES : features =VHOST_NET_BACKEND_FEATURES ;
Signature · 1 line
2.48.1
Quoted 29 lines · Akihiko Odaki
virtio-net have two usage of hashes: one is RSS and another is hash reporting. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.
Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF.
Introduce the code to compute hashes to the kernel in order to overcome thse challenges.
An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but it is based on context rewrites, which is in feature freeze. We can adopt kfuncs, but they will not be UAPIs. We opt to ioctl to align with other relevant UAPIs (KVM and
vhost_net).The patches for QEMU to use this new feature was submitted as RFC and is available at:
https://patchew.org/QEMU/[email protected]/This work was presented at LPC 2024:
https://lpc.events/event/18/contributions/1963/V1 -> V2:
Changed to introduce a new BPF program type.
Quoted 7 lines
Signed-off-by Akihiko Odaki <[email protected]>---
Changes in v11:
- Added the missing code to freevnet_hashin patch
"tap: Introduce virtio-net hash feature".
- Link to v10: https://lore.kernel.org/r/[email protected]
We only have 2 or 3 points that need to be sorted out. Let's hold on to the iteration until we had an agreement.
Thanks
Lei Yang
Tested-byQE tested this series of patches v11 under linux-next repo with virtio-net regression tests, everything works fine.
Quoted 44 lines · Jason Wang
Quoted 29 lines · Akihiko Odaki
virtio-net have two usage of hashes: one is RSS and another is hash reporting. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.
Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF.
Introduce the code to compute hashes to the kernel in order to overcome thse challenges.
An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but it is based on context rewrites, which is in feature freeze. We can adopt kfuncs, but they will not be UAPIs. We opt to ioctl to align with other relevant UAPIs (KVM and
vhost_net).The patches for QEMU to use this new feature was submitted as RFC and is available at:
https://patchew.org/QEMU/[email protected]/This work was presented at LPC 2024:
https://lpc.events/event/18/contributions/1963/V1 -> V2:
Changed to introduce a new BPF program type.Quoted 7 lines
Signed-off-by Akihiko Odaki <[email protected]>---
Changes in v11:
- Added the missing code to freevnet_hashin patch
"tap: Introduce virtio-net hash feature".
- Link to v10: https://lore.kernel.org/r/[email protected]We only have 2 or 3 points that need to be sorted out. Let's hold on to the iteration until we had an agreement.
Thanks