]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests: drv-net: Factor out ksft C helpers
authorJoe Damato <jdamato@fastly.com>
Thu, 24 Apr 2025 00:27:32 +0000 (00:27 +0000)
committerJakub Kicinski <kuba@kernel.org>
Fri, 25 Apr 2025 01:30:33 +0000 (18:30 -0700)
Factor ksft C helpers to a header so they can be used by other C-based
tests.

Signed-off-by: Joe Damato <jdamato@fastly.com>
Link: https://patch.msgid.link/20250424002746.16891-3-jdamato@fastly.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/selftests/drivers/net/ksft.h [new file with mode: 0644]
tools/testing/selftests/drivers/net/xdp_helper.c

diff --git a/tools/testing/selftests/drivers/net/ksft.h b/tools/testing/selftests/drivers/net/ksft.h
new file mode 100644 (file)
index 0000000..17dc34a
--- /dev/null
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#if !defined(__NET_KSFT_H__)
+#define __NET_KSFT_H__
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static inline void ksft_ready(void)
+{
+       const char msg[7] = "ready\n";
+       char *env_str;
+       int fd;
+
+       env_str = getenv("KSFT_READY_FD");
+       if (env_str) {
+               fd = atoi(env_str);
+               if (!fd) {
+                       fprintf(stderr, "invalid KSFT_READY_FD = '%s'\n",
+                               env_str);
+                       return;
+               }
+       } else {
+               fd = STDOUT_FILENO;
+       }
+
+       write(fd, msg, sizeof(msg));
+       if (fd != STDOUT_FILENO)
+               close(fd);
+}
+
+static inline void ksft_wait(void)
+{
+       char *env_str;
+       char byte;
+       int fd;
+
+       env_str = getenv("KSFT_WAIT_FD");
+       if (env_str) {
+               fd = atoi(env_str);
+               if (!fd) {
+                       fprintf(stderr, "invalid KSFT_WAIT_FD = '%s'\n",
+                               env_str);
+                       return;
+               }
+       } else {
+               /* Not running in KSFT env, wait for input from STDIN instead */
+               fd = STDIN_FILENO;
+       }
+
+       read(fd, &byte, sizeof(byte));
+       if (fd != STDIN_FILENO)
+               close(fd);
+}
+
+#endif
index aeed259141045bbf31db2c4cd4bdd0f2fb561f77..d5bb8ac33efa5f151c1955be4e2d679b0908be85 100644 (file)
 #include <net/if.h>
 #include <inttypes.h>
 
+#include "ksft.h"
+
 #define UMEM_SZ (1U << 16)
 #define NUM_DESC (UMEM_SZ / 2048)
 
-/* Move this to a common header when reused! */
-static void ksft_ready(void)
-{
-       const char msg[7] = "ready\n";
-       char *env_str;
-       int fd;
-
-       env_str = getenv("KSFT_READY_FD");
-       if (env_str) {
-               fd = atoi(env_str);
-               if (!fd) {
-                       fprintf(stderr, "invalid KSFT_READY_FD = '%s'\n",
-                               env_str);
-                       return;
-               }
-       } else {
-               fd = STDOUT_FILENO;
-       }
-
-       write(fd, msg, sizeof(msg));
-       if (fd != STDOUT_FILENO)
-               close(fd);
-}
-
-static void ksft_wait(void)
-{
-       char *env_str;
-       char byte;
-       int fd;
-
-       env_str = getenv("KSFT_WAIT_FD");
-       if (env_str) {
-               fd = atoi(env_str);
-               if (!fd) {
-                       fprintf(stderr, "invalid KSFT_WAIT_FD = '%s'\n",
-                               env_str);
-                       return;
-               }
-       } else {
-               /* Not running in KSFT env, wait for input from STDIN instead */
-               fd = STDIN_FILENO;
-       }
-
-       read(fd, &byte, sizeof(byte));
-       if (fd != STDIN_FILENO)
-               close(fd);
-}
 
 /* this is a simple helper program that creates an XDP socket and does the
  * minimum necessary to get bind() to succeed.