Skip to contentSkip to conversation

tun: Introduce virtio-net hashing feature

PATCH In review Akihiko Odaki 5 participants 39 messages May 30, 2025 – Jul 16, 2025

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

v12 cover tun: Introduce virtio-net hashing feature

NOTE: I'm leaving Daynix Computing Ltd., for which I worked on this patch series, by the end of this month.

While net-next is closed, this is the last chance for me to send another version so let me send the local changes now.

Please contact Yuri Benditovich, who is CCed on this email, for anything about this series.

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 v12:
- Updated tools/testing/selftests/net/config.
- Split TUNSETVNETHASH.
- Link to v11: https://lore.kernel.org/r/[email protected]

Changes in v11:
- Added the missing code to free vnet_hash in patch
"tap: Introduce virtio-net hash feature".
- Link to v10: https://lore.kernel.org/r/[email protected]

Changes in v10:
- Split common code and TUN/TAP-specific code into separate patches.
- Reverted a spurious style change in patch "tun: Introduce virtio-net hash feature".
- Added a comment explaining disable_ipv6 in tests.
- Used AF_PACKET for patch "selftest: tun: Add tests for
virtio-net hashing". I also added the usage of FIXTURE_VARIANT() as the testing function now needs access to more variant-specific variables.
- Corrected the message of patch "selftest: tun: Add tests for virtio-net hashing"; it mentioned validation of configuration but it is not scope of this patch.
- Expanded the description of patch "selftest: tun: Add tests for virtio-net hashing".
- Added patch "tun: Allow steering eBPF program to fall back".
- Changed to handle TUNGETVNETHASHCAP before taking the rtnl lock.
- Removed redundant tests for tun_vnet_ioctl().
- Added patch "selftest: tap: Add tests for virtio-net ioctls".
- Added a design explanation of ioctls for extensibility and migration.
- Removed a few branches in patch
"vhost/net: Support VIRTIO_NET_F_HASH_REPORT".
- Link to v9: https://lore.kernel.org/r/[email protected]

Changes in v9:
- Added a missing return statement in patch
"tun: Introduce virtio-net hash feature".
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- Disabled IPv6 to eliminate noises in tests.
- Added a branch in tap to avoid unnecessary dissection when hash reporting is disabled.
- Removed unnecessary rtnl_lock().
- Extracted code to handle new ioctls into separate functions to avoid adding extra NULL checks to the code handling other ioctls.
- Introduced variable named "fd" to __tun_chr_ioctl().
- s/-/=/g in a patch message to avoid confusing Git.
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Ensured to set hash_report to VIRTIO_NET_HASH_REPORT_NONE for VHOST_NET_F_VIRTIO_NET_HDR.
- s/4/sizeof(u32)/ in patch "virtio_net: Add functions for hashing".
- Added tap_skb_cb type.
- Rebased.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Extracted changes to fill vnet header holes into another series.
- Squashed patches "skbuff: Introduce SKB_EXT_TUN_VNET_HASH", "tun:
Introduce virtio-net hash reporting feature", and "tun: Introduce virtio-net RSS" into patch "tun: Introduce virtio-net hash feature".
- Dropped the RFC tag.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Fixed a compilation error with CONFIG_TUN_VNET_CROSS_LE.
- Optimized the calculation of the hash value according to:
https://git.dpdk.org/dpdk/commit/?id=3fb1ea032bd6ff8317af5dac9af901f1f324cab4
- Added patch "tun: Unify vnet implementation".
- Dropped patch "tap: Pad virtio header with zero".
- Added patch "selftest: tun: Test vnet ioctls without device".
- Reworked selftests to skip for older kernels.
- Documented the case when the underlying device is deleted and packets have queue_mapping set by TC.
- Reordered test harness arguments.
- Added code to handle fragmented packets.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Moved tun_vnet_hash_ext to if_tun.h.
- Renamed virtio_net_toeplitz() to virtio_net_toeplitz_calc().
- Replaced htons() with cpu_to_be16().
- Changed virtio_net_hash_rss() to return void.
- Reordered variable declarations in virtio_net_hash_rss().
- Removed virtio_net_hdr_v1_hash_from_skb().
- Updated messages of "tap: Pad virtio header with zero" and
"tun: Pad virtio header with zero".
- Fixed vnet_hash allocation size.
- Ensured to free vnet_hash when destructing tun_struct.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Reverted back to add ioctl.
- Split patch "tun: Introduce virtio-net hashing feature" into
"tun: Introduce virtio-net hash reporting feature" and
"tun: Introduce virtio-net RSS".
- Changed to reuse hash values computed for automq instead of performing RSS hashing when hash reporting is requested but RSS is not.
- Extracted relevant data from struct tun_struct to keep it minimal.
- Added kernel-doc.
- Changed to allow calling TUNGETVNETHASHCAP before TUNSETIFF.
- Initialized num_buffers with 1.
- Added a test case for unclassified packets.
- Fixed error handling in tests.
- Changed tests to verify that the queue index will not overflow.
- Rebased.
- Link to v2: https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (10):
virtio_net: Add functions for hashing
net: flow_dissector: Export flow_keys_dissector_symmetric
tun: Allow steering eBPF program to fall back
tun: Add common virtio-net hash feature code
tun: Introduce virtio-net hash feature
tap: Introduce virtio-net hash feature
selftest: tun: Test vnet ioctls without device
selftest: tun: Add tests for virtio-net hashing
selftest: tap: Add tests for virtio-net ioctls vhost/net: Support VIRTIO_NET_F_HASH_REPORT

 Documentation/networking/tuntap.rst  |   7 +
 drivers/net/Kconfig                  |   1 +
 drivers/net/ipvlan/ipvtap.c          |   2 +-
 drivers/net/macvtap.c                |   2 +-
 drivers/net/tap.c                    |  80 +++++-
 drivers/net/tun.c                    |  92 +++++--
 drivers/net/tun_vnet.h               | 165 +++++++++++-
 drivers/vhost/net.c                  |  68 ++---
 include/linux/if_tap.h               |   4 +-
 include/linux/skbuff.h               |   3 +
 include/linux/virtio_net.h           | 188 ++++++++++++++
 include/net/flow_dissector.h         |   1 +
 include/uapi/linux/if_tun.h          |  80 ++++++
 net/core/flow_dissector.c            |   3 +-
 net/core/skbuff.c                    |   4 +
 tools/testing/selftests/net/Makefile |   2 +-
 tools/testing/selftests/net/config   |   1 +
 tools/testing/selftests/net/tap.c    | 131 +++++++++-
 tools/testing/selftests/net/tun.c    | 485 ++++++++++++++++++++++++++++++++++-
 19 files changed, 1234 insertions(+), 85 deletions(-)

---
base-commit: 5cb8274d66c611b7889565c418a8158517810f9b
change-id: 20240403-rss-e737d89efa77

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw

PATCH v12 1/10 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 02a9f4dc594d..426f33b4b824 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.49.0
#2 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 190 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 +172 -0
index 02a9f4dc594d..426f33b4b824 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:

include/linux/virtio_net.h line 193

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

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

Thanks

Quoted 13 lines
include/linux/virtio_net.h +6 -1
+       }
+
+       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.49.0

#3 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 198 lines · Jason Wang
Quoted 197 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 +179 -0
index 02a9f4dc594d..426f33b4b824 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;
+
Quoted 8 lines

include/linux/virtio_net.h line 136

Quoted 3 lines
include/linux/virtio_net.h +3 -0
+       default:
+               hash->report = VIRTIO_NET_HASH_REPORT_NONE;
+               return;

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

This is on the device side, and don't report HASH_TYPE_XXX_EX.

Quoted 3 lines

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

5.1.6.4.3.4 "IPv6 packets with extension header" says:

Quoted 10 lines

If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
- Home address from the home address option in the IPv6 destination options header. If the extension header is not present, use the Source IPv6 address.
- IPv6 address that is contained in the Routing-Header-Type-2 from the associated extension header. If the extension header is not present, use the Destination IPv6 address.
- Source TCP port
- Destination TCP port

Therefore, if VIRTIO_NET_HASH_TYPE_TCP_EX is set, the packet has a TCPv6 and an home address option in the IPv6 destination options header is present, the hash is calculated over the home address. If the hash is not calculated over the home address in such a case, the device is contradicting with this section and violating the spec. The same goes for the other HASH_TYPE_XXX_EX types and Routing-Header-Type-2.

Regards,
Akihiko Odaki

#4 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 227 lines · Akihiko Odaki
Quoted 198 lines · Jason Wang
Quoted 197 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 +179 -0
index 02a9f4dc594d..426f33b4b824 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;
+
Quoted 8 lines

include/linux/virtio_net.h line 136

Quoted 3 lines
include/linux/virtio_net.h +3 -0
+       default:
+               hash->report = VIRTIO_NET_HASH_REPORT_NONE;
+               return;

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

This is on the device side, and don't report HASH_TYPE_XXX_EX.

Quoted 3 lines

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

5.1.6.4.3.4 "IPv6 packets with extension header" says:

Quoted 10 lines

If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
- Home address from the home address option in the IPv6 destination options header. If the extension header is not present, use the Source IPv6 address.
- IPv6 address that is contained in the Routing-Header-Type-2 from the associated extension header. If the extension header is not present, use the Destination IPv6 address.
- Source TCP port
- Destination TCP port

Quoted 6 lines

Therefore, if VIRTIO_NET_HASH_TYPE_TCP_EX is set, the packet has a TCPv6 and an home address option in the IPv6 destination options header is present, the hash is calculated over the home address. If the hash is not calculated over the home address in such a case, the device is contradicting with this section and violating the spec. The same goes for the other HASH_TYPE_XXX_EX types and Routing-Header-Type-2.

Just to make sure we are one the same page. I meant:

1) If the hash is not calculated over the home address (in the case of IPv6 destination destination), it can still report VIRTIO_NET_RSS_HASH_TYPE_IPv6. This is what you implemented in your series. So the device can simply fallback to e.g TCPv6 if it can't understand all or part of the IPv6 options.
2) the VIRTIO_NET_SUPPORTED_HASH_TYPES is not checked against the tun_vnet_ioctl_sethash(), so userspace may set VIRTIO_NET_HASH_TYPE_TCP_EX regardless of what has been returned by tun_vnet_ioctl_gethashtypes(). In this case they won't get VIRTIO_NET_HASH_TYPE_TCP_EX.
3) implementing part of the hash types might complicate the migration or at least we need to describe the expectations of libvirt or other management in this case. For example, do we plan to have a dedicated Qemu command line like:

-device virtio-net-pci,hash_report=on,supported_hash_types=X,Y,Z?

Thanks

Quoted 4 lines

Regards,
Akihiko Odaki

#5 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 235 lines · Jason Wang
Quoted 227 lines · Akihiko Odaki
Quoted 198 lines · Jason Wang
Quoted 197 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 +179 -0
index 02a9f4dc594d..426f33b4b824 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;
+
Quoted 8 lines

include/linux/virtio_net.h line 136

Quoted 3 lines
include/linux/virtio_net.h +3 -0
+       default:
+               hash->report = VIRTIO_NET_HASH_REPORT_NONE;
+               return;

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

This is on the device side, and don't report HASH_TYPE_XXX_EX.

Quoted 3 lines

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

5.1.6.4.3.4 "IPv6 packets with extension header" says:

Quoted 10 lines

If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
- Home address from the home address option in the IPv6 destination options header. If the extension header is not present, use the Source IPv6 address.
- IPv6 address that is contained in the Routing-Header-Type-2 from the associated extension header. If the extension header is not present, use the Destination IPv6 address.
- Source TCP port
- Destination TCP port

Quoted 6 lines

Therefore, if VIRTIO_NET_HASH_TYPE_TCP_EX is set, the packet has a TCPv6 and an home address option in the IPv6 destination options header is present, the hash is calculated over the home address. If the hash is not calculated over the home address in such a case, the device is contradicting with this section and violating the spec. The same goes for the other HASH_TYPE_XXX_EX types and Routing-Header-Type-2.

Quoted 7 lines

Just to make sure we are one the same page. I meant:

1) If the hash is not calculated over the home address (in the case of IPv6 destination destination), it can still report VIRTIO_NET_RSS_HASH_TYPE_IPv6. This is what you implemented in your series. So the device can simply fallback to e.g TCPv6 if it can't understand all or part of the IPv6 options.

The spec says it can fallback if "the extension header is not present", not if the device can't understand the extension header.

Quoted 5 lines

2) the VIRTIO_NET_SUPPORTED_HASH_TYPES is not checked against the tun_vnet_ioctl_sethash(), so userspace may set VIRTIO_NET_HASH_TYPE_TCP_EX regardless of what has been returned by tun_vnet_ioctl_gethashtypes(). In this case they won't get VIRTIO_NET_HASH_TYPE_TCP_EX.

That's right. It's the responsibility of the userspace to set only the supported hash types.

Quoted 6 lines

3) implementing part of the hash types might complicate the migration or at least we need to describe the expectations of libvirt or other management in this case. For example, do we plan to have a dedicated Qemu command line like:

-device virtio-net-pci,hash_report=on,supported_hash_types=X,Y,Z?

I posted a patch series to implement such a command line for vDPA[1]. The patch series that wires this tuntap feature up[2] reuses the infrastructure so it doesn't bring additional complexity.

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/

Regards,
Akihiko Odaki

#6 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 239 lines · Akihiko Odaki
Quoted 237 lines · Jason Wang
Quoted 227 lines · Akihiko Odaki
Quoted 198 lines · Jason Wang
Quoted 197 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 +179 -0
index 02a9f4dc594d..426f33b4b824 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;
+
Quoted 8 lines

include/linux/virtio_net.h line 136

Quoted 3 lines
include/linux/virtio_net.h +3 -0
+       default:
+               hash->report = VIRTIO_NET_HASH_REPORT_NONE;
+               return;

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

This is on the device side, and don't report HASH_TYPE_XXX_EX.

Quoted 3 lines

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

5.1.6.4.3.4 "IPv6 packets with extension header" says:

Quoted 10 lines

If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
- Home address from the home address option in the IPv6 destination options header. If the extension header is not present, use the Source IPv6 address.
- IPv6 address that is contained in the Routing-Header-Type-2 from the associated extension header. If the extension header is not present, use the Destination IPv6 address.
- Source TCP port
- Destination TCP port

Quoted 6 lines

Therefore, if VIRTIO_NET_HASH_TYPE_TCP_EX is set, the packet has a TCPv6 and an home address option in the IPv6 destination options header is present, the hash is calculated over the home address. If the hash is not calculated over the home address in such a case, the device is contradicting with this section and violating the spec. The same goes for the other HASH_TYPE_XXX_EX types and Routing-Header-Type-2.

Just to make sure we are one the same page. I meant:

Quoted 8 lines
Quoted 5 lines

1) If the hash is not calculated over the home address (in the case of IPv6 destination destination), it can still report VIRTIO_NET_RSS_HASH_TYPE_IPv6. This is what you implemented in your series. So the device can simply fallback to e.g TCPv6 if it can't understand all or part of the IPv6 options.

The spec says it can fallback if "the extension header is not present", not if the device can't understand the extension header.

I don't think so,

1) spec had a condition beforehand:

"""
If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
...
If the extension header is not present ...
"""

So the device can choose not to set VIRTIO_NET_HASH_TYPE_TCP_EX as spec doesn't say device MUST set VIRTIO_NET_HASH_TYPE_TCP_EX if ...

2) implementation wise, since device has limited resources, we can't expect the device can parse arbitrary number of ipv6 options

3) if 1) and 2) not the case, we need fix the spec otherwise implement a spec compliant device is impractical

Quoted 9 lines
Quoted 6 lines

2) the VIRTIO_NET_SUPPORTED_HASH_TYPES is not checked against the tun_vnet_ioctl_sethash(), so userspace may set VIRTIO_NET_HASH_TYPE_TCP_EX regardless of what has been returned by tun_vnet_ioctl_gethashtypes(). In this case they won't get VIRTIO_NET_HASH_TYPE_TCP_EX.

That's right. It's the responsibility of the userspace to set only the supported hash types.

Well, the kernel should filter out the unsupported one to have a robust uAPI. Otherwise, we give green light to the buggy userspace which will have unexpected results.

Quoted 8 lines
Quoted 7 lines

3) implementing part of the hash types might complicate the migration or at least we need to describe the expectations of libvirt or other management in this case. For example, do we plan to have a dedicated Qemu command line like:

-device virtio-net-pci,hash_report=on,supported_hash_types=X,Y,Z?

Quoted 8 lines

I posted a patch series to implement such a command line for vDPA[1]. The patch series that wires this tuntap feature up[2] reuses the infrastructure so it doesn't bring additional complexity.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/
[2]
https://lore.kernel.org/qemu-devel/[email protected]/

I meant, if we implement a full hash report feature, it means a single hash cmdline option is more than sufficient and so compatibility code can just turn it off when dealing with machine types. This is much more simpler than

1) having both hash as well as supported_hash_features
2) dealing both hash as well as supported_hash_features in compatibility codes
3) libvirt will be happy

For [1], it seems it introduces a per has type option, this seems to be a burden to the management layer as it need to learn new option everytime a new hash type is supported

Thanks

Quoted 4 lines

Regards,
Akihiko Odaki

#7 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 260 lines · Jason Wang
Quoted 239 lines · Akihiko Odaki
Quoted 237 lines · Jason Wang
Quoted 227 lines · Akihiko Odaki
Quoted 198 lines · Jason Wang
Quoted 197 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 +179 -0
index 02a9f4dc594d..426f33b4b824 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;
+
Quoted 8 lines

include/linux/virtio_net.h line 136

Quoted 3 lines
include/linux/virtio_net.h +3 -0
+       default:
+               hash->report = VIRTIO_NET_HASH_REPORT_NONE;
+               return;

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

This is on the device side, and don't report HASH_TYPE_XXX_EX.

Quoted 3 lines

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

5.1.6.4.3.4 "IPv6 packets with extension header" says:

Quoted 10 lines

If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
- Home address from the home address option in the IPv6 destination options header. If the extension header is not present, use the Source IPv6 address.
- IPv6 address that is contained in the Routing-Header-Type-2 from the associated extension header. If the extension header is not present, use the Destination IPv6 address.
- Source TCP port
- Destination TCP port

Quoted 6 lines

Therefore, if VIRTIO_NET_HASH_TYPE_TCP_EX is set, the packet has a TCPv6 and an home address option in the IPv6 destination options header is present, the hash is calculated over the home address. If the hash is not calculated over the home address in such a case, the device is contradicting with this section and violating the spec. The same goes for the other HASH_TYPE_XXX_EX types and Routing-Header-Type-2.

Just to make sure we are one the same page. I meant:

Quoted 8 lines
Quoted 5 lines

1) If the hash is not calculated over the home address (in the case of IPv6 destination destination), it can still report VIRTIO_NET_RSS_HASH_TYPE_IPv6. This is what you implemented in your series. So the device can simply fallback to e.g TCPv6 if it can't understand all or part of the IPv6 options.

The spec says it can fallback if "the extension header is not present", not if the device can't understand the extension header.

I don't think so,

1) spec had a condition beforehand:

"""
If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
...
If the extension header is not present ...
"""

Quoted 8 lines

So the device can choose not to set VIRTIO_NET_HASH_TYPE_TCP_EX as spec doesn't say device MUST set VIRTIO_NET_HASH_TYPE_TCP_EX if ...

2) implementation wise, since device has limited resources, we can't expect the device can parse arbitrary number of ipv6 options

3) if 1) and 2) not the case, we need fix the spec otherwise implement a spec compliant device is impractical

The statement is preceded by the following:

Quoted 2 lines

The device calculates the hash on IPv4 packets according to
’Enabled hash types’ bitmask as follows:

The 'Enabled hash types' bitmask is specified by the device.

I think the spec needs amendment.

I wonder if there are any people interested in the feature though. Looking at virtnet_set_hashflow() in drivers/net/virtio_net.c, the driver of Linux does not let users configure HASH_TYPE_XXX_EX. I suppose Windows supports HASH_TYPE_XXX_EX, but those who care network performance most would use Linux so HASH_TYPE_XXX_EX support without Linux driver's support may not be useful.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

2) the VIRTIO_NET_SUPPORTED_HASH_TYPES is not checked against the tun_vnet_ioctl_sethash(), so userspace may set VIRTIO_NET_HASH_TYPE_TCP_EX regardless of what has been returned by tun_vnet_ioctl_gethashtypes(). In this case they won't get VIRTIO_NET_HASH_TYPE_TCP_EX.

Quoted 6 lines
Quoted 2 lines

That's right. It's the responsibility of the userspace to set only the supported hash types.

Well, the kernel should filter out the unsupported one to have a robust uAPI. Otherwise, we give green light to the buggy userspace which will have unexpected results.

My reasoning was that it may be fine for some use cases other than VM (e.g., DPDK); in such a use case, it is fine as long as the UAPI works in the best-effort basis.

For example, suppose a userspace program that processes TCP packets; the program can enable: HASH_TYPE_IPv4, HASH_TYPE_TCPv4, HASH_TYPE_IPv6, and HASH_TYPE_TCPv6. Ideally, the kernel should support all the hash types, but, even if e.g., HASH_TYPE_TCPv6 is not available, it will fall back to HASH_TYPE_IPv6, which still does something good and may be acceptable.

That said, for a use case that involves VM and implements virtio-net (e.g., QEMU), setting an unsupported hash type here is definitely a bug. Catching the bug may outweigh the extra trouble for other use cases.

Quoted 23 lines
Quoted 9 lines
Quoted 8 lines

3) implementing part of the hash types might complicate the migration or at least we need to describe the expectations of libvirt or other management in this case. For example, do we plan to have a dedicated Qemu command line like:

-device virtio-net-pci,hash_report=on,supported_hash_types=X,Y,Z?

Quoted 8 lines

I posted a patch series to implement such a command line for vDPA[1]. The patch series that wires this tuntap feature up[2] reuses the infrastructure so it doesn't bring additional complexity.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/
[2]
https://lore.kernel.org/qemu-devel/[email protected]/

I meant, if we implement a full hash report feature, it means a single hash cmdline option is more than sufficient and so compatibility code can just turn it off when dealing with machine types. This is much more simpler than

Quoted 7 lines

1) having both hash as well as supported_hash_features
2) dealing both hash as well as supported_hash_features in compatibility codes
3) libvirt will be happy

For [1], it seems it introduces a per has type option, this seems to be a burden to the management layer as it need to learn new option everytime a new hash type is supported

Even with the command line you proposed (supported_hash_types=X,Y,Z), it is still necessary to know the values the supported_hash_types property accepts (X.Y,Z), so I don't think it makes difference.

The burden to the management layer is already present for features, so it is an existing problem (or its mere extension).

This problem was discussed in the following thread in the past, but no solution is implemented yet, and probably solving it will be difficult.
https://lore.kernel.org/qemu-devel/[email protected]/

Regards,
Akihiko Odaki

#8 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 271 lines · Akihiko Odaki
Quoted 260 lines · Jason Wang
Quoted 239 lines · Akihiko Odaki
Quoted 237 lines · Jason Wang
Quoted 227 lines · Akihiko Odaki
Quoted 198 lines · Jason Wang
Quoted 197 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 +179 -0
index 02a9f4dc594d..426f33b4b824 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;
+
Quoted 8 lines

include/linux/virtio_net.h line 193

Quoted 3 lines
include/linux/virtio_net.h +3 -0
+       default:
+               hash->report = VIRTIO_NET_HASH_REPORT_NONE;
+               return;

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

This is on the device side, and don't report HASH_TYPE_XXX_EX.

Quoted 3 lines

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

5.1.6.4.3.4 "IPv6 packets with extension header" says:

Quoted 10 lines

If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
- Home address from the home address option in the IPv6 destination options header. If the extension header is not present, use the Source IPv6 address.
- IPv6 address that is contained in the Routing-Header-Type-2 from the associated extension header. If the extension header is not present, use the Destination IPv6 address.
- Source TCP port
- Destination TCP port

Quoted 6 lines

Therefore, if VIRTIO_NET_HASH_TYPE_TCP_EX is set, the packet has a TCPv6 and an home address option in the IPv6 destination options header is present, the hash is calculated over the home address. If the hash is not calculated over the home address in such a case, the device is contradicting with this section and violating the spec. The same goes for the other HASH_TYPE_XXX_EX types and Routing-Header-Type-2.

Just to make sure we are one the same page. I meant:

Quoted 8 lines
Quoted 5 lines

1) If the hash is not calculated over the home address (in the case of IPv6 destination destination), it can still report VIRTIO_NET_RSS_HASH_TYPE_IPv6. This is what you implemented in your series. So the device can simply fallback to e.g TCPv6 if it can't understand all or part of the IPv6 options.

The spec says it can fallback if "the extension header is not present", not if the device can't understand the extension header.

I don't think so,

1) spec had a condition beforehand:

"""
If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
...
If the extension header is not present ...
"""

Quoted 8 lines

So the device can choose not to set VIRTIO_NET_HASH_TYPE_TCP_EX as spec doesn't say device MUST set VIRTIO_NET_HASH_TYPE_TCP_EX if ...

2) implementation wise, since device has limited resources, we can't expect the device can parse arbitrary number of ipv6 options

3) if 1) and 2) not the case, we need fix the spec otherwise implement a spec compliant device is impractical

Quoted 7 lines

The statement is preceded by the following:

Quoted 2 lines

The device calculates the hash on IPv4 packets according to
’Enabled hash types’ bitmask as follows:

The 'Enabled hash types' bitmask is specified by the device.

I think the spec needs amendment.

Michael, can you help to clarify here?

Quoted 7 lines

I wonder if there are any people interested in the feature though. Looking at virtnet_set_hashflow() in drivers/net/virtio_net.c, the driver of Linux does not let users configure HASH_TYPE_XXX_EX. I suppose Windows supports HASH_TYPE_XXX_EX, but those who care network performance most would use Linux so HASH_TYPE_XXX_EX support without Linux driver's support may not be useful.

It might be still interesting for example for the hardware virtio vendors to support windows etc.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

2) the VIRTIO_NET_SUPPORTED_HASH_TYPES is not checked against the tun_vnet_ioctl_sethash(), so userspace may set VIRTIO_NET_HASH_TYPE_TCP_EX regardless of what has been returned by tun_vnet_ioctl_gethashtypes(). In this case they won't get VIRTIO_NET_HASH_TYPE_TCP_EX.

That's right. It's the responsibility of the userspace to set only the supported hash types.

Quoted 7 lines
Quoted 3 lines

Well, the kernel should filter out the unsupported one to have a robust uAPI. Otherwise, we give green light to the buggy userspace which will have unexpected results.

My reasoning was that it may be fine for some use cases other than VM (e.g., DPDK); in such a use case, it is fine as long as the UAPI works in the best-effort basis.

Best-effort might increase the chance for user visisable changes after migration.

Quoted 5 lines

For example, suppose a userspace program that processes TCP packets; the program can enable: HASH_TYPE_IPv4, HASH_TYPE_TCPv4, HASH_TYPE_IPv6, and HASH_TYPE_TCPv6. Ideally, the kernel should support all the hash types, but, even if e.g., HASH_TYPE_TCPv6 is not available,

For "available" did you mean it is not supported by the device?

Quoted 2 lines

it will fall back
to HASH_TYPE_IPv6, which still does something good and may be acceptable.

This fallback is exactly the same as I said above, let VIRTIO_NET_HASH_TYPE_TCP_EX to fallback.

My point is that, the implementation should either:

1) allow fallback so it can claim to support all hash types

or

2) don't allow fallback so it can only support a part of the hash types

If we're doing something in the middle, for example, allow part of the type to fallback.

Quoted 32 lines

That said, for a use case that involves VM and implements virtio-net (e.g., QEMU), setting an unsupported hash type here is definitely a bug. Catching the bug may outweigh the extra trouble for other use cases.

Quoted 27 lines
Quoted 9 lines
Quoted 8 lines

3) implementing part of the hash types might complicate the migration or at least we need to describe the expectations of libvirt or other management in this case. For example, do we plan to have a dedicated Qemu command line like:

-device virtio-net-pci,hash_report=on,supported_hash_types=X,Y,Z?

Quoted 8 lines

I posted a patch series to implement such a command line for vDPA[1]. The patch series that wires this tuntap feature up[2] reuses the infrastructure so it doesn't bring additional complexity.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/
[2]
https://lore.kernel.org/qemu-devel/[email protected]/

I meant, if we implement a full hash report feature, it means a single hash cmdline option is more than sufficient and so compatibility code can just turn it off when dealing with machine types. This is much more simpler than

1) having both hash as well as supported_hash_features
2) dealing both hash as well as supported_hash_features in compatibility codes
3) libvirt will be happy

Quoted 7 lines
Quoted 3 lines

For [1], it seems it introduces a per has type option, this seems to be a burden to the management layer as it need to learn new option everytime a new hash type is supported

Even with the command line you proposed (supported_hash_types=X,Y,Z), it is still necessary to know the values the supported_hash_types property accepts (X.Y,Z), so I don't think it makes difference.

It could be a uint32_t.

Quoted 3 lines

The burden to the management layer is already present for features, so it is an existing problem (or its mere extension).

Yes, but since this feature is new it's better to try our best to avoid that.

Quoted 4 lines

This problem was discussed in the following thread in the past, but no solution is implemented yet, and probably solving it will be difficult.
https://lore.kernel.org/qemu-devel/[email protected]/

It's a similar issue but not the same, it looks more like a discussion on whether the fallback from vhost-net to qemu works for missing features etc.

Thanks

Quoted 4 lines

Regards,
Akihiko Odaki

#9 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 290 lines · Jason Wang
Quoted 271 lines · Akihiko Odaki
Quoted 260 lines · Jason Wang
Quoted 239 lines · Akihiko Odaki
Quoted 237 lines · Jason Wang
Quoted 227 lines · Akihiko Odaki
Quoted 198 lines · Jason Wang
Quoted 197 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 +179 -0
index 02a9f4dc594d..426f33b4b824 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;
+
Quoted 8 lines

include/linux/virtio_net.h line 193

Quoted 3 lines
include/linux/virtio_net.h +3 -0
+       default:
+               hash->report = VIRTIO_NET_HASH_REPORT_NONE;
+               return;

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

This is on the device side, and don't report HASH_TYPE_XXX_EX.

Quoted 3 lines

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

5.1.6.4.3.4 "IPv6 packets with extension header" says:

Quoted 10 lines

If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
- Home address from the home address option in the IPv6 destination options header. If the extension header is not present, use the Source IPv6 address.
- IPv6 address that is contained in the Routing-Header-Type-2 from the associated extension header. If the extension header is not present, use the Destination IPv6 address.
- Source TCP port
- Destination TCP port

Quoted 6 lines

Therefore, if VIRTIO_NET_HASH_TYPE_TCP_EX is set, the packet has a TCPv6 and an home address option in the IPv6 destination options header is present, the hash is calculated over the home address. If the hash is not calculated over the home address in such a case, the device is contradicting with this section and violating the spec. The same goes for the other HASH_TYPE_XXX_EX types and Routing-Header-Type-2.

Just to make sure we are one the same page. I meant:

Quoted 8 lines
Quoted 5 lines

1) If the hash is not calculated over the home address (in the case of IPv6 destination destination), it can still report VIRTIO_NET_RSS_HASH_TYPE_IPv6. This is what you implemented in your series. So the device can simply fallback to e.g TCPv6 if it can't understand all or part of the IPv6 options.

The spec says it can fallback if "the extension header is not present", not if the device can't understand the extension header.

I don't think so,

1) spec had a condition beforehand:

"""
If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
...
If the extension header is not present ...
"""

Quoted 8 lines

So the device can choose not to set VIRTIO_NET_HASH_TYPE_TCP_EX as spec doesn't say device MUST set VIRTIO_NET_HASH_TYPE_TCP_EX if ...

2) implementation wise, since device has limited resources, we can't expect the device can parse arbitrary number of ipv6 options

3) if 1) and 2) not the case, we need fix the spec otherwise implement a spec compliant device is impractical

Quoted 7 lines

The statement is preceded by the following:

Quoted 2 lines

The device calculates the hash on IPv4 packets according to
’Enabled hash types’ bitmask as follows:

The 'Enabled hash types' bitmask is specified by the device.

I think the spec needs amendment.

Michael, can you help to clarify here?

Quoted 7 lines

I wonder if there are any people interested in the feature though. Looking at virtnet_set_hashflow() in drivers/net/virtio_net.c, the driver of Linux does not let users configure HASH_TYPE_XXX_EX. I suppose Windows supports HASH_TYPE_XXX_EX, but those who care network performance most would use Linux so HASH_TYPE_XXX_EX support without Linux driver's support may not be useful.

Quoted 2 lines

It might be still interesting for example for the hardware virtio vendors to support windows etc.

I don't know if Windows needs them for e.g., device/driver certification so surveying Windows makes sense.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 4 lines
Quoted 8 lines
Quoted 5 lines

2) the VIRTIO_NET_SUPPORTED_HASH_TYPES is not checked against the tun_vnet_ioctl_sethash(), so userspace may set VIRTIO_NET_HASH_TYPE_TCP_EX regardless of what has been returned by tun_vnet_ioctl_gethashtypes(). In this case they won't get VIRTIO_NET_HASH_TYPE_TCP_EX.

That's right. It's the responsibility of the userspace to set only the supported hash types.

Well, the kernel should filter out the unsupported one to have a robust uAPI. Otherwise, we give green light to the buggy userspace which will have unexpected results.

Quoted 6 lines
Quoted 3 lines

My reasoning was that it may be fine for some use cases other than VM (e.g., DPDK); in such a use case, it is fine as long as the UAPI works in the best-effort basis.

Best-effort might increase the chance for user visisable changes after migration.

It is a trade-off between catching a migration bug for VMM and making life a bit easier for userspace programs other than VMM.

Quoted 17 lines
Quoted 6 lines

For example, suppose a userspace program that processes TCP packets; the program can enable: HASH_TYPE_IPv4, HASH_TYPE_TCPv4, HASH_TYPE_IPv6, and HASH_TYPE_TCPv6. Ideally, the kernel should support all the hash types, but, even if e.g., HASH_TYPE_TCPv6 is not available,

For "available" did you mean it is not supported by the device?

Quoted 2 lines

it will fall back
to HASH_TYPE_IPv6, which still does something good and may be acceptable.

This fallback is exactly the same as I said above, let VIRTIO_NET_HASH_TYPE_TCP_EX to fallback.

My point is that, the implementation should either:

Quoted 8 lines

1) allow fallback so it can claim to support all hash types

or

2) don't allow fallback so it can only support a part of the hash types

If we're doing something in the middle, for example, allow part of the type to fallback.

1) or the middle will make it unsuitable for VM because it violates the virtio spec. 2) makes sense though the trade-off I mentioned should be taken into consideration.

Quoted 37 lines
Quoted 37 lines

That said, for a use case that involves VM and implements virtio-net (e.g., QEMU), setting an unsupported hash type here is definitely a bug. Catching the bug may outweigh the extra trouble for other use cases.

Quoted 30 lines
Quoted 9 lines
Quoted 8 lines

3) implementing part of the hash types might complicate the migration or at least we need to describe the expectations of libvirt or other management in this case. For example, do we plan to have a dedicated Qemu command line like:

-device virtio-net-pci,hash_report=on,supported_hash_types=X,Y,Z?

Quoted 8 lines

I posted a patch series to implement such a command line for vDPA[1]. The patch series that wires this tuntap feature up[2] reuses the infrastructure so it doesn't bring additional complexity.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/
[2]
https://lore.kernel.org/qemu-devel/[email protected]/

I meant, if we implement a full hash report feature, it means a single hash cmdline option is more than sufficient and so compatibility code can just turn it off when dealing with machine types. This is much more simpler than

1) having both hash as well as supported_hash_features
2) dealing both hash as well as supported_hash_features in compatibility codes
3) libvirt will be happy

For [1], it seems it introduces a per has type option, this seems to be a burden to the management layer as it need to learn new option everytime a new hash type is supported

Quoted 5 lines
Quoted 3 lines

Even with the command line you proposed (supported_hash_types=X,Y,Z), it is still necessary to know the values the supported_hash_types property accepts (X.Y,Z), so I don't think it makes difference.

It could be a uint32_t.

The management layer will need to know what bits are accepted even with uint32_t.

Quoted 8 lines
Quoted 4 lines

The burden to the management layer is already present for features, so it is an existing problem (or its mere extension).

Yes, but since this feature is new it's better to try our best to avoid that.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

This problem was discussed in the following thread in the past, but no solution is implemented yet, and probably solving it will be difficult.
https://lore.kernel.org/qemu-devel/[email protected]/

It's a similar issue but not the same, it looks more like a discussion on whether the fallback from vhost-net to qemu works for missing features etc.

Perhaps we may be able to do better since this feature is new as you say and we don't have to worry much about breaking change. I don't have an idea for that yet.

Regards,
Akihiko Odaki

#10 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 292 lines · Akihiko Odaki
Quoted 290 lines · Jason Wang
Quoted 271 lines · Akihiko Odaki
Quoted 260 lines · Jason Wang
Quoted 239 lines · Akihiko Odaki
Quoted 237 lines · Jason Wang
Quoted 227 lines · Akihiko Odaki
Quoted 198 lines · Jason Wang
Quoted 197 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 +179 -0
index 02a9f4dc594d..426f33b4b824 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;
+
Quoted 8 lines

include/linux/virtio_net.h line 193

Quoted 3 lines
include/linux/virtio_net.h +3 -0
+       default:
+               hash->report = VIRTIO_NET_HASH_REPORT_NONE;
+               return;

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

This is on the device side, and don't report HASH_TYPE_XXX_EX.

Quoted 3 lines

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

5.1.6.4.3.4 "IPv6 packets with extension header" says:

Quoted 10 lines

If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
- Home address from the home address option in the IPv6 destination options header. If the extension header is not present, use the Source IPv6 address.
- IPv6 address that is contained in the Routing-Header-Type-2 from the associated extension header. If the extension header is not present, use the Destination IPv6 address.
- Source TCP port
- Destination TCP port

Quoted 6 lines

Therefore, if VIRTIO_NET_HASH_TYPE_TCP_EX is set, the packet has a TCPv6 and an home address option in the IPv6 destination options header is present, the hash is calculated over the home address. If the hash is not calculated over the home address in such a case, the device is contradicting with this section and violating the spec. The same goes for the other HASH_TYPE_XXX_EX types and Routing-Header-Type-2.

Just to make sure we are one the same page. I meant:

Quoted 8 lines
Quoted 5 lines

1) If the hash is not calculated over the home address (in the case of IPv6 destination destination), it can still report VIRTIO_NET_RSS_HASH_TYPE_IPv6. This is what you implemented in your series. So the device can simply fallback to e.g TCPv6 if it can't understand all or part of the IPv6 options.

The spec says it can fallback if "the extension header is not present", not if the device can't understand the extension header.

I don't think so,

1) spec had a condition beforehand:

"""
If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
...
If the extension header is not present ...
"""

Quoted 8 lines

So the device can choose not to set VIRTIO_NET_HASH_TYPE_TCP_EX as spec doesn't say device MUST set VIRTIO_NET_HASH_TYPE_TCP_EX if ...

2) implementation wise, since device has limited resources, we can't expect the device can parse arbitrary number of ipv6 options

3) if 1) and 2) not the case, we need fix the spec otherwise implement a spec compliant device is impractical

Quoted 7 lines

The statement is preceded by the following:

Quoted 2 lines

The device calculates the hash on IPv4 packets according to
’Enabled hash types’ bitmask as follows:

The 'Enabled hash types' bitmask is specified by the device.

I think the spec needs amendment.

Michael, can you help to clarify here?

Quoted 7 lines

I wonder if there are any people interested in the feature though. Looking at virtnet_set_hashflow() in drivers/net/virtio_net.c, the driver of Linux does not let users configure HASH_TYPE_XXX_EX. I suppose Windows supports HASH_TYPE_XXX_EX, but those who care network performance most would use Linux so HASH_TYPE_XXX_EX support without Linux driver's support may not be useful.

Quoted 5 lines
Quoted 2 lines

It might be still interesting for example for the hardware virtio vendors to support windows etc.

I don't know if Windows needs them for e.g., device/driver certification so surveying Windows makes sense.

Yuri, can you help to clarify this?

Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

2) the VIRTIO_NET_SUPPORTED_HASH_TYPES is not checked against the tun_vnet_ioctl_sethash(), so userspace may set VIRTIO_NET_HASH_TYPE_TCP_EX regardless of what has been returned by tun_vnet_ioctl_gethashtypes(). In this case they won't get VIRTIO_NET_HASH_TYPE_TCP_EX.

Quoted 6 lines
Quoted 2 lines

That's right. It's the responsibility of the userspace to set only the supported hash types.

Well, the kernel should filter out the unsupported one to have a robust uAPI. Otherwise, we give green light to the buggy userspace which will have unexpected results.

My reasoning was that it may be fine for some use cases other than VM (e.g., DPDK); in such a use case, it is fine as long as the UAPI works in the best-effort basis.

Quoted 5 lines
Quoted 2 lines

Best-effort might increase the chance for user visisable changes after migration.

It is a trade-off between catching a migration bug for VMM and making life a bit easier for userspace programs other than VMM.

My understanding is to avoid breaking the migration compatibility as much as possible as fixing that would be complicated or even impossible.

Quoted 68 lines
Quoted 18 lines
Quoted 7 lines

For example, suppose a userspace program that processes TCP packets; the program can enable: HASH_TYPE_IPv4, HASH_TYPE_TCPv4, HASH_TYPE_IPv6, and HASH_TYPE_TCPv6. Ideally, the kernel should support all the hash types, but, even if e.g., HASH_TYPE_TCPv6 is not available,

For "available" did you mean it is not supported by the device?

Quoted 2 lines

it will fall back
to HASH_TYPE_IPv6, which still does something good and may be acceptable.

This fallback is exactly the same as I said above, let VIRTIO_NET_HASH_TYPE_TCP_EX to fallback.

My point is that, the implementation should either:

Quoted 8 lines

1) allow fallback so it can claim to support all hash types

or

2) don't allow fallback so it can only support a part of the hash types

If we're doing something in the middle, for example, allow part of the type to fallback.

1) or the middle will make it unsuitable for VM because it violates the virtio spec. 2) makes sense though the trade-off I mentioned should be taken into consideration.

Quoted 37 lines
Quoted 37 lines

That said, for a use case that involves VM and implements virtio-net (e.g., QEMU), setting an unsupported hash type here is definitely a bug. Catching the bug may outweigh the extra trouble for other use cases.

Quoted 30 lines
Quoted 9 lines
Quoted 8 lines

3) implementing part of the hash types might complicate the migration or at least we need to describe the expectations of libvirt or other management in this case. For example, do we plan to have a dedicated Qemu command line like:

-device virtio-net-pci,hash_report=on,supported_hash_types=X,Y,Z?

Quoted 8 lines

I posted a patch series to implement such a command line for vDPA[1]. The patch series that wires this tuntap feature up[2] reuses the infrastructure so it doesn't bring additional complexity.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/
[2]
https://lore.kernel.org/qemu-devel/[email protected]/

I meant, if we implement a full hash report feature, it means a single hash cmdline option is more than sufficient and so compatibility code can just turn it off when dealing with machine types. This is much more simpler than

1) having both hash as well as supported_hash_features
2) dealing both hash as well as supported_hash_features in compatibility codes
3) libvirt will be happy

For [1], it seems it introduces a per has type option, this seems to be a burden to the management layer as it need to learn new option everytime a new hash type is supported

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Even with the command line you proposed (supported_hash_types=X,Y,Z), it is still necessary to know the values the supported_hash_types property accepts (X.Y,Z), so I don't think it makes difference.

It could be a uint32_t.

The management layer will need to know what bits are accepted even with uint32_t.

Ease the management, basically it would be used by debugging or machine type only.

Quoted 13 lines
Quoted 13 lines
Quoted 5 lines

The burden to the management layer is already present for features, so it is an existing problem (or its mere extension).

Yes, but since this feature is new it's better to try our best to avoid that.

Quoted 4 lines

This problem was discussed in the following thread in the past, but no solution is implemented yet, and probably solving it will be difficult.
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 7 lines
Quoted 3 lines

It's a similar issue but not the same, it looks more like a discussion on whether the fallback from vhost-net to qemu works for missing features etc.

Perhaps we may be able to do better since this feature is new as you say and we don't have to worry much about breaking change. I don't have an idea for that yet.

Right.

Quoted 4 lines

Regards,
Akihiko Odaki

Thanks

#11 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 326 lines · Jason Wang
Quoted 292 lines · Akihiko Odaki
Quoted 290 lines · Jason Wang
Quoted 271 lines · Akihiko Odaki
Quoted 260 lines · Jason Wang
Quoted 239 lines · Akihiko Odaki
Quoted 237 lines · Jason Wang
Quoted 227 lines · Akihiko Odaki
Quoted 198 lines · Jason Wang
Quoted 197 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 +179 -0
index 02a9f4dc594d..426f33b4b824 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;
+
Quoted 8 lines

include/linux/virtio_net.h line 193

Quoted 3 lines
include/linux/virtio_net.h +3 -0
+       default:
+               hash->report = VIRTIO_NET_HASH_REPORT_NONE;
+               return;

So I still think we need a comment here to explain why this is not an issue if the device can report HASH_XXX_EX. Or we need to add the support, since this is the code from the driver side, I don't think we need to worry about the device implementation issues.

This is on the device side, and don't report HASH_TYPE_XXX_EX.

Quoted 3 lines

For the issue of the number of options, does the spec forbid fallback to VIRTIO_NET_HASH_REPORT_NONE? If not, we can do that.

5.1.6.4.3.4 "IPv6 packets with extension header" says:
> If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 > header, the hash is calculated over the following fields:
> - Home address from the home address option in the IPv6 destination
> options header. If the extension header is not present, use the
> Source IPv6 address.
> - IPv6 address that is contained in the Routing-Header-Type-2 from the
> associated extension header. If the extension header is not present,
> use the Destination IPv6 address.
> - Source TCP port
> - Destination TCP port

Quoted 6 lines

Therefore, if VIRTIO_NET_HASH_TYPE_TCP_EX is set, the packet has a TCPv6 and an home address option in the IPv6 destination options header is present, the hash is calculated over the home address. If the hash is not calculated over the home address in such a case, the device is contradicting with this section and violating the spec. The same goes for the other HASH_TYPE_XXX_EX types and Routing-Header-Type-2.

Just to make sure we are one the same page. I meant:

Quoted 8 lines
Quoted 5 lines

1) If the hash is not calculated over the home address (in the case of IPv6 destination destination), it can still report VIRTIO_NET_RSS_HASH_TYPE_IPv6. This is what you implemented in your series. So the device can simply fallback to e.g TCPv6 if it can't understand all or part of the IPv6 options.

The spec says it can fallback if "the extension header is not present", not if the device can't understand the extension header.

I don't think so,

1) spec had a condition beforehand:

"""
If VIRTIO_NET_HASH_TYPE_TCP_EX is set and the packet has a TCPv6 header, the hash is calculated over the following fields:
...
If the extension header is not present ...
"""

Quoted 8 lines

So the device can choose not to set VIRTIO_NET_HASH_TYPE_TCP_EX as spec doesn't say device MUST set VIRTIO_NET_HASH_TYPE_TCP_EX if ...

2) implementation wise, since device has limited resources, we can't expect the device can parse arbitrary number of ipv6 options

3) if 1) and 2) not the case, we need fix the spec otherwise implement a spec compliant device is impractical

Quoted 7 lines

The statement is preceded by the following:

Quoted 2 lines

The device calculates the hash on IPv4 packets according to
’Enabled hash types’ bitmask as follows:

The 'Enabled hash types' bitmask is specified by the device.

I think the spec needs amendment.

Michael, can you help to clarify here?

Quoted 7 lines

I wonder if there are any people interested in the feature though. Looking at virtnet_set_hashflow() in drivers/net/virtio_net.c, the driver of Linux does not let users configure HASH_TYPE_XXX_EX. I suppose Windows supports HASH_TYPE_XXX_EX, but those who care network performance most would use Linux so HASH_TYPE_XXX_EX support without Linux driver's support may not be useful.

Quoted 5 lines
Quoted 2 lines

It might be still interesting for example for the hardware virtio vendors to support windows etc.

I don't know if Windows needs them for e.g., device/driver certification so surveying Windows makes sense.

Yuri, can you help to clarify this?

Quoted 25 lines
Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 10 lines

2) the VIRTIO_NET_SUPPORTED_HASH_TYPES is not checked against the tun_vnet_ioctl_sethash(), so userspace may set VIRTIO_NET_HASH_TYPE_TCP_EX regardless of what has been returned by tun_vnet_ioctl_gethashtypes(). In this case they won't get VIRTIO_NET_HASH_TYPE_TCP_EX.

That's right. It's the responsibility of the userspace to set only the supported hash types.

Quoted 7 lines
Quoted 3 lines

Well, the kernel should filter out the unsupported one to have a robust uAPI. Otherwise, we give green light to the buggy userspace which will have unexpected results.

My reasoning was that it may be fine for some use cases other than VM (e.g., DPDK); in such a use case, it is fine as long as the UAPI works in the best-effort basis.

Best-effort might increase the chance for user visisable changes after migration.

Quoted 6 lines
Quoted 2 lines

It is a trade-off between catching a migration bug for VMM and making life a bit easier for userspace programs other than VMM.

My understanding is to avoid breaking the migration compatibility as much as possible as fixing that would be complicated or even impossible.

That makes a point.

Quoted 73 lines
Quoted 73 lines
Quoted 19 lines
Quoted 8 lines

For example, suppose a userspace program that processes TCP packets; the program can enable: HASH_TYPE_IPv4, HASH_TYPE_TCPv4, HASH_TYPE_IPv6, and HASH_TYPE_TCPv6. Ideally, the kernel should support all the hash types, but, even if e.g., HASH_TYPE_TCPv6 is not available,

For "available" did you mean it is not supported by the device?

Quoted 2 lines

it will fall back
to HASH_TYPE_IPv6, which still does something good and may be acceptable.

This fallback is exactly the same as I said above, let VIRTIO_NET_HASH_TYPE_TCP_EX to fallback.

My point is that, the implementation should either:

Quoted 8 lines

1) allow fallback so it can claim to support all hash types

or

2) don't allow fallback so it can only support a part of the hash types

If we're doing something in the middle, for example, allow part of the type to fallback.

1) or the middle will make it unsuitable for VM because it violates the virtio spec. 2) makes sense though the trade-off I mentioned should be taken into consideration.

Quoted 41 lines
Quoted 40 lines

That said, for a use case that involves VM and implements virtio-net (e.g., QEMU), setting an unsupported hash type here is definitely a bug. Catching the bug may outweigh the extra trouble for other use cases.

Quoted 23 lines
Quoted 9 lines
Quoted 8 lines

3) implementing part of the hash types might complicate the migration or at least we need to describe the expectations of libvirt or other management in this case. For example, do we plan to have a dedicated Qemu command line like:

-device virtio-net-pci,hash_report=on,supported_hash_types=X,Y,Z?

Quoted 8 lines

I posted a patch series to implement such a command line for vDPA[1]. The patch series that wires this tuntap feature up[2] reuses the infrastructure so it doesn't bring additional complexity.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/
[2]
https://lore.kernel.org/qemu-devel/[email protected]/

I meant, if we implement a full hash report feature, it means a single hash cmdline option is more than sufficient and so compatibility code can just turn it off when dealing with machine types. This is much more simpler than

Quoted 7 lines

1) having both hash as well as supported_hash_features
2) dealing both hash as well as supported_hash_features in compatibility codes
3) libvirt will be happy

For [1], it seems it introduces a per has type option, this seems to be a burden to the management layer as it need to learn new option everytime a new hash type is supported

Even with the command line you proposed (supported_hash_types=X,Y,Z), it is still necessary to know the values the supported_hash_types property accepts (X.Y,Z), so I don't think it makes difference.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

It could be a uint32_t.

The management layer will need to know what bits are accepted even with uint32_t.

Ease the management, basically it would be used by debugging or machine type only.

Users need to be able to specify hash types for the reasons same with virtio features (via domain XML for libvirt). In particular:

1) To ensure migration compatibility in case that hosts have different sets of supported hash types. This kind of situation can happen when vDPA devices have different capabilities or when the spec gains new hash types and a new version of kernel/tuntap starts to support it.

2) For debugging as you mentioned.

It makes sense to let users specify hash types with mnemonics instead of cryptic bitmasks. 2) is not an end-user requirement so it may be tolerable to expose bitmasks, but it is less so for 1). The management layer needs to have a list of hash types to support mnemonics whether QOM properties are uint32_t or not.

We can have an interface different from the one for features without worrying about backward compatibility, but this aspect of hash types is exactly same with features and does not motivate to do so.

Regards,
Akihiko Odaki

Quoted 31 lines
Quoted 14 lines
Quoted 14 lines
Quoted 6 lines

The burden to the management layer is already present for features, so it is an existing problem (or its mere extension).

Yes, but since this feature is new it's better to try our best to avoid that.

Quoted 4 lines

This problem was discussed in the following thread in the past, but no solution is implemented yet, and probably solving it will be difficult.
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 7 lines
Quoted 3 lines

It's a similar issue but not the same, it looks more like a discussion on whether the fallback from vhost-net to qemu works for missing features etc.

Perhaps we may be able to do better since this feature is new as you say and we don't have to worry much about breaking change. I don't have an idea for that yet.

Right.

Quoted 4 lines

Regards,
Akihiko Odaki

Thanks

#12 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

In reply to Jason Wang
Quoted 1 line

Yuri, can you help to clarify this?

I see here several questions:
1. Whether it is ok for the device not to indicate support for XXX_EX hash type?
- I think, yes (strictly speaking, it was better to test that before submitting the patches )
2. Is it possible that the guest will enable some XXX_EX hash type if the device does not indicate that it is supported?
- No (I think this is part of the spec)
3. What to do if we migrate between systems with different capabilities of hash support/reporting/whatever
- IMO, at this moment such case should be excluded and only mechanism we have for that is the compatible machine version
- in some future the change of device capabilities can be communicated to the driver and _probably_ the driver might be able to communicate the change of device capabilities to the OS
4. Does it make sense to have fine configuration of hash types mask via command-line?
- IMO, no. This would require the user to have too much knowledge about RSS internals

Please let me know if I missed something.

#13 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 10 lines · Yuri Benditovich
Quoted 2 lines

Yuri, can you help to clarify this?

I see here several questions:
1. Whether it is ok for the device not to indicate support for XXX_EX hash type?
- I think, yes (strictly speaking, it was better to test that before submitting the patches )
2. Is it possible that the guest will enable some XXX_EX hash type if the device does not indicate that it is supported?
- No (I think this is part of the spec)

There's another question, is the device allowed to fallback to VIRTIO_NET_HASH_TYPE_IPv6 if it fails to parse extensions?

Quoted 7 lines

3. What to do if we migrate between systems with different capabilities of hash support/reporting/whatever
- IMO, at this moment such case should be excluded and only mechanism we have for that is the compatible machine version
- in some future the change of device capabilities can be communicated to the driver and _probably_ the driver might be able to communicate the change of device capabilities to the OS

Are you suggesting implementing all hash types? Note that Akihiko raises the issue that in the actual implementation there should be a limitation of the maximum number of options. If such a limitation is different between src and dst, the difference could be noticed by the guest.

Quoted 7 lines

4. Does it make sense to have fine configuration of hash types mask via command-line?
- IMO, no. This would require the user to have too much knowledge about RSS internals

Please let me know if I missed something.

Thanks

#14 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 14 lines · Jason Wang
Quoted 10 lines · Yuri Benditovich
Quoted 2 lines

Yuri, can you help to clarify this?

I see here several questions:
1. Whether it is ok for the device not to indicate support for XXX_EX hash type?
- I think, yes (strictly speaking, it was better to test that before submitting the patches )
2. Is it possible that the guest will enable some XXX_EX hash type if the device does not indicate that it is supported?
- No (I think this is part of the spec)

Quoted 2 lines

There's another question, is the device allowed to fallback to VIRTIO_NET_HASH_TYPE_IPv6 if it fails to parse extensions?

MSFT expectations for that are at
https://learn.microsoft.com/en-us/windows-hardware/drivers/network/rss-hashing-types
If I read them correctly, the answer is "no"
BTW, my personal opinion is that placing all these things with hash calculations into kernel instead of ebpf does not make too much sense.

Quoted 25 lines
Quoted 8 lines

3. What to do if we migrate between systems with different capabilities of hash support/reporting/whatever
- IMO, at this moment such case should be excluded and only mechanism we have for that is the compatible machine version
- in some future the change of device capabilities can be communicated to the driver and _probably_ the driver might be able to communicate the change of device capabilities to the OS

Are you suggesting implementing all hash types? Note that Akihiko raises the issue that in the actual implementation there should be a limitation of the maximum number of options. If such a limitation is different between src and dst, the difference could be noticed by the guest.

Quoted 7 lines

4. Does it make sense to have fine configuration of hash types mask via command-line?
- IMO, no. This would require the user to have too much knowledge about RSS internals

Please let me know if I missed something.

Thanks

#15 lore raw

Re: [PATCH net-next v12 01/10] virtio_net: Add functions for hashing

Quoted 16 lines · Yuri Benditovich
Quoted 14 lines · Jason Wang
Quoted 10 lines · Yuri Benditovich
Quoted 2 lines

Yuri, can you help to clarify this?

I see here several questions:
1. Whether it is ok for the device not to indicate support for XXX_EX hash type?
- I think, yes (strictly speaking, it was better to test that before submitting the patches )
2. Is it possible that the guest will enable some XXX_EX hash type if the device does not indicate that it is supported?
- No (I think this is part of the spec)

Quoted 5 lines
Quoted 2 lines

There's another question, is the device allowed to fallback to VIRTIO_NET_HASH_TYPE_IPv6 if it fails to parse extensions?

MSFT expectations for that are at
https://learn.microsoft.com/en-us/windows-hardware/drivers/network/rss-hashing-types
If I read them correctly, the answer is "no"

Ok, so I guess it implies the implementation should be ready to deal with arbitrary length of ipv6 options.

Quoted 2 lines

BTW, my personal opinion is that placing all these things with hash calculations into kernel instead of ebpf does not make too much sense.

If I remember correctly, we tried to enable it via eBPF, but failed due to the rejection of eBPF maintainers.

Maybe we can revisit the idea. But anyhow the hardcoded logic might still be useful as eBPF is not guaranteed to work in all cases.

Thanks

Quoted 27 lines
Quoted 26 lines
Quoted 9 lines

3. What to do if we migrate between systems with different capabilities of hash support/reporting/whatever
- IMO, at this moment such case should be excluded and only mechanism we have for that is the compatible machine version
- in some future the change of device capabilities can be communicated to the driver and _probably_ the driver might be able to communicate the change of device capabilities to the OS

Are you suggesting implementing all hash types? Note that Akihiko raises the issue that in the actual implementation there should be a limitation of the maximum number of options. If such a limitation is different between src and dst, the difference could be noticed by the guest.

Quoted 7 lines

4. Does it make sense to have fine configuration of hash types mask via command-line?
- IMO, no. This would require the user to have too much knowledge about RSS internals

Please let me know if I missed something.

Thanks

#16 lore raw

PATCH v12 2/10 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 ced79dc8e856..d01c1ec77b7d 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 9cd8de6bebb5..32c7ee31330c 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.49.0
#18 lore raw

PATCH v12 3/10 tun: Allow steering eBPF program to fall back

In reply to Akihiko Odaki

This clarifies a steering eBPF program takes precedence over the other steering algorithms.

---
 Documentation/networking/tuntap.rst |  7 +++++++
 drivers/net/tun.c                   | 28 +++++++++++++++++-----------
 include/uapi/linux/if_tun.h         |  9 +++++++++
 3 files changed, 33 insertions(+), 11 deletions(-)
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be..86b4ae8caa8a 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/tun.c +17 -11
index d8f4d3e996a7..9133ab9ed3f5 100644
@@ -476,21 +476,29 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
 	return txq;
 }
 
-static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static bool tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb,
+				  u16 *ret)
 {
 	struct tun_prog *prog;
 	u32 numqueues;
-	u16 ret = 0;
+	u32 prog_ret;
+
+	prog = rcu_dereference(tun->steering_prog);
+	if (!prog)
+		return false;
 
 	numqueues = READ_ONCE(tun->numqueues);
-	if (!numqueues)
-		return 0;
+	if (!numqueues) {
+		*ret = 0;
+		return true;
+	}
 
-	prog = rcu_dereference(tun->steering_prog);
-	if (prog)
-		ret = bpf_prog_run_clear_cb(prog->prog, skb);
+	prog_ret = bpf_prog_run_clear_cb(prog->prog, skb);
+	if (prog_ret == TUN_STEERINGEBPF_FALLBACK)
+		return false;
 
-	return ret % numqueues;
+	*ret = (u16)prog_ret % numqueues;
+	return true;
 }
 
 static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
@@ -500,9 +508,7 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
 	u16 ret;
 
 	rcu_read_lock();
-	if (rcu_dereference(tun->steering_prog))
-		ret = tun_ebpf_select_queue(tun, skb);
-	else
+	if (!tun_ebpf_select_queue(tun, skb, &ret))
 		ret = tun_automq_select_queue(tun, skb);
 	rcu_read_unlock();
 
include/uapi/linux/if_tun.h +9 -0
index 287cdc81c939..980de74724fc 100644
@@ -115,4 +115,13 @@ struct tun_filter {
 	__u8   addr[][ETH_ALEN];
 };
 
+/**
+ * define TUN_STEERINGEBPF_FALLBACK - A steering eBPF return value to fall back
+ *
+ * A steering eBPF program may return this value to fall back to the steering
+ * algorithm that should have been used if the program was not set. This allows
+ * selectively overriding the steering decision.
+ */
+#define TUN_STEERINGEBPF_FALLBACK -1
+
 #endif /* _UAPI__IF_TUN_H */
Signature · 1 line
2.49.0
#19 lore raw

Re: [PATCH net-next v12 03/10] tun: Allow steering eBPF program to fall back

Quoted 3 lines · Akihiko Odaki

This clarifies a steering eBPF program takes precedence over the other steering algorithms.

Let's give an example on the use case for this.

Quoted 51 lines
---
 Documentation/networking/tuntap.rst |  7 +++++++
 drivers/net/tun.c                   | 28 +++++++++++++++++-----------
 include/uapi/linux/if_tun.h         |  9 +++++++++
 3 files changed, 33 insertions(+), 11 deletions(-)
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be..86b4ae8caa8a 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/tun.c +8 -4
index d8f4d3e996a7..9133ab9ed3f5 100644
@@ -476,21 +476,29 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
        return txq;
 }
 
-static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static bool tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb,
+                                 u16 *ret)
 {
        struct tun_prog *prog;
        u32 numqueues;
-       u16 ret = 0;
+       u32 prog_ret;
+
+       prog = rcu_dereference(tun->steering_prog);
+       if (!prog)
+               return false;
 
        numqueues = READ_ONCE(tun->numqueues);
-       if (!numqueues)
-               return 0;
+       if (!numqueues) {

drivers/net/tun.c static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) · line 498

Quoted 10 lines
drivers/net/tun.c +6 -3
+               *ret = 0;
+               return true;
+       }
 
-       prog = rcu_dereference(tun->steering_prog);
-       if (prog)
-               ret = bpf_prog_run_clear_cb(prog->prog, skb);
+       prog_ret = bpf_prog_run_clear_cb(prog->prog, skb);
+       if (prog_ret == TUN_STEERINGEBPF_FALLBACK)
+               return false;

This seems to break the uAPI. So I think we need a new ioctl to enable the behaviour

Quoted 24 lines
drivers/net/tun.c +3 -4
-       return ret % numqueues;
+       *ret = (u16)prog_ret % numqueues;
+       return true;
 }
 
 static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
@@ -500,9 +508,7 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
        u16 ret;
 
        rcu_read_lock();
-       if (rcu_dereference(tun->steering_prog))
-               ret = tun_ebpf_select_queue(tun, skb);
-       else
+       if (!tun_ebpf_select_queue(tun, skb, &ret))
                ret = tun_automq_select_queue(tun, skb);
        rcu_read_unlock();
 
include/uapi/linux/if_tun.h +0 -0
index 287cdc81c939..980de74724fc 100644
@@ -115,4 +115,13 @@ struct tun_filter {
        __u8   addr[][ETH_ALEN];

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

Quoted 10 lines
include/uapi/linux/if_tun.h +8 -0
 };
 
+/**
+ * define TUN_STEERINGEBPF_FALLBACK - A steering eBPF return value to fall back
+ *
+ * A steering eBPF program may return this value to fall back to the steering
+ * algorithm that should have been used if the program was not set. This allows
+ * selectively overriding the steering decision.
+ */
+#define TUN_STEERINGEBPF_FALLBACK -1

Not a native speaker, consider it works more like XDP_PASS, would it be better to use "TUN_STERRING_PASS"?

Thanks

Quoted 6 lines
include/uapi/linux/if_tun.h +1 -1
+
 #endif /* _UAPI__IF_TUN_H */
 
--

2.49.0

#20 lore raw

Re: [PATCH net-next v12 03/10] tun: Allow steering eBPF program to fall back

Quoted 62 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

This clarifies a steering eBPF program takes precedence over the other steering algorithms.

Let's give an example on the use case for this.

Quoted 55 lines
---
  Documentation/networking/tuntap.rst |  7 +++++++
  drivers/net/tun.c                   | 28 +++++++++++++++++-----------
  include/uapi/linux/if_tun.h         |  9 +++++++++
  3 files changed, 33 insertions(+), 11 deletions(-)
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be..86b4ae8caa8a 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/tun.c +11 -4
index d8f4d3e996a7..9133ab9ed3f5 100644
@@ -476,21 +476,29 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
         return txq;
  }
 
-static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static bool tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb,
+                                 u16 *ret)
  {
         struct tun_prog *prog;
         u32 numqueues;
-       u16 ret = 0;
+       u32 prog_ret;
+
+       prog = rcu_dereference(tun->steering_prog);
+       if (!prog)
+               return false;
 
         numqueues = READ_ONCE(tun->numqueues);
-       if (!numqueues)
-               return 0;
+       if (!numqueues) {
+               *ret = 0;
+               return true;
+       }
 
Quoted 9 lines

drivers/net/tun.c static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) · line 498

Quoted 6 lines
drivers/net/tun.c +3 -3
-       prog = rcu_dereference(tun->steering_prog);
-       if (prog)
-               ret = bpf_prog_run_clear_cb(prog->prog, skb);
+       prog_ret = bpf_prog_run_clear_cb(prog->prog, skb);
+       if (prog_ret == TUN_STEERINGEBPF_FALLBACK)
+               return false;

This seems to break the uAPI. So I think we need a new ioctl to enable the behaviour

I assumed it is fine to repurpose one of the 32-bit integer values since 32-bit integer is too big to specify the queue number, but it may not be fine. I don't have a concrete use case either.

Perhaps it is safer to note that TUNSETSTEERINGEBPF takes precedence over TUNSETVNETRSS to allow such an extension in the future (but without implementing one now).

Quoted 27 lines
Quoted 27 lines
drivers/net/tun.c +3 -4
-       return ret % numqueues;
+       *ret = (u16)prog_ret % numqueues;
+       return true;
  }
 
  static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
@@ -500,9 +508,7 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
         u16 ret;
 
         rcu_read_lock();
-       if (rcu_dereference(tun->steering_prog))
-               ret = tun_ebpf_select_queue(tun, skb);
-       else
+       if (!tun_ebpf_select_queue(tun, skb, &ret))
                 ret = tun_automq_select_queue(tun, skb);
         rcu_read_unlock();
 
include/uapi/linux/if_tun.h +0 -0
index 287cdc81c939..980de74724fc 100644
@@ -115,4 +115,13 @@ struct tun_filter {
         __u8   addr[][ETH_ALEN];
  };
 
Quoted 11 lines

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

Quoted 8 lines
include/uapi/linux/if_tun.h +8 -0
+/**
+ * define TUN_STEERINGEBPF_FALLBACK - A steering eBPF return value to fall back
+ *
+ * A steering eBPF program may return this value to fall back to the steering
+ * algorithm that should have been used if the program was not set. This allows
+ * selectively overriding the steering decision.
+ */
+#define TUN_STEERINGEBPF_FALLBACK -1

Not a native speaker, consider it works more like XDP_PASS, would it be better to use "TUN_STERRING_PASS"?

That sounds indeed better to me.

Regards,
Akihiko Odaki

#21 lore raw

Re: [PATCH net-next v12 03/10] tun: Allow steering eBPF program to fall back

Quoted 74 lines · Akihiko Odaki
Quoted 62 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

This clarifies a steering eBPF program takes precedence over the other steering algorithms.

Let's give an example on the use case for this.

Quoted 55 lines
---
  Documentation/networking/tuntap.rst |  7 +++++++
  drivers/net/tun.c                   | 28 +++++++++++++++++-----------
  include/uapi/linux/if_tun.h         |  9 +++++++++
  3 files changed, 33 insertions(+), 11 deletions(-)
Documentation/networking/tuntap.rst +7 -0
index 4d7087f727be..86b4ae8caa8a 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/tun.c +11 -4
index d8f4d3e996a7..9133ab9ed3f5 100644
@@ -476,21 +476,29 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
         return txq;
  }
 
-static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static bool tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb,
+                                 u16 *ret)
  {
         struct tun_prog *prog;
         u32 numqueues;
-       u16 ret = 0;
+       u32 prog_ret;
+
+       prog = rcu_dereference(tun->steering_prog);
+       if (!prog)
+               return false;
 
         numqueues = READ_ONCE(tun->numqueues);
-       if (!numqueues)
-               return 0;
+       if (!numqueues) {
+               *ret = 0;
+               return true;
+       }
 
Quoted 9 lines

drivers/net/tun.c static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) · line 498

Quoted 6 lines
drivers/net/tun.c +3 -3
-       prog = rcu_dereference(tun->steering_prog);
-       if (prog)
-               ret = bpf_prog_run_clear_cb(prog->prog, skb);
+       prog_ret = bpf_prog_run_clear_cb(prog->prog, skb);
+       if (prog_ret == TUN_STEERINGEBPF_FALLBACK)
+               return false;

This seems to break the uAPI. So I think we need a new ioctl to enable the behaviour

Quoted 7 lines

I assumed it is fine to repurpose one of the 32-bit integer values since 32-bit integer is too big to specify the queue number, but it may not be fine. I don't have a concrete use case either.

Perhaps it is safer to note that TUNSETSTEERINGEBPF takes precedence over TUNSETVNETRSS to allow such an extension in the future (but without implementing one now).

Yes, that's my original point. Let's start from something that is simpler and safer.

Thanks

#22 lore raw

PATCH v12 4/10 tun: Add common virtio-net hash feature code

In reply to Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
 drivers/net/tap.c           |  10 ++-
 drivers/net/tun.c           |  12 +++-
 drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
 include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
 4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
 	sock_put(&q->sk);
 }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+	return NULL;
+}
+
 /*
  * Select a queue based on the rxq of the device on which this packet
  * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
 	int total;
 
 	if (q->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr vnet_hdr;
+		struct virtio_net_hdr_v1_hash vnet_hdr;
 
 		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-		ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+		ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+					    tap_find_hash, &vnet_hdr);
 		if (ret)
 			return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
 		e->rps_rxhash = hash;
 }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+	return NULL;
+}
+
 /* We try to identify a flow through its rxhash. The reason that
  * we do not check rxq no. is because some cards(e.g 82599), chooses
  * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
 	ssize_t ret;
 
 	if (tun->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr gso = { 0 };
+		struct virtio_net_hdr_v1_hash gso = { 0 };
 
 		vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
 		ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
 	}
 
 	if (vnet_hdr_sz) {
-		struct virtio_net_hdr gso;
+		struct virtio_net_hdr_v1_hash gso;
 
-		ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+		ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+					    skb, tun_find_hash, &gso);
 		if (ret)
 			return ret;
 
drivers/net/tun_vnet.h +156 -9

209 lines not on this page · raw message

include/uapi/linux/if_tun.h +71 -0

81 lines not on this page · raw message

Signature · 1 line
2.49.0
#23 lore raw

Re: [PATCH net-next v12 04/10] tun: Add common virtio-net hash feature code

Quoted 181 lines · Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
 drivers/net/tap.c           |  10 ++-
 drivers/net/tun.c           |  12 +++-
 drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
 include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
 4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
        sock_put(&q->sk);
 }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
 /*
  * Select a queue based on the rxq of the device on which this packet
  * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
        int total;
 
        if (q->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr vnet_hdr;
+               struct virtio_net_hdr_v1_hash vnet_hdr;
 
                vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-               ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+                                           tap_find_hash, &vnet_hdr);
                if (ret)
                        return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                e->rps_rxhash = hash;
 }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
 /* We try to identify a flow through its rxhash. The reason that
  * we do not check rxq no. is because some cards(e.g 82599), chooses
  * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
        ssize_t ret;
 
        if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
+               struct virtio_net_hdr_v1_hash gso = { 0 };
 
                vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
                ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
        }
 
        if (vnet_hdr_sz) {
-               struct virtio_net_hdr gso;
+               struct virtio_net_hdr_v1_hash gso;
 
-               ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+                                           skb, tun_find_hash, &gso);
                if (ret)
                        return ret;
 
drivers/net/tun_vnet.h +35 -0
index 58b9ac7a5fc4..45d0533efc8d 100644
@@ -6,6 +6,17 @@
 #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 {
+       bool report;
+       bool rss;
+       struct tun_vnet_rss common;
+       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 +118,128 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
        }
 }
 
+static inline long tun_vnet_ioctl_gethashtypes(u32 __user *argp)
+{
+       return put_user(VIRTIO_NET_SUPPORTED_HASH_TYPES, argp) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash __rcu **hashp,
+                                         unsigned int cmd,
+                                         void __user *argp)
+{
+       struct tun_vnet_rss common;
+       struct tun_vnet_hash *hash;
+       size_t indirection_table_size;
+       size_t key_size;
+       size_t size;
+
+       switch (cmd) {
+       case TUNSETVNETREPORTINGAUTOMQ:
+               if (get_user(common.hash_types, (u32 __user *)argp))
+                       return -EFAULT;
+
+               if (common.hash_types) {
+                       hash = kzalloc(sizeof(*hash), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+
+                       hash->report = true;
+                       hash->common.hash_types = common.hash_types;
+               } else {
+                       hash = NULL;
+               }
+               break;
+
+       case TUNSETVNETREPORTINGRSS:
+       case TUNSETVNETRSS:

So the above three shows unnecessary design redundancy as well as a burden for the future extension. Why not simply have

1) TUNSETVNET_RSS
2) TUNSETVNET_HASH_REPORT
?

Which maps to

#define VIRTIO_NET_CTRL_MQ_RSS_CONFIG 1 (for configurable
receive steering)
#define VIRTIO_NET_CTRL_MQ_HASH_CONFIG 2 (for configurable
hash calculation)

It would be always easier to start with what spec had or at least we need to explain why we choose a different design here or in the changelog to ease our life.

One day we would have tun_select_queue_algorithm_x() we don't have to duplicate the ioctls once again here like TUNSETVNETREPORTINGXYZ

Quoted 17 lines
drivers/net/tun_vnet.h +17 -0
+               if (copy_from_user(&common, argp, sizeof(common)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)common.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(common.hash_types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)common.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;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+               }
+               argp = (u16 __user *)argp + common.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->report = cmd == TUNSETVNETREPORTINGRSS;

At least, if this is the only difference why not simply code this into the ioctl itself other than having a very similar command?

Quoted 156 lines
drivers/net/tun_vnet.h +84 -9
+               hash->rss = true;
+               hash->common = common;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+       return 0;
+}
+
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash *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->report)
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.hash_types, keys),
+               .value = value
+       };
+}
+
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.hash_types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->common.unclassified_queue % numqueues;
+
+       if (hash->report) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->common.indirection_table_mask;
+
+       return hash->rss_indirection_table[index] % numqueues;
+}
+
 static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                   struct iov_iter *from,
                                   struct virtio_net_hdr *hdr)
@@ -135,15 +268,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 +290,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/uapi/linux/if_tun.h +18 -0
index 980de74724fc..fe4b984d3bbb 100644
@@ -62,6 +62,62 @@
 #define TUNSETCARRIER _IOW('T', 226, int)
 #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHTYPES - ioctl to get supported virtio_net hashing types
+ *
+ * The argument is a pointer to __u32 which will store the supported virtio_net
+ * hashing types.
+ */
+#define TUNGETVNETHASHTYPES _IOR('T', 228, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGAUTOMQ - ioctl to enable automq with hash reporting
+ *
+ * Disable RSS and enable automatic receive steering with hash reporting.
+ *
+ * The argument is a pointer to __u32 that contains a bitmask of hash types
+ * allowed to be reported.
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.

include/uapi/linux/if_tun.h line 92

Quoted 10 lines
include/uapi/linux/if_tun.h +10 -0
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.
+ */
+#define TUNSETVNETREPORTINGAUTOMQ _IOR('T', 229, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGRSS - ioctl to enable RSS with hash reporting
+ *
+ * Disable automatic receive steering and enable RSS with hash reporting.

This is unnecessary, e.g one day will have select_queue_xyz(), we don't want to say "Disable automatic receive steering and xyz ..."

include/uapi/linux/if_tun.h line 105

Quoted 13 lines
include/uapi/linux/if_tun.h +13 -0
+ *
+ * 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 TUNSETVNETREPORTINGRSS _IOR('T', 230, struct tun_vnet_rss)
+
+/**
+ * define TUNSETVNETRSS - ioctl to enable RSS without hash reporting
+ *
+ * Disable automatic receive steering and enable RSS without hash reporting.

Same here.

Quoted 40 lines
include/uapi/linux/if_tun.h +30 -1
+ *
+ * The argument is a pointer to the compound of the following in order:
+ *
+ * 1. &struct tun_vnet_rss
+ * 3. Indirection table
+ * 4. Key
+ *
+ * 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 TUNSETVNETRSS _IOR('T', 231, struct tun_vnet_rss)
+
 /* TUNSETIFF ifr flags */
 #define IFF_TUN                0x0001
 #define IFF_TAP                0x0002
@@ -124,4 +180,19 @@ struct tun_filter {
  */
 #define TUN_STEERINGEBPF_FALLBACK -1
 
+/**
+ * struct tun_vnet_rss - virtio_net RSS configuration
+ * @hash_types:
+ *             Bitmask of allowed hash types
+ * @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_rss {
+       __u32 hash_types;
+       __u16 indirection_table_mask;
+       __u16 unclassified_queue;
+};
+
 #endif /* _UAPI__IF_TUN_H */
 
--
Quoted 2 lines

2.49.0

Thanks

#24 lore raw

Re: [PATCH net-next v12 04/10] tun: Add common virtio-net hash feature code

Quoted 202 lines · Jason Wang
Quoted 181 lines · Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
  drivers/net/tap.c           |  10 ++-
  drivers/net/tun.c           |  12 +++-
  drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
  include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
  4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
         sock_put(&q->sk);
  }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
  /*
   * Select a queue based on the rxq of the device on which this packet
   * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
         int total;
 
         if (q->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr vnet_hdr;
+               struct virtio_net_hdr_v1_hash vnet_hdr;
 
                 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-               ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+                                           tap_find_hash, &vnet_hdr);
                 if (ret)
                         return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                 e->rps_rxhash = hash;
  }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
  /* We try to identify a flow through its rxhash. The reason that
   * we do not check rxq no. is because some cards(e.g 82599), chooses
   * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
         ssize_t ret;
 
         if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
+               struct virtio_net_hdr_v1_hash gso = { 0 };
 
                 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
                 ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
         }
 
         if (vnet_hdr_sz) {
-               struct virtio_net_hdr gso;
+               struct virtio_net_hdr_v1_hash gso;
 
-               ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+                                           skb, tun_find_hash, &gso);
                 if (ret)
                         return ret;
 
drivers/net/tun_vnet.h +35 -0
index 58b9ac7a5fc4..45d0533efc8d 100644
@@ -6,6 +6,17 @@
  #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 {
+       bool report;
+       bool rss;
+       struct tun_vnet_rss common;
+       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 +118,128 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
         }
  }
 
+static inline long tun_vnet_ioctl_gethashtypes(u32 __user *argp)
+{
+       return put_user(VIRTIO_NET_SUPPORTED_HASH_TYPES, argp) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash __rcu **hashp,
+                                         unsigned int cmd,
+                                         void __user *argp)
+{
+       struct tun_vnet_rss common;
+       struct tun_vnet_hash *hash;
+       size_t indirection_table_size;
+       size_t key_size;
+       size_t size;
+
+       switch (cmd) {
+       case TUNSETVNETREPORTINGAUTOMQ:
+               if (get_user(common.hash_types, (u32 __user *)argp))
+                       return -EFAULT;
+
+               if (common.hash_types) {
+                       hash = kzalloc(sizeof(*hash), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+
+                       hash->report = true;
+                       hash->common.hash_types = common.hash_types;
+               } else {
+                       hash = NULL;
+               }
+               break;
+
+       case TUNSETVNETREPORTINGRSS:
+       case TUNSETVNETRSS:

So the above three shows unnecessary design redundancy as well as a burden for the future extension. Why not simply have

1) TUNSETVNET_RSS
2) TUNSETVNET_HASH_REPORT
?

Which maps to

Quoted 8 lines

#define VIRTIO_NET_CTRL_MQ_RSS_CONFIG 1 (for configurable
receive steering)
#define VIRTIO_NET_CTRL_MQ_HASH_CONFIG 2 (for configurable
hash calculation)

It would be always easier to start with what spec had or at least we need to explain why we choose a different design here or in the changelog to ease our life.

TUNSETVNETREPORTINGAUTOMQ maps to VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

TUNSETVNETREPORTINGRSS and TUNSETVNETRSS map to VIRTIO_NET_CTRL_MQ_RSS_CONFIG. We have two ioctls here because VIRTIO_NET_CTRL_MQ_RSS_CONFIG behaves differently depending on whether VIRTIO_NET_F_HASH_REPORT is negotiated or not; it also enables hash reporting if the feature is negotiated.

Quoted 3 lines

One day we would have tun_select_queue_algorithm_x() we don't have to duplicate the ioctls once again here like TUNSETVNETREPORTINGXYZ

5.1.6.5.6.4 Hash calculation says:

Quoted 7 lines

If VIRTIO_NET_F_HASH_REPORT was negotiated and the device uses
automatic receive steering, the device MUST support a command to configure hash calculation parameters.

The driver provides parameters for hash calculation as follows:

class VIRTIO_NET_CTRL_MQ, command VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

VIRTIO_NET_CTRL_MQ_HASH_CONFIG is for automatic receive steering and not for RSS (or other steering algorithms if the spec gets any in the future).

Quoted 23 lines
Quoted 23 lines
drivers/net/tun_vnet.h +22 -0
+               if (copy_from_user(&common, argp, sizeof(common)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)common.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(common.hash_types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)common.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 + common.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {
+                       kfree(hash);
Quoted 8 lines

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

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->report = cmd == TUNSETVNETREPORTINGRSS;

At least, if this is the only difference why not simply code this into the ioctl itself other than having a very similar command?

It is what the previous version did. Either is fine I guess; the only practical difference would be the size of the configuration struct is smaller with this approach.

Quoted 162 lines
Quoted 162 lines
drivers/net/tun_vnet.h +84 -9
+               hash->rss = true;
+               hash->common = common;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+       return 0;
+}
+
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash *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->report)
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.hash_types, keys),
+               .value = value
+       };
+}
+
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.hash_types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->common.unclassified_queue % numqueues;
+
+       if (hash->report) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->common.indirection_table_mask;
+
+       return hash->rss_indirection_table[index] % numqueues;
+}
+
  static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                    struct iov_iter *from,
                                    struct virtio_net_hdr *hdr)
@@ -135,15 +268,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 +290,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/uapi/linux/if_tun.h +23 -0
index 980de74724fc..fe4b984d3bbb 100644
@@ -62,6 +62,62 @@
  #define TUNSETCARRIER _IOW('T', 226, int)
  #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHTYPES - ioctl to get supported virtio_net hashing types
+ *
+ * The argument is a pointer to __u32 which will store the supported virtio_net
+ * hashing types.
+ */
+#define TUNGETVNETHASHTYPES _IOR('T', 228, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGAUTOMQ - ioctl to enable automq with hash reporting
+ *
+ * Disable RSS and enable automatic receive steering with hash reporting.
+ *
+ * The argument is a pointer to __u32 that contains a bitmask of hash types
+ * allowed to be reported.
+ *
+ * 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 TUNSETVNETREPORTINGAUTOMQ _IOR('T', 229, __u32)
Quoted 8 lines

include/uapi/linux/if_tun.h line 92

Quoted 5 lines
include/uapi/linux/if_tun.h +5 -0
+
+/**
+ * define TUNSETVNETREPORTINGRSS - ioctl to enable RSS with hash reporting
+ *
+ * Disable automatic receive steering and enable RSS with hash reporting.

This is unnecessary, e.g one day will have select_queue_xyz(), we don't want to say "Disable automatic receive steering and xyz ..."

It is still something better to be documented as its behavior is somewhat complicated.

Concretely, this ioctl disables automatic receive steering but doesn't disable steering by eBPF, which is implied by TUN_STEERINGEBPF_FALLBACK.

Perhaps it may be rephrased to tell it disables "the other receive steering mechanism except eBPF".

Regards,
Akihiko Odaki

Quoted 62 lines
Quoted 4 lines
include/uapi/linux/if_tun.h +3 -0
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.

include/uapi/linux/if_tun.h line 105

Quoted 10 lines
include/uapi/linux/if_tun.h +10 -0
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.
+ */
+#define TUNSETVNETREPORTINGRSS _IOR('T', 230, struct tun_vnet_rss)
+
+/**
+ * define TUNSETVNETRSS - ioctl to enable RSS without hash reporting
+ *
+ * Disable automatic receive steering and enable RSS without hash reporting.

Same here.

Quoted 40 lines
include/uapi/linux/if_tun.h +30 -1
+ *
+ * The argument is a pointer to the compound of the following in order:
+ *
+ * 1. &struct tun_vnet_rss
+ * 3. Indirection table
+ * 4. Key
+ *
+ * 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 TUNSETVNETRSS _IOR('T', 231, struct tun_vnet_rss)
+
  /* TUNSETIFF ifr flags */
  #define IFF_TUN                0x0001
  #define IFF_TAP                0x0002
@@ -124,4 +180,19 @@ struct tun_filter {
   */
  #define TUN_STEERINGEBPF_FALLBACK -1
 
+/**
+ * struct tun_vnet_rss - virtio_net RSS configuration
+ * @hash_types:
+ *             Bitmask of allowed hash types
+ * @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_rss {
+       __u32 hash_types;
+       __u16 indirection_table_mask;
+       __u16 unclassified_queue;
+};
+
  #endif /* _UAPI__IF_TUN_H */
 
--
Quoted 2 lines

2.49.0

Thanks

#25 lore raw

Re: [PATCH net-next v12 04/10] tun: Add common virtio-net hash feature code

Quoted 209 lines · Akihiko Odaki
Quoted 207 lines · Jason Wang
Quoted 181 lines · Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
  drivers/net/tap.c           |  10 ++-
  drivers/net/tun.c           |  12 +++-
  drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
  include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
  4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
         sock_put(&q->sk);
  }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
  /*
   * Select a queue based on the rxq of the device on which this packet
   * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
         int total;
 
         if (q->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr vnet_hdr;
+               struct virtio_net_hdr_v1_hash vnet_hdr;
 
                 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-               ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+                                           tap_find_hash, &vnet_hdr);
                 if (ret)
                         return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                 e->rps_rxhash = hash;
  }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
  /* We try to identify a flow through its rxhash. The reason that
   * we do not check rxq no. is because some cards(e.g 82599), chooses
   * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
         ssize_t ret;
 
         if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
+               struct virtio_net_hdr_v1_hash gso = { 0 };
 
                 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
                 ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
         }
 
         if (vnet_hdr_sz) {
-               struct virtio_net_hdr gso;
+               struct virtio_net_hdr_v1_hash gso;
 
-               ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+                                           skb, tun_find_hash, &gso);
                 if (ret)
                         return ret;
 
drivers/net/tun_vnet.h +35 -0
index 58b9ac7a5fc4..45d0533efc8d 100644
@@ -6,6 +6,17 @@
  #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 {
+       bool report;
+       bool rss;
+       struct tun_vnet_rss common;
+       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 +118,128 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
         }
  }
 
+static inline long tun_vnet_ioctl_gethashtypes(u32 __user *argp)
+{
+       return put_user(VIRTIO_NET_SUPPORTED_HASH_TYPES, argp) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash __rcu **hashp,
+                                         unsigned int cmd,
+                                         void __user *argp)
+{
+       struct tun_vnet_rss common;
+       struct tun_vnet_hash *hash;
+       size_t indirection_table_size;
+       size_t key_size;
+       size_t size;
+
+       switch (cmd) {
+       case TUNSETVNETREPORTINGAUTOMQ:
+               if (get_user(common.hash_types, (u32 __user *)argp))
+                       return -EFAULT;
+
+               if (common.hash_types) {
+                       hash = kzalloc(sizeof(*hash), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+
+                       hash->report = true;
+                       hash->common.hash_types = common.hash_types;
+               } else {
+                       hash = NULL;
+               }
+               break;
+
+       case TUNSETVNETREPORTINGRSS:
+       case TUNSETVNETRSS:

So the above three shows unnecessary design redundancy as well as a burden for the future extension. Why not simply have

1) TUNSETVNET_RSS
2) TUNSETVNET_HASH_REPORT
?

Which maps to

#define VIRTIO_NET_CTRL_MQ_RSS_CONFIG 1 (for configurable
receive steering)
#define VIRTIO_NET_CTRL_MQ_HASH_CONFIG 2 (for configurable
hash calculation)

Quoted 5 lines
Quoted 3 lines

It would be always easier to start with what spec had or at least we need to explain why we choose a different design here or in the changelog to ease our life.

TUNSETVNETREPORTINGAUTOMQ maps to VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

It's not:

VIRTIO_NET_CTRL_MQ_HASH_CONFIG uses:

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

but TUNSETVNETREPORTINGAUTOMQ only accepts hash_types without others:

Quoted 3 lines

TUNSETVNETREPORTINGRSS and TUNSETVNETRSS map to
VIRTIO_NET_CTRL_MQ_RSS_CONFIG.

I think we've already had a discussion about this.

Reusing virtio-net uAPI is much better instead of having a tun specific one considering tun may need to support more virtio commands in the future. Or maybe it's the time to introduce a transport for the virtio control virtqueue uAPI in tuntap to avoid inventing new uAPI endlessly.

What's more I see:

struct tun_vnet_rss {
        __u32 hash_types;
        __u16 indirection_table_mask;
        __u16 unclassified_queue;
};
struct tun_vnet_hash {
        bool report;
        bool rss;
        struct tun_vnet_rss common;
        u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
        u16 rss_indirection_table[];
};

As I pointed out in the past, let's just decouple the rss from hash, everything would be much simpler, or you need to explain why you couple this somewhere.

For example:

1) why is the tun_vnet_hash not part of the uAPI but tun_vnet_rss, or how could userspace know what kind of format it would use for TUNSETVNETREPORTINGRSS?
2) what's the advantages of embedding rss specific stuff into hash report structure
3) what's the advantages of not using virtio-net uAPI

More issues:

1) max_tx_vq is ignored, so do you expect the userspace to intercept this and switch to using TUNSETQUEUE? This seems like a burden as TUN can simply accept it and do the attaching/detaching by itself
2) the rx depends on the indirection table, so userspace need to intercept the indirection and change the rx queue numbers accordingly
3) RSS allows a async TX/RX queue, this is not supported by TUN now, no matter if we decide to let userspace to intercept max_tx_vq or not, we need to implement it first

Things would be much more simpler, if kernel can do 1) and 2).

Quoted 3 lines

We have two ioctls here because VIRTIO_NET_CTRL_MQ_RSS_CONFIG behaves differently depending on whether VIRTIO_NET_F_HASH_REPORT is negotiated or not;

It wouldn't be a problem if you do 1:1 mapping between virtio commands and TUN uAPI, otherwise it should have a bug somewhere.

Quoted 2 lines

it also enables hash
reporting if the feature is negotiated.

Again, starting from virtio-net command is easier, a strong justification is needed to explain why we choose another for tun/tap.

Quoted 10 lines
Quoted 4 lines

One day we would have tun_select_queue_algorithm_x() we don't have to duplicate the ioctls once again here like TUNSETVNETREPORTINGXYZ

5.1.6.5.6.4 Hash calculation says:

Quoted 4 lines

If VIRTIO_NET_F_HASH_REPORT was negotiated and the device uses
automatic receive steering, the device MUST support a command to configure hash calculation parameters.

Quoted 6 lines
Quoted 3 lines

The driver provides parameters for hash calculation as follows:

class VIRTIO_NET_CTRL_MQ, command VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

VIRTIO_NET_CTRL_MQ_HASH_CONFIG is for automatic receive steering and not for RSS (or other steering algorithms if the spec gets any in the future).

I'm not sure but the spec needs some tweaking. For example, I don't expect there would be a dedicated hash config command for flow filters in the future. I think the reason why spec says like this is that virtio-net only supports automatic receive steering.

Note that macvtap doesn't implement automatic receive steering.

Quoted 208 lines
Quoted 24 lines
Quoted 24 lines
drivers/net/tun_vnet.h +22 -0
+               if (copy_from_user(&common, argp, sizeof(common)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)common.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(common.hash_types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)common.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 + common.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {
+                       kfree(hash);
Quoted 8 lines

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

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->report = cmd == TUNSETVNETREPORTINGRSS;

At least, if this is the only difference why not simply code this into the ioctl itself other than having a very similar command?

It is what the previous version did. Either is fine I guess; the only practical difference would be the size of the configuration struct is smaller with this approach.

Quoted 162 lines
Quoted 162 lines
drivers/net/tun_vnet.h +84 -9
+               hash->rss = true;
+               hash->common = common;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+       return 0;
+}
+
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash *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->report)
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.hash_types, keys),
+               .value = value
+       };
+}
+
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.hash_types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->common.unclassified_queue % numqueues;
+
+       if (hash->report) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->common.indirection_table_mask;
+
+       return hash->rss_indirection_table[index] % numqueues;
+}
+
  static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                    struct iov_iter *from,
                                    struct virtio_net_hdr *hdr)
@@ -135,15 +268,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 +290,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/uapi/linux/if_tun.h +23 -0
index 980de74724fc..fe4b984d3bbb 100644
@@ -62,6 +62,62 @@
  #define TUNSETCARRIER _IOW('T', 226, int)
  #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHTYPES - ioctl to get supported virtio_net hashing types
+ *
+ * The argument is a pointer to __u32 which will store the supported virtio_net
+ * hashing types.
+ */
+#define TUNGETVNETHASHTYPES _IOR('T', 228, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGAUTOMQ - ioctl to enable automq with hash reporting
+ *
+ * Disable RSS and enable automatic receive steering with hash reporting.
+ *
+ * The argument is a pointer to __u32 that contains a bitmask of hash types
+ * allowed to be reported.
+ *
+ * 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 TUNSETVNETREPORTINGAUTOMQ _IOR('T', 229, __u32)
Quoted 8 lines

include/uapi/linux/if_tun.h line 92

Quoted 5 lines
include/uapi/linux/if_tun.h +5 -0
+
+/**
+ * define TUNSETVNETREPORTINGRSS - ioctl to enable RSS with hash reporting
+ *
+ * Disable automatic receive steering and enable RSS with hash reporting.

This is unnecessary, e.g one day will have select_queue_xyz(), we don't want to say "Disable automatic receive steering and xyz ..."

Quoted 2 lines

It is still something better to be documented as its behavior is somewhat complicated.

This is a hint of uAPI design issue.

Quoted 3 lines

Concretely, this ioctl disables automatic receive steering but doesn't disable steering by eBPF, which is implied by TUN_STEERINGEBPF_FALLBACK.

It would be simpler:

1) not having TUN_STEERINGEBPF_FALLBACK
2) the steering algorithm depends on the last uAPI call

Quoted 8 lines

Perhaps it may be rephrased to tell it disables "the other receive steering mechanism except eBPF".

Regards,
Akihiko Odaki

Quoted 1 line

Thanks

#26 lore raw

Re: [PATCH net-next v12 04/10] tun: Add common virtio-net hash feature code

Quoted 220 lines · Jason Wang
Quoted 209 lines · Akihiko Odaki
Quoted 207 lines · Jason Wang
Quoted 181 lines · Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
   drivers/net/tap.c           |  10 ++-
   drivers/net/tun.c           |  12 +++-
   drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
   include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
   4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
          sock_put(&q->sk);
   }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
   /*
    * Select a queue based on the rxq of the device on which this packet
    * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
          int total;
 
          if (q->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr vnet_hdr;
+               struct virtio_net_hdr_v1_hash vnet_hdr;
 
                  vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-               ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+                                           tap_find_hash, &vnet_hdr);
                  if (ret)
                          return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                  e->rps_rxhash = hash;
   }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
   /* We try to identify a flow through its rxhash. The reason that
    * we do not check rxq no. is because some cards(e.g 82599), chooses
    * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
          ssize_t ret;
 
          if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
+               struct virtio_net_hdr_v1_hash gso = { 0 };
 
                  vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
                  ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
          }
 
          if (vnet_hdr_sz) {
-               struct virtio_net_hdr gso;
+               struct virtio_net_hdr_v1_hash gso;
 
-               ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+                                           skb, tun_find_hash, &gso);
                  if (ret)
                          return ret;
 
drivers/net/tun_vnet.h +35 -0
index 58b9ac7a5fc4..45d0533efc8d 100644
@@ -6,6 +6,17 @@
   #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 {
+       bool report;
+       bool rss;
+       struct tun_vnet_rss common;
+       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 +118,128 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
          }
   }
 
+static inline long tun_vnet_ioctl_gethashtypes(u32 __user *argp)
+{
+       return put_user(VIRTIO_NET_SUPPORTED_HASH_TYPES, argp) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash __rcu **hashp,
+                                         unsigned int cmd,
+                                         void __user *argp)
+{
+       struct tun_vnet_rss common;
+       struct tun_vnet_hash *hash;
+       size_t indirection_table_size;
+       size_t key_size;
+       size_t size;
+
+       switch (cmd) {
+       case TUNSETVNETREPORTINGAUTOMQ:
+               if (get_user(common.hash_types, (u32 __user *)argp))
+                       return -EFAULT;
+
+               if (common.hash_types) {
+                       hash = kzalloc(sizeof(*hash), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+
+                       hash->report = true;
+                       hash->common.hash_types = common.hash_types;
+               } else {
+                       hash = NULL;
+               }
+               break;
+
+       case TUNSETVNETREPORTINGRSS:
+       case TUNSETVNETRSS:

So the above three shows unnecessary design redundancy as well as a burden for the future extension. Why not simply have

1) TUNSETVNET_RSS
2) TUNSETVNET_HASH_REPORT
?

Which maps to

   #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1 (for configurable

receive steering)
#define VIRTIO_NET_CTRL_MQ_HASH_CONFIG 2 (for configurable
hash calculation)

Quoted 5 lines
Quoted 3 lines

It would be always easier to start with what spec had or at least we need to explain why we choose a different design here or in the changelog to ease our life.

TUNSETVNETREPORTINGAUTOMQ maps to VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

It's not:

VIRTIO_NET_CTRL_MQ_HASH_CONFIG uses:

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

but TUNSETVNETREPORTINGAUTOMQ only accepts hash_types without others:

The others are not present because the spec doesn't specify what to do with them and the kernel doesn't use them either.

Quoted 33 lines
Quoted 4 lines

TUNSETVNETREPORTINGRSS and TUNSETVNETRSS map to
VIRTIO_NET_CTRL_MQ_RSS_CONFIG.

I think we've already had a discussion about this.

Reusing virtio-net uAPI is much better instead of having a tun specific one considering tun may need to support more virtio commands in the future. Or maybe it's the time to introduce a transport for the virtio control virtqueue uAPI in tuntap to avoid inventing new uAPI endlessly.

What's more I see:

struct tun_vnet_rss {
         __u32 hash_types;
         __u16 indirection_table_mask;
         __u16 unclassified_queue;
};
struct tun_vnet_hash {
         bool report;
         bool rss;
         struct tun_vnet_rss common;
         u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
         u16 rss_indirection_table[];
};

As I pointed out in the past, let's just decouple the rss from hash, everything would be much simpler, or you need to explain why you couple this somewhere.

Quoted 5 lines

For example:

1) why is the tun_vnet_hash not part of the uAPI but tun_vnet_rss, or how could userspace know what kind of format it would use for TUNSETVNETREPORTINGRSS?

That was the previous version.

Quoted 2 lines

2) what's the advantages of embedding rss specific stuff into hash report structure

Because the hash types field in struct tun_vnet_rss is used by hash reporting too.

Quoted 1 line

3) what's the advantages of not using virtio-net uAPI

1. The use cases that don't involve VM will be simplified; programs for such a use case will not need to convert endian or to fill fileds the kernel doesn't care.
2. It aligns with existing UAPIs that operate in native endian and don't use virtio-net data structures like TUNSETOFFLOAD and TUNSETVNETHDRSZ.

Quoted 12 lines

More issues:

1) max_tx_vq is ignored, so do you expect the userspace to intercept this and switch to using TUNSETQUEUE? This seems like a burden as TUN can simply accept it and do the attaching/detaching by itself
2) the rx depends on the indirection table, so userspace need to intercept the indirection and change the rx queue numbers accordingly
3) RSS allows a async TX/RX queue, this is not supported by TUN now, no matter if we decide to let userspace to intercept max_tx_vq or not, we need to implement it first

Quoted 1 line

Things would be much more simpler, if kernel can do 1) and 2).

Attaching and detaching queues is not possible for the kernel because it doesn't know what file descriptors that map to queues will be used by the userspace.

The following patch does 2) for QEMU:
https://lore.kernel.org/qemu-devel/[email protected]/

For 3), the patch for QEMU takes the maximum of TX and RX queue numbers to derive the number of queue pairs.

Quoted 7 lines
Quoted 4 lines

We have two ioctls here because VIRTIO_NET_CTRL_MQ_RSS_CONFIG behaves differently depending on whether VIRTIO_NET_F_HASH_REPORT is negotiated or not;

It wouldn't be a problem if you do 1:1 mapping between virtio commands and TUN uAPI, otherwise it should have a bug somewhere.

Speaking of 1:1 mapping, it is possible to map VIRTIO_NET_F_HASH_REPORT into another ioctl. It may help add another receive steering algorithm in the future by not requiring two ioctls (TUNSETVNETREPORTING_X and TUNSETVNET_X).

Quoted 24 lines
Quoted 3 lines

it also enables hash
reporting if the feature is negotiated.

Again, starting from virtio-net command is easier, a strong justification is needed to explain why we choose another for tun/tap.

Quoted 10 lines
Quoted 4 lines

One day we would have tun_select_queue_algorithm_x() we don't have to duplicate the ioctls once again here like TUNSETVNETREPORTINGXYZ

5.1.6.5.6.4 Hash calculation says:

Quoted 4 lines

If VIRTIO_NET_F_HASH_REPORT was negotiated and the device uses
automatic receive steering, the device MUST support a command to configure hash calculation parameters.

Quoted 6 lines
Quoted 3 lines

The driver provides parameters for hash calculation as follows:

class VIRTIO_NET_CTRL_MQ, command VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

VIRTIO_NET_CTRL_MQ_HASH_CONFIG is for automatic receive steering and not for RSS (or other steering algorithms if the spec gets any in the future).

Quoted 6 lines

I'm not sure but the spec needs some tweaking. For example, I don't expect there would be a dedicated hash config command for flow filters in the future. I think the reason why spec says like this is that virtio-net only supports automatic receive steering.

Note that macvtap doesn't implement automatic receive steering.

QEMU advertises VIRTIO_NET_F_CTRL_VQ for macvtap too, so it should have implemented it. I think QEMU with macvtap still compliant to the spec.

"5.1.6.5.6 Automatic receive steering in multiqueue mode" says:

Quoted 3 lines

After the driver transmitted a packet of a flow on transmitqX, the device SHOULD cause incoming packets for that flow to be steered to receiveqX.

It is "SHOULD", so it is still compliant if the device doesn't properly respect the flow.

Quoted 215 lines
Quoted 209 lines
Quoted 25 lines
Quoted 25 lines
drivers/net/tun_vnet.h +22 -0
+               if (copy_from_user(&common, argp, sizeof(common)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)common.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(common.hash_types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)common.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 + common.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {
+                       kfree(hash);
Quoted 8 lines

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

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->report = cmd == TUNSETVNETREPORTINGRSS;

At least, if this is the only difference why not simply code this into the ioctl itself other than having a very similar command?

It is what the previous version did. Either is fine I guess; the only practical difference would be the size of the configuration struct is smaller with this approach.

Quoted 162 lines
Quoted 162 lines
drivers/net/tun_vnet.h +84 -9
+               hash->rss = true;
+               hash->common = common;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+       return 0;
+}
+
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash *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->report)
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.hash_types, keys),
+               .value = value
+       };
+}
+
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.hash_types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->common.unclassified_queue % numqueues;
+
+       if (hash->report) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->common.indirection_table_mask;
+
+       return hash->rss_indirection_table[index] % numqueues;
+}
+
   static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                     struct iov_iter *from,
                                     struct virtio_net_hdr *hdr)
@@ -135,15 +268,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 +290,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/uapi/linux/if_tun.h +23 -0
index 980de74724fc..fe4b984d3bbb 100644
@@ -62,6 +62,62 @@
   #define TUNSETCARRIER _IOW('T', 226, int)
   #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHTYPES - ioctl to get supported virtio_net hashing types
+ *
+ * The argument is a pointer to __u32 which will store the supported virtio_net
+ * hashing types.
+ */
+#define TUNGETVNETHASHTYPES _IOR('T', 228, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGAUTOMQ - ioctl to enable automq with hash reporting
+ *
+ * Disable RSS and enable automatic receive steering with hash reporting.
+ *
+ * The argument is a pointer to __u32 that contains a bitmask of hash types
+ * allowed to be reported.
+ *
+ * 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 TUNSETVNETREPORTINGAUTOMQ _IOR('T', 229, __u32)
Quoted 8 lines

include/uapi/linux/if_tun.h line 92

Quoted 5 lines
include/uapi/linux/if_tun.h +5 -0
+
+/**
+ * define TUNSETVNETREPORTINGRSS - ioctl to enable RSS with hash reporting
+ *
+ * Disable automatic receive steering and enable RSS with hash reporting.

This is unnecessary, e.g one day will have select_queue_xyz(), we don't want to say "Disable automatic receive steering and xyz ..."

Quoted 2 lines

It is still something better to be documented as its behavior is somewhat complicated.

This is a hint of uAPI design issue.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Concretely, this ioctl disables automatic receive steering but doesn't disable steering by eBPF, which is implied by TUN_STEERINGEBPF_FALLBACK.

It would be simpler:

1) not having TUN_STEERINGEBPF_FALLBACK
2) the steering algorithm depends on the last uAPI call

What will TUNSETSTEERINGEBPF with NULL mean in that case? It currently switches the steering algorithm to automq.

Regards,
Akihiko Odaki

#27 lore raw

Re: [PATCH net-next v12 04/10] tun: Add common virtio-net hash feature code

Quoted 229 lines · Akihiko Odaki
Quoted 227 lines · Jason Wang
Quoted 209 lines · Akihiko Odaki
Quoted 207 lines · Jason Wang
Quoted 181 lines · Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
   drivers/net/tap.c           |  10 ++-
   drivers/net/tun.c           |  12 +++-
   drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
   include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
   4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
          sock_put(&q->sk);
   }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
   /*
    * Select a queue based on the rxq of the device on which this packet
    * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
          int total;
 
          if (q->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr vnet_hdr;
+               struct virtio_net_hdr_v1_hash vnet_hdr;
 
                  vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-               ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+                                           tap_find_hash, &vnet_hdr);
                  if (ret)
                          return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                  e->rps_rxhash = hash;
   }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
   /* We try to identify a flow through its rxhash. The reason that
    * we do not check rxq no. is because some cards(e.g 82599), chooses
    * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
          ssize_t ret;
 
          if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
+               struct virtio_net_hdr_v1_hash gso = { 0 };
 
                  vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
                  ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
          }
 
          if (vnet_hdr_sz) {
-               struct virtio_net_hdr gso;
+               struct virtio_net_hdr_v1_hash gso;
 
-               ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+                                           skb, tun_find_hash, &gso);
                  if (ret)
                          return ret;
 
drivers/net/tun_vnet.h +35 -0
index 58b9ac7a5fc4..45d0533efc8d 100644
@@ -6,6 +6,17 @@
   #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 {
+       bool report;
+       bool rss;
+       struct tun_vnet_rss common;
+       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 +118,128 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
          }
   }
 
+static inline long tun_vnet_ioctl_gethashtypes(u32 __user *argp)
+{
+       return put_user(VIRTIO_NET_SUPPORTED_HASH_TYPES, argp) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash __rcu **hashp,
+                                         unsigned int cmd,
+                                         void __user *argp)
+{
+       struct tun_vnet_rss common;
+       struct tun_vnet_hash *hash;
+       size_t indirection_table_size;
+       size_t key_size;
+       size_t size;
+
+       switch (cmd) {
+       case TUNSETVNETREPORTINGAUTOMQ:
+               if (get_user(common.hash_types, (u32 __user *)argp))
+                       return -EFAULT;
+
+               if (common.hash_types) {
+                       hash = kzalloc(sizeof(*hash), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+
+                       hash->report = true;
+                       hash->common.hash_types = common.hash_types;
+               } else {
+                       hash = NULL;
+               }
+               break;
+
+       case TUNSETVNETREPORTINGRSS:
+       case TUNSETVNETRSS:

So the above three shows unnecessary design redundancy as well as a burden for the future extension. Why not simply have

1) TUNSETVNET_RSS
2) TUNSETVNET_HASH_REPORT
?

Which maps to

   #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1 (for configurable

receive steering)
#define VIRTIO_NET_CTRL_MQ_HASH_CONFIG 2 (for configurable
hash calculation)

Quoted 5 lines
Quoted 3 lines

It would be always easier to start with what spec had or at least we need to explain why we choose a different design here or in the changelog to ease our life.

TUNSETVNETREPORTINGAUTOMQ maps to VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

It's not:

VIRTIO_NET_CTRL_MQ_HASH_CONFIG uses:

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

but TUNSETVNETREPORTINGAUTOMQ only accepts hash_types without others:

The others are not present because the spec doesn't specify what to do with them and the kernel doesn't use them either.

Did you mean the hash_key_length and hash_key_data? Note that we have drivers other than the Linux ones as well.

Quoted 45 lines
Quoted 34 lines
Quoted 5 lines

TUNSETVNETREPORTINGRSS and TUNSETVNETRSS map to
VIRTIO_NET_CTRL_MQ_RSS_CONFIG.

I think we've already had a discussion about this.

Reusing virtio-net uAPI is much better instead of having a tun specific one considering tun may need to support more virtio commands in the future. Or maybe it's the time to introduce a transport for the virtio control virtqueue uAPI in tuntap to avoid inventing new uAPI endlessly.

What's more I see:

struct tun_vnet_rss {
         __u32 hash_types;
         __u16 indirection_table_mask;
         __u16 unclassified_queue;
};
struct tun_vnet_hash {
         bool report;
         bool rss;
         struct tun_vnet_rss common;
         u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
         u16 rss_indirection_table[];
};

As I pointed out in the past, let's just decouple the rss from hash, everything would be much simpler, or you need to explain why you couple this somewhere.

Quoted 5 lines

For example:

1) why is the tun_vnet_hash not part of the uAPI but tun_vnet_rss, or how could userspace know what kind of format it would use for TUNSETVNETREPORTINGRSS?

That was the previous version.

Quoted 2 lines

2) what's the advantages of embedding rss specific stuff into hash report structure

Quoted 8 lines

Because the hash types field in struct tun_vnet_rss is used by hash reporting too.

Quoted 1 line

3) what's the advantages of not using virtio-net uAPI

1. The use cases that don't involve VM will be simplified; programs for such a use case will not need to convert endian or to fill fileds the kernel doesn't care.

Well, virtio_net_hdr is used by packet socket as well. Considering the complexity of designing a new uAPI, it's still better.

Or maybe you can clarify which field that kernel doesn't care about? In this case TUN/TAP is basically the device datapath, if some of the fields don't make sense, it's a bug of the spec.

Quoted 2 lines

2. It aligns with existing UAPIs that operate in native endian and don't use virtio-net data structures like TUNSETOFFLOAD and TUNSETVNETHDRSZ.

For those two examples, it would be used by guests directly. This is different from RSS stuff.

With native endian, you need an endian conversation that converts le to native.

Quoted 12 lines
Quoted 12 lines

More issues:

1) max_tx_vq is ignored, so do you expect the userspace to intercept this and switch to using TUNSETQUEUE? This seems like a burden as TUN can simply accept it and do the attaching/detaching by itself
2) the rx depends on the indirection table, so userspace need to intercept the indirection and change the rx queue numbers accordingly
3) RSS allows a async TX/RX queue, this is not supported by TUN now, no matter if we decide to let userspace to intercept max_tx_vq or not, we need to implement it first

Quoted 5 lines
Quoted 1 line
Quoted 1 line

Things would be much more simpler, if kernel can do 1) and 2).

Attaching and detaching queues is not possible for the kernel because it doesn't know what file descriptors that map to queues will be used by the userspace.

The kernel knows, tfile has a queue_index part.

Quoted 3 lines

The following patch does 2) for QEMU:
https://lore.kernel.org/qemu-devel/[email protected]/

See below point, form the view of the kernel, it's still a queue pair not async TX/RX queue.

Quoted 9 lines

For 3), the patch for QEMU takes the maximum of TX and RX queue numbers to derive the number of queue pairs.

Quoted 5 lines
Quoted 4 lines

We have two ioctls here because VIRTIO_NET_CTRL_MQ_RSS_CONFIG behaves differently depending on whether VIRTIO_NET_F_HASH_REPORT is negotiated or not;

Quoted 7 lines
Quoted 2 lines

It wouldn't be a problem if you do 1:1 mapping between virtio commands and TUN uAPI, otherwise it should have a bug somewhere.

Speaking of 1:1 mapping, it is possible to map VIRTIO_NET_F_HASH_REPORT into another ioctl. It may help add another receive steering algorithm in the future by not requiring two ioctls (TUNSETVNETREPORTING_X and TUNSETVNET_X).

Yes and as I pointed out, virtio_net_hash_config should not be specific to automq, it can work for other steering algorithm as well.

Quoted 30 lines
Quoted 30 lines
Quoted 4 lines

it also enables hash
reporting if the feature is negotiated.

Again, starting from virtio-net command is easier, a strong justification is needed to explain why we choose another for tun/tap.

Quoted 10 lines
Quoted 4 lines

One day we would have tun_select_queue_algorithm_x() we don't have to duplicate the ioctls once again here like TUNSETVNETREPORTINGXYZ

5.1.6.5.6.4 Hash calculation says:

Quoted 4 lines

If VIRTIO_NET_F_HASH_REPORT was negotiated and the device uses
automatic receive steering, the device MUST support a command to configure hash calculation parameters.

Quoted 6 lines
Quoted 3 lines

The driver provides parameters for hash calculation as follows:

class VIRTIO_NET_CTRL_MQ, command VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

VIRTIO_NET_CTRL_MQ_HASH_CONFIG is for automatic receive steering and not for RSS (or other steering algorithms if the spec gets any in the future).

I'm not sure but the spec needs some tweaking. For example, I don't expect there would be a dedicated hash config command for flow filters in the future. I think the reason why spec says like this is that virtio-net only supports automatic receive steering.

Quoted 4 lines
Quoted 1 line

Note that macvtap doesn't implement automatic receive steering.

QEMU advertises VIRTIO_NET_F_CTRL_VQ for macvtap too, so it should have implemented it. I think QEMU with macvtap still compliant to the spec.

Compliant, but automatic traffic steering is the best effort as well.

Nope, TUN/TAP implements a flow cache that can steer tx based on rx. Macvtap simply uses hash here.

Quoted 9 lines

"5.1.6.5.6 Automatic receive steering in multiqueue mode" says:

Quoted 3 lines

After the driver transmitted a packet of a flow on transmitqX, the device SHOULD cause incoming packets for that flow to be steered to receiveqX.

It is "SHOULD", so it is still compliant if the device doesn't properly respect the flow.

Yes, a quality of implementation, or it's impractical to support a correct steering for this device as limited resources and mailious users can do syn flood etc.

Quoted 219 lines
Quoted 219 lines
Quoted 210 lines
Quoted 26 lines
Quoted 26 lines
drivers/net/tun_vnet.h +22 -0
+               if (copy_from_user(&common, argp, sizeof(common)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)common.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(common.hash_types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)common.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 + common.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {
+                       kfree(hash);
Quoted 8 lines

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

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->report = cmd == TUNSETVNETREPORTINGRSS;

At least, if this is the only difference why not simply code this into the ioctl itself other than having a very similar command?

It is what the previous version did. Either is fine I guess; the only practical difference would be the size of the configuration struct is smaller with this approach.

Quoted 162 lines
Quoted 162 lines
drivers/net/tun_vnet.h +84 -9
+               hash->rss = true;
+               hash->common = common;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+       return 0;
+}
+
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash *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->report)
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.hash_types, keys),
+               .value = value
+       };
+}
+
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.hash_types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->common.unclassified_queue % numqueues;
+
+       if (hash->report) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->common.indirection_table_mask;
+
+       return hash->rss_indirection_table[index] % numqueues;
+}
+
   static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                     struct iov_iter *from,
                                     struct virtio_net_hdr *hdr)
@@ -135,15 +268,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 +290,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/uapi/linux/if_tun.h +23 -0
index 980de74724fc..fe4b984d3bbb 100644
@@ -62,6 +62,62 @@
   #define TUNSETCARRIER _IOW('T', 226, int)
   #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHTYPES - ioctl to get supported virtio_net hashing types
+ *
+ * The argument is a pointer to __u32 which will store the supported virtio_net
+ * hashing types.
+ */
+#define TUNGETVNETHASHTYPES _IOR('T', 228, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGAUTOMQ - ioctl to enable automq with hash reporting
+ *
+ * Disable RSS and enable automatic receive steering with hash reporting.
+ *
+ * The argument is a pointer to __u32 that contains a bitmask of hash types
+ * allowed to be reported.
+ *
+ * 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 TUNSETVNETREPORTINGAUTOMQ _IOR('T', 229, __u32)
Quoted 8 lines

include/uapi/linux/if_tun.h line 92

Quoted 5 lines
include/uapi/linux/if_tun.h +5 -0
+
+/**
+ * define TUNSETVNETREPORTINGRSS - ioctl to enable RSS with hash reporting
+ *
+ * Disable automatic receive steering and enable RSS with hash reporting.

This is unnecessary, e.g one day will have select_queue_xyz(), we don't want to say "Disable automatic receive steering and xyz ..."

Quoted 2 lines

It is still something better to be documented as its behavior is somewhat complicated.

This is a hint of uAPI design issue.

Quoted 3 lines

Concretely, this ioctl disables automatic receive steering but doesn't disable steering by eBPF, which is implied by TUN_STEERINGEBPF_FALLBACK.

Quoted 7 lines
Quoted 4 lines

It would be simpler:

1) not having TUN_STEERINGEBPF_FALLBACK
2) the steering algorithm depends on the last uAPI call

What will TUNSETSTEERINGEBPF with NULL mean in that case? It currently switches the steering algorithm to automq.

A stackwise semantic then?

Thanks

Quoted 4 lines

Regards,
Akihiko Odaki

#28 lore raw

Re: [PATCH net-next v12 04/10] tun: Add common virtio-net hash feature code

Quoted 230 lines · Jason Wang
Quoted 229 lines · Akihiko Odaki
Quoted 227 lines · Jason Wang
Quoted 209 lines · Akihiko Odaki
Quoted 207 lines · Jason Wang
Quoted 181 lines · Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
    drivers/net/tap.c           |  10 ++-
    drivers/net/tun.c           |  12 +++-
    drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
    include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
    4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
           sock_put(&q->sk);
    }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
    /*
     * Select a queue based on the rxq of the device on which this packet
     * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
           int total;
 
           if (q->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr vnet_hdr;
+               struct virtio_net_hdr_v1_hash vnet_hdr;
 
                   vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-               ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+                                           tap_find_hash, &vnet_hdr);
                   if (ret)
                           return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                   e->rps_rxhash = hash;
    }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
    /* We try to identify a flow through its rxhash. The reason that
     * we do not check rxq no. is because some cards(e.g 82599), chooses
     * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
           ssize_t ret;
 
           if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
+               struct virtio_net_hdr_v1_hash gso = { 0 };
 
                   vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
                   ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
           }
 
           if (vnet_hdr_sz) {
-               struct virtio_net_hdr gso;
+               struct virtio_net_hdr_v1_hash gso;
 
-               ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+                                           skb, tun_find_hash, &gso);
                   if (ret)
                           return ret;
 
drivers/net/tun_vnet.h +35 -0
index 58b9ac7a5fc4..45d0533efc8d 100644
@@ -6,6 +6,17 @@
    #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 {
+       bool report;
+       bool rss;
+       struct tun_vnet_rss common;
+       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 +118,128 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
           }
    }
 
+static inline long tun_vnet_ioctl_gethashtypes(u32 __user *argp)
+{
+       return put_user(VIRTIO_NET_SUPPORTED_HASH_TYPES, argp) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash __rcu **hashp,
+                                         unsigned int cmd,
+                                         void __user *argp)
+{
+       struct tun_vnet_rss common;
+       struct tun_vnet_hash *hash;
+       size_t indirection_table_size;
+       size_t key_size;
+       size_t size;
+
+       switch (cmd) {
+       case TUNSETVNETREPORTINGAUTOMQ:
+               if (get_user(common.hash_types, (u32 __user *)argp))
+                       return -EFAULT;
+
+               if (common.hash_types) {
+                       hash = kzalloc(sizeof(*hash), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+
+                       hash->report = true;
+                       hash->common.hash_types = common.hash_types;
+               } else {
+                       hash = NULL;
+               }
+               break;
+
+       case TUNSETVNETREPORTINGRSS:
+       case TUNSETVNETRSS:

So the above three shows unnecessary design redundancy as well as a burden for the future extension. Why not simply have

1) TUNSETVNET_RSS
2) TUNSETVNET_HASH_REPORT
?

Which maps to

    #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1 (for configurable
receive steering)
    #define VIRTIO_NET_CTRL_MQ_HASH_CONFIG         2 (for configurable

hash calculation)

Quoted 5 lines
Quoted 3 lines

It would be always easier to start with what spec had or at least we need to explain why we choose a different design here or in the changelog to ease our life.

TUNSETVNETREPORTINGAUTOMQ maps to VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

It's not:

VIRTIO_NET_CTRL_MQ_HASH_CONFIG uses:

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

but TUNSETVNETREPORTINGAUTOMQ only accepts hash_types without others:

The others are not present because the spec doesn't specify what to do with them and the kernel doesn't use them either.

Did you mean the hash_key_length and hash_key_data? Note that we have drivers other than the Linux ones as well.

And reserved. Drivers can set whatever to these fields. It is not specified how these fields should be used.

Quoted 49 lines
Quoted 49 lines
Quoted 35 lines
Quoted 6 lines

TUNSETVNETREPORTINGRSS and TUNSETVNETRSS map to
VIRTIO_NET_CTRL_MQ_RSS_CONFIG.

I think we've already had a discussion about this.

Reusing virtio-net uAPI is much better instead of having a tun specific one considering tun may need to support more virtio commands in the future. Or maybe it's the time to introduce a transport for the virtio control virtqueue uAPI in tuntap to avoid inventing new uAPI endlessly.

What's more I see:

struct tun_vnet_rss {
          __u32 hash_types;
          __u16 indirection_table_mask;
          __u16 unclassified_queue;
};
struct tun_vnet_hash {
          bool report;
          bool rss;
          struct tun_vnet_rss common;
          u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
          u16 rss_indirection_table[];
};

As I pointed out in the past, let's just decouple the rss from hash, everything would be much simpler, or you need to explain why you couple this somewhere.

Quoted 5 lines

For example:

1) why is the tun_vnet_hash not part of the uAPI but tun_vnet_rss, or how could userspace know what kind of format it would use for TUNSETVNETREPORTINGRSS?

That was the previous version.

Quoted 2 lines

2) what's the advantages of embedding rss specific stuff into hash report structure

Because the hash types field in struct tun_vnet_rss is used by hash reporting too.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

3) what's the advantages of not using virtio-net uAPI

1. The use cases that don't involve VM will be simplified; programs for such a use case will not need to convert endian or to fill fileds the kernel doesn't care.

Well, virtio_net_hdr is used by packet socket as well. Considering the complexity of designing a new uAPI, it's still better.

This patch series also reuses the datapath, following the prior examples.

Quoted 4 lines

Or maybe you can clarify which field that kernel doesn't care about? In this case TUN/TAP is basically the device datapath, if some of the fields don't make sense, it's a bug of the spec.

reserved, hash_key_length, and hash_key_data.

Quoted 6 lines
Quoted 3 lines

2. It aligns with existing UAPIs that operate in native endian and don't use virtio-net data structures like TUNSETOFFLOAD and TUNSETVNETHDRSZ.

For those two examples, it would be used by guests directly. This is different from RSS stuff.

They are mediated by the VMM, which is no different from RSS.

Quoted 2 lines

With native endian, you need an endian conversation that converts le to native.

That's true, but QEMU does so anyway to validate the configuration, to attach/detach queues, and to share the data structures with userspace RSS implementations. I expect other VMMs will do so too.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

More issues:

1) max_tx_vq is ignored, so do you expect the userspace to intercept this and switch to using TUNSETQUEUE? This seems like a burden as TUN can simply accept it and do the attaching/detaching by itself
2) the rx depends on the indirection table, so userspace need to intercept the indirection and change the rx queue numbers accordingly
3) RSS allows a async TX/RX queue, this is not supported by TUN now, no matter if we decide to let userspace to intercept max_tx_vq or not, we need to implement it first

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

Things would be much more simpler, if kernel can do 1) and 2).

Attaching and detaching queues is not possible for the kernel because it doesn't know what file descriptors that map to queues will be used by the userspace.

The kernel knows, tfile has a queue_index part.

queue_index is set with TUNSETQUEUE so we need the ioctl.

Quoted 20 lines
Quoted 4 lines

The following patch does 2) for QEMU:
https://lore.kernel.org/qemu-devel/[email protected]/

See below point, form the view of the kernel, it's still a queue pair not async TX/RX queue.

Quoted 12 lines

For 3), the patch for QEMU takes the maximum of TX and RX queue numbers to derive the number of queue pairs.

Quoted 7 lines
Quoted 4 lines

We have two ioctls here because VIRTIO_NET_CTRL_MQ_RSS_CONFIG behaves differently depending on whether VIRTIO_NET_F_HASH_REPORT is negotiated or not;

It wouldn't be a problem if you do 1:1 mapping between virtio commands and TUN uAPI, otherwise it should have a bug somewhere.

Quoted 7 lines
Quoted 4 lines

Speaking of 1:1 mapping, it is possible to map VIRTIO_NET_F_HASH_REPORT into another ioctl. It may help add another receive steering algorithm in the future by not requiring two ioctls (TUNSETVNETREPORTING_X and TUNSETVNET_X).

Yes and as I pointed out, virtio_net_hash_config should not be specific to automq, it can work for other steering algorithm as well.

That's not what the virtio spec says, so it will not be 1:1 mapping between virtio commands and TUN uAPI.

Quoted 276 lines
Quoted 31 lines
Quoted 31 lines
Quoted 5 lines

it also enables hash
reporting if the feature is negotiated.

Again, starting from virtio-net command is easier, a strong justification is needed to explain why we choose another for tun/tap.

Quoted 10 lines
Quoted 4 lines

One day we would have tun_select_queue_algorithm_x() we don't have to duplicate the ioctls once again here like TUNSETVNETREPORTINGXYZ

5.1.6.5.6.4 Hash calculation says:

Quoted 4 lines

If VIRTIO_NET_F_HASH_REPORT was negotiated and the device uses
automatic receive steering, the device MUST support a command to configure hash calculation parameters.

Quoted 6 lines
Quoted 3 lines

The driver provides parameters for hash calculation as follows:

class VIRTIO_NET_CTRL_MQ, command VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

VIRTIO_NET_CTRL_MQ_HASH_CONFIG is for automatic receive steering and not for RSS (or other steering algorithms if the spec gets any in the future).

I'm not sure but the spec needs some tweaking. For example, I don't expect there would be a dedicated hash config command for flow filters in the future. I think the reason why spec says like this is that virtio-net only supports automatic receive steering.

Quoted 4 lines
Quoted 1 line

Note that macvtap doesn't implement automatic receive steering.

QEMU advertises VIRTIO_NET_F_CTRL_VQ for macvtap too, so it should have implemented it. I think QEMU with macvtap still compliant to the spec.

Compliant, but automatic traffic steering is the best effort as well.

Nope, TUN/TAP implements a flow cache that can steer tx based on rx. Macvtap simply uses hash here.

Quoted 9 lines

"5.1.6.5.6 Automatic receive steering in multiqueue mode" says:

Quoted 3 lines

After the driver transmitted a packet of a flow on transmitqX, the device SHOULD cause incoming packets for that flow to be steered to receiveqX.

It is "SHOULD", so it is still compliant if the device doesn't properly respect the flow.

Yes, a quality of implementation, or it's impractical to support a correct steering for this device as limited resources and mailious users can do syn flood etc.

Quoted 221 lines
Quoted 221 lines
Quoted 210 lines
Quoted 26 lines
Quoted 26 lines
drivers/net/tun_vnet.h +22 -0
+               if (copy_from_user(&common, argp, sizeof(common)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)common.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(common.hash_types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)common.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 + common.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {
+                       kfree(hash);
Quoted 8 lines

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

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->report = cmd == TUNSETVNETREPORTINGRSS;

At least, if this is the only difference why not simply code this into the ioctl itself other than having a very similar command?

It is what the previous version did. Either is fine I guess; the only practical difference would be the size of the configuration struct is smaller with this approach.

Quoted 162 lines
Quoted 162 lines
drivers/net/tun_vnet.h +84 -9
+               hash->rss = true;
+               hash->common = common;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+       return 0;
+}
+
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash *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->report)
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.hash_types, keys),
+               .value = value
+       };
+}
+
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.hash_types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->common.unclassified_queue % numqueues;
+
+       if (hash->report) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->common.indirection_table_mask;
+
+       return hash->rss_indirection_table[index] % numqueues;
+}
+
    static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                      struct iov_iter *from,
                                      struct virtio_net_hdr *hdr)
@@ -135,15 +268,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 +290,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/uapi/linux/if_tun.h +23 -0
index 980de74724fc..fe4b984d3bbb 100644
@@ -62,6 +62,62 @@
    #define TUNSETCARRIER _IOW('T', 226, int)
    #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHTYPES - ioctl to get supported virtio_net hashing types
+ *
+ * The argument is a pointer to __u32 which will store the supported virtio_net
+ * hashing types.
+ */
+#define TUNGETVNETHASHTYPES _IOR('T', 228, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGAUTOMQ - ioctl to enable automq with hash reporting
+ *
+ * Disable RSS and enable automatic receive steering with hash reporting.
+ *
+ * The argument is a pointer to __u32 that contains a bitmask of hash types
+ * allowed to be reported.
+ *
+ * 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 TUNSETVNETREPORTINGAUTOMQ _IOR('T', 229, __u32)
Quoted 8 lines

include/uapi/linux/if_tun.h line 92

Quoted 5 lines
include/uapi/linux/if_tun.h +5 -0
+
+/**
+ * define TUNSETVNETREPORTINGRSS - ioctl to enable RSS with hash reporting
+ *
+ * Disable automatic receive steering and enable RSS with hash reporting.

This is unnecessary, e.g one day will have select_queue_xyz(), we don't want to say "Disable automatic receive steering and xyz ..."

Quoted 2 lines

It is still something better to be documented as its behavior is somewhat complicated.

This is a hint of uAPI design issue.

Quoted 3 lines

Concretely, this ioctl disables automatic receive steering but doesn't disable steering by eBPF, which is implied by TUN_STEERINGEBPF_FALLBACK.

It would be simpler:

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

1) not having TUN_STEERINGEBPF_FALLBACK
2) the steering algorithm depends on the last uAPI call

What will TUNSETSTEERINGEBPF with NULL mean in that case? It currently switches the steering algorithm to automq.

A stackwise semantic then?

Can you clarify the semantics with an example of a set of ioctls? Perhaps it is an easy way to demonstrate an alternative design idea.

Regards,
Akihiko Odaki

#29 lore raw

Re: [PATCH net-next v12 04/10] tun: Add common virtio-net hash feature code

Quoted 301 lines · Akihiko Odaki
Quoted 230 lines · Jason Wang
Quoted 229 lines · Akihiko Odaki
Quoted 227 lines · Jason Wang
Quoted 209 lines · Akihiko Odaki
Quoted 207 lines · Jason Wang
Quoted 181 lines · Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
    drivers/net/tap.c           |  10 ++-
    drivers/net/tun.c           |  12 +++-
    drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
    include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
    4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
           sock_put(&q->sk);
    }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
    /*
     * Select a queue based on the rxq of the device on which this packet
     * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
           int total;
 
           if (q->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr vnet_hdr;
+               struct virtio_net_hdr_v1_hash vnet_hdr;
 
                   vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-               ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+                                           tap_find_hash, &vnet_hdr);
                   if (ret)
                           return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                   e->rps_rxhash = hash;
    }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
    /* We try to identify a flow through its rxhash. The reason that
     * we do not check rxq no. is because some cards(e.g 82599), chooses
     * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
           ssize_t ret;
 
           if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
+               struct virtio_net_hdr_v1_hash gso = { 0 };
 
                   vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
                   ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
           }
 
           if (vnet_hdr_sz) {
-               struct virtio_net_hdr gso;
+               struct virtio_net_hdr_v1_hash gso;
 
-               ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+                                           skb, tun_find_hash, &gso);
                   if (ret)
                           return ret;
 
drivers/net/tun_vnet.h +35 -0
index 58b9ac7a5fc4..45d0533efc8d 100644
@@ -6,6 +6,17 @@
    #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 {
+       bool report;
+       bool rss;
+       struct tun_vnet_rss common;
+       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 +118,128 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
           }
    }
 
+static inline long tun_vnet_ioctl_gethashtypes(u32 __user *argp)
+{
+       return put_user(VIRTIO_NET_SUPPORTED_HASH_TYPES, argp) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash __rcu **hashp,
+                                         unsigned int cmd,
+                                         void __user *argp)
+{
+       struct tun_vnet_rss common;
+       struct tun_vnet_hash *hash;
+       size_t indirection_table_size;
+       size_t key_size;
+       size_t size;
+
+       switch (cmd) {
+       case TUNSETVNETREPORTINGAUTOMQ:
+               if (get_user(common.hash_types, (u32 __user *)argp))
+                       return -EFAULT;
+
+               if (common.hash_types) {
+                       hash = kzalloc(sizeof(*hash), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+
+                       hash->report = true;
+                       hash->common.hash_types = common.hash_types;
+               } else {
+                       hash = NULL;
+               }
+               break;
+
+       case TUNSETVNETREPORTINGRSS:
+       case TUNSETVNETRSS:

So the above three shows unnecessary design redundancy as well as a burden for the future extension. Why not simply have

1) TUNSETVNET_RSS
2) TUNSETVNET_HASH_REPORT
?

Which maps to

    #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1 (for configurable
receive steering)
    #define VIRTIO_NET_CTRL_MQ_HASH_CONFIG         2 (for configurable

hash calculation)

Quoted 5 lines
Quoted 3 lines

It would be always easier to start with what spec had or at least we need to explain why we choose a different design here or in the changelog to ease our life.

TUNSETVNETREPORTINGAUTOMQ maps to VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

It's not:

VIRTIO_NET_CTRL_MQ_HASH_CONFIG uses:

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

but TUNSETVNETREPORTINGAUTOMQ only accepts hash_types without others:

The others are not present because the spec doesn't specify what to do with them and the kernel doesn't use them either.

Did you mean the hash_key_length and hash_key_data? Note that we have drivers other than the Linux ones as well.

And reserved. Drivers can set whatever to these fields. It is not specified how these fields should be used.

Quoted 49 lines
Quoted 49 lines
Quoted 35 lines
Quoted 6 lines

TUNSETVNETREPORTINGRSS and TUNSETVNETRSS map to
VIRTIO_NET_CTRL_MQ_RSS_CONFIG.

I think we've already had a discussion about this.

Reusing virtio-net uAPI is much better instead of having a tun specific one considering tun may need to support more virtio commands in the future. Or maybe it's the time to introduce a transport for the virtio control virtqueue uAPI in tuntap to avoid inventing new uAPI endlessly.

What's more I see:

struct tun_vnet_rss {
          __u32 hash_types;
          __u16 indirection_table_mask;
          __u16 unclassified_queue;
};
struct tun_vnet_hash {
          bool report;
          bool rss;
          struct tun_vnet_rss common;
          u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
          u16 rss_indirection_table[];
};

As I pointed out in the past, let's just decouple the rss from hash, everything would be much simpler, or you need to explain why you couple this somewhere.

Quoted 5 lines

For example:

1) why is the tun_vnet_hash not part of the uAPI but tun_vnet_rss, or how could userspace know what kind of format it would use for TUNSETVNETREPORTINGRSS?

That was the previous version.

Quoted 2 lines

2) what's the advantages of embedding rss specific stuff into hash report structure

Because the hash types field in struct tun_vnet_rss is used by hash reporting too.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

3) what's the advantages of not using virtio-net uAPI

1. The use cases that don't involve VM will be simplified; programs for such a use case will not need to convert endian or to fill fileds the kernel doesn't care.

Well, virtio_net_hdr is used by packet socket as well. Considering the complexity of designing a new uAPI, it's still better.

Quoted 8 lines

This patch series also reuses the datapath, following the prior examples.

Quoted 4 lines

Or maybe you can clarify which field that kernel doesn't care about? In this case TUN/TAP is basically the device datapath, if some of the fields don't make sense, it's a bug of the spec.

reserved, hash_key_length, and hash_key_data.

I may miss something when RSS is not negotiated, hash_key_length, and hash_key_data is necessary, otherwise how could we calculate the hash?

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

2. It aligns with existing UAPIs that operate in native endian and don't use virtio-net data structures like TUNSETOFFLOAD and TUNSETVNETHDRSZ.

For those two examples, it would be used by guests directly. This is different from RSS stuff.

They are mediated by the VMM, which is no different from RSS.

Not necessarily, e,g Qemu support vDPA control virtqueue passthrough.

Quoted 7 lines
Quoted 3 lines

With native endian, you need an endian conversation that converts le to native.

That's true, but QEMU does so anyway to validate the configuration, to attach/detach queues, and to share the data structures with userspace RSS implementations. I expect other VMMs will do so too.

See above.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

More issues:

1) max_tx_vq is ignored, so do you expect the userspace to intercept this and switch to using TUNSETQUEUE? This seems like a burden as TUN can simply accept it and do the attaching/detaching by itself
2) the rx depends on the indirection table, so userspace need to intercept the indirection and change the rx queue numbers accordingly
3) RSS allows a async TX/RX queue, this is not supported by TUN now, no matter if we decide to let userspace to intercept max_tx_vq or not, we need to implement it first

Quoted 1 line

Things would be much more simpler, if kernel can do 1) and 2).

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Attaching and detaching queues is not possible for the kernel because it doesn't know what file descriptors that map to queues will be used by the userspace.

The kernel knows, tfile has a queue_index part.

queue_index is set with TUNSETQUEUE so we need the ioctl.

queue_index would be reshuffle during attaching/detaching since the netdev core forbids sparse active queue indices.

But I don't see it's an issue since we are talking about introducing new uAPI here.

If it doesn't work, it doesn't change the point, a new uAPI is needed since RSS requires async tx/rx queue numbers, current TUN only allows combined queue pairs.

Quoted 26 lines
Quoted 26 lines
Quoted 5 lines

The following patch does 2) for QEMU:
https://lore.kernel.org/qemu-devel/[email protected]/

See below point, form the view of the kernel, it's still a queue pair not async TX/RX queue.

Quoted 16 lines

For 3), the patch for QEMU takes the maximum of TX and RX queue numbers to derive the number of queue pairs.

Quoted 7 lines
Quoted 4 lines

We have two ioctls here because VIRTIO_NET_CTRL_MQ_RSS_CONFIG behaves differently depending on whether VIRTIO_NET_F_HASH_REPORT is negotiated or not;

It wouldn't be a problem if you do 1:1 mapping between virtio commands and TUN uAPI, otherwise it should have a bug somewhere.

Speaking of 1:1 mapping, it is possible to map VIRTIO_NET_F_HASH_REPORT into another ioctl. It may help add another receive steering algorithm in the future by not requiring two ioctls (TUNSETVNETREPORTING_X and TUNSETVNET_X).

Quoted 5 lines
Quoted 2 lines

Yes and as I pointed out, virtio_net_hash_config should not be specific to automq, it can work for other steering algorithm as well.

That's not what the virtio spec says, so it will not be 1:1 mapping between virtio commands and TUN uAPI.

That's only because the spec only supports RSS and AUTOMQ so far. Or do we expect a new virtio_net_XXX_hash_config for the new steering algorithm?

Quoted 280 lines
Quoted 280 lines
Quoted 32 lines
Quoted 32 lines
Quoted 6 lines

it also enables hash
reporting if the feature is negotiated.

Again, starting from virtio-net command is easier, a strong justification is needed to explain why we choose another for tun/tap.

Quoted 10 lines
Quoted 4 lines

One day we would have tun_select_queue_algorithm_x() we don't have to duplicate the ioctls once again here like TUNSETVNETREPORTINGXYZ

5.1.6.5.6.4 Hash calculation says:

Quoted 4 lines

If VIRTIO_NET_F_HASH_REPORT was negotiated and the device uses
automatic receive steering, the device MUST support a command to configure hash calculation parameters.

Quoted 6 lines
Quoted 3 lines

The driver provides parameters for hash calculation as follows:

class VIRTIO_NET_CTRL_MQ, command VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

VIRTIO_NET_CTRL_MQ_HASH_CONFIG is for automatic receive steering and not for RSS (or other steering algorithms if the spec gets any in the future).

I'm not sure but the spec needs some tweaking. For example, I don't expect there would be a dedicated hash config command for flow filters in the future. I think the reason why spec says like this is that virtio-net only supports automatic receive steering.

Quoted 4 lines
Quoted 1 line

Note that macvtap doesn't implement automatic receive steering.

QEMU advertises VIRTIO_NET_F_CTRL_VQ for macvtap too, so it should have implemented it. I think QEMU with macvtap still compliant to the spec.

Compliant, but automatic traffic steering is the best effort as well.

Nope, TUN/TAP implements a flow cache that can steer tx based on rx. Macvtap simply uses hash here.

Quoted 9 lines

"5.1.6.5.6 Automatic receive steering in multiqueue mode" says:

Quoted 3 lines

After the driver transmitted a packet of a flow on transmitqX, the device SHOULD cause incoming packets for that flow to be steered to receiveqX.

It is "SHOULD", so it is still compliant if the device doesn't properly respect the flow.

Yes, a quality of implementation, or it's impractical to support a correct steering for this device as limited resources and mailious users can do syn flood etc.

Quoted 224 lines
Quoted 223 lines
Quoted 210 lines
Quoted 26 lines
Quoted 26 lines
drivers/net/tun_vnet.h +22 -0
+               if (copy_from_user(&common, argp, sizeof(common)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)common.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(common.hash_types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)common.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 + common.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {
+                       kfree(hash);
Quoted 8 lines

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

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->report = cmd == TUNSETVNETREPORTINGRSS;

At least, if this is the only difference why not simply code this into the ioctl itself other than having a very similar command?

It is what the previous version did. Either is fine I guess; the only practical difference would be the size of the configuration struct is smaller with this approach.

Quoted 162 lines
Quoted 162 lines
drivers/net/tun_vnet.h +84 -9
+               hash->rss = true;
+               hash->common = common;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+       return 0;
+}
+
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash *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->report)
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.hash_types, keys),
+               .value = value
+       };
+}
+
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.hash_types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->common.unclassified_queue % numqueues;
+
+       if (hash->report) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->common.indirection_table_mask;
+
+       return hash->rss_indirection_table[index] % numqueues;
+}
+
    static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                      struct iov_iter *from,
                                      struct virtio_net_hdr *hdr)
@@ -135,15 +268,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 +290,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/uapi/linux/if_tun.h +23 -0
index 980de74724fc..fe4b984d3bbb 100644
@@ -62,6 +62,62 @@
    #define TUNSETCARRIER _IOW('T', 226, int)
    #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHTYPES - ioctl to get supported virtio_net hashing types
+ *
+ * The argument is a pointer to __u32 which will store the supported virtio_net
+ * hashing types.
+ */
+#define TUNGETVNETHASHTYPES _IOR('T', 228, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGAUTOMQ - ioctl to enable automq with hash reporting
+ *
+ * Disable RSS and enable automatic receive steering with hash reporting.
+ *
+ * The argument is a pointer to __u32 that contains a bitmask of hash types
+ * allowed to be reported.
+ *
+ * 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 TUNSETVNETREPORTINGAUTOMQ _IOR('T', 229, __u32)
Quoted 8 lines

include/uapi/linux/if_tun.h line 92

Quoted 5 lines
include/uapi/linux/if_tun.h +5 -0
+
+/**
+ * define TUNSETVNETREPORTINGRSS - ioctl to enable RSS with hash reporting
+ *
+ * Disable automatic receive steering and enable RSS with hash reporting.

This is unnecessary, e.g one day will have select_queue_xyz(), we don't want to say "Disable automatic receive steering and xyz ..."

Quoted 2 lines

It is still something better to be documented as its behavior is somewhat complicated.

This is a hint of uAPI design issue.

Quoted 3 lines

Concretely, this ioctl disables automatic receive steering but doesn't disable steering by eBPF, which is implied by TUN_STEERINGEBPF_FALLBACK.

It would be simpler:

1) not having TUN_STEERINGEBPF_FALLBACK
2) the steering algorithm depends on the last uAPI call

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

What will TUNSETSTEERINGEBPF with NULL mean in that case? It currently switches the steering algorithm to automq.

A stackwise semantic then?

Can you clarify the semantics with an example of a set of ioctls? Perhaps it is an easy way to demonstrate an alternative design idea.

Consider user do:

1) TUNSETQUEUE /* enable automq, push */
2) TUNSETSTEERINGEBPF /* enable steering ebp, push */
3) TUNSETETTERINGEBPF to NULL /* disable steering ebpf, pop */

Automq is in the stack top, so TUN will use that.

Thanks

Quoted 4 lines

Regards,
Akihiko Odaki

#30 lore raw

Re: [PATCH net-next v12 04/10] tun: Add common virtio-net hash feature code

Quoted 305 lines · Jason Wang
Quoted 304 lines · Akihiko Odaki
Quoted 230 lines · Jason Wang
Quoted 229 lines · Akihiko Odaki
Quoted 227 lines · Jason Wang
Quoted 209 lines · Akihiko Odaki
Quoted 207 lines · Jason Wang
Quoted 181 lines · Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
     drivers/net/tap.c           |  10 ++-
     drivers/net/tun.c           |  12 +++-
     drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
     include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
     4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
            sock_put(&q->sk);
     }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
     /*
      * Select a queue based on the rxq of the device on which this packet
      * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
            int total;
 
            if (q->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr vnet_hdr;
+               struct virtio_net_hdr_v1_hash vnet_hdr;
 
                    vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-               ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+                                           tap_find_hash, &vnet_hdr);
                    if (ret)
                            return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                    e->rps_rxhash = hash;
     }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
     /* We try to identify a flow through its rxhash. The reason that
      * we do not check rxq no. is because some cards(e.g 82599), chooses
      * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
            ssize_t ret;
 
            if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
+               struct virtio_net_hdr_v1_hash gso = { 0 };
 
                    vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
                    ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
            }
 
            if (vnet_hdr_sz) {
-               struct virtio_net_hdr gso;
+               struct virtio_net_hdr_v1_hash gso;
 
-               ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+                                           skb, tun_find_hash, &gso);
                    if (ret)
                            return ret;
 
drivers/net/tun_vnet.h +35 -0
index 58b9ac7a5fc4..45d0533efc8d 100644
@@ -6,6 +6,17 @@
     #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 {
+       bool report;
+       bool rss;
+       struct tun_vnet_rss common;
+       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 +118,128 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
            }
     }
 
+static inline long tun_vnet_ioctl_gethashtypes(u32 __user *argp)
+{
+       return put_user(VIRTIO_NET_SUPPORTED_HASH_TYPES, argp) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash __rcu **hashp,
+                                         unsigned int cmd,
+                                         void __user *argp)
+{
+       struct tun_vnet_rss common;
+       struct tun_vnet_hash *hash;
+       size_t indirection_table_size;
+       size_t key_size;
+       size_t size;
+
+       switch (cmd) {
+       case TUNSETVNETREPORTINGAUTOMQ:
+               if (get_user(common.hash_types, (u32 __user *)argp))
+                       return -EFAULT;
+
+               if (common.hash_types) {
+                       hash = kzalloc(sizeof(*hash), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+
+                       hash->report = true;
+                       hash->common.hash_types = common.hash_types;
+               } else {
+                       hash = NULL;
+               }
+               break;
+
+       case TUNSETVNETREPORTINGRSS:
+       case TUNSETVNETRSS:

So the above three shows unnecessary design redundancy as well as a burden for the future extension. Why not simply have

1) TUNSETVNET_RSS
2) TUNSETVNET_HASH_REPORT
?

Which maps to

     #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1 (for configurable
receive steering)
     #define VIRTIO_NET_CTRL_MQ_HASH_CONFIG         2 (for configurable

hash calculation)

Quoted 5 lines
Quoted 3 lines

It would be always easier to start with what spec had or at least we need to explain why we choose a different design here or in the changelog to ease our life.

TUNSETVNETREPORTINGAUTOMQ maps to VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

It's not:

VIRTIO_NET_CTRL_MQ_HASH_CONFIG uses:

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

but TUNSETVNETREPORTINGAUTOMQ only accepts hash_types without others:

The others are not present because the spec doesn't specify what to do with them and the kernel doesn't use them either.

Did you mean the hash_key_length and hash_key_data? Note that we have drivers other than the Linux ones as well.

And reserved. Drivers can set whatever to these fields. It is not specified how these fields should be used.

Quoted 49 lines
Quoted 49 lines
Quoted 35 lines
Quoted 6 lines

TUNSETVNETREPORTINGRSS and TUNSETVNETRSS map to
VIRTIO_NET_CTRL_MQ_RSS_CONFIG.

I think we've already had a discussion about this.

Reusing virtio-net uAPI is much better instead of having a tun specific one considering tun may need to support more virtio commands in the future. Or maybe it's the time to introduce a transport for the virtio control virtqueue uAPI in tuntap to avoid inventing new uAPI endlessly.

What's more I see:

struct tun_vnet_rss {
           __u32 hash_types;
           __u16 indirection_table_mask;
           __u16 unclassified_queue;
};
struct tun_vnet_hash {
           bool report;
           bool rss;
           struct tun_vnet_rss common;
           u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
           u16 rss_indirection_table[];
};

As I pointed out in the past, let's just decouple the rss from hash, everything would be much simpler, or you need to explain why you couple this somewhere.

Quoted 5 lines

For example:

1) why is the tun_vnet_hash not part of the uAPI but tun_vnet_rss, or how could userspace know what kind of format it would use for TUNSETVNETREPORTINGRSS?

That was the previous version.

Quoted 2 lines

2) what's the advantages of embedding rss specific stuff into hash report structure

Because the hash types field in struct tun_vnet_rss is used by hash reporting too.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

3) what's the advantages of not using virtio-net uAPI

1. The use cases that don't involve VM will be simplified; programs for such a use case will not need to convert endian or to fill fileds the kernel doesn't care.

Well, virtio_net_hdr is used by packet socket as well. Considering the complexity of designing a new uAPI, it's still better.

This patch series also reuses the datapath, following the prior examples.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Or maybe you can clarify which field that kernel doesn't care about? In this case TUN/TAP is basically the device datapath, if some of the fields don't make sense, it's a bug of the spec.

reserved, hash_key_length, and hash_key_data.

I may miss something when RSS is not negotiated, hash_key_length, and hash_key_data is necessary, otherwise how could we calculate the hash?

I was not clear that I was referring to the fields of struct virtio_net_hash_config. struct virtio_net_rss_config provides hash_key_length and hash_key_data for RSS.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

2. It aligns with existing UAPIs that operate in native endian and don't use virtio-net data structures like TUNSETOFFLOAD and TUNSETVNETHDRSZ.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

For those two examples, it would be used by guests directly. This is different from RSS stuff.

They are mediated by the VMM, which is no different from RSS.

Not necessarily, e,g Qemu support vDPA control virtqueue passthrough.

TUNSETOFFLOAD and TUNSETVNETHDRSZ are not used with vDPA, are they?

Quoted 33 lines
Quoted 8 lines
Quoted 4 lines

With native endian, you need an endian conversation that converts le to native.

That's true, but QEMU does so anyway to validate the configuration, to attach/detach queues, and to share the data structures with userspace RSS implementations. I expect other VMMs will do so too.

See above.

Quoted 22 lines
Quoted 21 lines
Quoted 14 lines
Quoted 14 lines

More issues:

1) max_tx_vq is ignored, so do you expect the userspace to intercept this and switch to using TUNSETQUEUE? This seems like a burden as TUN can simply accept it and do the attaching/detaching by itself
2) the rx depends on the indirection table, so userspace need to intercept the indirection and change the rx queue numbers accordingly
3) RSS allows a async TX/RX queue, this is not supported by TUN now, no matter if we decide to let userspace to intercept max_tx_vq or not, we need to implement it first

Quoted 5 lines
Quoted 1 line
Quoted 1 line

Things would be much more simpler, if kernel can do 1) and 2).

Attaching and detaching queues is not possible for the kernel because it doesn't know what file descriptors that map to queues will be used by the userspace.

The kernel knows, tfile has a queue_index part.

Quoted 7 lines
Quoted 1 line

queue_index is set with TUNSETQUEUE so we need the ioctl.

queue_index would be reshuffle during attaching/detaching since the netdev core forbids sparse active queue indices.

But I don't see it's an issue since we are talking about introducing new uAPI here.

If queue_index is not usable, how can we pick queues for the new UAPI?

Quoted 4 lines

If it doesn't work, it doesn't change the point, a new uAPI is needed since RSS requires async tx/rx queue numbers, current TUN only allows combined queue pairs.

TUN can have more tx/rx queues than the guest requests, so a VMM can take the maximum of TX and RX queue numbers to derive the number of queue pairs when the guest reqeusts async tx/rx queue numbers.

Quoted 30 lines
Quoted 30 lines
Quoted 29 lines
Quoted 6 lines

The following patch does 2) for QEMU:
https://lore.kernel.org/qemu-devel/[email protected]/

See below point, form the view of the kernel, it's still a queue pair not async TX/RX queue.

Quoted 9 lines

For 3), the patch for QEMU takes the maximum of TX and RX queue numbers to derive the number of queue pairs.

Quoted 5 lines
Quoted 4 lines

We have two ioctls here because VIRTIO_NET_CTRL_MQ_RSS_CONFIG behaves differently depending on whether VIRTIO_NET_F_HASH_REPORT is negotiated or not;

Quoted 7 lines
Quoted 2 lines

It wouldn't be a problem if you do 1:1 mapping between virtio commands and TUN uAPI, otherwise it should have a bug somewhere.

Speaking of 1:1 mapping, it is possible to map VIRTIO_NET_F_HASH_REPORT into another ioctl. It may help add another receive steering algorithm in the future by not requiring two ioctls (TUNSETVNETREPORTING_X and TUNSETVNET_X).

Yes and as I pointed out, virtio_net_hash_config should not be specific to automq, it can work for other steering algorithm as well.

Quoted 6 lines
Quoted 2 lines

That's not what the virtio spec says, so it will not be 1:1 mapping between virtio commands and TUN uAPI.

That's only because the spec only supports RSS and AUTOMQ so far. Or do we expect a new virtio_net_XXX_hash_config for the new steering algorithm?

No. RSS only needs struct virtio_net_rss_config for hash configuration, and a new steering algorithm will only need one struct. For example, if the spec is to gain siphash, we will need to add struct virtio_net_siphash_config.

struct virtio_net_hash_config is only for automq.

Quoted 289 lines
Quoted 281 lines
Quoted 281 lines
Quoted 33 lines
Quoted 33 lines
Quoted 7 lines

it also enables hash
reporting if the feature is negotiated.

Again, starting from virtio-net command is easier, a strong justification is needed to explain why we choose another for tun/tap.

Quoted 10 lines
Quoted 4 lines

One day we would have tun_select_queue_algorithm_x() we don't have to duplicate the ioctls once again here like TUNSETVNETREPORTINGXYZ

5.1.6.5.6.4 Hash calculation says:

Quoted 4 lines

If VIRTIO_NET_F_HASH_REPORT was negotiated and the device uses
automatic receive steering, the device MUST support a command to configure hash calculation parameters.

Quoted 6 lines
Quoted 3 lines

The driver provides parameters for hash calculation as follows:

class VIRTIO_NET_CTRL_MQ, command VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

VIRTIO_NET_CTRL_MQ_HASH_CONFIG is for automatic receive steering and not for RSS (or other steering algorithms if the spec gets any in the future).

I'm not sure but the spec needs some tweaking. For example, I don't expect there would be a dedicated hash config command for flow filters in the future. I think the reason why spec says like this is that virtio-net only supports automatic receive steering.

Quoted 4 lines
Quoted 1 line

Note that macvtap doesn't implement automatic receive steering.

QEMU advertises VIRTIO_NET_F_CTRL_VQ for macvtap too, so it should have implemented it. I think QEMU with macvtap still compliant to the spec.

Compliant, but automatic traffic steering is the best effort as well.

Nope, TUN/TAP implements a flow cache that can steer tx based on rx. Macvtap simply uses hash here.

Quoted 9 lines

"5.1.6.5.6 Automatic receive steering in multiqueue mode" says:

Quoted 3 lines

After the driver transmitted a packet of a flow on transmitqX, the device SHOULD cause incoming packets for that flow to be steered to receiveqX.

It is "SHOULD", so it is still compliant if the device doesn't properly respect the flow.

Yes, a quality of implementation, or it's impractical to support a correct steering for this device as limited resources and mailious users can do syn flood etc.

Quoted 224 lines
Quoted 223 lines
Quoted 210 lines
Quoted 26 lines
Quoted 26 lines
drivers/net/tun_vnet.h +22 -0
+               if (copy_from_user(&common, argp, sizeof(common)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)common.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(common.hash_types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)common.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 + common.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {
+                       kfree(hash);
Quoted 8 lines

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

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->report = cmd == TUNSETVNETREPORTINGRSS;

At least, if this is the only difference why not simply code this into the ioctl itself other than having a very similar command?

It is what the previous version did. Either is fine I guess; the only practical difference would be the size of the configuration struct is smaller with this approach.

Quoted 162 lines
Quoted 162 lines
drivers/net/tun_vnet.h +84 -9
+               hash->rss = true;
+               hash->common = common;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+       return 0;
+}
+
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash *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->report)
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.hash_types, keys),
+               .value = value
+       };
+}
+
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.hash_types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->common.unclassified_queue % numqueues;
+
+       if (hash->report) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->common.indirection_table_mask;
+
+       return hash->rss_indirection_table[index] % numqueues;
+}
+
     static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                       struct iov_iter *from,
                                       struct virtio_net_hdr *hdr)
@@ -135,15 +268,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 +290,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/uapi/linux/if_tun.h +23 -0
index 980de74724fc..fe4b984d3bbb 100644
@@ -62,6 +62,62 @@
     #define TUNSETCARRIER _IOW('T', 226, int)
     #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHTYPES - ioctl to get supported virtio_net hashing types
+ *
+ * The argument is a pointer to __u32 which will store the supported virtio_net
+ * hashing types.
+ */
+#define TUNGETVNETHASHTYPES _IOR('T', 228, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGAUTOMQ - ioctl to enable automq with hash reporting
+ *
+ * Disable RSS and enable automatic receive steering with hash reporting.
+ *
+ * The argument is a pointer to __u32 that contains a bitmask of hash types
+ * allowed to be reported.
+ *
+ * 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 TUNSETVNETREPORTINGAUTOMQ _IOR('T', 229, __u32)
Quoted 8 lines

include/uapi/linux/if_tun.h line 92

Quoted 5 lines
include/uapi/linux/if_tun.h +5 -0
+
+/**
+ * define TUNSETVNETREPORTINGRSS - ioctl to enable RSS with hash reporting
+ *
+ * Disable automatic receive steering and enable RSS with hash reporting.

This is unnecessary, e.g one day will have select_queue_xyz(), we don't want to say "Disable automatic receive steering and xyz ..."

Quoted 2 lines

It is still something better to be documented as its behavior is somewhat complicated.

This is a hint of uAPI design issue.

Quoted 3 lines

Concretely, this ioctl disables automatic receive steering but doesn't disable steering by eBPF, which is implied by TUN_STEERINGEBPF_FALLBACK.

It would be simpler:

1) not having TUN_STEERINGEBPF_FALLBACK
2) the steering algorithm depends on the last uAPI call

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

What will TUNSETSTEERINGEBPF with NULL mean in that case? It currently switches the steering algorithm to automq.

A stackwise semantic then?

Can you clarify the semantics with an example of a set of ioctls? Perhaps it is an easy way to demonstrate an alternative design idea.

Quoted 7 lines

Consider user do:

1) TUNSETQUEUE /* enable automq, push */
2) TUNSETSTEERINGEBPF /* enable steering ebp, push */
3) TUNSETETTERINGEBPF to NULL /* disable steering ebpf, pop */

Automq is in the stack top, so TUN will use that.

In that case, what will happen if the user does:

1) TUNSETQUEUE
2) TUNSETVNETRSS
3) TUNSETETTERINGEBPF to NULL

Regards,
Akihiko Odaki

#31 lore raw

Re: [PATCH net-next v12 04/10] tun: Add common virtio-net hash feature code

Quoted 326 lines · Akihiko Odaki
Quoted 305 lines · Jason Wang
Quoted 304 lines · Akihiko Odaki
Quoted 230 lines · Jason Wang
Quoted 229 lines · Akihiko Odaki
Quoted 227 lines · Jason Wang
Quoted 209 lines · Akihiko Odaki
Quoted 207 lines · Jason Wang
Quoted 181 lines · Akihiko Odaki

Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches.

Added Features
==============

Hash reporting
--------------

Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation.

Receive Side Scaling (RSS)
--------------------------

RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program.

Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs.

Added ioctls
============

They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace.

TUNGETVNETHASHTYPES
-------------------

This ioctl tells supported hash types. It is useful to check if a VM can be migrated to the current host.

TUNSETVNETREPORTINGAUTOMQ, TUNSETVNETREPORTINGRSS, and TUNSETVNETRSS
--------------------------------------------------------------------

These ioctls configures a steering algorithm and, if needed, hash reporting.

---
     drivers/net/tap.c           |  10 ++-
     drivers/net/tun.c           |  12 +++-
     drivers/net/tun_vnet.h      | 165 +++++++++++++++++++++++++++++++++++++++++---
     include/uapi/linux/if_tun.h |  71 +++++++++++++++++++
     4 files changed, 244 insertions(+), 14 deletions(-)
drivers/net/tap.c +8 -2
index d4ece538f1b2..25c60ff2d3f2 100644
@@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q)
            sock_put(&q->sk);
     }
 
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
     /*
      * Select a queue based on the rxq of the device on which this packet
      * arrived. If the incoming device is not mq, calculate a flow hash
@@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q,
            int total;
 
            if (q->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr vnet_hdr;
+               struct virtio_net_hdr_v1_hash vnet_hdr;
 
                    vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
-               ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb,
+                                           tap_find_hash, &vnet_hdr);
                    if (ret)
                            return ret;
 
drivers/net/tun.c +9 -3
index 9133ab9ed3f5..03d47799e9bd 100644
@@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                    e->rps_rxhash = hash;
     }
 
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return NULL;
+}
+
     /* We try to identify a flow through its rxhash. The reason that
      * we do not check rxq no. is because some cards(e.g 82599), chooses
      * the rxq based on the txq where the last packet of the flow comes. As
@@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
            ssize_t ret;
 
            if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
+               struct virtio_net_hdr_v1_hash gso = { 0 };
 
                    vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
                    ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun,
            }
 
            if (vnet_hdr_sz) {
-               struct virtio_net_hdr gso;
+               struct virtio_net_hdr_v1_hash gso;
 
-               ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
+               ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev,
+                                           skb, tun_find_hash, &gso);
                    if (ret)
                            return ret;
 
drivers/net/tun_vnet.h +35 -0
index 58b9ac7a5fc4..45d0533efc8d 100644
@@ -6,6 +6,17 @@
     #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 {
+       bool report;
+       bool rss;
+       struct tun_vnet_rss common;
+       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 +118,128 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags,
            }
     }
 
+static inline long tun_vnet_ioctl_gethashtypes(u32 __user *argp)
+{
+       return put_user(VIRTIO_NET_SUPPORTED_HASH_TYPES, argp) ? -EFAULT : 0;
+}
+
+static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash __rcu **hashp,
+                                         unsigned int cmd,
+                                         void __user *argp)
+{
+       struct tun_vnet_rss common;
+       struct tun_vnet_hash *hash;
+       size_t indirection_table_size;
+       size_t key_size;
+       size_t size;
+
+       switch (cmd) {
+       case TUNSETVNETREPORTINGAUTOMQ:
+               if (get_user(common.hash_types, (u32 __user *)argp))
+                       return -EFAULT;
+
+               if (common.hash_types) {
+                       hash = kzalloc(sizeof(*hash), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+
+                       hash->report = true;
+                       hash->common.hash_types = common.hash_types;
+               } else {
+                       hash = NULL;
+               }
+               break;
+
+       case TUNSETVNETREPORTINGRSS:
+       case TUNSETVNETRSS:

So the above three shows unnecessary design redundancy as well as a burden for the future extension. Why not simply have

1) TUNSETVNET_RSS
2) TUNSETVNET_HASH_REPORT
?

Which maps to

     #define VIRTIO_NET_CTRL_MQ_RSS_CONFIG          1 (for configurable
receive steering)
     #define VIRTIO_NET_CTRL_MQ_HASH_CONFIG         2 (for configurable

hash calculation)

Quoted 5 lines
Quoted 3 lines

It would be always easier to start with what spec had or at least we need to explain why we choose a different design here or in the changelog to ease our life.

TUNSETVNETREPORTINGAUTOMQ maps to VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

It's not:

VIRTIO_NET_CTRL_MQ_HASH_CONFIG uses:

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

but TUNSETVNETREPORTINGAUTOMQ only accepts hash_types without others:

The others are not present because the spec doesn't specify what to do with them and the kernel doesn't use them either.

Did you mean the hash_key_length and hash_key_data? Note that we have drivers other than the Linux ones as well.

And reserved. Drivers can set whatever to these fields. It is not specified how these fields should be used.

Quoted 49 lines
Quoted 49 lines
Quoted 35 lines
Quoted 6 lines

TUNSETVNETREPORTINGRSS and TUNSETVNETRSS map to
VIRTIO_NET_CTRL_MQ_RSS_CONFIG.

I think we've already had a discussion about this.

Reusing virtio-net uAPI is much better instead of having a tun specific one considering tun may need to support more virtio commands in the future. Or maybe it's the time to introduce a transport for the virtio control virtqueue uAPI in tuntap to avoid inventing new uAPI endlessly.

What's more I see:

struct tun_vnet_rss {
           __u32 hash_types;
           __u16 indirection_table_mask;
           __u16 unclassified_queue;
};
struct tun_vnet_hash {
           bool report;
           bool rss;
           struct tun_vnet_rss common;
           u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
           u16 rss_indirection_table[];
};

As I pointed out in the past, let's just decouple the rss from hash, everything would be much simpler, or you need to explain why you couple this somewhere.

Quoted 5 lines

For example:

1) why is the tun_vnet_hash not part of the uAPI but tun_vnet_rss, or how could userspace know what kind of format it would use for TUNSETVNETREPORTINGRSS?

That was the previous version.

Quoted 2 lines

2) what's the advantages of embedding rss specific stuff into hash report structure

Because the hash types field in struct tun_vnet_rss is used by hash reporting too.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

3) what's the advantages of not using virtio-net uAPI

1. The use cases that don't involve VM will be simplified; programs for such a use case will not need to convert endian or to fill fileds the kernel doesn't care.

Well, virtio_net_hdr is used by packet socket as well. Considering the complexity of designing a new uAPI, it's still better.

This patch series also reuses the datapath, following the prior examples.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Or maybe you can clarify which field that kernel doesn't care about? In this case TUN/TAP is basically the device datapath, if some of the fields don't make sense, it's a bug of the spec.

reserved, hash_key_length, and hash_key_data.

I may miss something when RSS is not negotiated, hash_key_length, and hash_key_data is necessary, otherwise how could we calculate the hash?

I was not clear that I was referring to the fields of struct virtio_net_hash_config. struct virtio_net_rss_config provides hash_key_length and hash_key_data for RSS.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

2. It aligns with existing UAPIs that operate in native endian and don't use virtio-net data structures like TUNSETOFFLOAD and TUNSETVNETHDRSZ.

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

For those two examples, it would be used by guests directly. This is different from RSS stuff.

They are mediated by the VMM, which is no different from RSS.

Not necessarily, e,g Qemu support vDPA control virtqueue passthrough.

TUNSETOFFLOAD and TUNSETVNETHDRSZ are not used with vDPA, are they?

They aren't but I mean hash/rss config.

Quoted 36 lines
Quoted 36 lines
Quoted 9 lines
Quoted 5 lines

With native endian, you need an endian conversation that converts le to native.

That's true, but QEMU does so anyway to validate the configuration, to attach/detach queues, and to share the data structures with userspace RSS implementations. I expect other VMMs will do so too.

See above.

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

More issues:

1) max_tx_vq is ignored, so do you expect the userspace to intercept this and switch to using TUNSETQUEUE? This seems like a burden as TUN can simply accept it and do the attaching/detaching by itself
2) the rx depends on the indirection table, so userspace need to intercept the indirection and change the rx queue numbers accordingly
3) RSS allows a async TX/RX queue, this is not supported by TUN now, no matter if we decide to let userspace to intercept max_tx_vq or not, we need to implement it first

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

Things would be much more simpler, if kernel can do 1) and 2).

Attaching and detaching queues is not possible for the kernel because it doesn't know what file descriptors that map to queues will be used by the userspace.

The kernel knows, tfile has a queue_index part.

queue_index is set with TUNSETQUEUE so we need the ioctl.

Quoted 7 lines
Quoted 5 lines

queue_index would be reshuffle during attaching/detaching since the netdev core forbids sparse active queue indices.

But I don't see it's an issue since we are talking about introducing new uAPI here.

If queue_index is not usable, how can we pick queues for the new UAPI?

But queue_index is contiguous, so we can use the first several ones.

Quoted 9 lines
Quoted 5 lines

If it doesn't work, it doesn't change the point, a new uAPI is needed since RSS requires async tx/rx queue numbers, current TUN only allows combined queue pairs.

TUN can have more tx/rx queues than the guest requests, so a VMM can take the maximum of TX and RX queue numbers to derive the number of queue pairs when the guest reqeusts async tx/rx queue numbers.

I think we are talking about the same issue, I mean anyhow you need a new uAPI to tell the kernel it needs to start async tx/rx. This is not supported in the current kernel.

Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 23 lines
Quoted 7 lines

The following patch does 2) for QEMU:
https://lore.kernel.org/qemu-devel/[email protected]/

See below point, form the view of the kernel, it's still a queue pair not async TX/RX queue.

Quoted 12 lines

For 3), the patch for QEMU takes the maximum of TX and RX queue numbers to derive the number of queue pairs.

Quoted 7 lines
Quoted 4 lines

We have two ioctls here because VIRTIO_NET_CTRL_MQ_RSS_CONFIG behaves differently depending on whether VIRTIO_NET_F_HASH_REPORT is negotiated or not;

It wouldn't be a problem if you do 1:1 mapping between virtio commands and TUN uAPI, otherwise it should have a bug somewhere.

Quoted 7 lines
Quoted 4 lines

Speaking of 1:1 mapping, it is possible to map VIRTIO_NET_F_HASH_REPORT into another ioctl. It may help add another receive steering algorithm in the future by not requiring two ioctls (TUNSETVNETREPORTING_X and TUNSETVNET_X).

Yes and as I pointed out, virtio_net_hash_config should not be specific to automq, it can work for other steering algorithm as well.

That's not what the virtio spec says, so it will not be 1:1 mapping between virtio commands and TUN uAPI.

Quoted 8 lines
Quoted 3 lines

That's only because the spec only supports RSS and AUTOMQ so far. Or do we expect a new virtio_net_XXX_hash_config for the new steering algorithm?

No. RSS only needs struct virtio_net_rss_config for hash configuration, and a new steering algorithm will only need one struct. For example, if the spec is to gain siphash, we will need to add struct virtio_net_siphash_config.

I think there're several things. We should decouple hash from the steering.

New steering algorithm doesn't necessarily hash or new hash algorithm.

Quoted 298 lines

struct virtio_net_hash_config is only for automq.

Quoted 295 lines
Quoted 281 lines
Quoted 281 lines
Quoted 33 lines
Quoted 33 lines
Quoted 7 lines

it also enables hash
reporting if the feature is negotiated.

Again, starting from virtio-net command is easier, a strong justification is needed to explain why we choose another for tun/tap.

Quoted 10 lines
Quoted 4 lines

One day we would have tun_select_queue_algorithm_x() we don't have to duplicate the ioctls once again here like TUNSETVNETREPORTINGXYZ

5.1.6.5.6.4 Hash calculation says:

Quoted 4 lines

If VIRTIO_NET_F_HASH_REPORT was negotiated and the device uses
automatic receive steering, the device MUST support a command to configure hash calculation parameters.

Quoted 6 lines
Quoted 3 lines

The driver provides parameters for hash calculation as follows:

class VIRTIO_NET_CTRL_MQ, command VIRTIO_NET_CTRL_MQ_HASH_CONFIG.

VIRTIO_NET_CTRL_MQ_HASH_CONFIG is for automatic receive steering and not for RSS (or other steering algorithms if the spec gets any in the future).

I'm not sure but the spec needs some tweaking. For example, I don't expect there would be a dedicated hash config command for flow filters in the future. I think the reason why spec says like this is that virtio-net only supports automatic receive steering.

Quoted 4 lines
Quoted 1 line

Note that macvtap doesn't implement automatic receive steering.

QEMU advertises VIRTIO_NET_F_CTRL_VQ for macvtap too, so it should have implemented it. I think QEMU with macvtap still compliant to the spec.

Compliant, but automatic traffic steering is the best effort as well.

Nope, TUN/TAP implements a flow cache that can steer tx based on rx. Macvtap simply uses hash here.

Quoted 9 lines

"5.1.6.5.6 Automatic receive steering in multiqueue mode" says:

Quoted 3 lines

After the driver transmitted a packet of a flow on transmitqX, the device SHOULD cause incoming packets for that flow to be steered to receiveqX.

It is "SHOULD", so it is still compliant if the device doesn't properly respect the flow.

Yes, a quality of implementation, or it's impractical to support a correct steering for this device as limited resources and mailious users can do syn flood etc.

Quoted 224 lines
Quoted 223 lines
Quoted 210 lines
Quoted 26 lines
Quoted 26 lines
drivers/net/tun_vnet.h +22 -0
+               if (copy_from_user(&common, argp, sizeof(common)))
+                       return -EFAULT;
+               argp = (struct tun_vnet_rss __user *)argp + 1;
+
+               indirection_table_size = ((size_t)common.indirection_table_mask + 1) * 2;
+               key_size = virtio_net_hash_key_length(common.hash_types);
+               size = struct_size(hash, rss_indirection_table,
+                                  (size_t)common.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 + common.indirection_table_mask + 1;
+
+               if (copy_from_user(hash->rss_key, argp, key_size)) {
+                       kfree(hash);
Quoted 8 lines

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

Quoted 5 lines
drivers/net/tun_vnet.h +5 -0
+                       return -EFAULT;
+               }
+
+               virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+               hash->report = cmd == TUNSETVNETREPORTINGRSS;

At least, if this is the only difference why not simply code this into the ioctl itself other than having a very similar command?

It is what the previous version did. Either is fine I guess; the only practical difference would be the size of the configuration struct is smaller with this approach.

Quoted 162 lines
Quoted 162 lines
drivers/net/tun_vnet.h +84 -9
+               hash->rss = true;
+               hash->common = common;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
+       return 0;
+}
+
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash *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->report)
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->common.hash_types, keys),
+               .value = value
+       };
+}
+
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.hash_types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->common.unclassified_queue % numqueues;
+
+       if (hash->report) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->common.indirection_table_mask;
+
+       return hash->rss_indirection_table[index] % numqueues;
+}
+
     static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                       struct iov_iter *from,
                                       struct virtio_net_hdr *hdr)
@@ -135,15 +268,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 +290,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/uapi/linux/if_tun.h +23 -0
index 980de74724fc..fe4b984d3bbb 100644
@@ -62,6 +62,62 @@
     #define TUNSETCARRIER _IOW('T', 226, int)
     #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHTYPES - ioctl to get supported virtio_net hashing types
+ *
+ * The argument is a pointer to __u32 which will store the supported virtio_net
+ * hashing types.
+ */
+#define TUNGETVNETHASHTYPES _IOR('T', 228, __u32)
+
+/**
+ * define TUNSETVNETREPORTINGAUTOMQ - ioctl to enable automq with hash reporting
+ *
+ * Disable RSS and enable automatic receive steering with hash reporting.
+ *
+ * The argument is a pointer to __u32 that contains a bitmask of hash types
+ * allowed to be reported.
+ *
+ * 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 TUNSETVNETREPORTINGAUTOMQ _IOR('T', 229, __u32)
Quoted 8 lines

include/uapi/linux/if_tun.h line 92

Quoted 5 lines
include/uapi/linux/if_tun.h +5 -0
+
+/**
+ * define TUNSETVNETREPORTINGRSS - ioctl to enable RSS with hash reporting
+ *
+ * Disable automatic receive steering and enable RSS with hash reporting.

This is unnecessary, e.g one day will have select_queue_xyz(), we don't want to say "Disable automatic receive steering and xyz ..."

Quoted 2 lines

It is still something better to be documented as its behavior is somewhat complicated.

This is a hint of uAPI design issue.

Quoted 3 lines

Concretely, this ioctl disables automatic receive steering but doesn't disable steering by eBPF, which is implied by TUN_STEERINGEBPF_FALLBACK.

It would be simpler:

1) not having TUN_STEERINGEBPF_FALLBACK
2) the steering algorithm depends on the last uAPI call

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

What will TUNSETSTEERINGEBPF with NULL mean in that case? It currently switches the steering algorithm to automq.

A stackwise semantic then?

Can you clarify the semantics with an example of a set of ioctls? Perhaps it is an easy way to demonstrate an alternative design idea.

Consider user do:

1) TUNSETQUEUE /* enable automq, push */
2) TUNSETSTEERINGEBPF /* enable steering ebp, push */
3) TUNSETETTERINGEBPF to NULL /* disable steering ebpf, pop */

Quoted 7 lines
Quoted 1 line

Automq is in the stack top, so TUN will use that.

In that case, what will happen if the user does:

1) TUNSETQUEUE
2) TUNSETVNETRSS
3) TUNSETETTERINGEBPF to NULL

RSS I guess since 3) will cause an error?

Or we can introduce new uAPI for setting a exclusively steering algorithm.

Thanks

Quoted 4 lines

Regards,
Akihiko Odaki

#32 lore raw

PATCH v12 5/10 tun: Introduce virtio-net hash feature

In reply to Akihiko Odaki

Add ioctls and storage required for the virtio-net hash feature to TUN.

---
 drivers/net/Kconfig    |  1 +
 drivers/net/tun.c      | 56 ++++++++++++++++++++++++++++++++++++++++++--------
 include/linux/skbuff.h |  3 +++
 net/core/skbuff.c      |  4 ++++
 4 files changed, 56 insertions(+), 8 deletions(-)
drivers/net/Kconfig +1 -0
index 1fd5acdc73c6..aecfd244dd83 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/tun.c +48 -8

119 lines not on this page · raw message

include/linux/skbuff.h +3 -0
index bb2b751d274a..cdd793f1c360 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 */
 };
net/core/skbuff.c +4 -0
index b1c81687e9d8..75d48217a20f 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.49.0
#33 lore raw

PATCH v12 6/10 tap: Introduce virtio-net hash feature

In reply to Akihiko Odaki

Add ioctls and storage required for the virtio-net hash feature to TAP.

---
 drivers/net/ipvlan/ipvtap.c |  2 +-
 drivers/net/macvtap.c       |  2 +-
 drivers/net/tap.c           | 72 +++++++++++++++++++++++++++++++++++++++++----
 include/linux/if_tap.h      |  4 ++-
 4 files changed, 71 insertions(+), 9 deletions(-)
drivers/net/ipvlan/ipvtap.c +1 -1
index 1afc4c47be73..305438abf7ae 100644
@@ -114,7 +114,7 @@ static void ipvtap_dellink(struct net_device *dev,
 	struct ipvtap_dev *vlan = netdev_priv(dev);
 
 	netdev_rx_handler_unregister(dev);
-	tap_del_queues(&vlan->tap);
+	tap_del(&vlan->tap);
 	ipvlan_link_delete(dev, head);
 }
 
drivers/net/macvtap.c +1 -1
index 29a5929d48e5..e72144d05ef4 100644
@@ -122,7 +122,7 @@ static void macvtap_dellink(struct net_device *dev,
 	struct macvtap_dev *vlantap = netdev_priv(dev);
 
 	netdev_rx_handler_unregister(dev);
-	tap_del_queues(&vlantap->tap);
+	tap_del(&vlantap->tap);
 	macvlan_dellink(dev, head);
 }
 
drivers/net/tap.c +66 -6

128 lines not on this page · raw message

include/linux/if_tap.h +3 -1
index 553552fa635c..6647a7a9e956 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;
 
 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 __rcu *vnet_hash;
 
 	void (*update_features)(struct tap_dev *tap, netdev_features_t features);
 	void (*count_tx_dropped)(struct tap_dev *tap);
@@ -74,7 +76,7 @@ struct tap_queue {
 };
 
 rx_handler_result_t tap_handle_frame(struct sk_buff **pskb);
-void tap_del_queues(struct tap_dev *tap);
+void tap_del(struct tap_dev *tap);
 int tap_get_minor(dev_t major, struct tap_dev *tap);
 void tap_free_minor(dev_t major, struct tap_dev *tap);
 int tap_queue_resize(struct tap_dev *tap);
Signature · 1 line
2.49.0
#34 lore raw

PATCH v12 7/10 selftest: tun: Test vnet ioctls without device

In reply to Akihiko Odaki

Ensure that vnet ioctls result in EBADFD when the underlying device is deleted.

---
 tools/testing/selftests/net/tun.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
tools/testing/selftests/net/tun.c +39 -0
index fa83918b62d1..41747e1728a6 100644
@@ -12,6 +12,7 @@
 #include <linux/if_tun.h>
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
+#include <linux/virtio_net.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 
@@ -159,4 +160,42 @@ TEST_F(tun, reattach_close_delete) {
 	EXPECT_EQ(tun_delete(self->ifname), 0);
 }
 
+FIXTURE(tun_deleted)
+{
+	char ifname[IFNAMSIZ];
+	int fd;
+};
+
+FIXTURE_SETUP(tun_deleted)
+{
+	self->ifname[0] = 0;
+	self->fd = tun_alloc(self->ifname);
+	ASSERT_LE(0, self->fd);
+
+	ASSERT_EQ(0, tun_delete(self->ifname))
+		EXPECT_EQ(0, close(self->fd));
+}
+
+FIXTURE_TEARDOWN(tun_deleted)
+{
+	EXPECT_EQ(0, close(self->fd));
+}
+
+TEST_F(tun_deleted, getvnethdrsz)
+{
+	ASSERT_EQ(-1, ioctl(self->fd, TUNGETVNETHDRSZ));
+	EXPECT_EQ(EBADFD, errno);
+}
+
+TEST_F(tun_deleted, getvnethashtypes)
+{
+	uint32_t hash_types;
+	int ret = ioctl(self->fd, TUNGETVNETHASHTYPES, &hash_types);
+
+	if (ret == -1 && errno == EBADFD)
+		SKIP(return, "TUNGETVNETHASHTYPES not supported");
+
+	EXPECT_FALSE(ret);
+}
+
 TEST_HARNESS_MAIN
Signature · 1 line
2.49.0
#35 lore raw

PATCH v12 8/10 selftest: tun: Add tests for virtio-net hashing

In reply to Akihiko Odaki

The added tests confirm tun can perform RSS for all supported hash types to select the receive queue and report hash values.

---
 tools/testing/selftests/net/Makefile |   2 +-
 tools/testing/selftests/net/tun.c    | 448 ++++++++++++++++++++++++++++++++++-
 2 files changed, 440 insertions(+), 10 deletions(-)
tools/testing/selftests/net/Makefile +1 -1
index 8f32b4f01aee..0e0c751a4691 100644
@@ -124,6 +124,6 @@ $(OUTPUT)/reuseport_bpf_numa: LDLIBS += -lnuma
 $(OUTPUT)/tcp_mmap: LDLIBS += -lpthread -lcrypto
 $(OUTPUT)/tcp_inq: LDLIBS += -lpthread
 $(OUTPUT)/bind_bhash: LDLIBS += -lpthread
-$(OUTPUT)/io_uring_zerocopy_tx: CFLAGS += -I../../../include/
+$(OUTPUT)/io_uring_zerocopy_tx $(OUTPUT)/tun: CFLAGS += -I../../../include/
 
 include bpf.mk
tools/testing/selftests/net/tun.c +439 -9

508 lines not on this page · raw message

Signature · 1 line
2.49.0
#36 lore raw

PATCH v12 9/10 selftest: tap: Add tests for virtio-net ioctls

In reply to Akihiko Odaki

They only test the ioctls are wired up to the implementation common with tun as it is already tested for tun.

---
 tools/testing/selftests/net/config |   1 +
 tools/testing/selftests/net/tap.c  | 131 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 129 insertions(+), 3 deletions(-)
tools/testing/selftests/net/config +1 -0
index 61e5116987f3..00cb1e65b392 100644
@@ -99,6 +99,7 @@ CONFIG_IPV6_IOAM6_LWTUNNEL=y
 CONFIG_CRYPTO_SM4_GENERIC=y
 CONFIG_AMT=m
 CONFIG_TUN=y
+CONFIG_TUN_VNET_CROSS_LE=y
 CONFIG_VXLAN=m
 CONFIG_IP_SCTP=m
 CONFIG_NETFILTER_XT_MATCH_POLICY=m
tools/testing/selftests/net/tap.c +128 -3

141 lines not on this page · raw message

Signature · 1 line
2.49.0
#37 lore raw

PATCH v12 10/10 vhost/net: Support VIRTIO_NET_F_HASH_REPORT

In reply to Akihiko Odaki

VIRTIO_NET_F_HASH_REPORT allows to report hash values calculated on the host. When VHOST_NET_F_VIRTIO_NET_HDR is employed, it will report no hash values (i.e., the hash_report member is always set to VIRTIO_NET_HASH_REPORT_NONE). Otherwise, the values reported by the underlying socket will be reported.

VIRTIO_NET_F_HASH_REPORT requires VIRTIO_F_VERSION_1.

---
 drivers/vhost/net.c | 68 +++++++++++++++++++++++++++--------------------------
 1 file changed, 35 insertions(+), 33 deletions(-)
drivers/vhost/net.c +35 -33
index b9b9e9d40951..fc5b43e43a06 100644
@@ -73,6 +73,7 @@ enum {
 	VHOST_NET_FEATURES = VHOST_FEATURES |
 			 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
 			 (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
+			 (1ULL << VIRTIO_NET_F_HASH_REPORT) |
 			 (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
 			 (1ULL << VIRTIO_F_RING_RESET)
 };
@@ -1097,10 +1098,6 @@ static void handle_rx(struct vhost_net *net)
 		.msg_controllen = 0,
 		.msg_flags = MSG_DONTWAIT,
 	};
-	struct virtio_net_hdr hdr = {
-		.flags = 0,
-		.gso_type = VIRTIO_NET_HDR_GSO_NONE
-	};
 	size_t total_len = 0;
 	int err, mergeable;
 	s16 headcount;
@@ -1174,11 +1171,15 @@ static void handle_rx(struct vhost_net *net)
 		/* We don't need to be notified again. */
 		iov_iter_init(&msg.msg_iter, ITER_DEST, vq->iov, in, vhost_len);
 		fixup = msg.msg_iter;
-		if (unlikely((vhost_hlen))) {
-			/* We will supply the header ourselves
-			 * TODO: support TSO.
-			 */
-			iov_iter_advance(&msg.msg_iter, vhost_hlen);
+		/*
+		 * Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR
+		 * TODO: support TSO.
+		 */
+		if (unlikely(vhost_hlen) &&
+		    iov_iter_zero(vhost_hlen, &msg.msg_iter) != vhost_hlen) {
+			vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
+			       vq->iov->iov_base);
+			goto out;
 		}
 		err = sock->ops->recvmsg(sock, &msg,
 					 sock_len, MSG_DONTWAIT | MSG_TRUNC);
@@ -1191,30 +1192,24 @@ static void handle_rx(struct vhost_net *net)
 			vhost_discard_vq_desc(vq, headcount);
 			continue;
 		}
-		/* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
-		if (unlikely(vhost_hlen)) {
-			if (copy_to_iter(&hdr, sizeof(hdr),
-					 &fixup) != sizeof(hdr)) {
-				vq_err(vq, "Unable to write vnet_hdr "
-				       "at addr %p\n", vq->iov->iov_base);
-				goto out;
-			}
-		} else {
-			/* Header came from socket; we'll need to patch
-			 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
-			 */
-			iov_iter_advance(&fixup, sizeof(hdr));
-		}
 		/* TODO: Should check and handle checksum. */
 
+		/*
+		 * We'll need to patch ->num_buffers over if
+		 * VIRTIO_NET_F_MRG_RXBUF or VIRTIO_F_VERSION_1
+		 */
 		num_buffers = cpu_to_vhost16(vq, headcount);
-		if (likely(set_num_buffers) &&
-		    copy_to_iter(&num_buffers, sizeof num_buffers,
-				 &fixup) != sizeof num_buffers) {
-			vq_err(vq, "Failed num_buffers write");
-			vhost_discard_vq_desc(vq, headcount);
-			goto out;
+		if (likely(set_num_buffers)) {
+			iov_iter_advance(&fixup, offsetof(struct virtio_net_hdr_v1, num_buffers));
+
+			if (copy_to_iter(&num_buffers, sizeof(num_buffers),
+					 &fixup) != sizeof(num_buffers)) {
+				vq_err(vq, "Failed num_buffers write");
+				vhost_discard_vq_desc(vq, headcount);
+				goto out;
+			}
 		}
+
 		nvq->done_idx += headcount;
 		if (nvq->done_idx > VHOST_NET_BATCH)
 			vhost_net_signal_used(nvq);
@@ -1607,10 +1602,13 @@ static int vhost_net_set_features(struct vhost_net *n, u64 features)
 	size_t vhost_hlen, sock_hlen, hdr_len;
 	int i;
 
-	hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
-			       (1ULL << VIRTIO_F_VERSION_1))) ?
-			sizeof(struct virtio_net_hdr_mrg_rxbuf) :
-			sizeof(struct virtio_net_hdr);
+	if (features & (1ULL << VIRTIO_NET_F_HASH_REPORT))
+		hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
+	else if (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
+			     (1ULL << VIRTIO_F_VERSION_1)))
+		hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
+	else
+		hdr_len = sizeof(struct virtio_net_hdr);
 	if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
 		/* vhost provides vnet_hdr */
 		vhost_hlen = hdr_len;
@@ -1691,6 +1689,10 @@ static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
 			return -EFAULT;
 		if (features & ~VHOST_NET_FEATURES)
 			return -EOPNOTSUPP;
+		if ((features & ((1ULL << VIRTIO_F_VERSION_1) |
+				 (1ULL << VIRTIO_NET_F_HASH_REPORT))) ==
+		    (1ULL << VIRTIO_NET_F_HASH_REPORT))
+			return -EINVAL;
 		return vhost_net_set_features(n, features);
 	case VHOST_GET_BACKEND_FEATURES:
 		features = VHOST_NET_BACKEND_FEATURES;
Signature · 1 line
2.49.0
#38 lore raw
In reply to Akihiko Odaki
Quoted 27 lines · Akihiko Odaki

NOTE: I'm leaving Daynix Computing Ltd., for which I worked on this patch series, by the end of this month.

While net-next is closed, this is the last chance for me to send another version so let me send the local changes now.

Please contact Yuri Benditovich, who is CCed on this email, for anything about this series.

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).

Quoted 6 lines

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/

It's been a while, do you intend to post v13?

Quoted 154 lines

V1 -> V2:
Changed to introduce a new BPF program type.

---
Changes in v12:
- Updated tools/testing/selftests/net/config.
- Split TUNSETVNETHASH.
- Link to v11: https://lore.kernel.org/r/[email protected]

Changes in v11:
- Added the missing code to free vnet_hash in patch
"tap: Introduce virtio-net hash feature".
- Link to v10: https://lore.kernel.org/r/[email protected]

Changes in v10:
- Split common code and TUN/TAP-specific code into separate patches.
- Reverted a spurious style change in patch "tun: Introduce virtio-net hash feature".
- Added a comment explaining disable_ipv6 in tests.
- Used AF_PACKET for patch "selftest: tun: Add tests for
virtio-net hashing". I also added the usage of FIXTURE_VARIANT() as the testing function now needs access to more variant-specific variables.
- Corrected the message of patch "selftest: tun: Add tests for virtio-net hashing"; it mentioned validation of configuration but it is not scope of this patch.
- Expanded the description of patch "selftest: tun: Add tests for virtio-net hashing".
- Added patch "tun: Allow steering eBPF program to fall back".
- Changed to handle TUNGETVNETHASHCAP before taking the rtnl lock.
- Removed redundant tests for tun_vnet_ioctl().
- Added patch "selftest: tap: Add tests for virtio-net ioctls".
- Added a design explanation of ioctls for extensibility and migration.
- Removed a few branches in patch
"vhost/net: Support VIRTIO_NET_F_HASH_REPORT".
- Link to v9: https://lore.kernel.org/r/[email protected]

Changes in v9:
- Added a missing return statement in patch
"tun: Introduce virtio-net hash feature".
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- Disabled IPv6 to eliminate noises in tests.
- Added a branch in tap to avoid unnecessary dissection when hash reporting is disabled.
- Removed unnecessary rtnl_lock().
- Extracted code to handle new ioctls into separate functions to avoid adding extra NULL checks to the code handling other ioctls.
- Introduced variable named "fd" to __tun_chr_ioctl().
- s/-/=/g in a patch message to avoid confusing Git.
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Ensured to set hash_report to VIRTIO_NET_HASH_REPORT_NONE for VHOST_NET_F_VIRTIO_NET_HDR.
- s/4/sizeof(u32)/ in patch "virtio_net: Add functions for hashing".
- Added tap_skb_cb type.
- Rebased.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Extracted changes to fill vnet header holes into another series.
- Squashed patches "skbuff: Introduce SKB_EXT_TUN_VNET_HASH", "tun:
Introduce virtio-net hash reporting feature", and "tun: Introduce virtio-net RSS" into patch "tun: Introduce virtio-net hash feature".
- Dropped the RFC tag.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Fixed a compilation error with CONFIG_TUN_VNET_CROSS_LE.
- Optimized the calculation of the hash value according to:
https://git.dpdk.org/dpdk/commit/?id=3fb1ea032bd6ff8317af5dac9af901f1f324cab4
- Added patch "tun: Unify vnet implementation".
- Dropped patch "tap: Pad virtio header with zero".
- Added patch "selftest: tun: Test vnet ioctls without device".
- Reworked selftests to skip for older kernels.
- Documented the case when the underlying device is deleted and packets have queue_mapping set by TC.
- Reordered test harness arguments.
- Added code to handle fragmented packets.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Moved tun_vnet_hash_ext to if_tun.h.
- Renamed virtio_net_toeplitz() to virtio_net_toeplitz_calc().
- Replaced htons() with cpu_to_be16().
- Changed virtio_net_hash_rss() to return void.
- Reordered variable declarations in virtio_net_hash_rss().
- Removed virtio_net_hdr_v1_hash_from_skb().
- Updated messages of "tap: Pad virtio header with zero" and
"tun: Pad virtio header with zero".
- Fixed vnet_hash allocation size.
- Ensured to free vnet_hash when destructing tun_struct.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Reverted back to add ioctl.
- Split patch "tun: Introduce virtio-net hashing feature" into
"tun: Introduce virtio-net hash reporting feature" and
"tun: Introduce virtio-net RSS".
- Changed to reuse hash values computed for automq instead of performing RSS hashing when hash reporting is requested but RSS is not.
- Extracted relevant data from struct tun_struct to keep it minimal.
- Added kernel-doc.
- Changed to allow calling TUNGETVNETHASHCAP before TUNSETIFF.
- Initialized num_buffers with 1.
- Added a test case for unclassified packets.
- Fixed error handling in tests.
- Changed tests to verify that the queue index will not overflow.
- Rebased.
- Link to v2: https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (10):
virtio_net: Add functions for hashing
net: flow_dissector: Export flow_keys_dissector_symmetric
tun: Allow steering eBPF program to fall back
tun: Add common virtio-net hash feature code
tun: Introduce virtio-net hash feature
tap: Introduce virtio-net hash feature
selftest: tun: Test vnet ioctls without device
selftest: tun: Add tests for virtio-net hashing
selftest: tap: Add tests for virtio-net ioctls vhost/net: Support VIRTIO_NET_F_HASH_REPORT

 Documentation/networking/tuntap.rst  |   7 +
 drivers/net/Kconfig                  |   1 +
 drivers/net/ipvlan/ipvtap.c          |   2 +-
 drivers/net/macvtap.c                |   2 +-
 drivers/net/tap.c                    |  80 +++++-
 drivers/net/tun.c                    |  92 +++++--
 drivers/net/tun_vnet.h               | 165 +++++++++++-
 drivers/vhost/net.c                  |  68 ++---
 include/linux/if_tap.h               |   4 +-
 include/linux/skbuff.h               |   3 +
 include/linux/virtio_net.h           | 188 ++++++++++++++
 include/net/flow_dissector.h         |   1 +
 include/uapi/linux/if_tun.h          |  80 ++++++
 net/core/flow_dissector.c            |   3 +-
 net/core/skbuff.c                    |   4 +
 tools/testing/selftests/net/Makefile |   2 +-
 tools/testing/selftests/net/config   |   1 +
 tools/testing/selftests/net/tap.c    | 131 +++++++++-
 tools/testing/selftests/net/tun.c    | 485 ++++++++++++++++++++++++++++++++++-
 19 files changed, 1234 insertions(+), 85 deletions(-)

---
base-commit: 5cb8274d66c611b7889565c418a8158517810f9b
change-id: 20240403-rss-e737d89efa77

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>