Skip to contentSkip to conversation

tun: Introduce virtio-net hashing feature

PATCH Superseded Akihiko Odaki 4 participants 54 messages Mar 7, 2025 – Mar 29, 2025 Page 1 of 2

Subsystems (9)

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v9 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 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 (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                    |  68 +++-
 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, 1255 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]>
#1 lore raw

PATCH v9 1/6 virtio_net: Add functions for hashing

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

---
 include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 188 insertions(+)
include/linux/virtio_net.h +188 -0
index 02a9f4dc594d02372a6c1850cd600eff9d000d8d..426f33b4b82440d61b2af9fdc4c0b0d4c571b2c5 100644
@@ -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
#2 lore raw

Re: [PATCH net-next v9 1/6] virtio_net: Add functions for hashing

Quoted 24 lines · Akihiko Odaki

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

---
 include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 188 insertions(+)
include/linux/virtio_net.h +6 -0
index 02a9f4dc594d02372a6c1850cd600eff9d000d8d..426f33b4b82440d61b2af9fdc4c0b0d4c571b2c5 100644
@@ -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 {

include/linux/virtio_net.h line 27

Quoted 10 lines
include/linux/virtio_net.h +10 -0
+       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)

Let's explain why

#define VIRTIO_NET_HASH_REPORT_IPv6_EX         7
#define VIRTIO_NET_HASH_REPORT_TCPv6_EX        8
#define VIRTIO_NET_HASH_REPORT_UDPv6_EX        9

are missed here.

And explain how we could maintain migration compatibility

1) Does those three work for userspace datapath in Qemu? If yes, migration will be broken.
2) once we support those three in the future. For example, is the qemu expected to probe this via TUNGETVNETHASHCAP in the destination and fail the migration?

Thanks

Quoted 179 lines
include/linux/virtio_net.h +172 -1
+
+#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) {
 
--

2.48.1

#3 lore raw

Re: [PATCH net-next v9 1/6] virtio_net: Add functions for hashing

Quoted 38 lines · Jason Wang
Quoted 24 lines · Akihiko Odaki

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

---
  include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 188 insertions(+)
include/linux/virtio_net.h +6 -0
index 02a9f4dc594d02372a6c1850cd600eff9d000d8d..426f33b4b82440d61b2af9fdc4c0b0d4c571b2c5 100644
@@ -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 {

include/linux/virtio_net.h line 27

Quoted 10 lines
include/linux/virtio_net.h +10 -0
+       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)

Let's explain why

Quoted 5 lines
#define VIRTIO_NET_HASH_REPORT_IPv6_EX         7
#define VIRTIO_NET_HASH_REPORT_TCPv6_EX        8
#define VIRTIO_NET_HASH_REPORT_UDPv6_EX        9

are missed here.

Because they require parsing IPv6 options and I'm not sure how many we need to parse. QEMU's eBPF program has a hard-coded limit of 30 options; it has some explanation for this limit, but it does not seem definitive either:
https://gitlab.com/qemu-project/qemu/-/commit/f3fa412de28ae3cb31d38811d30a77e4e20456cc#6ec48fc8af2f802e92f5127425e845c4c213ff60_0_165

In this patch series, I add an ioctl to query capability instead; it allows me leaving those hash types unimplemented and is crucial to assure extensibility for future additions of hash types anyway. Anyone who find these hash types useful can implement in the future.

Quoted 5 lines

And explain how we could maintain migration compatibility

1) Does those three work for userspace datapath in Qemu? If yes, migration will be broken.

They work for userspace datapath so my RFC patch series for QEMU uses TUNGETVNETHASHCAP to prevent breaking migration:
https://patchew.org/QEMU/[email protected]/

This patch series first adds configuration options for users to choose hash types. QEMU then automatically picks one implementation from the following (the earlier one is the more preferred):
1) The hash capability of vhost hardware
2) The hash capability I'm proposing here
3) The eBPF program
4) The pure userspace implementation

This decision depends on the following:
- The required hash types; supported ones are queried for 1) and 2)
- Whether vhost is enabled or not and what vhost backend is used
- Whether hash reporting is enabled; 3) is incompatible with this

The network device will not be realized if no implementation satisfies the requirements.

Quoted 3 lines

2) once we support those three in the future. For example, is the qemu expected to probe this via TUNGETVNETHASHCAP in the destination and fail the migration?

QEMU is expected to use TUNGETVNETHASHCAP, but it can selectively enable hash types with TUNSETVNETHASH to keep migration working.

In summary, this patch series provides a sufficient facility for the userspace to make extensibility and migration compatible; TUNGETVNETHASHCAP exposes all of the kernel capabilities and TUNSETVNETHASH allows the userspace to limit them.

Regards,
Akihiko Odaki

Quoted 185 lines

Thanks

Quoted 179 lines
include/linux/virtio_net.h +172 -1
+
+#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) {
 
--

2.48.1

#4 lore raw

Re: [PATCH net-next v9 1/6] virtio_net: Add functions for hashing

Quoted 44 lines · Akihiko Odaki
Quoted 42 lines · Jason Wang
Quoted 24 lines · Akihiko Odaki

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

---
  include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 188 insertions(+)
include/linux/virtio_net.h +6 -0
index 02a9f4dc594d02372a6c1850cd600eff9d000d8d..426f33b4b82440d61b2af9fdc4c0b0d4c571b2c5 100644
@@ -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 {

include/linux/virtio_net.h line 27

Quoted 10 lines
include/linux/virtio_net.h +10 -0
+       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)

Let's explain why

#define VIRTIO_NET_HASH_REPORT_IPv6_EX         7
#define VIRTIO_NET_HASH_REPORT_TCPv6_EX        8
#define VIRTIO_NET_HASH_REPORT_UDPv6_EX        9
Quoted 8 lines
Quoted 1 line

are missed here.

Because they require parsing IPv6 options and I'm not sure how many we need to parse. QEMU's eBPF program has a hard-coded limit of 30 options; it has some explanation for this limit, but it does not seem definitive either:
https://gitlab.com/qemu-project/qemu/-/commit/f3fa412de28ae3cb31d38811d30a77e4e20456cc#6ec48fc8af2f802e92f5127425e845c4c213ff60_0_165

How about the usersapce datapath RSS in Qemu? (We probably don't need to align with eBPF RSS as it's just a reference implementation)

Quoted 4 lines

In this patch series, I add an ioctl to query capability instead; it allows me leaving those hash types unimplemented and is crucial to assure extensibility for future additions of hash types anyway. Anyone who find these hash types useful can implement in the future.

Yes, but we need to make sure no userspace visible behaviour changes after migration.

Quoted 11 lines
Quoted 6 lines

And explain how we could maintain migration compatibility

1) Does those three work for userspace datapath in Qemu? If yes, migration will be broken.

They work for userspace datapath so my RFC patch series for QEMU uses TUNGETVNETHASHCAP to prevent breaking migration:
https://patchew.org/QEMU/[email protected]/

Ok, let's mention this in the cover letter. Another interesting thing is the migration from 10.0 to 9.0.

Quoted 8 lines

This patch series first adds configuration options for users to choose hash types. QEMU then automatically picks one implementation from the following (the earlier one is the more preferred):
1) The hash capability of vhost hardware
2) The hash capability I'm proposing here
3) The eBPF program
4) The pure userspace implementation

Quoted 7 lines

This decision depends on the following:
- The required hash types; supported ones are queried for 1) and 2)
- Whether vhost is enabled or not and what vhost backend is used
- Whether hash reporting is enabled; 3) is incompatible with this

The network device will not be realized if no implementation satisfies the requirements.

This makes sense, let's add this in the cover letter.

Quoted 8 lines
Quoted 4 lines

2) once we support those three in the future. For example, is the qemu expected to probe this via TUNGETVNETHASHCAP in the destination and fail the migration?

QEMU is expected to use TUNGETVNETHASHCAP, but it can selectively enable hash types with TUNSETVNETHASH to keep migration working.

Quoted 7 lines

In summary, this patch series provides a sufficient facility for the userspace to make extensibility and migration compatible; TUNGETVNETHASHCAP exposes all of the kernel capabilities and TUNSETVNETHASH allows the userspace to limit them.

Regards,
Akihiko Odaki

Fine.

Thanks

Quoted 187 lines
Quoted 186 lines

Thanks

Quoted 179 lines
include/linux/virtio_net.h +172 -1
+
+#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) {
 
--

2.48.1

#5 lore raw

Re: [PATCH net-next v9 1/6] virtio_net: Add functions for hashing

Quoted 54 lines · Jason Wang
Quoted 44 lines · Akihiko Odaki
Quoted 42 lines · Jason Wang
Quoted 24 lines · Akihiko Odaki

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

---
   include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++
   1 file changed, 188 insertions(+)
include/linux/virtio_net.h +6 -0
index 02a9f4dc594d02372a6c1850cd600eff9d000d8d..426f33b4b82440d61b2af9fdc4c0b0d4c571b2c5 100644
@@ -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 {

include/linux/virtio_net.h line 27

Quoted 10 lines
include/linux/virtio_net.h +10 -0
+       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)

Let's explain why

#define VIRTIO_NET_HASH_REPORT_IPv6_EX         7
#define VIRTIO_NET_HASH_REPORT_TCPv6_EX        8
#define VIRTIO_NET_HASH_REPORT_UDPv6_EX        9
Quoted 8 lines
Quoted 1 line

are missed here.

Because they require parsing IPv6 options and I'm not sure how many we need to parse. QEMU's eBPF program has a hard-coded limit of 30 options; it has some explanation for this limit, but it does not seem definitive either:
https://gitlab.com/qemu-project/qemu/-/commit/f3fa412de28ae3cb31d38811d30a77e4e20456cc#6ec48fc8af2f802e92f5127425e845c4c213ff60_0_165

Quoted 2 lines

How about the usersapce datapath RSS in Qemu? (We probably don't need to align with eBPF RSS as it's just a reference implementation)

The userspace datapath RSS has no limit.

The reference implementation is the userspace datapath. The eBPF program
is intended to bring real performance benefit to Windows guests in
contrary.

The userspace implementation does its best to provide defined RSS capabilities but may not be performant. Parsing all IPv6 options have a performance implication, but it is fine because it is not intended to be performant in the first place.

The performance problem is inherent to the userspace implementation, which adds an extra overhead to the datapath. The eBPF program on the other hand does not incur such overhead because it replaces the existing steering algorithm (automq) instead of adding another layer. Hence the eBPF program can be practical.

That said, it is not that important to align with the userspace and eBPF RSS in QEMU because they are still experimental anyway; the eBPF RSS has potential to become a practical implementation but it is still in development. The libvirt integration for the eBPF RSS is still not complete, and we occasionally add fixes for RSS and hash reporting without backporting to the stable branch.

I'm adding interfaces to negotiate hash types rather for the future extensibility. The specification may gain more hash types in the future and other vhost backends may have a different set of hash types supported. Figuring out how to deal with different sets of supported hash typs is essential for both the kernel and QEMU.

Quoted 8 lines
Quoted 5 lines

In this patch series, I add an ioctl to query capability instead; it allows me leaving those hash types unimplemented and is crucial to assure extensibility for future additions of hash types anyway. Anyone who find these hash types useful can implement in the future.

Yes, but we need to make sure no userspace visible behaviour changes after migration.

Indeed, the goal is to make extensibility and migration compatible.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

And explain how we could maintain migration compatibility

1) Does those three work for userspace datapath in Qemu? If yes, migration will be broken.

Quoted 7 lines
Quoted 4 lines

They work for userspace datapath so my RFC patch series for QEMU uses TUNGETVNETHASHCAP to prevent breaking migration:
https://patchew.org/QEMU/[email protected]/

Ok, let's mention this in the cover letter. Another interesting thing is the migration from 10.0 to 9.0.

The patch series is already mentioned in the cover letter. A description of the intended use case of TUNGETVNETHASHCAP will be a good addition. I'll add it to this patch so that it will be kept in tree after it gets merged.

Migration between two different QEMU versions should be handled with versioned machine types.

When a machine created in 9.0 is being migrated to 10.0, the machine must set the hash type properties to match with the hash types supported by the existing implementations, which means it sets the property for VIRTIO_NET_HASH_REPORT_IPv6_EX to true, for example. Because this hash type is currently not included in TUNGETVNETHASHCAP, the machine will keep using the implementation used previously. The machine can be also migrated back to 9.0 again.

A machine type with version 10.0 cannot be migrated to 9.0 by design so there is no new problem.

Quoted 14 lines
Quoted 14 lines

This patch series first adds configuration options for users to choose hash types. QEMU then automatically picks one implementation from the following (the earlier one is the more preferred):
1) The hash capability of vhost hardware
2) The hash capability I'm proposing here
3) The eBPF program
4) The pure userspace implementation

This decision depends on the following:
- The required hash types; supported ones are queried for 1) and 2)
- Whether vhost is enabled or not and what vhost backend is used
- Whether hash reporting is enabled; 3) is incompatible with this

Quoted 4 lines
Quoted 2 lines

The network device will not be realized if no implementation satisfies the requirements.

This makes sense, let's add this in the cover letter.

I'll add it to the QEMU patch as it's more about details of QEMU.
The message of this patch will explain how TUNGETVNETHASHCAP and TUNSETVNETHASH makes extensibility and migrattion compatible in general.

Regards,
Akihiko Odaki

Quoted 209 lines
Quoted 9 lines
Quoted 5 lines

2) once we support those three in the future. For example, is the qemu expected to probe this via TUNGETVNETHASHCAP in the destination and fail the migration?

QEMU is expected to use TUNGETVNETHASHCAP, but it can selectively enable hash types with TUNSETVNETHASH to keep migration working.

Quoted 7 lines

In summary, this patch series provides a sufficient facility for the userspace to make extensibility and migration compatible; TUNGETVNETHASHCAP exposes all of the kernel capabilities and TUNSETVNETHASH allows the userspace to limit them.

Regards,
Akihiko Odaki

Fine.

Thanks

Quoted 187 lines
Quoted 186 lines

Thanks

Quoted 179 lines
include/linux/virtio_net.h +172 -1
+
+#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) {
 
--

2.48.1

#6 lore raw

Re: [PATCH net-next v9 1/6] virtio_net: Add functions for hashing

Quoted 95 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 44 lines · Akihiko Odaki
Quoted 42 lines · Jason Wang
Quoted 24 lines · Akihiko Odaki

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

---
   include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++
   1 file changed, 188 insertions(+)
include/linux/virtio_net.h +6 -0
index 02a9f4dc594d02372a6c1850cd600eff9d000d8d..426f33b4b82440d61b2af9fdc4c0b0d4c571b2c5 100644
@@ -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 {

include/linux/virtio_net.h line 27

Quoted 10 lines
include/linux/virtio_net.h +10 -0
+       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)

Let's explain why

#define VIRTIO_NET_HASH_REPORT_IPv6_EX         7
#define VIRTIO_NET_HASH_REPORT_TCPv6_EX        8
#define VIRTIO_NET_HASH_REPORT_UDPv6_EX        9
Quoted 8 lines
Quoted 1 line

are missed here.

Because they require parsing IPv6 options and I'm not sure how many we need to parse. QEMU's eBPF program has a hard-coded limit of 30 options; it has some explanation for this limit, but it does not seem definitive either:
https://gitlab.com/qemu-project/qemu/-/commit/f3fa412de28ae3cb31d38811d30a77e4e20456cc#6ec48fc8af2f802e92f5127425e845c4c213ff60_0_165

Quoted 2 lines

How about the usersapce datapath RSS in Qemu? (We probably don't need to align with eBPF RSS as it's just a reference implementation)

The userspace datapath RSS has no limit.

The reference implementation is the userspace datapath. The eBPF program
is intended to bring real performance benefit to Windows guests in
contrary.

The userspace implementation does its best to provide defined RSS capabilities but may not be performant. Parsing all IPv6 options have a performance implication, but it is fine because it is not intended to be performant in the first place.

The performance problem is inherent to the userspace implementation, which adds an extra overhead to the datapath. The eBPF program on the other hand does not incur such overhead because it replaces the existing steering algorithm (automq) instead of adding another layer. Hence the eBPF program can be practical.

That said, it is not that important to align with the userspace and eBPF RSS in QEMU because they are still experimental anyway; the eBPF RSS has potential to become a practical implementation but it is still in development. The libvirt integration for the eBPF RSS is still not complete, and we occasionally add fixes for RSS and hash reporting without backporting to the stable branch.

I'm adding interfaces to negotiate hash types rather for the future extensibility. The specification may gain more hash types in the future and other vhost backends may have a different set of hash types supported. Figuring out how to deal with different sets of supported hash typs is essential for both the kernel and QEMU.

Quoted 6 lines
Quoted 5 lines

In this patch series, I add an ioctl to query capability instead; it allows me leaving those hash types unimplemented and is crucial to assure extensibility for future additions of hash types anyway. Anyone who find these hash types useful can implement in the future.

Quoted 4 lines
Quoted 2 lines

Yes, but we need to make sure no userspace visible behaviour changes after migration.

Indeed, the goal is to make extensibility and migration compatible.

So I see this part:

+ uint32_t supported_hash_types = n->rss_data.supported_hash_types;
+ uint32_t peer_hash_types = n->rss_data.peer_hash_types;
+ bool use_own_hash =
+ (supported_hash_types & VIRTIO_NET_RSS_SUPPORTED_HASHES) ==
+ supported_hash_types;
+ bool use_peer_hash =
+ n->rss_data.peer_hash_available &&
+ (supported_hash_types & peer_hash_types) == supported_hash_types;

It looks like it would be a challenge to support vhost-user in the future if vhost-user supports hash feature others than source?

Quoted 33 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

And explain how we could maintain migration compatibility

1) Does those three work for userspace datapath in Qemu? If yes, migration will be broken.

Quoted 7 lines
Quoted 4 lines

They work for userspace datapath so my RFC patch series for QEMU uses TUNGETVNETHASHCAP to prevent breaking migration:
https://patchew.org/QEMU/[email protected]/

Ok, let's mention this in the cover letter. Another interesting thing is the migration from 10.0 to 9.0.

The patch series is already mentioned in the cover letter. A description of the intended use case of TUNGETVNETHASHCAP will be a good addition. I'll add it to this patch so that it will be kept in tree after it gets merged.

Migration between two different QEMU versions should be handled with versioned machine types.

When a machine created in 9.0 is being migrated to 10.0, the machine must set the hash type properties to match with the hash types supported by the existing implementations, which means it sets the property for VIRTIO_NET_HASH_REPORT_IPv6_EX to true, for example. Because this hash type is currently not included in TUNGETVNETHASHCAP, the machine will keep using the implementation used previously. The machine can be also migrated back to 9.0 again.

Quoted 2 lines

A machine type with version 10.0 cannot be migrated to 9.0 by design so there is no new problem.

I meant migrate qemu 11.0 with machine type 10.0 to qemu 10.0 with machine 10.0 etc.

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines

This patch series first adds configuration options for users to choose hash types. QEMU then automatically picks one implementation from the following (the earlier one is the more preferred):
1) The hash capability of vhost hardware
2) The hash capability I'm proposing here
3) The eBPF program
4) The pure userspace implementation

This decision depends on the following:
- The required hash types; supported ones are queried for 1) and 2)
- Whether vhost is enabled or not and what vhost backend is used
- Whether hash reporting is enabled; 3) is incompatible with this

The network device will not be realized if no implementation satisfies the requirements.

Quoted 30 lines
Quoted 1 line

This makes sense, let's add this in the cover letter.

I'll add it to the QEMU patch as it's more about details of QEMU. The message of this patch will explain how TUNGETVNETHASHCAP and TUNSETVNETHASH makes extensibility and migrattion compatible in general.

Regards,
Akihiko Odaki

Quoted 21 lines
Quoted 9 lines
Quoted 5 lines

2) once we support those three in the future. For example, is the qemu expected to probe this via TUNGETVNETHASHCAP in the destination and fail the migration?

QEMU is expected to use TUNGETVNETHASHCAP, but it can selectively enable hash types with TUNSETVNETHASH to keep migration working.

Quoted 7 lines

In summary, this patch series provides a sufficient facility for the userspace to make extensibility and migration compatible; TUNGETVNETHASHCAP exposes all of the kernel capabilities and TUNSETVNETHASH allows the userspace to limit them.

Regards,
Akihiko Odaki

Fine.

Thanks

Thanks

#7 lore raw

Re: [PATCH net-next v9 1/6] virtio_net: Add functions for hashing

Quoted 112 lines · Jason Wang
Quoted 95 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 44 lines · Akihiko Odaki
Quoted 42 lines · Jason Wang
Quoted 24 lines · Akihiko Odaki

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

---
    include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++
    1 file changed, 188 insertions(+)
include/linux/virtio_net.h +6 -0
index 02a9f4dc594d02372a6c1850cd600eff9d000d8d..426f33b4b82440d61b2af9fdc4c0b0d4c571b2c5 100644
@@ -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 {

include/linux/virtio_net.h line 27

Quoted 10 lines
include/linux/virtio_net.h +10 -0
+       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)

Let's explain why

#define VIRTIO_NET_HASH_REPORT_IPv6_EX         7
#define VIRTIO_NET_HASH_REPORT_TCPv6_EX        8
#define VIRTIO_NET_HASH_REPORT_UDPv6_EX        9
Quoted 8 lines
Quoted 1 line

are missed here.

Because they require parsing IPv6 options and I'm not sure how many we need to parse. QEMU's eBPF program has a hard-coded limit of 30 options; it has some explanation for this limit, but it does not seem definitive either:
https://gitlab.com/qemu-project/qemu/-/commit/f3fa412de28ae3cb31d38811d30a77e4e20456cc#6ec48fc8af2f802e92f5127425e845c4c213ff60_0_165

Quoted 2 lines

How about the usersapce datapath RSS in Qemu? (We probably don't need to align with eBPF RSS as it's just a reference implementation)

The userspace datapath RSS has no limit.

The reference implementation is the userspace datapath. The eBPF program
is intended to bring real performance benefit to Windows guests in
contrary.

The userspace implementation does its best to provide defined RSS capabilities but may not be performant. Parsing all IPv6 options have a performance implication, but it is fine because it is not intended to be performant in the first place.

The performance problem is inherent to the userspace implementation, which adds an extra overhead to the datapath. The eBPF program on the other hand does not incur such overhead because it replaces the existing steering algorithm (automq) instead of adding another layer. Hence the eBPF program can be practical.

That said, it is not that important to align with the userspace and eBPF RSS in QEMU because they are still experimental anyway; the eBPF RSS has potential to become a practical implementation but it is still in development. The libvirt integration for the eBPF RSS is still not complete, and we occasionally add fixes for RSS and hash reporting without backporting to the stable branch.

I'm adding interfaces to negotiate hash types rather for the future extensibility. The specification may gain more hash types in the future and other vhost backends may have a different set of hash types supported. Figuring out how to deal with different sets of supported hash typs is essential for both the kernel and QEMU.

Quoted 6 lines
Quoted 5 lines

In this patch series, I add an ioctl to query capability instead; it allows me leaving those hash types unimplemented and is crucial to assure extensibility for future additions of hash types anyway. Anyone who find these hash types useful can implement in the future.

Quoted 4 lines
Quoted 2 lines

Yes, but we need to make sure no userspace visible behaviour changes after migration.

Indeed, the goal is to make extensibility and migration compatible.

So I see this part:

+ uint32_t supported_hash_types = n->rss_data.supported_hash_types;
+ uint32_t peer_hash_types = n->rss_data.peer_hash_types;
+ bool use_own_hash =
+ (supported_hash_types & VIRTIO_NET_RSS_SUPPORTED_HASHES) ==
+ supported_hash_types;
+ bool use_peer_hash =
+ n->rss_data.peer_hash_available &&
+ (supported_hash_types & peer_hash_types) == supported_hash_types;

Quoted 2 lines

It looks like it would be a challenge to support vhost-user in the future if vhost-user supports hash feature others than source?

The vhost-user backend will need to retrieve the supported hash types with VHOST_USER_GET_CONFIG as the vhost-vdpa backend does with VHOST_VDPA_GET_CONFIG.

Quoted 34 lines
Quoted 34 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

And explain how we could maintain migration compatibility

1) Does those three work for userspace datapath in Qemu? If yes, migration will be broken.

Quoted 7 lines
Quoted 4 lines

They work for userspace datapath so my RFC patch series for QEMU uses TUNGETVNETHASHCAP to prevent breaking migration:
https://patchew.org/QEMU/[email protected]/

Ok, let's mention this in the cover letter. Another interesting thing is the migration from 10.0 to 9.0.

The patch series is already mentioned in the cover letter. A description of the intended use case of TUNGETVNETHASHCAP will be a good addition. I'll add it to this patch so that it will be kept in tree after it gets merged.

Migration between two different QEMU versions should be handled with versioned machine types.

When a machine created in 9.0 is being migrated to 10.0, the machine must set the hash type properties to match with the hash types supported by the existing implementations, which means it sets the property for VIRTIO_NET_HASH_REPORT_IPv6_EX to true, for example. Because this hash type is currently not included in TUNGETVNETHASHCAP, the machine will keep using the implementation used previously. The machine can be also migrated back to 9.0 again.

Quoted 5 lines
Quoted 2 lines

A machine type with version 10.0 cannot be migrated to 9.0 by design so there is no new problem.

I meant migrate qemu 11.0 with machine type 10.0 to qemu 10.0 with machine 10.0 etc.

Let's assume QEMU 10.0 will support this new ioctl while QEMU 9.0 doesn't.

The description in my previous email was wrong. Checking the patch series again, I found I bumped the version number of vmstate_virtio_net_rss. So migrating QEMU 10.0 with machine type 9.0 to QEMU 9.0 will result in an error.

We can remove this error by introducing a compatibility property, but I don't think it's worth. As I noted in the previous email, the RSS feature is still in development and I don't think we need to support migrating to older QEMU versions. It gives an error instead of silently breaking a migrated VM at least.

Regards,
Akihiko Odaki

Quoted 52 lines
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines

This patch series first adds configuration options for users to choose hash types. QEMU then automatically picks one implementation from the following (the earlier one is the more preferred):
1) The hash capability of vhost hardware
2) The hash capability I'm proposing here
3) The eBPF program
4) The pure userspace implementation

This decision depends on the following:
- The required hash types; supported ones are queried for 1) and 2)
- Whether vhost is enabled or not and what vhost backend is used
- Whether hash reporting is enabled; 3) is incompatible with this

The network device will not be realized if no implementation satisfies the requirements.

Quoted 30 lines
Quoted 1 line

This makes sense, let's add this in the cover letter.

I'll add it to the QEMU patch as it's more about details of QEMU. The message of this patch will explain how TUNGETVNETHASHCAP and TUNSETVNETHASH makes extensibility and migrattion compatible in general.

Regards,
Akihiko Odaki

Quoted 21 lines
Quoted 9 lines
Quoted 5 lines

2) once we support those three in the future. For example, is the qemu expected to probe this via TUNGETVNETHASHCAP in the destination and fail the migration?

QEMU is expected to use TUNGETVNETHASHCAP, but it can selectively enable hash types with TUNSETVNETHASH to keep migration working.

Quoted 7 lines

In summary, this patch series provides a sufficient facility for the userspace to make extensibility and migration compatible; TUNGETVNETHASHCAP exposes all of the kernel capabilities and TUNSETVNETHASH allows the userspace to limit them.

Regards,
Akihiko Odaki

Fine.

Thanks

Thanks

#8 lore raw

PATCH v9 2/6 net: flow_dissector: Export flow_keys_dissector_symmetric

In reply to Akihiko Odaki

flow_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
index ced79dc8e8560e25a4dd567a04f5710b53452b45..d01c1ec77b7d21b17c14b05c47e3cdda39651bec 100644
@@ -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
index 9cd8de6bebb543c3d672f576e03b29aa86b9d34a..32c7ee31330cf52df05d7a23b3e50d1a1bed9908 100644
@@ -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
#9 lore raw

PATCH v9 3/6 tun: Introduce virtio-net hash feature

In reply to Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
 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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -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
index 1fd5acdc73c6af0e1a861867039c3624fc618e25..aecfd244dd83585fea2c5b815dcd787c58166c28 100644
@@ -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 +65 -3

117 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
index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..7334c46a3f101675a0d4e5a036987cfe18842f9f 100644
@@ -31,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
 #define MAX_TAP_QUEUES 256
 
 struct 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
index bb2b751d274acff931281a72e8b4b0c699b4e8af..cdd793f1c360ad5f63fcc4cbf67d845f5e2ccf6f 100644
@@ -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
index 7b03b64fdcb276f68ce881d1d8da8e4c6b897efc..aa2a091b649f0c9d6e0196f34f345ba78b5498fb 100644
@@ -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
maintainer
#10 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 29 lines · Akihiko Odaki

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.

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.

Quoted 11 lines
---
 Documentation/networking/tuntap.rst |   7 ++
 drivers/net/Kconfig                 |   1 +
 drivers/net/tap.c                   |  68 ++++++++++++++-
 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, 386 insertions(+), 31 deletions(-)

This is arguably still doing too much in a single patch.

Can you split tap from tun? Move ioctl contrl operations out to their own patch?

Quoted 56 lines
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -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
index 1fd5acdc73c6af0e1a861867039c3624fc618e25..aecfd244dd83585fea2c5b815dcd787c58166c28 100644
@@ -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 +10 -0
index d4ece538f1b23789ca60caa6232690e4d0a4d14a..9428b63ec27e7f92e78a78afcb5e24383862c00d 100644
@@ -49,6 +49,10 @@ struct major_info {
 	struct list_head next;
 };
 
+struct tap_skb_cb {
+	struct virtio_net_hash hash;
+};
+
 #define GOODCOPY_LEN 128
 
 static const struct proto_ops tap_socket_ops;
@@ -179,6 +183,22 @@ static void tap_put_queue(struct tap_queue *q)
 	sock_put(&q->sk);
 }
 
+static struct tap_skb_cb *tap_skb_cb(const struct sk_buff *skb)
+{
+	BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct tap_skb_cb));
+	return (struct tap_skb_cb *)skb->cb;
+}
+

drivers/net/tap.c static void tap_put_queue(struct tap_queue *q) · line 201

Quoted 10 lines
drivers/net/tap.c +10 -0
+static struct virtio_net_hash *tap_add_hash(struct sk_buff *skb)
+{
+	return &tap_skb_cb(skb)->hash;
+}
+
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+	return &tap_skb_cb(skb)->hash;
+}
+

These two helpers do the same thing.

Quoted 23 lines
drivers/net/tap.c +6 -0
 /*
  * 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
@@ -189,6 +209,7 @@ static void tap_put_queue(struct tap_queue *q)
 static struct tap_queue *tap_get_queue(struct tap_dev *tap,
 				       struct sk_buff *skb)
 {
+	struct flow_keys_basic keys_basic;
 	struct tap_queue *queue = NULL;
 	/* Access to taps array is protected by rcu, but access to numvtaps
 	 * isn't. Below we use it to lookup a queue, but treat it as a hint
@@ -196,17 +217,47 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
 	 * racing against queue removal.
 	 */
 	int numvtaps = READ_ONCE(tap->numvtaps);
+	struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash);
 	__u32 rxq;
 
+	*tap_skb_cb(skb) = (struct tap_skb_cb) {
+		.hash = { .report = VIRTIO_NET_HASH_REPORT_NONE }
+	};
+
 	if (!numvtaps)

drivers/net/tap.c static struct tap_queue *tap_get_queue(struct tap_dev *tap, · line 237

Quoted 10 lines
drivers/net/tap.c +5 -0
 		goto out;
 
 	if (numvtaps == 1)
 		goto single;
 
+	if (vnet_hash) {
+		if ((vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+			rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash);
+			queue = rcu_dereference(tap->taps[rxq]);
+			goto out;

so tun_vnet_hash_report does not work in this case?

Quoted 267 lines
drivers/net/tap.c +34 -3
+		}
+
+		if (!skb->l4_hash && !skb->sw_hash) {
+			struct flow_keys keys;
+
+			skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+			rxq = flow_hash_from_keys(&keys);
+			keys_basic = (struct flow_keys_basic) {
+				.control = keys.control,
+				.basic = keys.basic
+			};
+		} else {
+			skb_flow_dissect_flow_keys_basic(NULL, skb, &keys_basic, NULL, 0, 0, 0,
+							 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+			rxq = skb->hash;
+		}
+	} else {
+		rxq = skb_get_hash(skb);
+	}
+
 	/* Check if we can use flow to select a queue */
-	rxq = skb_get_hash(skb);
 	if (rxq) {
+		tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
 		queue = rcu_dereference(tap->taps[rxq % numvtaps]);
 		goto out;
 	}
@@ -711,11 +762,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;
 
@@ -992,6 +1044,16 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
 		rtnl_unlock();
 		return ret;
 
+	case TUNGETVNETHASHCAP:
+		return tun_vnet_ioctl_gethashcap(argp);
+
+	case TUNSETVNETHASH:
+		rtnl_lock();
+		tap = rtnl_dereference(q->tap);
+		ret = tap ? tun_vnet_ioctl_sethash(&tap->vnet_hash, true, argp) : -EBADFD;
+		rtnl_unlock();
+		return ret;
+
 	case SIOCGIFHWADDR:
 		rtnl_lock();
 		tap = tap_get_tap_dev(q);
drivers/net/tun.c +71 -18
index d8f4d3e996a7a81d1f8b04635054081671a14f07..520013df416e93d3a50b46be9b53ae9ab410eab4 100644
@@ -209,6 +209,7 @@ struct tun_struct {
 	struct bpf_prog __rcu *xdp_prog;
 	struct tun_prog __rcu *steering_prog;
 	struct tun_prog __rcu *filter_prog;
+	struct tun_vnet_hash_container __rcu *vnet_hash;
 	struct ethtool_link_ksettings link_ksettings;
 	/* init args */
 	struct file *file;
@@ -451,20 +452,37 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
 		e->rps_rxhash = hash;
 }
 
+static struct virtio_net_hash *tun_add_hash(struct sk_buff *skb)
+{
+	return skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+	return skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
 /* 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
  * the userspace application move between processors, we may get a
  * different rxq no. here.
  */
-static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static u16 tun_automq_select_queue(struct tun_struct *tun,
+				   const struct tun_vnet_hash_container *vnet_hash,
+				   struct sk_buff *skb)
 {
+	struct flow_keys keys;
+	struct flow_keys_basic keys_basic;
 	struct tun_flow_entry *e;
 	u32 txq, numqueues;
 
 	numqueues = READ_ONCE(tun->numqueues);
 
-	txq = __skb_get_hash_symmetric(skb);
+	memset(&keys, 0, sizeof(keys));
+	skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, 0);
+
+	txq = flow_hash_from_keys(&keys);
 	e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
 	if (e) {
 		tun_flow_save_rps_rxhash(e, txq);
@@ -473,6 +491,13 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
 		txq = reciprocal_scale(txq, numqueues);
 	}
 
+	keys_basic = (struct flow_keys_basic) {
+		.control = keys.control,
+		.basic = keys.basic
+	};
+	tun_vnet_hash_report(vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+			     tun_add_hash);
+
 	return txq;
 }
 
@@ -500,10 +525,17 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
 	u16 ret;
 
 	rcu_read_lock();
-	if (rcu_dereference(tun->steering_prog))
+	if (rcu_dereference(tun->steering_prog)) {
 		ret = tun_ebpf_select_queue(tun, skb);
-	else
-		ret = tun_automq_select_queue(tun, skb);
+	} else {
+		struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tun->vnet_hash);
+
+		if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS))
+			ret = tun_vnet_rss_select_queue(READ_ONCE(tun->numqueues), vnet_hash,
+							skb, tun_add_hash);
+		else
+			ret = tun_automq_select_queue(tun, vnet_hash, skb);
+	}
 	rcu_read_unlock();
 
 	return ret;
@@ -1987,7 +2019,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);
@@ -2040,9 +2072,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;
 
@@ -2223,6 +2256,7 @@ static void tun_free_netdev(struct net_device *dev)
 	security_tun_dev_free_security(tun->security);
 	__tun_set_ebpf(tun, &tun->steering_prog, NULL);
 	__tun_set_ebpf(tun, &tun->filter_prog, NULL);
+	kfree_rcu_mightsleep(rcu_access_pointer(tun->vnet_hash));
 }
 
 static void tun_setup(struct net_device *dev)
@@ -2921,13 +2955,9 @@ static int tun_set_queue(struct file *file, struct ifreq *ifr)
 }
 
 static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
-			void __user *data)
+			int fd)
 {
 	struct bpf_prog *prog;
-	int fd;
-
-	if (copy_from_user(&fd, data, sizeof(fd)))
-		return -EFAULT;
 
 	if (fd == -1) {
 		prog = NULL;
@@ -2993,7 +3023,9 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 	int ifindex;
 	int sndbuf;
 	int ret;
+	int fd;
 	bool do_notify = false;
+	struct tun_vnet_hash_container *vnet_hash;
 
 	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
 	    (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
@@ -3020,7 +3052,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 	rtnl_lock();
 
 	tun = tun_get(tfile);
-	if (cmd == TUNSETIFF) {
+	switch (cmd) {
+	case TUNSETIFF:
 		ret = -EEXIST;
 		if (tun)
 			goto unlock;
@@ -3035,8 +3068,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 		if (copy_to_user(argp, &ifr, ifreq_len))
 			ret = -EFAULT;
 		goto unlock;
-	}
-	if (cmd == TUNSETIFINDEX) {
+
+	case TUNSETIFINDEX:
 		ret = -EPERM;
 		if (tun)
 			goto unlock;
@@ -3050,6 +3083,10 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 		ret = 0;
 		tfile->ifindex = ifindex;
 		goto unlock;
+
+	case TUNGETVNETHASHCAP:
+		ret = tun_vnet_ioctl_gethashcap(argp);
+		goto unlock;
 	}
 
 	ret = -EBADFD;
@@ -3230,11 +3267,27 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 		break;
 
 	case TUNSETSTEERINGEBPF:
-		ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
+		if (get_user(fd, (int __user *)argp)) {
+			ret = -EFAULT;
+			break;
+		}
+
+		vnet_hash = rtnl_dereference(tun->vnet_hash);
+		if (fd != -1 && vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+			ret = -EBUSY;
+			break;
+		}
+
+		ret = tun_set_ebpf(tun, &tun->steering_prog, fd);
 		break;
 
 	case TUNSETFILTEREBPF:
-		ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
+		if (get_user(fd, (int __user *)argp)) {
+			ret = -EFAULT;
+			break;
+		}
+
+		ret = tun_set_ebpf(tun, &tun->filter_prog, fd);
 		break;
 
 	case TUNSETCARRIER:
@@ -3252,8 +3305,15 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 		ret = open_related_ns(&net->ns, get_net_ns);
 		break;
 

drivers/net/tun.c static long __tun_chr_ioctl(struct file *file, unsigned int cmd, · line 3316

Quoted 10 lines
drivers/net/tun.c +8 -1
+	case TUNSETVNETHASH:
+		ret = tun_vnet_ioctl_sethash(&tun->vnet_hash,
+					     !rtnl_dereference(tun->steering_prog),
+					     argp);
+		break;
+
 	default:
-		ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags, cmd, argp);
+		ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
+				     cmd, argp);

no need to touch this

Quoted 365 lines
drivers/net/tun.c +0 -0
 		break;
 	}
 
drivers/net/tun_vnet.h +150 -9
index 58b9ac7a5fc4084c789fe94fe36b5f8631bf1fa4..8e7d51fb0b4742cef56e7c5ad778b156cc654bed 100644
@@ -6,6 +6,16 @@
 #define TUN_VNET_LE     0x80000000
 #define TUN_VNET_BE     0x40000000
 
+typedef struct virtio_net_hash *(*tun_vnet_hash_add)(struct sk_buff *);
+typedef const struct virtio_net_hash *(*tun_vnet_hash_find)(const struct sk_buff *);
+
+struct tun_vnet_hash_container {
+	struct tun_vnet_hash common;
+	struct tun_vnet_hash_rss rss;
+	u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
+	u16 rss_indirection_table[];
+};
+
 static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
 {
 	bool be = IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) &&
@@ -107,6 +117,123 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
 	}
 }
 
+static inline long tun_vnet_ioctl_gethashcap(void __user *argp)
+{
+	static const struct tun_vnet_hash cap = {
+		.flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS,
+		.types = VIRTIO_NET_SUPPORTED_HASH_TYPES
+	};
+
+	return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+					  bool can_rss, void __user *argp)
+{
+	struct tun_vnet_hash hash_buf;
+	struct tun_vnet_hash_container *hash;
+
+	if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
+		return -EFAULT;
+	argp = (struct tun_vnet_hash __user *)argp + 1;
+
+	if (hash_buf.flags & TUN_VNET_HASH_RSS) {
+		struct tun_vnet_hash_rss rss;
+		size_t indirection_table_size;
+		size_t key_size;
+		size_t size;
+
+		if (!can_rss)
+			return -EBUSY;
+
+		if (copy_from_user(&rss, argp, sizeof(rss)))
+			return -EFAULT;
+		argp = (struct tun_vnet_hash_rss __user *)argp + 1;
+
+		indirection_table_size = ((size_t)rss.indirection_table_mask + 1) * 2;
+		key_size = virtio_net_hash_key_length(hash_buf.types);
+		size = struct_size(hash, rss_indirection_table,
+				   (size_t)rss.indirection_table_mask + 1);
+
+		hash = kmalloc(size, GFP_KERNEL);
+		if (!hash)
+			return -ENOMEM;
+
+		if (copy_from_user(hash->rss_indirection_table,
+				   argp, indirection_table_size)) {
+			kfree(hash);
+			return -EFAULT;
+		}
+		argp = (u16 __user *)argp + rss.indirection_table_mask + 1;
+
+		if (copy_from_user(hash->rss_key, argp, key_size)) {
+			kfree(hash);
+			return -EFAULT;
+		}
+
+		virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+		hash->rss = rss;
+	} else {
+		hash = kmalloc(sizeof(hash->common), GFP_KERNEL);
+		if (!hash)
+			return -ENOMEM;
+	}
+
+	hash->common = hash_buf;
+	kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+	return 0;
+}
+
+static void tun_vnet_hash_report(const struct tun_vnet_hash_container *hash,
+				 struct sk_buff *skb,
+				 const struct flow_keys_basic *keys,
+				 u32 value,
+				 tun_vnet_hash_add vnet_hash_add)
+{
+	struct virtio_net_hash *report;
+
+	if (!hash || !(hash->common.flags & TUN_VNET_HASH_REPORT))
+		return;
+
+	report = vnet_hash_add(skb);
+	if (!report)
+		return;
+
+	*report = (struct virtio_net_hash) {
+		.report = virtio_net_hash_report(hash->common.types, keys),
+		.value = value
+	};
+}
+
+static u16 tun_vnet_rss_select_queue(u32 numqueues,
+				     const struct tun_vnet_hash_container *hash,
+				     struct sk_buff *skb,
+				     tun_vnet_hash_add vnet_hash_add)
+{
+	struct virtio_net_hash *report;
+	struct virtio_net_hash ret;
+	u16 txq, index;
+
+	if (!numqueues)
+		return 0;
+
+	virtio_net_hash_rss(skb, hash->common.types, hash->rss_key, &ret);
+
+	if (!ret.report)
+		return hash->rss.unclassified_queue % numqueues;
+
+	if (hash->common.flags & TUN_VNET_HASH_REPORT) {
+		report = vnet_hash_add(skb);
+		if (report)
+			*report = ret;
+	}
+
+	index = ret.value & hash->rss.indirection_table_mask;
+	txq = READ_ONCE(hash->rss_indirection_table[index]);
+
+	return txq % numqueues;
+}
+
 static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
 				   struct iov_iter *from,
 				   struct virtio_net_hdr *hdr)
@@ -135,15 +262,17 @@ static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
 }
 
 static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-				   const struct virtio_net_hdr *hdr)
+				   const struct virtio_net_hdr_v1_hash *hdr)
 {
+	int content_sz = MIN(sizeof(*hdr), sz);
+
 	if (unlikely(iov_iter_count(iter) < sz))
 		return -EINVAL;
 
-	if (unlikely(copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr)))
+	if (unlikely(copy_to_iter(hdr, content_sz, iter) != content_sz))
 		return -EFAULT;
 
-	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+	if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
 		return -EFAULT;
 
 	return 0;
@@ -155,26 +284,38 @@ static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
 	return virtio_net_hdr_to_skb(skb, hdr, tun_vnet_is_little_endian(flags));
 }
 
-static inline int tun_vnet_hdr_from_skb(unsigned int flags,
+static inline int tun_vnet_hdr_from_skb(int sz, unsigned int flags,
 					const struct net_device *dev,
 					const struct sk_buff *skb,
-					struct virtio_net_hdr *hdr)
+					tun_vnet_hash_find vnet_hash_find,
+					struct virtio_net_hdr_v1_hash *hdr)
 {
 	int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
+	const struct virtio_net_hash *report = sz < sizeof(struct virtio_net_hdr_v1_hash) ?
+					       NULL : vnet_hash_find(skb);
+
+	*hdr = (struct virtio_net_hdr_v1_hash) {
+		.hash_report = VIRTIO_NET_HASH_REPORT_NONE
+	};
+
+	if (report) {
+		hdr->hash_value = cpu_to_le32(report->value);
+		hdr->hash_report = cpu_to_le16(report->report);
+	}
 
-	if (virtio_net_hdr_from_skb(skb, hdr,
+	if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
 				    tun_vnet_is_little_endian(flags), true,
 				    vlan_hlen)) {
 		struct skb_shared_info *sinfo = skb_shinfo(skb);
 
 		if (net_ratelimit()) {
 			netdev_err(dev, "unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n",
-				   sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->gso_size),
-				   tun_vnet16_to_cpu(flags, hdr->hdr_len));
+				   sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->hdr.gso_size),
+				   tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len));
 			print_hex_dump(KERN_ERR, "tun: ",
 				       DUMP_PREFIX_NONE,
 				       16, 1, skb->head,
-				       min(tun_vnet16_to_cpu(flags, hdr->hdr_len), 64), true);
+				       min(tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len), 64), true);
 		}
 		WARN_ON_ONCE(1);
 		return -EINVAL;
include/linux/if_tap.h +2 -0
index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..7334c46a3f101675a0d4e5a036987cfe18842f9f 100644
@@ -31,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
 #define MAX_TAP_QUEUES 256
 
 struct 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
index bb2b751d274acff931281a72e8b4b0c699b4e8af..cdd793f1c360ad5f63fcc4cbf67d845f5e2ccf6f 100644
@@ -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
index 287cdc81c9390c289a30545aa7ed23d81c3329d3..4887f97500a870c7ef3c96a5837b2d0a5a225040 100644
@@ -62,6 +62,42 @@
 #define TUNSETCARRIER _IOW('T', 226, int)
 #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHCAP - ioctl to get virtio_net hashing capability.
+ *
+ * The argument is a pointer to &struct tun_vnet_hash which will store the
+ * maximal virtio_net hashing configuration.
+ */
+#define TUNGETVNETHASHCAP _IOR('T', 228, struct tun_vnet_hash)
+
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
+ * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
+ * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
+ * to the size of &struct virtio_net_hdr_v1_hash.
+ *
+ * The members added to the legacy header by %TUN_VNET_HASH_REPORT flag will
+ * always be little-endian.
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.
+ */
+#define TUNSETVNETHASH _IOW('T', 229, struct tun_vnet_hash)
+
 /* TUNSETIFF ifr flags */
 #define IFF_TUN		0x0001
 #define IFF_TAP		0x0002
@@ -115,4 +151,43 @@ struct tun_filter {
 	__u8   addr[][ETH_ALEN];
 };
 
+/**
+ * define TUN_VNET_HASH_REPORT - Request virtio_net hash reporting for vhost
+ */
+#define TUN_VNET_HASH_REPORT	0x0001
+
+/**
+ * define TUN_VNET_HASH_RSS - Request virtio_net RSS
+ *
+ * This is mutually exclusive with eBPF steering program.
+ */
+#define TUN_VNET_HASH_RSS	0x0002
+
+/**
+ * struct tun_vnet_hash - virtio_net hashing configuration
+ * @flags:
+ *		Bitmask consists of %TUN_VNET_HASH_REPORT and %TUN_VNET_HASH_RSS
+ * @pad:
+ *		Should be filled with zero before passing to %TUNSETVNETHASH
+ * @types:
+ *		Bitmask of allowed hash types
+ */
+struct tun_vnet_hash {
+	__u16 flags;
+	__u8 pad[2];
+	__u32 types;
+};
+
+/**
+ * struct tun_vnet_hash_rss - virtio_net RSS configuration
+ * @indirection_table_mask:
+ *		Bitmask to be applied to the indirection table index
+ * @unclassified_queue:
+ *		The index of the queue to place unclassified packets in
+ */
+struct tun_vnet_hash_rss {
+	__u16 indirection_table_mask;
+	__u16 unclassified_queue;
+};
+
 #endif /* _UAPI__IF_TUN_H */
net/core/skbuff.c +4 -0
index 7b03b64fdcb276f68ce881d1d8da8e4c6b897efc..aa2a091b649f0c9d6e0196f34f345ba78b5498fb 100644
@@ -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 · 2 lines
-- 
2.48.1
#11 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 42 lines · Willem de Bruijn
Quoted 29 lines · Akihiko Odaki

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.

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.

Quoted 11 lines
---
  Documentation/networking/tuntap.rst |   7 ++
  drivers/net/Kconfig                 |   1 +
  drivers/net/tap.c                   |  68 ++++++++++++++-
  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, 386 insertions(+), 31 deletions(-)
Quoted 4 lines

This is arguably still doing too much in a single patch.

Can you split tap from tun? Move ioctl contrl operations out to their own patch?

I'll split changes for the code specific to TUN and TAP from the changes for the common code in the next version.

Quoted 61 lines
Quoted 61 lines
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -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
index 1fd5acdc73c6af0e1a861867039c3624fc618e25..aecfd244dd83585fea2c5b815dcd787c58166c28 100644
@@ -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 +14 -0
index d4ece538f1b23789ca60caa6232690e4d0a4d14a..9428b63ec27e7f92e78a78afcb5e24383862c00d 100644
@@ -49,6 +49,10 @@ struct major_info {
  	struct list_head next;
  };
 
+struct tap_skb_cb {
+	struct virtio_net_hash hash;
+};
+
  #define GOODCOPY_LEN 128
 
  static const struct proto_ops tap_socket_ops;
@@ -179,6 +183,22 @@ static void tap_put_queue(struct tap_queue *q)
  	sock_put(&q->sk);
  }
 
+static struct tap_skb_cb *tap_skb_cb(const struct sk_buff *skb)
+{
+	BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct tap_skb_cb));
+	return (struct tap_skb_cb *)skb->cb;
+}
+
+static struct virtio_net_hash *tap_add_hash(struct sk_buff *skb)
+{
+	return &tap_skb_cb(skb)->hash;
+}
Quoted 8 lines

drivers/net/tap.c static void tap_put_queue(struct tap_queue *q) · line 201

Quoted 6 lines
drivers/net/tap.c +6 -0
+
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+	return &tap_skb_cb(skb)->hash;
+}
+

These two helpers do the same thing.

They have different signatures, which matter because they are passed as function pointers to common interfaces.

Quoted 29 lines
Quoted 29 lines
drivers/net/tap.c +6 -0
  /*
   * 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
@@ -189,6 +209,7 @@ static void tap_put_queue(struct tap_queue *q)
  static struct tap_queue *tap_get_queue(struct tap_dev *tap,
  				       struct sk_buff *skb)
  {
+	struct flow_keys_basic keys_basic;
  	struct tap_queue *queue = NULL;
  	/* Access to taps array is protected by rcu, but access to numvtaps
  	 * isn't. Below we use it to lookup a queue, but treat it as a hint
@@ -196,17 +217,47 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
  	 * racing against queue removal.
  	 */
  	int numvtaps = READ_ONCE(tap->numvtaps);
+	struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash);
  	__u32 rxq;
 
+	*tap_skb_cb(skb) = (struct tap_skb_cb) {
+		.hash = { .report = VIRTIO_NET_HASH_REPORT_NONE }
+	};
+
  	if (!numvtaps)
  		goto out;
 
  	if (numvtaps == 1)
  		goto single;
 
Quoted 7 lines

drivers/net/tap.c static struct tap_queue *tap_get_queue(struct tap_dev *tap, · line 237

Quoted 5 lines
drivers/net/tap.c +5 -0
+	if (vnet_hash) {
+		if ((vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+			rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash);
+			queue = rcu_dereference(tap->taps[rxq]);
+			goto out;

so tun_vnet_hash_report does not work in this case?

tun_vnet_rss_select_queue() adds the hash to skb if necessary so it has tap_add_hash as its argument.

Quoted 648 lines
Quoted 268 lines
drivers/net/tap.c +34 -3
+		}
+
+		if (!skb->l4_hash && !skb->sw_hash) {
+			struct flow_keys keys;
+
+			skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+			rxq = flow_hash_from_keys(&keys);
+			keys_basic = (struct flow_keys_basic) {
+				.control = keys.control,
+				.basic = keys.basic
+			};
+		} else {
+			skb_flow_dissect_flow_keys_basic(NULL, skb, &keys_basic, NULL, 0, 0, 0,
+							 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+			rxq = skb->hash;
+		}
+	} else {
+		rxq = skb_get_hash(skb);
+	}
+
  	/* Check if we can use flow to select a queue */
-	rxq = skb_get_hash(skb);
  	if (rxq) {
+		tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
  		queue = rcu_dereference(tap->taps[rxq % numvtaps]);
  		goto out;
  	}
@@ -711,11 +762,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;
 
@@ -992,6 +1044,16 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
  		rtnl_unlock();
  		return ret;
 
+	case TUNGETVNETHASHCAP:
+		return tun_vnet_ioctl_gethashcap(argp);
+
+	case TUNSETVNETHASH:
+		rtnl_lock();
+		tap = rtnl_dereference(q->tap);
+		ret = tap ? tun_vnet_ioctl_sethash(&tap->vnet_hash, true, argp) : -EBADFD;
+		rtnl_unlock();
+		return ret;
+
  	case SIOCGIFHWADDR:
  		rtnl_lock();
  		tap = tap_get_tap_dev(q);
drivers/net/tun.c +71 -18
index d8f4d3e996a7a81d1f8b04635054081671a14f07..520013df416e93d3a50b46be9b53ae9ab410eab4 100644
@@ -209,6 +209,7 @@ struct tun_struct {
  	struct bpf_prog __rcu *xdp_prog;
  	struct tun_prog __rcu *steering_prog;
  	struct tun_prog __rcu *filter_prog;
+	struct tun_vnet_hash_container __rcu *vnet_hash;
  	struct ethtool_link_ksettings link_ksettings;
  	/* init args */
  	struct file *file;
@@ -451,20 +452,37 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
  		e->rps_rxhash = hash;
  }
 
+static struct virtio_net_hash *tun_add_hash(struct sk_buff *skb)
+{
+	return skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+	return skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
  /* 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
   * the userspace application move between processors, we may get a
   * different rxq no. here.
   */
-static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static u16 tun_automq_select_queue(struct tun_struct *tun,
+				   const struct tun_vnet_hash_container *vnet_hash,
+				   struct sk_buff *skb)
  {
+	struct flow_keys keys;
+	struct flow_keys_basic keys_basic;
  	struct tun_flow_entry *e;
  	u32 txq, numqueues;
 
  	numqueues = READ_ONCE(tun->numqueues);
 
-	txq = __skb_get_hash_symmetric(skb);
+	memset(&keys, 0, sizeof(keys));
+	skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, 0);
+
+	txq = flow_hash_from_keys(&keys);
  	e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
  	if (e) {
  		tun_flow_save_rps_rxhash(e, txq);
@@ -473,6 +491,13 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
  		txq = reciprocal_scale(txq, numqueues);
  	}
 
+	keys_basic = (struct flow_keys_basic) {
+		.control = keys.control,
+		.basic = keys.basic
+	};
+	tun_vnet_hash_report(vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+			     tun_add_hash);
+
  	return txq;
  }
 
@@ -500,10 +525,17 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
  	u16 ret;
 
  	rcu_read_lock();
-	if (rcu_dereference(tun->steering_prog))
+	if (rcu_dereference(tun->steering_prog)) {
  		ret = tun_ebpf_select_queue(tun, skb);
-	else
-		ret = tun_automq_select_queue(tun, skb);
+	} else {
+		struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tun->vnet_hash);
+
+		if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS))
+			ret = tun_vnet_rss_select_queue(READ_ONCE(tun->numqueues), vnet_hash,
+							skb, tun_add_hash);
+		else
+			ret = tun_automq_select_queue(tun, vnet_hash, skb);
+	}
  	rcu_read_unlock();
 
  	return ret;
@@ -1987,7 +2019,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);
@@ -2040,9 +2072,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;
 
@@ -2223,6 +2256,7 @@ static void tun_free_netdev(struct net_device *dev)
  	security_tun_dev_free_security(tun->security);
  	__tun_set_ebpf(tun, &tun->steering_prog, NULL);
  	__tun_set_ebpf(tun, &tun->filter_prog, NULL);
+	kfree_rcu_mightsleep(rcu_access_pointer(tun->vnet_hash));
  }
 
  static void tun_setup(struct net_device *dev)
@@ -2921,13 +2955,9 @@ static int tun_set_queue(struct file *file, struct ifreq *ifr)
  }
 
  static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
-			void __user *data)
+			int fd)
  {
  	struct bpf_prog *prog;
-	int fd;
-
-	if (copy_from_user(&fd, data, sizeof(fd)))
-		return -EFAULT;
 
  	if (fd == -1) {
  		prog = NULL;
@@ -2993,7 +3023,9 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  	int ifindex;
  	int sndbuf;
  	int ret;
+	int fd;
  	bool do_notify = false;
+	struct tun_vnet_hash_container *vnet_hash;
 
  	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
  	    (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
@@ -3020,7 +3052,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  	rtnl_lock();
 
  	tun = tun_get(tfile);
-	if (cmd == TUNSETIFF) {
+	switch (cmd) {
+	case TUNSETIFF:
  		ret = -EEXIST;
  		if (tun)
  			goto unlock;
@@ -3035,8 +3068,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  		if (copy_to_user(argp, &ifr, ifreq_len))
  			ret = -EFAULT;
  		goto unlock;
-	}
-	if (cmd == TUNSETIFINDEX) {
+
+	case TUNSETIFINDEX:
  		ret = -EPERM;
  		if (tun)
  			goto unlock;
@@ -3050,6 +3083,10 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  		ret = 0;
  		tfile->ifindex = ifindex;
  		goto unlock;
+
+	case TUNGETVNETHASHCAP:
+		ret = tun_vnet_ioctl_gethashcap(argp);
+		goto unlock;
  	}
 
  	ret = -EBADFD;
@@ -3230,11 +3267,27 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  		break;
 
  	case TUNSETSTEERINGEBPF:
-		ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
+		if (get_user(fd, (int __user *)argp)) {
+			ret = -EFAULT;
+			break;
+		}
+
+		vnet_hash = rtnl_dereference(tun->vnet_hash);
+		if (fd != -1 && vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+			ret = -EBUSY;
+			break;
+		}
+
+		ret = tun_set_ebpf(tun, &tun->steering_prog, fd);
  		break;
 
  	case TUNSETFILTEREBPF:
-		ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
+		if (get_user(fd, (int __user *)argp)) {
+			ret = -EFAULT;
+			break;
+		}
+
+		ret = tun_set_ebpf(tun, &tun->filter_prog, fd);
  		break;
 
  	case TUNSETCARRIER:
@@ -3252,8 +3305,15 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  		ret = open_related_ns(&net->ns, get_net_ns);
  		break;
 

drivers/net/tun.c static long __tun_chr_ioctl(struct file *file, unsigned int cmd, · line 3316

Quoted 10 lines
drivers/net/tun.c +8 -1
+	case TUNSETVNETHASH:
+		ret = tun_vnet_ioctl_sethash(&tun->vnet_hash,
+					     !rtnl_dereference(tun->steering_prog),
+					     argp);
+		break;
+
  	default:
-		ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags, cmd, argp);
+		ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
+				     cmd, argp);

no need to touch this

Quoted 365 lines
drivers/net/tun.c +0 -0
  		break;
  	}
 
drivers/net/tun_vnet.h +150 -9
index 58b9ac7a5fc4084c789fe94fe36b5f8631bf1fa4..8e7d51fb0b4742cef56e7c5ad778b156cc654bed 100644
@@ -6,6 +6,16 @@
  #define TUN_VNET_LE     0x80000000
  #define TUN_VNET_BE     0x40000000
 
+typedef struct virtio_net_hash *(*tun_vnet_hash_add)(struct sk_buff *);
+typedef const struct virtio_net_hash *(*tun_vnet_hash_find)(const struct sk_buff *);
+
+struct tun_vnet_hash_container {
+	struct tun_vnet_hash common;
+	struct tun_vnet_hash_rss rss;
+	u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
+	u16 rss_indirection_table[];
+};
+
  static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
  {
  	bool be = IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) &&
@@ -107,6 +117,123 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
  	}
  }
 
+static inline long tun_vnet_ioctl_gethashcap(void __user *argp)
+{
+	static const struct tun_vnet_hash cap = {
+		.flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS,
+		.types = VIRTIO_NET_SUPPORTED_HASH_TYPES
+	};
+
+	return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+					  bool can_rss, void __user *argp)
+{
+	struct tun_vnet_hash hash_buf;
+	struct tun_vnet_hash_container *hash;
+
+	if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
+		return -EFAULT;
+	argp = (struct tun_vnet_hash __user *)argp + 1;
+
+	if (hash_buf.flags & TUN_VNET_HASH_RSS) {
+		struct tun_vnet_hash_rss rss;
+		size_t indirection_table_size;
+		size_t key_size;
+		size_t size;
+
+		if (!can_rss)
+			return -EBUSY;
+
+		if (copy_from_user(&rss, argp, sizeof(rss)))
+			return -EFAULT;
+		argp = (struct tun_vnet_hash_rss __user *)argp + 1;
+
+		indirection_table_size = ((size_t)rss.indirection_table_mask + 1) * 2;
+		key_size = virtio_net_hash_key_length(hash_buf.types);
+		size = struct_size(hash, rss_indirection_table,
+				   (size_t)rss.indirection_table_mask + 1);
+
+		hash = kmalloc(size, GFP_KERNEL);
+		if (!hash)
+			return -ENOMEM;
+
+		if (copy_from_user(hash->rss_indirection_table,
+				   argp, indirection_table_size)) {
+			kfree(hash);
+			return -EFAULT;
+		}
+		argp = (u16 __user *)argp + rss.indirection_table_mask + 1;
+
+		if (copy_from_user(hash->rss_key, argp, key_size)) {
+			kfree(hash);
+			return -EFAULT;
+		}
+
+		virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+		hash->rss = rss;
+	} else {
+		hash = kmalloc(sizeof(hash->common), GFP_KERNEL);
+		if (!hash)
+			return -ENOMEM;
+	}
+
+	hash->common = hash_buf;
+	kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+	return 0;
+}
+
+static void tun_vnet_hash_report(const struct tun_vnet_hash_container *hash,
+				 struct sk_buff *skb,
+				 const struct flow_keys_basic *keys,
+				 u32 value,
+				 tun_vnet_hash_add vnet_hash_add)
+{
+	struct virtio_net_hash *report;
+
+	if (!hash || !(hash->common.flags & TUN_VNET_HASH_REPORT))
+		return;
+
+	report = vnet_hash_add(skb);
+	if (!report)
+		return;
+
+	*report = (struct virtio_net_hash) {
+		.report = virtio_net_hash_report(hash->common.types, keys),
+		.value = value
+	};
+}
+
+static u16 tun_vnet_rss_select_queue(u32 numqueues,
+				     const struct tun_vnet_hash_container *hash,
+				     struct sk_buff *skb,
+				     tun_vnet_hash_add vnet_hash_add)
+{
+	struct virtio_net_hash *report;
+	struct virtio_net_hash ret;
+	u16 txq, index;
+
+	if (!numqueues)
+		return 0;
+
+	virtio_net_hash_rss(skb, hash->common.types, hash->rss_key, &ret);
+
+	if (!ret.report)
+		return hash->rss.unclassified_queue % numqueues;
+
+	if (hash->common.flags & TUN_VNET_HASH_REPORT) {
+		report = vnet_hash_add(skb);
+		if (report)
+			*report = ret;
+	}
+
+	index = ret.value & hash->rss.indirection_table_mask;
+	txq = READ_ONCE(hash->rss_indirection_table[index]);
+
+	return txq % numqueues;
+}
+
  static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
  				   struct iov_iter *from,
  				   struct virtio_net_hdr *hdr)
@@ -135,15 +262,17 @@ static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
  }
 
  static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-				   const struct virtio_net_hdr *hdr)
+				   const struct virtio_net_hdr_v1_hash *hdr)
  {
+	int content_sz = MIN(sizeof(*hdr), sz);
+
  	if (unlikely(iov_iter_count(iter) < sz))
  		return -EINVAL;
 
-	if (unlikely(copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr)))
+	if (unlikely(copy_to_iter(hdr, content_sz, iter) != content_sz))
  		return -EFAULT;
 
-	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+	if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
  		return -EFAULT;
 
  	return 0;
@@ -155,26 +284,38 @@ static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
  	return virtio_net_hdr_to_skb(skb, hdr, tun_vnet_is_little_endian(flags));
  }
 
-static inline int tun_vnet_hdr_from_skb(unsigned int flags,
+static inline int tun_vnet_hdr_from_skb(int sz, unsigned int flags,
  					const struct net_device *dev,
  					const struct sk_buff *skb,
-					struct virtio_net_hdr *hdr)
+					tun_vnet_hash_find vnet_hash_find,
+					struct virtio_net_hdr_v1_hash *hdr)
  {
  	int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
+	const struct virtio_net_hash *report = sz < sizeof(struct virtio_net_hdr_v1_hash) ?
+					       NULL : vnet_hash_find(skb);
+
+	*hdr = (struct virtio_net_hdr_v1_hash) {
+		.hash_report = VIRTIO_NET_HASH_REPORT_NONE
+	};
+
+	if (report) {
+		hdr->hash_value = cpu_to_le32(report->value);
+		hdr->hash_report = cpu_to_le16(report->report);
+	}
 
-	if (virtio_net_hdr_from_skb(skb, hdr,
+	if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
  				    tun_vnet_is_little_endian(flags), true,
  				    vlan_hlen)) {
  		struct skb_shared_info *sinfo = skb_shinfo(skb);
 
  		if (net_ratelimit()) {
  			netdev_err(dev, "unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n",
-				   sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->gso_size),
-				   tun_vnet16_to_cpu(flags, hdr->hdr_len));
+				   sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->hdr.gso_size),
+				   tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len));
  			print_hex_dump(KERN_ERR, "tun: ",
  				       DUMP_PREFIX_NONE,
  				       16, 1, skb->head,
-				       min(tun_vnet16_to_cpu(flags, hdr->hdr_len), 64), true);
+				       min(tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len), 64), true);
  		}
  		WARN_ON_ONCE(1);
  		return -EINVAL;
include/linux/if_tap.h +2 -0
index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..7334c46a3f101675a0d4e5a036987cfe18842f9f 100644
@@ -31,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
  #define MAX_TAP_QUEUES 256
 
  struct 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
index bb2b751d274acff931281a72e8b4b0c699b4e8af..cdd793f1c360ad5f63fcc4cbf67d845f5e2ccf6f 100644
@@ -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
index 287cdc81c9390c289a30545aa7ed23d81c3329d3..4887f97500a870c7ef3c96a5837b2d0a5a225040 100644
@@ -62,6 +62,42 @@
  #define TUNSETCARRIER _IOW('T', 226, int)
  #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHCAP - ioctl to get virtio_net hashing capability.
+ *
+ * The argument is a pointer to &struct tun_vnet_hash which will store the
+ * maximal virtio_net hashing configuration.
+ */
+#define TUNGETVNETHASHCAP _IOR('T', 228, struct tun_vnet_hash)
+
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
+ * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
+ * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
+ * to the size of &struct virtio_net_hdr_v1_hash.
+ *
+ * The members added to the legacy header by %TUN_VNET_HASH_REPORT flag will
+ * always be little-endian.
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.
+ */
+#define TUNSETVNETHASH _IOW('T', 229, struct tun_vnet_hash)
+
  /* TUNSETIFF ifr flags */
  #define IFF_TUN		0x0001
  #define IFF_TAP		0x0002
@@ -115,4 +151,43 @@ struct tun_filter {
  	__u8   addr[][ETH_ALEN];
  };
 
+/**
+ * define TUN_VNET_HASH_REPORT - Request virtio_net hash reporting for vhost
+ */
+#define TUN_VNET_HASH_REPORT	0x0001
+
+/**
+ * define TUN_VNET_HASH_RSS - Request virtio_net RSS
+ *
+ * This is mutually exclusive with eBPF steering program.
+ */
+#define TUN_VNET_HASH_RSS	0x0002
+
+/**
+ * struct tun_vnet_hash - virtio_net hashing configuration
+ * @flags:
+ *		Bitmask consists of %TUN_VNET_HASH_REPORT and %TUN_VNET_HASH_RSS
+ * @pad:
+ *		Should be filled with zero before passing to %TUNSETVNETHASH
+ * @types:
+ *		Bitmask of allowed hash types
+ */
+struct tun_vnet_hash {
+	__u16 flags;
+	__u8 pad[2];
+	__u32 types;
+};
+
+/**
+ * struct tun_vnet_hash_rss - virtio_net RSS configuration
+ * @indirection_table_mask:
+ *		Bitmask to be applied to the indirection table index
+ * @unclassified_queue:
+ *		The index of the queue to place unclassified packets in
+ */
+struct tun_vnet_hash_rss {
+	__u16 indirection_table_mask;
+	__u16 unclassified_queue;
+};
+
  #endif /* _UAPI__IF_TUN_H */
net/core/skbuff.c +4 -0
index 7b03b64fdcb276f68ce881d1d8da8e4c6b897efc..aa2a091b649f0c9d6e0196f34f345ba78b5498fb 100644
@@ -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 · 2 lines
-- 
2.48.1
#12 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

In reply to Akihiko Odaki
Quoted 425 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
 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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -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
index 1fd5acdc73c6af0e1a861867039c3624fc618e25..aecfd244dd83585fea2c5b815dcd787c58166c28 100644
@@ -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 +65 -3
index d4ece538f1b23789ca60caa6232690e4d0a4d14a..9428b63ec27e7f92e78a78afcb5e24383862c00d 100644
@@ -49,6 +49,10 @@ struct major_info {
        struct list_head next;
 };
 
+struct tap_skb_cb {
+       struct virtio_net_hash hash;
+};
+
 #define GOODCOPY_LEN 128
 
 static const struct proto_ops tap_socket_ops;
@@ -179,6 +183,22 @@ static void tap_put_queue(struct tap_queue *q)
        sock_put(&q->sk);
 }
 
+static struct tap_skb_cb *tap_skb_cb(const struct sk_buff *skb)
+{
+       BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct tap_skb_cb));
+       return (struct tap_skb_cb *)skb->cb;
+}
+
+static struct virtio_net_hash *tap_add_hash(struct sk_buff *skb)
+{
+       return &tap_skb_cb(skb)->hash;
+}
+
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return &tap_skb_cb(skb)->hash;
+}
+
 /*
  * 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
@@ -189,6 +209,7 @@ static void tap_put_queue(struct tap_queue *q)
 static struct tap_queue *tap_get_queue(struct tap_dev *tap,
                                       struct sk_buff *skb)
 {
+       struct flow_keys_basic keys_basic;
        struct tap_queue *queue = NULL;
        /* Access to taps array is protected by rcu, but access to numvtaps
         * isn't. Below we use it to lookup a queue, but treat it as a hint
@@ -196,17 +217,47 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
         * racing against queue removal.
         */
        int numvtaps = READ_ONCE(tap->numvtaps);
+       struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash);
        __u32 rxq;
 
+       *tap_skb_cb(skb) = (struct tap_skb_cb) {
+               .hash = { .report = VIRTIO_NET_HASH_REPORT_NONE }
+       };
+
        if (!numvtaps)
                goto out;
 
        if (numvtaps == 1)
                goto single;
 
+       if (vnet_hash) {
+               if ((vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash);
+                       queue = rcu_dereference(tap->taps[rxq]);
+                       goto out;
+               }
+
+               if (!skb->l4_hash && !skb->sw_hash) {
+                       struct flow_keys keys;
+
+                       skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+                       rxq = flow_hash_from_keys(&keys);
+                       keys_basic = (struct flow_keys_basic) {
+                               .control = keys.control,
+                               .basic = keys.basic
+                       };
+               } else {
+                       skb_flow_dissect_flow_keys_basic(NULL, skb, &keys_basic, NULL, 0, 0, 0,
+                                                        FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+                       rxq = skb->hash;
+               }
+       } else {
+               rxq = skb_get_hash(skb);
+       }
+
        /* Check if we can use flow to select a queue */
-       rxq = skb_get_hash(skb);
        if (rxq) {
+               tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
                queue = rcu_dereference(tap->taps[rxq % numvtaps]);
                goto out;
        }
@@ -711,11 +762,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;
 
@@ -992,6 +1044,16 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
                rtnl_unlock();
                return ret;
 
+       case TUNGETVNETHASHCAP:
+               return tun_vnet_ioctl_gethashcap(argp);
+
+       case TUNSETVNETHASH:
+               rtnl_lock();
+               tap = rtnl_dereference(q->tap);
+               ret = tap ? tun_vnet_ioctl_sethash(&tap->vnet_hash, true, argp) : -EBADFD;
+               rtnl_unlock();
+               return ret;
+
        case SIOCGIFHWADDR:
                rtnl_lock();
                tap = tap_get_tap_dev(q);
drivers/net/tun.c +79 -19
index d8f4d3e996a7a81d1f8b04635054081671a14f07..520013df416e93d3a50b46be9b53ae9ab410eab4 100644
@@ -209,6 +209,7 @@ struct tun_struct {
        struct bpf_prog __rcu *xdp_prog;
        struct tun_prog __rcu *steering_prog;
        struct tun_prog __rcu *filter_prog;
+       struct tun_vnet_hash_container __rcu *vnet_hash;
        struct ethtool_link_ksettings link_ksettings;
        /* init args */
        struct file *file;
@@ -451,20 +452,37 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                e->rps_rxhash = hash;
 }
 
+static struct virtio_net_hash *tun_add_hash(struct sk_buff *skb)
+{
+       return skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
 /* 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
  * the userspace application move between processors, we may get a
  * different rxq no. here.
  */
-static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static u16 tun_automq_select_queue(struct tun_struct *tun,
+                                  const struct tun_vnet_hash_container *vnet_hash,
+                                  struct sk_buff *skb)
 {
+       struct flow_keys keys;
+       struct flow_keys_basic keys_basic;
        struct tun_flow_entry *e;
        u32 txq, numqueues;
 
        numqueues = READ_ONCE(tun->numqueues);
 
-       txq = __skb_get_hash_symmetric(skb);
+       memset(&keys, 0, sizeof(keys));
+       skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, 0);
+
+       txq = flow_hash_from_keys(&keys);
        e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
        if (e) {
                tun_flow_save_rps_rxhash(e, txq);
@@ -473,6 +491,13 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
                txq = reciprocal_scale(txq, numqueues);
        }
 
+       keys_basic = (struct flow_keys_basic) {
+               .control = keys.control,
+               .basic = keys.basic
+       };
+       tun_vnet_hash_report(vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+                            tun_add_hash);
+
        return txq;
 }
 
@@ -500,10 +525,17 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
        u16 ret;
 
        rcu_read_lock();
-       if (rcu_dereference(tun->steering_prog))
+       if (rcu_dereference(tun->steering_prog)) {
                ret = tun_ebpf_select_queue(tun, skb);
-       else
-               ret = tun_automq_select_queue(tun, skb);
+       } else {
+               struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tun->vnet_hash);
+
+               if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS))
+                       ret = tun_vnet_rss_select_queue(READ_ONCE(tun->numqueues), vnet_hash,
+                                                       skb, tun_add_hash);
+               else
+                       ret = tun_automq_select_queue(tun, vnet_hash, skb);
+       }
        rcu_read_unlock();
 
        return ret;
@@ -1987,7 +2019,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);
@@ -2040,9 +2072,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;
 
@@ -2223,6 +2256,7 @@ static void tun_free_netdev(struct net_device *dev)
        security_tun_dev_free_security(tun->security);
        __tun_set_ebpf(tun, &tun->steering_prog, NULL);
        __tun_set_ebpf(tun, &tun->filter_prog, NULL);
+       kfree_rcu_mightsleep(rcu_access_pointer(tun->vnet_hash));
 }
 
 static void tun_setup(struct net_device *dev)
@@ -2921,13 +2955,9 @@ static int tun_set_queue(struct file *file, struct ifreq *ifr)
 }
 
 static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
-                       void __user *data)
+                       int fd)
 {
        struct bpf_prog *prog;
-       int fd;
-
-       if (copy_from_user(&fd, data, sizeof(fd)))
-               return -EFAULT;
 
        if (fd == -1) {
                prog = NULL;
@@ -2993,7 +3023,9 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
        int ifindex;
        int sndbuf;
        int ret;
+       int fd;
        bool do_notify = false;
+       struct tun_vnet_hash_container *vnet_hash;
 
        if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
            (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
@@ -3020,7 +3052,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
        rtnl_lock();
 
        tun = tun_get(tfile);
-       if (cmd == TUNSETIFF) {
+       switch (cmd) {
+       case TUNSETIFF:
                ret = -EEXIST;
                if (tun)
                        goto unlock;
@@ -3035,8 +3068,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                if (copy_to_user(argp, &ifr, ifreq_len))
                        ret = -EFAULT;
                goto unlock;
-       }
-       if (cmd == TUNSETIFINDEX) {
+
+       case TUNSETIFINDEX:
                ret = -EPERM;
                if (tun)
                        goto unlock;
@@ -3050,6 +3083,10 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                ret = 0;
                tfile->ifindex = ifindex;
                goto unlock;
+
+       case TUNGETVNETHASHCAP:
+               ret = tun_vnet_ioctl_gethashcap(argp);
+               goto unlock;
        }
 
        ret = -EBADFD;
@@ -3230,11 +3267,27 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                break;
 
        case TUNSETSTEERINGEBPF:
-               ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
+               if (get_user(fd, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               vnet_hash = rtnl_dereference(tun->vnet_hash);
+               if (fd != -1 && vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       ret = -EBUSY;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->steering_prog, fd);
                break;
 
        case TUNSETFILTEREBPF:
-               ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
+               if (get_user(fd, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->filter_prog, fd);
                break;
 
        case TUNSETCARRIER:
@@ -3252,8 +3305,15 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                ret = open_related_ns(&net->ns, get_net_ns);
                break;
 
+       case TUNSETVNETHASH:
+               ret = tun_vnet_ioctl_sethash(&tun->vnet_hash,
+                                            !rtnl_dereference(tun->steering_prog),
+                                            argp);
+               break;
+
        default:
-               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags, cmd, argp);
+               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
+                                    cmd, argp);
                break;
        }
 
drivers/net/tun_vnet.h +0 -0
index 58b9ac7a5fc4084c789fe94fe36b5f8631bf1fa4..8e7d51fb0b4742cef56e7c5ad778b156cc654bed 100644

drivers/net/tun_vnet.h line 13

Quoted 8 lines
drivers/net/tun_vnet.h +5 -0
@@ -6,6 +6,16 @@
 #define TUN_VNET_LE     0x80000000
 #define TUN_VNET_BE     0x40000000
 
+typedef struct virtio_net_hash *(*tun_vnet_hash_add)(struct sk_buff *);
+typedef const struct virtio_net_hash *(*tun_vnet_hash_find)(const struct sk_buff *);
+
+struct tun_vnet_hash_container {
+       struct tun_vnet_hash common;

I'd rename this as hash.

drivers/net/tun_vnet.h line 17

Quoted 4 lines
drivers/net/tun_vnet.h +4 -0
+       struct tun_vnet_hash_rss rss;
+       u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
+       u16 rss_indirection_table[];
+};

Besides the separated ioctl, I'd split this structure into rss and hash part as well.

Quoted 6 lines
drivers/net/tun_vnet.h +1 -0
+
 static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
 {
        bool be = IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) &&
@@ -107,6 +117,123 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
        }

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 127

Quoted 10 lines
drivers/net/tun_vnet.h +8 -0
 }
 
+static inline long tun_vnet_ioctl_gethashcap(void __user *argp)
+{
+       static const struct tun_vnet_hash cap = {
+               .flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS,
+               .types = VIRTIO_NET_SUPPORTED_HASH_TYPES
+       };
+
+       return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;

Let's has a consistent name for this and the uapi to be consistent with TUNSETIFF/TUNGETIFF. Probably TUNSETVNETHASH and tun_vnet_ioctl_gethash().

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 131

Quoted 4 lines
drivers/net/tun_vnet.h +4 -0
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

Quoted 38 lines
drivers/net/tun_vnet.h +38 -0
+{
+       struct tun_vnet_hash hash_buf;
+       struct tun_vnet_hash_container *hash;
+
+       if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
+               return -EFAULT;
+       argp = (struct tun_vnet_hash __user *)argp + 1;
+
+       if (hash_buf.flags & TUN_VNET_HASH_RSS) {
+               struct tun_vnet_hash_rss rss;
+               size_t indirection_table_size;
+               size_t key_size;
+               size_t size;
+
+               if (!can_rss)
+                       return -EBUSY;
+
+               if (copy_from_user(&rss, argp, sizeof(rss)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_hash_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)rss.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(hash_buf.types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)rss.indirection_table_mask + 1);
+
+               hash = kmalloc(size, GFP_KERNEL);
+               if (!hash)
+                       return -ENOMEM;
+
+               if (copy_from_user(hash->rss_indirection_table,
+                                  argp, indirection_table_size)) {
+                       kfree(hash);
+                       return -EFAULT;
+               }
+               argp = (u16 __user *)argp + rss.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 179

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+                       kfree(hash);
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->rss = rss;
+       } else {
+               hash = kmalloc(sizeof(hash->common), GFP_KERNEL);
+               if (!hash)
+                       return -ENOMEM;

Do we need to validate the hash here (at least against the types we supported?)

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 183

Quoted 4 lines
drivers/net/tun_vnet.h +4 -0
+       }
+
+       hash->common = hash_buf;
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));

I still didn't understand the trick here. E.g we use very simple primitives in synchronizing ebpf program through RCU in __tun_set_ebpf().

Quoted 12 lines
drivers/net/tun_vnet.h +12 -0
+       return 0;
+}
+
+static void tun_vnet_hash_report(const struct tun_vnet_hash_container *hash,
+                                struct sk_buff *skb,
+                                const struct flow_keys_basic *keys,
+                                u32 value,
+                                tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+
+       if (!hash || !(hash->common.flags & TUN_VNET_HASH_REPORT))

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 205

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.types, keys),
+               .value = value
+       };

What's the advantage of using Designated Initializers here? Simple assignment can save two lines of code.

Quoted 14 lines
drivers/net/tun_vnet.h +14 -0
+}
+
+static u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                    const struct tun_vnet_hash_container *hash,
+                                    struct sk_buff *skb,
+                                    tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 txq, index;
+
+       if (!numqueues)
+               return 0;
+

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 229

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+       virtio_net_hash_rss(skb, hash->common.types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->rss.unclassified_queue % numqueues;
+
+       if (hash->common.flags & TUN_VNET_HASH_REPORT) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 232

Quoted 3 lines
drivers/net/tun_vnet.h +3 -0
+
+       index = ret.value & hash->rss.indirection_table_mask;
+       txq = READ_ONCE(hash->rss_indirection_table[index]);

So vnet_hash is accessed via rcu_dereference(), I don't get any reason we need READ_ONCE here, is this paired with something? If yes, let's add a comment here. If rss_indirection_table need why indirection_table_mask doesn't need this?

Quoted 8 lines
drivers/net/tun_vnet.h +4 -0
+
+       return txq % numqueues;
+}
+
 static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                   struct iov_iter *from,
                                   struct virtio_net_hdr *hdr)

drivers/net/tun_vnet.h static inline int tun_vnet_hdr_get(int sz, unsigned int flags, · line 266

Quoted 6 lines
drivers/net/tun_vnet.h +1 -1
@@ -135,15 +262,17 @@ static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
 }
 
 static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-                                  const struct virtio_net_hdr *hdr)
+                                  const struct virtio_net_hdr_v1_hash *hdr)
 {

To be more robust, we can tweak the function to accept a vnet_hdr_len parameter then we can avoid touching this every time when we need to extend vnet hdr in the future?

Quoted 115 lines
drivers/net/tun_vnet.h +22 -8
+       int content_sz = MIN(sizeof(*hdr), sz);
+
        if (unlikely(iov_iter_count(iter) < sz))
                return -EINVAL;
 
-       if (unlikely(copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr)))
+       if (unlikely(copy_to_iter(hdr, content_sz, iter) != content_sz))
                return -EFAULT;
 
-       if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+       if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
                return -EFAULT;
 
        return 0;
@@ -155,26 +284,38 @@ static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
        return virtio_net_hdr_to_skb(skb, hdr, tun_vnet_is_little_endian(flags));
 }
 
-static inline int tun_vnet_hdr_from_skb(unsigned int flags,
+static inline int tun_vnet_hdr_from_skb(int sz, unsigned int flags,
                                        const struct net_device *dev,
                                        const struct sk_buff *skb,
-                                       struct virtio_net_hdr *hdr)
+                                       tun_vnet_hash_find vnet_hash_find,
+                                       struct virtio_net_hdr_v1_hash *hdr)
 {
        int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
+       const struct virtio_net_hash *report = sz < sizeof(struct virtio_net_hdr_v1_hash) ?
+                                              NULL : vnet_hash_find(skb);
+
+       *hdr = (struct virtio_net_hdr_v1_hash) {
+               .hash_report = VIRTIO_NET_HASH_REPORT_NONE
+       };
+
+       if (report) {
+               hdr->hash_value = cpu_to_le32(report->value);
+               hdr->hash_report = cpu_to_le16(report->report);
+       }
 
-       if (virtio_net_hdr_from_skb(skb, hdr,
+       if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
                                    tun_vnet_is_little_endian(flags), true,
                                    vlan_hlen)) {
                struct skb_shared_info *sinfo = skb_shinfo(skb);
 
                if (net_ratelimit()) {
                        netdev_err(dev, "unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n",
-                                  sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->gso_size),
-                                  tun_vnet16_to_cpu(flags, hdr->hdr_len));
+                                  sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->hdr.gso_size),
+                                  tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len));
                        print_hex_dump(KERN_ERR, "tun: ",
                                       DUMP_PREFIX_NONE,
                                       16, 1, skb->head,
-                                      min(tun_vnet16_to_cpu(flags, hdr->hdr_len), 64), true);
+                                      min(tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len), 64), true);
                }
                WARN_ON_ONCE(1);
                return -EINVAL;
include/linux/if_tap.h +2 -0
index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..7334c46a3f101675a0d4e5a036987cfe18842f9f 100644
@@ -31,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
 #define MAX_TAP_QUEUES 256
 
 struct 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
index bb2b751d274acff931281a72e8b4b0c699b4e8af..cdd793f1c360ad5f63fcc4cbf67d845f5e2ccf6f 100644
@@ -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 +14 -0
index 287cdc81c9390c289a30545aa7ed23d81c3329d3..4887f97500a870c7ef3c96a5837b2d0a5a225040 100644
@@ -62,6 +62,42 @@
 #define TUNSETCARRIER _IOW('T', 226, int)
 #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHCAP - ioctl to get virtio_net hashing capability.
+ *
+ * The argument is a pointer to &struct tun_vnet_hash which will store the
+ * maximal virtio_net hashing configuration.
+ */
+#define TUNGETVNETHASHCAP _IOR('T', 228, struct tun_vnet_hash)
+
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if

include/uapi/linux/if_tun.h line 88

Quoted 10 lines
include/uapi/linux/if_tun.h +10 -0
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
+ * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
+ * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
+ * to the size of &struct virtio_net_hdr_v1_hash.

So you had a dependency check already for vnet hdr len. I'd still suggest to split this into rss and hash as they are separated features. Then we can use separate data structure for them instead of a container struct.

Quoted 35 lines
include/uapi/linux/if_tun.h +28 -0
+ *
+ * The members added to the legacy header by %TUN_VNET_HASH_REPORT flag will
+ * always be little-endian.
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.
+ */
+#define TUNSETVNETHASH _IOW('T', 229, struct tun_vnet_hash)
+
 /* TUNSETIFF ifr flags */
 #define IFF_TUN                0x0001
 #define IFF_TAP                0x0002
@@ -115,4 +151,43 @@ struct tun_filter {
        __u8   addr[][ETH_ALEN];
 };
 
+/**
+ * define TUN_VNET_HASH_REPORT - Request virtio_net hash reporting for vhost
+ */
+#define TUN_VNET_HASH_REPORT   0x0001
+
+/**
+ * define TUN_VNET_HASH_RSS - Request virtio_net RSS
+ *
+ * This is mutually exclusive with eBPF steering program.
+ */
+#define TUN_VNET_HASH_RSS      0x0002
+
+/**
+ * struct tun_vnet_hash - virtio_net hashing configuration
+ * @flags:
+ *             Bitmask consists of %TUN_VNET_HASH_REPORT and %TUN_VNET_HASH_RSS

include/uapi/linux/if_tun.h struct tun_filter { · line 179

Quoted 10 lines
include/uapi/linux/if_tun.h +10 -0
+ * @pad:
+ *             Should be filled with zero before passing to %TUNSETVNETHASH
+ * @types:
+ *             Bitmask of allowed hash types
+ */
+struct tun_vnet_hash {
+       __u16 flags;
+       __u8 pad[2];
+       __u32 types;
+};

Padding in the middle of the structure is not elegant. Any reason for this?

And hash->types seems never used.

Quoted 38 lines
include/uapi/linux/if_tun.h +13 -0
+
+/**
+ * struct tun_vnet_hash_rss - virtio_net RSS configuration
+ * @indirection_table_mask:
+ *             Bitmask to be applied to the indirection table index
+ * @unclassified_queue:
+ *             The index of the queue to place unclassified packets in
+ */
+struct tun_vnet_hash_rss {
+       __u16 indirection_table_mask;
+       __u16 unclassified_queue;
+};
+
 #endif /* _UAPI__IF_TUN_H */
net/core/skbuff.c +4 -1
index 7b03b64fdcb276f68ce881d1d8da8e4c6b897efc..aa2a091b649f0c9d6e0196f34f345ba78b5498fb 100644
@@ -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)
 
--
Quoted 2 lines

2.48.1

Thanks

#13 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 43 lines · Jason Wang
Quoted 41 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
 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, 386 insertions(+), 31 deletions(-)

[...]

Quoted 10 lines
Quoted 4 lines

+ *
+ * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
+ * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
+ * to the size of &struct virtio_net_hdr_v1_hash.

So you had a dependency check already for vnet hdr len. I'd still suggest to split this into rss and hash as they are separated features. Then we can use separate data structure for them instead of a container struct.

Besides this, I think we still need to add new bits to TUNGETIFF to let userspace know about the new ability.

Thanks

#14 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 52 lines · Jason Wang
Quoted 43 lines · Jason Wang
Quoted 41 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
  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, 386 insertions(+), 31 deletions(-)

[...]

Quoted 5 lines
Quoted 4 lines

+ *
+ * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
+ * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
+ * to the size of &struct virtio_net_hdr_v1_hash.

Quoted 8 lines
Quoted 5 lines

So you had a dependency check already for vnet hdr len. I'd still suggest to split this into rss and hash as they are separated features. Then we can use separate data structure for them instead of a container struct.

Besides this, I think we still need to add new bits to TUNGETIFF to let userspace know about the new ability.

The userspace can peform TUNGETVNETHASHCAP and see if it results in EINVAL.

Regards,
Akihiko Odaki

Quoted 3 lines

Thanks

#15 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

In reply to Jason Wang
Quoted 453 lines · Jason Wang
Quoted 425 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
  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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -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
index 1fd5acdc73c6af0e1a861867039c3624fc618e25..aecfd244dd83585fea2c5b815dcd787c58166c28 100644
@@ -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 +65 -3
index d4ece538f1b23789ca60caa6232690e4d0a4d14a..9428b63ec27e7f92e78a78afcb5e24383862c00d 100644
@@ -49,6 +49,10 @@ struct major_info {
         struct list_head next;
  };
 
+struct tap_skb_cb {
+       struct virtio_net_hash hash;
+};
+
  #define GOODCOPY_LEN 128
 
  static const struct proto_ops tap_socket_ops;
@@ -179,6 +183,22 @@ static void tap_put_queue(struct tap_queue *q)
         sock_put(&q->sk);
  }
 
+static struct tap_skb_cb *tap_skb_cb(const struct sk_buff *skb)
+{
+       BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct tap_skb_cb));
+       return (struct tap_skb_cb *)skb->cb;
+}
+
+static struct virtio_net_hash *tap_add_hash(struct sk_buff *skb)
+{
+       return &tap_skb_cb(skb)->hash;
+}
+
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return &tap_skb_cb(skb)->hash;
+}
+
  /*
   * 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
@@ -189,6 +209,7 @@ static void tap_put_queue(struct tap_queue *q)
  static struct tap_queue *tap_get_queue(struct tap_dev *tap,
                                        struct sk_buff *skb)
  {
+       struct flow_keys_basic keys_basic;
         struct tap_queue *queue = NULL;
         /* Access to taps array is protected by rcu, but access to numvtaps
          * isn't. Below we use it to lookup a queue, but treat it as a hint
@@ -196,17 +217,47 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
          * racing against queue removal.
          */
         int numvtaps = READ_ONCE(tap->numvtaps);
+       struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash);
         __u32 rxq;
 
+       *tap_skb_cb(skb) = (struct tap_skb_cb) {
+               .hash = { .report = VIRTIO_NET_HASH_REPORT_NONE }
+       };
+
         if (!numvtaps)
                 goto out;
 
         if (numvtaps == 1)
                 goto single;
 
+       if (vnet_hash) {
+               if ((vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash);
+                       queue = rcu_dereference(tap->taps[rxq]);
+                       goto out;
+               }
+
+               if (!skb->l4_hash && !skb->sw_hash) {
+                       struct flow_keys keys;
+
+                       skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+                       rxq = flow_hash_from_keys(&keys);
+                       keys_basic = (struct flow_keys_basic) {
+                               .control = keys.control,
+                               .basic = keys.basic
+                       };
+               } else {
+                       skb_flow_dissect_flow_keys_basic(NULL, skb, &keys_basic, NULL, 0, 0, 0,
+                                                        FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+                       rxq = skb->hash;
+               }
+       } else {
+               rxq = skb_get_hash(skb);
+       }
+
         /* Check if we can use flow to select a queue */
-       rxq = skb_get_hash(skb);
         if (rxq) {
+               tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
                 queue = rcu_dereference(tap->taps[rxq % numvtaps]);
                 goto out;
         }
@@ -711,11 +762,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;
 
@@ -992,6 +1044,16 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
                 rtnl_unlock();
                 return ret;
 
+       case TUNGETVNETHASHCAP:
+               return tun_vnet_ioctl_gethashcap(argp);
+
+       case TUNSETVNETHASH:
+               rtnl_lock();
+               tap = rtnl_dereference(q->tap);
+               ret = tap ? tun_vnet_ioctl_sethash(&tap->vnet_hash, true, argp) : -EBADFD;
+               rtnl_unlock();
+               return ret;
+
         case SIOCGIFHWADDR:
                 rtnl_lock();
                 tap = tap_get_tap_dev(q);
drivers/net/tun.c +79 -19
index d8f4d3e996a7a81d1f8b04635054081671a14f07..520013df416e93d3a50b46be9b53ae9ab410eab4 100644
@@ -209,6 +209,7 @@ struct tun_struct {
         struct bpf_prog __rcu *xdp_prog;
         struct tun_prog __rcu *steering_prog;
         struct tun_prog __rcu *filter_prog;
+       struct tun_vnet_hash_container __rcu *vnet_hash;
         struct ethtool_link_ksettings link_ksettings;
         /* init args */
         struct file *file;
@@ -451,20 +452,37 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                 e->rps_rxhash = hash;
  }
 
+static struct virtio_net_hash *tun_add_hash(struct sk_buff *skb)
+{
+       return skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
  /* 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
   * the userspace application move between processors, we may get a
   * different rxq no. here.
   */
-static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static u16 tun_automq_select_queue(struct tun_struct *tun,
+                                  const struct tun_vnet_hash_container *vnet_hash,
+                                  struct sk_buff *skb)
  {
+       struct flow_keys keys;
+       struct flow_keys_basic keys_basic;
         struct tun_flow_entry *e;
         u32 txq, numqueues;
 
         numqueues = READ_ONCE(tun->numqueues);
 
-       txq = __skb_get_hash_symmetric(skb);
+       memset(&keys, 0, sizeof(keys));
+       skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, 0);
+
+       txq = flow_hash_from_keys(&keys);
         e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
         if (e) {
                 tun_flow_save_rps_rxhash(e, txq);
@@ -473,6 +491,13 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
                 txq = reciprocal_scale(txq, numqueues);
         }
 
+       keys_basic = (struct flow_keys_basic) {
+               .control = keys.control,
+               .basic = keys.basic
+       };
+       tun_vnet_hash_report(vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+                            tun_add_hash);
+
         return txq;
  }
 
@@ -500,10 +525,17 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
         u16 ret;
 
         rcu_read_lock();
-       if (rcu_dereference(tun->steering_prog))
+       if (rcu_dereference(tun->steering_prog)) {
                 ret = tun_ebpf_select_queue(tun, skb);
-       else
-               ret = tun_automq_select_queue(tun, skb);
+       } else {
+               struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tun->vnet_hash);
+
+               if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS))
+                       ret = tun_vnet_rss_select_queue(READ_ONCE(tun->numqueues), vnet_hash,
+                                                       skb, tun_add_hash);
+               else
+                       ret = tun_automq_select_queue(tun, vnet_hash, skb);
+       }
         rcu_read_unlock();
 
         return ret;
@@ -1987,7 +2019,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);
@@ -2040,9 +2072,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;
 
@@ -2223,6 +2256,7 @@ static void tun_free_netdev(struct net_device *dev)
         security_tun_dev_free_security(tun->security);
         __tun_set_ebpf(tun, &tun->steering_prog, NULL);
         __tun_set_ebpf(tun, &tun->filter_prog, NULL);
+       kfree_rcu_mightsleep(rcu_access_pointer(tun->vnet_hash));
  }
 
  static void tun_setup(struct net_device *dev)
@@ -2921,13 +2955,9 @@ static int tun_set_queue(struct file *file, struct ifreq *ifr)
  }
 
  static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
-                       void __user *data)
+                       int fd)
  {
         struct bpf_prog *prog;
-       int fd;
-
-       if (copy_from_user(&fd, data, sizeof(fd)))
-               return -EFAULT;
 
         if (fd == -1) {
                 prog = NULL;
@@ -2993,7 +3023,9 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
         int ifindex;
         int sndbuf;
         int ret;
+       int fd;
         bool do_notify = false;
+       struct tun_vnet_hash_container *vnet_hash;
 
         if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
             (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
@@ -3020,7 +3052,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
         rtnl_lock();
 
         tun = tun_get(tfile);
-       if (cmd == TUNSETIFF) {
+       switch (cmd) {
+       case TUNSETIFF:
                 ret = -EEXIST;
                 if (tun)
                         goto unlock;
@@ -3035,8 +3068,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 if (copy_to_user(argp, &ifr, ifreq_len))
                         ret = -EFAULT;
                 goto unlock;
-       }
-       if (cmd == TUNSETIFINDEX) {
+
+       case TUNSETIFINDEX:
                 ret = -EPERM;
                 if (tun)
                         goto unlock;
@@ -3050,6 +3083,10 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 ret = 0;
                 tfile->ifindex = ifindex;
                 goto unlock;
+
+       case TUNGETVNETHASHCAP:
+               ret = tun_vnet_ioctl_gethashcap(argp);
+               goto unlock;
         }
 
         ret = -EBADFD;
@@ -3230,11 +3267,27 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 break;
 
         case TUNSETSTEERINGEBPF:
-               ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
+               if (get_user(fd, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               vnet_hash = rtnl_dereference(tun->vnet_hash);
+               if (fd != -1 && vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       ret = -EBUSY;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->steering_prog, fd);
                 break;
 
         case TUNSETFILTEREBPF:
-               ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
+               if (get_user(fd, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->filter_prog, fd);
                 break;
 
         case TUNSETCARRIER:
@@ -3252,8 +3305,15 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 ret = open_related_ns(&net->ns, get_net_ns);
                 break;
 
+       case TUNSETVNETHASH:
+               ret = tun_vnet_ioctl_sethash(&tun->vnet_hash,
+                                            !rtnl_dereference(tun->steering_prog),
+                                            argp);
+               break;
+
         default:
-               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags, cmd, argp);
+               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
+                                    cmd, argp);
                 break;
         }
 
drivers/net/tun_vnet.h +0 -0
index 58b9ac7a5fc4084c789fe94fe36b5f8631bf1fa4..8e7d51fb0b4742cef56e7c5ad778b156cc654bed 100644

drivers/net/tun_vnet.h line 13

Quoted 8 lines
drivers/net/tun_vnet.h +5 -0
@@ -6,6 +6,16 @@
  #define TUN_VNET_LE     0x80000000
  #define TUN_VNET_BE     0x40000000
 
+typedef struct virtio_net_hash *(*tun_vnet_hash_add)(struct sk_buff *);
+typedef const struct virtio_net_hash *(*tun_vnet_hash_find)(const struct sk_buff *);
+
+struct tun_vnet_hash_container {
+       struct tun_vnet_hash common;

I'd rename this as hash.

drivers/net/tun_vnet.h line 17

Quoted 4 lines
drivers/net/tun_vnet.h +4 -0
+       struct tun_vnet_hash_rss rss;
+       u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
+       u16 rss_indirection_table[];
+};

Besides the separated ioctl, I'd split this structure into rss and hash part as well.

Quoted 8 lines
drivers/net/tun_vnet.h +1 -0
+
  static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
  {
         bool be = IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) &&
@@ -107,6 +117,123 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
         }
  }
 
Quoted 12 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 127

Quoted 8 lines
drivers/net/tun_vnet.h +8 -0
+static inline long tun_vnet_ioctl_gethashcap(void __user *argp)
+{
+       static const struct tun_vnet_hash cap = {
+               .flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS,
+               .types = VIRTIO_NET_SUPPORTED_HASH_TYPES
+       };
+
+       return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;

Let's has a consistent name for this and the uapi to be consistent with TUNSETIFF/TUNGETIFF. Probably TUNSETVNETHASH and tun_vnet_ioctl_gethash().

They have different semantics so they should have different names. TUNGETIFF reports the value currently set while TUNGETVNETHASHCAP reports the value that can be set later.

Quoted 11 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 131

Quoted 5 lines
drivers/net/tun_vnet.h +4 -0
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

We don't even have an interface for eBPF to let it fall back to another alogirhtm. I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 52 lines
Quoted 39 lines
drivers/net/tun_vnet.h +38 -0
+{
+       struct tun_vnet_hash hash_buf;
+       struct tun_vnet_hash_container *hash;
+
+       if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
+               return -EFAULT;
+       argp = (struct tun_vnet_hash __user *)argp + 1;
+
+       if (hash_buf.flags & TUN_VNET_HASH_RSS) {
+               struct tun_vnet_hash_rss rss;
+               size_t indirection_table_size;
+               size_t key_size;
+               size_t size;
+
+               if (!can_rss)
+                       return -EBUSY;
+
+               if (copy_from_user(&rss, argp, sizeof(rss)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_hash_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)rss.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(hash_buf.types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)rss.indirection_table_mask + 1);
+
+               hash = kmalloc(size, GFP_KERNEL);
+               if (!hash)
+                       return -ENOMEM;
+
+               if (copy_from_user(hash->rss_indirection_table,
+                                  argp, indirection_table_size)) {
+                       kfree(hash);
+                       return -EFAULT;
+               }
+               argp = (u16 __user *)argp + rss.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 179

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+                       kfree(hash);
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->rss = rss;
+       } else {
+               hash = kmalloc(sizeof(hash->common), GFP_KERNEL);
+               if (!hash)
+                       return -ENOMEM;

Do we need to validate the hash here (at least against the types we supported?)

Quoted 8 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 183

Quoted 4 lines
drivers/net/tun_vnet.h +4 -0
+       }
+
+       hash->common = hash_buf;
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));

I still didn't understand the trick here. E.g we use very simple primitives in synchronizing ebpf program through RCU in __tun_set_ebpf().

It is even simpler than __tun_set_ebpf(). The differences from __tun_set_ebpf() are:
1. This uses the rtnl lock instead of TUN-specific one. It makes the code simpler as the rtnl lock is already taken in __tun_chr_ioctl().
2. This does not add rcu_head and uses blocking APIs for simplicity.

Quoted 18 lines
Quoted 18 lines
drivers/net/tun_vnet.h +17 -0
+       return 0;
+}
+
+static void tun_vnet_hash_report(const struct tun_vnet_hash_container *hash,
+                                struct sk_buff *skb,
+                                const struct flow_keys_basic *keys,
+                                u32 value,
+                                tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+
+       if (!hash || !(hash->common.flags & TUN_VNET_HASH_REPORT))
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
Quoted 8 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 205

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.types, keys),
+               .value = value
+       };

What's the advantage of using Designated Initializers here? Simple assignment can save two lines of code.

It automatically fills other fileds with zero. Simple assignments will need more tokens for zeroing.

Quoted 20 lines
Quoted 20 lines
drivers/net/tun_vnet.h +19 -0
+}
+
+static u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                    const struct tun_vnet_hash_container *hash,
+                                    struct sk_buff *skb,
+                                    tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 txq, index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->rss.unclassified_queue % numqueues;
+
Quoted 8 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 229

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+       if (hash->common.flags & TUN_VNET_HASH_REPORT) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT. It is more of a feature instead of a bug; it behaves like QEMU's eBPF program but requires no privilege and is more optimized with native code and ffs().

Quoted 9 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 232

Quoted 4 lines
drivers/net/tun_vnet.h +3 -0
+
+       index = ret.value & hash->rss.indirection_table_mask;
+       txq = READ_ONCE(hash->rss_indirection_table[index]);

So vnet_hash is accessed via rcu_dereference(), I don't get any reason we need READ_ONCE here, is this paired with something? If yes, let's add a comment here. If rss_indirection_table need why indirection_table_mask doesn't need this?

I'll drop it. I think it's just a left-over of previous versions without RCU.

Quoted 11 lines
Quoted 11 lines
drivers/net/tun_vnet.h +4 -0
+
+       return txq % numqueues;
+}
+
  static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                    struct iov_iter *from,
                                    struct virtio_net_hdr *hdr)
@@ -135,15 +262,17 @@ static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
  }
 
Quoted 8 lines

drivers/net/tun_vnet.h static inline int tun_vnet_hdr_get(int sz, unsigned int flags, · line 266

Quoted 4 lines
drivers/net/tun_vnet.h +1 -1
  static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-                                  const struct virtio_net_hdr *hdr)
+                                  const struct virtio_net_hdr_v1_hash *hdr)
  {

To be more robust, we can tweak the function to accept a vnet_hdr_len parameter then we can avoid touching this every time when we need to extend vnet hdr in the future?

I think you meant vnet_hdr_sz instead of vnet_hdr_len. It is already passed just as "sz" here as the function name already says it's about the header.

It is possible to add another parameter for sizeof(*hdr) and convert the hdr parameter to void * to avoid future changes. But I rather keep it as is because the current form ensures the hdr is large enough and statically avoids buffer overrun.

Quoted 123 lines
Quoted 123 lines
drivers/net/tun_vnet.h +22 -8
+       int content_sz = MIN(sizeof(*hdr), sz);
+
         if (unlikely(iov_iter_count(iter) < sz))
                 return -EINVAL;
 
-       if (unlikely(copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr)))
+       if (unlikely(copy_to_iter(hdr, content_sz, iter) != content_sz))
                 return -EFAULT;
 
-       if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+       if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
                 return -EFAULT;
 
         return 0;
@@ -155,26 +284,38 @@ static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
         return virtio_net_hdr_to_skb(skb, hdr, tun_vnet_is_little_endian(flags));
  }
 
-static inline int tun_vnet_hdr_from_skb(unsigned int flags,
+static inline int tun_vnet_hdr_from_skb(int sz, unsigned int flags,
                                         const struct net_device *dev,
                                         const struct sk_buff *skb,
-                                       struct virtio_net_hdr *hdr)
+                                       tun_vnet_hash_find vnet_hash_find,
+                                       struct virtio_net_hdr_v1_hash *hdr)
  {
         int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
+       const struct virtio_net_hash *report = sz < sizeof(struct virtio_net_hdr_v1_hash) ?
+                                              NULL : vnet_hash_find(skb);
+
+       *hdr = (struct virtio_net_hdr_v1_hash) {
+               .hash_report = VIRTIO_NET_HASH_REPORT_NONE
+       };
+
+       if (report) {
+               hdr->hash_value = cpu_to_le32(report->value);
+               hdr->hash_report = cpu_to_le16(report->report);
+       }
 
-       if (virtio_net_hdr_from_skb(skb, hdr,
+       if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
                                     tun_vnet_is_little_endian(flags), true,
                                     vlan_hlen)) {
                 struct skb_shared_info *sinfo = skb_shinfo(skb);
 
                 if (net_ratelimit()) {
                         netdev_err(dev, "unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n",
-                                  sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->gso_size),
-                                  tun_vnet16_to_cpu(flags, hdr->hdr_len));
+                                  sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->hdr.gso_size),
+                                  tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len));
                         print_hex_dump(KERN_ERR, "tun: ",
                                        DUMP_PREFIX_NONE,
                                        16, 1, skb->head,
-                                      min(tun_vnet16_to_cpu(flags, hdr->hdr_len), 64), true);
+                                      min(tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len), 64), true);
                 }
                 WARN_ON_ONCE(1);
                 return -EINVAL;
include/linux/if_tap.h +2 -0
index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..7334c46a3f101675a0d4e5a036987cfe18842f9f 100644
@@ -31,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
  #define MAX_TAP_QUEUES 256
 
  struct 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
index bb2b751d274acff931281a72e8b4b0c699b4e8af..cdd793f1c360ad5f63fcc4cbf67d845f5e2ccf6f 100644
@@ -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 +21 -0
index 287cdc81c9390c289a30545aa7ed23d81c3329d3..4887f97500a870c7ef3c96a5837b2d0a5a225040 100644
@@ -62,6 +62,42 @@
  #define TUNSETCARRIER _IOW('T', 226, int)
  #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHCAP - ioctl to get virtio_net hashing capability.
+ *
+ * The argument is a pointer to &struct tun_vnet_hash which will store the
+ * maximal virtio_net hashing configuration.
+ */
+#define TUNGETVNETHASHCAP _IOR('T', 228, struct tun_vnet_hash)
+
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
Quoted 8 lines

include/uapi/linux/if_tun.h line 88

Quoted 3 lines
include/uapi/linux/if_tun.h +3 -0
+ * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
+ * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
+ * to the size of &struct virtio_net_hdr_v1_hash.

So you had a dependency check already for vnet hdr len. I'd still suggest to split this into rss and hash as they are separated features. Then we can use separate data structure for them instead of a container struct.

I added a dependency check and found it is complicating the code and requires additional tests. I need a reason to justify the complexity if we are going to split it.

Regards,
Akihiko Odaki

Quoted 94 lines
Quoted 36 lines
include/uapi/linux/if_tun.h +28 -0
+ *
+ * The members added to the legacy header by %TUN_VNET_HASH_REPORT flag will
+ * always be little-endian.
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.
+ */
+#define TUNSETVNETHASH _IOW('T', 229, struct tun_vnet_hash)
+
  /* TUNSETIFF ifr flags */
  #define IFF_TUN                0x0001
  #define IFF_TAP                0x0002
@@ -115,4 +151,43 @@ struct tun_filter {
         __u8   addr[][ETH_ALEN];
  };
 
+/**
+ * define TUN_VNET_HASH_REPORT - Request virtio_net hash reporting for vhost
+ */
+#define TUN_VNET_HASH_REPORT   0x0001
+
+/**
+ * define TUN_VNET_HASH_RSS - Request virtio_net RSS
+ *
+ * This is mutually exclusive with eBPF steering program.
+ */
+#define TUN_VNET_HASH_RSS      0x0002
+
+/**
+ * struct tun_vnet_hash - virtio_net hashing configuration
+ * @flags:
+ *             Bitmask consists of %TUN_VNET_HASH_REPORT and %TUN_VNET_HASH_RSS

include/uapi/linux/if_tun.h struct tun_filter { · line 179

Quoted 10 lines
include/uapi/linux/if_tun.h +10 -0
+ * @pad:
+ *             Should be filled with zero before passing to %TUNSETVNETHASH
+ * @types:
+ *             Bitmask of allowed hash types
+ */
+struct tun_vnet_hash {
+       __u16 flags;
+       __u8 pad[2];
+       __u32 types;
+};

Padding in the middle of the structure is not elegant. Any reason for this?

And hash->types seems never used.

Quoted 38 lines
include/uapi/linux/if_tun.h +13 -0
+
+/**
+ * struct tun_vnet_hash_rss - virtio_net RSS configuration
+ * @indirection_table_mask:
+ *             Bitmask to be applied to the indirection table index
+ * @unclassified_queue:
+ *             The index of the queue to place unclassified packets in
+ */
+struct tun_vnet_hash_rss {
+       __u16 indirection_table_mask;
+       __u16 unclassified_queue;
+};
+
  #endif /* _UAPI__IF_TUN_H */
net/core/skbuff.c +4 -1
index 7b03b64fdcb276f68ce881d1d8da8e4c6b897efc..aa2a091b649f0c9d6e0196f34f345ba78b5498fb 100644
@@ -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)
 
--
Quoted 2 lines

2.48.1

Thanks

#16 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 468 lines · Akihiko Odaki
Quoted 453 lines · Jason Wang
Quoted 425 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
  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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -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
index 1fd5acdc73c6af0e1a861867039c3624fc618e25..aecfd244dd83585fea2c5b815dcd787c58166c28 100644
@@ -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 +65 -3
index d4ece538f1b23789ca60caa6232690e4d0a4d14a..9428b63ec27e7f92e78a78afcb5e24383862c00d 100644
@@ -49,6 +49,10 @@ struct major_info {
         struct list_head next;
  };
 
+struct tap_skb_cb {
+       struct virtio_net_hash hash;
+};
+
  #define GOODCOPY_LEN 128
 
  static const struct proto_ops tap_socket_ops;
@@ -179,6 +183,22 @@ static void tap_put_queue(struct tap_queue *q)
         sock_put(&q->sk);
  }
 
+static struct tap_skb_cb *tap_skb_cb(const struct sk_buff *skb)
+{
+       BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct tap_skb_cb));
+       return (struct tap_skb_cb *)skb->cb;
+}
+
+static struct virtio_net_hash *tap_add_hash(struct sk_buff *skb)
+{
+       return &tap_skb_cb(skb)->hash;
+}
+
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return &tap_skb_cb(skb)->hash;
+}
+
  /*
   * 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
@@ -189,6 +209,7 @@ static void tap_put_queue(struct tap_queue *q)
  static struct tap_queue *tap_get_queue(struct tap_dev *tap,
                                        struct sk_buff *skb)
  {
+       struct flow_keys_basic keys_basic;
         struct tap_queue *queue = NULL;
         /* Access to taps array is protected by rcu, but access to numvtaps
          * isn't. Below we use it to lookup a queue, but treat it as a hint
@@ -196,17 +217,47 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
          * racing against queue removal.
          */
         int numvtaps = READ_ONCE(tap->numvtaps);
+       struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash);
         __u32 rxq;
 
+       *tap_skb_cb(skb) = (struct tap_skb_cb) {
+               .hash = { .report = VIRTIO_NET_HASH_REPORT_NONE }
+       };
+
         if (!numvtaps)
                 goto out;
 
         if (numvtaps == 1)
                 goto single;
 
+       if (vnet_hash) {
+               if ((vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash);
+                       queue = rcu_dereference(tap->taps[rxq]);
+                       goto out;
+               }
+
+               if (!skb->l4_hash && !skb->sw_hash) {
+                       struct flow_keys keys;
+
+                       skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+                       rxq = flow_hash_from_keys(&keys);
+                       keys_basic = (struct flow_keys_basic) {
+                               .control = keys.control,
+                               .basic = keys.basic
+                       };
+               } else {
+                       skb_flow_dissect_flow_keys_basic(NULL, skb, &keys_basic, NULL, 0, 0, 0,
+                                                        FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+                       rxq = skb->hash;
+               }
+       } else {
+               rxq = skb_get_hash(skb);
+       }
+
         /* Check if we can use flow to select a queue */
-       rxq = skb_get_hash(skb);
         if (rxq) {
+               tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
                 queue = rcu_dereference(tap->taps[rxq % numvtaps]);
                 goto out;
         }
@@ -711,11 +762,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;
 
@@ -992,6 +1044,16 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
                 rtnl_unlock();
                 return ret;
 
+       case TUNGETVNETHASHCAP:
+               return tun_vnet_ioctl_gethashcap(argp);
+
+       case TUNSETVNETHASH:
+               rtnl_lock();
+               tap = rtnl_dereference(q->tap);
+               ret = tap ? tun_vnet_ioctl_sethash(&tap->vnet_hash, true, argp) : -EBADFD;
+               rtnl_unlock();
+               return ret;
+
         case SIOCGIFHWADDR:
                 rtnl_lock();
                 tap = tap_get_tap_dev(q);
drivers/net/tun.c +79 -19
index d8f4d3e996a7a81d1f8b04635054081671a14f07..520013df416e93d3a50b46be9b53ae9ab410eab4 100644
@@ -209,6 +209,7 @@ struct tun_struct {
         struct bpf_prog __rcu *xdp_prog;
         struct tun_prog __rcu *steering_prog;
         struct tun_prog __rcu *filter_prog;
+       struct tun_vnet_hash_container __rcu *vnet_hash;
         struct ethtool_link_ksettings link_ksettings;
         /* init args */
         struct file *file;
@@ -451,20 +452,37 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                 e->rps_rxhash = hash;
  }
 
+static struct virtio_net_hash *tun_add_hash(struct sk_buff *skb)
+{
+       return skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
  /* 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
   * the userspace application move between processors, we may get a
   * different rxq no. here.
   */
-static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static u16 tun_automq_select_queue(struct tun_struct *tun,
+                                  const struct tun_vnet_hash_container *vnet_hash,
+                                  struct sk_buff *skb)
  {
+       struct flow_keys keys;
+       struct flow_keys_basic keys_basic;
         struct tun_flow_entry *e;
         u32 txq, numqueues;
 
         numqueues = READ_ONCE(tun->numqueues);
 
-       txq = __skb_get_hash_symmetric(skb);
+       memset(&keys, 0, sizeof(keys));
+       skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, 0);
+
+       txq = flow_hash_from_keys(&keys);
         e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
         if (e) {
                 tun_flow_save_rps_rxhash(e, txq);
@@ -473,6 +491,13 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
                 txq = reciprocal_scale(txq, numqueues);
         }
 
+       keys_basic = (struct flow_keys_basic) {
+               .control = keys.control,
+               .basic = keys.basic
+       };
+       tun_vnet_hash_report(vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+                            tun_add_hash);
+
         return txq;
  }
 
@@ -500,10 +525,17 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
         u16 ret;
 
         rcu_read_lock();
-       if (rcu_dereference(tun->steering_prog))
+       if (rcu_dereference(tun->steering_prog)) {
                 ret = tun_ebpf_select_queue(tun, skb);
-       else
-               ret = tun_automq_select_queue(tun, skb);
+       } else {
+               struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tun->vnet_hash);
+
+               if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS))
+                       ret = tun_vnet_rss_select_queue(READ_ONCE(tun->numqueues), vnet_hash,
+                                                       skb, tun_add_hash);
+               else
+                       ret = tun_automq_select_queue(tun, vnet_hash, skb);
+       }
         rcu_read_unlock();
 
         return ret;
@@ -1987,7 +2019,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);
@@ -2040,9 +2072,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;
 
@@ -2223,6 +2256,7 @@ static void tun_free_netdev(struct net_device *dev)
         security_tun_dev_free_security(tun->security);
         __tun_set_ebpf(tun, &tun->steering_prog, NULL);
         __tun_set_ebpf(tun, &tun->filter_prog, NULL);
+       kfree_rcu_mightsleep(rcu_access_pointer(tun->vnet_hash));
  }
 
  static void tun_setup(struct net_device *dev)
@@ -2921,13 +2955,9 @@ static int tun_set_queue(struct file *file, struct ifreq *ifr)
  }
 
  static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
-                       void __user *data)
+                       int fd)
  {
         struct bpf_prog *prog;
-       int fd;
-
-       if (copy_from_user(&fd, data, sizeof(fd)))
-               return -EFAULT;
 
         if (fd == -1) {
                 prog = NULL;
@@ -2993,7 +3023,9 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
         int ifindex;
         int sndbuf;
         int ret;
+       int fd;
         bool do_notify = false;
+       struct tun_vnet_hash_container *vnet_hash;
 
         if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
             (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
@@ -3020,7 +3052,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
         rtnl_lock();
 
         tun = tun_get(tfile);
-       if (cmd == TUNSETIFF) {
+       switch (cmd) {
+       case TUNSETIFF:
                 ret = -EEXIST;
                 if (tun)
                         goto unlock;
@@ -3035,8 +3068,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 if (copy_to_user(argp, &ifr, ifreq_len))
                         ret = -EFAULT;
                 goto unlock;
-       }
-       if (cmd == TUNSETIFINDEX) {
+
+       case TUNSETIFINDEX:
                 ret = -EPERM;
                 if (tun)
                         goto unlock;
@@ -3050,6 +3083,10 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 ret = 0;
                 tfile->ifindex = ifindex;
                 goto unlock;
+
+       case TUNGETVNETHASHCAP:
+               ret = tun_vnet_ioctl_gethashcap(argp);
+               goto unlock;
         }
 
         ret = -EBADFD;
@@ -3230,11 +3267,27 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 break;
 
         case TUNSETSTEERINGEBPF:
-               ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
+               if (get_user(fd, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               vnet_hash = rtnl_dereference(tun->vnet_hash);
+               if (fd != -1 && vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       ret = -EBUSY;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->steering_prog, fd);
                 break;
 
         case TUNSETFILTEREBPF:
-               ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
+               if (get_user(fd, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->filter_prog, fd);
                 break;
 
         case TUNSETCARRIER:
@@ -3252,8 +3305,15 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 ret = open_related_ns(&net->ns, get_net_ns);
                 break;
 
+       case TUNSETVNETHASH:
+               ret = tun_vnet_ioctl_sethash(&tun->vnet_hash,
+                                            !rtnl_dereference(tun->steering_prog),
+                                            argp);
+               break;
+
         default:
-               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags, cmd, argp);
+               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
+                                    cmd, argp);
                 break;
         }
 
drivers/net/tun_vnet.h +0 -0
index 58b9ac7a5fc4084c789fe94fe36b5f8631bf1fa4..8e7d51fb0b4742cef56e7c5ad778b156cc654bed 100644

drivers/net/tun_vnet.h line 13

Quoted 8 lines
drivers/net/tun_vnet.h +5 -0
@@ -6,6 +6,16 @@
  #define TUN_VNET_LE     0x80000000
  #define TUN_VNET_BE     0x40000000
 
+typedef struct virtio_net_hash *(*tun_vnet_hash_add)(struct sk_buff *);
+typedef const struct virtio_net_hash *(*tun_vnet_hash_find)(const struct sk_buff *);
+
+struct tun_vnet_hash_container {
+       struct tun_vnet_hash common;

I'd rename this as hash.

drivers/net/tun_vnet.h line 17

Quoted 4 lines
drivers/net/tun_vnet.h +4 -0
+       struct tun_vnet_hash_rss rss;
+       u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
+       u16 rss_indirection_table[];
+};

Besides the separated ioctl, I'd split this structure into rss and hash part as well.

Quoted 8 lines
drivers/net/tun_vnet.h +1 -0
+
  static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
  {
         bool be = IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) &&
@@ -107,6 +117,123 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
         }
  }
 
Quoted 12 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 127

Quoted 8 lines
drivers/net/tun_vnet.h +8 -0
+static inline long tun_vnet_ioctl_gethashcap(void __user *argp)
+{
+       static const struct tun_vnet_hash cap = {
+               .flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS,
+               .types = VIRTIO_NET_SUPPORTED_HASH_TYPES
+       };
+
+       return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;

Let's has a consistent name for this and the uapi to be consistent with TUNSETIFF/TUNGETIFF. Probably TUNSETVNETHASH and tun_vnet_ioctl_gethash().

Quoted 3 lines

They have different semantics so they should have different names. TUNGETIFF reports the value currently set while TUNGETVNETHASHCAP reports the value that can be set later.

I'm not sure I will get here. I meant a symmetric name

TUNSETVNETHASH and TUNVETVNETHASH.

Quoted 12 lines
Quoted 12 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 131

Quoted 6 lines
drivers/net/tun_vnet.h +4 -0
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 6 lines
Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

Quoted 3 lines

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

Quoted 58 lines
Quoted 58 lines
Quoted 40 lines
drivers/net/tun_vnet.h +38 -0
+{
+       struct tun_vnet_hash hash_buf;
+       struct tun_vnet_hash_container *hash;
+
+       if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
+               return -EFAULT;
+       argp = (struct tun_vnet_hash __user *)argp + 1;
+
+       if (hash_buf.flags & TUN_VNET_HASH_RSS) {
+               struct tun_vnet_hash_rss rss;
+               size_t indirection_table_size;
+               size_t key_size;
+               size_t size;
+
+               if (!can_rss)
+                       return -EBUSY;
+
+               if (copy_from_user(&rss, argp, sizeof(rss)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_hash_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)rss.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(hash_buf.types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)rss.indirection_table_mask + 1);
+
+               hash = kmalloc(size, GFP_KERNEL);
+               if (!hash)
+                       return -ENOMEM;
+
+               if (copy_from_user(hash->rss_indirection_table,
+                                  argp, indirection_table_size)) {
+                       kfree(hash);
+                       return -EFAULT;
+               }
+               argp = (u16 __user *)argp + rss.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 179

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+                       kfree(hash);
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->rss = rss;
+       } else {
+               hash = kmalloc(sizeof(hash->common), GFP_KERNEL);
+               if (!hash)
+                       return -ENOMEM;

Do we need to validate the hash here (at least against the types we supported?)

Quoted 4 lines
drivers/net/tun_vnet.h +4 -0
+       }
+
+       hash->common = hash_buf;
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
Quoted 8 lines
Quoted 3 lines

I still didn't understand the trick here. E.g we use very simple primitives in synchronizing ebpf program through RCU in __tun_set_ebpf().

It is even simpler than __tun_set_ebpf(). The differences from __tun_set_ebpf() are:
1. This uses the rtnl lock instead of TUN-specific one. It makes the code simpler as the rtnl lock is already taken in __tun_chr_ioctl().

It can be tweaked to use rtnl as well.

Quoted 1 line

2. This does not add rcu_head and uses blocking APIs for simplicity.

Right.

Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
drivers/net/tun_vnet.h +17 -0
+       return 0;
+}
+
+static void tun_vnet_hash_report(const struct tun_vnet_hash_container *hash,
+                                struct sk_buff *skb,
+                                const struct flow_keys_basic *keys,
+                                u32 value,
+                                tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+
+       if (!hash || !(hash->common.flags & TUN_VNET_HASH_REPORT))
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
Quoted 8 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 205

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.types, keys),
+               .value = value
+       };

What's the advantage of using Designated Initializers here? Simple assignment can save two lines of code.

Quoted 2 lines

It automatically fills other fileds with zero. Simple assignments will need more tokens for zeroing.

Ok.

Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
drivers/net/tun_vnet.h +19 -0
+}
+
+static u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                    const struct tun_vnet_hash_container *hash,
+                                    struct sk_buff *skb,
+                                    tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 txq, index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->rss.unclassified_queue % numqueues;
+
Quoted 8 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 229

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+       if (hash->common.flags & TUN_VNET_HASH_REPORT) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

Quoted 1 line

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

Quoted 37 lines

It is more of a
feature instead of a bug; it behaves like QEMU's eBPF program but requires no privilege and is more optimized with native code and ffs().

Quoted 9 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 232

Quoted 4 lines
drivers/net/tun_vnet.h +3 -0
+
+       index = ret.value & hash->rss.indirection_table_mask;
+       txq = READ_ONCE(hash->rss_indirection_table[index]);

So vnet_hash is accessed via rcu_dereference(), I don't get any reason we need READ_ONCE here, is this paired with something? If yes, let's add a comment here. If rss_indirection_table need why indirection_table_mask doesn't need this?

I'll drop it. I think it's just a left-over of previous versions without RCU.

Quoted 11 lines
Quoted 11 lines
drivers/net/tun_vnet.h +4 -0
+
+       return txq % numqueues;
+}
+
  static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                    struct iov_iter *from,
                                    struct virtio_net_hdr *hdr)
@@ -135,15 +262,17 @@ static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
  }
 
Quoted 8 lines

drivers/net/tun_vnet.h static inline int tun_vnet_hdr_get(int sz, unsigned int flags, · line 266

Quoted 4 lines
drivers/net/tun_vnet.h +1 -1
  static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-                                  const struct virtio_net_hdr *hdr)
+                                  const struct virtio_net_hdr_v1_hash *hdr)
  {

To be more robust, we can tweak the function to accept a vnet_hdr_len parameter then we can avoid touching this every time when we need to extend vnet hdr in the future?

Quoted 8 lines

I think you meant vnet_hdr_sz instead of vnet_hdr_len. It is already passed just as "sz" here as the function name already says it's about the header.

It is possible to add another parameter for sizeof(*hdr) and convert the hdr parameter to void * to avoid future changes. But I rather keep it as is because the current form ensures the hdr is large enough and statically avoids buffer overrun.

Right.

Quoted 133 lines
Quoted 124 lines
Quoted 124 lines
drivers/net/tun_vnet.h +22 -8
+       int content_sz = MIN(sizeof(*hdr), sz);
+
         if (unlikely(iov_iter_count(iter) < sz))
                 return -EINVAL;
 
-       if (unlikely(copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr)))
+       if (unlikely(copy_to_iter(hdr, content_sz, iter) != content_sz))
                 return -EFAULT;
 
-       if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+       if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
                 return -EFAULT;
 
         return 0;
@@ -155,26 +284,38 @@ static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
         return virtio_net_hdr_to_skb(skb, hdr, tun_vnet_is_little_endian(flags));
  }
 
-static inline int tun_vnet_hdr_from_skb(unsigned int flags,
+static inline int tun_vnet_hdr_from_skb(int sz, unsigned int flags,
                                         const struct net_device *dev,
                                         const struct sk_buff *skb,
-                                       struct virtio_net_hdr *hdr)
+                                       tun_vnet_hash_find vnet_hash_find,
+                                       struct virtio_net_hdr_v1_hash *hdr)
  {
         int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
+       const struct virtio_net_hash *report = sz < sizeof(struct virtio_net_hdr_v1_hash) ?
+                                              NULL : vnet_hash_find(skb);
+
+       *hdr = (struct virtio_net_hdr_v1_hash) {
+               .hash_report = VIRTIO_NET_HASH_REPORT_NONE
+       };
+
+       if (report) {
+               hdr->hash_value = cpu_to_le32(report->value);
+               hdr->hash_report = cpu_to_le16(report->report);
+       }
 
-       if (virtio_net_hdr_from_skb(skb, hdr,
+       if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
                                     tun_vnet_is_little_endian(flags), true,
                                     vlan_hlen)) {
                 struct skb_shared_info *sinfo = skb_shinfo(skb);
 
                 if (net_ratelimit()) {
                         netdev_err(dev, "unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n",
-                                  sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->gso_size),
-                                  tun_vnet16_to_cpu(flags, hdr->hdr_len));
+                                  sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->hdr.gso_size),
+                                  tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len));
                         print_hex_dump(KERN_ERR, "tun: ",
                                        DUMP_PREFIX_NONE,
                                        16, 1, skb->head,
-                                      min(tun_vnet16_to_cpu(flags, hdr->hdr_len), 64), true);
+                                      min(tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len), 64), true);
                 }
                 WARN_ON_ONCE(1);
                 return -EINVAL;
include/linux/if_tap.h +2 -0
index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..7334c46a3f101675a0d4e5a036987cfe18842f9f 100644
@@ -31,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
  #define MAX_TAP_QUEUES 256
 
  struct 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
index bb2b751d274acff931281a72e8b4b0c699b4e8af..cdd793f1c360ad5f63fcc4cbf67d845f5e2ccf6f 100644
@@ -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 +21 -0
index 287cdc81c9390c289a30545aa7ed23d81c3329d3..4887f97500a870c7ef3c96a5837b2d0a5a225040 100644
@@ -62,6 +62,42 @@
  #define TUNSETCARRIER _IOW('T', 226, int)
  #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHCAP - ioctl to get virtio_net hashing capability.
+ *
+ * The argument is a pointer to &struct tun_vnet_hash which will store the
+ * maximal virtio_net hashing configuration.
+ */
+#define TUNGETVNETHASHCAP _IOR('T', 228, struct tun_vnet_hash)
+
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
Quoted 8 lines

include/uapi/linux/if_tun.h line 88

Quoted 3 lines
include/uapi/linux/if_tun.h +3 -0
+ * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
+ * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
+ * to the size of &struct virtio_net_hdr_v1_hash.

So you had a dependency check already for vnet hdr len. I'd still suggest to split this into rss and hash as they are separated features. Then we can use separate data structure for them instead of a container struct.

Quoted 3 lines

I added a dependency check and found it is complicating the code and requires additional tests. I need a reason to justify the complexity if we are going to split it.

As we discussed above: They don't dedpend on each other.

Quoted 99 lines

Regards,
Akihiko Odaki

Quoted 94 lines
Quoted 36 lines
include/uapi/linux/if_tun.h +28 -0
+ *
+ * The members added to the legacy header by %TUN_VNET_HASH_REPORT flag will
+ * always be little-endian.
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.
+ */
+#define TUNSETVNETHASH _IOW('T', 229, struct tun_vnet_hash)
+
  /* TUNSETIFF ifr flags */
  #define IFF_TUN                0x0001
  #define IFF_TAP                0x0002
@@ -115,4 +151,43 @@ struct tun_filter {
         __u8   addr[][ETH_ALEN];
  };
 
+/**
+ * define TUN_VNET_HASH_REPORT - Request virtio_net hash reporting for vhost
+ */
+#define TUN_VNET_HASH_REPORT   0x0001
+
+/**
+ * define TUN_VNET_HASH_RSS - Request virtio_net RSS
+ *
+ * This is mutually exclusive with eBPF steering program.
+ */
+#define TUN_VNET_HASH_RSS      0x0002
+
+/**
+ * struct tun_vnet_hash - virtio_net hashing configuration
+ * @flags:
+ *             Bitmask consists of %TUN_VNET_HASH_REPORT and %TUN_VNET_HASH_RSS

include/uapi/linux/if_tun.h struct tun_filter { · line 179

Quoted 10 lines
include/uapi/linux/if_tun.h +10 -0
+ * @pad:
+ *             Should be filled with zero before passing to %TUNSETVNETHASH
+ * @types:
+ *             Bitmask of allowed hash types
+ */
+struct tun_vnet_hash {
+       __u16 flags;
+       __u8 pad[2];
+       __u32 types;
+};

Padding in the middle of the structure is not elegant. Any reason for this?

And hash->types seems never used.

Quoted 38 lines
include/uapi/linux/if_tun.h +13 -0
+
+/**
+ * struct tun_vnet_hash_rss - virtio_net RSS configuration
+ * @indirection_table_mask:
+ *             Bitmask to be applied to the indirection table index
+ * @unclassified_queue:
+ *             The index of the queue to place unclassified packets in
+ */
+struct tun_vnet_hash_rss {
+       __u16 indirection_table_mask;
+       __u16 unclassified_queue;
+};
+
  #endif /* _UAPI__IF_TUN_H */
net/core/skbuff.c +4 -1
index 7b03b64fdcb276f68ce881d1d8da8e4c6b897efc..aa2a091b649f0c9d6e0196f34f345ba78b5498fb 100644
@@ -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)
 
--
Quoted 2 lines

2.48.1

Thanks

#17 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 504 lines · Jason Wang
Quoted 468 lines · Akihiko Odaki
Quoted 453 lines · Jason Wang
Quoted 425 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
   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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -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
index 1fd5acdc73c6af0e1a861867039c3624fc618e25..aecfd244dd83585fea2c5b815dcd787c58166c28 100644
@@ -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 +65 -3
index d4ece538f1b23789ca60caa6232690e4d0a4d14a..9428b63ec27e7f92e78a78afcb5e24383862c00d 100644
@@ -49,6 +49,10 @@ struct major_info {
          struct list_head next;
   };
 
+struct tap_skb_cb {
+       struct virtio_net_hash hash;
+};
+
   #define GOODCOPY_LEN 128
 
   static const struct proto_ops tap_socket_ops;
@@ -179,6 +183,22 @@ static void tap_put_queue(struct tap_queue *q)
          sock_put(&q->sk);
   }
 
+static struct tap_skb_cb *tap_skb_cb(const struct sk_buff *skb)
+{
+       BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct tap_skb_cb));
+       return (struct tap_skb_cb *)skb->cb;
+}
+
+static struct virtio_net_hash *tap_add_hash(struct sk_buff *skb)
+{
+       return &tap_skb_cb(skb)->hash;
+}
+
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return &tap_skb_cb(skb)->hash;
+}
+
   /*
    * 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
@@ -189,6 +209,7 @@ static void tap_put_queue(struct tap_queue *q)
   static struct tap_queue *tap_get_queue(struct tap_dev *tap,
                                         struct sk_buff *skb)
   {
+       struct flow_keys_basic keys_basic;
          struct tap_queue *queue = NULL;
          /* Access to taps array is protected by rcu, but access to numvtaps
           * isn't. Below we use it to lookup a queue, but treat it as a hint
@@ -196,17 +217,47 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
           * racing against queue removal.
           */
          int numvtaps = READ_ONCE(tap->numvtaps);
+       struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash);
          __u32 rxq;
 
+       *tap_skb_cb(skb) = (struct tap_skb_cb) {
+               .hash = { .report = VIRTIO_NET_HASH_REPORT_NONE }
+       };
+
          if (!numvtaps)
                  goto out;
 
          if (numvtaps == 1)
                  goto single;
 
+       if (vnet_hash) {
+               if ((vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash);
+                       queue = rcu_dereference(tap->taps[rxq]);
+                       goto out;
+               }
+
+               if (!skb->l4_hash && !skb->sw_hash) {
+                       struct flow_keys keys;
+
+                       skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+                       rxq = flow_hash_from_keys(&keys);
+                       keys_basic = (struct flow_keys_basic) {
+                               .control = keys.control,
+                               .basic = keys.basic
+                       };
+               } else {
+                       skb_flow_dissect_flow_keys_basic(NULL, skb, &keys_basic, NULL, 0, 0, 0,
+                                                        FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+                       rxq = skb->hash;
+               }
+       } else {
+               rxq = skb_get_hash(skb);
+       }
+
          /* Check if we can use flow to select a queue */
-       rxq = skb_get_hash(skb);
          if (rxq) {
+               tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
                  queue = rcu_dereference(tap->taps[rxq % numvtaps]);
                  goto out;
          }
@@ -711,11 +762,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;
 
@@ -992,6 +1044,16 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
                  rtnl_unlock();
                  return ret;
 
+       case TUNGETVNETHASHCAP:
+               return tun_vnet_ioctl_gethashcap(argp);
+
+       case TUNSETVNETHASH:
+               rtnl_lock();
+               tap = rtnl_dereference(q->tap);
+               ret = tap ? tun_vnet_ioctl_sethash(&tap->vnet_hash, true, argp) : -EBADFD;
+               rtnl_unlock();
+               return ret;
+
          case SIOCGIFHWADDR:
                  rtnl_lock();
                  tap = tap_get_tap_dev(q);
drivers/net/tun.c +79 -19
index d8f4d3e996a7a81d1f8b04635054081671a14f07..520013df416e93d3a50b46be9b53ae9ab410eab4 100644
@@ -209,6 +209,7 @@ struct tun_struct {
          struct bpf_prog __rcu *xdp_prog;
          struct tun_prog __rcu *steering_prog;
          struct tun_prog __rcu *filter_prog;
+       struct tun_vnet_hash_container __rcu *vnet_hash;
          struct ethtool_link_ksettings link_ksettings;
          /* init args */
          struct file *file;
@@ -451,20 +452,37 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                  e->rps_rxhash = hash;
   }
 
+static struct virtio_net_hash *tun_add_hash(struct sk_buff *skb)
+{
+       return skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
   /* 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
    * the userspace application move between processors, we may get a
    * different rxq no. here.
    */
-static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static u16 tun_automq_select_queue(struct tun_struct *tun,
+                                  const struct tun_vnet_hash_container *vnet_hash,
+                                  struct sk_buff *skb)
   {
+       struct flow_keys keys;
+       struct flow_keys_basic keys_basic;
          struct tun_flow_entry *e;
          u32 txq, numqueues;
 
          numqueues = READ_ONCE(tun->numqueues);
 
-       txq = __skb_get_hash_symmetric(skb);
+       memset(&keys, 0, sizeof(keys));
+       skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, 0);
+
+       txq = flow_hash_from_keys(&keys);
          e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
          if (e) {
                  tun_flow_save_rps_rxhash(e, txq);
@@ -473,6 +491,13 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
                  txq = reciprocal_scale(txq, numqueues);
          }
 
+       keys_basic = (struct flow_keys_basic) {
+               .control = keys.control,
+               .basic = keys.basic
+       };
+       tun_vnet_hash_report(vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+                            tun_add_hash);
+
          return txq;
   }
 
@@ -500,10 +525,17 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
          u16 ret;
 
          rcu_read_lock();
-       if (rcu_dereference(tun->steering_prog))
+       if (rcu_dereference(tun->steering_prog)) {
                  ret = tun_ebpf_select_queue(tun, skb);
-       else
-               ret = tun_automq_select_queue(tun, skb);
+       } else {
+               struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tun->vnet_hash);
+
+               if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS))
+                       ret = tun_vnet_rss_select_queue(READ_ONCE(tun->numqueues), vnet_hash,
+                                                       skb, tun_add_hash);
+               else
+                       ret = tun_automq_select_queue(tun, vnet_hash, skb);
+       }
          rcu_read_unlock();
 
          return ret;
@@ -1987,7 +2019,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);
@@ -2040,9 +2072,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;
 
@@ -2223,6 +2256,7 @@ static void tun_free_netdev(struct net_device *dev)
          security_tun_dev_free_security(tun->security);
          __tun_set_ebpf(tun, &tun->steering_prog, NULL);
          __tun_set_ebpf(tun, &tun->filter_prog, NULL);
+       kfree_rcu_mightsleep(rcu_access_pointer(tun->vnet_hash));
   }
 
   static void tun_setup(struct net_device *dev)
@@ -2921,13 +2955,9 @@ static int tun_set_queue(struct file *file, struct ifreq *ifr)
   }
 
   static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
-                       void __user *data)
+                       int fd)
   {
          struct bpf_prog *prog;
-       int fd;
-
-       if (copy_from_user(&fd, data, sizeof(fd)))
-               return -EFAULT;
 
          if (fd == -1) {
                  prog = NULL;
@@ -2993,7 +3023,9 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
          int ifindex;
          int sndbuf;
          int ret;
+       int fd;
          bool do_notify = false;
+       struct tun_vnet_hash_container *vnet_hash;
 
          if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
              (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
@@ -3020,7 +3052,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
          rtnl_lock();
 
          tun = tun_get(tfile);
-       if (cmd == TUNSETIFF) {
+       switch (cmd) {
+       case TUNSETIFF:
                  ret = -EEXIST;
                  if (tun)
                          goto unlock;
@@ -3035,8 +3068,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                  if (copy_to_user(argp, &ifr, ifreq_len))
                          ret = -EFAULT;
                  goto unlock;
-       }
-       if (cmd == TUNSETIFINDEX) {
+
+       case TUNSETIFINDEX:
                  ret = -EPERM;
                  if (tun)
                          goto unlock;
@@ -3050,6 +3083,10 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                  ret = 0;
                  tfile->ifindex = ifindex;
                  goto unlock;
+
+       case TUNGETVNETHASHCAP:
+               ret = tun_vnet_ioctl_gethashcap(argp);
+               goto unlock;
          }
 
          ret = -EBADFD;
@@ -3230,11 +3267,27 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                  break;
 
          case TUNSETSTEERINGEBPF:
-               ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
+               if (get_user(fd, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               vnet_hash = rtnl_dereference(tun->vnet_hash);
+               if (fd != -1 && vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       ret = -EBUSY;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->steering_prog, fd);
                  break;
 
          case TUNSETFILTEREBPF:
-               ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
+               if (get_user(fd, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->filter_prog, fd);
                  break;
 
          case TUNSETCARRIER:
@@ -3252,8 +3305,15 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                  ret = open_related_ns(&net->ns, get_net_ns);
                  break;
 
+       case TUNSETVNETHASH:
+               ret = tun_vnet_ioctl_sethash(&tun->vnet_hash,
+                                            !rtnl_dereference(tun->steering_prog),
+                                            argp);
+               break;
+
          default:
-               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags, cmd, argp);
+               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
+                                    cmd, argp);
                  break;
          }
 
drivers/net/tun_vnet.h +0 -0
index 58b9ac7a5fc4084c789fe94fe36b5f8631bf1fa4..8e7d51fb0b4742cef56e7c5ad778b156cc654bed 100644

drivers/net/tun_vnet.h line 13

Quoted 8 lines
drivers/net/tun_vnet.h +5 -0
@@ -6,6 +6,16 @@
   #define TUN_VNET_LE     0x80000000
   #define TUN_VNET_BE     0x40000000
 
+typedef struct virtio_net_hash *(*tun_vnet_hash_add)(struct sk_buff *);
+typedef const struct virtio_net_hash *(*tun_vnet_hash_find)(const struct sk_buff *);
+
+struct tun_vnet_hash_container {
+       struct tun_vnet_hash common;

I'd rename this as hash.

drivers/net/tun_vnet.h line 17

Quoted 4 lines
drivers/net/tun_vnet.h +4 -0
+       struct tun_vnet_hash_rss rss;
+       u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
+       u16 rss_indirection_table[];
+};

Besides the separated ioctl, I'd split this structure into rss and hash part as well.

Quoted 8 lines
drivers/net/tun_vnet.h +1 -0
+
   static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
   {
          bool be = IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) &&
@@ -107,6 +117,123 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
          }
   }
 
Quoted 12 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 127

Quoted 8 lines
drivers/net/tun_vnet.h +8 -0
+static inline long tun_vnet_ioctl_gethashcap(void __user *argp)
+{
+       static const struct tun_vnet_hash cap = {
+               .flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS,
+               .types = VIRTIO_NET_SUPPORTED_HASH_TYPES
+       };
+
+       return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;

Let's has a consistent name for this and the uapi to be consistent with TUNSETIFF/TUNGETIFF. Probably TUNSETVNETHASH and tun_vnet_ioctl_gethash().

Quoted 3 lines

They have different semantics so they should have different names. TUNGETIFF reports the value currently set while TUNGETVNETHASHCAP reports the value that can be set later.

I'm not sure I will get here. I meant a symmetric name

TUNSETVNETHASH and TUNVETVNETHASH.

Quoted 12 lines
Quoted 12 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 131

Quoted 6 lines
drivers/net/tun_vnet.h +4 -0
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 6 lines
Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

Quoted 3 lines
Quoted 1 line

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 8 lines
Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 137 lines
Quoted 59 lines
Quoted 59 lines
Quoted 41 lines
drivers/net/tun_vnet.h +38 -0
+{
+       struct tun_vnet_hash hash_buf;
+       struct tun_vnet_hash_container *hash;
+
+       if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
+               return -EFAULT;
+       argp = (struct tun_vnet_hash __user *)argp + 1;
+
+       if (hash_buf.flags & TUN_VNET_HASH_RSS) {
+               struct tun_vnet_hash_rss rss;
+               size_t indirection_table_size;
+               size_t key_size;
+               size_t size;
+
+               if (!can_rss)
+                       return -EBUSY;
+
+               if (copy_from_user(&rss, argp, sizeof(rss)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_hash_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)rss.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(hash_buf.types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)rss.indirection_table_mask + 1);
+
+               hash = kmalloc(size, GFP_KERNEL);
+               if (!hash)
+                       return -ENOMEM;
+
+               if (copy_from_user(hash->rss_indirection_table,
+                                  argp, indirection_table_size)) {
+                       kfree(hash);
+                       return -EFAULT;
+               }
+               argp = (u16 __user *)argp + rss.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 179

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+                       kfree(hash);
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->rss = rss;
+       } else {
+               hash = kmalloc(sizeof(hash->common), GFP_KERNEL);
+               if (!hash)
+                       return -ENOMEM;

Do we need to validate the hash here (at least against the types we supported?)

Quoted 4 lines
drivers/net/tun_vnet.h +4 -0
+       }
+
+       hash->common = hash_buf;
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
Quoted 8 lines
Quoted 3 lines

I still didn't understand the trick here. E.g we use very simple primitives in synchronizing ebpf program through RCU in __tun_set_ebpf().

It is even simpler than __tun_set_ebpf(). The differences from __tun_set_ebpf() are:
1. This uses the rtnl lock instead of TUN-specific one. It makes the code simpler as the rtnl lock is already taken in __tun_chr_ioctl().

It can be tweaked to use rtnl as well.

Quoted 1 line

2. This does not add rcu_head and uses blocking APIs for simplicity.

Right.

Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
drivers/net/tun_vnet.h +17 -0
+       return 0;
+}
+
+static void tun_vnet_hash_report(const struct tun_vnet_hash_container *hash,
+                                struct sk_buff *skb,
+                                const struct flow_keys_basic *keys,
+                                u32 value,
+                                tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+
+       if (!hash || !(hash->common.flags & TUN_VNET_HASH_REPORT))
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
Quoted 8 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 205

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.types, keys),
+               .value = value
+       };

What's the advantage of using Designated Initializers here? Simple assignment can save two lines of code.

Quoted 2 lines

It automatically fills other fileds with zero. Simple assignments will need more tokens for zeroing.

Ok.

Quoted 30 lines
Quoted 29 lines
Quoted 16 lines
drivers/net/tun_vnet.h +14 -0
+}
+
+static u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                    const struct tun_vnet_hash_container *hash,
+                                    struct sk_buff *skb,
+                                    tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 txq, index;
+
+       if (!numqueues)
+               return 0;
+

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 229

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+       virtio_net_hash_rss(skb, hash->common.types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->rss.unclassified_queue % numqueues;
+
+       if (hash->common.flags & TUN_VNET_HASH_REPORT) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

Quoted 3 lines
Quoted 1 line

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting. RSS and hash reporting must share this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

Quoted 288 lines
Quoted 38 lines

It is more of a
feature instead of a bug; it behaves like QEMU's eBPF program but requires no privilege and is more optimized with native code and ffs().

Quoted 9 lines

drivers/net/tun_vnet.h static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, · line 232

Quoted 4 lines
drivers/net/tun_vnet.h +3 -0
+
+       index = ret.value & hash->rss.indirection_table_mask;
+       txq = READ_ONCE(hash->rss_indirection_table[index]);

So vnet_hash is accessed via rcu_dereference(), I don't get any reason we need READ_ONCE here, is this paired with something? If yes, let's add a comment here. If rss_indirection_table need why indirection_table_mask doesn't need this?

I'll drop it. I think it's just a left-over of previous versions without RCU.

Quoted 11 lines
Quoted 11 lines
drivers/net/tun_vnet.h +4 -0
+
+       return txq % numqueues;
+}
+
   static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                     struct iov_iter *from,
                                     struct virtio_net_hdr *hdr)
@@ -135,15 +262,17 @@ static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
   }
 
Quoted 8 lines

drivers/net/tun_vnet.h static inline int tun_vnet_hdr_get(int sz, unsigned int flags, · line 266

Quoted 4 lines
drivers/net/tun_vnet.h +1 -1
   static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-                                  const struct virtio_net_hdr *hdr)
+                                  const struct virtio_net_hdr_v1_hash *hdr)
   {

To be more robust, we can tweak the function to accept a vnet_hdr_len parameter then we can avoid touching this every time when we need to extend vnet hdr in the future?

Quoted 8 lines

I think you meant vnet_hdr_sz instead of vnet_hdr_len. It is already passed just as "sz" here as the function name already says it's about the header.

It is possible to add another parameter for sizeof(*hdr) and convert the hdr parameter to void * to avoid future changes. But I rather keep it as is because the current form ensures the hdr is large enough and statically avoids buffer overrun.

Right.

Quoted 133 lines
Quoted 124 lines
Quoted 124 lines
drivers/net/tun_vnet.h +22 -8
+       int content_sz = MIN(sizeof(*hdr), sz);
+
          if (unlikely(iov_iter_count(iter) < sz))
                  return -EINVAL;
 
-       if (unlikely(copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr)))
+       if (unlikely(copy_to_iter(hdr, content_sz, iter) != content_sz))
                  return -EFAULT;
 
-       if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+       if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
                  return -EFAULT;
 
          return 0;
@@ -155,26 +284,38 @@ static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
          return virtio_net_hdr_to_skb(skb, hdr, tun_vnet_is_little_endian(flags));
   }
 
-static inline int tun_vnet_hdr_from_skb(unsigned int flags,
+static inline int tun_vnet_hdr_from_skb(int sz, unsigned int flags,
                                          const struct net_device *dev,
                                          const struct sk_buff *skb,
-                                       struct virtio_net_hdr *hdr)
+                                       tun_vnet_hash_find vnet_hash_find,
+                                       struct virtio_net_hdr_v1_hash *hdr)
   {
          int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
+       const struct virtio_net_hash *report = sz < sizeof(struct virtio_net_hdr_v1_hash) ?
+                                              NULL : vnet_hash_find(skb);
+
+       *hdr = (struct virtio_net_hdr_v1_hash) {
+               .hash_report = VIRTIO_NET_HASH_REPORT_NONE
+       };
+
+       if (report) {
+               hdr->hash_value = cpu_to_le32(report->value);
+               hdr->hash_report = cpu_to_le16(report->report);
+       }
 
-       if (virtio_net_hdr_from_skb(skb, hdr,
+       if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
                                      tun_vnet_is_little_endian(flags), true,
                                      vlan_hlen)) {
                  struct skb_shared_info *sinfo = skb_shinfo(skb);
 
                  if (net_ratelimit()) {
                          netdev_err(dev, "unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n",
-                                  sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->gso_size),
-                                  tun_vnet16_to_cpu(flags, hdr->hdr_len));
+                                  sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->hdr.gso_size),
+                                  tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len));
                          print_hex_dump(KERN_ERR, "tun: ",
                                         DUMP_PREFIX_NONE,
                                         16, 1, skb->head,
-                                      min(tun_vnet16_to_cpu(flags, hdr->hdr_len), 64), true);
+                                      min(tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len), 64), true);
                  }
                  WARN_ON_ONCE(1);
                  return -EINVAL;
include/linux/if_tap.h +2 -0
index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..7334c46a3f101675a0d4e5a036987cfe18842f9f 100644
@@ -31,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
   #define MAX_TAP_QUEUES 256
 
   struct 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
index bb2b751d274acff931281a72e8b4b0c699b4e8af..cdd793f1c360ad5f63fcc4cbf67d845f5e2ccf6f 100644
@@ -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 +21 -0
index 287cdc81c9390c289a30545aa7ed23d81c3329d3..4887f97500a870c7ef3c96a5837b2d0a5a225040 100644
@@ -62,6 +62,42 @@
   #define TUNSETCARRIER _IOW('T', 226, int)
   #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHCAP - ioctl to get virtio_net hashing capability.
+ *
+ * The argument is a pointer to &struct tun_vnet_hash which will store the
+ * maximal virtio_net hashing configuration.
+ */
+#define TUNGETVNETHASHCAP _IOR('T', 228, struct tun_vnet_hash)
+
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
Quoted 8 lines

include/uapi/linux/if_tun.h line 88

Quoted 3 lines
include/uapi/linux/if_tun.h +3 -0
+ * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
+ * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
+ * to the size of &struct virtio_net_hdr_v1_hash.

So you had a dependency check already for vnet hdr len. I'd still suggest to split this into rss and hash as they are separated features. Then we can use separate data structure for them instead of a container struct.

Quoted 3 lines

I added a dependency check and found it is complicating the code and requires additional tests. I need a reason to justify the complexity if we are going to split it.

As we discussed above: They don't dedpend on each other.

Quoted 99 lines

Regards,
Akihiko Odaki

Quoted 94 lines
Quoted 36 lines
include/uapi/linux/if_tun.h +28 -0
+ *
+ * The members added to the legacy header by %TUN_VNET_HASH_REPORT flag will
+ * always be little-endian.
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.
+ */
+#define TUNSETVNETHASH _IOW('T', 229, struct tun_vnet_hash)
+
   /* TUNSETIFF ifr flags */
   #define IFF_TUN                0x0001
   #define IFF_TAP                0x0002
@@ -115,4 +151,43 @@ struct tun_filter {
          __u8   addr[][ETH_ALEN];
   };
 
+/**
+ * define TUN_VNET_HASH_REPORT - Request virtio_net hash reporting for vhost
+ */
+#define TUN_VNET_HASH_REPORT   0x0001
+
+/**
+ * define TUN_VNET_HASH_RSS - Request virtio_net RSS
+ *
+ * This is mutually exclusive with eBPF steering program.
+ */
+#define TUN_VNET_HASH_RSS      0x0002
+
+/**
+ * struct tun_vnet_hash - virtio_net hashing configuration
+ * @flags:
+ *             Bitmask consists of %TUN_VNET_HASH_REPORT and %TUN_VNET_HASH_RSS

include/uapi/linux/if_tun.h struct tun_filter { · line 179

Quoted 10 lines
include/uapi/linux/if_tun.h +10 -0
+ * @pad:
+ *             Should be filled with zero before passing to %TUNSETVNETHASH
+ * @types:
+ *             Bitmask of allowed hash types
+ */
+struct tun_vnet_hash {
+       __u16 flags;
+       __u8 pad[2];
+       __u32 types;
+};

Padding in the middle of the structure is not elegant. Any reason for this?

And hash->types seems never used.

Quoted 38 lines
include/uapi/linux/if_tun.h +13 -0
+
+/**
+ * struct tun_vnet_hash_rss - virtio_net RSS configuration
+ * @indirection_table_mask:
+ *             Bitmask to be applied to the indirection table index
+ * @unclassified_queue:
+ *             The index of the queue to place unclassified packets in
+ */
+struct tun_vnet_hash_rss {
+       __u16 indirection_table_mask;
+       __u16 unclassified_queue;
+};
+
   #endif /* _UAPI__IF_TUN_H */
net/core/skbuff.c +4 -1
index 7b03b64fdcb276f68ce881d1d8da8e4c6b897efc..aa2a091b649f0c9d6e0196f34f345ba78b5498fb 100644
@@ -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)
 
--
Quoted 2 lines

2.48.1

Thanks

#18 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang
Quoted 50 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
   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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +0 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
         return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
     }
 

[...]

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
Quoted 8 lines

Documentation/networking/tuntap.rst

Quoted 2 lines
Documentation/networking/tuntap.rst +2 -0
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 6 lines
Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

Quoted 3 lines
Quoted 1 line

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

Quoted 3 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Quoted 3 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Note that spec use different commands for hash_report and rss.

Quoted 8 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#19 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang
Quoted 50 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
    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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +0 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
          return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
      }
 

[...]

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
Quoted 8 lines

Documentation/networking/tuntap.rst

Quoted 2 lines
Documentation/networking/tuntap.rst +2 -0
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 6 lines
Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

Quoted 3 lines
Quoted 1 line

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

The current version implements 3) because it is not obvious whether we should choose either 1) or 2). But 1) will be the most capable option if eBPF has a fall-back feature.

Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

Quoted 7 lines
Quoted 1 line

RSS and hash reporting are not completely independent though.

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Quoted 6 lines
Quoted 4 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

Quoted 8 lines
Quoted 4 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:

Quoted 8 lines

A device attempts to calculate a per-packet hash in the following cases:

- The feature VIRTIO_NET_F_RSS was negotiated. The device uses the hash to determine the receive virtqueue to place incoming packets.
- The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device reports the hash value and the hash type with the packet.

Quoted 5 lines

If the feature VIRTIO_NET_F_RSS was negotiated:

- The device uses hash_types of the virtio_net_rss_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and

hash_key_length of the virtio_net_rss_config structure (see

Quoted 9 lines
     5.1.6.5.7.1).

If the feature VIRTIO_NET_F_RSS was not negotiated:

- The device uses hash_types of the virtio_net_hash_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and hash_key_length of the virtio_net_hash_config structure (see .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 2 lines

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

In the virtio-net specification, it is not defined what would happen if these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#20 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang
Quoted 50 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
    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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +0 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
          return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
      }
 

[...]

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
Quoted 8 lines

Documentation/networking/tuntap.rst

Quoted 2 lines
Documentation/networking/tuntap.rst +2 -0
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 6 lines
Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

Quoted 3 lines
Quoted 1 line

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

Quoted 3 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

Quoted 20 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
    le32 hash_types;
    le16 reserved[4];
    u8 hash_key_length;
    u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""
struct rss_rq_id {
le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */ le16 reserved: 1; /* Set to zero */
};

struct virtio_net_rss_config {
    le32 hash_types;
    le16 indirection_table_mask;
    struct rss_rq_id unclassified_queue;
    struct rss_rq_id indirection_table[indirection_table_length];
    le16 max_tx_vq;
    u8 hash_key_length;
    u8 hash_key_data[hash_key_length];
};

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

And it seems to lack parameters like max_tx_vq.

What's more, we've already had virito-net uAPI. Why not simply reusing them?

Quoted 40 lines
Quoted 9 lines
Quoted 5 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:

Quoted 8 lines

A device attempts to calculate a per-packet hash in the following cases:

- The feature VIRTIO_NET_F_RSS was negotiated. The device uses the hash to determine the receive virtqueue to place incoming packets.
- The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device reports the hash value and the hash type with the packet.

Quoted 5 lines

If the feature VIRTIO_NET_F_RSS was negotiated:

- The device uses hash_types of the virtio_net_rss_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and

hash_key_length of the virtio_net_rss_config structure (see

Quoted 9 lines
     5.1.6.5.7.1).

If the feature VIRTIO_NET_F_RSS was not negotiated:

- The device uses hash_types of the virtio_net_hash_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and hash_key_length of the virtio_net_hash_config structure (see .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Quoted 4 lines

In the virtio-net specification, it is not defined what would happen if these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#21 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 124 lines · Jason Wang
Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang
Quoted 50 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
     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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +0 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
           return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
       }
 

[...]

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
Quoted 8 lines

Documentation/networking/tuntap.rst

Quoted 2 lines
Documentation/networking/tuntap.rst +2 -0
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 6 lines
Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

Quoted 3 lines
Quoted 1 line

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

Quoted 7 lines

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

I referred to the eBPF steering program instead of tc-ebpf. tc-ebpf is nothing to do with the TUNSETSTEERINGEBPF ioctl, which this patch changes.

Quoted 7 lines
Quoted 4 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

I wrote: "because it is not obvious whether we should choose either 1) or 2)", but I think I can explain it better:

When an eBPF steering program cannot implement a fallback, it means the eBPF steering program requests the full control over the steering. On the other hand, RSS also requests the same control. So these two will conflict and the entity controlling the steering will be undefined when both are enabled.

3) eliminates the undefined semantics by rejecting to enable both. An alternative approach is to allow eBPF steering programs to fall back. When both the eBPF program and RSS are enabled, RSS will gain the control of steering under the well-defined situation where the eBPF steering program decides to fall back.

Quoted 60 lines
Quoted 21 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
     le32 hash_types;
     le16 reserved[4];
     u8 hash_key_length;
     u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""

struct rss_rq_id {
    le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */
    le16 reserved: 1; /* Set to zero */
};
struct virtio_net_rss_config {
     le32 hash_types;
     le16 indirection_table_mask;
     struct rss_rq_id unclassified_queue;
     struct rss_rq_id indirection_table[indirection_table_length];
     le16 max_tx_vq;
     u8 hash_key_length;
     u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

The spec also defines struct virtio_net_hash_config (which will be used when RSS is disabled) and struct virtio_net_rss_config to match the layout to share some fields. However, the UAPI does not follow the interface design of virtio due to some problems with these structures.

Below is the definition of struct virtio_net_hash_config:

struct virtio_net_hash_config {
    le32 hash_types;
    le16 reserved[4];
    u8 hash_key_length;
    u8 hash_key_data[hash_key_length];
};

Here, hash_types, hash_key_length, and hash_key_data are shared with struct virtio_net_rss_config.

One problem is that struct virtio_net_rss_config has a flexible array (indirection_table) between hash_types and hash_key_length. This is something we cannot express with C.

Another problem is that the semantics of the key in struct virtio_net_hash_config is not defined in the spec.

To solve these problems, I defined the UAPI structures that do not include indiretion_table.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

The UAPI shares struct tun_vnet_hash for both hash report and rss.

Quoted 17 lines

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

Quoted 1 line

And it seems to lack parameters like max_tx_vq.

max_tx_vq is not relevant with hashing.

Quoted 2 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 54 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:

Quoted 8 lines

A device attempts to calculate a per-packet hash in the following cases:

- The feature VIRTIO_NET_F_RSS was negotiated. The device uses the hash to determine the receive virtqueue to place incoming packets.
- The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device reports the hash value and the hash type with the packet.

Quoted 5 lines

If the feature VIRTIO_NET_F_RSS was negotiated:

- The device uses hash_types of the virtio_net_rss_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and

hash_key_length of the virtio_net_rss_config structure (see

Quoted 9 lines
     5.1.6.5.7.1).

If the feature VIRTIO_NET_F_RSS was not negotiated:

- The device uses hash_types of the virtio_net_hash_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and hash_key_length of the virtio_net_hash_config structure (see .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

Quoted 7 lines

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

Quoted 11 lines
Quoted 4 lines
Quoted 1 line

In the virtio-net specification, it is not defined what would happen if

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics? The virtio spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands. The best possibility is to share structures, not commands, and I don't think even sharing structures makes sense here because of the reasons described above.

Regards,
Akihiko Odaki

Quoted 23 lines

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#22 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 129 lines · Akihiko Odaki
Quoted 127 lines · Jason Wang
Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang
Quoted 50 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
     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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +0 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
           return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
       }
 

[...]

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
Quoted 8 lines

Documentation/networking/tuntap.rst

Quoted 2 lines
Documentation/networking/tuntap.rst +2 -0
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 6 lines
Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

Quoted 3 lines
Quoted 1 line

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

Quoted 7 lines
Quoted 4 lines

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

I referred to the eBPF steering program instead of tc-ebpf. tc-ebpf is nothing to do with the TUNSETSTEERINGEBPF ioctl, which this patch changes.

I meant you can't do "full control" in any case, the point below doesn't stand. Queue mapping could be restored even if RSS is set.

Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

Quoted 8 lines

I wrote: "because it is not obvious whether we should choose either 1) or 2)", but I think I can explain it better:

When an eBPF steering program cannot implement a fallback, it means the eBPF steering program requests the full control over the steering. On the other hand, RSS also requests the same control. So these two will conflict and the entity controlling the steering will be undefined when both are enabled.

Well, the fallback is orthogonal to the proposal here. We haven't had that since the introduction of the eBPF steering program. This means automq has been in "conflict" with eBPF for years. Again, another advantage, allowing the eBPF program to be the first to allow the management layer to override Qemu's steering.

Quoted 2 lines

3) eliminates the undefined semantics by rejecting to enable both.

This would lead a usersapce noticeable change of the behaviour? And what do you mean by "rejecting to enable both"?

Quoted 5 lines

An
alternative approach is to allow eBPF steering programs to fall back. When both the eBPF program and RSS are enabled, RSS will gain the control of steering under the well-defined situation where the eBPF steering program decides to fall back.

How about just stick the eBPF precedence in this proposal and introduce the fallback on top? This helps to speed up the iteration (as the version has been iterated to 11).

Quoted 68 lines
Quoted 61 lines
Quoted 22 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
     le32 hash_types;
     le16 reserved[4];
     u8 hash_key_length;
     u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""

struct rss_rq_id {
    le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */
    le16 reserved: 1; /* Set to zero */
};
struct virtio_net_rss_config {
     le32 hash_types;
     le16 indirection_table_mask;
     struct rss_rq_id unclassified_queue;
     struct rss_rq_id indirection_table[indirection_table_length];
     le16 max_tx_vq;
     u8 hash_key_length;
     u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

Quoted 4 lines

The spec also defines struct virtio_net_hash_config (which will be used when RSS is disabled) and struct virtio_net_rss_config to match the layout to share some fields. However, the UAPI does not follow the interface design of virtio due to some problems with these structures.

Copy-paste error. The above is copied from the virtio spec, but I meant the existing uAPI in virtio_net.h:

/*
* The command VIRTIO_NET_CTRL_MQ_RSS_CONFIG has the same effect as
* VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET does and additionally configures
* the receive steering to use a hash calculated for incoming packet
* to decide on receive virtqueue to place the packet. The command
* also provides parameters to calculate a hash and receive virtqueue. */

struct virtio_net_rss_config {
        __le32 hash_types;
        __le16 indirection_table_mask;
        __le16 unclassified_queue;
        __le16 indirection_table[1/* + indirection_table_mask */];
        __le16 max_tx_vq;
        __u8 hash_key_length;
        __u8 hash_key_data[/* hash_key_length */];
};

#define VIRTIO_NET_CTRL_MQ_RSS_CONFIG 1

/*
* The command VIRTIO_NET_CTRL_MQ_HASH_CONFIG requests the device
* to include in the virtio header of the packet the value of the
* calculated hash and the report type of hash. It also provides
* parameters for hash calculation. The command requires feature
* VIRTIO_NET_F_HASH_REPORT to be negotiated to extend the
* layout of virtio header as defined in virtio_net_hdr_v1_hash. */

struct virtio_net_hash_config {
        __le32 hash_types;
        /* for compatibility with virtio_net_rss_config */
        __le16 reserved[4];
        __u8 hash_key_length;
        __u8 hash_key_data[/* hash_key_length */];
};

This has been used by Qemu but I see a virtio-net version of:

struct virtio_net_ctrl_rss {
        u32 hash_types;
        u16 indirection_table_mask;
        u16 unclassified_queue;
        u16 hash_cfg_reserved; /* for HASH_CONFIG (see
virtio_net_hash_config for details) */
        u16 max_tx_vq;
        u8 hash_key_length;
        u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

        u16 *indirection_table;
};

This is ugly and results in a tricky code when trying to submit RSS/HASH commands to the device:

        if (vi->has_rss) {
                sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
                sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
        } else {
                sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved,
sizeof(uint16_t));
        }
Quoted 10 lines

Below is the definition of struct virtio_net_hash_config:

struct virtio_net_hash_config {
     le32 hash_types;
     le16 reserved[4];
     u8 hash_key_length;
     u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

Here, hash_types, hash_key_length, and hash_key_data are shared with struct virtio_net_rss_config.

One problem is that struct virtio_net_rss_config has a flexible array (indirection_table) between hash_types and hash_key_length. This is something we cannot express with C.

We can split the virtio_net_rss_config to ease the dealing with arrays, more below.

Quoted 3 lines

Another problem is that the semantics of the key in struct virtio_net_hash_config is not defined in the spec.

If this is the case. Let's fix that in the spec first to make sure our uAPI aligns with spec without ambiguity. It would be a nightmare to deal with the in-consistency between virtio spec and Linux uAPIs.

Quoted 11 lines

To solve these problems, I defined the UAPI structures that do not include indiretion_table.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

The UAPI shares struct tun_vnet_hash for both hash report and rss.

I meant sharing structure in two ioctls instead of reusing a specific structure for two semantics in one ioctl if possible. Though I don't think we need any sharing.

Quoted 18 lines
Quoted 18 lines

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

Quoted 3 lines
Quoted 1 line

And it seems to lack parameters like max_tx_vq.

max_tx_vq is not relevant with hashing.

It is needed for RSS and we don't have that, no?

Quoted 68 lines
Quoted 3 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 54 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:

Quoted 8 lines

A device attempts to calculate a per-packet hash in the following cases:

- The feature VIRTIO_NET_F_RSS was negotiated. The device uses the hash to determine the receive virtqueue to place incoming packets.
- The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device reports the hash value and the hash type with the packet.

Quoted 5 lines

If the feature VIRTIO_NET_F_RSS was negotiated:

- The device uses hash_types of the virtio_net_rss_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and

hash_key_length of the virtio_net_rss_config structure (see

Quoted 9 lines
     5.1.6.5.7.1).

If the feature VIRTIO_NET_F_RSS was not negotiated:

- The device uses hash_types of the virtio_net_hash_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and hash_key_length of the virtio_net_hash_config structure (see .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

Quoted 7 lines

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Quoted 3 lines

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

TUNSETIFF, enables the device to be able to handle RSS/HASREPORT. TUNSETHASH/RSS. dealing with RSS/HASH command from userspace.

This is the way we used to do for multi queue and vnet header. TUNSETIFF requires CAP_NET_ADMIN, this could be an extra safe guard for unprivileged userspace.

Quoted 14 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In the virtio-net specification, it is not defined what would

happen if

Quoted 10 lines
Quoted 3 lines

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Quoted 4 lines

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics?

That's perfectly fine, but it needs to be done in virtio-net's uAPI not tun's. What's more, if you think two commands are not well-defined, let's fix that in the virtio spec first.

Quoted 3 lines

The virtio
spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

See above, let's fix the uAPI first. We don't want DPDK to use tun's uAPI for RSS

Quoted 5 lines

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands.

I don't see a connection with the operation code. For example, we can add new uAPIs in virtio-net which could be something like:

 struct virtio_net_rss_config_header {
      __le32 hash_types;
      __le16 indirection_table_mask;
      __le16 unclassified_queue;
      __le16 indirection_table[];
}
struct virtio_net_rss_config_tailer {
      __le16 max_tx_vq;
      u8 hash_key_length;
      u8 hash_key_data[];
}

These two are used by TUNSETVNETRSS. And simply reuse the virtio_net_hash_config for TUNSETVETHASH.

With this, we can tweak the virtio-net driver with this new uAPI. Then tap* can reuse this.

Quoted 3 lines

The best possibility is to share structures, not commands, and I don't think even sharing structures makes sense here because of the reasons described above.

I don't want to share structures, I meant starting from something that is simple and has been sorted in the virtio spec. Optimization could be done on top.

Thanks

Quoted 28 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#23 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 132 lines · Jason Wang
Quoted 131 lines · Akihiko Odaki
Quoted 129 lines · Jason Wang
Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang
Quoted 50 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
      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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +0 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
            return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
        }
 

[...]

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
Quoted 8 lines

Documentation/networking/tuntap.rst

Quoted 2 lines
Documentation/networking/tuntap.rst +2 -0
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 6 lines
Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

Quoted 3 lines
Quoted 1 line

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

I referred to the eBPF steering program instead of tc-ebpf. tc-ebpf is nothing to do with the TUNSETSTEERINGEBPF ioctl, which this patch changes.

I meant you can't do "full control" in any case, the point below doesn't stand. Queue mapping could be restored even if RSS is set.

What matters here is how we handle the control when tc didn't take it. eBPF, RSS, or automq make take all of it; I referred that as "full control".

Quoted 19 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

Quoted 8 lines

I wrote: "because it is not obvious whether we should choose either 1) or 2)", but I think I can explain it better:

When an eBPF steering program cannot implement a fallback, it means the eBPF steering program requests the full control over the steering. On the other hand, RSS also requests the same control. So these two will conflict and the entity controlling the steering will be undefined when both are enabled.

Quoted 5 lines

Well, the fallback is orthogonal to the proposal here. We haven't had that since the introduction of the eBPF steering program. This means automq has been in "conflict" with eBPF for years. Again, another advantage, allowing the eBPF program to be the first to allow the management layer to override Qemu's steering.

What if a VMM uses eBPF steering program and the management layer decides to override it with RSS?

eBPF is obviously predecedent to automq as eBPF is an opt-in feature and automq is the implicit default. But this logic cannot be applied to decide the order of eBPF and RSS because they are both opt-in features.

Quoted 6 lines
Quoted 3 lines

3) eliminates the undefined semantics by rejecting to enable both.

This would lead a usersapce noticeable change of the behaviour? And what do you mean by "rejecting to enable both"?

Existing userspace code should see no change as it only cares the case where RSS is enabled.

Here, rejecting to enable both means to deny setting an eBPF steering program when RSS is enabled, and visa-versa.

Quoted 10 lines
Quoted 6 lines

An
alternative approach is to allow eBPF steering programs to fall back. When both the eBPF program and RSS are enabled, RSS will gain the control of steering under the well-defined situation where the eBPF steering program decides to fall back.

How about just stick the eBPF precedence in this proposal and introduce the fallback on top? This helps to speed up the iteration (as the version has been iterated to 11).

I don't think that helps much since we have another ongoing discussion below and it is not the sole roadblock.

Quoted 129 lines
Quoted 69 lines
Quoted 62 lines
Quoted 23 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
      le32 hash_types;
      le16 reserved[4];
      u8 hash_key_length;
      u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""

struct rss_rq_id {
     le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */
     le16 reserved: 1; /* Set to zero */
};
struct virtio_net_rss_config {
      le32 hash_types;
      le16 indirection_table_mask;
      struct rss_rq_id unclassified_queue;
      struct rss_rq_id indirection_table[indirection_table_length];
      le16 max_tx_vq;
      u8 hash_key_length;
      u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

Quoted 4 lines

The spec also defines struct virtio_net_hash_config (which will be used when RSS is disabled) and struct virtio_net_rss_config to match the layout to share some fields. However, the UAPI does not follow the interface design of virtio due to some problems with these structures.

Copy-paste error. The above is copied from the virtio spec, but I meant the existing uAPI in virtio_net.h:

/*
* The command VIRTIO_NET_CTRL_MQ_RSS_CONFIG has the same effect as
* VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET does and additionally configures
* the receive steering to use a hash calculated for incoming packet
* to decide on receive virtqueue to place the packet. The command
* also provides parameters to calculate a hash and receive virtqueue. */

struct virtio_net_rss_config {
         __le32 hash_types;
         __le16 indirection_table_mask;
         __le16 unclassified_queue;
         __le16 indirection_table[1/* + indirection_table_mask */];
         __le16 max_tx_vq;
         __u8 hash_key_length;
         __u8 hash_key_data[/* hash_key_length */];
};
Quoted 1 line
  #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1

/*
* The command VIRTIO_NET_CTRL_MQ_HASH_CONFIG requests the device
* to include in the virtio header of the packet the value of the
* calculated hash and the report type of hash. It also provides
* parameters for hash calculation. The command requires feature
* VIRTIO_NET_F_HASH_REPORT to be negotiated to extend the
* layout of virtio header as defined in virtio_net_hdr_v1_hash. */

struct virtio_net_hash_config {
         __le32 hash_types;
         /* for compatibility with virtio_net_rss_config */
         __le16 reserved[4];
         __u8 hash_key_length;
         __u8 hash_key_data[/* hash_key_length */];
};

This has been used by Qemu but I see a virtio-net version of:

struct virtio_net_ctrl_rss {
         u32 hash_types;
         u16 indirection_table_mask;
         u16 unclassified_queue;
         u16 hash_cfg_reserved; /* for HASH_CONFIG (see
virtio_net_hash_config for details) */
         u16 max_tx_vq;
         u8 hash_key_length;
         u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

         u16 *indirection_table;
};

This is ugly and results in a tricky code when trying to submit RSS/HASH commands to the device:

Quoted 7 lines
         if (vi->has_rss) {
                 sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
                 sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
         } else {
                 sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved,
sizeof(uint16_t));
         }

The only reference to struct virtio_net_rss_config in QEMU is to derive the offset of indirection_table. This is because the definition in virtio_net.h also includes indirection_table in the middle and the offsets of later part are unusable.

QEMU internally has a structure named VirtioNetRssData which just looks like struct virtio_net_ctrl_rss.

Quoted 22 lines
Quoted 11 lines

Below is the definition of struct virtio_net_hash_config:

struct virtio_net_hash_config {
      le32 hash_types;
      le16 reserved[4];
      u8 hash_key_length;
      u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

Here, hash_types, hash_key_length, and hash_key_data are shared with struct virtio_net_rss_config.

One problem is that struct virtio_net_rss_config has a flexible array (indirection_table) between hash_types and hash_key_length. This is something we cannot express with C.

We can split the virtio_net_rss_config to ease the dealing with arrays, more below.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Another problem is that the semantics of the key in struct virtio_net_hash_config is not defined in the spec.

If this is the case. Let's fix that in the spec first to make sure our uAPI aligns with spec without ambiguity. It would be a nightmare to deal with the in-consistency between virtio spec and Linux uAPIs.

The userspace doesn't need to do anything to deal with inconsistency since these fields are unused.

Quoted 16 lines
Quoted 16 lines

To solve these problems, I defined the UAPI structures that do not include indiretion_table.

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

The UAPI shares struct tun_vnet_hash for both hash report and rss.

I meant sharing structure in two ioctls instead of reusing a specific structure for two semantics in one ioctl if possible. Though I don't think we need any sharing.

The UAPI implemented in this patch already shares struct tun_vnet_hash and having two ioctls doesn't change that.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

And it seems to lack parameters like max_tx_vq.

max_tx_vq is not relevant with hashing.

It is needed for RSS and we don't have that, no?

No. RSS is Receive Side Scaling but it's not about receiving.

Quoted 69 lines
Quoted 69 lines
Quoted 4 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 61 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:

Quoted 8 lines

A device attempts to calculate a per-packet hash in the following cases:

- The feature VIRTIO_NET_F_RSS was negotiated. The device uses the hash to determine the receive virtqueue to place incoming packets.
- The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device reports the hash value and the hash type with the packet.

Quoted 5 lines

If the feature VIRTIO_NET_F_RSS was negotiated:

- The device uses hash_types of the virtio_net_rss_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and

hash_key_length of the virtio_net_rss_config structure (see

Quoted 9 lines
     5.1.6.5.7.1).

If the feature VIRTIO_NET_F_RSS was not negotiated:

- The device uses hash_types of the virtio_net_hash_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and hash_key_length of the virtio_net_hash_config structure (see .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Quoted 6 lines
Quoted 3 lines

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

TUNSETIFF, enables the device to be able to handle RSS/HASREPORT. TUNSETHASH/RSS. dealing with RSS/HASH command from userspace.

We also needs to be able to disable them at runtime so that we can handle resets.

Quoted 4 lines

This is the way we used to do for multi queue and vnet header. TUNSETIFF requires CAP_NET_ADMIN, this could be an extra safe guard for unprivileged userspace.

I intend to allow using this feature without privilege. A VMM is usually unprivileged and requiring a privilege to configure tuntap is too prohibitive.

Quoted 24 lines
Quoted 15 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In the virtio-net specification, it is not defined what would

happen if

Quoted 10 lines
Quoted 3 lines

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Quoted 4 lines

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics?

That's perfectly fine, but it needs to be done in virtio-net's uAPI not tun's. What's more, if you think two commands are not well-defined, let's fix that in the virtio spec first.

Quoted 6 lines
Quoted 3 lines

The virtio
spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

See above, let's fix the uAPI first. We don't want DPDK to use tun's uAPI for RSS

virtio-net's UAPI is for the virtio spec which has a capable generic feature negotiation mechanism. tuntap needs its own feature negotiation and it's nothing to do with virtio-net's UAPI.

The structures for two commands have unused or redundant fields and a flexible array in the middle of the structure, but they are ABIs so we can't change it.

DPDK is another reason to define tuntap's own UAPIs. They don't care unused or redundant fields and a flexible array in middle that are present in the virtio spec. It will also not want to deal with the requirement of little endian. Constructing struct virtio_net_rss_config is an extra burden for DPDK.

On the other hand, Constructing tuntap-specific structures is not that complicated for VMMs. A VMM will need to inspect struct virtio_net_rss_config anyway to handle migration and check its size so it can store the values it inspected to struct tun_vnet_hash and struct tun_vnet_hash_rss and pass them to the kernel.

The overall userspace implementation will be simpler by having structures specifically tailored for the communication between the userspace and kernel.

Quoted 23 lines
Quoted 6 lines

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands.

I don't see a connection with the operation code. For example, we can add new uAPIs in virtio-net which could be something like:

  struct virtio_net_rss_config_header {
       __le32 hash_types;
       __le16 indirection_table_mask;
       __le16 unclassified_queue;
       __le16 indirection_table[];
}
struct virtio_net_rss_config_tailer {
       __le16 max_tx_vq;
       u8 hash_key_length;
       u8 hash_key_data[];
}
Quoted 4 lines

These two are used by TUNSETVNETRSS. And simply reuse the virtio_net_hash_config for TUNSETVETHASH.

Quoted 1 line

With this, we can tweak the virtio-net driver with this new uAPI. Then

tap* can reuse this.

I implemented a UAPI and driver change accordingly:
https://lore.kernel.org/r/[email protected]

This is a nice improvement for the driver, but I still don't think it is suited for the UAPI of tuntap. The requirements of extra fields and little endian cannot be removed from the virtio spec but they are irrelevant for tuntap.

Quoted 8 lines
Quoted 4 lines

The best possibility is to share structures, not commands, and I don't think even sharing structures makes sense here because of the reasons described above.

I don't want to share structures, I meant starting from something that is simple and has been sorted in the virtio spec. Optimization could be done on top.

I meant to reuse the structures in virtio_net.h.

Regards,
Akihiko Odaki

Quoted 33 lines

Thanks

Quoted 28 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#24 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 165 lines · Akihiko Odaki
Quoted 132 lines · Jason Wang
Quoted 131 lines · Akihiko Odaki
Quoted 129 lines · Jason Wang
Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang
Quoted 50 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
      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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +0 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
            return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
        }
 

[...]

Quoted 28 lines
Quoted 28 lines
Quoted 8 lines
Quoted 8 lines

Documentation/networking/tuntap.rst

Quoted 2 lines
Documentation/networking/tuntap.rst +2 -0
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

Quoted 6 lines
Quoted 3 lines

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

Quoted 3 lines
Quoted 1 line

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

I referred to the eBPF steering program instead of tc-ebpf. tc-ebpf is nothing to do with the TUNSETSTEERINGEBPF ioctl, which this patch changes.

I meant you can't do "full control" in any case, the point below doesn't stand. Queue mapping could be restored even if RSS is set.

What matters here is how we handle the control when tc didn't take it. eBPF, RSS, or automq make take all of it; I referred that as "full control".

Quoted 19 lines
Quoted 18 lines
Quoted 9 lines
Quoted 6 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

I wrote: "because it is not obvious whether we should choose either 1) or 2)", but I think I can explain it better:

When an eBPF steering program cannot implement a fallback, it means the eBPF steering program requests the full control over the steering. On the other hand, RSS also requests the same control. So these two will conflict and the entity controlling the steering will be undefined when both are enabled.

Quoted 8 lines
Quoted 5 lines

Well, the fallback is orthogonal to the proposal here. We haven't had that since the introduction of the eBPF steering program. This means automq has been in "conflict" with eBPF for years. Again, another advantage, allowing the eBPF program to be the first to allow the management layer to override Qemu's steering.

What if a VMM uses eBPF steering program and the management layer decides to override it with RSS?

That's possible but I think we're seeking which approach is better. In this case, RSS could be implemented in eBPF but not the reverse.

So my point is to start from something that is simpler. Simply allow eBPF on top of RSS as automq. And optimize on top.

Quoted 4 lines

eBPF is obviously predecedent to automq as eBPF is an opt-in feature and automq is the implicit default. But this logic cannot be applied to decide the order of eBPF and RSS because they are both opt-in features.

This is from the perspective of kernel development. But let's try to think from the userspace: A well written user space knows what it does, rejecting eBPF while RSS is set doesn't help. But anyhow if you stick, it doesn't harm.

Quoted 165 lines
Quoted 7 lines
Quoted 4 lines

3) eliminates the undefined semantics by rejecting to enable both.

This would lead a usersapce noticeable change of the behaviour? And what do you mean by "rejecting to enable both"?

Existing userspace code should see no change as it only cares the case where RSS is enabled.

Here, rejecting to enable both means to deny setting an eBPF steering program when RSS is enabled, and visa-versa.

Quoted 10 lines
Quoted 6 lines

An
alternative approach is to allow eBPF steering programs to fall back. When both the eBPF program and RSS are enabled, RSS will gain the control of steering under the well-defined situation where the eBPF steering program decides to fall back.

How about just stick the eBPF precedence in this proposal and introduce the fallback on top? This helps to speed up the iteration (as the version has been iterated to 11).

I don't think that helps much since we have another ongoing discussion below and it is not the sole roadblock.

Quoted 129 lines
Quoted 69 lines
Quoted 62 lines
Quoted 23 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
      le32 hash_types;
      le16 reserved[4];
      u8 hash_key_length;
      u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""

struct rss_rq_id {
     le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */
     le16 reserved: 1; /* Set to zero */
};
struct virtio_net_rss_config {
      le32 hash_types;
      le16 indirection_table_mask;
      struct rss_rq_id unclassified_queue;
      struct rss_rq_id indirection_table[indirection_table_length];
      le16 max_tx_vq;
      u8 hash_key_length;
      u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

Quoted 4 lines

The spec also defines struct virtio_net_hash_config (which will be used when RSS is disabled) and struct virtio_net_rss_config to match the layout to share some fields. However, the UAPI does not follow the interface design of virtio due to some problems with these structures.

Copy-paste error. The above is copied from the virtio spec, but I meant the existing uAPI in virtio_net.h:

/*
* The command VIRTIO_NET_CTRL_MQ_RSS_CONFIG has the same effect as
* VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET does and additionally configures
* the receive steering to use a hash calculated for incoming packet
* to decide on receive virtqueue to place the packet. The command
* also provides parameters to calculate a hash and receive virtqueue. */

struct virtio_net_rss_config {
         __le32 hash_types;
         __le16 indirection_table_mask;
         __le16 unclassified_queue;
         __le16 indirection_table[1/* + indirection_table_mask */];
         __le16 max_tx_vq;
         __u8 hash_key_length;
         __u8 hash_key_data[/* hash_key_length */];
};
Quoted 1 line
  #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1

/*
* The command VIRTIO_NET_CTRL_MQ_HASH_CONFIG requests the device
* to include in the virtio header of the packet the value of the
* calculated hash and the report type of hash. It also provides
* parameters for hash calculation. The command requires feature
* VIRTIO_NET_F_HASH_REPORT to be negotiated to extend the
* layout of virtio header as defined in virtio_net_hdr_v1_hash. */

struct virtio_net_hash_config {
         __le32 hash_types;
         /* for compatibility with virtio_net_rss_config */
         __le16 reserved[4];
         __u8 hash_key_length;
         __u8 hash_key_data[/* hash_key_length */];
};

This has been used by Qemu but I see a virtio-net version of:

struct virtio_net_ctrl_rss {
         u32 hash_types;
         u16 indirection_table_mask;
         u16 unclassified_queue;
         u16 hash_cfg_reserved; /* for HASH_CONFIG (see
virtio_net_hash_config for details) */
         u16 max_tx_vq;
         u8 hash_key_length;
         u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

         u16 *indirection_table;
};

This is ugly and results in a tricky code when trying to submit RSS/HASH commands to the device:

Quoted 7 lines
         if (vi->has_rss) {
                 sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
                 sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
         } else {
                 sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved,
sizeof(uint16_t));
         }
Quoted 4 lines

The only reference to struct virtio_net_rss_config in QEMU is to derive the offset of indirection_table. This is because the definition in virtio_net.h also includes indirection_table in the middle and the offsets of later part are unusable.

Yes.

Quoted 3 lines

QEMU internally has a structure named VirtioNetRssData which just looks like struct virtio_net_ctrl_rss.

It's a pity that it doesn't use uAPI. We might need to fix them.

Quoted 80 lines
Quoted 23 lines
Quoted 12 lines

Below is the definition of struct virtio_net_hash_config:

struct virtio_net_hash_config {
      le32 hash_types;
      le16 reserved[4];
      u8 hash_key_length;
      u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

Here, hash_types, hash_key_length, and hash_key_data are shared with struct virtio_net_rss_config.

One problem is that struct virtio_net_rss_config has a flexible array (indirection_table) between hash_types and hash_key_length. This is something we cannot express with C.

We can split the virtio_net_rss_config to ease the dealing with arrays, more below.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Another problem is that the semantics of the key in struct virtio_net_hash_config is not defined in the spec.

If this is the case. Let's fix that in the spec first to make sure our uAPI aligns with spec without ambiguity. It would be a nightmare to deal with the in-consistency between virtio spec and Linux uAPIs.

The userspace doesn't need to do anything to deal with inconsistency since these fields are unused.

Quoted 16 lines
Quoted 16 lines

To solve these problems, I defined the UAPI structures that do not include indiretion_table.

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

The UAPI shares struct tun_vnet_hash for both hash report and rss.

I meant sharing structure in two ioctls instead of reusing a specific structure for two semantics in one ioctl if possible. Though I don't think we need any sharing.

The UAPI implemented in this patch already shares struct tun_vnet_hash and having two ioctls doesn't change that.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

And it seems to lack parameters like max_tx_vq.

max_tx_vq is not relevant with hashing.

It is needed for RSS and we don't have that, no?

No. RSS is Receive Side Scaling but it's not about receiving.

Just to make sure I understand this, max_tx_vq is part of the virtio_net_rss_config, how would Qemu behave when it receives this from guest?

"""
A driver sets max_tx_vq to inform a device how many transmit virtqueues it may use (transmitq1…transmitq max_tx_vq). """

Quoted 74 lines
Quoted 74 lines
Quoted 73 lines
Quoted 5 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 54 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:

Quoted 8 lines

A device attempts to calculate a per-packet hash in the following cases:

- The feature VIRTIO_NET_F_RSS was negotiated. The device uses the hash to determine the receive virtqueue to place incoming packets.
- The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device reports the hash value and the hash type with the packet.

Quoted 5 lines

If the feature VIRTIO_NET_F_RSS was negotiated:

- The device uses hash_types of the virtio_net_rss_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and

hash_key_length of the virtio_net_rss_config structure (see

Quoted 9 lines
     5.1.6.5.7.1).

If the feature VIRTIO_NET_F_RSS was not negotiated:

- The device uses hash_types of the virtio_net_hash_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and hash_key_length of the virtio_net_hash_config structure (see .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

Quoted 7 lines

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

Quoted 5 lines
Quoted 2 lines

TUNSETIFF, enables the device to be able to handle RSS/HASREPORT. TUNSETHASH/RSS. dealing with RSS/HASH command from userspace.

We also needs to be able to disable them at runtime so that we can handle resets.

Via TUNSETHASH/RSS? I think it should have a way to accept parameters that disable RSS or hash report.

Quoted 9 lines
Quoted 5 lines

This is the way we used to do for multi queue and vnet header. TUNSETIFF requires CAP_NET_ADMIN, this could be an extra safe guard for unprivileged userspace.

I intend to allow using this feature without privilege. A VMM is usually unprivileged and requiring a privilege to configure tuntap is too prohibitive.

For safety, tun is not allowed to be created by unprivileged users. And it's not to configure the tuntap dynamically, it's about telling the function that tuntap can have (not necessarily enabled though) .

Quoted 29 lines
Quoted 29 lines
Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

In the virtio-net specification, it is not defined what would

happen if

Quoted 10 lines
Quoted 3 lines

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Quoted 4 lines

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics?

That's perfectly fine, but it needs to be done in virtio-net's uAPI not tun's. What's more, if you think two commands are not well-defined, let's fix that in the virtio spec first.

Quoted 3 lines

The virtio
spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

Quoted 6 lines
Quoted 2 lines

See above, let's fix the uAPI first. We don't want DPDK to use tun's uAPI for RSS

virtio-net's UAPI is for the virtio spec which has a capable generic feature negotiation mechanism. tuntap needs its own feature negotiation and it's nothing to do with virtio-net's UAPI.

Well, I don't mean the part of the feature negotiation. I mean the part for rss and hash report configuration.

Quoted 10 lines

The structures for two commands have unused or redundant fields and a flexible array in the middle of the structure, but they are ABIs so we can't change it.

DPDK is another reason to define tuntap's own UAPIs. They don't care unused or redundant fields and a flexible array in middle that are present in the virtio spec. It will also not want to deal with the requirement of little endian. Constructing struct virtio_net_rss_config is an extra burden for DPDK.

I meant for vhost-user implementation in DPDK, it needs to use virtio-net uAPI not tuntap's for example.

Quoted 3 lines

On the other hand, Constructing tuntap-specific structures is not that complicated for VMMs.

Not complicated but redundant.

Quoted 4 lines

A VMM will need to inspect struct
virtio_net_rss_config anyway to handle migration and check its size so it can store the values it inspected to struct tun_vnet_hash and struct tun_vnet_hash_rss and pass them to the kernel.

I don't see how rss and hash reports differ from what we have now. Those inspections must be done anyhow for compatibility for example the check of offloading features. Such steps could not be eliminated no matter how we design the uAPI.

Quoted 4 lines

The overall userspace implementation will be simpler by having structures specifically tailored for the communication between the userspace and kernel.

This is exactly how a good uAPI should behave. If uAPI in virtio-net can't do this, I don't understand why uAPI in tuntap can solve it.

Quoted 26 lines
Quoted 26 lines
Quoted 7 lines

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands.

I don't see a connection with the operation code. For example, we can add new uAPIs in virtio-net which could be something like:

  struct virtio_net_rss_config_header {
       __le32 hash_types;
       __le16 indirection_table_mask;
       __le16 unclassified_queue;
       __le16 indirection_table[];
}
struct virtio_net_rss_config_tailer {
       __le16 max_tx_vq;
       u8 hash_key_length;
       u8 hash_key_data[];
}

These two are used by TUNSETVNETRSS. And simply reuse the virtio_net_hash_config for TUNSETVETHASH.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

With this, we can tweak the virtio-net driver with this new uAPI. Then

tap* can reuse this.

I implemented a UAPI and driver change accordingly:
https://lore.kernel.org/r/[email protected]

This is a nice improvement for the driver, but I still don't think it is suited for the UAPI of tuntap.

Any reason for this? It should work like virtio_net_hdr.

Quoted 3 lines

The requirements of extra fields and
little endian cannot be removed from the virtio spec but they are irrelevant for tuntap.

I don't understand this part. What fields are "extra" and need to be removed from the spec?

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

The best possibility is to share structures, not commands, and I don't think even sharing structures makes sense here because of the reasons described above.

Quoted 8 lines
Quoted 3 lines

I don't want to share structures, I meant starting from something that is simple and has been sorted in the virtio spec. Optimization could be done on top.

I meant to reuse the structures in virtio_net.h.

Regards,
Akihiko Odaki

Thanks

Quoted 35 lines
Quoted 34 lines

Thanks

Quoted 28 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#25 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 172 lines · Jason Wang
Quoted 171 lines · Akihiko Odaki
Quoted 132 lines · Jason Wang
Quoted 131 lines · Akihiko Odaki
Quoted 129 lines · Jason Wang
Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang
Quoted 50 lines · Akihiko Odaki

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.

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                   |  68 ++++++++++++++-
       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, 386 insertions(+), 31 deletions(-)
Documentation/networking/tuntap.rst +0 -0
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
             return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
         }
 

[...]

Quoted 28 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

Documentation/networking/tuntap.rst

Quoted 2 lines
Documentation/networking/tuntap.rst +2 -0
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+                                         bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

Quoted 5 lines
Quoted 2 lines

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

     > >>
Quoted 10 lines
Quoted 2 lines

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

I referred to the eBPF steering program instead of tc-ebpf. tc-ebpf is nothing to do with the TUNSETSTEERINGEBPF ioctl, which this patch changes.

I meant you can't do "full control" in any case, the point below doesn't stand. Queue mapping could be restored even if RSS is set.

What matters here is how we handle the control when tc didn't take it. eBPF, RSS, or automq make take all of it; I referred that as "full control".

Quoted 24 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

Quoted 8 lines

I wrote: "because it is not obvious whether we should choose either 1) or 2)", but I think I can explain it better:

When an eBPF steering program cannot implement a fallback, it means the eBPF steering program requests the full control over the steering. On the other hand, RSS also requests the same control. So these two will conflict and the entity controlling the steering will be undefined when both are enabled.

Well, the fallback is orthogonal to the proposal here. We haven't had that since the introduction of the eBPF steering program. This means automq has been in "conflict" with eBPF for years. Again, another advantage, allowing the eBPF program to be the first to allow the management layer to override Qemu's steering.

Quoted 8 lines
Quoted 2 lines

What if a VMM uses eBPF steering program and the management layer decides to override it with RSS?

That's possible but I think we're seeking which approach is better. In this case, RSS could be implemented in eBPF but not the reverse.

So my point is to start from something that is simpler. Simply allow eBPF on top of RSS as automq. And optimize on top.

The in-kernel RSS implementation is more optimized and capable of hash reporting. I don't think either eBPF steering program or in-kernel RSS is more capable than the other and there is a reason to place eBPF on top of RSS.

Quoted 10 lines
Quoted 5 lines

eBPF is obviously predecedent to automq as eBPF is an opt-in feature and automq is the implicit default. But this logic cannot be applied to decide the order of eBPF and RSS because they are both opt-in features.

This is from the perspective of kernel development. But let's try to think from the userspace: A well written user space knows what it does, rejecting eBPF while RSS is set doesn't help. But anyhow if you stick, it doesn't harm.

Yes, it not for the current userspace but for the future kernel development; the kernel can reserve the freedom to decide the priority of eBPF and RSS by rejecting eBPF while RSS.

Quoted 171 lines
Quoted 166 lines
Quoted 8 lines
Quoted 5 lines

3) eliminates the undefined semantics by rejecting to enable both.

This would lead a usersapce noticeable change of the behaviour? And what do you mean by "rejecting to enable both"?

Existing userspace code should see no change as it only cares the case where RSS is enabled.

Here, rejecting to enable both means to deny setting an eBPF steering program when RSS is enabled, and visa-versa.

Quoted 10 lines
Quoted 6 lines

An
alternative approach is to allow eBPF steering programs to fall back. When both the eBPF program and RSS are enabled, RSS will gain the control of steering under the well-defined situation where the eBPF steering program decides to fall back.

How about just stick the eBPF precedence in this proposal and introduce the fallback on top? This helps to speed up the iteration (as the version has been iterated to 11).

I don't think that helps much since we have another ongoing discussion below and it is not the sole roadblock.

Quoted 129 lines
Quoted 69 lines
Quoted 62 lines
Quoted 23 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
       le32 hash_types;
       le16 reserved[4];
       u8 hash_key_length;
       u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""

struct rss_rq_id {
      le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */
      le16 reserved: 1; /* Set to zero */
};
struct virtio_net_rss_config {
       le32 hash_types;
       le16 indirection_table_mask;
       struct rss_rq_id unclassified_queue;
       struct rss_rq_id indirection_table[indirection_table_length];
       le16 max_tx_vq;
       u8 hash_key_length;
       u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

Quoted 4 lines

The spec also defines struct virtio_net_hash_config (which will be used when RSS is disabled) and struct virtio_net_rss_config to match the layout to share some fields. However, the UAPI does not follow the interface design of virtio due to some problems with these structures.

Copy-paste error. The above is copied from the virtio spec, but I meant the existing uAPI in virtio_net.h:

/*
* The command VIRTIO_NET_CTRL_MQ_RSS_CONFIG has the same effect as
* VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET does and additionally configures
* the receive steering to use a hash calculated for incoming packet
* to decide on receive virtqueue to place the packet. The command
* also provides parameters to calculate a hash and receive virtqueue. */

struct virtio_net_rss_config {
          __le32 hash_types;
          __le16 indirection_table_mask;
          __le16 unclassified_queue;
          __le16 indirection_table[1/* + indirection_table_mask */];
          __le16 max_tx_vq;
          __u8 hash_key_length;
          __u8 hash_key_data[/* hash_key_length */];
};
Quoted 1 line
   #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1

/*
* The command VIRTIO_NET_CTRL_MQ_HASH_CONFIG requests the device
* to include in the virtio header of the packet the value of the
* calculated hash and the report type of hash. It also provides
* parameters for hash calculation. The command requires feature
* VIRTIO_NET_F_HASH_REPORT to be negotiated to extend the
* layout of virtio header as defined in virtio_net_hdr_v1_hash. */

struct virtio_net_hash_config {
          __le32 hash_types;
          /* for compatibility with virtio_net_rss_config */
          __le16 reserved[4];
          __u8 hash_key_length;
          __u8 hash_key_data[/* hash_key_length */];
};

This has been used by Qemu but I see a virtio-net version of:

struct virtio_net_ctrl_rss {
          u32 hash_types;
          u16 indirection_table_mask;
          u16 unclassified_queue;
          u16 hash_cfg_reserved; /* for HASH_CONFIG (see
virtio_net_hash_config for details) */
          u16 max_tx_vq;
          u8 hash_key_length;
          u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

          u16 *indirection_table;
};

This is ugly and results in a tricky code when trying to submit RSS/HASH commands to the device:

Quoted 7 lines
          if (vi->has_rss) {
                  sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
                  sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
          } else {
                  sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved,
sizeof(uint16_t));
          }
Quoted 4 lines

The only reference to struct virtio_net_rss_config in QEMU is to derive the offset of indirection_table. This is because the definition in virtio_net.h also includes indirection_table in the middle and the offsets of later part are unusable.

Quoted 7 lines

Yes.

Quoted 3 lines

QEMU internally has a structure named VirtioNetRssData which just looks like struct virtio_net_ctrl_rss.

It's a pity that it doesn't use uAPI. We might need to fix them.

It doesn't want to use the UAPI structures for the internal storage because it wants to store them in native endians and QEMU is not interested in some fields in the UAPI structures. struct tun_vnet_hash and struct tun_vnet_hash_rss are easy to fill using VirtioNetRssData.

Quoted 89 lines
Quoted 81 lines
Quoted 24 lines
Quoted 13 lines

Below is the definition of struct virtio_net_hash_config:

struct virtio_net_hash_config {
       le32 hash_types;
       le16 reserved[4];
       u8 hash_key_length;
       u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

Here, hash_types, hash_key_length, and hash_key_data are shared with struct virtio_net_rss_config.

One problem is that struct virtio_net_rss_config has a flexible array (indirection_table) between hash_types and hash_key_length. This is something we cannot express with C.

We can split the virtio_net_rss_config to ease the dealing with arrays, more below.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Another problem is that the semantics of the key in struct virtio_net_hash_config is not defined in the spec.

If this is the case. Let's fix that in the spec first to make sure our uAPI aligns with spec without ambiguity. It would be a nightmare to deal with the in-consistency between virtio spec and Linux uAPIs.

The userspace doesn't need to do anything to deal with inconsistency since these fields are unused.

Quoted 16 lines
Quoted 16 lines

To solve these problems, I defined the UAPI structures that do not include indiretion_table.

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

The UAPI shares struct tun_vnet_hash for both hash report and rss.

I meant sharing structure in two ioctls instead of reusing a specific structure for two semantics in one ioctl if possible. Though I don't think we need any sharing.

The UAPI implemented in this patch already shares struct tun_vnet_hash and having two ioctls doesn't change that.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

And it seems to lack parameters like max_tx_vq.

max_tx_vq is not relevant with hashing.

It is needed for RSS and we don't have that, no?

No. RSS is Receive Side Scaling but it's not about receiving.

Quoted 8 lines

Just to make sure I understand this, max_tx_vq is part of the virtio_net_rss_config, how would Qemu behave when it receives this from guest?

"""
A driver sets max_tx_vq to inform a device how many transmit virtqueues it may use (transmitq1…transmitq max_tx_vq). """

It does nothing.

Quoted 75 lines
Quoted 75 lines
Quoted 75 lines
Quoted 74 lines
Quoted 6 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 54 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:

Quoted 8 lines

A device attempts to calculate a per-packet hash in the following cases:

- The feature VIRTIO_NET_F_RSS was negotiated. The device uses the hash to determine the receive virtqueue to place incoming packets.
- The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device reports the hash value and the hash type with the packet.

Quoted 5 lines

If the feature VIRTIO_NET_F_RSS was negotiated:

- The device uses hash_types of the virtio_net_rss_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and

hash_key_length of the virtio_net_rss_config structure (see
> 5.1.6.5.7.1).
>
> If the feature VIRTIO_NET_F_RSS was not negotiated:
>
> - The device uses hash_types of the virtio_net_hash_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
> hash_key_length of the virtio_net_hash_config structure (see
> .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

Quoted 7 lines

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

TUNSETIFF, enables the device to be able to handle RSS/HASREPORT. TUNSETHASH/RSS. dealing with RSS/HASH command from userspace.

We also needs to be able to disable them at runtime so that we can handle resets.

Via TUNSETHASH/RSS? I think it should have a way to accept parameters that disable RSS or hash report.

That's what this patch implements. TUNSETVNETHASH accepts parameters to choose what features to be enabled.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

This is the way we used to do for multi queue and vnet header. TUNSETIFF requires CAP_NET_ADMIN, this could be an extra safe guard for unprivileged userspace.

Quoted 7 lines
Quoted 3 lines

I intend to allow using this feature without privilege. A VMM is usually unprivileged and requiring a privilege to configure tuntap is too prohibitive.

For safety, tun is not allowed to be created by unprivileged users. And it's not to configure the tuntap dynamically, it's about telling the function that tuntap can have (not necessarily enabled though) .

I don't think we need another barrier for the new functions. Once an unprivileged user get a file descriptor of tuntap from a privileged user, they are free to enable RSS and/or hash reporting.

Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

In the virtio-net specification, it is not defined what would

happen if

Quoted 10 lines
Quoted 3 lines

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Quoted 4 lines

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics?

That's perfectly fine, but it needs to be done in virtio-net's uAPI not tun's. What's more, if you think two commands are not well-defined, let's fix that in the virtio spec first.

Quoted 3 lines

The virtio
spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

See above, let's fix the uAPI first. We don't want DPDK to use tun's uAPI for RSS

Quoted 6 lines
Quoted 3 lines

virtio-net's UAPI is for the virtio spec which has a capable generic feature negotiation mechanism. tuntap needs its own feature negotiation and it's nothing to do with virtio-net's UAPI.

Well, I don't mean the part of the feature negotiation. I mean the part for rss and hash report configuration.

The feature negotiation still matters when deciding the granularity of ioctls. We need one ioctl for a feature negotiation, and to avoid having an intermediate state, the ioctl should also do the configuration. Hence that one ioctl should do all of the feature negotiation and configuration.

Quoted 6 lines
Quoted 6 lines

The structures for two commands have unused or redundant fields and a flexible array in the middle of the structure, but they are ABIs so we can't change it.

Quoted 8 lines
Quoted 5 lines

DPDK is another reason to define tuntap's own UAPIs. They don't care unused or redundant fields and a flexible array in middle that are present in the virtio spec. It will also not want to deal with the requirement of little endian. Constructing struct virtio_net_rss_config is an extra burden for DPDK.

I meant for vhost-user implementation in DPDK, it needs to use virtio-net uAPI not tuntap's for example.

The vhost-user implementation will use tuntap's UAPIs for its ethernet device backend. It uses the generic interface of ethernet device so for RSS it will use functions like rte_eth_dev_rss_hash_update() for example. tuntap's UAPIs are more suited to implement these interfaces as they operate in native endian and don't have extra fields.

DPDk applications other than vhost-user also matter; they do not care what virtio does at all.

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

On the other hand, Constructing tuntap-specific structures is not that complicated for VMMs.

Not complicated but redundant.

Quoted 4 lines

A VMM will need to inspect struct
virtio_net_rss_config anyway to handle migration and check its size so it can store the values it inspected to struct tun_vnet_hash and struct tun_vnet_hash_rss and pass them to the kernel.

Quoted 4 lines

I don't see how rss and hash reports differ from what we have now. Those inspections must be done anyhow for compatibility for example the check of offloading features. Such steps could not be eliminated no matter how we design the uAPI.

I explained the difference between the virtio and tuntap UAPIs, not between RSS and hash reporting.

Quoted 8 lines
Quoted 5 lines

The overall userspace implementation will be simpler by having structures specifically tailored for the communication between the userspace and kernel.

This is exactly how a good uAPI should behave. If uAPI in virtio-net can't do this, I don't understand why uAPI in tuntap can solve it.

The UAPI in virtio-net cannot do it because it's already fixed and it also needs to perform endian conversion for the VM use case. tuntap doesn't have these restrictions.

Quoted 36 lines
Quoted 27 lines
Quoted 27 lines
Quoted 8 lines

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands.

I don't see a connection with the operation code. For example, we can add new uAPIs in virtio-net which could be something like:

   struct virtio_net_rss_config_header {
        __le32 hash_types;
        __le16 indirection_table_mask;
        __le16 unclassified_queue;
        __le16 indirection_table[];
}
struct virtio_net_rss_config_tailer {
        __le16 max_tx_vq;
        u8 hash_key_length;
        u8 hash_key_data[];
}

These two are used by TUNSETVNETRSS. And simply reuse the virtio_net_hash_config for TUNSETVETHASH.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

With this, we can tweak the virtio-net driver with this new uAPI. Then

tap* can reuse this.

I implemented a UAPI and driver change accordingly:
https://lore.kernel.org/r/[email protected]

This is a nice improvement for the driver, but I still don't think it is suited for the UAPI of tuntap.

Quoted 8 lines

Any reason for this? It should work like virtio_net_hdr.

Quoted 3 lines

The requirements of extra fields and
little endian cannot be removed from the virtio spec but they are irrelevant for tuntap.

I don't understand this part. What fields are "extra" and need to be removed from the spec?

All fields not included in struct tun_vnet_hash and struct tun_vnet_hash_rss. Namely, for struct virtio_net_hash_config:
- reserved
- hash_key_length
- hash_key_data

For struct virtio_net_rss_config:
- max_tx_vq
- hash_key_length

Regards,
Akihiko Odaki

Quoted 54 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The best possibility is to share structures, not commands, and I don't think even sharing structures makes sense here because of the reasons described above.

Quoted 8 lines
Quoted 3 lines

I don't want to share structures, I meant starting from something that is simple and has been sorted in the virtio spec. Optimization could be done on top.

I meant to reuse the structures in virtio_net.h.

Regards,
Akihiko Odaki

Thanks

Quoted 35 lines
Quoted 34 lines

Thanks

Quoted 28 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

Acked-by Michael S. Tsirkin
#26 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 481 lines · Akihiko Odaki
Quoted 172 lines · Jason Wang
Quoted 171 lines · Akihiko Odaki
Quoted 132 lines · Jason Wang
Quoted 131 lines · Akihiko Odaki
Quoted 129 lines · Jason Wang
Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang
Quoted 50 lines · Akihiko Odaki

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.

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.

Signed-off-by: Akihiko Odaki <[email protected]>
Tested-by: Lei Yang <[email protected]>
---
Documentation/networking/tuntap.rst | 7 ++
drivers/net/Kconfig | 1 +
drivers/net/tap.c | 68 ++++++++++++++-
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, 386 insertions(+), 31 deletions(-)

diff --git a/Documentation/networking/tuntap.rst b/Documentation/networking/tuntap.rst
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
--- a/Documentation/networking/tuntap.rst
+++ b/Documentation/networking/tuntap.rst
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
}

[...]

Quoted 28 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 2 lines

+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+ bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires to make eBPF and RSS mutually exclusive. I still don't understand why we need this. Allow eBPF program to override some of the path seems to be common practice.

What's more, we didn't try (or even can't) to make automq and eBPF to be mutually exclusive. So I still didn't see what we gain from this and it complicates the codes and may lead to ambiguous uAPI/behaviour.

Quoted 5 lines
Quoted 2 lines

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

     > >>
Quoted 10 lines
Quoted 2 lines

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

I referred to the eBPF steering program instead of tc-ebpf. tc-ebpf is nothing to do with the TUNSETSTEERINGEBPF ioctl, which this patch changes.

I meant you can't do "full control" in any case, the point below doesn't stand. Queue mapping could be restored even if RSS is set.

What matters here is how we handle the control when tc didn't take it. eBPF, RSS, or automq make take all of it; I referred that as "full control".

Quoted 24 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

Quoted 8 lines

I wrote: "because it is not obvious whether we should choose either 1) or 2)", but I think I can explain it better:

When an eBPF steering program cannot implement a fallback, it means the eBPF steering program requests the full control over the steering. On the other hand, RSS also requests the same control. So these two will conflict and the entity controlling the steering will be undefined when both are enabled.

Well, the fallback is orthogonal to the proposal here. We haven't had that since the introduction of the eBPF steering program. This means automq has been in "conflict" with eBPF for years. Again, another advantage, allowing the eBPF program to be the first to allow the management layer to override Qemu's steering.

Quoted 8 lines
Quoted 2 lines

What if a VMM uses eBPF steering program and the management layer decides to override it with RSS?

That's possible but I think we're seeking which approach is better. In this case, RSS could be implemented in eBPF but not the reverse.

So my point is to start from something that is simpler. Simply allow eBPF on top of RSS as automq. And optimize on top.

The in-kernel RSS implementation is more optimized and capable of hash reporting. I don't think either eBPF steering program or in-kernel RSS is more capable than the other and there is a reason to place eBPF on top of RSS.

Quoted 10 lines
Quoted 5 lines

eBPF is obviously predecedent to automq as eBPF is an opt-in feature and automq is the implicit default. But this logic cannot be applied to decide the order of eBPF and RSS because they are both opt-in features.

This is from the perspective of kernel development. But let's try to think from the userspace: A well written user space knows what it does, rejecting eBPF while RSS is set doesn't help. But anyhow if you stick, it doesn't harm.

Yes, it not for the current userspace but for the future kernel development; the kernel can reserve the freedom to decide the priority of eBPF and RSS by rejecting eBPF while RSS.

Quoted 171 lines
Quoted 166 lines
Quoted 8 lines
Quoted 5 lines

3) eliminates the undefined semantics by rejecting to enable both.

This would lead a usersapce noticeable change of the behaviour? And what do you mean by "rejecting to enable both"?

Existing userspace code should see no change as it only cares the case where RSS is enabled.

Here, rejecting to enable both means to deny setting an eBPF steering program when RSS is enabled, and visa-versa.

Quoted 10 lines
Quoted 6 lines

An
alternative approach is to allow eBPF steering programs to fall back. When both the eBPF program and RSS are enabled, RSS will gain the control of steering under the well-defined situation where the eBPF steering program decides to fall back.

How about just stick the eBPF precedence in this proposal and introduce the fallback on top? This helps to speed up the iteration (as the version has been iterated to 11).

I don't think that helps much since we have another ongoing discussion below and it is not the sole roadblock.

Quoted 129 lines
Quoted 69 lines
Quoted 62 lines
Quoted 23 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT? If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
       le32 hash_types;
       le16 reserved[4];
       u8 hash_key_length;
       u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""

struct rss_rq_id {
      le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */
      le16 reserved: 1; /* Set to zero */
};
struct virtio_net_rss_config {
       le32 hash_types;
       le16 indirection_table_mask;
       struct rss_rq_id unclassified_queue;
       struct rss_rq_id indirection_table[indirection_table_length];
       le16 max_tx_vq;
       u8 hash_key_length;
       u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

Quoted 4 lines

The spec also defines struct virtio_net_hash_config (which will be used when RSS is disabled) and struct virtio_net_rss_config to match the layout to share some fields. However, the UAPI does not follow the interface design of virtio due to some problems with these structures.

Copy-paste error. The above is copied from the virtio spec, but I meant the existing uAPI in virtio_net.h:

/*
* The command VIRTIO_NET_CTRL_MQ_RSS_CONFIG has the same effect as
* VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET does and additionally configures
* the receive steering to use a hash calculated for incoming packet
* to decide on receive virtqueue to place the packet. The command
* also provides parameters to calculate a hash and receive virtqueue. */

struct virtio_net_rss_config {
          __le32 hash_types;
          __le16 indirection_table_mask;
          __le16 unclassified_queue;
          __le16 indirection_table[1/* + indirection_table_mask */];
          __le16 max_tx_vq;
          __u8 hash_key_length;
          __u8 hash_key_data[/* hash_key_length */];
};
Quoted 1 line
   #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1

/*
* The command VIRTIO_NET_CTRL_MQ_HASH_CONFIG requests the device
* to include in the virtio header of the packet the value of the
* calculated hash and the report type of hash. It also provides
* parameters for hash calculation. The command requires feature
* VIRTIO_NET_F_HASH_REPORT to be negotiated to extend the
* layout of virtio header as defined in virtio_net_hdr_v1_hash. */

struct virtio_net_hash_config {
          __le32 hash_types;
          /* for compatibility with virtio_net_rss_config */
          __le16 reserved[4];
          __u8 hash_key_length;
          __u8 hash_key_data[/* hash_key_length */];
};

This has been used by Qemu but I see a virtio-net version of:

struct virtio_net_ctrl_rss {
          u32 hash_types;
          u16 indirection_table_mask;
          u16 unclassified_queue;
          u16 hash_cfg_reserved; /* for HASH_CONFIG (see
virtio_net_hash_config for details) */
          u16 max_tx_vq;
          u8 hash_key_length;
          u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

          u16 *indirection_table;
};

This is ugly and results in a tricky code when trying to submit RSS/HASH commands to the device:

Quoted 7 lines
          if (vi->has_rss) {
                  sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
                  sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
          } else {
                  sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved,
sizeof(uint16_t));
          }
Quoted 4 lines

The only reference to struct virtio_net_rss_config in QEMU is to derive the offset of indirection_table. This is because the definition in virtio_net.h also includes indirection_table in the middle and the offsets of later part are unusable.

Quoted 7 lines

Yes.

Quoted 3 lines

QEMU internally has a structure named VirtioNetRssData which just looks like struct virtio_net_ctrl_rss.

It's a pity that it doesn't use uAPI. We might need to fix them.

It doesn't want to use the UAPI structures for the internal storage because it wants to store them in native endians and QEMU is not interested in some fields in the UAPI structures. struct tun_vnet_hash and struct tun_vnet_hash_rss are easy to fill using VirtioNetRssData.

Quoted 93 lines
Quoted 81 lines
Quoted 24 lines
Quoted 13 lines

Below is the definition of struct virtio_net_hash_config:

struct virtio_net_hash_config {
       le32 hash_types;
       le16 reserved[4];
       u8 hash_key_length;
       u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

Here, hash_types, hash_key_length, and hash_key_data are shared with struct virtio_net_rss_config.

One problem is that struct virtio_net_rss_config has a flexible array (indirection_table) between hash_types and hash_key_length. This is something we cannot express with C.

We can split the virtio_net_rss_config to ease the dealing with arrays, more below.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Another problem is that the semantics of the key in struct virtio_net_hash_config is not defined in the spec.

If this is the case. Let's fix that in the spec first to make sure our uAPI aligns with spec without ambiguity. It would be a nightmare to deal with the in-consistency between virtio spec and Linux uAPIs.

The userspace doesn't need to do anything to deal with inconsistency since these fields are unused.

Quoted 16 lines
Quoted 16 lines

To solve these problems, I defined the UAPI structures that do not include indiretion_table.

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

The UAPI shares struct tun_vnet_hash for both hash report and rss.

I meant sharing structure in two ioctls instead of reusing a specific structure for two semantics in one ioctl if possible. Though I don't think we need any sharing.

The UAPI implemented in this patch already shares struct tun_vnet_hash and having two ioctls doesn't change that.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

And it seems to lack parameters like max_tx_vq.

max_tx_vq is not relevant with hashing.

It is needed for RSS and we don't have that, no?

No. RSS is Receive Side Scaling but it's not about receiving.

Just to make sure I understand this, max_tx_vq is part of the virtio_net_rss_config, how would Qemu behave when it receives this from guest?

Quoted 6 lines
Quoted 4 lines

"""
A driver sets max_tx_vq to inform a device how many transmit virtqueues it may use (transmitq1…transmitq max_tx_vq). """

It does nothing.

Nope, see:

commit 50bfcaedd78e53135ec0504302269b3b65bf1eff

Author: Philo Lu <[email protected]>
Date:   Mon Nov 4 16:57:06 2024 +0800

virtio_net: Update rss when set queue

RSS configuration should be updated with queue number. In particular, it should be updated when (1) rss enabled and (2) default rss configuration is used without user modification.

During rss command processing, device updates queue_pairs using rss.max_tx_vq. That is, the device updates queue_pairs together with rss, so we can skip the sperate queue_pairs update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly.

Also remove the vi->has_rss ? check when setting vi->rss.max_tx_vq, because this is not used in the other hash_report case.

Fixes c7114b1249fa ("drivers/net/virtio_net: Added basic RSS support.")

RSS doesn't depend on MQ.

If it is not handled by Qemu, it should be a bug?

Quoted 99 lines
Quoted 76 lines
Quoted 76 lines
Quoted 76 lines
Quoted 75 lines
Quoted 7 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 54 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:

Quoted 8 lines

A device attempts to calculate a per-packet hash in the following cases:

- The feature VIRTIO_NET_F_RSS was negotiated. The device uses the hash to determine the receive virtqueue to place incoming packets.
- The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device reports the hash value and the hash type with the packet.

Quoted 5 lines

If the feature VIRTIO_NET_F_RSS was negotiated:

- The device uses hash_types of the virtio_net_rss_config structure as ’Enabled hash types’ bitmask.
- The device uses a key as defined in hash_key_data and

hash_key_length of the virtio_net_rss_config structure (see
> 5.1.6.5.7.1).
>
> If the feature VIRTIO_NET_F_RSS was not negotiated:
>
> - The device uses hash_types of the virtio_net_hash_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
> hash_key_length of the virtio_net_hash_config structure (see
> .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

Quoted 7 lines

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

TUNSETIFF, enables the device to be able to handle RSS/HASREPORT. TUNSETHASH/RSS. dealing with RSS/HASH command from userspace.

We also needs to be able to disable them at runtime so that we can handle resets.

Via TUNSETHASH/RSS? I think it should have a way to accept parameters that disable RSS or hash report.

That's what this patch implements. TUNSETVNETHASH accepts parameters to choose what features to be enabled.

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

This is the way we used to do for multi queue and vnet header. TUNSETIFF requires CAP_NET_ADMIN, this could be an extra safe guard for unprivileged userspace.

I intend to allow using this feature without privilege. A VMM is usually unprivileged and requiring a privilege to configure tuntap is too prohibitive.

Quoted 7 lines
Quoted 3 lines

For safety, tun is not allowed to be created by unprivileged users. And it's not to configure the tuntap dynamically, it's about telling the function that tuntap can have (not necessarily enabled though) .

I don't think we need another barrier for the new functions. Once an unprivileged user get a file descriptor of tuntap from a privileged user, they are free to enable RSS and/or hash reporting.

Only if such a feature is allowed by the privileged user.

Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Quoted 27 lines
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

In the virtio-net specification, it is not defined what would

happen if

Quoted 10 lines
Quoted 3 lines

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Quoted 4 lines

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics?

That's perfectly fine, but it needs to be done in virtio-net's uAPI not tun's. What's more, if you think two commands are not well-defined, let's fix that in the virtio spec first.

Quoted 6 lines
Quoted 3 lines

The virtio
spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

See above, let's fix the uAPI first. We don't want DPDK to use tun's uAPI for RSS

virtio-net's UAPI is for the virtio spec which has a capable generic feature negotiation mechanism. tuntap needs its own feature negotiation and it's nothing to do with virtio-net's UAPI.

Quoted 6 lines
Quoted 2 lines

Well, I don't mean the part of the feature negotiation. I mean the part for rss and hash report configuration.

The feature negotiation still matters when deciding the granularity of ioctls. We need one ioctl for a feature negotiation, and to avoid having an intermediate state,

I don't understand this. For example, driver can choose to

1) negotiate RSS
2) do something else.
3) configure RSS

Spec doesn't require those two to be configured at the same time, so "intermediate state" is allowed.

Quoted 15 lines

the ioctl should also do the configuration. Hence
that one ioctl should do all of the feature negotiation and configuration.

Quoted 12 lines
Quoted 11 lines

The structures for two commands have unused or redundant fields and a flexible array in the middle of the structure, but they are ABIs so we can't change it.

DPDK is another reason to define tuntap's own UAPIs. They don't care unused or redundant fields and a flexible array in middle that are present in the virtio spec. It will also not want to deal with the requirement of little endian. Constructing struct virtio_net_rss_config is an extra burden for DPDK.

Quoted 5 lines
Quoted 2 lines

I meant for vhost-user implementation in DPDK, it needs to use virtio-net uAPI not tuntap's for example.

The vhost-user implementation will use tuntap's UAPIs for its ethernet device backend.

That sounds pretty weird, vhost-user has nothing related to tuntap.

Quoted 4 lines

It uses the generic interface of ethernet device so for
RSS it will use functions like rte_eth_dev_rss_hash_update() for example. tuntap's UAPIs are more suited to implement these interfaces as they operate in native endian and don't have extra fields.

Nope, for example it needs to use le for virtio_net_hdr if a modern device is used. But it needs a "native" endian according to the guest endian via TUNSETVNETLE/BE. We don't have a choice as virtio-net hdr support in tuntap is much earlier than modern devices.

Let's don't do the same thing (native endian) for tuntap as RSS depends on modern, so we know it must be le.

Quoted 15 lines

DPDk applications other than vhost-user also matter; they do not care what virtio does at all.

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

On the other hand, Constructing tuntap-specific structures is not that complicated for VMMs.

Not complicated but redundant.

Quoted 4 lines

A VMM will need to inspect struct
virtio_net_rss_config anyway to handle migration and check its size so it can store the values it inspected to struct tun_vnet_hash and struct tun_vnet_hash_rss and pass them to the kernel.

Quoted 7 lines
Quoted 4 lines

I don't see how rss and hash reports differ from what we have now. Those inspections must be done anyhow for compatibility for example the check of offloading features. Such steps could not be eliminated no matter how we design the uAPI.

I explained the difference between the virtio and tuntap UAPIs, not between RSS and hash reporting.

See above.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The overall userspace implementation will be simpler by having structures specifically tailored for the communication between the userspace and kernel.

Quoted 6 lines
Quoted 2 lines

This is exactly how a good uAPI should behave. If uAPI in virtio-net can't do this, I don't understand why uAPI in tuntap can solve it.

The UAPI in virtio-net cannot do it because it's already fixed and it also needs to perform endian conversion for the VM use case. tuntap doesn't have these restrictions.

Same here.

Quoted 52 lines
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
Quoted 9 lines

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands.

I don't see a connection with the operation code. For example, we can add new uAPIs in virtio-net which could be something like:

   struct virtio_net_rss_config_header {
        __le32 hash_types;
        __le16 indirection_table_mask;
        __le16 unclassified_queue;
        __le16 indirection_table[];
}
struct virtio_net_rss_config_tailer {
        __le16 max_tx_vq;
        u8 hash_key_length;
        u8 hash_key_data[];
}

These two are used by TUNSETVNETRSS. And simply reuse the virtio_net_hash_config for TUNSETVETHASH.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

With this, we can tweak the virtio-net driver with this new uAPI. Then

tap* can reuse this.

I implemented a UAPI and driver change accordingly:
https://lore.kernel.org/r/[email protected]

This is a nice improvement for the driver, but I still don't think it is suited for the UAPI of tuntap.

Quoted 8 lines

Any reason for this? It should work like virtio_net_hdr.

Quoted 3 lines

The requirements of extra fields and
little endian cannot be removed from the virtio spec but they are irrelevant for tuntap.

I don't understand this part. What fields are "extra" and need to be removed from the spec?

All fields not included in struct tun_vnet_hash and struct tun_vnet_hash_rss. Namely, for struct virtio_net_hash_config:
- reserved
- hash_key_length
- hash_key_data

Quoted 3 lines

For struct virtio_net_rss_config:
- max_tx_vq
- hash_key_length

See my above reply, and I basically meant

TUNSETVETHASH accept struct virtio_net_hash_config; TUNSETVETRSS accept struct virtio_net_rss_config_hdr + struct virtio_net_rss_config_trailer;

Thanks

Quoted 59 lines

Regards,
Akihiko Odaki

Quoted 54 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The best possibility is to share structures, not commands, and I don't think even sharing structures makes sense here because of the reasons described above.

Quoted 8 lines
Quoted 3 lines

I don't want to share structures, I meant starting from something that is simple and has been sorted in the virtio spec. Optimization could be done on top.

I meant to reuse the structures in virtio_net.h.

Regards,
Akihiko Odaki

Thanks

Quoted 35 lines
Quoted 34 lines

Thanks

Quoted 28 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#27 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 515 lines · Jason Wang
Quoted 481 lines · Akihiko Odaki
Quoted 172 lines · Jason Wang
Quoted 171 lines · Akihiko Odaki
Quoted 132 lines · Jason Wang
Quoted 131 lines · Akihiko Odaki
Quoted 129 lines · Jason Wang
Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki
Quoted 51 lines · Jason Wang

On Fri, Mar 7, 2025 at 7:01 PM Akihiko Odaki <[email protected]> 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.

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.

Signed-off-by: Akihiko Odaki <[email protected]>
Tested-by: Lei Yang <[email protected]>
---
Documentation/networking/tuntap.rst | 7 ++
drivers/net/Kconfig | 1 +
drivers/net/tap.c | 68 ++++++++++++++-
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, 386 insertions(+), 31 deletions(-)

diff --git a/Documentation/networking/tuntap.rst b/Documentation/networking/tuntap.rst
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
--- a/Documentation/networking/tuntap.rst
+++ b/Documentation/networking/tuntap.rst
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
}

[...]

Quoted 28 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+ bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires
to make eBPF and RSS mutually exclusive. I still don't understand why
we need this. Allow eBPF program to override some of the path seems to
be common practice.

What's more, we didn't try (or even can't) to make automq and eBPF to
be mutually exclusive. So I still didn't see what we gain from this
and it complicates the codes and may lead to ambiguous uAPI/behaviour.

Quoted 5 lines
Quoted 2 lines

automq and eBPF are mutually exclusive; automq is disabled when an eBPF steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

      > >>
Quoted 10 lines
Quoted 2 lines

We don't even have an interface for eBPF to let it fall back to another alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering program is designed to fall back to automq when it returns e.g., -1. But such an interface is currently not defined and defining one is out of scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

I referred to the eBPF steering program instead of tc-ebpf. tc-ebpf is nothing to do with the TUNSETSTEERINGEBPF ioctl, which this patch changes.

I meant you can't do "full control" in any case, the point below doesn't stand. Queue mapping could be restored even if RSS is set.

What matters here is how we handle the control when tc didn't take it. eBPF, RSS, or automq make take all of it; I referred that as "full control".

Quoted 24 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

Quoted 8 lines

I wrote: "because it is not obvious whether we should choose either 1) or 2)", but I think I can explain it better:

When an eBPF steering program cannot implement a fallback, it means the eBPF steering program requests the full control over the steering. On the other hand, RSS also requests the same control. So these two will conflict and the entity controlling the steering will be undefined when both are enabled.

Well, the fallback is orthogonal to the proposal here. We haven't had that since the introduction of the eBPF steering program. This means automq has been in "conflict" with eBPF for years. Again, another advantage, allowing the eBPF program to be the first to allow the management layer to override Qemu's steering.

Quoted 8 lines
Quoted 2 lines

What if a VMM uses eBPF steering program and the management layer decides to override it with RSS?

That's possible but I think we're seeking which approach is better. In this case, RSS could be implemented in eBPF but not the reverse.

So my point is to start from something that is simpler. Simply allow eBPF on top of RSS as automq. And optimize on top.

The in-kernel RSS implementation is more optimized and capable of hash reporting. I don't think either eBPF steering program or in-kernel RSS is more capable than the other and there is a reason to place eBPF on top of RSS.

Quoted 10 lines
Quoted 5 lines

eBPF is obviously predecedent to automq as eBPF is an opt-in feature and automq is the implicit default. But this logic cannot be applied to decide the order of eBPF and RSS because they are both opt-in features.

This is from the perspective of kernel development. But let's try to think from the userspace: A well written user space knows what it does, rejecting eBPF while RSS is set doesn't help. But anyhow if you stick, it doesn't harm.

Yes, it not for the current userspace but for the future kernel development; the kernel can reserve the freedom to decide the priority of eBPF and RSS by rejecting eBPF while RSS.

Quoted 171 lines
Quoted 166 lines
Quoted 8 lines
Quoted 5 lines

3) eliminates the undefined semantics by rejecting to enable both.

This would lead a usersapce noticeable change of the behaviour? And what do you mean by "rejecting to enable both"?

Existing userspace code should see no change as it only cares the case where RSS is enabled.

Here, rejecting to enable both means to deny setting an eBPF steering program when RSS is enabled, and visa-versa.

Quoted 10 lines
Quoted 6 lines

An
alternative approach is to allow eBPF steering programs to fall back. When both the eBPF program and RSS are enabled, RSS will gain the control of steering under the well-defined situation where the eBPF steering program decides to fall back.

How about just stick the eBPF precedence in this proposal and introduce the fallback on top? This helps to speed up the iteration (as the version has been iterated to 11).

I don't think that helps much since we have another ongoing discussion below and it is not the sole roadblock.

Quoted 129 lines
Quoted 69 lines
Quoted 62 lines
Quoted 23 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT?
If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
        le32 hash_types;
        le16 reserved[4];
        u8 hash_key_length;
        u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""

struct rss_rq_id {
       le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */
       le16 reserved: 1; /* Set to zero */
};
struct virtio_net_rss_config {
        le32 hash_types;
        le16 indirection_table_mask;
        struct rss_rq_id unclassified_queue;
        struct rss_rq_id indirection_table[indirection_table_length];
        le16 max_tx_vq;
        u8 hash_key_length;
        u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

Quoted 4 lines

The spec also defines struct virtio_net_hash_config (which will be used when RSS is disabled) and struct virtio_net_rss_config to match the layout to share some fields. However, the UAPI does not follow the interface design of virtio due to some problems with these structures.

Copy-paste error. The above is copied from the virtio spec, but I meant the existing uAPI in virtio_net.h:

/*
* The command VIRTIO_NET_CTRL_MQ_RSS_CONFIG has the same effect as
* VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET does and additionally configures
* the receive steering to use a hash calculated for incoming packet
* to decide on receive virtqueue to place the packet. The command
* also provides parameters to calculate a hash and receive virtqueue. */

struct virtio_net_rss_config {
           __le32 hash_types;
           __le16 indirection_table_mask;
           __le16 unclassified_queue;
           __le16 indirection_table[1/* + indirection_table_mask */];
           __le16 max_tx_vq;
           __u8 hash_key_length;
           __u8 hash_key_data[/* hash_key_length */];
};
Quoted 1 line
    #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1

/*
* The command VIRTIO_NET_CTRL_MQ_HASH_CONFIG requests the device
* to include in the virtio header of the packet the value of the
* calculated hash and the report type of hash. It also provides
* parameters for hash calculation. The command requires feature
* VIRTIO_NET_F_HASH_REPORT to be negotiated to extend the
* layout of virtio header as defined in virtio_net_hdr_v1_hash. */

struct virtio_net_hash_config {
           __le32 hash_types;
           /* for compatibility with virtio_net_rss_config */
           __le16 reserved[4];
           __u8 hash_key_length;
           __u8 hash_key_data[/* hash_key_length */];
};

This has been used by Qemu but I see a virtio-net version of:

struct virtio_net_ctrl_rss {
           u32 hash_types;
           u16 indirection_table_mask;
           u16 unclassified_queue;
           u16 hash_cfg_reserved; /* for HASH_CONFIG (see
virtio_net_hash_config for details) */
           u16 max_tx_vq;
           u8 hash_key_length;
           u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

           u16 *indirection_table;
};

This is ugly and results in a tricky code when trying to submit RSS/HASH commands to the device:

Quoted 7 lines
           if (vi->has_rss) {
                   sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
                   sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
           } else {
                   sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved,
sizeof(uint16_t));
           }
Quoted 4 lines

The only reference to struct virtio_net_rss_config in QEMU is to derive the offset of indirection_table. This is because the definition in virtio_net.h also includes indirection_table in the middle and the offsets of later part are unusable.

Quoted 7 lines

Yes.

Quoted 3 lines

QEMU internally has a structure named VirtioNetRssData which just looks like struct virtio_net_ctrl_rss.

It's a pity that it doesn't use uAPI. We might need to fix them.

It doesn't want to use the UAPI structures for the internal storage because it wants to store them in native endians and QEMU is not interested in some fields in the UAPI structures. struct tun_vnet_hash and struct tun_vnet_hash_rss are easy to fill using VirtioNetRssData.

Quoted 93 lines
Quoted 81 lines
Quoted 24 lines
Quoted 13 lines

Below is the definition of struct virtio_net_hash_config:

struct virtio_net_hash_config {
        le32 hash_types;
        le16 reserved[4];
        u8 hash_key_length;
        u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

Here, hash_types, hash_key_length, and hash_key_data are shared with struct virtio_net_rss_config.

One problem is that struct virtio_net_rss_config has a flexible array (indirection_table) between hash_types and hash_key_length. This is something we cannot express with C.

We can split the virtio_net_rss_config to ease the dealing with arrays, more below.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Another problem is that the semantics of the key in struct virtio_net_hash_config is not defined in the spec.

If this is the case. Let's fix that in the spec first to make sure our uAPI aligns with spec without ambiguity. It would be a nightmare to deal with the in-consistency between virtio spec and Linux uAPIs.

The userspace doesn't need to do anything to deal with inconsistency since these fields are unused.

Quoted 16 lines
Quoted 16 lines

To solve these problems, I defined the UAPI structures that do not include indiretion_table.

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

The UAPI shares struct tun_vnet_hash for both hash report and rss.

I meant sharing structure in two ioctls instead of reusing a specific structure for two semantics in one ioctl if possible. Though I don't think we need any sharing.

The UAPI implemented in this patch already shares struct tun_vnet_hash and having two ioctls doesn't change that.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

And it seems to lack parameters like max_tx_vq.

max_tx_vq is not relevant with hashing.

It is needed for RSS and we don't have that, no?

No. RSS is Receive Side Scaling but it's not about receiving.

Just to make sure I understand this, max_tx_vq is part of the virtio_net_rss_config, how would Qemu behave when it receives this from guest?

Quoted 6 lines
Quoted 4 lines

"""
A driver sets max_tx_vq to inform a device how many transmit virtqueues it may use (transmitq1…transmitq max_tx_vq). """

It does nothing.

Nope, see:

commit 50bfcaedd78e53135ec0504302269b3b65bf1eff

Author: Philo Lu <[email protected]>
Date:   Mon Nov 4 16:57:06 2024 +0800

virtio_net: Update rss when set queue

RSS configuration should be updated with queue number. In particular, it should be updated when (1) rss enabled and (2) default rss configuration is used without user modification.

During rss command processing, device updates queue_pairs using rss.max_tx_vq. That is, the device updates queue_pairs together with rss, so we can skip the sperate queue_pairs update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly.

Also remove the vi->has_rss ? check when setting vi->rss.max_tx_vq, because this is not used in the other hash_report case.

Fixes c7114b1249fa ("drivers/net/virtio_net: Added basic RSS support.")
Quoted 3 lines

RSS doesn't depend on MQ.

If it is not handled by Qemu, it should be a bug?

I was wrong; QEMU does handle this field, but it doesn't use the definition of struct virtio_net_rss_config and name it queue_pairs instead of max_tx_vq so I could not find it by grep.

For tap, max_tx_vq is handled by changing the number of open file descriptors so passing it via an ioctl is redundant.

Quoted 104 lines
Quoted 104 lines
Quoted 77 lines
Quoted 77 lines
Quoted 77 lines
Quoted 76 lines
Quoted 8 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 54 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:
> A device attempts to calculate a per-packet hash in the following > cases:
>
> - The feature VIRTIO_NET_F_RSS was negotiated. The device uses the
> hash to determine the receive virtqueue to place incoming packets.
> - The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device
> reports the hash value and the hash type with the packet.
>
> If the feature VIRTIO_NET_F_RSS was negotiated:
>
> - The device uses hash_types of the virtio_net_rss_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
hash_key_length of the virtio_net_rss_config structure (see
> 5.1.6.5.7.1).
>
> If the feature VIRTIO_NET_F_RSS was not negotiated:
>
> - The device uses hash_types of the virtio_net_hash_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
> hash_key_length of the virtio_net_hash_config structure (see
> .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

Quoted 7 lines

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

TUNSETIFF, enables the device to be able to handle RSS/HASREPORT. TUNSETHASH/RSS. dealing with RSS/HASH command from userspace.

We also needs to be able to disable them at runtime so that we can handle resets.

Via TUNSETHASH/RSS? I think it should have a way to accept parameters that disable RSS or hash report.

That's what this patch implements. TUNSETVNETHASH accepts parameters to choose what features to be enabled.

Quoted 14 lines
Quoted 10 lines
Quoted 6 lines

This is the way we used to do for multi queue and vnet header. TUNSETIFF requires CAP_NET_ADMIN, this could be an extra safe guard for unprivileged userspace.

I intend to allow using this feature without privilege. A VMM is usually unprivileged and requiring a privilege to configure tuntap is too prohibitive.

For safety, tun is not allowed to be created by unprivileged users. And it's not to configure the tuntap dynamically, it's about telling the function that tuntap can have (not necessarily enabled though) .

Quoted 5 lines
Quoted 3 lines

I don't think we need another barrier for the new functions. Once an unprivileged user get a file descriptor of tuntap from a privileged user, they are free to enable RSS and/or hash reporting.

Only if such a feature is allowed by the privileged user.

I don't see a reason not to allow the feature to unprivileged users. It only complicates the setup.

Quoted 46 lines
Quoted 39 lines
Quoted 39 lines
Quoted 38 lines
Quoted 28 lines
Quoted 19 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

In the virtio-net specification, it is not defined what would

happen if

Quoted 10 lines
Quoted 3 lines

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Quoted 4 lines

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics?

That's perfectly fine, but it needs to be done in virtio-net's uAPI not tun's. What's more, if you think two commands are not well-defined, let's fix that in the virtio spec first.

Quoted 6 lines
Quoted 3 lines

The virtio
spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

See above, let's fix the uAPI first. We don't want DPDK to use tun's uAPI for RSS

virtio-net's UAPI is for the virtio spec which has a capable generic feature negotiation mechanism. tuntap needs its own feature negotiation and it's nothing to do with virtio-net's UAPI.

Quoted 6 lines
Quoted 2 lines

Well, I don't mean the part of the feature negotiation. I mean the part for rss and hash report configuration.

The feature negotiation still matters when deciding the granularity of ioctls. We need one ioctl for a feature negotiation, and to avoid having an intermediate state,

Quoted 8 lines

I don't understand this. For example, driver can choose to

1) negotiate RSS
2) do something else.
3) configure RSS

Spec doesn't require those two to be configured at the same time, so "intermediate state" is allowed.

The spec doesn't define what should happen in the intermediate state either.

For a hardware implementation I think it's fine whatever the implementation defines as the intermediate state. But for the UAPI, it's better avoiding having such a definition to keep the interface minimal and maximize the UAPI stability.

Quoted 16 lines
Quoted 16 lines

the ioctl should also do the configuration. Hence
that one ioctl should do all of the feature negotiation and configuration.

Quoted 12 lines
Quoted 11 lines

The structures for two commands have unused or redundant fields and a flexible array in the middle of the structure, but they are ABIs so we can't change it.

DPDK is another reason to define tuntap's own UAPIs. They don't care unused or redundant fields and a flexible array in middle that are present in the virtio spec. It will also not want to deal with the requirement of little endian. Constructing struct virtio_net_rss_config is an extra burden for DPDK.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I meant for vhost-user implementation in DPDK, it needs to use virtio-net uAPI not tuntap's for example.

The vhost-user implementation will use tuntap's UAPIs for its ethernet device backend.

That sounds pretty weird, vhost-user has nothing related to tuntap.

My expression in the last email was weird. More precisely, the ethernet backend of tuntap will use the UAPIs, and the vhost-user will use the ethernet backend in turn.

Quoted 6 lines
Quoted 5 lines

It uses the generic interface of ethernet device so for
RSS it will use functions like rte_eth_dev_rss_hash_update() for example. tuntap's UAPIs are more suited to implement these interfaces as they operate in native endian and don't have extra fields.

Quoted 7 lines

Nope, for example it needs to use le for virtio_net_hdr if a modern device is used. But it needs a "native" endian according to the guest endian via TUNSETVNETLE/BE. We don't have a choice as virtio-net hdr support in tuntap is much earlier than modern devices.

Let's don't do the same thing (native endian) for tuntap as RSS depends on modern, so we know it must be le.

virtio_net_hdr is the data path while the current discussion is about the control path. All configuration knobs of tuntap operates in the native endian.

So I think we should stick to the little endian for the data path while we should stick to the native endian for the control path to maximize the consistency.

Quoted 99 lines
Quoted 17 lines

DPDk applications other than vhost-user also matter; they do not care what virtio does at all.

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

On the other hand, Constructing tuntap-specific structures is not that complicated for VMMs.

Not complicated but redundant.

Quoted 4 lines

A VMM will need to inspect struct
virtio_net_rss_config anyway to handle migration and check its size so it can store the values it inspected to struct tun_vnet_hash and struct tun_vnet_hash_rss and pass them to the kernel.

Quoted 7 lines
Quoted 4 lines

I don't see how rss and hash reports differ from what we have now. Those inspections must be done anyhow for compatibility for example the check of offloading features. Such steps could not be eliminated no matter how we design the uAPI.

I explained the difference between the virtio and tuntap UAPIs, not between RSS and hash reporting.

See above.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The overall userspace implementation will be simpler by having structures specifically tailored for the communication between the userspace and kernel.

Quoted 6 lines
Quoted 2 lines

This is exactly how a good uAPI should behave. If uAPI in virtio-net can't do this, I don't understand why uAPI in tuntap can solve it.

The UAPI in virtio-net cannot do it because it's already fixed and it also needs to perform endian conversion for the VM use case. tuntap doesn't have these restrictions.

Same here.

Quoted 52 lines
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
Quoted 9 lines

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands.

I don't see a connection with the operation code. For example, we can add new uAPIs in virtio-net which could be something like:

    struct virtio_net_rss_config_header {
         __le32 hash_types;
         __le16 indirection_table_mask;
         __le16 unclassified_queue;
         __le16 indirection_table[];
}
struct virtio_net_rss_config_tailer {
         __le16 max_tx_vq;
         u8 hash_key_length;
         u8 hash_key_data[];
}

These two are used by TUNSETVNETRSS. And simply reuse the virtio_net_hash_config for TUNSETVETHASH.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

With this, we can tweak the virtio-net driver with this new uAPI. Then

tap* can reuse this.

I implemented a UAPI and driver change accordingly:
https://lore.kernel.org/r/[email protected]

This is a nice improvement for the driver, but I still don't think it is suited for the UAPI of tuntap.

Quoted 8 lines

Any reason for this? It should work like virtio_net_hdr.

Quoted 3 lines

The requirements of extra fields and
little endian cannot be removed from the virtio spec but they are irrelevant for tuntap.

I don't understand this part. What fields are "extra" and need to be removed from the spec?

All fields not included in struct tun_vnet_hash and struct tun_vnet_hash_rss. Namely, for struct virtio_net_hash_config:
- reserved
- hash_key_length
- hash_key_data

Quoted 3 lines

For struct virtio_net_rss_config:
- max_tx_vq
- hash_key_length

Quoted 5 lines

See my above reply, and I basically meant

TUNSETVETHASH accept struct virtio_net_hash_config; TUNSETVETRSS accept struct virtio_net_rss_config_hdr + struct virtio_net_rss_config_trailer;

That still bring the extra fields I noted in the last email.

Regards,
Akihiko Odaki

Quoted 63 lines

Thanks

Quoted 59 lines

Regards,
Akihiko Odaki

Quoted 54 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The best possibility is to share structures, not commands, and I don't think even sharing structures makes sense here because of the reasons described above.

Quoted 8 lines
Quoted 3 lines

I don't want to share structures, I meant starting from something that is simple and has been sorted in the virtio spec. Optimization could be done on top.

I meant to reuse the structures in virtio_net.h.

Regards,
Akihiko Odaki

Thanks

Quoted 35 lines
Quoted 34 lines

Thanks

Quoted 28 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#28 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 517 lines · Akihiko Odaki
Quoted 515 lines · Jason Wang
Quoted 481 lines · Akihiko Odaki
Quoted 172 lines · Jason Wang
Quoted 171 lines · Akihiko Odaki
Quoted 132 lines · Jason Wang
Quoted 131 lines · Akihiko Odaki
Quoted 129 lines · Jason Wang
Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang
Quoted 53 lines · Akihiko Odaki

On 2025/03/10 12:55, Jason Wang wrote:
On Fri, Mar 7, 2025 at 7:01 PM Akihiko Odaki <[email protected]> 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.

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.

Signed-off-by: Akihiko Odaki <[email protected]>
Tested-by: Lei Yang <[email protected]>
---
Documentation/networking/tuntap.rst | 7 ++
drivers/net/Kconfig | 1 +
drivers/net/tap.c | 68 ++++++++++++++-
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, 386 insertions(+), 31 deletions(-)

diff --git a/Documentation/networking/tuntap.rst b/Documentation/networking/tuntap.rst
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
--- a/Documentation/networking/tuntap.rst
+++ b/Documentation/networking/tuntap.rst
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
}

[...]

Quoted 28 lines
Quoted 12 lines
Quoted 12 lines

+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+ bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires
to make eBPF and RSS mutually exclusive. I still don't understand why
we need this. Allow eBPF program to override some of the path seems to
be common practice.

What's more, we didn't try (or even can't) to make automq and eBPF to
be mutually exclusive. So I still didn't see what we gain from this
and it complicates the codes and may lead to ambiguous uAPI/behaviour.

Quoted 5 lines
Quoted 2 lines

automq and eBPF are mutually exclusive; automq is disabled when an eBPF
steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF while using automq.

      > >>
Quoted 10 lines
Quoted 2 lines

We don't even have an interface for eBPF to let it fall back to another
alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

Quoted 4 lines

I could make it fall back to RSS if the eBPF steeering
program is designed to fall back to automq when it returns e.g., -1. But
such an interface is currently not defined and defining one is out of
scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to make the behaviour consistent: allow eBPF to override the behaviour of both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

I referred to the eBPF steering program instead of tc-ebpf. tc-ebpf is nothing to do with the TUNSETSTEERINGEBPF ioctl, which this patch changes.

I meant you can't do "full control" in any case, the point below doesn't stand. Queue mapping could be restored even if RSS is set.

What matters here is how we handle the control when tc didn't take it. eBPF, RSS, or automq make take all of it; I referred that as "full control".

Quoted 24 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

Quoted 8 lines

I wrote: "because it is not obvious whether we should choose either 1) or 2)", but I think I can explain it better:

When an eBPF steering program cannot implement a fallback, it means the eBPF steering program requests the full control over the steering. On the other hand, RSS also requests the same control. So these two will conflict and the entity controlling the steering will be undefined when both are enabled.

Well, the fallback is orthogonal to the proposal here. We haven't had that since the introduction of the eBPF steering program. This means automq has been in "conflict" with eBPF for years. Again, another advantage, allowing the eBPF program to be the first to allow the management layer to override Qemu's steering.

Quoted 8 lines
Quoted 2 lines

What if a VMM uses eBPF steering program and the management layer decides to override it with RSS?

That's possible but I think we're seeking which approach is better. In this case, RSS could be implemented in eBPF but not the reverse.

So my point is to start from something that is simpler. Simply allow eBPF on top of RSS as automq. And optimize on top.

The in-kernel RSS implementation is more optimized and capable of hash reporting. I don't think either eBPF steering program or in-kernel RSS is more capable than the other and there is a reason to place eBPF on top of RSS.

Quoted 10 lines
Quoted 5 lines

eBPF is obviously predecedent to automq as eBPF is an opt-in feature and automq is the implicit default. But this logic cannot be applied to decide the order of eBPF and RSS because they are both opt-in features.

This is from the perspective of kernel development. But let's try to think from the userspace: A well written user space knows what it does, rejecting eBPF while RSS is set doesn't help. But anyhow if you stick, it doesn't harm.

Yes, it not for the current userspace but for the future kernel development; the kernel can reserve the freedom to decide the priority of eBPF and RSS by rejecting eBPF while RSS.

Quoted 171 lines
Quoted 166 lines
Quoted 8 lines
Quoted 5 lines

3) eliminates the undefined semantics by rejecting to enable both.

This would lead a usersapce noticeable change of the behaviour? And what do you mean by "rejecting to enable both"?

Existing userspace code should see no change as it only cares the case where RSS is enabled.

Here, rejecting to enable both means to deny setting an eBPF steering program when RSS is enabled, and visa-versa.

Quoted 10 lines
Quoted 6 lines

An
alternative approach is to allow eBPF steering programs to fall back. When both the eBPF program and RSS are enabled, RSS will gain the control of steering under the well-defined situation where the eBPF steering program decides to fall back.

How about just stick the eBPF precedence in this proposal and introduce the fallback on top? This helps to speed up the iteration (as the version has been iterated to 11).

I don't think that helps much since we have another ongoing discussion below and it is not the sole roadblock.

Quoted 129 lines
Quoted 69 lines
Quoted 62 lines
Quoted 23 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT?
If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
        le32 hash_types;
        le16 reserved[4];
        u8 hash_key_length;
        u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""

struct rss_rq_id {
       le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */
       le16 reserved: 1; /* Set to zero */
};
struct virtio_net_rss_config {
        le32 hash_types;
        le16 indirection_table_mask;
        struct rss_rq_id unclassified_queue;
        struct rss_rq_id indirection_table[indirection_table_length];
        le16 max_tx_vq;
        u8 hash_key_length;
        u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

Quoted 4 lines

The spec also defines struct virtio_net_hash_config (which will be used when RSS is disabled) and struct virtio_net_rss_config to match the layout to share some fields. However, the UAPI does not follow the interface design of virtio due to some problems with these structures.

Copy-paste error. The above is copied from the virtio spec, but I meant the existing uAPI in virtio_net.h:

/*
* The command VIRTIO_NET_CTRL_MQ_RSS_CONFIG has the same effect as
* VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET does and additionally configures
* the receive steering to use a hash calculated for incoming packet
* to decide on receive virtqueue to place the packet. The command
* also provides parameters to calculate a hash and receive virtqueue. */

struct virtio_net_rss_config {
           __le32 hash_types;
           __le16 indirection_table_mask;
           __le16 unclassified_queue;
           __le16 indirection_table[1/* + indirection_table_mask */];
           __le16 max_tx_vq;
           __u8 hash_key_length;
           __u8 hash_key_data[/* hash_key_length */];
};
Quoted 1 line
    #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1

/*
* The command VIRTIO_NET_CTRL_MQ_HASH_CONFIG requests the device
* to include in the virtio header of the packet the value of the
* calculated hash and the report type of hash. It also provides
* parameters for hash calculation. The command requires feature
* VIRTIO_NET_F_HASH_REPORT to be negotiated to extend the
* layout of virtio header as defined in virtio_net_hdr_v1_hash. */

struct virtio_net_hash_config {
           __le32 hash_types;
           /* for compatibility with virtio_net_rss_config */
           __le16 reserved[4];
           __u8 hash_key_length;
           __u8 hash_key_data[/* hash_key_length */];
};

This has been used by Qemu but I see a virtio-net version of:

struct virtio_net_ctrl_rss {
           u32 hash_types;
           u16 indirection_table_mask;
           u16 unclassified_queue;
           u16 hash_cfg_reserved; /* for HASH_CONFIG (see
virtio_net_hash_config for details) */
           u16 max_tx_vq;
           u8 hash_key_length;
           u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

           u16 *indirection_table;
};

This is ugly and results in a tricky code when trying to submit RSS/HASH commands to the device:

Quoted 7 lines
           if (vi->has_rss) {
                   sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
                   sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
           } else {
                   sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved,
sizeof(uint16_t));
           }
Quoted 4 lines

The only reference to struct virtio_net_rss_config in QEMU is to derive the offset of indirection_table. This is because the definition in virtio_net.h also includes indirection_table in the middle and the offsets of later part are unusable.

Quoted 7 lines

Yes.

Quoted 3 lines

QEMU internally has a structure named VirtioNetRssData which just looks like struct virtio_net_ctrl_rss.

It's a pity that it doesn't use uAPI. We might need to fix them.

It doesn't want to use the UAPI structures for the internal storage because it wants to store them in native endians and QEMU is not interested in some fields in the UAPI structures. struct tun_vnet_hash and struct tun_vnet_hash_rss are easy to fill using VirtioNetRssData.

Quoted 93 lines
Quoted 81 lines
Quoted 24 lines
Quoted 13 lines

Below is the definition of struct virtio_net_hash_config:

struct virtio_net_hash_config {
        le32 hash_types;
        le16 reserved[4];
        u8 hash_key_length;
        u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

Here, hash_types, hash_key_length, and hash_key_data are shared with struct virtio_net_rss_config.

One problem is that struct virtio_net_rss_config has a flexible array (indirection_table) between hash_types and hash_key_length. This is something we cannot express with C.

We can split the virtio_net_rss_config to ease the dealing with arrays, more below.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Another problem is that the semantics of the key in struct virtio_net_hash_config is not defined in the spec.

If this is the case. Let's fix that in the spec first to make sure our uAPI aligns with spec without ambiguity. It would be a nightmare to deal with the in-consistency between virtio spec and Linux uAPIs.

The userspace doesn't need to do anything to deal with inconsistency since these fields are unused.

Quoted 16 lines
Quoted 16 lines

To solve these problems, I defined the UAPI structures that do not include indiretion_table.

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

The UAPI shares struct tun_vnet_hash for both hash report and rss.

I meant sharing structure in two ioctls instead of reusing a specific structure for two semantics in one ioctl if possible. Though I don't think we need any sharing.

The UAPI implemented in this patch already shares struct tun_vnet_hash and having two ioctls doesn't change that.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

And it seems to lack parameters like max_tx_vq.

max_tx_vq is not relevant with hashing.

It is needed for RSS and we don't have that, no?

No. RSS is Receive Side Scaling but it's not about receiving.

Just to make sure I understand this, max_tx_vq is part of the virtio_net_rss_config, how would Qemu behave when it receives this from guest?

Quoted 6 lines
Quoted 4 lines

"""
A driver sets max_tx_vq to inform a device how many transmit virtqueues it may use (transmitq1…transmitq max_tx_vq). """

It does nothing.

Nope, see:

commit 50bfcaedd78e53135ec0504302269b3b65bf1eff

Author: Philo Lu <[email protected]>
Date:   Mon Nov 4 16:57:06 2024 +0800

virtio_net: Update rss when set queue

RSS configuration should be updated with queue number. In particular, it should be updated when (1) rss enabled and (2) default rss configuration is used without user modification.

During rss command processing, device updates queue_pairs using rss.max_tx_vq. That is, the device updates queue_pairs together with rss, so we can skip the sperate queue_pairs update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly.

Also remove the vi->has_rss ? check when setting vi->rss.max_tx_vq, because this is not used in the other hash_report case.

Fixes c7114b1249fa ("drivers/net/virtio_net: Added basic RSS support.")
Quoted 7 lines
Quoted 3 lines

RSS doesn't depend on MQ.

If it is not handled by Qemu, it should be a bug?

I was wrong; QEMU does handle this field, but it doesn't use the definition of struct virtio_net_rss_config and name it queue_pairs instead of max_tx_vq so I could not find it by grep.

Yes, another side effect is that uAPI is not even used there...

Quoted 3 lines

For tap, max_tx_vq is handled by changing the number of open file descriptors so passing it via an ioctl is redundant.

See my reply below.

Quoted 105 lines
Quoted 105 lines
Quoted 105 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 77 lines
Quoted 9 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 54 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:
> A device attempts to calculate a per-packet hash in the following > cases:
>
> - The feature VIRTIO_NET_F_RSS was negotiated. The device uses the
> hash to determine the receive virtqueue to place incoming packets.
> - The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device
> reports the hash value and the hash type with the packet.
>
> If the feature VIRTIO_NET_F_RSS was negotiated:
>
> - The device uses hash_types of the virtio_net_rss_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
hash_key_length of the virtio_net_rss_config structure (see
> 5.1.6.5.7.1).
>
> If the feature VIRTIO_NET_F_RSS was not negotiated:
>
> - The device uses hash_types of the virtio_net_hash_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
> hash_key_length of the virtio_net_hash_config structure (see
> .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

Quoted 7 lines

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

TUNSETIFF, enables the device to be able to handle RSS/HASREPORT. TUNSETHASH/RSS. dealing with RSS/HASH command from userspace.

We also needs to be able to disable them at runtime so that we can handle resets.

Via TUNSETHASH/RSS? I think it should have a way to accept parameters that disable RSS or hash report.

That's what this patch implements. TUNSETVNETHASH accepts parameters to choose what features to be enabled.

Quoted 14 lines
Quoted 10 lines
Quoted 6 lines

This is the way we used to do for multi queue and vnet header. TUNSETIFF requires CAP_NET_ADMIN, this could be an extra safe guard for unprivileged userspace.

I intend to allow using this feature without privilege. A VMM is usually unprivileged and requiring a privilege to configure tuntap is too prohibitive.

For safety, tun is not allowed to be created by unprivileged users. And it's not to configure the tuntap dynamically, it's about telling the function that tuntap can have (not necessarily enabled though) .

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I don't think we need another barrier for the new functions. Once an unprivileged user get a file descriptor of tuntap from a privileged user, they are free to enable RSS and/or hash reporting.

Only if such a feature is allowed by the privileged user.

I don't see a reason not to allow the feature to unprivileged users. It only complicates the setup.

For safety, e.g reduce the chance for unprivileged user to explore part of the kernel codes.

Quoted 49 lines
Quoted 49 lines
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
Quoted 29 lines
Quoted 20 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

In the virtio-net specification, it is not defined what would

happen if

Quoted 10 lines
Quoted 3 lines

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Quoted 4 lines

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics?

That's perfectly fine, but it needs to be done in virtio-net's uAPI not tun's. What's more, if you think two commands are not well-defined, let's fix that in the virtio spec first.

Quoted 6 lines
Quoted 3 lines

The virtio
spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

See above, let's fix the uAPI first. We don't want DPDK to use tun's uAPI for RSS

virtio-net's UAPI is for the virtio spec which has a capable generic feature negotiation mechanism. tuntap needs its own feature negotiation and it's nothing to do with virtio-net's UAPI.

Quoted 6 lines
Quoted 2 lines

Well, I don't mean the part of the feature negotiation. I mean the part for rss and hash report configuration.

The feature negotiation still matters when deciding the granularity of ioctls. We need one ioctl for a feature negotiation, and to avoid having an intermediate state,

I don't understand this. For example, driver can choose to

Quoted 8 lines
Quoted 6 lines

1) negotiate RSS
2) do something else.
3) configure RSS

Spec doesn't require those two to be configured at the same time, so "intermediate state" is allowed.

The spec doesn't define what should happen in the intermediate state either.

Yes but my point is that in the uAPI layer we don't need to care about the intermediate state. It can just work as other features, e.g having a default state after feature negotiation is more than enough. This is the way we deal with other features like vnet header etc.

Quoted 5 lines

For a hardware implementation I think it's fine whatever the implementation defines as the intermediate state. But for the UAPI, it's better avoiding having such a definition to keep the interface minimal and maximize the UAPI stability.

Well, even if you think there's an issue:

1) I don't see how we can avoid the intermediate state consider guest have such state
2) We need to "fix" virtio spec and virito-net first, tuntap is not the right place to workaround virtio specific issues

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

the ioctl should also do the configuration. Hence
that one ioctl should do all of the feature negotiation and configuration.

Quoted 14 lines
Quoted 11 lines

The structures for two commands have unused or redundant fields and a flexible array in the middle of the structure, but they are ABIs so we can't change it.

DPDK is another reason to define tuntap's own UAPIs. They don't care unused or redundant fields and a flexible array in middle that are present in the virtio spec. It will also not want to deal with the requirement of little endian. Constructing struct virtio_net_rss_config is an extra burden for DPDK.

I meant for vhost-user implementation in DPDK, it needs to use virtio-net uAPI not tuntap's for example.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The vhost-user implementation will use tuntap's UAPIs for its ethernet device backend.

That sounds pretty weird, vhost-user has nothing related to tuntap.

My expression in the last email was weird. More precisely, the ethernet backend of tuntap will use the UAPIs, and the vhost-user will use the ethernet backend in turn.

I don't understand what "ethernet backend" means here.

Quoted 12 lines
Quoted 12 lines
Quoted 6 lines

It uses the generic interface of ethernet device so for
RSS it will use functions like rte_eth_dev_rss_hash_update() for example. tuntap's UAPIs are more suited to implement these interfaces as they operate in native endian and don't have extra fields.

Nope, for example it needs to use le for virtio_net_hdr if a modern device is used. But it needs a "native" endian according to the guest endian via TUNSETVNETLE/BE. We don't have a choice as virtio-net hdr support in tuntap is much earlier than modern devices.

Quoted 6 lines
Quoted 2 lines

Let's don't do the same thing (native endian) for tuntap as RSS depends on modern, so we know it must be le.

virtio_net_hdr is the data path while the current discussion is about the control path. All configuration knobs of tuntap operates in the native endian.

Because they are not directly related to virtio specification. We don't want to duplicate virtio-net with our own version every time E.g once RSSv2 or aRFS were implemented. Or I would even introduce a single uAPI to transport possible cvq commands then we can avoid inventing new ioctls that just transport cvq commands.

Quoted 4 lines

So I think we should stick to the little endian for the data path while we should stick to the native endian for the control path to maximize the consistency.

I don't see a reason to differ datapath from control path. Virtio-net uAPI has been reused by tuntap for more than a decade.

Quoted 100 lines
Quoted 100 lines
Quoted 18 lines

DPDk applications other than vhost-user also matter; they do not care what virtio does at all.

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

On the other hand, Constructing tuntap-specific structures is not that complicated for VMMs.

Not complicated but redundant.

Quoted 4 lines

A VMM will need to inspect struct
virtio_net_rss_config anyway to handle migration and check its size so it can store the values it inspected to struct tun_vnet_hash and struct tun_vnet_hash_rss and pass them to the kernel.

Quoted 7 lines
Quoted 4 lines

I don't see how rss and hash reports differ from what we have now. Those inspections must be done anyhow for compatibility for example the check of offloading features. Such steps could not be eliminated no matter how we design the uAPI.

I explained the difference between the virtio and tuntap UAPIs, not between RSS and hash reporting.

See above.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The overall userspace implementation will be simpler by having structures specifically tailored for the communication between the userspace and kernel.

Quoted 6 lines
Quoted 2 lines

This is exactly how a good uAPI should behave. If uAPI in virtio-net can't do this, I don't understand why uAPI in tuntap can solve it.

The UAPI in virtio-net cannot do it because it's already fixed and it also needs to perform endian conversion for the VM use case. tuntap doesn't have these restrictions.

Same here.

Quoted 55 lines
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
Quoted 9 lines

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands.

I don't see a connection with the operation code. For example, we can add new uAPIs in virtio-net which could be something like:

    struct virtio_net_rss_config_header {
         __le32 hash_types;
         __le16 indirection_table_mask;
         __le16 unclassified_queue;
         __le16 indirection_table[];
}
struct virtio_net_rss_config_tailer {
         __le16 max_tx_vq;
         u8 hash_key_length;
         u8 hash_key_data[];
}

These two are used by TUNSETVNETRSS. And simply reuse the virtio_net_hash_config for TUNSETVETHASH.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

With this, we can tweak the virtio-net driver with this new uAPI. Then

tap* can reuse this.

I implemented a UAPI and driver change accordingly:
https://lore.kernel.org/r/[email protected]

This is a nice improvement for the driver, but I still don't think it is suited for the UAPI of tuntap.

Quoted 8 lines

Any reason for this? It should work like virtio_net_hdr.

Quoted 3 lines

The requirements of extra fields and
little endian cannot be removed from the virtio spec but they are irrelevant for tuntap.

I don't understand this part. What fields are "extra" and need to be removed from the spec?

All fields not included in struct tun_vnet_hash and struct tun_vnet_hash_rss. Namely, for struct virtio_net_hash_config:
- reserved
- hash_key_length
- hash_key_data

For struct virtio_net_rss_config:
- max_tx_vq
- hash_key_length

Quoted 7 lines
Quoted 5 lines

See my above reply, and I basically meant

TUNSETVETHASH accept struct virtio_net_hash_config; TUNSETVETRSS accept struct virtio_net_rss_config_hdr + struct virtio_net_rss_config_trailer;

That still bring the extra fields I noted in the last email.

I don't know how to define "extra" here. Let's summarize here:

Method A:

1) virtio specification use separate commands for has_report and rss
2) hash_port ans rss doesn't depend on each other
3) reuse virtio-net uAPI

Method B:

1) trying to define and remove the "extra" fields in tuntap, and redefine it in TUNTAP

It would always be much easier to start from simply reusing the virtio-net uAPI. Method B makes both the implementation and reviewing harder, as we need to

1) revisit the design of the virtio spec, this needs to be done in the virtio community not here
2) audit the difference between virtio spec and TUN/TAP, that's why we have a very long discussion here

For example, the root cause of why you think the max_tx_vq is "extra" is:

1) The spec defines VIRTIO_NET_F_RSS and VIRTIO_NET_F_MQ as independent features
2) Your code tries to re-use IFF_MULTI_QUEUE for both VIRTIO_NET_F_RSS and VIRTIO_NET_F_MQ, this would have a lot of implications, e.g automatic steering might be applied when only RSS is negotiated etc

The correct way to implement this is:

1) Introduce IFF_RSS and only set it during TUNSETIFF when device only offers RSS
2) reuse virtio-net uAPI and accept max_tx_vq and use that to change the queue(or queue paris) if necessary

Then we have a clean and well defined behaviour (for example when devices only support RSS but not MQ).

Thanks

Quoted 68 lines

Regards,
Akihiko Odaki

Quoted 63 lines

Thanks

Quoted 59 lines

Regards,
Akihiko Odaki

Quoted 54 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The best possibility is to share structures, not commands, and I don't think even sharing structures makes sense here because of the reasons described above.

Quoted 8 lines
Quoted 3 lines

I don't want to share structures, I meant starting from something that is simple and has been sorted in the virtio spec. Optimization could be done on top.

I meant to reuse the structures in virtio_net.h.

Regards,
Akihiko Odaki

Thanks

Quoted 35 lines
Quoted 34 lines

Thanks

Quoted 28 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#29 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 520 lines · Jason Wang
Quoted 519 lines · Akihiko Odaki
Quoted 517 lines · Jason Wang
Quoted 481 lines · Akihiko Odaki
Quoted 172 lines · Jason Wang
Quoted 171 lines · Akihiko Odaki
Quoted 132 lines · Jason Wang
Quoted 131 lines · Akihiko Odaki
Quoted 129 lines · Jason Wang
Quoted 115 lines · Akihiko Odaki
Quoted 104 lines · Jason Wang
Quoted 56 lines · Akihiko Odaki
Quoted 54 lines · Jason Wang

On Mon, Mar 10, 2025 at 3:45 PM Akihiko Odaki <[email protected]> wrote:

On 2025/03/10 12:55, Jason Wang wrote:
On Fri, Mar 7, 2025 at 7:01 PM Akihiko Odaki <[email protected]> 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.

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.

Signed-off-by: Akihiko Odaki <[email protected]>
Tested-by: Lei Yang <[email protected]>
---
Documentation/networking/tuntap.rst | 7 ++
drivers/net/Kconfig | 1 +
drivers/net/tap.c | 68 ++++++++++++++-
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, 386 insertions(+), 31 deletions(-)

diff --git a/Documentation/networking/tuntap.rst b/Documentation/networking/tuntap.rst
index 4d7087f727be5e37dfbf5066a9e9c872cc98898d..86b4ae8caa8ad062c1e558920be42ce0d4217465 100644
--- a/Documentation/networking/tuntap.rst
+++ b/Documentation/networking/tuntap.rst
@@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it::
return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
}

[...]

Quoted 28 lines
Quoted 17 lines

+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp,
+ bool can_rss, void __user *argp)

So again, can_rss seems to be tricky. Looking at its caller, it tires
to make eBPF and RSS mutually exclusive. I still don't understand why
we need this. Allow eBPF program to override some of the path seems to
be common practice.

What's more, we didn't try (or even can't) to make automq and eBPF to
be mutually exclusive. So I still didn't see what we gain from this
and it complicates the codes and may lead to ambiguous uAPI/behaviour.

automq and eBPF are mutually exclusive; automq is disabled when an eBPF
steering program is set so I followed the example here.

I meant from the view of uAPI, the kernel doesn't or can't reject eBPF
while using automq.

       > >>
Quoted 10 lines

We don't even have an interface for eBPF to let it fall back to another
alogirhtm.

It doesn't even need this, e.g XDP overrides the default receiving path.

I could make it fall back to RSS if the eBPF steeering
program is designed to fall back to automq when it returns e.g., -1. But
such an interface is currently not defined and defining one is out of
scope of this patch series.

Quoted 5 lines
Quoted 3 lines

Just to make sure we are on the same page, I meant we just need to
make the behaviour consistent: allow eBPF to override the behaviour of
both automq and rss.

That assumes eBPF takes precedence over RSS, which is not obvious to me.

Well, it's kind of obvious. Not speaking the eBPF selector, we have other eBPF stuffs like skbedit etc.

Quoted 6 lines

Let's add an interface for the eBPF steering program to fall back to another steering algorithm. I said it is out of scope before, but it makes clear that the eBPF steering program takes precedence over other algorithms and allows us to delete the code for the configuration validation in this patch.

Quoted 5 lines

Fallback is out of scope but it's not what I meant.

I meant in the current uAPI take eBPF precedence over automq. It's much more simpler to stick this precedence unless we see obvious advanatge.

We still have three different design options that preserve the current precedence:

Quoted 7 lines

1) Precedence order: eBPF -> RSS -> automq
2) Precedence order: RSS -> eBPF -> automq
3) Precedence order: eBPF OR RSS -> automq where eBPF and RSS are mutually exclusive

I think this is a unique situation for this steering program and I could not find another example in other eBPF stuffs.

As described above, queue mapping could be overridden by tc-ebpf. So there's no way to guarantee the RSS will work:

https://github.com/DPDK/dpdk/blob/main/drivers/net/tap/bpf/tap_rss.c#L262

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Making eBPF first leaves a chance for the management layer to override the choice of Qemu.

I referred to the eBPF steering program instead of tc-ebpf. tc-ebpf is nothing to do with the TUNSETSTEERINGEBPF ioctl, which this patch changes.

I meant you can't do "full control" in any case, the point below doesn't stand. Queue mapping could be restored even if RSS is set.

What matters here is how we handle the control when tc didn't take it. eBPF, RSS, or automq make take all of it; I referred that as "full control".

Quoted 24 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

The current version implements 3) because it is not obvious whether we should choose either 1) or 2).

But you didn't explain why you choose 3), and it leads to tricky code (e.g the can_rss stuff etc).

Quoted 8 lines

I wrote: "because it is not obvious whether we should choose either 1) or 2)", but I think I can explain it better:

When an eBPF steering program cannot implement a fallback, it means the eBPF steering program requests the full control over the steering. On the other hand, RSS also requests the same control. So these two will conflict and the entity controlling the steering will be undefined when both are enabled.

Well, the fallback is orthogonal to the proposal here. We haven't had that since the introduction of the eBPF steering program. This means automq has been in "conflict" with eBPF for years. Again, another advantage, allowing the eBPF program to be the first to allow the management layer to override Qemu's steering.

Quoted 8 lines
Quoted 2 lines

What if a VMM uses eBPF steering program and the management layer decides to override it with RSS?

That's possible but I think we're seeking which approach is better. In this case, RSS could be implemented in eBPF but not the reverse.

So my point is to start from something that is simpler. Simply allow eBPF on top of RSS as automq. And optimize on top.

The in-kernel RSS implementation is more optimized and capable of hash reporting. I don't think either eBPF steering program or in-kernel RSS is more capable than the other and there is a reason to place eBPF on top of RSS.

Quoted 10 lines
Quoted 5 lines

eBPF is obviously predecedent to automq as eBPF is an opt-in feature and automq is the implicit default. But this logic cannot be applied to decide the order of eBPF and RSS because they are both opt-in features.

This is from the perspective of kernel development. But let's try to think from the userspace: A well written user space knows what it does, rejecting eBPF while RSS is set doesn't help. But anyhow if you stick, it doesn't harm.

Yes, it not for the current userspace but for the future kernel development; the kernel can reserve the freedom to decide the priority of eBPF and RSS by rejecting eBPF while RSS.

Quoted 171 lines
Quoted 166 lines
Quoted 8 lines
Quoted 5 lines

3) eliminates the undefined semantics by rejecting to enable both.

This would lead a usersapce noticeable change of the behaviour? And what do you mean by "rejecting to enable both"?

Existing userspace code should see no change as it only cares the case where RSS is enabled.

Here, rejecting to enable both means to deny setting an eBPF steering program when RSS is enabled, and visa-versa.

Quoted 10 lines
Quoted 6 lines

An
alternative approach is to allow eBPF steering programs to fall back. When both the eBPF program and RSS are enabled, RSS will gain the control of steering under the well-defined situation where the eBPF steering program decides to fall back.

How about just stick the eBPF precedence in this proposal and introduce the fallback on top? This helps to speed up the iteration (as the version has been iterated to 11).

I don't think that helps much since we have another ongoing discussion below and it is not the sole roadblock.

Quoted 129 lines
Quoted 69 lines
Quoted 62 lines
Quoted 23 lines

But 1) will be the most capable option if
eBPF has a fall-back feature.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines

[...]

Quoted 8 lines
Quoted 6 lines

Is there a chance that we can reach here without TUN_VNET_HASH_REPORT?
If yes, it should be a bug.

It is possible to use RSS without TUN_VNET_HASH_REPORT.

Another call to separate the ioctls then.

RSS and hash reporting are not completely independent though.

Quoted 8 lines
Quoted 5 lines

Spec said:

"""
VIRTIO_NET_F_RSSRequires VIRTIO_NET_F_CTRL_VQ.
"""

I meant the features can be enabled independently, but they will share the hash type set when they are enabled at the same time.

Looking at the spec:

Hash repot uses:

"""

struct virtio_net_hash_config {
         le32 hash_types;
         le16 reserved[4];
         u8 hash_key_length;
         u8 hash_key_data[hash_key_length];
};

"""

RSS uses

"""

struct rss_rq_id {
        le16 vq_index_1_16: 15; /* Bits 1 to 16 of the virtqueue index */
        le16 reserved: 1; /* Set to zero */
};
struct virtio_net_rss_config {
         le32 hash_types;
         le16 indirection_table_mask;
         struct rss_rq_id unclassified_queue;
         struct rss_rq_id indirection_table[indirection_table_length];
         le16 max_tx_vq;
         u8 hash_key_length;
         u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

"""

Instead of trying to figure out whether we can share some data structures, why not simply start from what has been done in the spec? This would ease the usersapce as well where it can simply do 1:1 mapping between ctrl vq command and tun uAPI.

Quoted 4 lines

The spec also defines struct virtio_net_hash_config (which will be used when RSS is disabled) and struct virtio_net_rss_config to match the layout to share some fields. However, the UAPI does not follow the interface design of virtio due to some problems with these structures.

Copy-paste error. The above is copied from the virtio spec, but I meant the existing uAPI in virtio_net.h:

/*
* The command VIRTIO_NET_CTRL_MQ_RSS_CONFIG has the same effect as
* VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET does and additionally configures
* the receive steering to use a hash calculated for incoming packet
* to decide on receive virtqueue to place the packet. The command
* also provides parameters to calculate a hash and receive virtqueue. */

struct virtio_net_rss_config {
            __le32 hash_types;
            __le16 indirection_table_mask;
            __le16 unclassified_queue;
            __le16 indirection_table[1/* + indirection_table_mask */];
            __le16 max_tx_vq;
            __u8 hash_key_length;
            __u8 hash_key_data[/* hash_key_length */];
};
Quoted 1 line
     #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1

/*
* The command VIRTIO_NET_CTRL_MQ_HASH_CONFIG requests the device
* to include in the virtio header of the packet the value of the
* calculated hash and the report type of hash. It also provides
* parameters for hash calculation. The command requires feature
* VIRTIO_NET_F_HASH_REPORT to be negotiated to extend the
* layout of virtio header as defined in virtio_net_hdr_v1_hash. */

struct virtio_net_hash_config {
            __le32 hash_types;
            /* for compatibility with virtio_net_rss_config */
            __le16 reserved[4];
            __u8 hash_key_length;
            __u8 hash_key_data[/* hash_key_length */];
};

This has been used by Qemu but I see a virtio-net version of:

struct virtio_net_ctrl_rss {
            u32 hash_types;
            u16 indirection_table_mask;
            u16 unclassified_queue;
            u16 hash_cfg_reserved; /* for HASH_CONFIG (see
virtio_net_hash_config for details) */
            u16 max_tx_vq;
            u8 hash_key_length;
            u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

            u16 *indirection_table;
};

This is ugly and results in a tricky code when trying to submit RSS/HASH commands to the device:

Quoted 7 lines
            if (vi->has_rss) {
                    sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
                    sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
            } else {
                    sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved,
sizeof(uint16_t));
            }
Quoted 4 lines

The only reference to struct virtio_net_rss_config in QEMU is to derive the offset of indirection_table. This is because the definition in virtio_net.h also includes indirection_table in the middle and the offsets of later part are unusable.

Quoted 7 lines

Yes.

Quoted 3 lines

QEMU internally has a structure named VirtioNetRssData which just looks like struct virtio_net_ctrl_rss.

It's a pity that it doesn't use uAPI. We might need to fix them.

It doesn't want to use the UAPI structures for the internal storage because it wants to store them in native endians and QEMU is not interested in some fields in the UAPI structures. struct tun_vnet_hash and struct tun_vnet_hash_rss are easy to fill using VirtioNetRssData.

Quoted 93 lines
Quoted 81 lines
Quoted 24 lines
Quoted 13 lines

Below is the definition of struct virtio_net_hash_config:

struct virtio_net_hash_config {
         le32 hash_types;
         le16 reserved[4];
         u8 hash_key_length;
         u8 hash_key_data[hash_key_length];
};
Quoted 6 lines

Here, hash_types, hash_key_length, and hash_key_data are shared with struct virtio_net_rss_config.

One problem is that struct virtio_net_rss_config has a flexible array (indirection_table) between hash_types and hash_key_length. This is something we cannot express with C.

We can split the virtio_net_rss_config to ease the dealing with arrays, more below.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Another problem is that the semantics of the key in struct virtio_net_hash_config is not defined in the spec.

If this is the case. Let's fix that in the spec first to make sure our uAPI aligns with spec without ambiguity. It would be a nightmare to deal with the in-consistency between virtio spec and Linux uAPIs.

The userspace doesn't need to do anything to deal with inconsistency since these fields are unused.

Quoted 16 lines
Quoted 16 lines

To solve these problems, I defined the UAPI structures that do not include indiretion_table.

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

A plot twist is the "types" parameter; it is a parameter that is "common" for RSS and hash reporting.

So we can share part of the structure through the uAPI.

Isn't that what this patch does?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I didn't see, basically I see only one TUNSETVNETHASH that is used to set both hash report and rss:

The UAPI shares struct tun_vnet_hash for both hash report and rss.

I meant sharing structure in two ioctls instead of reusing a specific structure for two semantics in one ioctl if possible. Though I don't think we need any sharing.

The UAPI implemented in this patch already shares struct tun_vnet_hash and having two ioctls doesn't change that.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

"""
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
"""

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

And it seems to lack parameters like max_tx_vq.

max_tx_vq is not relevant with hashing.

It is needed for RSS and we don't have that, no?

No. RSS is Receive Side Scaling but it's not about receiving.

Just to make sure I understand this, max_tx_vq is part of the virtio_net_rss_config, how would Qemu behave when it receives this from guest?

Quoted 6 lines
Quoted 4 lines

"""
A driver sets max_tx_vq to inform a device how many transmit virtqueues it may use (transmitq1…transmitq max_tx_vq). """

It does nothing.

Nope, see:

commit 50bfcaedd78e53135ec0504302269b3b65bf1eff

Author: Philo Lu <[email protected]>
Date:   Mon Nov 4 16:57:06 2024 +0800

virtio_net: Update rss when set queue

RSS configuration should be updated with queue number. In particular, it should be updated when (1) rss enabled and (2) default rss configuration is used without user modification.

During rss command processing, device updates queue_pairs using rss.max_tx_vq. That is, the device updates queue_pairs together with rss, so we can skip the sperate queue_pairs update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly.

Also remove the vi->has_rss ? check when setting vi->rss.max_tx_vq, because this is not used in the other hash_report case.

Fixes c7114b1249fa ("drivers/net/virtio_net: Added basic RSS support.")

RSS doesn't depend on MQ.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

If it is not handled by Qemu, it should be a bug?

I was wrong; QEMU does handle this field, but it doesn't use the definition of struct virtio_net_rss_config and name it queue_pairs instead of max_tx_vq so I could not find it by grep.

Yes, another side effect is that uAPI is not even used there...

I hope the split structures you proposed will improve it too.

Quoted 116 lines
Quoted 4 lines

For tap, max_tx_vq is handled by changing the number of open file descriptors so passing it via an ioctl is redundant.

See my reply below.

Quoted 109 lines
Quoted 109 lines
Quoted 108 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 77 lines
Quoted 9 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 54 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:
> A device attempts to calculate a per-packet hash in the following > cases:
>
> - The feature VIRTIO_NET_F_RSS was negotiated. The device uses the
> hash to determine the receive virtqueue to place incoming packets.
> - The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device
> reports the hash value and the hash type with the packet.
>
> If the feature VIRTIO_NET_F_RSS was negotiated:
>
> - The device uses hash_types of the virtio_net_rss_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
hash_key_length of the virtio_net_rss_config structure (see
> 5.1.6.5.7.1).
>
> If the feature VIRTIO_NET_F_RSS was not negotiated:
>
> - The device uses hash_types of the virtio_net_hash_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
> hash_key_length of the virtio_net_hash_config structure (see
> .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

Quoted 7 lines

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

TUNSETIFF, enables the device to be able to handle RSS/HASREPORT. TUNSETHASH/RSS. dealing with RSS/HASH command from userspace.

We also needs to be able to disable them at runtime so that we can handle resets.

Via TUNSETHASH/RSS? I think it should have a way to accept parameters that disable RSS or hash report.

That's what this patch implements. TUNSETVNETHASH accepts parameters to choose what features to be enabled.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

This is the way we used to do for multi queue and vnet header. TUNSETIFF requires CAP_NET_ADMIN, this could be an extra safe guard for unprivileged userspace.

Quoted 7 lines
Quoted 3 lines

I intend to allow using this feature without privilege. A VMM is usually unprivileged and requiring a privilege to configure tuntap is too prohibitive.

For safety, tun is not allowed to be created by unprivileged users. And it's not to configure the tuntap dynamically, it's about telling the function that tuntap can have (not necessarily enabled though) .

I don't think we need another barrier for the new functions. Once an unprivileged user get a file descriptor of tuntap from a privileged user, they are free to enable RSS and/or hash reporting.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Only if such a feature is allowed by the privileged user.

I don't see a reason not to allow the feature to unprivileged users. It only complicates the setup.

For safety, e.g reduce the chance for unprivileged user to explore part of the kernel codes.

It indeed reduces the attack surface, but it's fine without the reduction I guess? It's not a feature so complicated; I saw there were complicated changes like namespaces and io_uring that caused controversy when exposing them to unprivilged users, but this feature is not like them, I suppose.

Quoted 69 lines
Quoted 50 lines
Quoted 50 lines
Quoted 41 lines
Quoted 41 lines
Quoted 40 lines
Quoted 30 lines
Quoted 21 lines

> >> In the virtio-net specification, it is not defined what would
happen if

Quoted 10 lines
Quoted 3 lines

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Quoted 4 lines

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics?

That's perfectly fine, but it needs to be done in virtio-net's uAPI not tun's. What's more, if you think two commands are not well-defined, let's fix that in the virtio spec first.

Quoted 6 lines
Quoted 3 lines

The virtio
spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

See above, let's fix the uAPI first. We don't want DPDK to use tun's uAPI for RSS

virtio-net's UAPI is for the virtio spec which has a capable generic feature negotiation mechanism. tuntap needs its own feature negotiation and it's nothing to do with virtio-net's UAPI.

Quoted 6 lines
Quoted 2 lines

Well, I don't mean the part of the feature negotiation. I mean the part for rss and hash report configuration.

The feature negotiation still matters when deciding the granularity of ioctls. We need one ioctl for a feature negotiation, and to avoid having an intermediate state,

I don't understand this. For example, driver can choose to

Quoted 8 lines
Quoted 6 lines

1) negotiate RSS
2) do something else.
3) configure RSS

Spec doesn't require those two to be configured at the same time, so "intermediate state" is allowed.

The spec doesn't define what should happen in the intermediate state either.

Yes but my point is that in the uAPI layer we don't need to care about the intermediate state. It can just work as other features, e.g having a default state after feature negotiation is more than enough. This is the way we deal with other features like vnet header etc.

Quoted 5 lines
Quoted 1 line

For a hardware implementation I think it's fine whatever the implementation defines as the intermediate state. But for the UAPI, it's better avoiding having such a definition to keep the interface minimal and maximize the UAPI stability.

Quoted 6 lines

Well, even if you think there's an issue:

1) I don't see how we can avoid the intermediate state consider guest have such state
2) We need to "fix" virtio spec and virito-net first, tuntap is not the right place to workaround virtio specific issues

Let me summarize my points that support having one ioctl to negotiate features and configuration:

The virtio spec has a generic feature negotiation mechanism and reusing it resulted in having an intermediate state between the feature negotiation and configuration. There is nothing wrong about that so we don't need to "fix" the virtio spec.

tuntap can also perform feature negotitaion with TUNSETIFF, but TUNSETIFF have a few problems:

1. It requires a privilege. One can argue that it reduces the attack surface and it indeed does, but it's fine without the reduction I guess? It's not a feature so complicated; I saw there were complicated changes like namespaces and io_uring that caused controversy when exposing them to unprivilged users, but this feature is not like them.

2. It cannot change the enabled feature set at runtime. The virtio spec allows changing it by resetting.

So we need to design a set of new ioctls for both feature negotiation and configuration. When doing so, eliminating the intermediate state is a good principle to determine the optimal size of ioctls.

In theory, it is possible to have small ioctls that set only one scalar value or even one bit, but that doesn't make sense. This principle helps determine the optimal size of ioctls; it minimizes the complexity of both the userspace and the kernel.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 23 lines

the ioctl should also do the configuration. Hence
that one ioctl should do all of the feature negotiation and configuration.

Quoted 6 lines
Quoted 6 lines

The structures for two commands have unused or redundant fields and a flexible array in the middle of the structure, but they are ABIs so we can't change it.

Quoted 8 lines
Quoted 5 lines

DPDK is another reason to define tuntap's own UAPIs. They don't care unused or redundant fields and a flexible array in middle that are present in the virtio spec. It will also not want to deal with the requirement of little endian. Constructing struct virtio_net_rss_config is an extra burden for DPDK.

I meant for vhost-user implementation in DPDK, it needs to use virtio-net uAPI not tuntap's for example.

The vhost-user implementation will use tuntap's UAPIs for its ethernet device backend.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

That sounds pretty weird, vhost-user has nothing related to tuntap.

My expression in the last email was weird. More precisely, the ethernet backend of tuntap will use the UAPIs, and the vhost-user will use the ethernet backend in turn.

I don't understand what "ethernet backend" means here.

It is a driver that serves the Ethernet Device API, which is agnostic on application and driver. The Ethernet Device API, including RSS configuration is documented at:
https://doc.dpdk.org/api/rte__ethdev_8h.html

The Ethernet API are not bound to the virtio spec since they are not specific to the vhost application or the tuntap driver. Hence they operate in native endian and do not have extra fields, and tuntap's structures are more suited to the ethernet backend than the virtio ones.

Quoted 27 lines
Quoted 13 lines
Quoted 13 lines
Quoted 7 lines

It uses the generic interface of ethernet device so for
RSS it will use functions like rte_eth_dev_rss_hash_update() for example. tuntap's UAPIs are more suited to implement these interfaces as they operate in native endian and don't have extra fields.

Nope, for example it needs to use le for virtio_net_hdr if a modern device is used. But it needs a "native" endian according to the guest endian via TUNSETVNETLE/BE. We don't have a choice as virtio-net hdr support in tuntap is much earlier than modern devices.

Quoted 6 lines
Quoted 2 lines

Let's don't do the same thing (native endian) for tuntap as RSS depends on modern, so we know it must be le.

virtio_net_hdr is the data path while the current discussion is about the control path. All configuration knobs of tuntap operates in the native endian.

Because they are not directly related to virtio specification. We don't want to duplicate virtio-net with our own version every time E.g once RSSv2 or aRFS were implemented. Or I would even introduce a single uAPI to transport possible cvq commands then we can avoid inventing new ioctls that just transport cvq commands.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

So I think we should stick to the little endian for the data path while we should stick to the native endian for the control path to maximize the consistency.

I don't see a reason to differ datapath from control path. Virtio-net uAPI has been reused by tuntap for more than a decade.

tuntap's control path all operate in the native endian. They never used the endian of the data path in the control path.

Quoted 138 lines
Quoted 101 lines
Quoted 101 lines
Quoted 19 lines

DPDk applications other than vhost-user also matter; they do not care what virtio does at all.

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

On the other hand, Constructing tuntap-specific structures is not that complicated for VMMs.

Not complicated but redundant.

Quoted 4 lines

A VMM will need to inspect struct
virtio_net_rss_config anyway to handle migration and check its size so it can store the values it inspected to struct tun_vnet_hash and struct tun_vnet_hash_rss and pass them to the kernel.

Quoted 7 lines
Quoted 4 lines

I don't see how rss and hash reports differ from what we have now. Those inspections must be done anyhow for compatibility for example the check of offloading features. Such steps could not be eliminated no matter how we design the uAPI.

I explained the difference between the virtio and tuntap UAPIs, not between RSS and hash reporting.

See above.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The overall userspace implementation will be simpler by having structures specifically tailored for the communication between the userspace and kernel.

Quoted 6 lines
Quoted 2 lines

This is exactly how a good uAPI should behave. If uAPI in virtio-net can't do this, I don't understand why uAPI in tuntap can solve it.

The UAPI in virtio-net cannot do it because it's already fixed and it also needs to perform endian conversion for the VM use case. tuntap doesn't have these restrictions.

Same here.

Quoted 55 lines
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
Quoted 9 lines

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands.

I don't see a connection with the operation code. For example, we can add new uAPIs in virtio-net which could be something like:

     struct virtio_net_rss_config_header {
          __le32 hash_types;
          __le16 indirection_table_mask;
          __le16 unclassified_queue;
          __le16 indirection_table[];
}
struct virtio_net_rss_config_tailer {
          __le16 max_tx_vq;
          u8 hash_key_length;
          u8 hash_key_data[];
}

These two are used by TUNSETVNETRSS. And simply reuse the virtio_net_hash_config for TUNSETVETHASH.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

With this, we can tweak the virtio-net driver with this new uAPI. Then

tap* can reuse this.

I implemented a UAPI and driver change accordingly:
https://lore.kernel.org/r/[email protected]

This is a nice improvement for the driver, but I still don't think it is suited for the UAPI of tuntap.

Quoted 8 lines

Any reason for this? It should work like virtio_net_hdr.

Quoted 3 lines

The requirements of extra fields and
little endian cannot be removed from the virtio spec but they are irrelevant for tuntap.

I don't understand this part. What fields are "extra" and need to be removed from the spec?

All fields not included in struct tun_vnet_hash and struct tun_vnet_hash_rss. Namely, for struct virtio_net_hash_config:
- reserved
- hash_key_length
- hash_key_data

For struct virtio_net_rss_config:
- max_tx_vq
- hash_key_length

Quoted 7 lines
Quoted 5 lines

See my above reply, and I basically meant

TUNSETVETHASH accept struct virtio_net_hash_config; TUNSETVETRSS accept struct virtio_net_rss_config_hdr + struct virtio_net_rss_config_trailer;

That still bring the extra fields I noted in the last email.

I don't know how to define "extra" here. Let's summarize here:

Method A:

1) virtio specification use separate commands for has_report and rss
2) hash_port ans rss doesn't depend on each other
3) reuse virtio-net uAPI

Method B:

1) trying to define and remove the "extra" fields in tuntap, and redefine it in TUNTAP

It would always be much easier to start from simply reusing the virtio-net uAPI. Method B makes both the implementation and reviewing harder, as we need to

1) revisit the design of the virtio spec, this needs to be done in the virtio community not here
2) audit the difference between virtio spec and TUN/TAP, that's why we have a very long discussion here

For example, the root cause of why you think the max_tx_vq is "extra" is:

1) The spec defines VIRTIO_NET_F_RSS and VIRTIO_NET_F_MQ as independent features
2) Your code tries to re-use IFF_MULTI_QUEUE for both VIRTIO_NET_F_RSS and VIRTIO_NET_F_MQ, this would have a lot of implications, e.g automatic steering might be applied when only RSS is negotiated etc

Quoted 4 lines

The correct way to implement this is:

1) Introduce IFF_RSS and only set it during TUNSETIFF when device only offers RSS

Please see the summary of "my points that support having one ioctl to negotiate features and configuration" I wrote the above.

Quoted 2 lines

2) reuse virtio-net uAPI and accept max_tx_vq and use that to change the queue(or queue paris) if necessary

I don't think it's possible; we need file descriptors associated with queues, which is something you cannot express with the virtio-net structures.

Regards,
Akihiko Odaki

Quoted 75 lines

Then we have a clean and well defined behaviour (for example when devices only support RSS but not MQ).

Thanks

Quoted 68 lines

Regards,
Akihiko Odaki

Quoted 63 lines

Thanks

Quoted 59 lines

Regards,
Akihiko Odaki

Quoted 54 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The best possibility is to share structures, not commands, and I don't think even sharing structures makes sense here because of the reasons described above.

Quoted 8 lines
Quoted 3 lines

I don't want to share structures, I meant starting from something that is simple and has been sorted in the virtio spec. Optimization could be done on top.

I meant to reuse the structures in virtio_net.h.

Regards,
Akihiko Odaki

Thanks

Quoted 35 lines
Quoted 34 lines

Thanks

Quoted 28 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Thanks

Quoted 19 lines

Regards,
Akihiko Odaki

Quoted 14 lines
Quoted 9 lines

The paramter will be duplicated if we have separate ioctls for RSS and hash reporting, and the kernel will have a chiken-egg problem when ensuring they are synchronized; when the ioctl for RSS is issued, should the kernel ensure the "types" parameter is identical with one specified for hash reporting? It will not work if the userspace may decide to configure hash reporting after RSS.

See my reply above.

Thanks

#30 lore raw

Re: [PATCH net-next v9 3/6] tun: Introduce virtio-net hash feature

Quoted 55 lines · Akihiko Odaki
Quoted 53 lines · Jason Wang
Quoted 52 lines · Akihiko Odaki
Quoted 50 lines · Jason Wang
Quoted 49 lines · Akihiko Odaki
Quoted 47 lines · Jason Wang
Quoted 46 lines · Akihiko Odaki
Quoted 44 lines · Jason Wang
Quoted 43 lines · Akihiko Odaki
Quoted 41 lines · Jason Wang
Quoted 40 lines · Akihiko Odaki
Quoted 38 lines · Jason Wang
Quoted 37 lines · Akihiko Odaki

On 2025/03/11 9:38, Jason Wang wrote:
On Mon, Mar 10, 2025 at 3:45 PM Akihiko Odaki <[email protected]> wrote:

On 2025/03/10 12:55, Jason Wang wrote:
On Fri, Mar 7, 2025 at 7:01 PM Akihiko Odaki <[email protected]> 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.

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.

Signed-off-by: Akihiko Odaki <[email protected]>
Tested-by: Lei Yang <[email protected]>
---

[...]

Quoted 112 lines
Quoted 112 lines
Quoted 112 lines
Quoted 111 lines
Quoted 102 lines
Quoted 79 lines
Quoted 79 lines
Quoted 79 lines
Quoted 78 lines
Quoted 10 lines

What's more, we've already had virito-net uAPI. Why not simply reusing them?

See the above.

Quoted 54 lines
Quoted 41 lines
Quoted 10 lines
Quoted 6 lines

RSS and hash reporting must share
this parameter when both are enabled at the same time; otherwise RSS may
compute hash values that are not suited for hash reporting.

Is this mandated by the spec? If yes, we can add a check. If not, userspace risk themselves as a mis-configuration which we don't need to bother.

Yes, it is mandated. 5.1.6.4.3 Hash calculation for incoming packets says:
> A device attempts to calculate a per-packet hash in the following > cases:
>
> - The feature VIRTIO_NET_F_RSS was negotiated. The device uses the
> hash to determine the receive virtqueue to place incoming packets.
> - The feature VIRTIO_NET_F_HASH_REPORT was negotiated. The device
> reports the hash value and the hash type with the packet.
>
> If the feature VIRTIO_NET_F_RSS was negotiated:
>
> - The device uses hash_types of the virtio_net_rss_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
hash_key_length of the virtio_net_rss_config structure (see
> 5.1.6.5.7.1).
>
> If the feature VIRTIO_NET_F_RSS was not negotiated:
>
> - The device uses hash_types of the virtio_net_hash_config structure
> as ’Enabled hash types’ bitmask.
> - The device uses a key as defined in hash_key_data and
> hash_key_length of the virtio_net_hash_config structure (see
> .1.6.5.6.4).

So when both VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT are negotiated, virtio_net_rss_config not only controls RSS but also the reported hash values and types. They cannot be divergent.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Note that spec use different commands for hash_report and rss.

TUNSETVNETHASH is different from these commands in terms that it also negotiates VIRTIO_NET_F_HASH_REPORT and VIRTIO_NET_F_RSS.

There Are different "issues" here:

1) Whether or not we need to use a unified API for negotiating RSS and HASH_REPORT features
2) Whether or not we need to sue a unified API for setting RSS and HASH_REPORT configuration

Quoted 7 lines

What I want to say is point 2. But what you raise is point 1.

For simplicity, it looks to me like it's a call for having separated ioctls for feature negotiation (for example via TUNSETIFF). You may argue that either RSS or HASH_REPORT requires configurations, we can just follow what spec defines or not (e.g what happens if RSS/HASH_REPORT were negotiated but no configurations were set).

Unfortunately TUNSETIFF does not fit in this use case. The flags set with TUNSETIFF are fixed, but the guest can request a different feature set anytime by resetting the device.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

TUNSETIFF, enables the device to be able to handle RSS/HASREPORT. TUNSETHASH/RSS. dealing with RSS/HASH command from userspace.

We also needs to be able to disable them at runtime so that we can handle resets.

Via TUNSETHASH/RSS? I think it should have a way to accept parameters that disable RSS or hash report.

That's what this patch implements. TUNSETVNETHASH accepts parameters to choose what features to be enabled.

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

This is the way we used to do for multi queue and vnet header. TUNSETIFF requires CAP_NET_ADMIN, this could be an extra safe guard for unprivileged userspace.

I intend to allow using this feature without privilege. A VMM is usually unprivileged and requiring a privilege to configure tuntap is too prohibitive.

Quoted 7 lines
Quoted 3 lines

For safety, tun is not allowed to be created by unprivileged users. And it's not to configure the tuntap dynamically, it's about telling the function that tuntap can have (not necessarily enabled though) .

I don't think we need another barrier for the new functions. Once an unprivileged user get a file descriptor of tuntap from a privileged user, they are free to enable RSS and/or hash reporting.

Only if such a feature is allowed by the privileged user.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I don't see a reason not to allow the feature to unprivileged users. It only complicates the setup.

For safety, e.g reduce the chance for unprivileged user to explore part of the kernel codes.

It indeed reduces the attack surface, but it's fine without the reduction I guess? It's not a feature so complicated;

I don't know how to define complicated things here but simplicity doesn't necessarily mean safety.

Quoted 4 lines

I saw there were
complicated changes like namespaces and io_uring that caused controversy when exposing them to unprivilged users, but this feature is not like them, I suppose.

We limit feature setting through tun_set_iff in the past. Instead of trying to argue if RSS is safe to be enabled without TUNSETIFF, following what has been used in the past is always simpler and easier.

Quoted 77 lines
Quoted 70 lines
Quoted 51 lines
Quoted 51 lines
Quoted 42 lines
Quoted 42 lines
Quoted 41 lines
Quoted 31 lines
Quoted 22 lines

> >> In the virtio-net specification, it is not defined what would
happen if

Quoted 10 lines
Quoted 3 lines

these features are negotiated but the VIRTIO_NET_CTRL_MQ_RSS_CONFIG or VIRTIO_NET_CTRL_MQ_HASH_CONFIG commands are not sent. There is no such ambiguity with TUNSETVNETHASH.

So I don't see advantages of unifying hash reports and rss into a single ioctl. Let's just follow what has been done in the spec that uses separated commands. Tuntap is not a good place to debate whether those commands could be unified or not. We need to move it to the spec but assuming spec has been done, it might be too late or too few advantages for having another design.

Quoted 4 lines

It makes sense for the spec to reuse the generic feature negotiation mechanism, but the situation is different for tuntap; we cannot use TUNSETIFF and need to define another. Then why don't we exploit this opportunity to have an interface with well-defined semantics?

That's perfectly fine, but it needs to be done in virtio-net's uAPI not tun's. What's more, if you think two commands are not well-defined, let's fix that in the virtio spec first.

Quoted 6 lines
Quoted 3 lines

The virtio
spec does its best as an interface between the host and guest and tuntap does its best as an UAPI.

See above, let's fix the uAPI first. We don't want DPDK to use tun's uAPI for RSS

virtio-net's UAPI is for the virtio spec which has a capable generic feature negotiation mechanism. tuntap needs its own feature negotiation and it's nothing to do with virtio-net's UAPI.

Quoted 6 lines
Quoted 2 lines

Well, I don't mean the part of the feature negotiation. I mean the part for rss and hash report configuration.

The feature negotiation still matters when deciding the granularity of ioctls. We need one ioctl for a feature negotiation, and to avoid having an intermediate state,

I don't understand this. For example, driver can choose to

Quoted 8 lines
Quoted 6 lines

1) negotiate RSS
2) do something else.
3) configure RSS

Spec doesn't require those two to be configured at the same time, so "intermediate state" is allowed.

The spec doesn't define what should happen in the intermediate state either.

Yes but my point is that in the uAPI layer we don't need to care about the intermediate state. It can just work as other features, e.g having a default state after feature negotiation is more than enough. This is the way we deal with other features like vnet header etc.

Quoted 5 lines
Quoted 1 line

For a hardware implementation I think it's fine whatever the implementation defines as the intermediate state. But for the UAPI, it's better avoiding having such a definition to keep the interface minimal and maximize the UAPI stability.

Quoted 6 lines

Well, even if you think there's an issue:

1) I don't see how we can avoid the intermediate state consider guest have such state
2) We need to "fix" virtio spec and virito-net first, tuntap is not the right place to workaround virtio specific issues

Quoted 7 lines

Let me summarize my points that support having one ioctl to negotiate features and configuration:

The virtio spec has a generic feature negotiation mechanism and reusing it resulted in having an intermediate state between the feature negotiation and configuration. There is nothing wrong about that so we don't need to "fix" the virtio spec.

Good to know that.

Quoted 3 lines

tuntap can also perform feature negotitaion with TUNSETIFF, but TUNSETIFF have a few problems:

TUNSETIFF is not feature negotiation, it's about device or queue provisioning as well as the features. From the view of the virtio, it is used to provision the device_features. For example, qemu only calls TUNSETIFF when it tries to open the tap fd.

Quoted 6 lines

1. It requires a privilege. One can argue that it reduces the attack surface and it indeed does, but it's fine without the reduction I guess? It's not a feature so complicated; I saw there were complicated changes like namespaces and io_uring that caused controversy when exposing them to unprivilged users, but this feature is not like them.

I'm not asking to invent something new, but just reuse the security stuff that has been already used for more than a decade. It would be always easier to relax the check instead of enforce the check which may break uAPI. I can imagine it would not take a lot of codes to achieve this.

Quoted 3 lines

2. It cannot change the enabled feature set at runtime. The virtio spec allows changing it by resetting.

RSS is not the first feature of those requirements. TUN has implemented various virtio specific features in the past.

Quoted 4 lines

So we need to design a set of new ioctls for both feature negotiation and configuration. When doing so, eliminating the intermediate state is a good principle to determine the optimal size of ioctls.

As discussed, having a default state after TUNSETIFF is more than enough. That is how a multi queue/vnet header works:

1) for multiqueue, when IFF_MULTIQUEUE is set, starting with 1 queue
2) for vnet header, vnet header will be zero unless TUNSETVETHDR is called

I don't see how RSS makes anything different.

For intermediate states, with your proposal, it still requires the userspace to assume a default state when doing TUNSETVETRSS etc.

Quoted 5 lines

In theory, it is possible to have small ioctls that set only one scalar value or even one bit, but that doesn't make sense. This principle helps determine the optimal size of ioctls; it minimizes the complexity of both the userspace and the kernel.

Well the complexity is not measured by the number of ioctls or structures. I basically meant:

1) IF_RSS to provision the device with the RSS features, this could be fetched from TUNGETIFF
2) Having a default state implemented in TUN that complies with the spec
3) TUNSETVET/GETHASH to send and receive RSS configuration

Quoted 38 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines

the ioctl should also do the configuration. Hence
that one ioctl should do all of the feature negotiation and configuration.

Quoted 6 lines
Quoted 6 lines

The structures for two commands have unused or redundant fields and a flexible array in the middle of the structure, but they are ABIs so we can't change it.

Quoted 8 lines
Quoted 5 lines

DPDK is another reason to define tuntap's own UAPIs. They don't care unused or redundant fields and a flexible array in middle that are present in the virtio spec. It will also not want to deal with the requirement of little endian. Constructing struct virtio_net_rss_config is an extra burden for DPDK.

I meant for vhost-user implementation in DPDK, it needs to use virtio-net uAPI not tuntap's for example.

The vhost-user implementation will use tuntap's UAPIs for its ethernet device backend.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

That sounds pretty weird, vhost-user has nothing related to tuntap.

My expression in the last email was weird. More precisely, the ethernet backend of tuntap will use the UAPIs, and the vhost-user will use the ethernet backend in turn.

I don't understand what "ethernet backend" means here.

It is a driver that serves the Ethernet Device API, which is agnostic on application and driver. The Ethernet Device API, including RSS configuration is documented at:
https://doc.dpdk.org/api/rte__ethdev_8h.html

Quoted 4 lines

The Ethernet API are not bound to the virtio spec since they are not specific to the vhost application or the tuntap driver. Hence they operate in native endian and do not have extra fields, and tuntap's structures are more suited to the ethernet backend than the virtio ones.

vhost-user is the device implementation not an ethernet driver. Why did it use tuntap's uAPI and do the useless endian conversion twice?

Quoted 28 lines
Quoted 28 lines
Quoted 14 lines
Quoted 14 lines
Quoted 8 lines

It uses the generic interface of ethernet device so for
RSS it will use functions like rte_eth_dev_rss_hash_update() for example. tuntap's UAPIs are more suited to implement these interfaces as they operate in native endian and don't have extra fields.

Nope, for example it needs to use le for virtio_net_hdr if a modern device is used. But it needs a "native" endian according to the guest endian via TUNSETVNETLE/BE. We don't have a choice as virtio-net hdr support in tuntap is much earlier than modern devices.

Quoted 6 lines
Quoted 2 lines

Let's don't do the same thing (native endian) for tuntap as RSS depends on modern, so we know it must be le.

virtio_net_hdr is the data path while the current discussion is about the control path. All configuration knobs of tuntap operates in the native endian.

Because they are not directly related to virtio specification. We don't want to duplicate virtio-net with our own version every time E.g once RSSv2 or aRFS were implemented. Or I would even introduce a single uAPI to transport possible cvq commands then we can avoid inventing new ioctls that just transport cvq commands.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

So I think we should stick to the little endian for the data path while we should stick to the native endian for the control path to maximize the consistency.

I don't see a reason to differ datapath from control path. Virtio-net uAPI has been reused by tuntap for more than a decade.

tuntap's control path all operate in the native endian.

It's just a description of the current status, people can easily say tuntap's data path all operate in the native endian before the support of version 1.0.

Quoted 2 lines

They never used
the endian of the data path in the control path.

Once virtio uAPI can be reused, we need to do that.

Quoted 147 lines
Quoted 139 lines
Quoted 102 lines
Quoted 102 lines
Quoted 20 lines

DPDk applications other than vhost-user also matter; they do not care what virtio does at all.

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

On the other hand, Constructing tuntap-specific structures is not that complicated for VMMs.

Not complicated but redundant.

Quoted 4 lines

A VMM will need to inspect struct
virtio_net_rss_config anyway to handle migration and check its size so it can store the values it inspected to struct tun_vnet_hash and struct tun_vnet_hash_rss and pass them to the kernel.

Quoted 7 lines
Quoted 4 lines

I don't see how rss and hash reports differ from what we have now. Those inspections must be done anyhow for compatibility for example the check of offloading features. Such steps could not be eliminated no matter how we design the uAPI.

I explained the difference between the virtio and tuntap UAPIs, not between RSS and hash reporting.

See above.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

The overall userspace implementation will be simpler by having structures specifically tailored for the communication between the userspace and kernel.

Quoted 6 lines
Quoted 2 lines

This is exactly how a good uAPI should behave. If uAPI in virtio-net can't do this, I don't understand why uAPI in tuntap can solve it.

The UAPI in virtio-net cannot do it because it's already fixed and it also needs to perform endian conversion for the VM use case. tuntap doesn't have these restrictions.

Same here.

Quoted 55 lines
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
Quoted 9 lines

I don't think there is an advantage to split ioctls to follow the spec after all. It makes sense if we can pass-through virtio commands to tuntap, but it is not possible as ioctl operation codes are different from virtio commands.

I don't see a connection with the operation code. For example, we can add new uAPIs in virtio-net which could be something like:

     struct virtio_net_rss_config_header {
          __le32 hash_types;
          __le16 indirection_table_mask;
          __le16 unclassified_queue;
          __le16 indirection_table[];
}
struct virtio_net_rss_config_tailer {
          __le16 max_tx_vq;
          u8 hash_key_length;
          u8 hash_key_data[];
}

These two are used by TUNSETVNETRSS. And simply reuse the virtio_net_hash_config for TUNSETVETHASH.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

With this, we can tweak the virtio-net driver with this new uAPI. Then

tap* can reuse this.

I implemented a UAPI and driver change accordingly:
https://lore.kernel.org/r/[email protected]

This is a nice improvement for the driver, but I still don't think it is suited for the UAPI of tuntap.

Quoted 8 lines

Any reason for this? It should work like virtio_net_hdr.

Quoted 3 lines

The requirements of extra fields and
little endian cannot be removed from the virtio spec but they are irrelevant for tuntap.

I don't understand this part. What fields are "extra" and need to be removed from the spec?

All fields not included in struct tun_vnet_hash and struct tun_vnet_hash_rss. Namely, for struct virtio_net_hash_config:
- reserved
- hash_key_length
- hash_key_data

For struct virtio_net_rss_config:
- max_tx_vq
- hash_key_length

Quoted 7 lines
Quoted 5 lines

See my above reply, and I basically meant

TUNSETVETHASH accept struct virtio_net_hash_config; TUNSETVETRSS accept struct virtio_net_rss_config_hdr + struct virtio_net_rss_config_trailer;

That still bring the extra fields I noted in the last email.

I don't know how to define "extra" here. Let's summarize here:

Method A:

1) virtio specification use separate commands for has_report and rss
2) hash_port ans rss doesn't depend on each other
3) reuse virtio-net uAPI

Method B:

1) trying to define and remove the "extra" fields in tuntap, and redefine it in TUNTAP

It would always be much easier to start from simply reusing the virtio-net uAPI. Method B makes both the implementation and reviewing harder, as we need to

1) revisit the design of the virtio spec, this needs to be done in the virtio community not here
2) audit the difference between virtio spec and TUN/TAP, that's why we have a very long discussion here

For example, the root cause of why you think the max_tx_vq is "extra" is:

1) The spec defines VIRTIO_NET_F_RSS and VIRTIO_NET_F_MQ as independent features
2) Your code tries to re-use IFF_MULTI_QUEUE for both VIRTIO_NET_F_RSS and VIRTIO_NET_F_MQ, this would have a lot of implications, e.g automatic steering might be applied when only RSS is negotiated etc

Quoted 4 lines

The correct way to implement this is:

1) Introduce IFF_RSS and only set it during TUNSETIFF when device only offers RSS

Please see the summary of "my points that support having one ioctl to negotiate features and configuration" I wrote the above.

Quoted 6 lines
Quoted 2 lines

2) reuse virtio-net uAPI and accept max_tx_vq and use that to change the queue(or queue paris) if necessary

I don't think it's possible; we need file descriptors associated with queues, which is something you cannot express with the virtio-net structures.

So:

1) Provisioning queues were still done via TUNSETIFF
2) We just need to hook max_tx_vq (the helpers were already there) to the helpers to enable and disable a queue instead of depending on the TUNSETQUEUE

Quoted 3 lines

Regards,
Akihiko Odaki

Thanks