From nobody Fri Nov 29 23:48:49 2024 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E972F1B85C2 for ; Sun, 15 Sep 2024 01:20:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726363209; cv=none; b=QBSjWJuglmRSWzrevdsEqAnyqxSMMJaDextSmCg0LL0r9xETyOqEqha60p3ysF9Y5MEuCbDqOnJLOi4obah5rzhvNoAJYTH8UH5bp4y7uof7I3U5RfJl32CC8HtuGuBXByg33NGdIKE9sBXpclzVPFDY/Q2YeMiXtB4Pv51o8so= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726363209; c=relaxed/simple; bh=WmBSaC/QSbjobyoahXyzvbyjUk5hRznJbyxtyhVOrGI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=cAbh0OyowvjfilVnzLgF/OoZxHyOn4NJHmJ1QvsBjJOwqi+wz2nKx9fPZbCtsyY4Xqpd7FoPZi1z0TDTG0qnoXg5fgPxSZdAtVf6qfRHr8a0pTWtnsccorYAaEEFTUb96eXPEZfYhoTPVR3DdLR1IxDmYmsbvo+21eU5Qm88SI8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=none smtp.mailfrom=daynix.com; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b=y5DQFx3y; arc=none smtp.client-ip=209.85.216.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=daynix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=daynix-com.20230601.gappssmtp.com header.i=@daynix-com.20230601.gappssmtp.com header.b="y5DQFx3y" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2d88edf1340so1517349a91.1 for ; Sat, 14 Sep 2024 18:20:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1726363207; x=1726968007; darn=vger.kernel.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=iYyQLQOeFQUpiZoKosJFqYZWB+Aa4MJVtAn2xVSJv2M=; b=y5DQFx3y/TgLLufOMaV+l5kgEveh27DCPr/TfcX/4Rpcs+JRbKeu8Op8kzUXmUdP6D 8oW+g8HmjcjPZLmxpJE2DMmNWX55G1JwsHS8aqbB5KGq3LdiS0WkDM5HUMJIrreVLb39 lrWK6MGgWK3UZ5dC0Pd9H5gxI4agKtWxACCN0Wvi5KFvyGY6zG3zfjruKZUdwidoaRqb EqfHoEPjlYcEqIqefd9+m9A0Fgc4M7EEijeYsqcZXIntBU8Ns9JHK2cG22LoFBeOClpP VL5eNO2UBxkD5m5o6vuMIoe3FActhlPDKp4rXxj4oxkmBW91k9FGZR7MxWcCDnCXROet p0bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726363207; x=1726968007; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iYyQLQOeFQUpiZoKosJFqYZWB+Aa4MJVtAn2xVSJv2M=; b=CbyFTqgIU4OAbC3Ed3kOuYetrRvl1oIr3k30FG+GJZJRecpAi2a7uqkw84oY3YZ6aE i8+r+4sx/w2wLQT6tZZd/AW5Db7xHcE/p+VkO+d+gY2+4C8KoX6QklfLYXAYcBcb1IXG 2EdjKZp/bj10v1DSdIsf1v5ib19OyId97ihoOkZbONFfvY/bCxCBoWGDl2Arbe1IR+yD oaBfVwRCmgwGp1SL8iJoBRX0UuhZduA7+N1kwAQ2/Ws5jCTg0QjvXcIDQLHYNj1s5BjA 6a2txdxs/V0iFZ8oAyfkrZ9M1c2ynOzH8dsEHiMEbR/7QTBoRtlDEUOOWm7yEmMKnJYx DEaA== X-Forwarded-Encrypted: i=1; AJvYcCWfjIyfkAxjqqx7kLj4yRT/sCQo0OPFV6+DQ1ULrR2s+QXhzkjM9tleDZDX7LOeoDPx9d20J/UuKfl0Mlc=@vger.kernel.org X-Gm-Message-State: AOJu0YzJDVK0ZI1ZJkM+aPljAXKlxtt0vxEKacIUXP+3zJT1Z7JLtr9/ EUUM8D3HRBtNuxMRAjeLKTLbmTx8Abq97QuSpNhdmVz+AhkXpvlJXtMtk4yarJc= X-Google-Smtp-Source: AGHT+IGyUfC3/ABtY9/wIl+W6Qg+BNPReG5FAyawSqjluEKUCPEWa7sstjTnvfK0g1w76208slgCjQ== X-Received: by 2002:a17:90b:3ec1:b0:2d8:ebef:547 with SMTP id 98e67ed59e1d1-2dbb9f08b33mr9461616a91.35.1726363207168; Sat, 14 Sep 2024 18:20:07 -0700 (PDT) Received: from localhost ([210.160.217.68]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-2dbcfcbadd0sm2227646a91.11.2024.09.14.18.19.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 14 Sep 2024 18:20:06 -0700 (PDT) From: Akihiko Odaki Date: Sun, 15 Sep 2024 10:17:46 +0900 Subject: [PATCH RFC v3 7/9] tun: Introduce virtio-net RSS Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240915-rss-v3-7-c630015db082@daynix.com> References: <20240915-rss-v3-0-c630015db082@daynix.com> In-Reply-To: <20240915-rss-v3-0-c630015db082@daynix.com> To: Jonathan Corbet , Willem de Bruijn , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Xuan Zhuo , Shuah Khan , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kselftest@vger.kernel.org, Yuri Benditovich , Andrew Melnychenko , Akihiko Odaki X-Mailer: b4 0.14-dev-fd6e3 RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS. Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program. Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs. Signed-off-by: Akihiko Odaki --- drivers/net/tun.c | 119 +++++++++++++++++++++++++++++++++++++++-= ---- include/uapi/linux/if_tun.h | 27 ++++++++++ 2 files changed, 133 insertions(+), 13 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index b8fcd71becac..5a429b391144 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -175,6 +175,9 @@ struct tun_prog { =20 struct tun_vnet_hash_container { struct tun_vnet_hash common; + struct tun_vnet_hash_rss rss; + __be32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE]; + u16 rss_indirection_table[]; }; =20 /* Since the socket were moved to tun_file, to preserve the behavior of pe= rsist @@ -227,7 +230,7 @@ struct veth { }; =20 static const struct tun_vnet_hash tun_vnet_hash_cap =3D { - .flags =3D TUN_VNET_HASH_REPORT, + .flags =3D TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS, .types =3D VIRTIO_NET_SUPPORTED_HASH_TYPES }; =20 @@ -591,6 +594,36 @@ static u16 tun_ebpf_select_queue(struct tun_struct *tu= n, struct sk_buff *skb) return ret % numqueues; } =20 +static u16 tun_vnet_rss_select_queue(struct tun_struct *tun, + struct sk_buff *skb, + const struct tun_vnet_hash_container *vnet_hash) +{ + struct tun_vnet_hash_ext *ext; + struct virtio_net_hash hash; + u32 numqueues =3D READ_ONCE(tun->numqueues); + u16 txq, index; + + if (!numqueues) + return 0; + + if (!virtio_net_hash_rss(skb, vnet_hash->common.types, vnet_hash->rss_key, + &hash)) + return vnet_hash->rss.unclassified_queue % numqueues; + + if (vnet_hash->common.flags & TUN_VNET_HASH_REPORT) { + ext =3D skb_ext_add(skb, SKB_EXT_TUN_VNET_HASH); + if (ext) { + ext->value =3D hash.value; + ext->report =3D hash.report; + } + } + + index =3D hash.value & vnet_hash->rss.indirection_table_mask; + txq =3D READ_ONCE(vnet_hash->rss_indirection_table[index]); + + return txq % numqueues; +} + static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, struct net_device *sb_dev) { @@ -603,7 +636,10 @@ static u16 tun_select_queue(struct net_device *dev, st= ruct sk_buff *skb, } else { struct tun_vnet_hash_container *vnet_hash =3D rcu_dereference(tun->vnet_= hash); =20 - ret =3D tun_automq_select_queue(tun, skb, vnet_hash); + if (vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_RSS)) + ret =3D tun_vnet_rss_select_queue(tun, skb, vnet_hash); + else + ret =3D tun_automq_select_queue(tun, skb, vnet_hash); } rcu_read_unlock(); =20 @@ -3085,13 +3121,9 @@ static int tun_set_queue(struct file *file, struct i= freq *ifr) } =20 static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **pr= og_p, - void __user *data) + int fd) { struct bpf_prog *prog; - int fd; - - if (copy_from_user(&fd, data, sizeof(fd))) - return -EFAULT; =20 if (fd =3D=3D -1) { prog =3D NULL; @@ -3157,6 +3189,7 @@ static long __tun_chr_ioctl(struct file *file, unsign= ed int cmd, int ifindex; int sndbuf; int vnet_hdr_sz; + int fd; int le; int ret; bool do_notify =3D false; @@ -3460,11 +3493,27 @@ static long __tun_chr_ioctl(struct file *file, unsi= gned int cmd, break; =20 case TUNSETSTEERINGEBPF: - ret =3D tun_set_ebpf(tun, &tun->steering_prog, argp); + if (get_user(fd, (int __user *)argp)) { + ret =3D -EFAULT; + break; + } + + vnet_hash =3D rtnl_dereference(tun->vnet_hash); + if (fd !=3D -1 && vnet_hash && (vnet_hash->common.flags & TUN_VNET_HASH_= RSS)) { + ret =3D -EBUSY; + break; + } + + ret =3D tun_set_ebpf(tun, &tun->steering_prog, fd); break; =20 case TUNSETFILTEREBPF: - ret =3D tun_set_ebpf(tun, &tun->filter_prog, argp); + if (get_user(fd, (int __user *)argp)) { + ret =3D -EFAULT; + break; + } + + ret =3D tun_set_ebpf(tun, &tun->filter_prog, fd); break; =20 case TUNSETCARRIER: @@ -3496,10 +3545,54 @@ static long __tun_chr_ioctl(struct file *file, unsi= gned int cmd, break; } =20 - vnet_hash =3D kmalloc(sizeof(vnet_hash->common), GFP_KERNEL); - if (!vnet_hash) { - ret =3D -ENOMEM; - break; + if (vnet_hash_common.flags & TUN_VNET_HASH_RSS) { + struct tun_vnet_hash_rss rss; + size_t indirection_table_size; + size_t key_size; + size_t size; + + if (tun->steering_prog) { + ret =3D -EBUSY; + break; + } + + if (copy_from_user(&rss, argp, sizeof(rss))) { + ret =3D -EFAULT; + break; + } + argp =3D (struct tun_vnet_hash_rss __user *)argp + 1; + + indirection_table_size =3D ((size_t)rss.indirection_table_mask + 1) * 2; + key_size =3D virtio_net_hash_key_length(vnet_hash_common.types); + size =3D sizeof(*vnet_hash) + indirection_table_size + key_size; + + vnet_hash =3D kmalloc(size, GFP_KERNEL); + if (!vnet_hash) { + ret =3D -ENOMEM; + break; + } + + if (copy_from_user(vnet_hash->rss_indirection_table, + argp, indirection_table_size)) { + kfree(vnet_hash); + ret =3D -EFAULT; + break; + } + argp =3D (u16 __user *)argp + rss.indirection_table_mask + 1; + + if (copy_from_user(vnet_hash->rss_key, argp, key_size)) { + kfree(vnet_hash); + ret =3D -EFAULT; + break; + } + + vnet_hash->rss =3D rss; + } else { + vnet_hash =3D kmalloc(sizeof(vnet_hash->common), GFP_KERNEL); + if (!vnet_hash) { + ret =3D -ENOMEM; + break; + } } =20 vnet_hash->common =3D vnet_hash_common; diff --git a/include/uapi/linux/if_tun.h b/include/uapi/linux/if_tun.h index 1561e8ce0a0a..1c130409db5d 100644 --- a/include/uapi/linux/if_tun.h +++ b/include/uapi/linux/if_tun.h @@ -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 + * * %TUNSETVNETHDRSZ ioctl must be called with a number greater than or equ= al to * the size of &struct virtio_net_hdr_v1_hash before calling this ioctl wi= th * %TUN_VNET_HASH_REPORT. @@ -144,6 +152,13 @@ struct tun_filter { */ #define TUN_VNET_HASH_REPORT 0x0001 =20 +/** + * 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: @@ -159,4 +174,16 @@ struct tun_vnet_hash { __u32 types; }; =20 +/** + * 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 */ --=20 2.46.0