Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2020 Facebook */
0003 
0004 #include <sys/types.h>
0005 #include <bpf/bpf.h>
0006 #include <bpf/libbpf.h>
0007 #include "test_progs.h"
0008 #include "network_helpers.h"
0009 #include "test_sk_storage_trace_itself.skel.h"
0010 #include "test_sk_storage_tracing.skel.h"
0011 
0012 #define LO_ADDR6 "::1"
0013 #define TEST_COMM "test_progs"
0014 
0015 struct sk_stg {
0016     __u32 pid;
0017     __u32 last_notclose_state;
0018     char comm[16];
0019 };
0020 
0021 static struct test_sk_storage_tracing *skel;
0022 static __u32 duration;
0023 static pid_t my_pid;
0024 
0025 static int check_sk_stg(int sk_fd, __u32 expected_state)
0026 {
0027     struct sk_stg sk_stg;
0028     int err;
0029 
0030     err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_stg_map), &sk_fd,
0031                   &sk_stg);
0032     if (!ASSERT_OK(err, "map_lookup(sk_stg_map)"))
0033         return -1;
0034 
0035     if (!ASSERT_EQ(sk_stg.last_notclose_state, expected_state,
0036                "last_notclose_state"))
0037         return -1;
0038 
0039     if (!ASSERT_EQ(sk_stg.pid, my_pid, "pid"))
0040         return -1;
0041 
0042     if (!ASSERT_STREQ(sk_stg.comm, skel->bss->task_comm, "task_comm"))
0043         return -1;
0044 
0045     return 0;
0046 }
0047 
0048 static void do_test(void)
0049 {
0050     int listen_fd = -1, passive_fd = -1, active_fd = -1, value = 1, err;
0051     char abyte;
0052 
0053     listen_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
0054     if (CHECK(listen_fd == -1, "start_server",
0055           "listen_fd:%d errno:%d\n", listen_fd, errno))
0056         return;
0057 
0058     active_fd = connect_to_fd(listen_fd, 0);
0059     if (CHECK(active_fd == -1, "connect_to_fd", "active_fd:%d errno:%d\n",
0060           active_fd, errno))
0061         goto out;
0062 
0063     err = bpf_map_update_elem(bpf_map__fd(skel->maps.del_sk_stg_map),
0064                   &active_fd, &value, 0);
0065     if (!ASSERT_OK(err, "map_update(del_sk_stg_map)"))
0066         goto out;
0067 
0068     passive_fd = accept(listen_fd, NULL, 0);
0069     if (CHECK(passive_fd == -1, "accept", "passive_fd:%d errno:%d\n",
0070           passive_fd, errno))
0071         goto out;
0072 
0073     shutdown(active_fd, SHUT_WR);
0074     err = read(passive_fd, &abyte, 1);
0075     if (!ASSERT_OK(err, "read(passive_fd)"))
0076         goto out;
0077 
0078     shutdown(passive_fd, SHUT_WR);
0079     err = read(active_fd, &abyte, 1);
0080     if (!ASSERT_OK(err, "read(active_fd)"))
0081         goto out;
0082 
0083     err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.del_sk_stg_map),
0084                   &active_fd, &value);
0085     if (!ASSERT_ERR(err, "map_lookup(del_sk_stg_map)"))
0086         goto out;
0087 
0088     err = check_sk_stg(listen_fd, BPF_TCP_LISTEN);
0089     if (!ASSERT_OK(err, "listen_fd sk_stg"))
0090         goto out;
0091 
0092     err = check_sk_stg(active_fd, BPF_TCP_FIN_WAIT2);
0093     if (!ASSERT_OK(err, "active_fd sk_stg"))
0094         goto out;
0095 
0096     err = check_sk_stg(passive_fd, BPF_TCP_LAST_ACK);
0097     ASSERT_OK(err, "passive_fd sk_stg");
0098 
0099 out:
0100     if (active_fd != -1)
0101         close(active_fd);
0102     if (passive_fd != -1)
0103         close(passive_fd);
0104     if (listen_fd != -1)
0105         close(listen_fd);
0106 }
0107 
0108 void serial_test_sk_storage_tracing(void)
0109 {
0110     struct test_sk_storage_trace_itself *skel_itself;
0111     int err;
0112 
0113     my_pid = getpid();
0114 
0115     skel_itself = test_sk_storage_trace_itself__open_and_load();
0116 
0117     if (!ASSERT_NULL(skel_itself, "test_sk_storage_trace_itself")) {
0118         test_sk_storage_trace_itself__destroy(skel_itself);
0119         return;
0120     }
0121 
0122     skel = test_sk_storage_tracing__open_and_load();
0123     if (!ASSERT_OK_PTR(skel, "test_sk_storage_tracing"))
0124         return;
0125 
0126     err = test_sk_storage_tracing__attach(skel);
0127     if (!ASSERT_OK(err, "test_sk_storage_tracing__attach")) {
0128         test_sk_storage_tracing__destroy(skel);
0129         return;
0130     }
0131 
0132     do_test();
0133 
0134     test_sk_storage_tracing__destroy(skel);
0135 }