1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2020 Facebook */ 3 4 #include <test_progs.h> 5 #include "test_module_attach.skel.h" 6 7 static int duration; 8 9 static int trigger_module_test_read(int read_sz) 10 { 11 int fd, err; 12 13 fd = open("/sys/kernel/bpf_testmod", O_RDONLY); 14 err = -errno; 15 if (CHECK(fd < 0, "testmod_file_open", "failed: %d\n", err)) 16 return err; 17 18 read(fd, NULL, read_sz); 19 close(fd); 20 21 return 0; 22 } 23 24 static int trigger_module_test_write(int write_sz) 25 { 26 int fd, err; 27 char *buf = malloc(write_sz); 28 29 if (!buf) 30 return -ENOMEM; 31 32 memset(buf, 'a', write_sz); 33 buf[write_sz-1] = '\0'; 34 35 fd = open("/sys/kernel/bpf_testmod", O_WRONLY); 36 err = -errno; 37 if (CHECK(fd < 0, "testmod_file_open", "failed: %d\n", err)) { 38 free(buf); 39 return err; 40 } 41 42 write(fd, buf, write_sz); 43 close(fd); 44 free(buf); 45 return 0; 46 } 47 48 void test_module_attach(void) 49 { 50 const int READ_SZ = 456; 51 const int WRITE_SZ = 457; 52 struct test_module_attach* skel; 53 struct test_module_attach__bss *bss; 54 int err; 55 56 skel = test_module_attach__open(); 57 if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) 58 return; 59 60 err = bpf_program__set_attach_target(skel->progs.handle_fentry_manual, 61 0, "bpf_testmod_test_read"); 62 ASSERT_OK(err, "set_attach_target"); 63 64 err = test_module_attach__load(skel); 65 if (CHECK(err, "skel_load", "failed to load skeleton\n")) 66 return; 67 68 bss = skel->bss; 69 70 err = test_module_attach__attach(skel); 71 if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) 72 goto cleanup; 73 74 /* trigger tracepoint */ 75 ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read"); 76 ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write"); 77 78 ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp"); 79 ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare"); 80 ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf"); 81 ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry"); 82 ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual"); 83 ASSERT_EQ(bss->fexit_read_sz, READ_SZ, "fexit"); 84 ASSERT_EQ(bss->fexit_ret, -EIO, "fexit_tet"); 85 ASSERT_EQ(bss->fmod_ret_read_sz, READ_SZ, "fmod_ret"); 86 87 cleanup: 88 test_module_attach__destroy(skel); 89 } 90