~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 #include <test_progs.h>
  4 
  5 #include "cgroup_helpers.h"
  6 
  7 #define PING_CMD        "ping -q -c1 -w1 127.0.0.1 > /dev/null"
  8 
  9 static char bpf_log_buf[BPF_LOG_BUF_SIZE];
 10 
 11 static int prog_load(void)
 12 {
 13         struct bpf_insn prog[] = {
 14                 BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */
 15                 BPF_EXIT_INSN(),
 16         };
 17         size_t insns_cnt = ARRAY_SIZE(prog);
 18 
 19         return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
 20                                prog, insns_cnt, "GPL", 0,
 21                                bpf_log_buf, BPF_LOG_BUF_SIZE);
 22 }
 23 
 24 void serial_test_cgroup_attach_autodetach(void)
 25 {
 26         __u32 duration = 0, prog_cnt = 4, attach_flags;
 27         int allow_prog[2] = {-1};
 28         __u32 prog_ids[2] = {0};
 29         void *ptr = NULL;
 30         int cg = 0, i;
 31         int attempts;
 32 
 33         for (i = 0; i < ARRAY_SIZE(allow_prog); i++) {
 34                 allow_prog[i] = prog_load();
 35                 if (CHECK(allow_prog[i] < 0, "prog_load",
 36                           "verifier output:\n%s\n-------\n", bpf_log_buf))
 37                         goto err;
 38         }
 39 
 40         if (CHECK_FAIL(setup_cgroup_environment()))
 41                 goto err;
 42 
 43         /* create a cgroup, attach two programs and remember their ids */
 44         cg = create_and_get_cgroup("/cg_autodetach");
 45         if (CHECK_FAIL(cg < 0))
 46                 goto err;
 47 
 48         if (CHECK_FAIL(join_cgroup("/cg_autodetach")))
 49                 goto err;
 50 
 51         for (i = 0; i < ARRAY_SIZE(allow_prog); i++)
 52                 if (CHECK(bpf_prog_attach(allow_prog[i], cg,
 53                                           BPF_CGROUP_INET_EGRESS,
 54                                           BPF_F_ALLOW_MULTI),
 55                           "prog_attach", "prog[%d], errno=%d\n", i, errno))
 56                         goto err;
 57 
 58         /* make sure that programs are attached and run some traffic */
 59         if (CHECK(bpf_prog_query(cg, BPF_CGROUP_INET_EGRESS, 0, &attach_flags,
 60                                  prog_ids, &prog_cnt),
 61                   "prog_query", "errno=%d\n", errno))
 62                 goto err;
 63         if (CHECK_FAIL(system(PING_CMD)))
 64                 goto err;
 65 
 66         /* allocate some memory (4Mb) to pin the original cgroup */
 67         ptr = malloc(4 * (1 << 20));
 68         if (CHECK_FAIL(!ptr))
 69                 goto err;
 70 
 71         /* close programs and cgroup fd */
 72         for (i = 0; i < ARRAY_SIZE(allow_prog); i++) {
 73                 close(allow_prog[i]);
 74                 allow_prog[i] = -1;
 75         }
 76 
 77         close(cg);
 78         cg = 0;
 79 
 80         /* leave the cgroup and remove it. don't detach programs */
 81         cleanup_cgroup_environment();
 82 
 83         /* wait for the asynchronous auto-detachment.
 84          * wait for no more than 5 sec and give up.
 85          */
 86         for (i = 0; i < ARRAY_SIZE(prog_ids); i++) {
 87                 for (attempts = 5; attempts >= 0; attempts--) {
 88                         int fd = bpf_prog_get_fd_by_id(prog_ids[i]);
 89 
 90                         if (fd < 0)
 91                                 break;
 92 
 93                         /* don't leave the fd open */
 94                         close(fd);
 95 
 96                         if (CHECK_FAIL(!attempts))
 97                                 goto err;
 98 
 99                         sleep(1);
100                 }
101         }
102 
103 err:
104         for (i = 0; i < ARRAY_SIZE(allow_prog); i++)
105                 if (allow_prog[i] >= 0)
106                         close(allow_prog[i]);
107         if (cg)
108                 close(cg);
109         free(ptr);
110         cleanup_cgroup_environment();
111 }
112 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php