tun: Introduce virtio-net hashing feature
Subsystems (9)
v8 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 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 (6):
virtio_net: Add functions for hashing
net: flow_dissector: Export flow_keys_dissector_symmetric
tun: Introduce virtio-net hash feature
selftest: tun: Test vnet ioctls without device
selftest: tun: Add tests for virtio-net hashing vhost/net: Support VIRTIO_NET_F_HASH_REPORT
Documentation/networking/tuntap.rst | 7 + drivers/net/Kconfig | 1 + drivers/net/tap.c | 67 +++- drivers/net/tun.c | 98 +++++- drivers/net/tun_vnet.h | 159 ++++++++- drivers/vhost/net.c | 49 +-- include/linux/if_tap.h | 2 + include/linux/skbuff.h | 3 + include/linux/virtio_net.h | 188 ++++++++++ include/net/flow_dissector.h | 1 + include/uapi/linux/if_tun.h | 75 ++++ net/core/flow_dissector.c | 3 +- net/core/skbuff.c | 4 + tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/tun.c | 656 ++++++++++++++++++++++++++++++++++- 15 files changed, 1254 insertions(+), 61 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 v8 1/6 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 v8 2/6 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 v8 3/6 tun: Introduce virtio-net hash feature
In reply to Akihiko OdakiHash reporting
==============
Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.
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.
--- Documentation/networking/tuntap.rst | 7 ++ drivers/net/Kconfig | 1 + drivers/net/tap.c | 67 ++++++++++++++- drivers/net/tun.c | 98 +++++++++++++++++----- drivers/net/tun_vnet.h | 159 ++++++++++++++++++++++++++++++++++-- include/linux/if_tap.h | 2 + include/linux/skbuff.h | 3 + include/uapi/linux/if_tun.h | 75 +++++++++++++++++ net/core/skbuff.c | 4 + 9 files changed, 385 insertions(+), 31 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/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/tap.c +64 -3
116 lines not on this page · raw message
drivers/net/tun.c +79 -19
203 lines not on this page · raw message
drivers/net/tun_vnet.h +150 -9
203 lines not on this page · raw message
include/linux/if_tap.h +2 -0
@@ -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);
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 */ };
include/uapi/linux/if_tun.h +75 -0
85 lines not on this page · raw message
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
Jakub Kicinski
maintainerRe: [PATCH net-next v8 3/6] tun: Introduce virtio-net hash feature
Quoted 18 lines · On Thu, 06 Mar 2025 18:56:33 +0900 Akihiko Odaki wrote:
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.
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.
Quoted 8 lines
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_netare UAPIs.
drivers/net/tap.c:1056:2: warning: unannotated fall-through between switch labels [-Wimplicit-fallthrough]
1056 | case SIOCGIFHWADDR:
| ^
drivers/net/tap.c:1056:2: note: insert '__attribute__((fallthrough));' to silence this warning
1056 | case SIOCGIFHWADDR:
| ^
| __attribute__((fallthrough));
drivers/net/tap.c:1056:2: note: insert 'break;' to avoid fall-through
1056 | case SIOCGIFHWADDR:
| ^
| break; Signature · 1 line
pw-bot: cr
kernel test robot
build botRe: [PATCH net-next v8 3/6] tun: Introduce virtio-net hash feature
In reply to Akihiko Odakikernel test robot noticed the following build warnings:
Hi Akihiko,
kernel test robot noticed the following build warnings:
[auto build test WARNING on dd83757f6e686a2188997cb58b5975f744bb7786]
url: https://github.com/intel-lab-lkp/linux/commits/Akihiko-Odaki/virtio_net-Add-functions-for-hashing/20250306-180546 base: dd83757f6e686a2188997cb58b5975f744bb7786 patch link: https://lore.kernel.org/r/20250306-rss-v8-3-7ab4f56ff423%40daynix.com patch subject: [PATCH net-next v8 3/6] tun: Introduce virtio-net hash feature config: x86_64-buildonly-randconfig-001-20250307 (https://download.01.org/0day-ci/archive/20250307/[email protected]/config) compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250307/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> drivers/net/tap.c:1056:2: warning: unannotated fall-through between switch labels [-Wimplicit-fallthrough]
1056 | case SIOCGIFHWADDR:
| ^
drivers/net/tap.c:1056:2: note: insert '__attribute__((fallthrough));' to silence this warning
1056 | case SIOCGIFHWADDR:
| ^
| __attribute__((fallthrough));
drivers/net/tap.c:1056:2: note: insert 'break;' to avoid fall-through
1056 | case SIOCGIFHWADDR:
| ^
| break; 1 warning generated.
vim +1056 drivers/net/tap.c
2be5c76794b0e5 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 964
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 965 /*
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 966 * provide compatibility with generic tun/tap interface
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 967 */
635b8c8ecdd271 drivers/net/tap.c Sainath Grandhi 2017-02-10 968 static long tap_ioctl(struct file *file, unsigned int cmd,
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 969 unsigned long arg)
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 970 {
635b8c8ecdd271 drivers/net/tap.c Sainath Grandhi 2017-02-10 971 struct tap_queue *q = file->private_data;
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 972 struct tap_dev *tap;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 973 void __user *argp = (void __user *)arg;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 974 struct ifreq __user *ifr = argp;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 975 unsigned int __user *up = argp;
39ec7de7092ba9 drivers/net/macvtap.c Michael S. Tsirkin 2014-12-16 976 unsigned short u;
55afbd0810922a drivers/net/macvtap.c Michael S. Tsirkin 2010-04-29 977 int __user *sp = argp;
7f460d30c8e130 drivers/net/macvtap.c Justin Cormack 2015-05-13 978 struct sockaddr sa;
55afbd0810922a drivers/net/macvtap.c Michael S. Tsirkin 2010-04-29 979 int s;
02df55d28c6001 drivers/net/macvtap.c Arnd Bergmann 2010-02-18 980 int ret;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 981
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 982 switch (cmd) {
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 983 case TUNSETIFF:
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 984 /* ignore the name, just look at flags */
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 985 if (get_user(u, &ifr->ifr_flags))
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 986 return -EFAULT;
b9fb9ee07e67fc drivers/net/macvtap.c Arnd Bergmann 2010-02-18 987
b9fb9ee07e67fc drivers/net/macvtap.c Arnd Bergmann 2010-02-18 988 ret = 0;
635b8c8ecdd271 drivers/net/tap.c Sainath Grandhi 2017-02-10 989 if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
b9fb9ee07e67fc drivers/net/macvtap.c Arnd Bergmann 2010-02-18 990 ret = -EINVAL;
b9fb9ee07e67fc drivers/net/macvtap.c Arnd Bergmann 2010-02-18 991 else
635b8c8ecdd271 drivers/net/tap.c Sainath Grandhi 2017-02-10 992 q->flags = (q->flags & ~TAP_IFFEATURES) | u;
b9fb9ee07e67fc drivers/net/macvtap.c Arnd Bergmann 2010-02-18 993
b9fb9ee07e67fc drivers/net/macvtap.c Arnd Bergmann 2010-02-18 994 return ret;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 995
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 996 case TUNGETIFF:
441ac0fcaadc76 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 997 rtnl_lock();
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 998 tap = tap_get_tap_dev(q);
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 999 if (!tap) {
441ac0fcaadc76 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 1000 rtnl_unlock();
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1001 return -ENOLINK;
441ac0fcaadc76 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 1002 }
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1003
02df55d28c6001 drivers/net/macvtap.c Arnd Bergmann 2010-02-18 1004 ret = 0;
39ec7de7092ba9 drivers/net/macvtap.c Michael S. Tsirkin 2014-12-16 1005 u = q->flags;
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 1006 if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
39ec7de7092ba9 drivers/net/macvtap.c Michael S. Tsirkin 2014-12-16 1007 put_user(u, &ifr->ifr_flags))
02df55d28c6001 drivers/net/macvtap.c Arnd Bergmann 2010-02-18 1008 ret = -EFAULT;
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 1009 tap_put_tap_dev(tap);
441ac0fcaadc76 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 1010 rtnl_unlock();
02df55d28c6001 drivers/net/macvtap.c Arnd Bergmann 2010-02-18 1011 return ret;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1012
815f236d622721 drivers/net/macvtap.c Jason Wang 2013-06-05 1013 case TUNSETQUEUE:
815f236d622721 drivers/net/macvtap.c Jason Wang 2013-06-05 1014 if (get_user(u, &ifr->ifr_flags))
815f236d622721 drivers/net/macvtap.c Jason Wang 2013-06-05 1015 return -EFAULT;
441ac0fcaadc76 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 1016 rtnl_lock();
635b8c8ecdd271 drivers/net/tap.c Sainath Grandhi 2017-02-10 1017 ret = tap_ioctl_set_queue(file, u);
441ac0fcaadc76 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 1018 rtnl_unlock();
82a19eb8c02ab9 drivers/net/macvtap.c Jason Wang 2013-07-16 1019 return ret;
815f236d622721 drivers/net/macvtap.c Jason Wang 2013-06-05 1020
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1021 case TUNGETFEATURES:
635b8c8ecdd271 drivers/net/tap.c Sainath Grandhi 2017-02-10 1022 if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1023 return -EFAULT;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1024 return 0;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1025
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1026 case TUNSETSNDBUF:
3ea79249e81e5e drivers/net/macvtap.c Michael S. Tsirkin 2015-09-18 1027 if (get_user(s, sp))
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1028 return -EFAULT;
93161922c658c7 drivers/net/tap.c Craig Gallek 2017-10-30 1029 if (s <= 0)
93161922c658c7 drivers/net/tap.c Craig Gallek 2017-10-30 1030 return -EINVAL;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1031
3ea79249e81e5e drivers/net/macvtap.c Michael S. Tsirkin 2015-09-18 1032 q->sk.sk_sndbuf = s;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1033 return 0;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1034
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1035 case TUNSETOFFLOAD:
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1036 /* let the user check for future flags */
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1037 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
399e0827642f6a drivers/net/tap.c Andrew Melnychenko 2022-12-07 1038 TUN_F_TSO_ECN | TUN_F_UFO |
399e0827642f6a drivers/net/tap.c Andrew Melnychenko 2022-12-07 1039 TUN_F_USO4 | TUN_F_USO6))
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1040 return -EINVAL;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1041
2be5c76794b0e5 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 1042 rtnl_lock();
2be5c76794b0e5 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 1043 ret = set_offload(q, arg);
2be5c76794b0e5 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 1044 rtnl_unlock();
2be5c76794b0e5 drivers/net/macvtap.c Vlad Yasevich 2013-06-25 1045 return ret;
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1046
2c592c9b450ea4 drivers/net/tap.c Akihiko Odaki 2025-03-06 1047 case TUNGETVNETHASHCAP:
2c592c9b450ea4 drivers/net/tap.c Akihiko Odaki 2025-03-06 1048 return tun_vnet_ioctl_gethashcap(argp);
2c592c9b450ea4 drivers/net/tap.c Akihiko Odaki 2025-03-06 1049
2c592c9b450ea4 drivers/net/tap.c Akihiko Odaki 2025-03-06 1050 case TUNSETVNETHASH:
2c592c9b450ea4 drivers/net/tap.c Akihiko Odaki 2025-03-06 1051 rtnl_lock();
2c592c9b450ea4 drivers/net/tap.c Akihiko Odaki 2025-03-06 1052 tap = rtnl_dereference(q->tap);
2c592c9b450ea4 drivers/net/tap.c Akihiko Odaki 2025-03-06 1053 ret = tap ? tun_vnet_ioctl_sethash(&tap->vnet_hash, true, argp) : -EBADFD;
2c592c9b450ea4 drivers/net/tap.c Akihiko Odaki 2025-03-06 1054 rtnl_unlock();
2c592c9b450ea4 drivers/net/tap.c Akihiko Odaki 2025-03-06 1055
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 @1056 case SIOCGIFHWADDR:
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1057 rtnl_lock();
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 1058 tap = tap_get_tap_dev(q);
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 1059 if (!tap) {
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1060 rtnl_unlock();
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1061 return -ENOLINK;
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1062 }
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1063 ret = 0;
3b23a32a63219f drivers/net/tap.c Cong Wang 2021-02-11 1064 dev_get_mac_address(&sa, dev_net(tap->dev), tap->dev->name);
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 1065 if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
3b23a32a63219f drivers/net/tap.c Cong Wang 2021-02-11 1066 copy_to_user(&ifr->ifr_hwaddr, &sa, sizeof(sa)))
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1067 ret = -EFAULT;
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 1068 tap_put_tap_dev(tap);
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1069 rtnl_unlock();
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1070 return ret;
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1071
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1072 case SIOCSIFHWADDR:
7f460d30c8e130 drivers/net/macvtap.c Justin Cormack 2015-05-13 1073 if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
7f460d30c8e130 drivers/net/macvtap.c Justin Cormack 2015-05-13 1074 return -EFAULT;
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1075 rtnl_lock();
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 1076 tap = tap_get_tap_dev(q);
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 1077 if (!tap) {
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1078 rtnl_unlock();
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1079 return -ENOLINK;
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1080 }
3b23a32a63219f drivers/net/tap.c Cong Wang 2021-02-11 1081 ret = dev_set_mac_address_user(tap->dev, &sa, NULL);
6fe3faf86757eb drivers/net/tap.c Sainath Grandhi 2017-02-10 1082 tap_put_tap_dev(tap);
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1083 rtnl_unlock();
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1084 return ret;
b5082083392224 drivers/net/macvtap.c Justin Cormack 2015-05-11 1085
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1086 default:
69113cb5de68da drivers/net/tap.c Akihiko Odaki 2025-02-07 1087 return tun_vnet_ioctl(&q->vnet_hdr_sz, &q->flags, cmd, sp);
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1088 }
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1089 }
20d29d7a916a47 drivers/net/macvtap.c Arnd Bergmann 2010-01-30 1090 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v8 4/6 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 | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+)
tools/testing/selftests/net/tun.c +74 -0
@@ -159,4 +159,78 @@ 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, setvnethdrsz) +{ +ASSERT_EQ (-1 ,ioctl (self->fd,TUNSETVNETHDRSZ )); +EXPECT_EQ (EBADFD , errno); +} + +TEST_F(tun_deleted, getvnetle) +{ +ASSERT_EQ (-1 ,ioctl (self->fd,TUNGETVNETLE )); +EXPECT_EQ (EBADFD , errno); +} + +TEST_F(tun_deleted, setvnetle) +{ +ASSERT_EQ (-1 ,ioctl (self->fd,TUNSETVNETLE )); +EXPECT_EQ (EBADFD , errno); +} + +TEST_F(tun_deleted, getvnetbe) +{ +ASSERT_EQ (-1 ,ioctl (self->fd,TUNGETVNETBE )); +EXPECT_EQ (EBADFD , errno); +} + +TEST_F(tun_deleted, setvnetbe) +{ +ASSERT_EQ (-1 ,ioctl (self->fd,TUNSETVNETBE )); +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_F(tun_deleted, setvnethash) +{ +ASSERT_EQ (-1 ,ioctl (self->fd,TUNSETVNETHASH )); +EXPECT_EQ (EBADFD , errno); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.48.1
PATCH v8 5/6 selftest: tun: Add tests for virtio-net hashing
In reply to Akihiko OdakiThe added tests confirm tun can perform RSS and hash reporting, and reject invalid configurations for them.
--- tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/tun.c | 584 ++++++++++++++++++++++++++++++++++- 2 files changed, 576 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 +575 -9
643 lines not on this page · raw message
Signature · 1 line
2.48.1
PATCH v8 6/6 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 | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-)
drivers/vhost/net.c +29 -20
@@ -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,9 +1098,11 @@ 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 +struct virtio_net_hdr_v1_hash hdr = { + .hdr = { + .flags =0 , + .gso_type =VIRTIO_NET_HDR_GSO_NONE + } };size_t total_len =0 ;int err, mergeable;
@@ -1110,7 +1113,6 @@ static void handle_rx(struct vhost_net *net)bool set_num_buffers;struct socket *sock;struct iov_iter fixup; -__virtio16 num_buffers;int recv_pkts =0 ;mutex_lock_nested (&vq->mutex,VHOST_NET_VQ_RX );
@@ -1191,30 +1193,30 @@ static void handle_rx(struct vhost_net *net)vhost_discard_vq_desc (vq, headcount);continue ; } + hdr.hdr.num_buffers =cpu_to_vhost16 (vq, headcount);/* 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)) { +if (copy_to_iter (&hdr, vhost_hlen, + &fixup) != vhost_hlen) {vq_err (vq,"Unable to write vnet_hdr " "at addr %p\n" , vq->iov->iov_base); goto out; } - }else { + }else if (likely (set_num_buffers)) {/* Header came from socket; we'll need to patch * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF */ -iov_iter_advance (&fixup,sizeof (hdr)); +iov_iter_advance (&fixup, offsetof(struct virtio_net_hdr_v1 , num_buffers)); + +if (copy_to_iter (&hdr.hdr.num_buffers,sizeof (hdr.hdr.num_buffers), + &fixup) !=sizeof (hdr.hdr.num_buffers)) { +vq_err (vq,"Failed num_buffers write" ); +vhost_discard_vq_desc (vq, headcount); + goto out; + } }/* TODO: Should check and handle checksum. */ - 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; - } nvq->done_idx += headcount;if (nvq->done_idx >VHOST_NET_BATCH )vhost_net_signal_used (nvq);
@@ -1607,10 +1609,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 +1696,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
QE tested this series of patches with virtio-net regression tests, everything works fine.
Quoted 145 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.Signed-off-by Akihiko Odaki <[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 unnecessaryrtnl_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 sethash_reporttoVIRTIO_NET_HASH_REPORT_NONEforVHOST_NET_F_VIRTIO_NET_HDR.
- s/4/sizeof(u32)/ in patch "virtio_net: Add functions for hashing".
- Addedtap_skb_cbtype.
- 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: IntroduceSKB_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 withCONFIG_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 havequeue_mappingset 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:
- Movedtun_vnet_hash_exttoif_tun.h.
- Renamedvirtio_net_toeplitz()tovirtio_net_toeplitz_calc().
- Replacedhtons()withcpu_to_be16().
- Changedvirtio_net_hash_rss()to return void.
- Reordered variable declarations invirtio_net_hash_rss().
- Removedvirtio_net_hdr_v1_hash_from_skb().
- Updated messages of "tap: Pad virtio header with zero" and
"tun: Pad virtio header with zero".
- Fixedvnet_hashallocation size.
- Ensured to freevnet_hashwhen destructingtun_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 structtun_structto keep it minimal.
- Added kernel-doc.
- Changed to allow calling TUNGETVNETHASHCAP before TUNSETIFF.
- Initializednum_bufferswith 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 (6):
virtio_net: Add functions for hashing
net:flow_dissector: Exportflow_keys_dissector_symmetric
tun: Introduce virtio-net hash feature
selftest: tun: Test vnet ioctls without device
selftest: tun: Add tests for virtio-net hashing vhost/net: SupportVIRTIO_NET_F_HASH_REPORTDocumentation/networking/tuntap.rst | 7 + drivers/net/Kconfig | 1 + drivers/net/tap.c | 67 +++- drivers/net/tun.c | 98 +++++- drivers/net/tun_vnet.h | 159 ++++++++- drivers/vhost/net.c | 49 +-- include/linux/if_tap.h | 2 + include/linux/skbuff.h | 3 + include/linux/virtio_net.h | 188 ++++++++++ include/net/flow_dissector.h | 1 + include/uapi/linux/if_tun.h | 75 ++++ net/core/flow_dissector.c | 3 +- net/core/skbuff.c | 4 + tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/tun.c | 656 ++++++++++++++++++++++++++++++++++- 15 files changed, 1254 insertions(+), 61 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:25fd4f99d4236a05a5ef16ab79f3e85ee57e21ccBest regards,
--
Akihiko Odaki <[email protected]>