From: Geliang Tang <[email protected]>

Some tests, such as the MPTCP bpf tests, require send_recv_data helper
to run in nonblock mode.

This patch adds nonblock support for send_recv_data(). Check if it is
currently in nonblock mode, and if so, ignore EWOULDBLOCK to continue
sending and receiving.

Signed-off-by: Geliang Tang <[email protected]>
---
 tools/testing/selftests/bpf/network_helpers.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/network_helpers.c 
b/tools/testing/selftests/bpf/network_helpers.c
index 55d41508fe1f..08a204828ded 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -565,6 +565,7 @@ struct send_recv_arg {
 static void *send_recv_server(void *arg)
 {
        struct send_recv_arg *a = (struct send_recv_arg *)arg;
+       int flags = fcntl(a->fd, F_GETFL);
        ssize_t nr_sent = 0, bytes = 0;
        char batch[1500];
        int err = 0, fd;
@@ -588,6 +589,8 @@ static void *send_recv_server(void *arg)
                if (nr_sent == -1 && errno == EINTR)
                        continue;
                if (nr_sent == -1) {
+                       if (flags & O_NONBLOCK && errno == EWOULDBLOCK)
+                               continue;
                        err = -errno;
                        break;
                }
@@ -609,6 +612,7 @@ static void *send_recv_server(void *arg)
 
 int send_recv_data(int lfd, int fd, uint32_t total_bytes)
 {
+       int flags = fcntl(lfd, F_GETFL);
        ssize_t nr_recv = 0, bytes = 0;
        struct send_recv_arg arg = {
                .fd     = lfd,
@@ -632,8 +636,11 @@ int send_recv_data(int lfd, int fd, uint32_t total_bytes)
                               MIN(total_bytes - bytes, sizeof(batch)), 0);
                if (nr_recv == -1 && errno == EINTR)
                        continue;
-               if (nr_recv == -1)
+               if (nr_recv == -1) {
+                       if (flags & O_NONBLOCK && errno == EWOULDBLOCK)
+                               continue;
                        break;
+               }
                bytes += nr_recv;
        }
 
-- 
2.40.1


Reply via email to