selftests/bpf: Deduplicate write_sysctl() to test_progs.c
authorYiFei Zhu <zhuyifei@google.com>
Fri, 9 Sep 2022 00:49:40 +0000 (00:49 +0000)
committerMartin KaFai Lau <martin.lau@kernel.org>
Fri, 9 Sep 2022 17:40:45 +0000 (10:40 -0700)
This helper is needed in multiple tests. Instead of copying it over
and over, better to deduplicate this helper to test_progs.c.

test_progs.c is chosen over testing_helpers.c because of this helper's
use of CHECK / ASSERT_*, and the CHECK was modified to use ASSERT_*
so it does not rely on a duration variable.

Suggested-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: YiFei Zhu <zhuyifei@google.com>
Link: https://lore.kernel.org/r/9b4fc9a27bd52f771b657b4c4090fc8d61f3a6b5.1662682323.git.zhuyifei@google.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
tools/testing/selftests/bpf/prog_tests/btf_skc_cls_ingress.c
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c
tools/testing/selftests/bpf/test_progs.c
tools/testing/selftests/bpf/test_progs.h

index 664ffc0..7a27703 100644 (file)
@@ -22,26 +22,6 @@ static __u32 duration;
 
 #define PROG_PIN_FILE "/sys/fs/bpf/btf_skc_cls_ingress"
 
-static int write_sysctl(const char *sysctl, const char *value)
-{
-       int fd, err, len;
-
-       fd = open(sysctl, O_WRONLY);
-       if (CHECK(fd == -1, "open sysctl", "open(%s): %s (%d)\n",
-                 sysctl, strerror(errno), errno))
-               return -1;
-
-       len = strlen(value);
-       err = write(fd, value, len);
-       close(fd);
-       if (CHECK(err != len, "write sysctl",
-                 "write(%s, %s, %d): err:%d %s (%d)\n",
-                 sysctl, value, len, err, strerror(errno), errno))
-               return -1;
-
-       return 0;
-}
-
 static int prepare_netns(void)
 {
        if (CHECK(unshare(CLONE_NEWNET), "create netns",
index 1fa7720..f24436d 100644 (file)
@@ -54,26 +54,6 @@ static int create_netns(void)
        return 0;
 }
 
-static int write_sysctl(const char *sysctl, const char *value)
-{
-       int fd, err, len;
-
-       fd = open(sysctl, O_WRONLY);
-       if (CHECK(fd == -1, "open sysctl", "open(%s): %s (%d)\n",
-                 sysctl, strerror(errno), errno))
-               return -1;
-
-       len = strlen(value);
-       err = write(fd, value, len);
-       close(fd);
-       if (CHECK(err != len, "write sysctl",
-                 "write(%s, %s): err:%d %s (%d)\n",
-                 sysctl, value, err, strerror(errno), errno))
-               return -1;
-
-       return 0;
-}
-
 static void print_hdr_stg(const struct hdr_stg *hdr_stg, const char *prefix)
 {
        fprintf(stderr, "%s{active:%u, resend_syn:%u, syncookie:%u, fastopen:%u}\n",
index 3561c97..0e9a47f 100644 (file)
@@ -943,6 +943,23 @@ int trigger_module_test_write(int write_sz)
        return 0;
 }
 
+int write_sysctl(const char *sysctl, const char *value)
+{
+       int fd, err, len;
+
+       fd = open(sysctl, O_WRONLY);
+       if (!ASSERT_NEQ(fd, -1, "open sysctl"))
+               return -1;
+
+       len = strlen(value);
+       err = write(fd, value, len);
+       close(fd);
+       if (!ASSERT_EQ(err, len, "write sysctl"))
+               return -1;
+
+       return 0;
+}
+
 #define MAX_BACKTRACE_SZ 128
 void crash_handler(int signum)
 {
index 5fe1365..b090996 100644 (file)
@@ -384,6 +384,7 @@ int extract_build_id(char *build_id, size_t size);
 int kern_sync_rcu(void);
 int trigger_module_test_read(int read_sz);
 int trigger_module_test_write(int write_sz);
+int write_sysctl(const char *sysctl, const char *value);
 
 #ifdef __x86_64__
 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"