From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=MM9/AeDH; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTP id 214FB5A004E for ; Fri, 23 Aug 2024 09:47:03 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724399222; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Okn0W7Rg5J0WfizJ2TUxBUJNy8XQFd2DDDb5hJ5i4V0=; b=MM9/AeDHnFbeOT0HciTzRIH0JN7nog6X6mZC25EiyXBAk+74uYQI0Vkyy3j/K+rlmMTWmd rF1w7LjUtnIr4a9DzOJ9XsNyppqTUDX1c8m7N1hk9K34HGi4k3egxgSc034fe87qZMzo1t wZFwRsLMdoniEXV4pezCpajP9roXhi4= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-536-xgXn16CyM82qBxaL1e4_sQ-1; Fri, 23 Aug 2024 03:43:56 -0400 X-MC-Unique: xgXn16CyM82qBxaL1e4_sQ-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-428ea5b1479so13083605e9.0 for ; Fri, 23 Aug 2024 00:43:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724399034; x=1725003834; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Okn0W7Rg5J0WfizJ2TUxBUJNy8XQFd2DDDb5hJ5i4V0=; b=ZsqrJ2Hq88ahOxeRq5rHjxhYmpMcrBGlkkx2TKghZFea1Hgt44LYDvH4UiB4DeTBcU 1kAZiQooHpZyi3+GBz+jI7xlTnqvzBrH1SrgufcbnZ1ojv5veDTxQMPwGeD4w24Nz1oM Sm+5pWby4JNcGRawrT8eVljHjadQmzRYZx/Gljba+j/2guhZuBlgHxcqlUaguAA65fD9 govQ5dARUSqrrE/wEroF0b1ABbnjnN9jHsQb2QjsNXNwUFK37eHSJWnGy2Kv9aOLiFB6 s0L7qrD10bGDsQyza84FmmngG/VhfQ+sGb75bp+7VZNOaJd4nSpzvnQwkSZgDTUupOaZ B/QQ== X-Gm-Message-State: AOJu0Yz6ZeyASnZSrZgp41PbqWwn5kl/OjlUWI1C8sEttNQ/MaQPk9n5 yGJOxMDCPtdbZfPG4XTsVFFXdekxjyg6KH0mZbplbnMVu2qSBBjQhVGECIAFYprkYGCXcqPnd64 2iJDJfd/JSfyFJ+/TwdgM3i9yxYOnGcX2cSAD00X8z6KeoyJynonHk1Q49Yq6eWCsTj0WOf2WpU /xXGpwVedC+JbJizg3FAgTc1ThCKRihnuOUtc= X-Received: by 2002:a5d:66c1:0:b0:368:74e0:2068 with SMTP id ffacd0b85a97d-37311865155mr806311f8f.33.1724399033663; Fri, 23 Aug 2024 00:43:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEuwAljuNse4yyzWQNJLWEUqkbPVmfX7H6zgJd32aS+r1dWHS//2+vtHdvdT8T8tSf0rpeENA== X-Received: by 2002:a5d:66c1:0:b0:368:74e0:2068 with SMTP id ffacd0b85a97d-37311865155mr806290f8f.33.1724399033133; Fri, 23 Aug 2024 00:43:53 -0700 (PDT) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [176.103.220.4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3730810ff96sm3506013f8f.14.2024.08.23.00.43.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2024 00:43:51 -0700 (PDT) Date: Fri, 23 Aug 2024 09:43:49 +0200 From: Stefano Brivio To: Jon Maloy Subject: Re: [PATCH v2 2/2] selftests: add selftest for tcp SO_PEEK_OFF support Message-ID: <20240823094349.1f0df608@elisabeth> In-Reply-To: <20240823013637.72719-3-jmaloy@redhat.com> References: <20240823013637.72719-1-jmaloy@redhat.com> <20240823013637.72719-3-jmaloy@redhat.com> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.41; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: NLZ4H5UNSF7MDYIJOUVMHPAIYSNKPAFX X-Message-ID-Hash: NLZ4H5UNSF7MDYIJOUVMHPAIYSNKPAFX X-MailFrom: sbrivio@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, lvivier@redhat.com, dgibson@redhat.com X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Thu, 22 Aug 2024 21:36:37 -0400 Jon Maloy wrote: > We add a selftest to check that the new feature added in > commit 05ea491641d3 ("tcp: add support for SO_PEEK_OFF socket option") > works correctly. > > Signed-off-by: Jon Maloy Reviewed-by: Stefano Brivio Tested-by: Stefano Brivio With or without fixing two nits below: > --- > v2: Fixed Makefile and improved code after suggestion from Stefano Brivio > --- > tools/testing/selftests/net/Makefile | 1 + > tools/testing/selftests/net/tcp_so_peek_off.c | 181 ++++++++++++++++++ > 2 files changed, 182 insertions(+) > create mode 100644 tools/testing/selftests/net/tcp_so_peek_off.c > > diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile > index 8eaffd7a641c..1179e3261bef 100644 > --- a/tools/testing/selftests/net/Makefile > +++ b/tools/testing/selftests/net/Makefile > @@ -80,6 +80,7 @@ TEST_PROGS += io_uring_zerocopy_tx.sh > TEST_GEN_FILES += bind_bhash > TEST_GEN_PROGS += sk_bind_sendto_listen > TEST_GEN_PROGS += sk_connect_zero_addr > +TEST_GEN_PROGS += tcp_so_peek_off > TEST_PROGS += test_ingress_egress_chaining.sh > TEST_GEN_PROGS += so_incoming_cpu > TEST_PROGS += sctp_vrf.sh > diff --git a/tools/testing/selftests/net/tcp_so_peek_off.c b/tools/testing/selftests/net/tcp_so_peek_off.c > new file mode 100644 > index 000000000000..8379ea02e3d7 > --- /dev/null > +++ b/tools/testing/selftests/net/tcp_so_peek_off.c > @@ -0,0 +1,181 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include "../kselftest.h" > + > +static char *afstr(int af) > +{ > + return af == AF_INET ? "TCP/IPv4" : "TCP/IPv6"; > +} > + > +int tcp_peek_offset_probe(sa_family_t af) > +{ > + int optv = 0; > + int ret = 0; > + int s; > + > + s = socket(af, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP); > + if (s < 0) { > + ksft_perror("Temporary TCP socket creation failed"); > + } else { > + if (!setsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &optv, sizeof(int))) > + ret = 1; > + else > + printf("%s does not support SO_PEEK_OFF\n", afstr(af)); You could use ksft_print_msg() for this one. But the naming of those functions is pretty redundant (what else do you want to do with a 'msg' other than printing it, I wonder), so it might make the code unnecessarily unreadable. > + close(s); > + } > + return ret; > +} > + > +static void tcp_peek_offset_set(int s, int offset) > +{ > + if (setsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &offset, sizeof(offset))) > + ksft_perror("Failed to set SO_PEEK_OFF value\n"); > +} > + > +static int tcp_peek_offset_get(int s) > +{ > + int offset; > + socklen_t len = sizeof(offset); > + > + if (getsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &offset, &len)) > + ksft_perror("Failed to get SO_PEEK_OFF value\n"); > + return offset; > +} > + > +static int tcp_peek_offset_test(sa_family_t af) > +{ > + union { > + struct sockaddr sa; > + struct sockaddr_in a4; > + struct sockaddr_in6 a6; > + } a; > + int res = 0; > + int s[2] = {0, 0}; > + int recv_sock = 0; > + int offset = 0; > + ssize_t len; > + char buf; > + > + memset(&a, 0, sizeof(a)); > + a.sa.sa_family = af; > + > + s[0] = socket(af, SOCK_STREAM, IPPROTO_TCP); > + s[1] = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP); > + > + if (s[0] < 0 || s[1] < 0) { > + ksft_perror("Temporary probe socket creation failed\n"); > + goto out; > + } > + if (bind(s[0], &a.sa, sizeof(a)) < 0) { > + ksft_perror("Temporary probe socket bind() failed\n"); > + goto out; > + } > + if (getsockname(s[0], &a.sa, &((socklen_t) { sizeof(a) })) < 0) { > + ksft_perror("Temporary probe socket getsockname() failed\n"); > + goto out; > + } > + if (listen(s[0], 0) < 0) { > + ksft_perror("Temporary probe socket listen() failed\n"); > + goto out; > + } > + if (connect(s[1], &a.sa, sizeof(a)) >= 0 || errno != EINPROGRESS) { > + ksft_perror("Temporary probe socket connect() failed\n"); > + goto out; > + } > + recv_sock = accept(s[0], NULL, NULL); > + if (recv_sock <= 0) { > + ksft_perror("Temporary probe socket accept() failed\n"); > + goto out; > + } > + > + /* Some basic tests of getting/setting offset */ > + offset = tcp_peek_offset_get(recv_sock); > + if (offset != -1) { > + ksft_perror("Initial value of socket offset not -1\n"); > + goto out; > + } > + tcp_peek_offset_set(recv_sock, 0); > + offset = tcp_peek_offset_get(recv_sock); > + if (offset != 0) { > + ksft_perror("Failed to set socket offset to 0\n"); > + goto out; > + } > + > + /* Transfer a message */ > + if (send(s[1], (char *)("ab"), 2, 0) <= 0 || errno != EINPROGRESS) { > + ksft_perror("Temporary probe socket send() failed\n"); > + goto out; > + } > + /* Read first byte */ > + len = recv(recv_sock, &buf, 1, MSG_PEEK); > + if (len != 1 || buf != 'a') { > + ksft_perror("Failed to read first byte of message\n"); > + goto out; > + } > + offset = tcp_peek_offset_get(recv_sock); > + if (offset != 1) { > + ksft_perror("Offset not forwarded correctly at first byte\n"); > + goto out; > + } > + /* Try to read beyond last byte */ > + len = recv(recv_sock, &buf, 2, MSG_PEEK); > + if (len != 1 || buf != 'b') { > + ksft_perror("Failed to read last byte of message\n"); > + goto out; > + } > + offset = tcp_peek_offset_get(recv_sock); > + if (offset != 2) { > + ksft_perror("Offset not forwarded correctly at last byte\n"); > + goto out; > + } > + /* Flush message */ > + len = recv(recv_sock, NULL, 2, MSG_TRUNC); > + if (len != 2) { > + ksft_perror("Failed to flush message\n"); > + goto out; > + } > + offset = tcp_peek_offset_get(recv_sock); > + if (offset != 0) { > + ksft_perror("Offset not reverted correctly after flush\n"); > + goto out; > + } > + > + printf("%s with MSG_PEEK_OFF works correctly\n", afstr(af)); Same here. > + res = 1; > +out: > + close(recv_sock); > + close(s[1]); > + close(s[0]); > + return res; > +} > + > +int main(void) > +{ > + int res4, res6; > + > + res4 = tcp_peek_offset_probe(AF_INET); > + res6 = tcp_peek_offset_probe(AF_INET6); > + > + if (!res4 && !res6) > + return KSFT_SKIP; > + > + if (res4) > + res4 = tcp_peek_offset_test(AF_INET); > + > + if (res6) > + res6 = tcp_peek_offset_test(AF_INET6); > + > + if (!res4 || !res6) > + return KSFT_FAIL; > + > + return KSFT_PASS; > +} > + -- Stefano