Skip to contentSkip to conversation

selftests: add selftest for UDP SO_PEEK_OFF support

PATCH Superseded Jason Xing 3 participants 6 messages Sep 1, 2024 – Sep 2, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason Xing

author
#0 lore raw

PATCH 1/1 selftests: add selftest for UDP SO_PEEK_OFF support

From: Jason Xing <[email protected]>

Add the SO_PEEK_OFF selftest for UDP. In this patch, I mainly do three things:
1. rename tcp_so_peek_off.c
2. adjust for UDP protocol
3. add selftests into it

---

---
 tools/testing/selftests/net/Makefile          |  2 +-
 .../{tcp_so_peek_off.c => sk_so_peek_off.c}   | 91 +++++++++++--------
 2 files changed, 56 insertions(+), 37 deletions(-)
 rename tools/testing/selftests/net/{tcp_so_peek_off.c => sk_so_peek_off.c} (58%)
tools/testing/selftests/net/Makefile +1 -1
index 1179e3261bef..d5029f978aa9 100644
@@ -80,7 +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_GEN_PROGS += sk_so_peek_off
 TEST_PROGS += test_ingress_egress_chaining.sh
 TEST_GEN_PROGS += so_incoming_cpu
 TEST_PROGS += sctp_vrf.sh
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +55 -36

213 lines not on this page · raw message

Signature · 1 line
2.37.3
#1 lore raw
Quoted 23 lines · Jason Xing

From: Jason Xing <[email protected]>

Add the SO_PEEK_OFF selftest for UDP. In this patch, I mainly do three things:
1. rename tcp_so_peek_off.c
2. adjust for UDP protocol
3. add selftests into it

---

---
 tools/testing/selftests/net/Makefile          |  2 +-
 .../{tcp_so_peek_off.c => sk_so_peek_off.c}   | 91 +++++++++++--------
 2 files changed, 56 insertions(+), 37 deletions(-)
 rename tools/testing/selftests/net/{tcp_so_peek_off.c => sk_so_peek_off.c} (58%)
tools/testing/selftests/net/Makefile +0 -0
index 1179e3261bef..d5029f978aa9 100644

tools/testing/selftests/net/Makefile TEST_PROGS += io_uring_zerocopy_tx.sh · line 83

Quoted 5 lines
tools/testing/selftests/net/Makefile +1 -1
@@ -80,7 +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_GEN_PROGS += sk_so_peek_off

Should we also add sk_so_peek_off to gitignore?

author
#2 lore raw
Quoted 20 lines · Stanislav Fomichev
Quoted 18 lines · Jason Xing

From: Jason Xing <[email protected]>

Add the SO_PEEK_OFF selftest for UDP. In this patch, I mainly do three things:
1. rename tcp_so_peek_off.c
2. adjust for UDP protocol
3. add selftests into it

---

---
 tools/testing/selftests/net/Makefile          |  2 +-
 .../{tcp_so_peek_off.c => sk_so_peek_off.c}   | 91 +++++++++++--------
 2 files changed, 56 insertions(+), 37 deletions(-)
 rename tools/testing/selftests/net/{tcp_so_peek_off.c => sk_so_peek_off.c} (58%)
Quoted 12 lines

tools/testing/selftests/net/Makefile TEST_PROGS += io_uring_zerocopy_tx.sh · line 83

Quoted 10 lines
tools/testing/selftests/net/Makefile +1 -1
index 1179e3261bef..d5029f978aa9 100644
@@ -80,7 +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_GEN_PROGS += sk_so_peek_off

Should we also add sk_so_peek_off to gitignore?

Good catch. Will update it in the next version :)

Thanks,
Jason

Reviewed-by on v1 1/1
#3 lore raw
In reply to Jason Xing
Quoted 10 lines · Jason Xing

From: Jason Xing <[email protected]>

Add the SO_PEEK_OFF selftest for UDP. In this patch, I mainly do three things:
1. rename tcp_so_peek_off.c
2. adjust for UDP protocol
3. add selftests into it

A few minor comments. Nothing important.

Subject to Stan's point about .gitignore:

tools/testing/selftests/net/sk_so_peek_off.c line 28

Quoted 10 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +3 -2
-int tcp_peek_offset_probe(sa_family_t af)
+int sk_peek_offset_probe(sa_family_t af, int proto)
 {
+	int type = (proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM);
 	int optv = 0;
 	int ret = 0;
 	int s;
 
-	s = socket(af, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
+	s = socket(af, type, proto);

Removing the SOCK_CLOEXEC because not relevant to this single thread process, I suppose?

Not important, but no need for proto, can just be 0.

Quoted 40 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +5 -4
 	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));
+			printf("%s does not support SO_PEEK_OFF\n", afstr(af, proto));
 		close(s);
 	}
 	return ret;
 }
 
-static void tcp_peek_offset_set(int s, int offset)
+static void sk_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)
+static int sk_peek_offset_get(int s)
 {
 	int offset;
 	socklen_t len = sizeof(offset);
@@ -50,8 +54,9 @@ static int tcp_peek_offset_get(int s)
 	return offset;
 }
 
-static int tcp_peek_offset_test(sa_family_t af)
+static int sk_peek_offset_test(sa_family_t af, int proto)
 {
+	int type = (proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM);
 	union {
 		struct sockaddr sa;
 		struct sockaddr_in a4;
@@ -62,13 +67,13 @@ static int tcp_peek_offset_test(sa_family_t af)
 	int recv_sock = 0;
 	int offset = 0;
 	ssize_t len;

tools/testing/selftests/net/sk_so_peek_off.c static int tcp_peek_offset_test(sa_family_t af) · line 76

Quoted 10 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +3 -3
-	char buf;
+	char buf[2];
 
 	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);
+	s[0] = recv_sock = socket(af, type, proto);
+	s[1] = socket(af, type, proto);

Same

Quoted 7 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +0 -0
 	if (s[0] < 0 || s[1] < 0) {
 		ksft_perror("Temporary socket creation failed\n");
@@ -82,76 +87,78 @@ static int tcp_peek_offset_test(sa_family_t af)
 		ksft_perror("Temporary socket getsockname() failed\n");
 		goto out;
 	}

tools/testing/selftests/net/sk_so_peek_off.c static int tcp_peek_offset_test(sa_family_t af) · line 97

Quoted 10 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +2 -2
-	if (listen(s[0], 0) < 0) {
+	if (proto == IPPROTO_TCP && listen(s[0], 0) < 0) {
 		ksft_perror("Temporary socket listen() failed\n");
 		goto out;
 	}
-	if (connect(s[1], &a.sa, sizeof(a)) >= 0 || errno != EINPROGRESS) {
+	if (connect(s[1], &a.sa, sizeof(a))) {
 		ksft_perror("Temporary socket connect() failed\n");
 		goto out;
 	}

Changed due to the removal of SOCK_NONBLOCK above. Definitely simplifies the test.

Just note that error test is == -1 or < 0, also for consistency with the rest of the file.

Quoted 10 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +6 -4
-	recv_sock = accept(s[0], NULL, NULL);
-	if (recv_sock <= 0) {
-		ksft_perror("Temporary socket accept() failed\n");
-		goto out;
+	if (proto == IPPROTO_TCP) {
+		recv_sock = accept(s[0], NULL, NULL);
+		if (recv_sock <= 0) {
+			ksft_perror("Temporary socket accept() failed\n");
+			goto out;
+		}
author
#4 lore raw
Quoted 10 lines · Willem de Bruijn
Quoted 8 lines · Jason Xing

From: Jason Xing <[email protected]>

Add the SO_PEEK_OFF selftest for UDP. In this patch, I mainly do three things:
1. rename tcp_so_peek_off.c
2. adjust for UDP protocol
3. add selftests into it

Quoted 8 lines
Quoted 2 lines

A few minor comments. Nothing important.

Subject to Stan's point about .gitignore:

Thanks for your review!

Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +2 -1
-int tcp_peek_offset_probe(sa_family_t af)
+int sk_peek_offset_probe(sa_family_t af, int proto)
 {
+     int type = (proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM);
      int optv = 0;
      int ret = 0;
      int s;
Quoted 5 lines

tools/testing/selftests/net/sk_so_peek_off.c line 28

Quoted 2 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +1 -1
-     s = socket(af, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
+     s = socket(af, type, proto);

Removing the SOCK_CLOEXEC because not relevant to this single thread process, I suppose?

Yep. We don't need this one.

Quoted 2 lines

Not important, but no need for proto, can just be 0.

You're right. I wonder if it is better if we explicitly pass the proto here? I would like not to touch it here.

Quoted 47 lines
Quoted 47 lines
      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
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +6 -5
-                     printf("%s does not support SO_PEEK_OFF\n", afstr(af));
+                     printf("%s does not support SO_PEEK_OFF\n", afstr(af, proto));
              close(s);
      }
      return ret;
 }
 
-static void tcp_peek_offset_set(int s, int offset)
+static void sk_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)
+static int sk_peek_offset_get(int s)
 {
      int offset;
      socklen_t len = sizeof(offset);
@@ -50,8 +54,9 @@ static int tcp_peek_offset_get(int s)
      return offset;
 }
 
-static int tcp_peek_offset_test(sa_family_t af)
+static int sk_peek_offset_test(sa_family_t af, int proto)
 {
+     int type = (proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM);
      union {
              struct sockaddr sa;
              struct sockaddr_in a4;
@@ -62,13 +67,13 @@ static int tcp_peek_offset_test(sa_family_t af)
      int recv_sock = 0;
      int offset = 0;
      ssize_t len;
-     char buf;
+     char buf[2];
 
      memset(&a, 0, sizeof(a));
      a.sa.sa_family = af;
 
Quoted 6 lines

tools/testing/selftests/net/sk_so_peek_off.c static int tcp_peek_offset_test(sa_family_t af) · line 76

Quoted 4 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +2 -2
-     s[0] = socket(af, SOCK_STREAM, IPPROTO_TCP);
-     s[1] = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
+     s[0] = recv_sock = socket(af, type, proto);
+     s[1] = socket(af, type, proto);

Same

I think we don't need this one, either.
As we can see, there are already some existing test files without the SOCK_NONBLOCK flag.

Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +1 -1
      if (s[0] < 0 || s[1] < 0) {
              ksft_perror("Temporary socket creation failed\n");
@@ -82,76 +87,78 @@ static int tcp_peek_offset_test(sa_family_t af)
              ksft_perror("Temporary socket getsockname() failed\n");
              goto out;
      }
-     if (listen(s[0], 0) < 0) {
+     if (proto == IPPROTO_TCP && listen(s[0], 0) < 0) {
              ksft_perror("Temporary socket listen() failed\n");
              goto out;
      }
Quoted 8 lines

tools/testing/selftests/net/sk_so_peek_off.c static int tcp_peek_offset_test(sa_family_t af) · line 97

Quoted 5 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +1 -1
-     if (connect(s[1], &a.sa, sizeof(a)) >= 0 || errno != EINPROGRESS) {
+     if (connect(s[1], &a.sa, sizeof(a))) {
              ksft_perror("Temporary socket connect() failed\n");
              goto out;
      }

Changed due to the removal of SOCK_NONBLOCK above. Definitely simplifies the test.

Yep.

Quoted 3 lines

Just note that error test is == -1 or < 0, also for consistency with the rest of the file.

I will add "< 0" here as you said.

Thanks,
Jason

#5 lore raw
Quoted 38 lines · Jason Xing
Quoted 10 lines · Willem de Bruijn
Quoted 8 lines · Jason Xing

From: Jason Xing <[email protected]>

Add the SO_PEEK_OFF selftest for UDP. In this patch, I mainly do three things:
1. rename tcp_so_peek_off.c
2. adjust for UDP protocol
3. add selftests into it

Quoted 8 lines
Quoted 2 lines

A few minor comments. Nothing important.

Subject to Stan's point about .gitignore:

Thanks for your review!

Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +2 -1
-int tcp_peek_offset_probe(sa_family_t af)
+int sk_peek_offset_probe(sa_family_t af, int proto)
 {
+     int type = (proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM);
      int optv = 0;
      int ret = 0;
      int s;
Quoted 5 lines

tools/testing/selftests/net/sk_so_peek_off.c line 28

Quoted 2 lines
tools/testing/selftests/net/sk_so_peek_off.c renamed from tools/testing/selftests/net/tcp_so_peek_off.c +1 -1
-     s = socket(af, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
+     s = socket(af, type, proto);

Removing the SOCK_CLOEXEC because not relevant to this single thread process, I suppose?

Quoted 7 lines

Yep. We don't need this one.

Quoted 2 lines

Not important, but no need for proto, can just be 0.

You're right. I wonder if it is better if we explicitly pass the proto here? I would like not to touch it here.

It's not better or worse. Just not needed. So either way.