From patchwork Thu Mar 13 07:01:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014452 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 979401FCD08 for ; Thu, 13 Mar 2025 07:01:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849293; cv=none; b=ghl/QaQA93sZ5elrLfLsSTUO0ozY0O89Nw3CkueALI79GpKVewPKAuktHMqeqNpJSosf4ftzUmXL8V6wOLSdcyqayKTXp4xGQqUOATBRgFfdL9rGzD6ApbkACM9lYKOepN87Ax1Tq7frfre+0EYlMWwXr/Y5wl4YKgFG9tfG3tI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849293; c=relaxed/simple; bh=A53HI8g8TXDmrSrb60YtMTi8R2diB0tM4HU4FoVrwbo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=Yxtk+9UX7FhwAWgKiY1hfArRG6iB+FrJ4vRlNV60o6UEQhf9i67daAtvtywvQuBVs5TT70tH284RfRuLFRmt+UW7TArWF0s9E0rab017pqR3RWIlPiy2d41ONu4wYUle0Z4QaKm2VLHZ+8mXjPXaG0nIh7L9u7Kjqe2l8lerW/s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=XfqLkaQ9; arc=none smtp.client-ip=209.85.216.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass 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="XfqLkaQ9" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2f9b9c0088fso1228272a91.0 for ; Thu, 13 Mar 2025 00:01:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849291; x=1742454091; 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=fDwrrcdF9GBvmBADJUBP7MW6rPk3woZkA2TcvHwrsYM=; b=XfqLkaQ97jZVZUvmodLSLNJ4wG6gBKLmb1/OT6mAql6WMkkDIsJtZ96uGFV4yfwjlv CwV4BGYc6bTfAPK7KCugnVHh13Bmbyr6mT9ar1Ap9IFZYbaX1mtMv14OyWHApXNx7Ycy AlspjUuXE7hj8pBXJWq5U8AuDr+ebr57XirfzoZBjiQeIh7QF3Zr183spHP6gkPsMvK1 iQl/WhW7JeQtZWdOeWMjbyielu1DXjA1XpVBCIBfBUkfnMBXw4pkP6wm04bVqJxg+jNT fvCANCpvNmTWUn10B9sKX3FZKQx85VoT6HeCBkhrDy6dLhOqQ9Y8hnAz0+6PmT9z5fHm /bdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849291; x=1742454091; 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=fDwrrcdF9GBvmBADJUBP7MW6rPk3woZkA2TcvHwrsYM=; b=mhsMcVHK2TGormShHeLa9nvzx3dEAlng9W3oc3VnKSdhPwFG6D74drYp8SOxpsFm6X KNN9whd7UwBpCJjWIJMo8+EoyE/SudE2uhdI2fmO57XojbhG6psXdNoCP8bLDaJYqYTB V+QlbPTJQBpBBtSuDOH0eV8CQDC+odhalRE/iK1f9Xwaye2qVQWufO8PkE0fQ7hedks5 F82Tps1G8gEuy5DCZMM224Hu88Dm6/OIntZlsFNwRXwBpKxrJ+d0Esnd+HnG3+ZKSkUk yOkuBblPdZ7tH7ov/SCuimkUMAOpJbg0HRoaObvIFbibXi0OVbQnxF3toAB7+NxNOQSn z5yg== X-Forwarded-Encrypted: i=1; AJvYcCWIoz1pHZtSARHKjckcKYYHegN8ESrOgJzFLgs41B+IjvWWXq1qUkpQ9cIKLlqrJECGgI8=@vger.kernel.org X-Gm-Message-State: AOJu0Ywg4I43VagvS95PeMJXtThVczqdJgB2kaMUt739CPAirjxT9mEL kf+YCf3GT3/yooXtaT44jsI/HADoKjK9hnC+IdWqeGonABf9o6kkAJV8Kqn9QHs= X-Gm-Gg: ASbGncu1QR+mAPhegEeRIZQxncnPVam4ie5uZnyrICBKzHzcJ8lAYUiUKlcqCaufkMk Xf+fFtkY9lZklgjqO/qshJhG07arxhZOfpXr2t24VFgnkCEItd2t8kxW4KooHd3ZSRYeCH6EJAw Hw9DD34I+quAmgGg5lSQ1tG0cAuzqRvoBGKYbrrLPChfBdp87uzx1/Ky71Q5in7YYm42HG6dZ78 uIw1d5wOQHdVF3eWjSqLNYj1MKtLDwmvGoDa9xdMkMWAjKNuYiC98hDGzSz5a/iJLF9xF5ppPr7 nHmkWPXWZK9aflsFXBsnv+VBHlHU22Vk2QCd2KjqSYFzWtCy X-Google-Smtp-Source: AGHT+IGaJtk0WNynsJ9JKCAqWUVuEtMPgSG06nZ5ziS8mniutUxDW6cCWzTaKgfpp9YCPQ2Z9T9CgA== X-Received: by 2002:a17:90b:5247:b0:2fa:6793:e860 with SMTP id 98e67ed59e1d1-30135da61b6mr2252846a91.0.1741849291028; Thu, 13 Mar 2025 00:01:31 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-30118218423sm3400131a91.2.2025.03.13.00.01.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:01:30 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:04 +0900 Subject: [PATCH net-next v10 01/10] virtio_net: Add functions for hashing Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-1-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 They are useful to implement VIRTIO_NET_F_RSS and VIRTIO_NET_F_HASH_REPORT. Signed-off-by: Akihiko Odaki Tested-by: Lei Yang --- include/linux/virtio_net.h | 188 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h index 02a9f4dc594d..426f33b4b824 100644 --- a/include/linux/virtio_net.h +++ b/include/linux/virtio_net.h @@ -9,6 +9,194 @@ #include #include +struct virtio_net_hash { + u32 value; + u16 report; +}; + +struct virtio_net_toeplitz_state { + u32 hash; + const u32 *key; +}; + +#define VIRTIO_NET_SUPPORTED_HASH_TYPES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \ + VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \ + VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \ + VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \ + VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \ + VIRTIO_NET_RSS_HASH_TYPE_UDPv6) + +#define VIRTIO_NET_RSS_MAX_KEY_SIZE 40 + +static inline void virtio_net_toeplitz_convert_key(u32 *input, size_t len) +{ + while (len >= sizeof(*input)) { + *input = be32_to_cpu((__force __be32)*input); + input++; + len -= sizeof(*input); + } +} + +static inline void virtio_net_toeplitz_calc(struct virtio_net_toeplitz_state *state, + const __be32 *input, size_t len) +{ + while (len >= sizeof(*input)) { + for (u32 map = be32_to_cpu(*input); map; map &= (map - 1)) { + u32 i = ffs(map); + + state->hash ^= state->key[0] << (32 - i) | + (u32)((u64)state->key[1] >> i); + } + + state->key++; + input++; + len -= sizeof(*input); + } +} + +static inline u8 virtio_net_hash_key_length(u32 types) +{ + size_t len = 0; + + if (types & VIRTIO_NET_HASH_REPORT_IPv4) + len = max(len, + sizeof(struct flow_dissector_key_ipv4_addrs)); + + if (types & + (VIRTIO_NET_HASH_REPORT_TCPv4 | VIRTIO_NET_HASH_REPORT_UDPv4)) + len = max(len, + sizeof(struct flow_dissector_key_ipv4_addrs) + + sizeof(struct flow_dissector_key_ports)); + + if (types & VIRTIO_NET_HASH_REPORT_IPv6) + len = max(len, + sizeof(struct flow_dissector_key_ipv6_addrs)); + + if (types & + (VIRTIO_NET_HASH_REPORT_TCPv6 | VIRTIO_NET_HASH_REPORT_UDPv6)) + len = max(len, + sizeof(struct flow_dissector_key_ipv6_addrs) + + sizeof(struct flow_dissector_key_ports)); + + return len + sizeof(u32); +} + +static inline u32 virtio_net_hash_report(u32 types, + const struct flow_keys_basic *keys) +{ + switch (keys->basic.n_proto) { + case cpu_to_be16(ETH_P_IP): + if (!(keys->control.flags & FLOW_DIS_IS_FRAGMENT)) { + if (keys->basic.ip_proto == IPPROTO_TCP && + (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) + return VIRTIO_NET_HASH_REPORT_TCPv4; + + if (keys->basic.ip_proto == IPPROTO_UDP && + (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) + return VIRTIO_NET_HASH_REPORT_UDPv4; + } + + if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) + return VIRTIO_NET_HASH_REPORT_IPv4; + + return VIRTIO_NET_HASH_REPORT_NONE; + + case cpu_to_be16(ETH_P_IPV6): + if (!(keys->control.flags & FLOW_DIS_IS_FRAGMENT)) { + if (keys->basic.ip_proto == IPPROTO_TCP && + (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv6)) + return VIRTIO_NET_HASH_REPORT_TCPv6; + + if (keys->basic.ip_proto == IPPROTO_UDP && + (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv6)) + return VIRTIO_NET_HASH_REPORT_UDPv6; + } + + if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv6) + return VIRTIO_NET_HASH_REPORT_IPv6; + + return VIRTIO_NET_HASH_REPORT_NONE; + + default: + return VIRTIO_NET_HASH_REPORT_NONE; + } +} + +static inline void virtio_net_hash_rss(const struct sk_buff *skb, + u32 types, const u32 *key, + struct virtio_net_hash *hash) +{ + struct virtio_net_toeplitz_state toeplitz_state = { .key = key }; + struct flow_keys flow; + struct flow_keys_basic flow_basic; + u16 report; + + if (!skb_flow_dissect_flow_keys(skb, &flow, 0)) { + hash->report = VIRTIO_NET_HASH_REPORT_NONE; + return; + } + + flow_basic = (struct flow_keys_basic) { + .control = flow.control, + .basic = flow.basic + }; + + report = virtio_net_hash_report(types, &flow_basic); + + switch (report) { + case VIRTIO_NET_HASH_REPORT_IPv4: + virtio_net_toeplitz_calc(&toeplitz_state, + (__be32 *)&flow.addrs.v4addrs, + sizeof(flow.addrs.v4addrs)); + break; + + case VIRTIO_NET_HASH_REPORT_TCPv4: + virtio_net_toeplitz_calc(&toeplitz_state, + (__be32 *)&flow.addrs.v4addrs, + sizeof(flow.addrs.v4addrs)); + virtio_net_toeplitz_calc(&toeplitz_state, &flow.ports.ports, + sizeof(flow.ports.ports)); + break; + + case VIRTIO_NET_HASH_REPORT_UDPv4: + virtio_net_toeplitz_calc(&toeplitz_state, + (__be32 *)&flow.addrs.v4addrs, + sizeof(flow.addrs.v4addrs)); + virtio_net_toeplitz_calc(&toeplitz_state, &flow.ports.ports, + sizeof(flow.ports.ports)); + break; + + case VIRTIO_NET_HASH_REPORT_IPv6: + virtio_net_toeplitz_calc(&toeplitz_state, + (__be32 *)&flow.addrs.v6addrs, + sizeof(flow.addrs.v6addrs)); + break; + + case VIRTIO_NET_HASH_REPORT_TCPv6: + virtio_net_toeplitz_calc(&toeplitz_state, + (__be32 *)&flow.addrs.v6addrs, + sizeof(flow.addrs.v6addrs)); + virtio_net_toeplitz_calc(&toeplitz_state, &flow.ports.ports, + sizeof(flow.ports.ports)); + break; + + case VIRTIO_NET_HASH_REPORT_UDPv6: + virtio_net_toeplitz_calc(&toeplitz_state, + (__be32 *)&flow.addrs.v6addrs, + sizeof(flow.addrs.v6addrs)); + virtio_net_toeplitz_calc(&toeplitz_state, &flow.ports.ports, + sizeof(flow.ports.ports)); + break; + + default: + hash->report = VIRTIO_NET_HASH_REPORT_NONE; + return; + } + + hash->value = toeplitz_state.hash; + hash->report = report; +} + static inline bool virtio_net_hdr_match_proto(__be16 protocol, __u8 gso_type) { switch (gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { From patchwork Thu Mar 13 07:01:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014453 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 ED4B41FFC6B for ; Thu, 13 Mar 2025 07:01:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849299; cv=none; b=pl97N6I71jT5rLo74IdEPJV3KFEIck7g/qQASitCEMWAxCvTZckWU8+2TslfyUTPRzWnEDd7B6Xl651DIGoTaCO/Lqf0vUb2bD5vADib2Z6vwnexfUlxPmbGWsbA3udNI5+9dNmEDaWOTo8dNi6Sw6L1Y/Nd+/cRnKx9tZ88KNo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849299; c=relaxed/simple; bh=tjt6UJr/WhYJJgKlXXv2tzURIehZ2/2wH4imLUM3bx4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=pvB6KiYMr8J0Og4Gxycrp/PmIrcAt6fRk6XFqer6Hj5t96JfyF+SagU4MkeDFcXgkn1ah9xB05H6+simfXCztyAcALt7r227hOBPWtGb57n9Cr/qdIB2UZO0UfW/+eqX2+ErBntWlNm8f6sV+CogglPCdLlk5JRLDJSMt65M/MQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=N9z5AYpR; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass 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="N9z5AYpR" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-223f4c06e9fso11048885ad.1 for ; Thu, 13 Mar 2025 00:01:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849297; x=1742454097; 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=DgNJjOB2VNdnBavlgHvSgZbzYgc2stuH8jHGRqRaRcM=; b=N9z5AYpRTofOvsMI8/LwRH3mSDceutebZIF40gGr1Wln4VrZqMJJxmSf1IN25vD9Bc XuHixSgts+t26afNcN0yLL502JAakibg5hlGjLEWp+kkGJs2ndzlZTii8AKKoyPiUkkQ 2LAuA2Z+Qc4+yK3341SYhq1awEmnVeFYiyYRUpGuwi6PTfySqYXNKNo3U64bz7G4rNIe 9QKbrsWC6KIWTLJIc7cU75WZBZtmuH1EwGg+jSTsbIGUfF38miFZq0n0VM1rObCdzozp VpTGXEiCKyyaxKxNrbxiBfY+hen+n2wsF+Ppal9ctqlT6l5E4cMAJj6gj3wjgJv42i9T nSrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849297; x=1742454097; 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=DgNJjOB2VNdnBavlgHvSgZbzYgc2stuH8jHGRqRaRcM=; b=V+FrUnmSxOTTgqLvHa2kEYdtdbf5mDnX7KU2e8n2rvxXJrTVwS4ZwMjeWEGK1RaCqS nX4yA8YGbd8MMCwNMZGdR/q6p9NBKdQ6PU5HhniO7aB///gBDFo75qXthghnJLOnNhTi /rTX4SoQohtxWjzgsvHyvAEQVAec3V/2zfapULmxisyIFMCtnpqZgDDWjotRKchbUewL psW12C+VMdG+0yQ4NG0tV1+250yHbul96F6ocBZwP7sFn43d0LJX1X9UylbegXp5qWlI K2IGOVA8u1PEZ2InEkQzWH528u5pKAJCa7Da6tDd8KPwKGGyv5XM0eShbw4/62Vlfkh6 oBKg== X-Forwarded-Encrypted: i=1; AJvYcCV9eYXRUGdh+DTX6Jv/HZiVhFv35HqTYANxLttEhXI08v99PgfHu4mzOSrFsACGaNfuzPA=@vger.kernel.org X-Gm-Message-State: AOJu0Yxge2aioVUbUX+GmmXMW/Hu7Xq6emBDue4VS+Gv7V8o2b2fDTWa XnfLb0/O2sB/xATSNzZn459yFryjKmmnDzGk27jl3TPIg3VkZPEMiJiWsGfDgi8= X-Gm-Gg: ASbGnctOp8YE3UYLqpEBlOOao7AvSXuftQ+YwpJS3ur4BXtnAO0MV0HAeRCd0rvYGwm vXxe+OyX7P36xQceU2uQsD58YVCr2gOWlov9hqzbQjBHnh30sW8w1cFj6wsljtTb2Iyh/ALXvf3 E/sHN5Q+0eeDMU+e3VSsXC/KQKdHTRXsF4EULdTYQ8GzjWW6qbvHoP22lt8YdaG2jyfMiYnOeXk xUH3d6vY/CzkOvNCm+od5rKhQ0bkZyokM7/Dbr+NS4JHCzdv+2/sibKDUca3hzCyFOfFDYPislV 7/jeSyG8EN0w6sbboHdVKHSG9z+i0mySEBJND8Tv3qu/SGJrQ53oX/QNvX0= X-Google-Smtp-Source: AGHT+IF5wWHOyH5JHVoHtTy7K1X2eW3fNA4SOZ5Z2GJbYtGXN3r5z7f/KOO/C3Z0RJnF3qBJwnVUzw== X-Received: by 2002:a17:902:f548:b0:215:a303:24e9 with SMTP id d9443c01a7336-225c651d65emr18870555ad.3.1741849297043; Thu, 13 Mar 2025 00:01:37 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-225c6bbeb7esm6519955ad.193.2025.03.13.00.01.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:01:36 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:05 +0900 Subject: [PATCH net-next v10 02/10] net: flow_dissector: Export flow_keys_dissector_symmetric Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-2-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 flow_keys_dissector_symmetric is useful to derive a symmetric hash and to know its source such as IPv4, IPv6, TCP, and UDP. Signed-off-by: Akihiko Odaki Tested-by: Lei Yang --- include/net/flow_dissector.h | 1 + net/core/flow_dissector.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/net/flow_dissector.h b/include/net/flow_dissector.h index ced79dc8e856..d01c1ec77b7d 100644 --- a/include/net/flow_dissector.h +++ b/include/net/flow_dissector.h @@ -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: diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c index 9cd8de6bebb5..32c7ee31330c 100644 --- a/net/core/flow_dissector.c +++ b/net/core/flow_dissector.c @@ -1862,7 +1862,8 @@ void make_flow_keys_digest(struct flow_keys_digest *digest, } EXPORT_SYMBOL(make_flow_keys_digest); -static struct flow_dissector flow_keys_dissector_symmetric __read_mostly; +struct flow_dissector flow_keys_dissector_symmetric __read_mostly; +EXPORT_SYMBOL(flow_keys_dissector_symmetric); u32 __skb_get_hash_symmetric_net(const struct net *net, const struct sk_buff *skb) { From patchwork Thu Mar 13 07:01:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014454 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 728CB200B99 for ; Thu, 13 Mar 2025 07:01:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849306; cv=none; b=bM99u5r4DxGVE4bt9PMTtvYalI6uKGwb4ulGAgfGhRYZLSYXu1ZzyiM6LWONPtqr35vdiDIyunsZqtBYRZWW6n8ERkpjlOZatj0BkBaH8LrdRySiOvn88hdLUqe2bJygeWR5GOLUe7l4XeA+OluaY4srX0u7bYOYkGM5cFSrTzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849306; c=relaxed/simple; bh=uj6BiB0IqVm8+xs3f9Zn6aCjK6f7zlPp0hv2djTMvTQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=CFvb9TwyRgy8p5c0GJjwu4DIlUqoBqSu+yz5SrX9Mp95qtGWfxknONEJGz7rFGFGj0ft039piVFt6V2wwUGtrUuW1Ou+d/nxbJWeo2dFfg2LlgrgJlAU1Gy4WcL/+Wjxoy9GhZUYwUJe4puOqfKzNp/UHXdjlOfccCKHcct7/OI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=ECdzwPAG; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass 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="ECdzwPAG" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2235189adaeso12411185ad.0 for ; Thu, 13 Mar 2025 00:01:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849303; x=1742454103; 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=CFouu/EN4lEKQm+2Nyv7q163Sd7hDT7Qc+wcMAoVrxU=; b=ECdzwPAGAoCSWTXNIPQAxfcp5P5SO/fvBwY8wBAZUecHrDq/oB25D5UMWszSldfTLP p5QDz7GxzNr20zvrqcBBLrPOhOkButkAn7HNPGKGn72o66/xWBn69cIc8OwYKzii2YHF UTHRdX1A7gEmF0OytmdVJrL9JF5JwU9BzZgP2kXboWM7r1e4HjB68MGU41+fgxzmdpBd qSs08DI12hK+QOphesK44Ut/fK55/lQt0/u3Lyr2X3iiy60Wb7Oh5Qehrr+3n0mE/EtG +zdg17kGP3uJaj9moQmRyyLwDMfU7aniXslS0xnqhH4oLBRi7FOKo4xuA1+QLLWqO7uf X1Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849303; x=1742454103; 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=CFouu/EN4lEKQm+2Nyv7q163Sd7hDT7Qc+wcMAoVrxU=; b=dX3t2qXJQFeMwAlyAeN1Xh/vRdPCI+x6Y6pUXI8dTfJcWq6UU9ChiCMEpERXBoTK0P Rt44MDbBOnzTqB0DBGvR41W2TwQWJeq0+xUXjBGIk31zrSO8hbhfD5zxv2ExDUtoNQqs OHcFJJAWn6T+7NGkLlLOGApVqq3+xOw6VVUL8XoAw8HF42JADzXfTyTaA/lnAvnwagBJ lnVdPdAsb6Rac8KlNB7rgbFF2xr+Wa8rBogCaJn9eotRc8EhWs6nMa0sFzIJ4dERYzTR uTVRykm5axUhEJlVo+9RI8sYuqZUMUI2UpuFS9DrHGXdy6V4dmRbLTic9kzD/XB3gHEi 4Fng== X-Forwarded-Encrypted: i=1; AJvYcCXxLRhaqUTAv7UBTye0gcpt/kT9zyl1EPScy3baubMfCuv4wTfCmbQ60hBhOWz/mtBX+Bg=@vger.kernel.org X-Gm-Message-State: AOJu0Yy0L8jWPB4zpTVrWfDrgs0f951nt/fCnNaELrH1Tp1YKXzce5PH gB5qyFVHaJNQjGD3L4pTv1MZUIkHyZIP835hmjPKCt84SufBrdqoqOkjPYvDZyo= X-Gm-Gg: ASbGncvQLvqbRGpLxtKBk2QhFp7UjNeALFkUHsn3dWAE9TvysTYFDNkDVM7Z9QERW/l bhpo+qV4KRFBM0nUeEOHTdbq926sTTVJC3WHbK0VWLNCI0SjMIGfC17cuqvnUwp7fxoDEvotvse G3ds2bc5Dw9k4z21/cVadPkGTq2Fs/4VhnJJi4wbEEs6KkuzGQwAFj4ZZVUGuE1kAcYR7Yyev65 YboKRdmvWrAuBMCO75UJ+9aq+Y1Tvc54bLAZ5stMUIgeqeoRQ2x4E8RR2FzTow1sAH6/iKSf+3K zdBPMr9Qva0iFwbi7HHLgKmThGaVa2s4Dm000E5AYbU5Xa5Y X-Google-Smtp-Source: AGHT+IF41bM6O52nxyqfLS5xcbnmD0BRXYmKkq1aTZiZh+PP5DLgrJ3Gf/DAzNS+0zNbGzXZ+/OHfQ== X-Received: by 2002:a17:903:189:b0:221:8568:c00f with SMTP id d9443c01a7336-225c64097abmr19829395ad.0.1741849302826; Thu, 13 Mar 2025 00:01:42 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-225c6bbe84esm6495685ad.176.2025.03.13.00.01.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:01:42 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:06 +0900 Subject: [PATCH net-next v10 03/10] tun: Allow steering eBPF program to fall back Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-3-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 This clarifies a steering eBPF program takes precedence over the other steering algorithms. Signed-off-by: Akihiko Odaki --- Documentation/networking/tuntap.rst | 7 +++++++ drivers/net/tun.c | 28 +++++++++++++++++----------- include/uapi/linux/if_tun.h | 9 +++++++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Documentation/networking/tuntap.rst b/Documentation/networking/tuntap.rst index 4d7087f727be..86b4ae8caa8a 100644 --- a/Documentation/networking/tuntap.rst +++ b/Documentation/networking/tuntap.rst @@ -206,6 +206,13 @@ enable is true we enable it, otherwise we disable it:: return ioctl(fd, TUNSETQUEUE, (void *)&ifr); } +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 ========================================================= diff --git a/drivers/net/tun.c b/drivers/net/tun.c index d8f4d3e996a7..9133ab9ed3f5 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -476,21 +476,29 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) return txq; } -static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb) +static bool tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb, + u16 *ret) { struct tun_prog *prog; u32 numqueues; - u16 ret = 0; + u32 prog_ret; + + prog = rcu_dereference(tun->steering_prog); + if (!prog) + return false; numqueues = READ_ONCE(tun->numqueues); - if (!numqueues) - return 0; + if (!numqueues) { + *ret = 0; + return true; + } - prog = rcu_dereference(tun->steering_prog); - if (prog) - ret = bpf_prog_run_clear_cb(prog->prog, skb); + prog_ret = bpf_prog_run_clear_cb(prog->prog, skb); + if (prog_ret == TUN_STEERINGEBPF_FALLBACK) + return false; - return ret % numqueues; + *ret = (u16)prog_ret % numqueues; + return true; } static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, @@ -500,9 +508,7 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, u16 ret; rcu_read_lock(); - if (rcu_dereference(tun->steering_prog)) - ret = tun_ebpf_select_queue(tun, skb); - else + if (!tun_ebpf_select_queue(tun, skb, &ret)) ret = tun_automq_select_queue(tun, skb); rcu_read_unlock(); diff --git a/include/uapi/linux/if_tun.h b/include/uapi/linux/if_tun.h index 287cdc81c939..980de74724fc 100644 --- a/include/uapi/linux/if_tun.h +++ b/include/uapi/linux/if_tun.h @@ -115,4 +115,13 @@ struct tun_filter { __u8 addr[][ETH_ALEN]; }; +/** + * define TUN_STEERINGEBPF_FALLBACK - A steering eBPF return value to fall back + * + * A steering eBPF program may return this value to fall back to the steering + * algorithm that should have been used if the program was not set. This allows + * selectively overriding the steering decision. + */ +#define TUN_STEERINGEBPF_FALLBACK -1 + #endif /* _UAPI__IF_TUN_H */ From patchwork Thu Mar 13 07:01:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014455 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 55CBF20102C for ; Thu, 13 Mar 2025 07:01:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849312; cv=none; b=kY4aKTVXhoifCie040Q6W2O0036AQtm1FEbUHgZNMUJpWNHKvvU7cqdhx5KIrJjwusvbGFnUjBu+R0IzXjkL3PqqvyOrKlVXwqXtA0bcpybRN2cYIvZ2ekjDIG4c+MYiTuk/f3BcpHfT5SKfA7RfQkJ34qlQUGtrm9T77+KJfm4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849312; c=relaxed/simple; bh=nFlZruyUGC1SefIAzclzdj2tkbf6BBZOp0YpRTkvlMg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=hS0NntM4DB0w1fH/qPOsBS0e8ddILDfYA5HcVBdQzlC93NdemvoGLZyrEHvDKt5VVASXWjP8wqs7ic2wj/tqjJYWVLMihifX+6kbSnO4qZK6Y4nezaDHoGJn0bd/sajmRYGpG8M8MCXW1SU45kwBW8/g99uM6QHPriv4S21qeAE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=m2TYYB4j; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass 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="m2TYYB4j" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-22403cbb47fso11881245ad.0 for ; Thu, 13 Mar 2025 00:01:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849309; x=1742454109; 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=DF+oAfxAaVuIF8loqeGyU6Uzr2digKuwKsJalo4eV8o=; b=m2TYYB4j60667IoEIdzUzKzS9qpNTO2zIPh4CTF+e9/aJK5o28a/7bz1F2aGH45CZV 8ZDE9il28MXyO2aTb8XbePu+RnRwilihTvZpdUXvYPVewwBi7lWzeUJUmBEQdRV1CnM+ l9balU9PHK3Ed0eXJXCY6NQkB0/2fAPyIRsaSfVvGt18fhDvheN1fqYGZVLjrYsxGNXr Zha7PIVdi02H0zQFJeYndNN09NQ/WAg79qxuDAm8/EP9EtJb5yEvgk72YB/oHgfklafc ZlWM4UsL0C5dTzb27zqZbz3ASChI/QdDZw5x9gPHAZd0gZdJG6hp+YDtK5f0fWzel04Y SE+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849309; x=1742454109; 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=DF+oAfxAaVuIF8loqeGyU6Uzr2digKuwKsJalo4eV8o=; b=nOSgCsiFTPzYaBTlc0nYN1bJxk3SLN/gHj5t76HujT7J/+F0n/jGHnT5EodpKkg++B XknHlr3pespoC5OeQYSbeDKmLHf4TkQNBcgzOJ8UNEUnCye7ggdOTynZglOBHAEBcdw2 DCo7VAzAabr6IWJ79T2owHD+MkBkxRzvM8lhjbiq5XkrfEMBvCXyk5ZWkKBAQGl8AveN 0IrTClVIIP2lbf4VXbigXnVjHaSaR8r4mCukyCqW0X4cEjPviGXcNWko3TUexbQJfVj9 JfvG9JHVhMIRTwe5wMqhnxR49Tpt/2iaJI2jvgqkbUAJKmK0BbIYNcHi7QW71jh9rMRP oyjg== X-Forwarded-Encrypted: i=1; AJvYcCVpI+UUbJaJOFf0jMLQI3WIiVCKvyWEpZ8DAnlYT6rQzQA8qNxQdqSa8VudjGamZmOfx9Y=@vger.kernel.org X-Gm-Message-State: AOJu0YzMBNe+vICo/E/cAuSceJE9x0f2z1CENs3UcEfrCrW2tWYGmmuZ 6LyAlu6oTZBntYl6JYMsZwQcbeo+aktXgUod4Od9Qzg9ip7Nyp9jdFVlEQUa7+o= X-Gm-Gg: ASbGncvkZUMtO5OaKeRxY6Zjsa3tm+J3psaAcsDZh2r5p1msLP4fbfQynD+c1XAcwGr F2ermwieh8eaKo07+a/3Mm0cEPkd1ny085XabcWLv0m3Hkkf7N61CHAyFCzXBlspXB/rMlmEpjV fZZDvX+XXG2+adzsb9zTkw+DQDK+QAFHA8z7z1FBvcvE+ja3+h7vBzFjAZRLGpjkjGvpyxNXo/Y eJeZcCufcEcHJjh96a4AN95F1cEb9b+7X8lftpSm1Yg7QnaXXSUm/6mysb/I71/gfFbBt2TU2U8 QopFi1sEgfb6yJ7KKX/+RLBHdPU+upVgRLzV2S3qYl22AKS+ X-Google-Smtp-Source: AGHT+IH5QsZweiomgoa/OhpQwcjK3b5H1Gh/Rd8XP29KHLtEBaqYadv3hlLrbFNVf4iCPy/aNayE1Q== X-Received: by 2002:a17:902:d509:b0:224:1e7a:43fe with SMTP id d9443c01a7336-22428bf168amr399052815ad.46.1741849309429; Thu, 13 Mar 2025 00:01:49 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-225c6ba6efesm6479755ad.137.2025.03.13.00.01.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:01:49 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:07 +0900 Subject: [PATCH net-next v10 04/10] tun: Add common virtio-net hash feature code Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-4-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Add common code required for the features being added to TUN and TAP. They will be enabled for each of them in following patches. Added Features ============== Hash reporting -------------- Allow the guest to reuse the hash value to make receive steering consistent between the host and guest, and to save hash computation. Receive Side Scaling (RSS) -------------------------- RSS is a receive steering algorithm that can be negotiated to use with virtio_net. Conventionally the hash calculation was done by the VMM. However, computing the hash after the queue was chosen defeats the purpose of RSS. Another approach is to use eBPF steering program. This approach has another downside: it cannot report the calculated hash due to the restrictive nature of eBPF steering program. Introduce the code to perform RSS to the kernel in order to overcome thse challenges. An alternative solution is to extend the eBPF steering program so that it will be able to report to the userspace, but I didn't opt for it because extending the current mechanism of eBPF steering program as is because it relies on legacy context rewriting, and introducing kfunc-based eBPF will result in non-UAPI dependency while the other relevant virtualization APIs such as KVM and vhost_net are UAPIs. Added ioctls ============ They are designed to make extensibility and VM migration compatible. This change only adds the implementation and does not expose them to the userspace. TUNGETVNETHASHCAP ----------------- This ioctl tells supported features and hash types. It is useful to check if a VM can be migrated to the current host. TUNSETVNETHASH -------------- This ioctl allows setting features and hash types to be enabled. It limits the features exposed to the guest to ensure proper migration. It also sets RSS parameters, depending on the enabled features and hash types. Signed-off-by: Akihiko Odaki Tested-by: Lei Yang --- drivers/net/tap.c | 10 ++- drivers/net/tun.c | 12 +++- drivers/net/tun_vnet.h | 155 +++++++++++++++++++++++++++++++++++++++++--- include/uapi/linux/if_tun.h | 73 +++++++++++++++++++++ 4 files changed, 236 insertions(+), 14 deletions(-) diff --git a/drivers/net/tap.c b/drivers/net/tap.c index d4ece538f1b2..25c60ff2d3f2 100644 --- a/drivers/net/tap.c +++ b/drivers/net/tap.c @@ -179,6 +179,11 @@ static void tap_put_queue(struct tap_queue *q) sock_put(&q->sk); } +static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb) +{ + return NULL; +} + /* * Select a queue based on the rxq of the device on which this packet * arrived. If the incoming device is not mq, calculate a flow hash @@ -711,11 +716,12 @@ static ssize_t tap_put_user(struct tap_queue *q, int total; if (q->flags & IFF_VNET_HDR) { - struct virtio_net_hdr vnet_hdr; + struct virtio_net_hdr_v1_hash vnet_hdr; vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz); - ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr); + ret = tun_vnet_hdr_from_skb(vnet_hdr_len, q->flags, NULL, skb, + tap_find_hash, &vnet_hdr); if (ret) return ret; diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 9133ab9ed3f5..03d47799e9bd 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -451,6 +451,11 @@ static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) e->rps_rxhash = hash; } +static const struct virtio_net_hash *tun_find_hash(const struct sk_buff *skb) +{ + return NULL; +} + /* We try to identify a flow through its rxhash. The reason that * we do not check rxq no. is because some cards(e.g 82599), chooses * the rxq based on the txq where the last packet of the flow comes. As @@ -1993,7 +1998,7 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun, ssize_t ret; if (tun->flags & IFF_VNET_HDR) { - struct virtio_net_hdr gso = { 0 }; + struct virtio_net_hdr_v1_hash gso = { 0 }; vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso); @@ -2046,9 +2051,10 @@ static ssize_t tun_put_user(struct tun_struct *tun, } if (vnet_hdr_sz) { - struct virtio_net_hdr gso; + struct virtio_net_hdr_v1_hash gso; - ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso); + ret = tun_vnet_hdr_from_skb(vnet_hdr_sz, tun->flags, tun->dev, + skb, tun_find_hash, &gso); if (ret) return ret; diff --git a/drivers/net/tun_vnet.h b/drivers/net/tun_vnet.h index 58b9ac7a5fc4..578adaac0671 100644 --- a/drivers/net/tun_vnet.h +++ b/drivers/net/tun_vnet.h @@ -6,6 +6,16 @@ #define TUN_VNET_LE 0x80000000 #define TUN_VNET_BE 0x40000000 +typedef struct virtio_net_hash *(*tun_vnet_hash_add)(struct sk_buff *); +typedef const struct virtio_net_hash *(*tun_vnet_hash_find)(const struct sk_buff *); + +struct tun_vnet_hash_container { + struct tun_vnet_hash common; + struct tun_vnet_hash_rss rss; + u32 rss_key[VIRTIO_NET_RSS_MAX_KEY_SIZE]; + u16 rss_indirection_table[]; +}; + static inline bool tun_vnet_legacy_is_little_endian(unsigned int flags) { bool be = IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) && @@ -107,6 +117,119 @@ static inline long tun_vnet_ioctl(int *vnet_hdr_sz, unsigned int *flags, } } +static inline long tun_vnet_ioctl_gethashcap(void __user *argp) +{ + static const struct tun_vnet_hash cap = { + .flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS, + .types = VIRTIO_NET_SUPPORTED_HASH_TYPES + }; + + return copy_to_user(argp, &cap, sizeof(cap)) ? -EFAULT : 0; +} + +static inline long tun_vnet_ioctl_sethash(struct tun_vnet_hash_container __rcu **hashp, + void __user *argp) +{ + struct tun_vnet_hash hash_buf; + struct tun_vnet_hash_container *hash; + + if (copy_from_user(&hash_buf, argp, sizeof(hash_buf))) + return -EFAULT; + argp = (struct tun_vnet_hash __user *)argp + 1; + + if (hash_buf.flags & TUN_VNET_HASH_RSS) { + struct tun_vnet_hash_rss rss; + size_t indirection_table_size; + size_t key_size; + size_t size; + + if (copy_from_user(&rss, argp, sizeof(rss))) + return -EFAULT; + argp = (struct tun_vnet_hash_rss __user *)argp + 1; + + indirection_table_size = ((size_t)rss.indirection_table_mask + 1) * 2; + key_size = virtio_net_hash_key_length(hash_buf.types); + size = struct_size(hash, rss_indirection_table, + (size_t)rss.indirection_table_mask + 1); + + hash = kmalloc(size, GFP_KERNEL); + if (!hash) + return -ENOMEM; + + if (copy_from_user(hash->rss_indirection_table, + argp, indirection_table_size)) { + kfree(hash); + return -EFAULT; + } + argp = (u16 __user *)argp + rss.indirection_table_mask + 1; + + if (copy_from_user(hash->rss_key, argp, key_size)) { + kfree(hash); + return -EFAULT; + } + + virtio_net_toeplitz_convert_key(hash->rss_key, key_size); + hash->rss = rss; + } else { + hash = kmalloc(sizeof(hash->common), GFP_KERNEL); + if (!hash) + return -ENOMEM; + } + + hash->common = hash_buf; + kfree_rcu_mightsleep(rcu_replace_pointer_rtnl(*hashp, hash)); + return 0; +} + +static 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, + tun_vnet_hash_add vnet_hash_add) +{ + struct virtio_net_hash *report; + + if (!hash || !(hash->common.flags & TUN_VNET_HASH_REPORT)) + return; + + report = vnet_hash_add(skb); + if (!report) + return; + + *report = (struct virtio_net_hash) { + .report = virtio_net_hash_report(hash->common.types, keys), + .value = value + }; +} + +static 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 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; + + return hash->rss_indirection_table[index] % numqueues; +} + static inline int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from, struct virtio_net_hdr *hdr) @@ -135,15 +258,17 @@ static inline int tun_vnet_hdr_get(int sz, unsigned int flags, } static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter, - const struct virtio_net_hdr *hdr) + const struct virtio_net_hdr_v1_hash *hdr) { + int content_sz = MIN(sizeof(*hdr), sz); + if (unlikely(iov_iter_count(iter) < sz)) return -EINVAL; - if (unlikely(copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))) + if (unlikely(copy_to_iter(hdr, content_sz, iter) != content_sz)) return -EFAULT; - if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr)) + if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz) return -EFAULT; return 0; @@ -155,26 +280,38 @@ static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb, return virtio_net_hdr_to_skb(skb, hdr, tun_vnet_is_little_endian(flags)); } -static inline int tun_vnet_hdr_from_skb(unsigned int flags, +static inline int tun_vnet_hdr_from_skb(int sz, unsigned int flags, const struct net_device *dev, const struct sk_buff *skb, - struct virtio_net_hdr *hdr) + tun_vnet_hash_find vnet_hash_find, + struct virtio_net_hdr_v1_hash *hdr) { int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0; + const struct virtio_net_hash *report = sz < sizeof(struct virtio_net_hdr_v1_hash) ? + NULL : vnet_hash_find(skb); + + *hdr = (struct virtio_net_hdr_v1_hash) { + .hash_report = VIRTIO_NET_HASH_REPORT_NONE + }; + + if (report) { + hdr->hash_value = cpu_to_le32(report->value); + hdr->hash_report = cpu_to_le16(report->report); + } - if (virtio_net_hdr_from_skb(skb, hdr, + if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr, tun_vnet_is_little_endian(flags), true, vlan_hlen)) { struct skb_shared_info *sinfo = skb_shinfo(skb); if (net_ratelimit()) { netdev_err(dev, "unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n", - sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->gso_size), - tun_vnet16_to_cpu(flags, hdr->hdr_len)); + sinfo->gso_type, tun_vnet16_to_cpu(flags, hdr->hdr.gso_size), + tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len)); print_hex_dump(KERN_ERR, "tun: ", DUMP_PREFIX_NONE, 16, 1, skb->head, - min(tun_vnet16_to_cpu(flags, hdr->hdr_len), 64), true); + min(tun_vnet16_to_cpu(flags, hdr->hdr.hdr_len), 64), true); } WARN_ON_ONCE(1); return -EINVAL; diff --git a/include/uapi/linux/if_tun.h b/include/uapi/linux/if_tun.h index 980de74724fc..b7b0fe69a652 100644 --- a/include/uapi/linux/if_tun.h +++ b/include/uapi/linux/if_tun.h @@ -62,6 +62,42 @@ #define TUNSETCARRIER _IOW('T', 226, int) #define TUNGETDEVNETNS _IO('T', 227) +/** + * define TUNGETVNETHASHCAP - ioctl to get virtio_net hashing capability + * + * The argument is a pointer to &struct tun_vnet_hash which will store the + * maximal virtio_net hashing configuration. + */ +#define TUNGETVNETHASHCAP _IOR('T', 228, struct tun_vnet_hash) + +/** + * define TUNSETVNETHASH - ioctl to configure virtio_net hashing + * + * The argument is a pointer to &struct tun_vnet_hash. + * + * The argument is a pointer to the compound of the following in order if + * %TUN_VNET_HASH_RSS is set: + * + * 1. &struct tun_vnet_hash + * 2. &struct tun_vnet_hash_rss + * 3. Indirection table + * 4. Key + * + * The %TUN_VNET_HASH_REPORT flag set with this ioctl will be effective only + * after calling the %TUNSETVNETHDRSZ ioctl with a number greater than or equal + * to the size of &struct virtio_net_hdr_v1_hash. + * + * The members added to the legacy header by %TUN_VNET_HASH_REPORT flag will + * always be little-endian. + * + * This ioctl results in %EBADFD if the underlying device is deleted. It affects + * all queues attached to the same device. + * + * This ioctl currently has no effect on XDP packets and packets with + * queue_mapping set by TC. + */ +#define TUNSETVNETHASH _IOW('T', 229, struct tun_vnet_hash) + /* TUNSETIFF ifr flags */ #define IFF_TUN 0x0001 #define IFF_TAP 0x0002 @@ -124,4 +160,41 @@ struct tun_filter { */ #define TUN_STEERINGEBPF_FALLBACK -1 +/** + * define TUN_VNET_HASH_REPORT - Request virtio_net hash reporting for vhost + */ +#define TUN_VNET_HASH_REPORT 0x0001 + +/** + * define TUN_VNET_HASH_RSS - Request virtio_net RSS + */ +#define TUN_VNET_HASH_RSS 0x0002 + +/** + * struct tun_vnet_hash - virtio_net hashing configuration + * @flags: + * Bitmask consists of %TUN_VNET_HASH_REPORT and %TUN_VNET_HASH_RSS + * @pad: + * Should be filled with zero before passing to %TUNSETVNETHASH + * @types: + * Bitmask of allowed hash types + */ +struct tun_vnet_hash { + __u16 flags; + __u8 pad[2]; + __u32 types; +}; + +/** + * struct tun_vnet_hash_rss - virtio_net RSS configuration + * @indirection_table_mask: + * Bitmask to be applied to the indirection table index + * @unclassified_queue: + * The index of the queue to place unclassified packets in + */ +struct tun_vnet_hash_rss { + __u16 indirection_table_mask; + __u16 unclassified_queue; +}; + #endif /* _UAPI__IF_TUN_H */ From patchwork Thu Mar 13 07:01:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014456 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 30F7C20127A for ; Thu, 13 Mar 2025 07:01:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849317; cv=none; b=ZUIYPCX6rcwppskFsCgfUeMr9arYKZmQuJHBuA5nAJ7JUPOBPzc4fgC1iyoYoHLMsiRayXNpH8f6BdHc9JShbrHUTzB+0jBkVDDVz4mg5keRV03AofnPHI/AXk+qtMxnAI0ErQUISxv6Klcc1Nz+ThOOoxhJMAn3Y0NtzUwy3JY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849317; c=relaxed/simple; bh=fAsNygmBH4vEFfwnr1O256Ehqy/tj24JUzQEngxqzTs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=seGj9+pmPB6Gk0jt9Jp9jPFT5EJrtzXRyxQsALufFnsOVs9JWwrGBpfBTFPlRF50eqG7H+AKQPH1u2Esd6+R2LmUVCJ3esNiFS1kzxU3U5eHBPfeQEPtpPNv2yRN2pk3HluTOuxzlzEGUgvQawmwHmnnhpNAIYO+3fB12qsvM2Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=dqDAFYC3; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass 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="dqDAFYC3" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-22435603572so10765255ad.1 for ; Thu, 13 Mar 2025 00:01:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849315; x=1742454115; 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=iEx1oB+WR7O6v2TIINfyTS8xxjNqfeRuV0OaAnQT/3s=; b=dqDAFYC3FI27Lq8swyujI9KWapP3mNM9Cr3I7YN7BsWKhW48xN+3OivsqDxl4heFio Jjhypw1gSs9gti7hTe1932GkVEpc4/u+FHtNpDevJpMm259CCoWH3TIxNp1JGN3yleTe MxVHlJP2kaKv3u6cxuoX4kghqDXzW7kXwuZgdAV/g8At4+PR/gsnE84t+fYX1iftslBt JkVbW+NUHDnB9r5tr2rGMC9n01cHBue3rHtJJJsLloMyy0X9kCK+XL65fPa4I9KRt8Kf j6cgMwVS8EvK+6rzO6WyICX3BDgXJSZvNLKJgQJ39AMWF4MvY9bTl2Vw6IYl3TMDHoa7 ompQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849315; x=1742454115; 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=iEx1oB+WR7O6v2TIINfyTS8xxjNqfeRuV0OaAnQT/3s=; b=PirKLn7h84DVFVVdv9gItPqWCkh71qNHJw7IvqVLQTOBssvm2xtDCoFUVg+m7oaw3T oSbtqj2QlFoNhxRsasJ0s/l/ert1x4gVD21ysexACoBFF9PCNaMoJoHatB5Iw6kNjkYF O0V4MgkriRWcX16Mx+2vhnxLz6hnoaQn8pJpNOPHFkS4cR2jQtoklK88Z36EmzlYF8W2 lNzG3gRNvCVzuJA77NY5gB0aNUcGS+I80DBdW3o3/WqBQOyjsiEuueOtumgBXFdBG7qL cQQ54sBKlLAS9iOctl5M6SqyPLR/AhDOtuRa/BITvxFGO6gVCu92DBM1BcBS3tf8Wwmk oPIA== X-Forwarded-Encrypted: i=1; AJvYcCX5qSgpq+vdQKU2n5jmLObfvu+ZLPgLkesM6eAE6vzCdW3CTTBTT92HX9GR2gBq0/Wu+fc=@vger.kernel.org X-Gm-Message-State: AOJu0YzMqUuK4E/C8zFFhKCkEUZN4QxHMSGP0g8SRyTT0AzHs3E+EAPQ qk1OKjCE1vTem7wVQYhyUyHXqijsSxCrkd8Gd5AdKnhVwpBdiM37TzIBxRynniQ= X-Gm-Gg: ASbGnctirYURFl/XLpmEIihkIyfD4FxtPhqfxxWmNMZtsesZ3qTaA3ZQf0R/VNsf8rH 6Y3qxtAngFS5VZFBoCKnqnTU2aRtLQqyMi6N3F7otJ6YWlkE5TvDDONKXEP5WHeFK4g4E8ixpjJ SgXVqZNxaMHoRj1gqSh0PW/ipSS7LjLTmJHNN+FiTA52UoSsohtnNn/bHn1GSFaozWVeQ9BUcZj CW+FiqX0ed2wBYk+mEViLV7TvkTm7iifnobBHLu8iw1YsN/Fl7LMZvwmJnJlQZeaXYe/0Eo7I40 ZwxXm9et5VpMoZ+op3d3JlbN+b7n9LjmZWF8Q6HKaIE+BRaEGzj4DOLHryI= X-Google-Smtp-Source: AGHT+IFxYhCxmO0r82s2kBmNT4izOERTLgrLPNuxMZ4nqqb1ELCFYiVGwW2lZs8BLOwjEz2BJgVjGw== X-Received: by 2002:a17:902:d50e:b0:223:4d7e:e523 with SMTP id d9443c01a7336-22428c24321mr304711965ad.50.1741849315481; Thu, 13 Mar 2025 00:01:55 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-225c6ba6c8dsm6485835ad.156.2025.03.13.00.01.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:01:55 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:08 +0900 Subject: [PATCH net-next v10 05/10] tun: Introduce virtio-net hash feature Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-5-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Add ioctls and storage required for the virtio-net hash feature to TUN. Signed-off-by: Akihiko Odaki --- drivers/net/Kconfig | 1 + drivers/net/tun.c | 54 ++++++++++++++++++++++++++++++++++++++++++-------- include/linux/skbuff.h | 3 +++ net/core/skbuff.c | 4 ++++ 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index 1fd5acdc73c6..aecfd244dd83 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -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 diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 03d47799e9bd..b2d74e0ec932 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -209,6 +209,7 @@ struct tun_struct { struct bpf_prog __rcu *xdp_prog; struct tun_prog __rcu *steering_prog; struct tun_prog __rcu *filter_prog; + struct tun_vnet_hash_container __rcu *vnet_hash; struct ethtool_link_ksettings link_ksettings; /* init args */ struct file *file; @@ -451,9 +452,14 @@ 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 NULL; + return skb_ext_find(skb, SKB_EXT_TUN_VNET_HASH); } /* We try to identify a flow through its rxhash. The reason that @@ -462,14 +468,21 @@ 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; 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); @@ -478,6 +491,13 @@ static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb) txq = reciprocal_scale(txq, numqueues); } + keys_basic = (struct flow_keys_basic) { + .control = keys.control, + .basic = keys.basic + }; + tun_vnet_hash_report(vnet_hash, skb, &keys_basic, skb->l4_hash ? skb->hash : txq, + tun_add_hash); + return txq; } @@ -513,8 +533,15 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, u16 ret; rcu_read_lock(); - if (!tun_ebpf_select_queue(tun, skb, &ret)) - ret = tun_automq_select_queue(tun, skb); + if (!tun_ebpf_select_queue(tun, skb, &ret)) { + 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; @@ -2235,6 +2262,7 @@ static void tun_free_netdev(struct net_device *dev) security_tun_dev_free_security(tun->security); __tun_set_ebpf(tun, &tun->steering_prog, NULL); __tun_set_ebpf(tun, &tun->filter_prog, NULL); + kfree_rcu_mightsleep(rcu_access_pointer(tun->vnet_hash)); } static void tun_setup(struct net_device *dev) @@ -3014,16 +3042,22 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, } else { memset(&ifr, 0, sizeof(ifr)); } - if (cmd == TUNGETFEATURES) { + switch (cmd) { + case TUNGETFEATURES: /* Currently this just means: "what IFF flags are valid?". * This is needed because we never checked for invalid flags on * TUNSETIFF. */ return put_user(IFF_TUN | IFF_TAP | IFF_NO_CARRIER | TUN_FEATURES, (unsigned int __user*)argp); - } else if (cmd == TUNSETQUEUE) { + + case TUNSETQUEUE: return tun_set_queue(file, &ifr); - } else if (cmd == SIOCGSKNS) { + + case TUNGETVNETHASHCAP: + return tun_vnet_ioctl_gethashcap(argp); + + case SIOCGSKNS: if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) return -EPERM; return open_related_ns(&net->ns, get_net_ns); @@ -3264,6 +3298,10 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, ret = open_related_ns(&net->ns, get_net_ns); break; + case TUNSETVNETHASH: + ret = tun_vnet_ioctl_sethash(&tun->vnet_hash, argp); + break; + default: ret = tun_vnet_ioctl(&tun->vnet_hdr_sz, &tun->flags, cmd, argp); break; diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index bb2b751d274a..cdd793f1c360 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -4842,6 +4842,9 @@ enum skb_ext_id { #endif #if IS_ENABLED(CONFIG_MCTP_FLOWS) SKB_EXT_MCTP, +#endif +#if IS_ENABLED(CONFIG_TUN) + SKB_EXT_TUN_VNET_HASH, #endif SKB_EXT_NUM, /* must be last */ }; diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 7b03b64fdcb2..aa2a091b649f 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -64,6 +64,7 @@ #include #include #include +#include #include #include @@ -4969,6 +4970,9 @@ static const u8 skb_ext_type_len[] = { #if IS_ENABLED(CONFIG_MCTP_FLOWS) [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), #endif +#if IS_ENABLED(CONFIG_TUN) + [SKB_EXT_TUN_VNET_HASH] = SKB_EXT_CHUNKSIZEOF(struct virtio_net_hash), +#endif }; static __always_inline unsigned int skb_ext_total_length(void) From patchwork Thu Mar 13 07:01:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014457 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 B6393202961 for ; Thu, 13 Mar 2025 07:02:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849324; cv=none; b=EMTw4tjCqTrFIUY39cTWjXSg9AH/1xcqwmQBCL38yzt6q823Kk1KDgEnTdpKoY1pynnivrYD0DgOzyMI2H/r896Di9Q9+G8LkQxOjI2BwiKBD/2+oOn4hiE+hNB0VvNyHCcKSsm7KCSVATb3wucrhh99ZchfZKN2Rk4ZUlwoYGs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849324; c=relaxed/simple; bh=HCjJw4xyhEzQ6zT8qCsmgUzt1olFBpZX2ZwL4amUYp0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=EEENYOfqiiL4l6HsNlzZKdx9yR9h2/olkK3i/L+kov6jg+uM7pASCbwuf9o/qk1/RpjSMo1pCZl5o7NhQ9q/eL10Q3nV1QKOFQViWemlwU8XFnWWnt0vnoPeOGU9WeM//IrrrBsfmXhH/TVNdMmSWa0Q6wrUtqzNG2JsXjM3ssI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=F/CrAVk8; arc=none smtp.client-ip=209.85.216.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass 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="F/CrAVk8" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2feb91a25bdso1135671a91.1 for ; Thu, 13 Mar 2025 00:02:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849322; x=1742454122; 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=UkGikd/Ad/tPfmKfWOKmNpfe6xZfSLW8Y3eIuF8D5nY=; b=F/CrAVk8xN1b3CJ5BbtJjJjQzf3qL7RZFEAOgHE/oeNDFmHTkBAgzgjNq66Lc2ipyo 96xPRPjxeNqbMs38Cv/Ej96T9Hd6j9GaZnyhfYqTvorYdTDlhGVdaLg20ei7gMAZULOM b4aMpNDDSnrJTk9CfFHEqhDCVeoxKczcCX5oqZcClXKr3V+eHEzvYSrPYGCYMFvj3Tf+ EyXgxBslmcdTn8cbfFyjAW5GkkwgWyfNK3CP7fhkKBkCFx0n45OA6MeLYtSXZ+Uy9Hwc 4H9U7tWy24yClf5VxOyxDKW/sJW61jN6BD8gtKe3XGTjtd7nEARaSX+SY9+By/fHdbSY 2FnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849322; x=1742454122; 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=UkGikd/Ad/tPfmKfWOKmNpfe6xZfSLW8Y3eIuF8D5nY=; b=Jr2P3rHSfoW3/WpH3LgJrvSxMjDP2wwfFOoZoVSBN1kG0H044hMHBIiuR3BqslYJyw F7UulQtvZPa1T08bha1Vq/ga48tqDRsZO/uFZRgAtMWvWTF7E+0uATcDFNmKpYyTPnTn 3trBmXgOoFjaLdLomNZfeuh6MTTWfNWiijN5yr57rVzQCPNsf49Gu+JLAyI6GL8ZCkH8 8512us1qFUfxpHVuVGCXprinC/67LTlEjovmfqebaMUfMQNYeGB0DdbrM3Y8zpNRZu6c L/nTcb3efP6gxcxX1Qdo8Klxot1WG58EF0lkaJGNVFTJ6vrJsvNJAN/GWdA8OrPK5keu jmEw== X-Forwarded-Encrypted: i=1; AJvYcCVqasDtuR+ZJ6gBMj/b1xWZ2qUbpVtz5MKFE8j2ln7SYmWGw9iNau9RZzBENoq4REmKmNw=@vger.kernel.org X-Gm-Message-State: AOJu0YwyMt+0F6RUckHfvf1BzWspQPVt6h0LB8LVMpsfzr2acIlA1N4+ 30bj8/0gmOSmB/RrlX0cPDzidoW7sGQoY1GDGE03ZELzFkLkCR4G+NkUNIzk/fg= X-Gm-Gg: ASbGncsq4FOhkP6hQbsWVw5vFNEGWaasYTn+a/SYeWh5WrA4eDczk8KlP0ftZHORijW 6H5ylFCs3yJzqhBd62XY1TyY58TG7b3LDBptjWGkUuLeIuCIGd1+VCRInwiLPkE01s44fPojLJz C7+URhIaiBi0koK9stn6PVbSIDuVn/ERCOsa2gMXqKEJVIG3I85rkyG53G0hJiGtDVxGQu3vpPU vateyS/DtXCTt0usNVQxcvjR9uexCS7JZYpZKxV4AdkAyca7iwMkm83yidjM/twTtp2YEvAWD3K v1LKuofO2mRZv5Q4vSnvplXpDt+oaRaegxIRFJvpwZAfqqtP X-Google-Smtp-Source: AGHT+IHTpYDPSLT5yc7d72SDsCx1rUk7tGT6u0RLi/zmklDJcrhUC3W1KPTvb1y0wV1IhmZnoguH1A== X-Received: by 2002:a17:90b:1c83:b0:2fe:a77b:d97e with SMTP id 98e67ed59e1d1-300ff0cadb2mr14806388a91.11.1741849321996; Thu, 13 Mar 2025 00:02:01 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-3010342f135sm3173081a91.1.2025.03.13.00.01.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:02:01 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:09 +0900 Subject: [PATCH net-next v10 06/10] tap: Introduce virtio-net hash feature Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-6-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Add ioctls and storage required for the virtio-net hash feature to TAP. Signed-off-by: Akihiko Odaki --- drivers/net/tap.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++-- include/linux/if_tap.h | 2 ++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/drivers/net/tap.c b/drivers/net/tap.c index 25c60ff2d3f2..86b5e7b88614 100644 --- a/drivers/net/tap.c +++ b/drivers/net/tap.c @@ -49,6 +49,10 @@ struct major_info { struct list_head next; }; +struct tap_skb_cb { + struct virtio_net_hash hash; +}; + #define GOODCOPY_LEN 128 static const struct proto_ops tap_socket_ops; @@ -179,9 +183,20 @@ static void tap_put_queue(struct tap_queue *q) sock_put(&q->sk); } +static struct tap_skb_cb *tap_skb_cb(const struct sk_buff *skb) +{ + BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct tap_skb_cb)); + return (struct tap_skb_cb *)skb->cb; +} + +static struct virtio_net_hash *tap_add_hash(struct sk_buff *skb) +{ + return &tap_skb_cb(skb)->hash; +} + static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb) { - return NULL; + return &tap_skb_cb(skb)->hash; } /* @@ -194,6 +209,7 @@ static const struct virtio_net_hash *tap_find_hash(const struct sk_buff *skb) 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 @@ -201,17 +217,47 @@ static struct tap_queue *tap_get_queue(struct tap_dev *tap, * racing against queue removal. */ int numvtaps = READ_ONCE(tap->numvtaps); + struct tun_vnet_hash_container *vnet_hash = rcu_dereference(tap->vnet_hash); __u32 rxq; + *tap_skb_cb(skb) = (struct tap_skb_cb) { + .hash = { .report = VIRTIO_NET_HASH_REPORT_NONE } + }; + if (!numvtaps) goto out; if (numvtaps == 1) goto single; + if (vnet_hash) { + if ((vnet_hash->common.flags & TUN_VNET_HASH_RSS)) { + rxq = tun_vnet_rss_select_queue(numvtaps, vnet_hash, skb, tap_add_hash); + queue = rcu_dereference(tap->taps[rxq]); + goto out; + } + + if (!skb->l4_hash && !skb->sw_hash) { + struct flow_keys keys; + + skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); + rxq = flow_hash_from_keys(&keys); + keys_basic = (struct flow_keys_basic) { + .control = keys.control, + .basic = keys.basic + }; + } else { + skb_flow_dissect_flow_keys_basic(NULL, skb, &keys_basic, NULL, 0, 0, 0, + FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); + rxq = skb->hash; + } + } else { + rxq = skb_get_hash(skb); + } + /* Check if we can use flow to select a queue */ - rxq = skb_get_hash(skb); if (rxq) { + tun_vnet_hash_report(vnet_hash, skb, &keys_basic, rxq, tap_add_hash); queue = rcu_dereference(tap->taps[rxq % numvtaps]); goto out; } @@ -998,6 +1044,16 @@ static long tap_ioctl(struct file *file, unsigned int cmd, rtnl_unlock(); return ret; + case TUNGETVNETHASHCAP: + return tun_vnet_ioctl_gethashcap(argp); + + case TUNSETVNETHASH: + rtnl_lock(); + tap = rtnl_dereference(q->tap); + ret = tap ? tun_vnet_ioctl_sethash(&tap->vnet_hash, argp) : -EBADFD; + rtnl_unlock(); + return ret; + case SIOCGIFHWADDR: rtnl_lock(); tap = tap_get_tap_dev(q); diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h index 553552fa635c..7334c46a3f10 100644 --- a/include/linux/if_tap.h +++ b/include/linux/if_tap.h @@ -31,6 +31,7 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct file *f) #define MAX_TAP_QUEUES 256 struct tap_queue; +struct tun_vnet_hash_container; struct tap_dev { struct net_device *dev; @@ -43,6 +44,7 @@ struct tap_dev { int numqueues; netdev_features_t tap_features; int minor; + struct tun_vnet_hash_container __rcu *vnet_hash; void (*update_features)(struct tap_dev *tap, netdev_features_t features); void (*count_tx_dropped)(struct tap_dev *tap); From patchwork Thu Mar 13 07:01:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014458 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 9FBD920370B for ; Thu, 13 Mar 2025 07:02:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849330; cv=none; b=aH9Wlsgxk55nCe9toCeFuyjEVnN93WwggR7/cvNgiYeLeNhMupfb3CGvJiwCTG0dGQy0dpeJwvH6YCGXoRuEB3fs29xJnRO9R5XHVKqrpIXbhoRVHZcLPiNtWdnn0h0r57gfQ2Voy4xmBHkvIYyfGEKa8ApBbi7cItyF5EyhHAI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849330; c=relaxed/simple; bh=GneHmk/qL//iBdk3HQU3hkToakkrBvwUGgglHdEvMAI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=g7vdGssfzON6uKNbeosyeRhAp8QuITG9S8udwoWrqbZllSmsvN6pwE97oo7yMwda1sHabeiKyfJ/cDMhrfKjxDyXlOT7toN1jVGleClabsq8xfTe7EVJgawDJFUwfjQc/lCPcnIS9Txqdz7FvmVp2NrqEqKp7lBOQ4i3COxBEp8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=Sxrp6bG1; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass 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="Sxrp6bG1" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-223959039f4so11311535ad.3 for ; Thu, 13 Mar 2025 00:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849328; x=1742454128; 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=Tr9XV/m9Yn0vfZS8el5Q1nXWgm/+NfgkMX4LFmMiC1g=; b=Sxrp6bG1mxL6vWdR8RGIDQQkX9KNPZJ34DspQcOeO1giXocwQVlD/GaGu3NJAf+osn UqeKnrj8plUVma2iEDhpLchJNffFqCo5KlNnxuJMiFKyiStxgjUxTh53cO2dXn9d5GhZ di2J7oTFDn5lYbsM6ht5OsQ0YanuxIrU30hGkW2HPds9Qrw4v9/v0oB4Fv40HgXoJGPe 0yqfo+ojJAPukwNDtR5sbpuxAcDtcvpGvyqPebYxcWQgPJvZYhwlG1/6bcoEqlKPsLWL mEeBSRWKXggnxvRM+JjJP8KvktZT3Wc7LrsK0+Zw6qea5qT5MknQdcqPzxkRthlHWAY9 krEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849328; x=1742454128; 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=Tr9XV/m9Yn0vfZS8el5Q1nXWgm/+NfgkMX4LFmMiC1g=; b=eMcBzNYJSE/LLHpGPln7vWu/vdwSQE8YpliBbxYs4TK1XHNd8lHQ+QVdc0SqhW7/t1 TWsj5nDeYp63s53d8VKajMAQmGY6tXTL+Igud+5hSI/GAkYNaNJUk5OtH8gp19805DGy wYPvggwsub4c/pRRVMudqzhP0wO/vtt9Lq/nSuS6sftD7pL6gi0t4glohrDRdWmPjn/2 FQvlYw6vFlwMLUq/c3odKIFgGBfvSSA1CHSkPJpMjIZbqm3wLuIUukRUrroD2Q5Xnvm6 B32ZYSjlJyww+TgANvuhBzDnq5xo8pYcQah7dSkJ6PKLPva83euWwJ6OAwE7sj2Q1RbQ 7tnw== X-Forwarded-Encrypted: i=1; AJvYcCUG3gaCqv7GCzmTe62fkjgecoXHlMx9OC3FyGp2RdlpW+Yv9q64tdOmcwj0MU9UlV9aqt0=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2Ctzglc6BWclCBmz1DXG0rFAtTrH7LJH14auG24mMoVOg5qEC lwnk47FPGWCc4vGb0J5TPnZTD5KyBmg9Yv6xaqv1LQ+pIvLtPzDP+jvA6jh8MP0= X-Gm-Gg: ASbGncs1juK22rC1l1LkVwB/yDWFz+RmV6AFKK4nRLub+IEnRIQMoq0whk/iDMICPo5 Hj3XVsPPGT0gsJRb2oUzgg2prNJVFivLsMIKaFpZeemSIbwPu79RNVcz3w0GWdLVg/1i3EE2Pc0 uzXEefcAv9IsA85DLVGH9BSxNoVzQKgLLKmyFKa1yhvo4lvpOWtZVmyCF7uBrjmcdW/zhXxPHdd XF5f3xgh14OcJL3MbUh+Toxcha+xcgKDZi6Sq6QCnRBHE0ca0vHIycuJBRk3ntQ+HzNknjQwF8p JaLRMcHQiVMAmvDHtr2Pr59f1n8bBNzHLZm125ZB+RiG5fny X-Google-Smtp-Source: AGHT+IELBqab1Bv03Rmb+11tOmP6uVWtb9Wp6RhocPBZEFYo+tXlKUya4SfhZrxV9fTmHmtnOCET6w== X-Received: by 2002:a05:6a00:27a0:b0:736:532b:7c10 with SMTP id d2e1a72fcca58-736aaae81dbmr33112877b3a.21.1741849327993; Thu, 13 Mar 2025 00:02:07 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-737115293f6sm662442b3a.14.2025.03.13.00.02.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:02:07 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:10 +0900 Subject: [PATCH net-next v10 07/10] selftest: tun: Test vnet ioctls without device Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-7-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Ensure that vnet ioctls result in EBADFD when the underlying device is deleted. Signed-off-by: Akihiko Odaki Tested-by: Lei Yang --- tools/testing/selftests/net/tun.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c index fa83918b62d1..ad168c15c02d 100644 --- a/tools/testing/selftests/net/tun.c +++ b/tools/testing/selftests/net/tun.c @@ -159,4 +159,42 @@ TEST_F(tun, reattach_close_delete) { EXPECT_EQ(tun_delete(self->ifname), 0); } +FIXTURE(tun_deleted) +{ + char ifname[IFNAMSIZ]; + int fd; +}; + +FIXTURE_SETUP(tun_deleted) +{ + self->ifname[0] = 0; + self->fd = tun_alloc(self->ifname); + ASSERT_LE(0, self->fd); + + ASSERT_EQ(0, tun_delete(self->ifname)) + EXPECT_EQ(0, close(self->fd)); +} + +FIXTURE_TEARDOWN(tun_deleted) +{ + EXPECT_EQ(0, close(self->fd)); +} + +TEST_F(tun_deleted, getvnethdrsz) +{ + ASSERT_EQ(-1, ioctl(self->fd, TUNGETVNETHDRSZ)); + EXPECT_EQ(EBADFD, errno); +} + +TEST_F(tun_deleted, 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_HARNESS_MAIN From patchwork Thu Mar 13 07:01:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014459 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 C5E6F20371C for ; Thu, 13 Mar 2025 07:02:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849336; cv=none; b=DDFp0zcLssPnKDfHrbCw2v7KfCzwrdBnlVCtSY0SYnCu0f+NQX6nKrNn5EGG0rVJXYk+wI0vUc2Honpu+xrpB1w7TYjz69PQYCi2KLxgndwv6/txc2t5QdEhJCYwpnR4vuEubwBHleGtYzEUcu6/SMj0MidUaeM15CCXC7DJ/30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849336; c=relaxed/simple; bh=lN6+uLr4YpWJFlGsF93rg99nxoQ2xyOlduljX4096YQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=e+fNLHk7jYdq2guh6bYYb5ppSGm6m2okr1uBMFoaEuhRgAxp2EAdMrQZU9zEpMCbhF4kMq8KI/hdOe4Y7ik7tTAsx6H6gC4ug+dL3nQFr4GxjVeoI7FYpU/tpybJS+2Ro40+EU4nn2TwfnpnRA4pE/Trq85BJDOMDf8ZGT9pbDE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=XYDIpekC; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass 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="XYDIpekC" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2235189adaeso12417995ad.0 for ; Thu, 13 Mar 2025 00:02:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849334; x=1742454134; 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=yuCKs6/yWaq7hp5qKF+oZHxstBPYJ3gUDKOwyUJw0c0=; b=XYDIpekCFrCMZ+nUOfsBleLKarZ6NUUIOcb9ZY+Fl2gz6slfDZInFA9aOXSNokgMYI 6EUcU4rdByou0xk+JDjUl0wObuco3zgXHfkhxeiN44C982GfOiOVi9ZsBnyjMZZh5BNo sSfUqm3aFSTZ0rgRrpeeCIPzLsiPNBuxiFcw05wymhWSSxhrsfhyRHWNNewXn1cRET8J yVMqptwop55QJ4e0g9HXKbqOSrCFORPrTaDjIXH0E/zikxZ5nbQBnWr6xxgQL/iz50lj JOEZ0RTHFE07EHzldp9sJ7mUFSAS20emdL4kZrXFY0A2rkenfS5yOHbcbg7ZLq3lXJ1s nQmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849334; x=1742454134; 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=yuCKs6/yWaq7hp5qKF+oZHxstBPYJ3gUDKOwyUJw0c0=; b=irQNSDQiuxFiE65pmsz6fLp2RD8yu8vuaAZTNWeoyOx1kGgvOWj+YRionhqAbuCQOu gzw6YJtF8Kx8bPLekC7rSIDNLgrnjpMN0SRljVa0w6u1N3sVmnACT4zNWmUvozs7z4RD NcP8h1dOCSAZIk86OKk7c0KnoZpfI+fUgp84mTw5FAdFGOCvPfLQX5EQQd9utbEsPtZ5 9lzgAIh2UiwSkBO9SUg+pf0BUfZBuAN5qTfzbVScsMKeWJAEfPOBvzao7mdJG9nYqxTk AMB9SadRKzEqzqJpr7TONhTwd5rbxMJmXBkNxSy9lnnXqhY9ya67obps6N3uYzrpcSdW MeFA== X-Forwarded-Encrypted: i=1; AJvYcCWuUjYK6D+scJos55YGoKIfaq93Xgy/0uVsl7vi+5F+h9oBOiLJ8ndYKHDsJ8Om42KeJew=@vger.kernel.org X-Gm-Message-State: AOJu0YzY1c/K2ev5A/qpY5BE8CXZcuBWVRi0HrGaROTaNnkzllwI1svg Wv4lXVAzX4Nyj2GjDlXkpkGFhFFpFU1kRFz4yXwQNzQ0bEZXqrZfil8KkP3PkTg= X-Gm-Gg: ASbGnctc4KW8LBHD6A0daCMERXZgULASm2jJTIQlkfefiNPVO4Y4OnqHD9MlXh74lIR VZDNkSCEbHVybSqeJ6Mlxh/Hmj6O7dmdeTtCaDRHA3TTW14bGctNC7kvLM72dGRZQsG1DxjFoxu ga5K7CG573Y0xzoR7CesekM8ZfNhGsOVKmeLGSkXKDHtuFwG3Sgp/bUBUF93epeqOQUG88cveMI kGmJmee1dLz2SBDlgouxB/V08EycRXF5/9d/gs13MVIUxsiqtoWlL7pDREVbUL4rH73k1J0qdXt 3oRkEn8dBzyeWHNeEEhtDUYxrYE6ALkGyhGxEd9IrhWvJN2KUNUc09ra6h0= X-Google-Smtp-Source: AGHT+IG81zJPizCcoXzf4ClNcRogGgyw+0yJZmt0+bldrZTjgnghDH1AlriGc/EMPAt01XLM4xZcNg== X-Received: by 2002:a17:902:d4c4:b0:216:271d:e06c with SMTP id d9443c01a7336-225c651d6aemr16176625ad.4.1741849333918; Thu, 13 Mar 2025 00:02:13 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-225c6ba6f38sm6536955ad.129.2025.03.13.00.02.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:02:13 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:11 +0900 Subject: [PATCH net-next v10 08/10] selftest: tun: Add tests for virtio-net hashing Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-8-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 The added tests confirm tun can perform RSS for all supported hash types to select the receive queue and report hash values. Signed-off-by: Akihiko Odaki Tested-by: Lei Yang --- tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/tun.c | 455 ++++++++++++++++++++++++++++++++++- 2 files changed, 447 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index 73ee88d6b043..9772f691a9a0 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -123,6 +123,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 diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c index ad168c15c02d..dfb84da50d91 100644 --- a/tools/testing/selftests/net/tun.c +++ b/tools/testing/selftests/net/tun.c @@ -2,21 +2,38 @@ #define _GNU_SOURCE +#include #include #include +#include +#include #include #include #include #include -#include +#include +#include +#include +#include +#include +#include +#include #include +#include #include #include -#include -#include +#include +#include +#include +#include #include "../kselftest_harness.h" +#define TUN_HWADDR_SOURCE { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 } +#define TUN_HWADDR_DEST { 0x02, 0x00, 0x00, 0x00, 0x00, 0x01 } +#define TUN_IPADDR_SOURCE htonl((172 << 24) | (17 << 16) | 0) +#define TUN_IPADDR_DEST htonl((172 << 24) | (17 << 16) | 1) + static int tun_attach(int fd, char *dev) { struct ifreq ifr; @@ -39,7 +56,7 @@ static int tun_detach(int fd, char *dev) return ioctl(fd, TUNSETQUEUE, (void *) &ifr); } -static int tun_alloc(char *dev) +static int tun_alloc(char *dev, short flags) { struct ifreq ifr; int fd, err; @@ -52,7 +69,8 @@ static int tun_alloc(char *dev) memset(&ifr, 0, sizeof(ifr)); strcpy(ifr.ifr_name, dev); - ifr.ifr_flags = IFF_TAP | IFF_NAPI | IFF_MULTI_QUEUE; + ifr.ifr_flags = flags | IFF_TAP | IFF_NAPI | IFF_NO_PI | + IFF_MULTI_QUEUE; err = ioctl(fd, TUNSETIFF, (void *) &ifr); if (err < 0) { @@ -64,6 +82,20 @@ static int tun_alloc(char *dev) return fd; } +static bool tun_set_flags(int local_fd, const char *name, short flags) +{ + struct ifreq ifreq = { .ifr_flags = flags }; + + strcpy(ifreq.ifr_name, name); + + if (ioctl(local_fd, SIOCSIFFLAGS, &ifreq)) { + perror("SIOCSIFFLAGS"); + return false; + } + + return true; +} + static int tun_delete(char *dev) { struct { @@ -102,6 +134,107 @@ static int tun_delete(char *dev) return ret; } +static uint32_t tun_sum(const void *buf, size_t len) +{ + const uint16_t *sbuf = buf; + uint32_t sum = 0; + + while (len > 1) { + sum += *sbuf++; + len -= 2; + } + + if (len) + sum += *(uint8_t *)sbuf; + + return sum; +} + +static uint16_t tun_build_ip_check(uint32_t sum) +{ + return ~((sum & 0xffff) + (sum >> 16)); +} + +static uint32_t tun_build_ip_pseudo_sum(const void *iphdr) +{ + uint16_t tot_len = ntohs(((struct iphdr *)iphdr)->tot_len); + + return tun_sum((char *)iphdr + offsetof(struct iphdr, saddr), 8) + + htons(((struct iphdr *)iphdr)->protocol) + + htons(tot_len - sizeof(struct iphdr)); +} + +static uint32_t tun_build_ipv6_pseudo_sum(const void *ipv6hdr) +{ + return tun_sum((char *)ipv6hdr + offsetof(struct ipv6hdr, saddr), 32) + + ((struct ipv6hdr *)ipv6hdr)->payload_len + + htons(((struct ipv6hdr *)ipv6hdr)->nexthdr); +} + +static void tun_build_iphdr(void *dest, uint16_t len, uint8_t protocol) +{ + struct iphdr iphdr = { + .ihl = sizeof(iphdr) / 4, + .version = 4, + .tot_len = htons(sizeof(iphdr) + len), + .ttl = 255, + .protocol = protocol, + .saddr = TUN_IPADDR_SOURCE, + .daddr = TUN_IPADDR_DEST + }; + + iphdr.check = tun_build_ip_check(tun_sum(&iphdr, sizeof(iphdr))); + memcpy(dest, &iphdr, sizeof(iphdr)); +} + +static void tun_build_ipv6hdr(void *dest, uint16_t len, uint8_t protocol) +{ + struct ipv6hdr ipv6hdr = { + .version = 6, + .payload_len = htons(len), + .nexthdr = protocol, + .saddr = { + .s6_addr32 = { + htonl(0xffff0000), 0, 0, TUN_IPADDR_SOURCE + } + }, + .daddr = { + .s6_addr32 = { + htonl(0xffff0000), 0, 0, TUN_IPADDR_DEST + } + }, + }; + + memcpy(dest, &ipv6hdr, sizeof(ipv6hdr)); +} + +static void tun_build_tcphdr(void *dest, uint32_t sum) +{ + struct tcphdr tcphdr = { + .source = htons(9), + .dest = htons(9), + .fin = 1, + .doff = sizeof(tcphdr) / 4, + }; + uint32_t tcp_sum = tun_sum(&tcphdr, sizeof(tcphdr)); + + tcphdr.check = tun_build_ip_check(sum + tcp_sum); + memcpy(dest, &tcphdr, sizeof(tcphdr)); +} + +static void tun_build_udphdr(void *dest, uint32_t sum) +{ + struct udphdr udphdr = { + .source = htons(9), + .dest = htons(9), + .len = htons(sizeof(udphdr)), + }; + uint32_t udp_sum = tun_sum(&udphdr, sizeof(udphdr)); + + udphdr.check = tun_build_ip_check(sum + udp_sum); + memcpy(dest, &udphdr, sizeof(udphdr)); +} + FIXTURE(tun) { char ifname[IFNAMSIZ]; @@ -112,10 +245,10 @@ FIXTURE_SETUP(tun) { memset(self->ifname, 0, sizeof(self->ifname)); - self->fd = tun_alloc(self->ifname); + self->fd = tun_alloc(self->ifname, 0); ASSERT_GE(self->fd, 0); - self->fd2 = tun_alloc(self->ifname); + self->fd2 = tun_alloc(self->ifname, 0); ASSERT_GE(self->fd2, 0); } @@ -168,7 +301,7 @@ FIXTURE(tun_deleted) FIXTURE_SETUP(tun_deleted) { self->ifname[0] = 0; - self->fd = tun_alloc(self->ifname); + self->fd = tun_alloc(self->ifname, 0); ASSERT_LE(0, self->fd); ASSERT_EQ(0, tun_delete(self->ifname)) @@ -197,4 +330,308 @@ TEST_F(tun_deleted, getvnethashcap) EXPECT_EQ(0, i); } -TEST_HARNESS_MAIN +FIXTURE(tun_vnet_hash) +{ + int dest_fds[3]; + unsigned int dest_ifindex; + int source_fd; + char buffer[sizeof(struct ipv6hdr) + sizeof(struct tcphdr)]; + uint16_t len; +}; + +FIXTURE_VARIANT(tun_vnet_hash) +{ + uint16_t eth_p; + uint8_t ipproto; + uint8_t flags; + uint16_t hash_report; + uint32_t hash_value; +}; + +FIXTURE_VARIANT_ADD(tun_vnet_hash, unclassified) +{ + .eth_p = ETH_P_LOOPBACK +}; + +FIXTURE_VARIANT_ADD(tun_vnet_hash, ipv4) +{ + .eth_p = ETH_P_IP, + .ipproto = 253, + .hash_report = VIRTIO_NET_HASH_REPORT_IPv4, + .hash_value = 0x6e45d952 +}; + +FIXTURE_VARIANT_ADD(tun_vnet_hash, tcpv4) +{ + .eth_p = ETH_P_IP, + .ipproto = IPPROTO_TCP, + .hash_report = VIRTIO_NET_HASH_REPORT_TCPv4, + .hash_value = 0xfb63539a +}; + +FIXTURE_VARIANT_ADD(tun_vnet_hash, udpv4) +{ + .eth_p = ETH_P_IP, + .ipproto = IPPROTO_UDP, + .hash_report = VIRTIO_NET_HASH_REPORT_UDPv4, + .hash_value = 0xfb63539a +}; + +FIXTURE_VARIANT_ADD(tun_vnet_hash, ipv6) +{ + .eth_p = ETH_P_IPV6, + .ipproto = 253, + .hash_report = VIRTIO_NET_HASH_REPORT_IPv6, + .hash_value = 0xd6eb560f +}; + +FIXTURE_VARIANT_ADD(tun_vnet_hash, tcpv6) +{ + .eth_p = ETH_P_IPV6, + .ipproto = IPPROTO_TCP, + .hash_report = VIRTIO_NET_HASH_REPORT_TCPv6, + .hash_value = 0xc2b9f251 +}; + +FIXTURE_VARIANT_ADD(tun_vnet_hash, udpv6) +{ + .eth_p = ETH_P_IPV6, + .ipproto = IPPROTO_UDP, + .hash_report = VIRTIO_NET_HASH_REPORT_UDPv6, + .hash_value = 0xc2b9f251 +}; + +FIXTURE_SETUP(tun_vnet_hash) +{ + static const struct { + struct tun_vnet_hash hdr; + struct tun_vnet_hash_rss rss; + uint16_t rss_indirection_table[2]; + uint8_t rss_key[40]; + } vnet_hash = { + .hdr = { + .flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS, + .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 + }, + .rss = { .indirection_table_mask = 1, .unclassified_queue = 5 }, + .rss_indirection_table = { 3, 4 }, + .rss_key = { + 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2, + 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0, + 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4, + 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c, + 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa + } + }; + + struct { + struct virtio_net_hdr_v1_hash vnet_hdr; + struct ethhdr ethhdr; + struct arphdr arphdr; + unsigned char sender_hwaddr[6]; + uint32_t sender_ipaddr; + unsigned char target_hwaddr[6]; + uint32_t target_ipaddr; + } __packed packet = { + .ethhdr = { + .h_source = TUN_HWADDR_SOURCE, + .h_dest = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, + .h_proto = htons(ETH_P_ARP) + }, + .arphdr = { + .ar_hrd = htons(ARPHRD_ETHER), + .ar_pro = htons(ETH_P_IP), + .ar_hln = ETH_ALEN, + .ar_pln = 4, + .ar_op = htons(ARPOP_REQUEST) + }, + .sender_hwaddr = TUN_HWADDR_DEST, + .sender_ipaddr = TUN_IPADDR_DEST, + .target_ipaddr = TUN_IPADDR_DEST + }; + + struct tun_vnet_hash cap; + char dest_ifname[IFNAMSIZ] = ""; + int i, local_fd; + uint16_t ip_payload_len; + uint32_t ip_sum; + + self->dest_fds[0] = tun_alloc(dest_ifname, IFF_VNET_HDR); + ASSERT_LE(0, self->dest_fds[0]) { + EXPECT_EQ(0, close(self->dest_fds[0])); + } + + i = ioctl(self->dest_fds[0], TUNGETVNETHASHCAP, &cap); + if (i == -1 && errno == EINVAL) { + EXPECT_EQ(0, close(self->dest_fds[0])); + SKIP(return, "TUNGETVNETHASHCAP not supported"); + } + + ASSERT_EQ(0, i) + EXPECT_EQ(0, close(self->dest_fds[0])); + + if ((cap.flags & vnet_hash.hdr.flags) != vnet_hash.hdr.flags) { + EXPECT_EQ(0, close(self->dest_fds[0])); + SKIP(return, "Lacks some hash flag support"); + } + + if ((cap.types & vnet_hash.hdr.types) != vnet_hash.hdr.types) { + EXPECT_EQ(0, close(self->dest_fds[0])); + SKIP(return, "Lacks some hash type support"); + } + + self->dest_ifindex = if_nametoindex(dest_ifname); + ASSERT_TRUE(self->dest_ifindex) + EXPECT_EQ(0, close(self->dest_fds[0])); + + i = sizeof(struct virtio_net_hdr_v1_hash); + ASSERT_EQ(0, ioctl(self->dest_fds[0], TUNSETVNETHDRSZ, &i)) + EXPECT_EQ(0, close(self->dest_fds[0])); + + i = 1; + ASSERT_EQ(0, ioctl(self->dest_fds[0], TUNSETVNETLE, &i)) + EXPECT_EQ(0, close(self->dest_fds[0])); + + local_fd = socket(AF_LOCAL, SOCK_STREAM, 0); + ASSERT_LE(0, local_fd) + EXPECT_EQ(0, close(self->dest_fds[0])); + + i = tun_set_flags(local_fd, dest_ifname, IFF_UP); + EXPECT_EQ(0, close(local_fd)); + ASSERT_TRUE(i) + EXPECT_EQ(0, close(self->dest_fds[0])); + + ASSERT_EQ(sizeof(packet), + write(self->dest_fds[0], &packet, sizeof(packet))) + EXPECT_EQ(0, close(self->dest_fds[0])); + + ASSERT_EQ(0, ioctl(self->dest_fds[0], TUNSETVNETHASH, &vnet_hash)) + EXPECT_EQ(0, close(self->dest_fds[0])); + + self->source_fd = socket(AF_PACKET, SOCK_DGRAM, 0); + ASSERT_LE(0, self->source_fd) + EXPECT_EQ(0, close(self->dest_fds[0])); + + for (i = 1; i < ARRAY_SIZE(self->dest_fds); i++) { + self->dest_fds[i] = tun_alloc(dest_ifname, IFF_VNET_HDR); + ASSERT_LE(0, self->dest_fds[i]) { + while (i) { + i--; + EXPECT_EQ(0, close(self->dest_fds[i])); + } + } + } + + if (variant->eth_p == ETH_P_IP || variant->eth_p == ETH_P_IPV6) { + switch (variant->ipproto) { + case IPPROTO_TCP: + ip_payload_len = sizeof(struct tcphdr); + break; + + case IPPROTO_UDP: + ip_payload_len = sizeof(struct udphdr); + break; + + default: + ip_payload_len = 0; + } + + switch (variant->eth_p) { + case ETH_P_IP: + tun_build_iphdr(self->buffer, ip_payload_len, variant->ipproto); + self->len = sizeof(struct iphdr); + ip_sum = tun_build_ip_pseudo_sum(self->buffer); + break; + + case ETH_P_IPV6: + tun_build_ipv6hdr(self->buffer, ip_payload_len, variant->ipproto); + self->len = sizeof(struct ipv6hdr); + ip_sum = tun_build_ipv6_pseudo_sum(self->buffer); + break; + } + + switch (variant->ipproto) { + case IPPROTO_TCP: + tun_build_tcphdr(self->buffer + self->len, ip_sum); + break; + + case IPPROTO_UDP: + tun_build_udphdr(self->buffer + self->len, ip_sum); + break; + } + + self->len += ip_payload_len; + } +} + +FIXTURE_TEARDOWN(tun_vnet_hash) +{ + EXPECT_EQ(0, close(self->source_fd)); + + for (size_t i = 0; i < ARRAY_SIZE(self->dest_fds); i++) + EXPECT_EQ(0, close(self->dest_fds[i])); +} + +TEST_F(tun_vnet_hash, rx) +{ + size_t len = sizeof(struct virtio_net_hdr_v1_hash) + ETH_HLEN + self->len; + struct { + struct virtio_net_hdr_v1_hash vnet_hdr; + struct ethhdr ethhdr; + char payload[sizeof(self->buffer)]; + } buffer; + struct virtio_net_hdr_v1_hash hdr = { + .hdr = { .flags = variant->flags }, + .hash_value = htole32(variant->hash_value), + .hash_report = htole16(variant->hash_report) + }; + int txq = variant->hash_report ? variant->hash_value & 1 : 2; + struct sockaddr_ll ll = { + .sll_family = AF_PACKET, + .sll_addr = TUN_HWADDR_DEST, + .sll_halen = ETH_ALEN, + .sll_ifindex = self->dest_ifindex, + .sll_protocol = htons(variant->eth_p), + }; + + EXPECT_EQ(self->len, + sendto(self->source_fd, self->buffer, self->len, 0, + (struct sockaddr *)&ll, sizeof(ll))); + EXPECT_EQ(len, read(self->dest_fds[txq], &buffer, len)); + ASSERT_FALSE(memcmp(&buffer, &hdr, sizeof(hdr))); + ASSERT_FALSE(memcmp(buffer.payload, self->buffer, self->len)); +} + +int main(int argc, char **argv) +{ + FILE *file; + + if (unshare(CLONE_NEWNET)) { + perror("unshare"); + return KSFT_FAIL; + } + + /* Disable IPv6 to eliminate IPv6 Neighbor Discovery messages. */ + file = fopen("/proc/sys/net/ipv6/conf/default/disable_ipv6", "w"); + if (file) { + if (fputc('1', file) != '1') { + perror("fputc"); + return KSFT_FAIL; + } + + if (fclose(file)) { + perror("fclose"); + return KSFT_FAIL; + } + } else if (errno != ENOENT) { + perror("fopen"); + return KSFT_FAIL; + } + + return test_harness_run(argc, argv); +} From patchwork Thu Mar 13 07:01:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014460 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 4445A2046B3 for ; Thu, 13 Mar 2025 07:02:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849342; cv=none; b=rfSAZjsSA7Nc1KJziPa1D9u/19+ycEM842sRLm6JaGX4OjEkDPVoFzfScHHYidPEXWCIZikwTf88h997vDsDpKhV7PjF/jJenK75yci581/T9TN0avXLrmvS6K7xW9M8HoPfgWI7I/kwuzxGXGF3uzuws6+sQ/ayiAtI0+wlDFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849342; c=relaxed/simple; bh=ccfXZvxiSAaG5MPb2w/JWrekNXtLfO8PT1kfNykthKs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=kT4+C0HnSIdVH997xm4Mb2XBteIPyUhJSaBxPUjoScGdb7kULUnFmexs4RKcuCnj7VvANs3RXe6JdG8W/iYYSn9F43TUU6zg3nn5t16KqYxyLKur7YXTvS3htL9X5wgtgVmG83GB0NcLu5vslIyodG8Qi2KpW+pqDgTKeflnoZg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=i9r7FPxF; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass 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="i9r7FPxF" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-219f8263ae0so11269255ad.0 for ; Thu, 13 Mar 2025 00:02:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849340; x=1742454140; 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=e3BkVPEmJoa+6yr2vocUy5kvhLH0cmr2yCWFD5W/C7U=; b=i9r7FPxFX/fIVo7ZgYjXUEZDkqOg/OVEQcry6mg+cS+b9Ls4mad/yx0nwZY8CUmkNn ZlbDyxDtZw7eX7ZOiYK+Hz2VZUbBj4okO9JeJiqVvbJoXZ1NiWRx9b/PGhF+OGmrkL9i pTtMdRoqLo5lgRmN5wbx3JUE5wCmXy8ncvusvdXaFvyVg1cjbl6ZFgjdOA+POkN+hs2P EW5xAYjUEJYRplTnVCwTTjx3m66xmvf6RaJwe6bE5ne9GkpFbFVZ26v5bG6C3h9XC6JP DNuW1VnjyI1OZj9bFQ0J3KbgKPOUjpQslNlgdVI+f1tSX/appThPc20ZBUyA1AP+9W+X 9OvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849340; x=1742454140; 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=e3BkVPEmJoa+6yr2vocUy5kvhLH0cmr2yCWFD5W/C7U=; b=stMmyCeJABiFcIPom0CIsW9AZJ44n9z9hjsxZ/XQdYm2/E5j030IKtKc+usF2/wtl8 ZTf7LzUbeQ6p+PZNCr4BFkKATTCxw/Cyc5otM7eBe74k/Je7TwVMDkzwJQcPRucw4ZCR WrYY+lUFF/AiiQbdKMEa7NT+puZq+7ETuETRErhcl03zBVtq7PMYZJ8iBGNM6l8Stgyl EREvS+S3sjFiq9zsXUVdj1Z0p5K86+mA0l0ngNhF+LQzp+I3N58rMxx8Ku4Exh5LF6D8 e5hmRxJZXupSER/pX54OJfdg1W6iMz3LcDdZjCmM4dKxsomqYhyaNKTDSvUIIAdRyU0Z T0rA== X-Forwarded-Encrypted: i=1; AJvYcCWoJfE7vw/fE2IF+XWIg7ai1utT41IYHqbvANimqvSsSkkecRj106eLQFkOi93MlBirOT8=@vger.kernel.org X-Gm-Message-State: AOJu0YwKjfBl/lzOM1OXaAttXMTf+Kb/2AURs8z8MrXzq8GyPhjD8AW2 Nf7UQqz5fslV5UlrBCOzj81s0GQ3mQGZRZU1FvpzBhGbGwNHnOzTdDxqUQvWmAY= X-Gm-Gg: ASbGncsk/jCC3rDtTESgmUAY+HCd4OItpKiRLpD5lIzFRnBRGhxBy1J8nNZ+Chmjwb+ 7g5xRSOnLbIWTm3GSCmPfiL+peGBQoJiNarROsP3zx3wU5na+VTejgICkOGUj12DgE8q/xX+jJ0 u316/V4aBXnwI7BdFMX4mSozXgrbeKWHCrhez1M37dqi1TAs8ZpUZXsVtiAAtI8+1IVhXB0PjyB qhiPCNe5PtByxfb6LJWRGoEMVqJFRVi5lP4dNhG/HA5//4NK4X1zO8o5PhT1zc4godRJcwcwnvu Xabyr3cxqUqqjrSUTrrsxLZQ8Lgo2noK+YaNYgMNNEBG95tH X-Google-Smtp-Source: AGHT+IFaX1olq5WQi+7fcP4HReHGKVkvSZ9sg15FYUug/XHg7z4NmpOMBbEE1p5tCvq05aXsAHzDQQ== X-Received: by 2002:a05:6a00:2d83:b0:736:a7ec:a366 with SMTP id d2e1a72fcca58-736eb7d882bmr12600967b3a.9.1741849340468; Thu, 13 Mar 2025 00:02:20 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-73711695ee8sm632427b3a.154.2025.03.13.00.02.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:02:20 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:12 +0900 Subject: [PATCH net-next v10 09/10] selftest: tap: Add tests for virtio-net ioctls Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-9-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 They only test the ioctls are wired up to the implementation common with tun as it is already tested for tun. Signed-off-by: Akihiko Odaki --- tools/testing/selftests/net/tap.c | 97 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/net/tap.c b/tools/testing/selftests/net/tap.c index 247c3b3ac1c9..fbd38b08fdfa 100644 --- a/tools/testing/selftests/net/tap.c +++ b/tools/testing/selftests/net/tap.c @@ -363,6 +363,7 @@ size_t build_test_packet_crash_tap_invalid_eth_proto(uint8_t *buf, FIXTURE(tap) { int fd; + bool deleted; }; FIXTURE_SETUP(tap) @@ -387,8 +388,10 @@ FIXTURE_TEARDOWN(tap) if (self->fd != -1) close(self->fd); - ret = dev_delete(param_dev_tap_name); - EXPECT_EQ(ret, 0); + if (!self->deleted) { + ret = dev_delete(param_dev_tap_name); + EXPECT_EQ(ret, 0); + } ret = dev_delete(param_dev_dummy_name); EXPECT_EQ(ret, 0); @@ -431,4 +434,94 @@ TEST_F(tap, test_packet_crash_tap_invalid_eth_proto) ASSERT_EQ(errno, EINVAL); } +TEST_F(tap, test_vnethdrsz) +{ + int sz = sizeof(struct virtio_net_hdr_v1_hash); + + ASSERT_FALSE(dev_delete(param_dev_tap_name)); + self->deleted = true; + + ASSERT_FALSE(ioctl(self->fd, TUNSETVNETHDRSZ, &sz)); + sz = 0; + ASSERT_FALSE(ioctl(self->fd, TUNGETVNETHDRSZ, &sz)); + EXPECT_EQ(sizeof(struct virtio_net_hdr_v1_hash), sz); +} + +TEST_F(tap, test_vnetle) +{ + int le = 1; + + ASSERT_FALSE(dev_delete(param_dev_tap_name)); + self->deleted = true; + + ASSERT_FALSE(ioctl(self->fd, TUNSETVNETLE, &le)); + le = 0; + ASSERT_FALSE(ioctl(self->fd, TUNGETVNETLE, &le)); + EXPECT_EQ(1, le); +} + +TEST_F(tap, test_vnetbe) +{ + int be = 1; + int ret; + + ASSERT_FALSE(dev_delete(param_dev_tap_name)); + self->deleted = true; + + ret = ioctl(self->fd, TUNSETVNETBE, &be); + if (ret == -1 && errno == EINVAL) + SKIP(return, "TUNSETVNETBE not supported"); + + ASSERT_FALSE(ret); + be = 0; + ASSERT_FALSE(ioctl(self->fd, TUNGETVNETBE, &be)); + EXPECT_EQ(1, be); +} + +TEST_F(tap, test_getvnethashcap) +{ + static const struct tun_vnet_hash expected = { + .flags = TUN_VNET_HASH_REPORT | TUN_VNET_HASH_RSS, + .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 + }; + struct tun_vnet_hash seen; + int ret; + + ASSERT_FALSE(dev_delete(param_dev_tap_name)); + self->deleted = true; + + ret = ioctl(self->fd, TUNGETVNETHASHCAP, &seen); + + if (ret == -1 && errno == EINVAL) + SKIP(return, "TUNGETVNETHASHCAP not supported"); + + EXPECT_FALSE(ret); + EXPECT_FALSE(memcmp(&expected, &seen, sizeof(expected))); +} + +TEST_F(tap, test_setvnethash_alive) +{ + struct tun_vnet_hash hash = { .flags = 0 }; + + EXPECT_FALSE(ioctl(self->fd, TUNSETVNETHASH, &hash)); +} + +TEST_F(tap, test_setvnethash_deleted) +{ + ASSERT_FALSE(dev_delete(param_dev_tap_name)); + self->deleted = true; + + ASSERT_EQ(-1, ioctl(self->fd, TUNSETVNETHASH)); + + if (errno == EINVAL) + SKIP(return, "TUNSETVNETHASH not supported"); + + EXPECT_EQ(EBADFD, errno); +} + TEST_HARNESS_MAIN From patchwork Thu Mar 13 07:01:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14014461 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 D3561204C2C for ; Thu, 13 Mar 2025 07:02:27 +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=1741849359; cv=none; b=ga98ZONLhUPeUKAPv+sEQ+dGRAhru/ur12RYZ1FPWm5moILYQpTtrTGozBz3SbtJVl1V3nvExLPA7LevoHnreNVeMjG+cx5RO9+Qd4+WGlmL0bY5X7zuUtD3Lrn9z/E7SoS0ETbHHaSVRt3Qw2qaYj5hDYc/js9vF71tCFnMcTE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741849359; c=relaxed/simple; bh=QysKwJMoIqaKFtrng8JoZjRW/Qn7QIvbuXMh0UY/638=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To; b=NiZMM17fVg5aSkXNARSpsFN3FYE/rBSc4CfOk/NC/CsySofPp1/gnC3wmF2lu2tAX7Ym7lICEbHDmwm3QwOfTJbQTy70cFOr+8BFHinaP4ro+IZGSraavQ6578NOROAA7kk51ATY5Rv7N9VA0ht+Tv71JgeELGnmiuECH/Px3Bk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=daynix.com; spf=pass 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=GmEyoS97; 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=pass 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="GmEyoS97" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2ff694d2d4dso1170454a91.0 for ; Thu, 13 Mar 2025 00:02:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741849347; x=1742454147; 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=jXhv781+liPB0AwwevhkbIFrQwob4kiywxMETbO/hoM=; b=GmEyoS97Nt/5JMl1iIhPkE/V0ILB5YyZLkYleokMg6O/uguC1HD//6dcex3s8HXiev OEBu5bzZzypBs1bTA7JS3+wfIzkLoPA1aUeEgp0mJ7IeOSQ8cyTly8NMchROZfY3jDT4 gPDfaNK/UKKGUp8FduHud/EX5Jxp2LePwoIA1OpiQdnYvzwR1ZDV/OTOdT6HFeJO4zat mSkjqihMUXrH3nSF6HiCotlqz8ttd/+I8si9fziCOU5dZYcS8Onh++7hM0++JDvIDHZb X7UeRT+eqxZCsAyNjgJrrDKqiIP2fFB68dYz1k2uKuB+J+juUWh5AF+zfJCZmss47Tvm JUTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741849347; x=1742454147; 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=jXhv781+liPB0AwwevhkbIFrQwob4kiywxMETbO/hoM=; b=EMoMMOS5yuM4LOqmTNqpFjMudBTFsvRpjNR43boK3Ohm78d9dYRKjoPRXDleye4iFI P55dF8scj1laywJEdN7wMRYkD3rkIAvFDdhVmGfQAxwdTFtCgW+zMDvFwArTOg1fNh7s ngVxRks+RZuSigeC8AvQl4O+ptOhUaFJVvuyUrl/VhXXmNxKDKPYLMq67zkq9TCCfSD0 sYSPnPGhNvGw2gdfGINRTwO8vOssriO1zCh/EPYglBPszMbs6fxfxrZExV7YGPBKJjCQ YdPnYC8sRs6fdWDgMp0Do8mO0eAAIeCLktxoYw2beJ5/lcJZKKK4dvZrswdmP9SvRCOW lyLA== X-Forwarded-Encrypted: i=1; AJvYcCXi1O6jNClBAy56Q4z2qaig+D+2zFLOQ3AT8yoYIUy0+wECWKHMpWZkfowkrPUOI+CQoPI=@vger.kernel.org X-Gm-Message-State: AOJu0YzB6BRAbWn/x4VJbmskeOKd3roCrFDqwZU7Aipcla+CJ/FVmy6u WsH+VQ3vy5fiyKGuzykIj/H44EjoQYgD2AfFnUh/uyECKKERlker6b2I3ugVYo8= X-Gm-Gg: ASbGnctB9F+cV92BhIS52RCnCKeeurr0gIYTsuSXgA5ACoOdDU1bWrOMWQXVuyrHLE8 8NuMLsXR8c6N0vI1Z3jyBvweHQUd3Nx9+z30sHTaHgth7834o9t6hwaOARhY2ctrWg0m42dxQrM YZPkmG1FfzOBLVCVTauX5FQoOHjqaGEO4DgKOH52u5vpPdAHDhGkbXPb9ULFKj+JvcuJtmOlFrg 2TMMg7LYdQ0CPvXYZv7ucvSm1H2w7t6kqMqiJEtdoq7PZYFLnP27PmpTFydle42qkasmlThQLbo 6UEAb20EqzuFlbpHQgVTMV9aIB1I93HiKYTupwfk6smGxP3E X-Google-Smtp-Source: AGHT+IGccZgOJLORtXOMA8FoHHhk3Vs+fThzqAecT9XzOzLOKltyG5QMhk3cYTHxosFWc0Sd5b9x1g== X-Received: by 2002:a17:90b:3e4f:b0:2ff:7b15:813b with SMTP id 98e67ed59e1d1-2ff7ce93a00mr40091027a91.17.1741849346958; Thu, 13 Mar 2025 00:02:26 -0700 (PDT) Received: from localhost ([157.82.205.237]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-225c68a4095sm6633235ad.66.2025.03.13.00.02.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Mar 2025 00:02:26 -0700 (PDT) From: Akihiko Odaki Date: Thu, 13 Mar 2025 16:01:13 +0900 Subject: [PATCH net-next v10 10/10] vhost/net: Support VIRTIO_NET_F_HASH_REPORT Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250313-rss-v10-10-3185d73a9af0@daynix.com> References: <20250313-rss-v10-0-3185d73a9af0@daynix.com> In-Reply-To: <20250313-rss-v10-0-3185d73a9af0@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 , Stephen Hemminger , gur.stavi@huawei.com, Lei Yang , Simon Horman , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 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. Signed-off-by: Akihiko Odaki Tested-by: Lei Yang --- drivers/vhost/net.c | 68 +++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index b9b9e9d40951..fc5b43e43a06 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -73,6 +73,7 @@ enum { VHOST_NET_FEATURES = VHOST_FEATURES | (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) | (1ULL << VIRTIO_NET_F_MRG_RXBUF) | + (1ULL << VIRTIO_NET_F_HASH_REPORT) | (1ULL << VIRTIO_F_ACCESS_PLATFORM) | (1ULL << VIRTIO_F_RING_RESET) }; @@ -1097,10 +1098,6 @@ static void handle_rx(struct vhost_net *net) .msg_controllen = 0, .msg_flags = MSG_DONTWAIT, }; - struct virtio_net_hdr hdr = { - .flags = 0, - .gso_type = VIRTIO_NET_HDR_GSO_NONE - }; size_t total_len = 0; int err, mergeable; s16 headcount; @@ -1174,11 +1171,15 @@ static void handle_rx(struct vhost_net *net) /* We don't need to be notified again. */ iov_iter_init(&msg.msg_iter, ITER_DEST, vq->iov, in, vhost_len); fixup = msg.msg_iter; - if (unlikely((vhost_hlen))) { - /* We will supply the header ourselves - * TODO: support TSO. - */ - iov_iter_advance(&msg.msg_iter, vhost_hlen); + /* + * Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR + * TODO: support TSO. + */ + if (unlikely(vhost_hlen) && + iov_iter_zero(vhost_hlen, &msg.msg_iter) != vhost_hlen) { + vq_err(vq, "Unable to write vnet_hdr at addr %p\n", + vq->iov->iov_base); + goto out; } err = sock->ops->recvmsg(sock, &msg, sock_len, MSG_DONTWAIT | MSG_TRUNC); @@ -1191,30 +1192,24 @@ static void handle_rx(struct vhost_net *net) vhost_discard_vq_desc(vq, headcount); continue; } - /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */ - if (unlikely(vhost_hlen)) { - if (copy_to_iter(&hdr, sizeof(hdr), - &fixup) != sizeof(hdr)) { - vq_err(vq, "Unable to write vnet_hdr " - "at addr %p\n", vq->iov->iov_base); - goto out; - } - } else { - /* Header came from socket; we'll need to patch - * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF - */ - iov_iter_advance(&fixup, sizeof(hdr)); - } /* TODO: Should check and handle checksum. */ + /* + * We'll need to patch ->num_buffers over if + * VIRTIO_NET_F_MRG_RXBUF or VIRTIO_F_VERSION_1 + */ num_buffers = cpu_to_vhost16(vq, headcount); - if (likely(set_num_buffers) && - copy_to_iter(&num_buffers, sizeof num_buffers, - &fixup) != sizeof num_buffers) { - vq_err(vq, "Failed num_buffers write"); - vhost_discard_vq_desc(vq, headcount); - goto out; + if (likely(set_num_buffers)) { + iov_iter_advance(&fixup, offsetof(struct virtio_net_hdr_v1, num_buffers)); + + if (copy_to_iter(&num_buffers, sizeof(num_buffers), + &fixup) != sizeof(num_buffers)) { + vq_err(vq, "Failed num_buffers write"); + vhost_discard_vq_desc(vq, headcount); + goto out; + } } + nvq->done_idx += headcount; if (nvq->done_idx > VHOST_NET_BATCH) vhost_net_signal_used(nvq); @@ -1607,10 +1602,13 @@ static int vhost_net_set_features(struct vhost_net *n, u64 features) size_t vhost_hlen, sock_hlen, hdr_len; int i; - hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | - (1ULL << VIRTIO_F_VERSION_1))) ? - sizeof(struct virtio_net_hdr_mrg_rxbuf) : - sizeof(struct virtio_net_hdr); + if (features & (1ULL << VIRTIO_NET_F_HASH_REPORT)) + hdr_len = sizeof(struct virtio_net_hdr_v1_hash); + else if (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | + (1ULL << VIRTIO_F_VERSION_1))) + hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); + else + hdr_len = sizeof(struct virtio_net_hdr); if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) { /* vhost provides vnet_hdr */ vhost_hlen = hdr_len; @@ -1691,6 +1689,10 @@ static long vhost_net_ioctl(struct file *f, unsigned int ioctl, return -EFAULT; if (features & ~VHOST_NET_FEATURES) return -EOPNOTSUPP; + if ((features & ((1ULL << VIRTIO_F_VERSION_1) | + (1ULL << VIRTIO_NET_F_HASH_REPORT))) == + (1ULL << VIRTIO_NET_F_HASH_REPORT)) + return -EINVAL; return vhost_net_set_features(n, features); case VHOST_GET_BACKEND_FEATURES: features = VHOST_NET_BACKEND_FEATURES;