[RFC PATCH 6/7] tun: populate hash in virtio-net header when needed

From: Yuri Benditovich
Date: Tue Jan 05 2021 - 07:26:55 EST


If the BPF program populated the hash in the skb the tun
propagates the hash value and hash report type to the
respective fields of virtio-net header.

Signed-off-by: Yuri Benditovich <yuri.benditovich@xxxxxxxxxx>
---
drivers/net/tun.c | 25 ++++++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 45f4f04a4a3e..214feb0b16fb 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -556,15 +556,20 @@ static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
{
struct tun_prog *prog;
u32 numqueues;
- u16 ret = 0;
+ u32 ret = 0;

numqueues = READ_ONCE(tun->numqueues);
if (!numqueues)
return 0;

prog = rcu_dereference(tun->steering_prog);
- if (prog)
+ if (prog) {
ret = bpf_prog_run_clear_cb(prog->prog, skb);
+ if (tun->bpf_populates_hash) {
+ *skb_hash_report_type(skb) = (__u8)(ret >> 16);
+ ret &= 0xffff;
+ }
+ }

return ret % numqueues;
}
@@ -2062,6 +2067,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,

if (vnet_hdr_sz) {
struct virtio_net_hdr gso;
+ __u16 extra_copy = 0;

if (iov_iter_count(iter) < vnet_hdr_sz)
return -EINVAL;
@@ -2085,7 +2091,20 @@ static ssize_t tun_put_user(struct tun_struct *tun,
if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
return -EFAULT;

- iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
+ if (tun->bpf_populates_hash &&
+ vnet_hdr_sz >= sizeof(struct virtio_net_hdr_v1_hash)) {
+ struct virtio_net_hdr_v1_hash hdr;
+
+ hdr.hdr.num_buffers = 0;
+ hdr.hash_value = cpu_to_le32(skb_get_hash(skb));
+ hdr.hash_report = cpu_to_le16(*skb_hash_report_type(skb));
+ hdr.padding = 0;
+ extra_copy = sizeof(hdr) - sizeof(gso);
+ if (copy_to_iter(&hdr.hdr.num_buffers, extra_copy, iter) != extra_copy)
+ return -EFAULT;
+ }
+
+ iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso) - extra_copy);
}

if (vlan_hlen) {
--
2.17.1