Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <test_progs.h>
0003 #include "udp_limit.skel.h"
0004 
0005 #include <sys/types.h>
0006 #include <sys/socket.h>
0007 
0008 static int duration;
0009 
0010 void test_udp_limit(void)
0011 {
0012     struct udp_limit *skel;
0013     int fd1 = -1, fd2 = -1;
0014     int cgroup_fd;
0015 
0016     cgroup_fd = test__join_cgroup("/udp_limit");
0017     if (CHECK(cgroup_fd < 0, "cg-join", "errno %d", errno))
0018         return;
0019 
0020     skel = udp_limit__open_and_load();
0021     if (CHECK(!skel, "skel-load", "errno %d", errno))
0022         goto close_cgroup_fd;
0023 
0024     skel->links.sock = bpf_program__attach_cgroup(skel->progs.sock, cgroup_fd);
0025     if (!ASSERT_OK_PTR(skel->links.sock, "cg_attach_sock"))
0026         goto close_skeleton;
0027     skel->links.sock_release = bpf_program__attach_cgroup(skel->progs.sock_release, cgroup_fd);
0028     if (!ASSERT_OK_PTR(skel->links.sock_release, "cg_attach_sock_release"))
0029         goto close_skeleton;
0030 
0031     /* BPF program enforces a single UDP socket per cgroup,
0032      * verify that.
0033      */
0034     fd1 = socket(AF_INET, SOCK_DGRAM, 0);
0035     if (CHECK(fd1 < 0, "fd1", "errno %d", errno))
0036         goto close_skeleton;
0037 
0038     fd2 = socket(AF_INET, SOCK_DGRAM, 0);
0039     if (CHECK(fd2 >= 0, "fd2", "errno %d", errno))
0040         goto close_skeleton;
0041 
0042     /* We can reopen again after close. */
0043     close(fd1);
0044     fd1 = -1;
0045 
0046     fd1 = socket(AF_INET, SOCK_DGRAM, 0);
0047     if (CHECK(fd1 < 0, "fd1-again", "errno %d", errno))
0048         goto close_skeleton;
0049 
0050     /* Make sure the program was invoked the expected
0051      * number of times:
0052      * - open fd1           - BPF_CGROUP_INET_SOCK_CREATE
0053      * - attempt to openfd2 - BPF_CGROUP_INET_SOCK_CREATE
0054      * - close fd1          - BPF_CGROUP_INET_SOCK_RELEASE
0055      * - open fd1 again     - BPF_CGROUP_INET_SOCK_CREATE
0056      */
0057     if (CHECK(skel->bss->invocations != 4, "bss-invocations",
0058           "invocations=%d", skel->bss->invocations))
0059         goto close_skeleton;
0060 
0061     /* We should still have a single socket in use */
0062     if (CHECK(skel->bss->in_use != 1, "bss-in_use",
0063           "in_use=%d", skel->bss->in_use))
0064         goto close_skeleton;
0065 
0066 close_skeleton:
0067     if (fd1 >= 0)
0068         close(fd1);
0069     if (fd2 >= 0)
0070         close(fd2);
0071     udp_limit__destroy(skel);
0072 close_cgroup_fd:
0073     close(cgroup_fd);
0074 }