Skip to contentSkip to conversation

tun: Introduce virtio-net hashing feature

RFC PATCH Superseded Akihiko Odaki 3 participants 24 messages Oct 8, 2024 – Oct 18, 2024

Subsystems (9)

v5 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v5 cover tun: Introduce virtio-net hashing feature

virtio-net have two usage of hashes: one is RSS and another is hash reporting. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS.

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

Introduce the code to compute hashes to the kernel in order to overcome thse challenges.

An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but it is based on context rewrites, which is in feature freeze. We can adopt kfuncs, but they will not be UAPIs. We opt to ioctl to align with other relevant UAPIs (KVM and vhost_net).

The patches for QEMU to use this new feature was submitted as RFC and is available at:
https://patchew.org/QEMU/[email protected]/

This work was presented at LPC 2024:
https://lpc.events/event/18/contributions/1963/

V1 -> V2:
Changed to introduce a new BPF program type.

---
Changes in 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
skbuff: Introduce SKB_EXT_TUN_VNET_HASH
net: flow_dissector: Export flow_keys_dissector_symmetric
tun: Unify vnet implementation
tun: Pad virtio header with zero
tun: Introduce virtio-net hash reporting feature
tun: Introduce virtio-net RSS
selftest: tun: Test vnet ioctls without device
selftest: tun: Add tests for virtio-net hashing vhost/net: Support VIRTIO_NET_F_HASH_REPORT

 Documentation/networking/tuntap.rst  |   7 +
 MAINTAINERS                          |   1 +
 drivers/net/Kconfig                  |   1 +
 drivers/net/tap.c                    | 218 ++++--------
 drivers/net/tun.c                    | 293 ++++++----------
 drivers/net/tun_vnet.h               | 342 +++++++++++++++++++
 drivers/vhost/net.c                  |  16 +-
 include/linux/if_tap.h               |   2 +
 include/linux/skbuff.h               |   3 +
 include/linux/virtio_net.h           | 188 +++++++++++
 include/net/flow_dissector.h         |   1 +
 include/uapi/linux/if_tun.h          |  75 +++++
 net/core/flow_dissector.c            |   3 +-
 net/core/skbuff.c                    |   4 +
 tools/testing/selftests/net/Makefile |   2 +-
 tools/testing/selftests/net/tun.c    | 630 ++++++++++++++++++++++++++++++++++-
 16 files changed, 1430 insertions(+), 356 deletions(-)

---
base-commit: 752ebcbe87aceeb6334e846a466116197711a982
change-id: 20240403-rss-e737d89efa77

Best regards,

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

RFC PATCH v5 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 276ca543ef44..6f192bb9ba1d 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 + 4;
+}
+
+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.46.2
maintainer
#2 lore raw

Re: [PATCH RFC v5 01/10] virtio_net: Add functions for hashing

Quoted 6 lines · Akihiko Odaki

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

---
 include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++

No need for these to be in header files

#3 lore raw

Re: [PATCH RFC v5 01/10] virtio_net: Add functions for hashing

Quoted 9 lines · Willem de Bruijn
Quoted 6 lines · Akihiko Odaki

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

---
  include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++

No need for these to be in header files

I naively followed prior examples in this file. Do you have an alternative idea?

maintainer
#4 lore raw

Re: [PATCH RFC v5 01/10] virtio_net: Add functions for hashing

Quoted 5 lines · Akihiko Odaki
Quoted 4 lines · Willem de Bruijn
Quoted 3 lines · Akihiko Odaki

They are useful to implement VIRTIO_NET_F_RSS and
VIRTIO_NET_F_HASH_REPORT.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
---
  include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++

No need for these to be in header files

I naively followed prior examples in this file. Do you have an alternative idea?

This is long overdue really, not specific to this fuatre.

And extends to your patch 4 that deduplicates tun.c and tap.c.

Perhaps drivers/net/virtio_net_hdr.c.

Or drivers/net/tun_vnet.c, matching your choice of drivers/net/tun_vnet.h.

#5 lore raw

RFC PATCH v5 2/10 skbuff: Introduce SKB_EXT_TUN_VNET_HASH

In reply to Akihiko Odaki

This new extension will be used by tun to carry the hash values and types to report with virtio-net headers.

---
 include/linux/skbuff.h | 3 +++
 net/core/skbuff.c      | 4 ++++
 2 files changed, 7 insertions(+)
include/linux/skbuff.h +3 -0
index 29c3ea5b6e93..a361c4150144 100644
@@ -4718,6 +4718,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 83f8cd8aa2d1..f0bf94cf458b 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>
@@ -4979,6 +4980,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.46.2
#6 lore raw

RFC PATCH v5 3/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 0e638a37aa09..9822988f2d49 100644
@@ -1852,7 +1852,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.46.2
#7 lore raw

RFC PATCH v5 4/10 tun: Unify vnet implementation

In reply to Akihiko Odaki

Both tun and tap exposes the same set of virtio-net-related features. Unify their implementations to ease future changes.

---
 MAINTAINERS            |   1 +
 drivers/net/tap.c      | 172 ++++++----------------------------------
 drivers/net/tun.c      | 208 ++++++++-----------------------------------------
 drivers/net/tun_vnet.h | 181 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 238 insertions(+), 324 deletions(-)
MAINTAINERS +1 -0
index cc40a9d9b8cd..209b4e1cccb1 100644
@@ -23338,6 +23338,7 @@ F:	Documentation/networking/tuntap.rst
 F:	arch/um/os-Linux/drivers/
 F:	drivers/net/tap.c
 F:	drivers/net/tun.c
+F:	drivers/net/tun_vnet.h
 
 TURBOCHANNEL SUBSYSTEM
 M:	"Maciej W. Rozycki" <[email protected]>
drivers/net/tap.c +23 -149

240 lines not on this page · raw message

drivers/net/tun.c +33 -175

326 lines not on this page · raw message

drivers/net/tun_vnet.h new file +181 -0

181 lines not on this page · raw message

Signature · 1 line
2.46.2
maintainer
#8 lore raw

Re: [PATCH RFC v5 04/10] tun: Unify vnet implementation

Quoted 9 lines · Akihiko Odaki

Both tun and tap exposes the same set of virtio-net-related features. Unify their implementations to ease future changes.

---
 MAINTAINERS            |   1 +
 drivers/net/tap.c      | 172 ++++++----------------------------------
 drivers/net/tun.c      | 208 ++++++++-----------------------------------------
 drivers/net/tun_vnet.h | 181 ++++++++++++++++++++++++++++++++++++++++++

Same point: should not be in a header.

Also: I've looked into deduplicating code between the various tun, tap and packet socket code as well.

In general it's a good idea. The main counter arguments is that such a break in continuity also breaks backporting fixes to stable. So the benefit must outweight that cost.

In this case, the benefits in terms of LoC are rather modest. Not sure it's worth it.

Even more importantly: are the two code paths that you deduplicate exactly identical? Often in the past the two subtly diverged over time, e.g., due to new features added only to one of the two.

If so, call out any behavioral changes to either as a result of deduplicating explicitly.

#9 lore raw

Re: [PATCH RFC v5 04/10] tun: Unify vnet implementation

Quoted 20 lines · Willem de Bruijn
Quoted 9 lines · Akihiko Odaki

Both tun and tap exposes the same set of virtio-net-related features. Unify their implementations to ease future changes.

---
  MAINTAINERS            |   1 +
  drivers/net/tap.c      | 172 ++++++----------------------------------
  drivers/net/tun.c      | 208 ++++++++-----------------------------------------
  drivers/net/tun_vnet.h | 181 ++++++++++++++++++++++++++++++++++++++++++

Same point: should not be in a header.

Also: I've looked into deduplicating code between the various tun, tap and packet socket code as well.

In general it's a good idea. The main counter arguments is that such a break in continuity also breaks backporting fixes to stable. So the benefit must outweight that cost.

Quoted 6 lines

In this case, the benefits in terms of LoC are rather modest. Not sure it's worth it.

Even more importantly: are the two code paths that you deduplicate exactly identical? Often in the past the two subtly diverged over time, e.g., due to new features added only to one of the two.

I find extracting the virtio_net-related code into functions is beneficial. For example, tun_get_user() is a big function and extracting the virtio_net-related code into tun_vnet_hdr_get() will ease understanding tun_get_user() when you are not interested in virtio_net. If virtio_net is your interest, you can look at this group of functions to figure out how they interact with each other.

Currently, the extracted code is almost identical for tun and tap so they can share it. We can copy the code back (but keep functions as semantic units) if they diverge in the future.

Quoted 3 lines

If so, call out any behavioral changes to either as a result of deduplicating explicitly.

This adds an error message for GSO failure, which was missing for tap. I will note that in the next version.

maintainer
#10 lore raw

Re: [PATCH RFC v5 04/10] tun: Unify vnet implementation

Quoted 24 lines · Akihiko Odaki
Quoted 23 lines · Willem de Bruijn
Quoted 9 lines · Akihiko Odaki

Both tun and tap exposes the same set of virtio-net-related features. Unify their implementations to ease future changes.

---
  MAINTAINERS            |   1 +
  drivers/net/tap.c      | 172 ++++++----------------------------------
  drivers/net/tun.c      | 208 ++++++++-----------------------------------------
  drivers/net/tun_vnet.h | 181 ++++++++++++++++++++++++++++++++++++++++++

Same point: should not be in a header.

Also: I've looked into deduplicating code between the various tun, tap and packet socket code as well.

In general it's a good idea. The main counter arguments is that such a break in continuity also breaks backporting fixes to stable. So the benefit must outweight that cost.

In this case, the benefits in terms of LoC are rather modest. Not sure it's worth it.

Quoted 6 lines
Quoted 3 lines

Even more importantly: are the two code paths that you deduplicate exactly identical? Often in the past the two subtly diverged over time, e.g., due to new features added only to one of the two.

I find extracting the virtio_net-related code into functions is beneficial.

I understand the benefits. But as I pointed out, it is a trade-off, not an unconditional good.

Quoted 7 lines

For example, tun_get_user() is a big function and extracting the virtio_net-related code into tun_vnet_hdr_get() will ease understanding tun_get_user() when you are not interested in virtio_net. If virtio_net is your interest, you can look at this group of functions to figure out how they interact with each other.

Currently, the extracted code is almost identical for tun and tap so

Almost here is the scary part. Any code that is not exactly identical must be called out in the commit message.

What often happened is that one of the two got an improvement, whether fix or feature extension. Then it is likely acceptable to extend this to the other, too. But we have to review each case.

Quoted 2 lines

they can share it. We can copy the code back (but keep functions as semantic units) if they diverge in the future.

There hopefully is no need to diverge in the future. I suspect that all previous divergences are accidental.

Quoted 6 lines
Quoted 3 lines

If so, call out any behavioral changes to either as a result of deduplicating explicitly.

This adds an error message for GSO failure, which was missing for tap. I will note that in the next version.

#11 lore raw

RFC PATCH v5 5/10 tun: Pad virtio header with zero

In reply to Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

---
 drivers/net/tun_vnet.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.h +2 -1
index 7c7f3f6d85e9..c40bde0fdf8c 100644
@@ -138,7 +138,8 @@ static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
 	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
 		return -EFAULT;
 
-	iov_iter_advance(iter, sz - sizeof(*hdr));
+	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+		return -EFAULT;
 
 	return 0;
 }
Signature · 1 line
2.46.2
#12 lore raw

Re: [PATCH RFC v5 05/10] tun: Pad virtio header with zero

Quoted 10 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 5 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

This sounds like an independent fix that is worth going to -net first.

Thanks

Quoted 22 lines
---
 drivers/net/tun_vnet.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.h +2 -2
index 7c7f3f6d85e9..c40bde0fdf8c 100644
@@ -138,7 +138,8 @@ static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
        if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
                return -EFAULT;
 
-       iov_iter_advance(iter, sz - sizeof(*hdr));
+       if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+               return -EFAULT;
 
        return 0;
 }
 
--

2.46.2

#13 lore raw

RFC PATCH v5 6/10 tun: Introduce virtio-net hash reporting feature

In reply to Akihiko Odaki

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

---
 Documentation/networking/tuntap.rst |   7 +++
 drivers/net/Kconfig                 |   1 +
 drivers/net/tap.c                   |  45 ++++++++++++++--
 drivers/net/tun.c                   |  46 ++++++++++++----
 drivers/net/tun_vnet.h              | 102 +++++++++++++++++++++++++++++++-----
 include/linux/if_tap.h              |   2 +
 include/uapi/linux/if_tun.h         |  48 +++++++++++++++++
 7 files changed, 223 insertions(+), 28 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/Kconfig +1 -0
index 9920b3a68ed1..e2a7bd703550 100644
@@ -395,6 +395,7 @@ config TUN
 	tristate "Universal TUN/TAP device driver support"
 	depends on INET
 	select CRC32
+	select SKB_EXTENSIONS
 	help
 	  TUN/TAP provides packet reception and transmission for user space
 	  programs.  It can be viewed as a simple Point-to-Point or Ethernet
drivers/net/tap.c +40 -5

89 lines not on this page · raw message

drivers/net/tun.c +35 -11

111 lines not on this page · raw message

drivers/net/tun_vnet.h +90 -12

189 lines not on this page · raw message

include/linux/if_tap.h +2 -0
index 553552fa635c..5bbb343a6dba 100644
@@ -4,6 +4,7 @@
 
 #include <net/sock.h>
 #include <linux/skb_array.h>
+#include <uapi/linux/if_tun.h>
 
 struct file;
 struct socket;
@@ -43,6 +44,7 @@ struct tap_dev {
 	int			numqueues;
 	netdev_features_t	tap_features;
 	int			minor;
+	struct tun_vnet_hash	vnet_hash;
 
 	void (*update_features)(struct tap_dev *tap, netdev_features_t features);
 	void (*count_tx_dropped)(struct tap_dev *tap);
include/uapi/linux/if_tun.h +48 -0

58 lines not on this page · raw message

Signature · 1 line
2.46.2
#14 lore raw

Re: [PATCH RFC v5 06/10] tun: Introduce virtio-net hash reporting feature

Quoted 5 lines · Akihiko Odaki

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

I wonder if this would cause overhead when hash reporting is not enabled?

Quoted 4 lines
---
 Documentation/networking/tuntap.rst |   7 +++
 drivers/net/Kconfig                 |   1 +
 drivers/net/tap.c                   |  45 ++++++++++++++--

Tile should be for tap as well or is this just for tun?

Quoted 29 lines
 drivers/net/tun.c                   |  46 ++++++++++++----
 drivers/net/tun_vnet.h              | 102 +++++++++++++++++++++++++++++++-----
 include/linux/if_tap.h              |   2 +
 include/uapi/linux/if_tun.h         |  48 +++++++++++++++++
 7 files changed, 223 insertions(+), 28 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/Kconfig +0 -0
index 9920b3a68ed1..e2a7bd703550 100644

drivers/net/Kconfig config TUN · line 398

Quoted 4 lines
drivers/net/Kconfig +1 -0
@@ -395,6 +395,7 @@ config TUN
        tristate "Universal TUN/TAP device driver support"
        depends on INET
        select CRC32
+       select SKB_EXTENSIONS

Then we need this for macvtap at least as well?

Quoted 8 lines
drivers/net/Kconfig +0 -0
        help
          TUN/TAP provides packet reception and transmission for user space
          programs.  It can be viewed as a simple Point-to-Point or Ethernet
drivers/net/tap.c +0 -0
index 9a34ceed0c2c..5e2fbe63ca47 100644

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

Quoted 6 lines
drivers/net/tap.c +3 -0
@@ -179,6 +179,16 @@ static void tap_put_queue(struct tap_queue *q)
        sock_put(&q->sk);
 }
 
+static struct virtio_net_hash *tap_add_hash(struct sk_buff *skb)
+{
+       return (struct virtio_net_hash *)skb->cb;

Any reason that tap uses skb->cb but not skb extensions? (And is it safe to use that without cloning?)

Quoted 136 lines
drivers/net/tap.c +37 -5
+}
+
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return (const struct virtio_net_hash *)skb->cb;
+}
+
 /*
  * Select a queue based on the rxq of the device on which this packet
  * arrived. If the incoming device is not mq, calculate a flow hash
@@ -189,6 +199,7 @@ static void tap_put_queue(struct tap_queue *q)
 static struct tap_queue *tap_get_queue(struct tap_dev *tap,
                                       struct sk_buff *skb)
 {
+       struct flow_keys_basic keys_basic;
        struct tap_queue *queue = NULL;
        /* Access to taps array is protected by rcu, but access to numvtaps
         * isn't. Below we use it to lookup a queue, but treat it as a hint
@@ -198,15 +209,32 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
        int numvtaps = READ_ONCE(tap->numvtaps);
        __u32 rxq;
 
+       *tap_add_hash(skb) = (struct virtio_net_hash) { .report = VIRTIO_NET_HASH_REPORT_NONE };
+
        if (!numvtaps)
                goto out;
 
        if (numvtaps == 1)
                goto single;
 
+       if (!skb->l4_hash && !skb->sw_hash) {
+               struct flow_keys keys;
+
+               skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+               rxq = flow_hash_from_keys(&keys);
+               keys_basic = (struct flow_keys_basic) {
+                       .control = keys.control,
+                       .basic = keys.basic
+               };
+       } else {
+               skb_flow_dissect_flow_keys_basic(NULL, skb, &keys_basic, NULL, 0, 0, 0,
+                                                FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+               rxq = skb->hash;
+       }
+
        /* Check if we can use flow to select a queue */
-       rxq = skb_get_hash(skb);
        if (rxq) {
+               tun_vnet_hash_report(&tap->vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
                queue = rcu_dereference(tap->taps[rxq % numvtaps]);
                goto out;
        }
@@ -713,15 +741,16 @@ 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 < 0)
                        goto done;
 
-               ret = tun_vnet_hdr_put(vnet_hdr_len, iter, &vnet_hdr);
+               ret = tun_vnet_hdr_put(vnet_hdr_len, iter, &vnet_hdr, ret);
                if (ret < 0)
                        goto done;
        }
@@ -1025,7 +1054,13 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
                return ret;
 
        default:
-               return tun_vnet_ioctl(&q->vnet_hdr_sz, &q->flags, cmd, sp);
+               rtnl_lock();
+               tap = rtnl_dereference(q->tap);
+               ret = tun_vnet_ioctl(&q->vnet_hdr_sz, &q->flags,
+                                    tap ? &tap->vnet_hash : NULL, -EINVAL,
+                                    cmd, sp);
+               rtnl_unlock();
+               return ret;
        }
 }
 
drivers/net/tun.c +17 -1
index dd8799d19518..27308417b834 100644
@@ -209,6 +209,7 @@ struct tun_struct {
        struct bpf_prog __rcu *xdp_prog;
        struct tun_prog __rcu *steering_prog;
        struct tun_prog __rcu *filter_prog;
+       struct tun_vnet_hash vnet_hash;
        struct ethtool_link_ksettings link_ksettings;
        /* init args */
        struct file *file;
@@ -451,6 +452,16 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                e->rps_rxhash = hash;
 }
 
+static struct virtio_net_hash *tun_add_hash(struct sk_buff *skb)
+{
+       return skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
 /* We try to identify a flow through its rxhash. The reason that
  * we do not check rxq no. is because some cards(e.g 82599), chooses
  * the rxq based on the txq where the last packet of the flow comes. As
@@ -459,12 +470,17 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
  */
 static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
 {
+       struct flow_keys keys;
+       struct flow_keys_basic keys_basic;
        struct tun_flow_entry *e;
        u32 txq, numqueues;
 
        numqueues = READ_ONCE(tun->numqueues);
 
-       txq = __skb_get_hash_symmetric(skb);
+       memset(&keys, 0, sizeof(keys));
+       skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, 0);
+
+       txq = flow_hash_from_keys(&keys);
        e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
        if (e) {
                tun_flow_save_rps_rxhash(e, txq);

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

Quoted 9 lines
drivers/net/tun.c +6 -0
@@ -473,6 +489,13 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
                txq = reciprocal_scale(txq, numqueues);
        }
 
+       keys_basic = (struct flow_keys_basic) {
+               .control = keys.control,
+               .basic = keys.basic
+       };
+       tun_vnet_hash_report(&tun->vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+                            tun_add_hash);

Is using txq required when not l4_hash is required by the virtio-spec?

Quoted 44 lines
drivers/net/tun.c +7 -7
+
        return txq;
 }
 
@@ -1990,10 +2013,8 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
        size_t total;
 
        if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
-
                vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
-               ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
+               ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, NULL, 0);
                if (ret < 0)
                        return ret;
        }
@@ -2018,7 +2039,6 @@ static ssize_t tun_put_user(struct tun_struct *tun,
        int vlan_offset = 0;
        int vlan_hlen = 0;
        int vnet_hdr_sz = 0;
-       int ret;
 
        if (skb_vlan_tag_present(skb))
                vlan_hlen = VLAN_HLEN;
@@ -2043,13 +2063,15 @@ 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;
+               int ret;
 
-               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 < 0)
                        goto done;
 
-               ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
+               ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso, ret);
                if (ret < 0)
                        goto done;
        }

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

Quoted 7 lines
drivers/net/tun.c +2 -2
@@ -3055,9 +3077,10 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                goto unlock;
        }
 
-       ret = -EBADFD;
-       if (!tun)
+       if (!tun) {
+               ret = tun_vnet_ioctl(NULL, NULL, NULL, -EBADFD, cmd, argp);

This seems not elegant (passing three NULL pointers). Any reason we can't just modify __tun_chr_ioctl() instead of introducing things like tun_vnet_ioctl()?

Quoted 31 lines
drivers/net/tun.c +3 -1
                goto unlock;
+       }
 
        netif_info(tun, drv, tun->dev, "tun_chr_ioctl cmd %u\n", cmd);
 
@@ -3256,7 +3279,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                break;
 
        default:
-               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags, cmd, argp);
+               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
+                                    &tun->vnet_hash, -EINVAL, cmd, argp);
        }
 
        if (do_notify)
drivers/net/tun_vnet.h +3 -0
index c40bde0fdf8c..589a97dd7d02 100644
@@ -6,6 +6,9 @@
 #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 *);
+
 static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
 {
        return !(IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) && (flags & TUN_VNET_BE)) &&
@@ -59,18 +62,31 @@ static inline __virtio16 cpu_to_tun_vnet16(unsigned int flags, u16 val)
 }

drivers/net/tun_vnet.h static inline __virtio16 cpu_to_tun_vnet16(unsigned int flags, u16 val) · line 71

Quoted 10 lines
drivers/net/tun_vnet.h +6 -1
 
 static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
-                                 unsigned int cmd, int __user *sp)
+                                 struct tun_vnet_hash *hash, long fallback,
+                                 unsigned int cmd, void __user *argp)
 {
+       static const struct tun_vnet_hash cap = {
+               .flags = TUN_VNET_HASH_REPORT,
+               .types = VIRTIO_NET_SUPPORTED_HASH_TYPES
+       };

Let's find a way to reuse virtio-net uAPI instead of introducing new stuff to stress the management layer.

Quoted 207 lines
drivers/net/tun_vnet.h +81 -11
+       struct tun_vnet_hash hash_buf;
+       int __user *sp = argp;
        int s;
 
        switch (cmd) {
        case TUNGETVNETHDRSZ:
+               if (!sz)
+                       return -EBADFD;
+
                s = *sz;
                if (put_user(s, sp))
                        return -EFAULT;
                return 0;
 
        case TUNSETVNETHDRSZ:
+               if (!sz)
+                       return -EBADFD;
+
                if (get_user(s, sp))
                        return -EFAULT;
                if (s < (int)sizeof(struct virtio_net_hdr))
@@ -80,12 +96,18 @@ static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
                return 0;
 
        case TUNGETVNETLE:
+               if (!flags)
+                       return -EBADFD;
+
                s = !!(*flags & TUN_VNET_LE);
                if (put_user(s, sp))
                        return -EFAULT;
                return 0;
 
        case TUNSETVNETLE:
+               if (!flags)
+                       return -EBADFD;
+
                if (get_user(s, sp))
                        return -EFAULT;
                if (s)
@@ -95,16 +117,56 @@ static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
                return 0;
 
        case TUNGETVNETBE:
+               if (!flags)
+                       return -EBADFD;
+
                return tun_vnet_get_be(*flags, sp);
 
        case TUNSETVNETBE:
+               if (!flags)
+                       return -EBADFD;
+
                return tun_vnet_set_be(flags, sp);
 
+       case TUNGETVNETHASHCAP:
+               return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;
+
+       case TUNSETVNETHASH:
+               if (!hash)
+                       return -EBADFD;
+
+               if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
+                       return -EFAULT;
+
+               *hash = hash_buf;
+               return 0;
+
        default:
-               return -EINVAL;
+               return fallback;
        }
 }
 
+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->flags & TUN_VNET_HASH_REPORT))
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->types, keys),
+               .value = value
+       };
+}
+
 static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                   struct iov_iter *from,
                                   struct virtio_net_hdr *hdr)
@@ -130,15 +192,15 @@ 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 void *hdr, int content_sz)
 {
        if (iov_iter_count(iter) < sz)
                return -EINVAL;
 
-       if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
+       if (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;
@@ -151,32 +213,48 @@ static inline int tun_vnet_hdr_to_skb(unsigned int flags,
        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);
+       int content_sz;
+
+       if (report) {
+               content_sz = sizeof(struct virtio_net_hdr_v1_hash);
+
+               *hdr = (struct virtio_net_hdr_v1_hash) {
+                       .hdr = { .num_buffers = __cpu_to_virtio16(true, 1) },
+                       .hash_value = cpu_to_le32(report->value),
+                       .hash_report = cpu_to_le16(report->report)
+               };
+       } else {
+               content_sz = sizeof(struct virtio_net_hdr);
+       }
 
-       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;
        }
 
-       return 0;
+       return content_sz;
 }
 
 #endif /* TUN_VNET_H */
include/linux/if_tap.h +2 -0
index 553552fa635c..5bbb343a6dba 100644
@@ -4,6 +4,7 @@
 
 #include <net/sock.h>
 #include <linux/skb_array.h>
+#include <uapi/linux/if_tun.h>
 
 struct file;
 struct socket;
@@ -43,6 +44,7 @@ struct tap_dev {
        int                     numqueues;
        netdev_features_t       tap_features;
        int                     minor;
+       struct tun_vnet_hash    vnet_hash;
 
        void (*update_features)(struct tap_dev *tap, netdev_features_t features);
        void (*count_tx_dropped)(struct tap_dev *tap);
include/uapi/linux/if_tun.h +6 -0
index 287cdc81c939..d11e79b4e0dc 100644
@@ -62,6 +62,34 @@
 #define TUNSETCARRIER _IOW('T', 226, int)
 #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHCAP - ioctl to get virtio_net hashing capability.
+ *
+ * The argument is a pointer to &struct tun_vnet_hash which will store the
+ * maximal virtio_net hashing configuration.
+ */

include/uapi/linux/if_tun.h line 80

Quoted 10 lines
include/uapi/linux/if_tun.h +10 -0
+#define TUNGETVNETHASHCAP _IOR('T', 228, struct tun_vnet_hash)
+
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
+ *
+ * The argument is a pointer to &struct tun_vnet_hash.
+ *
+ * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
+ * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
+ * to the size of &struct virtio_net_hdr_v1_hash.

I think we don't need & here.

include/uapi/linux/if_tun.h line 89

Quoted 9 lines
include/uapi/linux/if_tun.h +9 -0
+ *
+ * The members added to the legacy header by %TUN_VNET_HASH_REPORT flag will
+ * always be little-endian.
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.

This needs to be fixed?

Quoted 9 lines
include/uapi/linux/if_tun.h +3 -0
+ */
+#define TUNSETVNETHASH _IOW('T', 229, struct tun_vnet_hash)
+
 /* TUNSETIFF ifr flags */
 #define IFF_TUN                0x0001
 #define IFF_TAP                0x0002
@@ -115,4 +143,24 @@ struct tun_filter {
        __u8   addr[][ETH_ALEN];
 };

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

Quoted 10 lines
include/uapi/linux/if_tun.h +9 -0
 
+/**
+ * define TUN_VNET_HASH_REPORT - Request virtio_net hash reporting for vhost
+ */
+#define TUN_VNET_HASH_REPORT   0x0001
+
+/**
+ * struct tun_vnet_hash - virtio_net hashing configuration
+ * @flags:
+ *             Bitmask consists of %TUN_VNET_HASH_REPORT and %TUN_VNET_HASH_RSS

Could we reuse TUNGETIFF by introduce new IFF_XXX stuffs?

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

Quoted 4 lines
include/uapi/linux/if_tun.h +4 -0
+ * @pad:
+ *             Should be filled with zero before passing to %TUNSETVNETHASH
+ * @types:
+ *             Bitmask of allowed hash types

What are they?

Quoted 10 lines
include/uapi/linux/if_tun.h +7 -1
+ */
+struct tun_vnet_hash {
+       __u16 flags;
+       __u8 pad[2];
+       __u32 types;
+};
+
 #endif /* _UAPI__IF_TUN_H */
 
--
Quoted 2 lines

2.46.2

Thanks

#15 lore raw

Re: [PATCH RFC v5 06/10] tun: Introduce virtio-net hash reporting feature

Quoted 8 lines · Jason Wang
Quoted 5 lines · Akihiko Odaki

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

I wonder if this would cause overhead when hash reporting is not enabled?

It only adds two branches in the data path. The first one is in tun_vnet_hash_report(), which determines to add the hash value to sk_buff. The second one is in tun_vnet_hdr_from_skb(), which determines to report the added hash value.

Quoted 7 lines
Quoted 5 lines
---
  Documentation/networking/tuntap.rst |   7 +++
  drivers/net/Kconfig                 |   1 +
  drivers/net/tap.c                   |  45 ++++++++++++++--

Tile should be for tap as well or is this just for tun?

It is also for tap. I will update the title in v6.

Quoted 48 lines
Quoted 30 lines
  drivers/net/tun.c                   |  46 ++++++++++++----
  drivers/net/tun_vnet.h              | 102 +++++++++++++++++++++++++++++++-----
  include/linux/if_tap.h              |   2 +
  include/uapi/linux/if_tun.h         |  48 +++++++++++++++++
  7 files changed, 223 insertions(+), 28 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/Kconfig +0 -0
index 9920b3a68ed1..e2a7bd703550 100644

drivers/net/Kconfig config TUN · line 398

Quoted 4 lines
drivers/net/Kconfig +1 -0
@@ -395,6 +395,7 @@ config TUN
         tristate "Universal TUN/TAP device driver support"
         depends on INET
         select CRC32
+       select SKB_EXTENSIONS

Then we need this for macvtap at least as well?

Quoted 11 lines
drivers/net/Kconfig +0 -0
         help
           TUN/TAP provides packet reception and transmission for user space
           programs.  It can be viewed as a simple Point-to-Point or Ethernet
drivers/net/tap.c +0 -0
index 9a34ceed0c2c..5e2fbe63ca47 100644
@@ -179,6 +179,16 @@ static void tap_put_queue(struct tap_queue *q)
         sock_put(&q->sk);
  }
 
Quoted 6 lines

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

Quoted 3 lines
drivers/net/tap.c +3 -0
+static struct virtio_net_hash *tap_add_hash(struct sk_buff *skb)
+{
+       return (struct virtio_net_hash *)skb->cb;

Any reason that tap uses skb->cb but not skb extensions? (And is it safe to use that without cloning?)

tun adds virtio_net_hash to a skb in ndo_select_queue(), but it does not immediately put it into its ptr_ring; instead ndo_start_xmit() does so. It is hard to ensure that nobody modifies skb->cb between the two calls.

The situation is different for tap. tap_handle_frame() adds virtio_net_hash to a skb and immediately adds it in its ptr_ring so nobody should touch it between that.

Quoted 140 lines
Quoted 140 lines
drivers/net/tap.c +37 -5
+}
+
+static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb)
+{
+       return (const struct virtio_net_hash *)skb->cb;
+}
+
  /*
   * Select a queue based on the rxq of the device on which this packet
   * arrived. If the incoming device is not mq, calculate a flow hash
@@ -189,6 +199,7 @@ static void tap_put_queue(struct tap_queue *q)
  static struct tap_queue *tap_get_queue(struct tap_dev *tap,
                                        struct sk_buff *skb)
  {
+       struct flow_keys_basic keys_basic;
         struct tap_queue *queue = NULL;
         /* Access to taps array is protected by rcu, but access to numvtaps
          * isn't. Below we use it to lookup a queue, but treat it as a hint
@@ -198,15 +209,32 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
         int numvtaps = READ_ONCE(tap->numvtaps);
         __u32 rxq;
 
+       *tap_add_hash(skb) = (struct virtio_net_hash) { .report = VIRTIO_NET_HASH_REPORT_NONE };
+
         if (!numvtaps)
                 goto out;
 
         if (numvtaps == 1)
                 goto single;
 
+       if (!skb->l4_hash && !skb->sw_hash) {
+               struct flow_keys keys;
+
+               skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+               rxq = flow_hash_from_keys(&keys);
+               keys_basic = (struct flow_keys_basic) {
+                       .control = keys.control,
+                       .basic = keys.basic
+               };
+       } else {
+               skb_flow_dissect_flow_keys_basic(NULL, skb, &keys_basic, NULL, 0, 0, 0,
+                                                FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
+               rxq = skb->hash;
+       }
+
         /* Check if we can use flow to select a queue */
-       rxq = skb_get_hash(skb);
         if (rxq) {
+               tun_vnet_hash_report(&tap->vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
                 queue = rcu_dereference(tap->taps[rxq % numvtaps]);
                 goto out;
         }
@@ -713,15 +741,16 @@ 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 < 0)
                         goto done;
 
-               ret = tun_vnet_hdr_put(vnet_hdr_len, iter, &vnet_hdr);
+               ret = tun_vnet_hdr_put(vnet_hdr_len, iter, &vnet_hdr, ret);
                 if (ret < 0)
                         goto done;
         }
@@ -1025,7 +1054,13 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
                 return ret;
 
         default:
-               return tun_vnet_ioctl(&q->vnet_hdr_sz, &q->flags, cmd, sp);
+               rtnl_lock();
+               tap = rtnl_dereference(q->tap);
+               ret = tun_vnet_ioctl(&q->vnet_hdr_sz, &q->flags,
+                                    tap ? &tap->vnet_hash : NULL, -EINVAL,
+                                    cmd, sp);
+               rtnl_unlock();
+               return ret;
         }
  }
 
drivers/net/tun.c +17 -1
index dd8799d19518..27308417b834 100644
@@ -209,6 +209,7 @@ struct tun_struct {
         struct bpf_prog __rcu *xdp_prog;
         struct tun_prog __rcu *steering_prog;
         struct tun_prog __rcu *filter_prog;
+       struct tun_vnet_hash vnet_hash;
         struct ethtool_link_ksettings link_ksettings;
         /* init args */
         struct file *file;
@@ -451,6 +452,16 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
                 e->rps_rxhash = hash;
  }
 
+static struct virtio_net_hash *tun_add_hash(struct sk_buff *skb)
+{
+       return skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
+static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
+{
+       return skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH);
+}
+
  /* We try to identify a flow through its rxhash. The reason that
   * we do not check rxq no. is because some cards(e.g 82599), chooses
   * the rxq based on the txq where the last packet of the flow comes. As
@@ -459,12 +470,17 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
   */
  static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
  {
+       struct flow_keys keys;
+       struct flow_keys_basic keys_basic;
         struct tun_flow_entry *e;
         u32 txq, numqueues;
 
         numqueues = READ_ONCE(tun->numqueues);
 
-       txq = __skb_get_hash_symmetric(skb);
+       memset(&keys, 0, sizeof(keys));
+       skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, 0);
+
+       txq = flow_hash_from_keys(&keys);
         e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
         if (e) {
                 tun_flow_save_rps_rxhash(e, txq);
@@ -473,6 +489,13 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
                 txq = reciprocal_scale(txq, numqueues);
         }
 
Quoted 8 lines

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

Quoted 6 lines
drivers/net/tun.c +6 -0
+       keys_basic = (struct flow_keys_basic) {
+               .control = keys.control,
+               .basic = keys.basic
+       };
+       tun_vnet_hash_report(&tun->vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+                            tun_add_hash);

Is using txq required when not l4_hash is required by the virtio-spec?

It is a limitation of the implementation. A hardware driver may set a hash value with skb_set_hash(), which takes enum pkt_hash_types. The enum is defined as follows:

enum pkt_hash_types {
	PKT_HASH_TYPE_NONE,	/* Undefined type */
	PKT_HASH_TYPE_L2,	/* Input: src_MAC, dest_MAC */
	PKT_HASH_TYPE_L3,	/* Input: src_IP, dst_IP */
	PKT_HASH_TYPE_L4,	/* Input: src_IP, dst_IP, src_port, dst_port */
};

A hash value with PKT_HASH_TYPE_L2 must be ignored as the virtio spec does not have a corresponding hash type. The virtio spec has corresponding hash types for PKT_HASH_TYPE_L3 and PKT_HASH_TYPE_L4 so we should report them with the virtio_net header.

However, skb only tells whether the hash is PKT_HASH_TYPE_L4 or not. So tun reports skb->hash with a L4 hash type if the hash is PKT_HASH_TYPE_L4. Otherwise it ignores skb->hash and uses the hash value computed its own because it cannot tell if skb->hash is PKT_HASH_TYPE_L2 or PKT_HASH_TYPE_L3.

Quoted 48 lines
Quoted 48 lines
drivers/net/tun.c +7 -7
+
         return txq;
  }
 
@@ -1990,10 +2013,8 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
         size_t total;
 
         if (tun->flags & IFF_VNET_HDR) {
-               struct virtio_net_hdr gso = { 0 };
-
                 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
-               ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
+               ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, NULL, 0);
                 if (ret < 0)
                         return ret;
         }
@@ -2018,7 +2039,6 @@ static ssize_t tun_put_user(struct tun_struct *tun,
         int vlan_offset = 0;
         int vlan_hlen = 0;
         int vnet_hdr_sz = 0;
-       int ret;
 
         if (skb_vlan_tag_present(skb))
                 vlan_hlen = VLAN_HLEN;
@@ -2043,13 +2063,15 @@ 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;
+               int ret;
 
-               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 < 0)
                         goto done;
 
-               ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
+               ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso, ret);
                 if (ret < 0)
                         goto done;
         }
@@ -3055,9 +3077,10 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 goto unlock;
         }
 
Quoted 8 lines

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

Quoted 4 lines
drivers/net/tun.c +2 -2
-       ret = -EBADFD;
-       if (!tun)
+       if (!tun) {
+               ret = tun_vnet_ioctl(NULL, NULL, NULL, -EBADFD, cmd, argp);

This seems not elegant (passing three NULL pointers). Any reason we can't just modify __tun_chr_ioctl() instead of introducing things like tun_vnet_ioctl()?

tun_vnet_ioctl() is introduced with patch "tun: Unify vnet implementation". We can abandon unifying the ioctl handling if the interface looks too awkward.

Quoted 33 lines
Quoted 33 lines
drivers/net/tun.c +3 -1
                 goto unlock;
+       }
 
         netif_info(tun, drv, tun->dev, "tun_chr_ioctl cmd %u\n", cmd);
 
@@ -3256,7 +3279,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 break;
 
         default:
-               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags, cmd, argp);
+               ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
+                                    &tun->vnet_hash, -EINVAL, cmd, argp);
         }
 
         if (do_notify)
drivers/net/tun_vnet.h +3 -0
index c40bde0fdf8c..589a97dd7d02 100644
@@ -6,6 +6,9 @@
  #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 *);
+
  static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
  {
         return !(IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) && (flags & TUN_VNET_BE)) &&
@@ -59,18 +62,31 @@ static inline __virtio16 cpu_to_tun_vnet16(unsigned int flags, u16 val)
  }
 
Quoted 12 lines

drivers/net/tun_vnet.h static inline __virtio16 cpu_to_tun_vnet16(unsigned int flags, u16 val) · line 71

Quoted 9 lines
drivers/net/tun_vnet.h +6 -1
  static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
-                                 unsigned int cmd, int __user *sp)
+                                 struct tun_vnet_hash *hash, long fallback,
+                                 unsigned int cmd, void __user *argp)
  {
+       static const struct tun_vnet_hash cap = {
+               .flags = TUN_VNET_HASH_REPORT,
+               .types = VIRTIO_NET_SUPPORTED_HASH_TYPES
+       };

Let's find a way to reuse virtio-net uAPI instead of introducing new stuff to stress the management layer.

I found include/uapi/linux/virtio_net.h inappropriate for this ioctl.

It has the following structure for hash reporting:

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

We do not care hash_key_length and hash_key_data.

It has the following structure for RSS and hash reporting when RSS is enabled:

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

This structure is hard to use as it has data members after the indirection_table flexible array. max_tx_vq is not our interest either.

I tested the usability of ioctl by actually using it in QEMU. The RFC patch series is available at:
https://patchew.org/QEMU/[email protected]/

Quoted 212 lines
Quoted 212 lines
drivers/net/tun_vnet.h +81 -11
+       struct tun_vnet_hash hash_buf;
+       int __user *sp = argp;
         int s;
 
         switch (cmd) {
         case TUNGETVNETHDRSZ:
+               if (!sz)
+                       return -EBADFD;
+
                 s = *sz;
                 if (put_user(s, sp))
                         return -EFAULT;
                 return 0;
 
         case TUNSETVNETHDRSZ:
+               if (!sz)
+                       return -EBADFD;
+
                 if (get_user(s, sp))
                         return -EFAULT;
                 if (s < (int)sizeof(struct virtio_net_hdr))
@@ -80,12 +96,18 @@ static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
                 return 0;
 
         case TUNGETVNETLE:
+               if (!flags)
+                       return -EBADFD;
+
                 s = !!(*flags & TUN_VNET_LE);
                 if (put_user(s, sp))
                         return -EFAULT;
                 return 0;
 
         case TUNSETVNETLE:
+               if (!flags)
+                       return -EBADFD;
+
                 if (get_user(s, sp))
                         return -EFAULT;
                 if (s)
@@ -95,16 +117,56 @@ static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
                 return 0;
 
         case TUNGETVNETBE:
+               if (!flags)
+                       return -EBADFD;
+
                 return tun_vnet_get_be(*flags, sp);
 
         case TUNSETVNETBE:
+               if (!flags)
+                       return -EBADFD;
+
                 return tun_vnet_set_be(flags, sp);
 
+       case TUNGETVNETHASHCAP:
+               return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;
+
+       case TUNSETVNETHASH:
+               if (!hash)
+                       return -EBADFD;
+
+               if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
+                       return -EFAULT;
+
+               *hash = hash_buf;
+               return 0;
+
         default:
-               return -EINVAL;
+               return fallback;
         }
  }
 
+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->flags & TUN_VNET_HASH_REPORT))
+               return;
+
+       report = vnet_hash_add(skb);
+       if (!report)
+               return;
+
+       *report = (struct virtio_net_hash) {
+               .report = virtio_net_hash_report(hash->types, keys),
+               .value = value
+       };
+}
+
  static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                    struct iov_iter *from,
                                    struct virtio_net_hdr *hdr)
@@ -130,15 +192,15 @@ 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 void *hdr, int content_sz)
  {
         if (iov_iter_count(iter) < sz)
                 return -EINVAL;
 
-       if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
+       if (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;
@@ -151,32 +213,48 @@ static inline int tun_vnet_hdr_to_skb(unsigned int flags,
         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);
+       int content_sz;
+
+       if (report) {
+               content_sz = sizeof(struct virtio_net_hdr_v1_hash);
+
+               *hdr = (struct virtio_net_hdr_v1_hash) {
+                       .hdr = { .num_buffers = __cpu_to_virtio16(true, 1) },
+                       .hash_value = cpu_to_le32(report->value),
+                       .hash_report = cpu_to_le16(report->report)
+               };
+       } else {
+               content_sz = sizeof(struct virtio_net_hdr);
+       }
 
-       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;
         }
 
-       return 0;
+       return content_sz;
  }
 
  #endif /* TUN_VNET_H */
include/linux/if_tap.h +2 -0
index 553552fa635c..5bbb343a6dba 100644
@@ -4,6 +4,7 @@
 
  #include <net/sock.h>
  #include <linux/skb_array.h>
+#include <uapi/linux/if_tun.h>
 
  struct file;
  struct socket;
@@ -43,6 +44,7 @@ struct tap_dev {
         int                     numqueues;
         netdev_features_t       tap_features;
         int                     minor;
+       struct tun_vnet_hash    vnet_hash;
 
         void (*update_features)(struct tap_dev *tap, netdev_features_t features);
         void (*count_tx_dropped)(struct tap_dev *tap);
include/uapi/linux/if_tun.h +10 -0
index 287cdc81c939..d11e79b4e0dc 100644
@@ -62,6 +62,34 @@
  #define TUNSETCARRIER _IOW('T', 226, int)
  #define TUNGETDEVNETNS _IO('T', 227)
 
+/**
+ * define TUNGETVNETHASHCAP - ioctl to get virtio_net hashing capability.
+ *
+ * The argument is a pointer to &struct tun_vnet_hash which will store the
+ * maximal virtio_net hashing configuration.
+ */
+#define TUNGETVNETHASHCAP _IOR('T', 228, struct tun_vnet_hash)
+
+/**
+ * define TUNSETVNETHASH - ioctl to configure virtio_net hashing
Quoted 8 lines

include/uapi/linux/if_tun.h line 80

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

I think we don't need & here.

Documentation/doc-guide/kernel-doc.rst says &struct is a token for struct cross-reference.

Quoted 11 lines

include/uapi/linux/if_tun.h line 89

Quoted 10 lines
include/uapi/linux/if_tun.h +9 -0
+ *
+ * The members added to the legacy header by %TUN_VNET_HASH_REPORT flag will
+ * always be little-endian.
+ *
+ * This ioctl results in %EBADFD if the underlying device is deleted. It affects
+ * all queues attached to the same device.
+ *
+ * This ioctl currently has no effect on XDP packets and packets with
+ * queue_mapping set by TC.
Quoted 1 line

This needs to be fixed?

We don't use a hash value to select a queue in such a case so there is no point to report one.

Quoted 11 lines
Quoted 11 lines
include/uapi/linux/if_tun.h +3 -0
+ */
+#define TUNSETVNETHASH _IOW('T', 229, struct tun_vnet_hash)
+
  /* TUNSETIFF ifr flags */
  #define IFF_TUN                0x0001
  #define IFF_TAP                0x0002
@@ -115,4 +143,24 @@ struct tun_filter {
         __u8   addr[][ETH_ALEN];
  };
 
Quoted 11 lines

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

Quoted 9 lines
include/uapi/linux/if_tun.h +9 -0
+/**
+ * define TUN_VNET_HASH_REPORT - Request virtio_net hash reporting for vhost
+ */
+#define TUN_VNET_HASH_REPORT   0x0001
+
+/**
+ * struct tun_vnet_hash - virtio_net hashing configuration
+ * @flags:
+ *             Bitmask consists of %TUN_VNET_HASH_REPORT and %TUN_VNET_HASH_RSS

Could we reuse TUNGETIFF by introduce new IFF_XXX stuffs?

That's certainly doable though I'm a bit worrying that exhausting all bits of IFF_XXX.

Quoted 7 lines

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

Quoted 5 lines
include/uapi/linux/if_tun.h +4 -0
+ * @pad:
+ *             Should be filled with zero before passing to %TUNSETVNETHASH
+ * @types:
+ *             Bitmask of allowed hash types

What are they?

They are defined in the virtio spec and include/uapi/linux/virtio_net.h contains them:

#define VIRTIO_NET_RSS_HASH_TYPE_IPv4          (1 << 0)
#define VIRTIO_NET_RSS_HASH_TYPE_TCPv4         (1 << 1)
#define VIRTIO_NET_RSS_HASH_TYPE_UDPv4         (1 << 2)
#define VIRTIO_NET_RSS_HASH_TYPE_IPv6          (1 << 3)
#define VIRTIO_NET_RSS_HASH_TYPE_TCPv6         (1 << 4)
#define VIRTIO_NET_RSS_HASH_TYPE_UDPv6         (1 << 5)
#define VIRTIO_NET_RSS_HASH_TYPE_IP_EX         (1 << 6)
#define VIRTIO_NET_RSS_HASH_TYPE_TCP_EX        (1 << 7)
#define VIRTIO_NET_RSS_HASH_TYPE_UDP_EX        (1 << 8)
Quoted 16 lines
Quoted 11 lines
include/uapi/linux/if_tun.h +7 -1
+ */
+struct tun_vnet_hash {
+       __u16 flags;
+       __u8 pad[2];
+       __u32 types;
+};
+
  #endif /* _UAPI__IF_TUN_H */
 
--
Quoted 2 lines

2.46.2

Thanks

maintainer
#16 lore raw

Re: [PATCH RFC v5 06/10] tun: Introduce virtio-net hash reporting feature

In reply to Akihiko Odaki

drivers/net/tap.c

Quoted 46 lines · Akihiko Odaki

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

---
 Documentation/networking/tuntap.rst |   7 +++
 drivers/net/Kconfig                 |   1 +
 drivers/net/tap.c                   |  45 ++++++++++++++--
 drivers/net/tun.c                   |  46 ++++++++++++----
 drivers/net/tun_vnet.h              | 102 +++++++++++++++++++++++++++++++-----
 include/linux/if_tap.h              |   2 +
 include/uapi/linux/if_tun.h         |  48 +++++++++++++++++
 7 files changed, 223 insertions(+), 28 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/Kconfig +1 -0
index 9920b3a68ed1..e2a7bd703550 100644
@@ -395,6 +395,7 @@ config TUN
 	tristate "Universal TUN/TAP device driver support"
 	depends on INET
 	select CRC32
+	select SKB_EXTENSIONS
 	help
 	  TUN/TAP provides packet reception and transmission for user space
 	  programs.  It can be viewed as a simple Point-to-Point or Ethernet
drivers/net/tap.c +0 -0
index 9a34ceed0c2c..5e2fbe63ca47 100644

Merge the earlier tiny patch 2 into this one.

#17 lore raw

RFC PATCH v5 7/10 tun: Introduce virtio-net RSS

In reply to Akihiko Odaki

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.

---
 drivers/net/tap.c           | 11 +++++-
 drivers/net/tun.c           | 57 ++++++++++++++++++++-------
 drivers/net/tun_vnet.h      | 96 +++++++++++++++++++++++++++++++++++++++++----
 include/linux/if_tap.h      |  4 +-
 include/uapi/linux/if_tun.h | 27 +++++++++++++
 5 files changed, 169 insertions(+), 26 deletions(-)
drivers/net/tap.c +9 -2
index 5e2fbe63ca47..a58b83285af4 100644
@@ -207,6 +207,7 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
 	 * racing against queue removal.
 	 */
 	int numvtaps = READ_ONCE(tap->numvtaps);
+	struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash);
 	__u32 rxq;
 
 	*tap_add_hash(skb) = (struct virtio_net_hash) { .report = VIRTIO_NET_HASH_REPORT_NONE };
@@ -217,6 +218,12 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
 	if (numvtaps == 1)
 		goto single;
 
+	if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+		rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash);
+		queue = rcu_dereference(tap->taps[rxq]);
+		goto out;
+	}
+
 	if (!skb->l4_hash && !skb->sw_hash) {
 		struct flow_keys keys;
 
@@ -234,7 +241,7 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
 
 	/* Check if we can use flow to select a queue */
 	if (rxq) {
-		tun_vnet_hash_report(&tap->vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
+		tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
 		queue = rcu_dereference(tap->taps[rxq % numvtaps]);
 		goto out;
 	}
@@ -1058,7 +1065,7 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
 		tap = rtnl_dereference(q->tap);
 		ret = tun_vnet_ioctl(&q->vnet_hdr_sz, &q->flags,
 				     tap ? &tap->vnet_hash : NULL, -EINVAL,
-				     cmd, sp);
+				     true, cmd, sp);
 		rtnl_unlock();
 		return ret;
 	}
drivers/net/tun.c +42 -15

123 lines not on this page · raw message

drivers/net/tun_vnet.h +89 -7

147 lines not on this page · raw message

include/linux/if_tap.h +2 -2
index 5bbb343a6dba..7334c46a3f10 100644
@@ -4,7 +4,6 @@
 
 #include <net/sock.h>
 #include <linux/skb_array.h>
-#include <uapi/linux/if_tun.h>
 
 struct file;
 struct socket;
@@ -32,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
 #define MAX_TAP_QUEUES 256
 
 struct tap_queue;
+struct tun_vnet_hash_container;
 
 struct tap_dev {
 	struct net_device	*dev;
@@ -44,7 +44,7 @@ struct tap_dev {
 	int			numqueues;
 	netdev_features_t	tap_features;
 	int			minor;
-	struct tun_vnet_hash	vnet_hash;
+	struct tun_vnet_hash_container __rcu *vnet_hash;
 
 	void (*update_features)(struct tap_dev *tap, netdev_features_t features);
 	void (*count_tx_dropped)(struct tap_dev *tap);
include/uapi/linux/if_tun.h +27 -0
index d11e79b4e0dc..4887f97500a8 100644
@@ -75,6 +75,14 @@
  *
  * The argument is a pointer to &struct tun_vnet_hash.
  *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *
  * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
  * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
  * to the size of &struct virtio_net_hdr_v1_hash.
@@ -148,6 +156,13 @@ struct tun_filter {
  */
 #define TUN_VNET_HASH_REPORT	0x0001
 
+/**
+ * define TUN_VNET_HASH_RSS - Request virtio_net RSS
+ *
+ * This is mutually exclusive with eBPF steering program.
+ */
+#define TUN_VNET_HASH_RSS	0x0002
+
 /**
  * struct tun_vnet_hash - virtio_net hashing configuration
  * @flags:
@@ -163,4 +178,16 @@ struct tun_vnet_hash {
 	__u32 types;
 };
 
+/**
+ * struct tun_vnet_hash_rss - virtio_net RSS configuration
+ * @indirection_table_mask:
+ *		Bitmask to be applied to the indirection table index
+ * @unclassified_queue:
+ *		The index of the queue to place unclassified packets in
+ */
+struct tun_vnet_hash_rss {
+	__u16 indirection_table_mask;
+	__u16 unclassified_queue;
+};
+
 #endif /* _UAPI__IF_TUN_H */
Signature · 1 line
2.46.2
#18 lore raw

Re: [PATCH RFC v5 07/10] tun: Introduce virtio-net RSS

Quoted 257 lines · Akihiko Odaki

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.

---
 drivers/net/tap.c           | 11 +++++-
 drivers/net/tun.c           | 57 ++++++++++++++++++++-------
 drivers/net/tun_vnet.h      | 96 +++++++++++++++++++++++++++++++++++++++++----
 include/linux/if_tap.h      |  4 +-
 include/uapi/linux/if_tun.h | 27 +++++++++++++
 5 files changed, 169 insertions(+), 26 deletions(-)
drivers/net/tap.c +9 -2
index 5e2fbe63ca47..a58b83285af4 100644
@@ -207,6 +207,7 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
         * racing against queue removal.
         */
        int numvtaps = READ_ONCE(tap->numvtaps);
+       struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash);
        __u32 rxq;
 
        *tap_add_hash(skb) = (struct virtio_net_hash) { .report = VIRTIO_NET_HASH_REPORT_NONE };
@@ -217,6 +218,12 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
        if (numvtaps == 1)
                goto single;
 
+       if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+               rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash);
+               queue = rcu_dereference(tap->taps[rxq]);
+               goto out;
+       }
+
        if (!skb->l4_hash && !skb->sw_hash) {
                struct flow_keys keys;
 
@@ -234,7 +241,7 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
 
        /* Check if we can use flow to select a queue */
        if (rxq) {
-               tun_vnet_hash_report(&tap->vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
+               tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
                queue = rcu_dereference(tap->taps[rxq % numvtaps]);
                goto out;
        }
@@ -1058,7 +1065,7 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
                tap = rtnl_dereference(q->tap);
                ret = tun_vnet_ioctl(&q->vnet_hdr_sz, &q->flags,
                                     tap ? &tap->vnet_hash : NULL, -EINVAL,
-                                    cmd, sp);
+                                    true, cmd, sp);
                rtnl_unlock();
                return ret;
        }
drivers/net/tun.c +42 -15
index 27308417b834..18528568aed7 100644
@@ -209,7 +209,7 @@ struct tun_struct {
        struct bpf_prog __rcu *xdp_prog;
        struct tun_prog __rcu *steering_prog;
        struct tun_prog __rcu *filter_prog;
-       struct tun_vnet_hash vnet_hash;
+       struct tun_vnet_hash_container __rcu *vnet_hash;
        struct ethtool_link_ksettings link_ksettings;
        /* init args */
        struct file *file;
@@ -468,7 +468,9 @@ static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
  * the userspace application move between processors, we may get a
  * different rxq no. here.
  */
-static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static u16 tun_automq_select_queue(struct tun_struct *tun,
+                                  const struct tun_vnet_hash_container *vnet_hash,
+                                  struct sk_buff *skb)
 {
        struct flow_keys keys;
        struct flow_keys_basic keys_basic;
@@ -493,7 +495,7 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
                .control = keys.control,
                .basic = keys.basic
        };
-       tun_vnet_hash_report(&tun->vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+       tun_vnet_hash_report(vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
                             tun_add_hash);
 
        return txq;
@@ -523,10 +525,17 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
        u16 ret;
 
        rcu_read_lock();
-       if (rcu_dereference(tun->steering_prog))
+       if (rcu_dereference(tun->steering_prog)) {
                ret = tun_ebpf_select_queue(tun, skb);
-       else
-               ret = tun_automq_select_queue(tun, skb);
+       } else {
+               struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tun->vnet_hash);
+
+               if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS))
+                       ret = tun_vnet_rss_select_queue(READ_ONCE(tun->numqueues), vnet_hash,
+                                                       skb, tun_add_hash);
+               else
+                       ret = tun_automq_select_queue(tun, vnet_hash, skb);
+       }
        rcu_read_unlock();
 
        return ret;
@@ -2248,6 +2257,9 @@ static void tun_free_netdev(struct net_device *dev)
        security_tun_dev_free_security(tun->security);
        __tun_set_ebpf(tun, &tun->steering_prog, NULL);
        __tun_set_ebpf(tun, &tun->filter_prog, NULL);
+       rtnl_lock();
+       kfree_rcu_mightsleep(rtnl_dereference(tun->vnet_hash));
+       rtnl_unlock();
 }
 
 static void tun_setup(struct net_device *dev)
@@ -2946,13 +2958,9 @@ static int tun_set_queue(struct file *file, struct ifreq *ifr)
 }
 
 static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
-                       void __user *data)
+                       int fd)
 {
        struct bpf_prog *prog;
-       int fd;
-
-       if (copy_from_user(&fd, data, sizeof(fd)))
-               return -EFAULT;
 
        if (fd == -1) {
                prog = NULL;
@@ -3019,6 +3027,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
        int sndbuf;
        int ret;
        bool do_notify = false;
+       struct tun_vnet_hash_container *vnet_hash;
 
        if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
            (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
@@ -3078,7 +3087,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
        }
 
        if (!tun) {
-               ret = tun_vnet_ioctl(NULL, NULL, NULL, -EBADFD, cmd, argp);
+               ret = tun_vnet_ioctl(NULL, NULL, NULL, -EBADFD, true, cmd, argp);
                goto unlock;
        }
 
@@ -3256,11 +3265,27 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                break;
 
        case TUNSETSTEERINGEBPF:
-               ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
+               if (get_user(ret, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               vnet_hash = rtnl_dereference(tun->vnet_hash);
+               if (ret != -1 && vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       ret = -EBUSY;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->steering_prog, ret);
                break;
 
        case TUNSETFILTEREBPF:
-               ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
+               if (get_user(ret, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->filter_prog, ret);
                break;
 
        case TUNSETCARRIER:
@@ -3280,7 +3305,9 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 
        default:
                ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
-                                    &tun->vnet_hash, -EINVAL, cmd, argp);
+                                    &tun->vnet_hash, -EINVAL,
+                                    !rtnl_dereference(tun->steering_prog),
+                                    cmd, argp);
        }
 
        if (do_notify)
drivers/net/tun_vnet.h +14 -3
index 589a97dd7d02..f5de4fe9d14e 100644
@@ -9,6 +9,13 @@
 typedef struct virtio_net_hash *(*tun_vnet_hash_add)(struct sk_buff *);
 typedef const struct virtio_net_hash *(*tun_vnet_hash_find)(const struct sk_buff *);
 
+struct tun_vnet_hash_container {
+       struct tun_vnet_hash common;
+       struct tun_vnet_hash_rss rss;
+       u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
+       u16 rss_indirection_table[];
+};
+
 static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
 {
        return !(IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) && (flags & TUN_VNET_BE)) &&
@@ -62,14 +69,16 @@ static inline __virtio16 cpu_to_tun_vnet16(unsigned int flags, u16 val)
 }
 
 static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
-                                 struct tun_vnet_hash *hash, long fallback,
+                                 struct tun_vnet_hash_container __rcu **hashp,
+                                 long fallback, bool can_rss,
                                  unsigned int cmd, void __user *argp)
 {
        static const struct tun_vnet_hash cap = {
-               .flags = TUN_VNET_HASH_REPORT,
+               .flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS,
                .types = VIRTIO_NET_SUPPORTED_HASH_TYPES
        };
        struct tun_vnet_hash hash_buf;
+       struct tun_vnet_hash_container *hash;
        int __user *sp = argp;
        int s;
 
@@ -132,13 +141,57 @@ static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
                return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;
 
        case TUNSETVNETHASH:
-               if (!hash)
+               if (!hashp)
                        return -EBADFD;
 
                if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
                        return -EFAULT;
+               argp = (struct tun_vnet_hash __user *)argp + 1;
+

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+               if (hash_buf.flags & TUN_VNET_HASH_RSS) {
+                       struct tun_vnet_hash_rss rss;
+                       size_t indirection_table_size;
+                       size_t key_size;
+                       size_t size;
+
+                       if (!can_rss)
+                               return -EBUSY;
+
+                       if (copy_from_user(&rss, argp, sizeof(rss)))

This seems to be a change of the uAPI of TUNSETVNETHASH.

Quoted 133 lines
drivers/net/tun_vnet.h +65 -4
+                               return -EFAULT;
+                       argp = (struct tun_vnet_hash_rss __user *)argp + 1;
+
+                       indirection_table_size = ((size_t)rss.indirection_table_mask + 1) * 2;
+                       key_size = virtio_net_hash_key_length(hash_buf.types);
+                       size = struct_size(hash, rss_indirection_table,
+                                          (size_t)rss.indirection_table_mask + 1);
+
+                       hash = kmalloc(size, GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;
+
+                       if (copy_from_user(hash->rss_indirection_table,
+                                          argp, indirection_table_size)) {
+                               kfree(hash);
+                               return -EFAULT;
+                       }
+                       argp = (u16 __user *)argp + rss.indirection_table_mask + 1;
+
+                       if (copy_from_user(hash->rss_key, argp, key_size)) {
+                               kfree(hash);
+                               return -EFAULT;
+                       }
+
+                       virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+                       hash->rss = rss;
+               } else {
+                       hash = kmalloc(sizeof(hash->common), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;
+               }
 
-               *hash = hash_buf;
+               hash->common = hash_buf;
+               kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
                return 0;
 
        default:
@@ -146,7 +199,7 @@ static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
        }
 }
 
-static inline void tun_vnet_hash_report(const struct tun_vnet_hash *hash,
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash_container *hash,
                                        struct sk_buff *skb,
                                        const struct flow_keys_basic *keys,
                                        u32 value,
@@ -154,7 +207,7 @@ static inline void tun_vnet_hash_report(const struct tun_vnet_hash *hash,
 {
        struct virtio_net_hash *report;
 
-       if (!(hash->flags & TUN_VNET_HASH_REPORT))
+       if (!hash || !(hash->common.flags & TUN_VNET_HASH_REPORT))
                return;
 
        report = vnet_hash_add(skb);
@@ -162,11 +215,40 @@ static inline void tun_vnet_hash_report(const struct tun_vnet_hash *hash,
                return;
 
        *report = (struct virtio_net_hash) {
-               .report = virtio_net_hash_report(hash->types, keys),
+               .report = virtio_net_hash_report(hash->common.types, keys),
                .value = value
        };
 }
 
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash_container *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 txq, index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->rss.unclassified_queue % numqueues;
+
+       if (hash->common.flags & TUN_VNET_HASH_REPORT) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->rss.indirection_table_mask;
+       txq = READ_ONCE(hash->rss_indirection_table[index]);
+
+       return txq % numqueues;
+}
+
 static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                   struct iov_iter *from,
                                   struct virtio_net_hdr *hdr)
include/linux/if_tap.h +2 -2
index 5bbb343a6dba..7334c46a3f10 100644
@@ -4,7 +4,6 @@
 
 #include <net/sock.h>
 #include <linux/skb_array.h>
-#include <uapi/linux/if_tun.h>
 
 struct file;
 struct socket;
@@ -32,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
 #define MAX_TAP_QUEUES 256
 
 struct tap_queue;
+struct tun_vnet_hash_container;
 
 struct tap_dev {
        struct net_device       *dev;
@@ -44,7 +44,7 @@ struct tap_dev {
        int                     numqueues;
        netdev_features_t       tap_features;
        int                     minor;
-       struct tun_vnet_hash    vnet_hash;
+       struct tun_vnet_hash_container __rcu *vnet_hash;
 
        void (*update_features)(struct tap_dev *tap, netdev_features_t features);
        void (*count_tx_dropped)(struct tap_dev *tap);
include/uapi/linux/if_tun.h +0 -0
index d11e79b4e0dc..4887f97500a8 100644
@@ -75,6 +75,14 @@
  *

include/uapi/linux/if_tun.h line 85

Quoted 10 lines
include/uapi/linux/if_tun.h +8 -0
  * The argument is a pointer to &struct tun_vnet_hash.
  *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *

Let's try not modify uAPI. We can introduce new ioctl if necessary.

Quoted 36 lines
include/uapi/linux/if_tun.h +19 -1
  * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
  * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
  * to the size of &struct virtio_net_hdr_v1_hash.
@@ -148,6 +156,13 @@ struct tun_filter {
  */
 #define TUN_VNET_HASH_REPORT   0x0001
 
+/**
+ * define TUN_VNET_HASH_RSS - Request virtio_net RSS
+ *
+ * This is mutually exclusive with eBPF steering program.
+ */
+#define TUN_VNET_HASH_RSS      0x0002
+
 /**
  * struct tun_vnet_hash - virtio_net hashing configuration
  * @flags:
@@ -163,4 +178,16 @@ struct tun_vnet_hash {
        __u32 types;
 };
 
+/**
+ * struct tun_vnet_hash_rss - virtio_net RSS configuration
+ * @indirection_table_mask:
+ *             Bitmask to be applied to the indirection table index
+ * @unclassified_queue:
+ *             The index of the queue to place unclassified packets in
+ */
+struct tun_vnet_hash_rss {
+       __u16 indirection_table_mask;
+       __u16 unclassified_queue;
+};
+
 #endif /* _UAPI__IF_TUN_H */
 
--
Quoted 2 lines

2.46.2

Thanks

#19 lore raw

Re: [PATCH RFC v5 07/10] tun: Introduce virtio-net RSS

Quoted 408 lines · Jason Wang
Quoted 257 lines · Akihiko Odaki

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.

---
  drivers/net/tap.c           | 11 +++++-
  drivers/net/tun.c           | 57 ++++++++++++++++++++-------
  drivers/net/tun_vnet.h      | 96 +++++++++++++++++++++++++++++++++++++++++----
  include/linux/if_tap.h      |  4 +-
  include/uapi/linux/if_tun.h | 27 +++++++++++++
  5 files changed, 169 insertions(+), 26 deletions(-)
drivers/net/tap.c +9 -2
index 5e2fbe63ca47..a58b83285af4 100644
@@ -207,6 +207,7 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
          * racing against queue removal.
          */
         int numvtaps = READ_ONCE(tap->numvtaps);
+       struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash);
         __u32 rxq;
 
         *tap_add_hash(skb) = (struct virtio_net_hash) { .report = VIRTIO_NET_HASH_REPORT_NONE };
@@ -217,6 +218,12 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
         if (numvtaps == 1)
                 goto single;
 
+       if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+               rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash);
+               queue = rcu_dereference(tap->taps[rxq]);
+               goto out;
+       }
+
         if (!skb->l4_hash && !skb->sw_hash) {
                 struct flow_keys keys;
 
@@ -234,7 +241,7 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap,
 
         /* Check if we can use flow to select a queue */
         if (rxq) {
-               tun_vnet_hash_report(&tap->vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
+               tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash);
                 queue = rcu_dereference(tap->taps[rxq % numvtaps]);
                 goto out;
         }
@@ -1058,7 +1065,7 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
                 tap = rtnl_dereference(q->tap);
                 ret = tun_vnet_ioctl(&q->vnet_hdr_sz, &q->flags,
                                      tap ? &tap->vnet_hash : NULL, -EINVAL,
-                                    cmd, sp);
+                                    true, cmd, sp);
                 rtnl_unlock();
                 return ret;
         }
drivers/net/tun.c +42 -15
index 27308417b834..18528568aed7 100644
@@ -209,7 +209,7 @@ struct tun_struct {
         struct bpf_prog __rcu *xdp_prog;
         struct tun_prog __rcu *steering_prog;
         struct tun_prog __rcu *filter_prog;
-       struct tun_vnet_hash vnet_hash;
+       struct tun_vnet_hash_container __rcu *vnet_hash;
         struct ethtool_link_ksettings link_ksettings;
         /* init args */
         struct file *file;
@@ -468,7 +468,9 @@ static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb)
   * the userspace application move between processors, we may get a
   * different rxq no. here.
   */
-static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
+static u16 tun_automq_select_queue(struct tun_struct *tun,
+                                  const struct tun_vnet_hash_container *vnet_hash,
+                                  struct sk_buff *skb)
  {
         struct flow_keys keys;
         struct flow_keys_basic keys_basic;
@@ -493,7 +495,7 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
                 .control = keys.control,
                 .basic = keys.basic
         };
-       tun_vnet_hash_report(&tun->vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
+       tun_vnet_hash_report(vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq,
                              tun_add_hash);
 
         return txq;
@@ -523,10 +525,17 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
         u16 ret;
 
         rcu_read_lock();
-       if (rcu_dereference(tun->steering_prog))
+       if (rcu_dereference(tun->steering_prog)) {
                 ret = tun_ebpf_select_queue(tun, skb);
-       else
-               ret = tun_automq_select_queue(tun, skb);
+       } else {
+               struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tun->vnet_hash);
+
+               if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS))
+                       ret = tun_vnet_rss_select_queue(READ_ONCE(tun->numqueues), vnet_hash,
+                                                       skb, tun_add_hash);
+               else
+                       ret = tun_automq_select_queue(tun, vnet_hash, skb);
+       }
         rcu_read_unlock();
 
         return ret;
@@ -2248,6 +2257,9 @@ static void tun_free_netdev(struct net_device *dev)
         security_tun_dev_free_security(tun->security);
         __tun_set_ebpf(tun, &tun->steering_prog, NULL);
         __tun_set_ebpf(tun, &tun->filter_prog, NULL);
+       rtnl_lock();
+       kfree_rcu_mightsleep(rtnl_dereference(tun->vnet_hash));
+       rtnl_unlock();
  }
 
  static void tun_setup(struct net_device *dev)
@@ -2946,13 +2958,9 @@ static int tun_set_queue(struct file *file, struct ifreq *ifr)
  }
 
  static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
-                       void __user *data)
+                       int fd)
  {
         struct bpf_prog *prog;
-       int fd;
-
-       if (copy_from_user(&fd, data, sizeof(fd)))
-               return -EFAULT;
 
         if (fd == -1) {
                 prog = NULL;
@@ -3019,6 +3027,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
         int sndbuf;
         int ret;
         bool do_notify = false;
+       struct tun_vnet_hash_container *vnet_hash;
 
         if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
             (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
@@ -3078,7 +3087,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
         }
 
         if (!tun) {
-               ret = tun_vnet_ioctl(NULL, NULL, NULL, -EBADFD, cmd, argp);
+               ret = tun_vnet_ioctl(NULL, NULL, NULL, -EBADFD, true, cmd, argp);
                 goto unlock;
         }
 
@@ -3256,11 +3265,27 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 break;
 
         case TUNSETSTEERINGEBPF:
-               ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
+               if (get_user(ret, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               vnet_hash = rtnl_dereference(tun->vnet_hash);
+               if (ret != -1 && vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) {
+                       ret = -EBUSY;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->steering_prog, ret);
                 break;
 
         case TUNSETFILTEREBPF:
-               ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
+               if (get_user(ret, (int __user *)argp)) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               ret = tun_set_ebpf(tun, &tun->filter_prog, ret);
                 break;
 
         case TUNSETCARRIER:
@@ -3280,7 +3305,9 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 
         default:
                 ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags,
-                                    &tun->vnet_hash, -EINVAL, cmd, argp);
+                                    &tun->vnet_hash, -EINVAL,
+                                    !rtnl_dereference(tun->steering_prog),
+                                    cmd, argp);
         }
 
         if (do_notify)
drivers/net/tun_vnet.h +14 -3
index 589a97dd7d02..f5de4fe9d14e 100644
@@ -9,6 +9,13 @@
  typedef struct virtio_net_hash *(*tun_vnet_hash_add)(struct sk_buff *);
  typedef const struct virtio_net_hash *(*tun_vnet_hash_find)(const struct sk_buff *);
 
+struct tun_vnet_hash_container {
+       struct tun_vnet_hash common;
+       struct tun_vnet_hash_rss rss;
+       u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
+       u16 rss_indirection_table[];
+};
+
  static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags)
  {
         return !(IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) && (flags & TUN_VNET_BE)) &&
@@ -62,14 +69,16 @@ static inline __virtio16 cpu_to_tun_vnet16(unsigned int flags, u16 val)
  }
 
  static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
-                                 struct tun_vnet_hash *hash, long fallback,
+                                 struct tun_vnet_hash_container __rcu **hashp,
+                                 long fallback, bool can_rss,
                                   unsigned int cmd, void __user *argp)
  {
         static const struct tun_vnet_hash cap = {
-               .flags = TUN_VNET_HASH_REPORT,
+               .flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS,
                 .types = VIRTIO_NET_SUPPORTED_HASH_TYPES
         };
         struct tun_vnet_hash hash_buf;
+       struct tun_vnet_hash_container *hash;
         int __user *sp = argp;
         int s;
 
@@ -132,13 +141,57 @@ static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
                 return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0;
 
         case TUNSETVNETHASH:
-               if (!hash)
+               if (!hashp)
                         return -EBADFD;
 
                 if (copy_from_user(&hash_buf, argp, sizeof(hash_buf)))
                         return -EFAULT;
+               argp = (struct tun_vnet_hash __user *)argp + 1;
+

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

Quoted 10 lines
drivers/net/tun_vnet.h +10 -0
+               if (hash_buf.flags & TUN_VNET_HASH_RSS) {
+                       struct tun_vnet_hash_rss rss;
+                       size_t indirection_table_size;
+                       size_t key_size;
+                       size_t size;
+
+                       if (!can_rss)
+                               return -EBUSY;
+
+                       if (copy_from_user(&rss, argp, sizeof(rss)))

This seems to be a change of the uAPI of TUNSETVNETHASH.

Quoted 137 lines
drivers/net/tun_vnet.h +65 -4
+                               return -EFAULT;
+                       argp = (struct tun_vnet_hash_rss __user *)argp + 1;
+
+                       indirection_table_size = ((size_t)rss.indirection_table_mask + 1) * 2;
+                       key_size = virtio_net_hash_key_length(hash_buf.types);
+                       size = struct_size(hash, rss_indirection_table,
+                                          (size_t)rss.indirection_table_mask + 1);
+
+                       hash = kmalloc(size, GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;
+
+                       if (copy_from_user(hash->rss_indirection_table,
+                                          argp, indirection_table_size)) {
+                               kfree(hash);
+                               return -EFAULT;
+                       }
+                       argp = (u16 __user *)argp + rss.indirection_table_mask + 1;
+
+                       if (copy_from_user(hash->rss_key, argp, key_size)) {
+                               kfree(hash);
+                               return -EFAULT;
+                       }
+
+                       virtio_net_toeplitz_convert_key(hash->rss_key, key_size);
+                       hash->rss = rss;
+               } else {
+                       hash = kmalloc(sizeof(hash->common), GFP_KERNEL);
+                       if (!hash)
+                               return -ENOMEM;
+               }
 
-               *hash = hash_buf;
+               hash->common = hash_buf;
+               kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash));
                 return 0;
 
         default:
@@ -146,7 +199,7 @@ static inline long tun_vnet_ioctl(int *sz, unsigned int *flags,
         }
  }
 
-static inline void tun_vnet_hash_report(const struct tun_vnet_hash *hash,
+static inline void tun_vnet_hash_report(const struct tun_vnet_hash_container *hash,
                                         struct sk_buff *skb,
                                         const struct flow_keys_basic *keys,
                                         u32 value,
@@ -154,7 +207,7 @@ static inline void tun_vnet_hash_report(const struct tun_vnet_hash *hash,
  {
         struct virtio_net_hash *report;
 
-       if (!(hash->flags & TUN_VNET_HASH_REPORT))
+       if (!hash || !(hash->common.flags & TUN_VNET_HASH_REPORT))
                 return;
 
         report = vnet_hash_add(skb);
@@ -162,11 +215,40 @@ static inline void tun_vnet_hash_report(const struct tun_vnet_hash *hash,
                 return;
 
         *report = (struct virtio_net_hash) {
-               .report = virtio_net_hash_report(hash->types, keys),
+               .report = virtio_net_hash_report(hash->common.types, keys),
                 .value = value
         };
  }
 
+static inline u16 tun_vnet_rss_select_queue(u32 numqueues,
+                                           const struct tun_vnet_hash_container *hash,
+                                           struct sk_buff *skb,
+                                           tun_vnet_hash_add vnet_hash_add)
+{
+       struct virtio_net_hash *report;
+       struct virtio_net_hash ret;
+       u16 txq, index;
+
+       if (!numqueues)
+               return 0;
+
+       virtio_net_hash_rss(skb, hash->common.types, hash->rss_key, &ret);
+
+       if (!ret.report)
+               return hash->rss.unclassified_queue % numqueues;
+
+       if (hash->common.flags & TUN_VNET_HASH_REPORT) {
+               report = vnet_hash_add(skb);
+               if (report)
+                       *report = ret;
+       }
+
+       index = ret.value & hash->rss.indirection_table_mask;
+       txq = READ_ONCE(hash->rss_indirection_table[index]);
+
+       return txq % numqueues;
+}
+
  static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
                                    struct iov_iter *from,
                                    struct virtio_net_hdr *hdr)
include/linux/if_tap.h +2 -2
index 5bbb343a6dba..7334c46a3f10 100644
@@ -4,7 +4,6 @@
 
  #include <net/sock.h>
  #include <linux/skb_array.h>
-#include <uapi/linux/if_tun.h>
 
  struct file;
  struct socket;
@@ -32,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f)
  #define MAX_TAP_QUEUES 256
 
  struct tap_queue;
+struct tun_vnet_hash_container;
 
  struct tap_dev {
         struct net_device       *dev;
@@ -44,7 +44,7 @@ struct tap_dev {
         int                     numqueues;
         netdev_features_t       tap_features;
         int                     minor;
-       struct tun_vnet_hash    vnet_hash;
+       struct tun_vnet_hash_container __rcu *vnet_hash;
 
         void (*update_features)(struct tap_dev *tap, netdev_features_t features);
         void (*count_tx_dropped)(struct tap_dev *tap);
include/uapi/linux/if_tun.h +2 -0
index d11e79b4e0dc..4887f97500a8 100644
@@ -75,6 +75,14 @@
   *
   * The argument is a pointer to &struct tun_vnet_hash.
   *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
Quoted 8 lines

include/uapi/linux/if_tun.h line 85

Quoted 6 lines
include/uapi/linux/if_tun.h +6 -0
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *

Let's try not modify uAPI. We can introduce new ioctl if necessary.

2, 3, and 4 are new additions. Adding a separate ioctl for them means we need to call two ioctls to configure RSS and it is hard to design the interactions with them.

For example, if we set TUN_VNET_HASH_RSS with TUNSETVNETHASH before setting struct tun_vnet_hash_rss with another ioctl, tuntap will enable RSS with undefined parameters. Setting struct tun_vnet_hash_rss with TUN_VNET_HASH_RSS unset also sounds unreasnoable.

Letting the new ioctl set TUN_VNET_HASH_RSS does not help either. TUNSETVNETHASH still sets the bitmask of allowed hash types so RSS will depend on two ioctls.

Quoted 42 lines
Quoted 37 lines
include/uapi/linux/if_tun.h +19 -1
   * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only
   * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal
   * to the size of &struct virtio_net_hdr_v1_hash.
@@ -148,6 +156,13 @@ struct tun_filter {
   */
  #define TUN_VNET_HASH_REPORT   0x0001
 
+/**
+ * define TUN_VNET_HASH_RSS - Request virtio_net RSS
+ *
+ * This is mutually exclusive with eBPF steering program.
+ */
+#define TUN_VNET_HASH_RSS      0x0002
+
  /**
   * struct tun_vnet_hash - virtio_net hashing configuration
   * @flags:
@@ -163,4 +178,16 @@ struct tun_vnet_hash {
         __u32 types;
  };
 
+/**
+ * struct tun_vnet_hash_rss - virtio_net RSS configuration
+ * @indirection_table_mask:
+ *             Bitmask to be applied to the indirection table index
+ * @unclassified_queue:
+ *             The index of the queue to place unclassified packets in
+ */
+struct tun_vnet_hash_rss {
+       __u16 indirection_table_mask;
+       __u16 unclassified_queue;
+};
+
  #endif /* _UAPI__IF_TUN_H */
 
--
Quoted 2 lines

2.46.2

Thanks

#20 lore raw

Re: [PATCH RFC v5 07/10] tun: Introduce virtio-net RSS

Quoted 31 lines · Akihiko Odaki
Quoted 29 lines · Jason Wang
Quoted 28 lines · Akihiko Odaki

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.

---
  drivers/net/tap.c           | 11 +++++-
  drivers/net/tun.c           | 57 ++++++++++++++++++++-------
  drivers/net/tun_vnet.h      | 96 +++++++++++++++++++++++++++++++++++++++++----
  include/linux/if_tap.h      |  4 +-
  include/uapi/linux/if_tun.h | 27 +++++++++++++
  5 files changed, 169 insertions(+), 26 deletions(-)

[...]

Quoted 23 lines
Quoted 10 lines
Quoted 10 lines
include/uapi/linux/if_tun.h +2 -0
index d11e79b4e0dc..4887f97500a8 100644
@@ -75,6 +75,14 @@
   *
   * The argument is a pointer to &struct tun_vnet_hash.
   *
+ * The argument is a pointer to the compound of the following in order if
+ * %TUN_VNET_HASH_RSS is set:
Quoted 8 lines

include/uapi/linux/if_tun.h line 85

Quoted 6 lines
include/uapi/linux/if_tun.h +6 -0
+ *
+ * 1. &struct tun_vnet_hash
+ * 2. &struct tun_vnet_hash_rss
+ * 3. Indirection table
+ * 4. Key
+ *

Let's try not modify uAPI. We can introduce new ioctl if necessary.

2, 3, and 4 are new additions. Adding a separate ioctl for them means we need to call two ioctls to configure RSS and it is hard to design the interactions with them.

Quoted 8 lines

For example, if we set TUN_VNET_HASH_RSS with TUNSETVNETHASH before setting struct tun_vnet_hash_rss with another ioctl, tuntap will enable RSS with undefined parameters. Setting struct tun_vnet_hash_rss with TUN_VNET_HASH_RSS unset also sounds unreasnoable.

Letting the new ioctl set TUN_VNET_HASH_RSS does not help either. TUNSETVNETHASH still sets the bitmask of allowed hash types so RSS will depend on two ioctls.

I meant let's avoid introducing an ioctl with function 1 in one patch, and adding 2,3,4 in exactly the same ioctl in the following. It breaks the uABI consistency and bisection.

We can add all in one patch.

Thanks

#21 lore raw

RFC PATCH v5 8/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 | 74 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
tools/testing/selftests/net/tun.c +74 -0
index fa83918b62d1..463dd98f2b80 100644
@@ -159,4 +159,78 @@ TEST_F(tun, reattach_close_delete) {
 	EXPECT_EQ(tun_delete(self->ifname), 0);
 }
 
+FIXTURE(tun_deleted)
+{
+	char ifname[IFNAMSIZ];
+	int fd;
+};
+
+FIXTURE_SETUP(tun_deleted)
+{
+	self->ifname[0] = 0;
+	self->fd = tun_alloc(self->ifname);
+	ASSERT_LE(0, self->fd);
+
+	ASSERT_EQ(0, tun_delete(self->ifname))
+		EXPECT_EQ(0, close(self->fd));
+}
+
+FIXTURE_TEARDOWN(tun_deleted)
+{
+	EXPECT_EQ(0, close(self->fd));
+}
+
+TEST_F(tun_deleted, getvnethdrsz)
+{
+	ASSERT_EQ(-1, ioctl(self->fd, TUNGETVNETHDRSZ));
+	EXPECT_EQ(EBADFD, errno);
+}
+
+TEST_F(tun_deleted, setvnethdrsz)
+{
+	ASSERT_EQ(-1, ioctl(self->fd, TUNSETVNETHDRSZ));
+	EXPECT_EQ(EBADFD, errno);
+}
+
+TEST_F(tun_deleted, getvnetle)
+{
+	ASSERT_EQ(-1, ioctl(self->fd, TUNGETVNETLE));
+	EXPECT_EQ(EBADFD, errno);
+}
+
+TEST_F(tun_deleted, setvnetle)
+{
+	ASSERT_EQ(-1, ioctl(self->fd, TUNSETVNETLE));
+	EXPECT_EQ(EBADFD, errno);
+}
+
+TEST_F(tun_deleted, getvnetbe)
+{
+	ASSERT_EQ(-1, ioctl(self->fd, TUNGETVNETBE));
+	EXPECT_EQ(EBADFD, errno);
+}
+
+TEST_F(tun_deleted, setvnetbe)
+{
+	ASSERT_EQ(-1, ioctl(self->fd, TUNSETVNETBE));
+	EXPECT_EQ(EBADFD, errno);
+}
+
+TEST_F(tun_deleted, getvnethashcap)
+{
+	struct tun_vnet_hash cap;
+	int i = ioctl(self->fd, TUNGETVNETHASHCAP, &cap);
+
+	if (i == -1 && errno == EBADFD)
+		SKIP(return, "TUNGETVNETHASHCAP not supported");
+
+	EXPECT_EQ(0, i);
+}
+
+TEST_F(tun_deleted, setvnethash)
+{
+	ASSERT_EQ(-1, ioctl(self->fd, TUNSETVNETHASH));
+	EXPECT_EQ(EBADFD, errno);
+}
+
 TEST_HARNESS_MAIN
Signature · 1 line
2.46.2
#22 lore raw

RFC PATCH v5 9/10 selftest: tun: Add tests for virtio-net hashing

In reply to Akihiko Odaki

The added tests confirm tun can perform RSS and hash reporting, and reject invalid configurations for them.

---
 tools/testing/selftests/net/Makefile |   2 +-
 tools/testing/selftests/net/tun.c    | 558 ++++++++++++++++++++++++++++++++++-
 2 files changed, 551 insertions(+), 9 deletions(-)
tools/testing/selftests/net/Makefile +1 -1
index 9d5aa817411b..8e2ab5068171 100644
@@ -110,6 +110,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 +550 -8

618 lines not on this page · raw message

Signature · 1 line
2.46.2
#23 lore raw

RFC PATCH v5 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 | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/vhost/net.c +12 -4
index f16279351db5..ec1167a782ec 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)
 };
@@ -1604,10 +1605,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;
@@ -1688,6 +1692,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.46.2