root/tools/testing/selftests/bpf/test_section_names.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_prog_type_by_name
  2. test_attach_type_by_name
  3. run_test_case
  4. run_tests
  5. main

   1 // SPDX-License-Identifier: GPL-2.0
   2 // Copyright (c) 2018 Facebook
   3 
   4 #include <err.h>
   5 #include <bpf/libbpf.h>
   6 
   7 #include "bpf_util.h"
   8 
   9 struct sec_name_test {
  10         const char sec_name[32];
  11         struct {
  12                 int rc;
  13                 enum bpf_prog_type prog_type;
  14                 enum bpf_attach_type expected_attach_type;
  15         } expected_load;
  16         struct {
  17                 int rc;
  18                 enum bpf_attach_type attach_type;
  19         } expected_attach;
  20 };
  21 
  22 static struct sec_name_test tests[] = {
  23         {"InvAliD", {-EINVAL, 0, 0}, {-EINVAL, 0} },
  24         {"cgroup", {-EINVAL, 0, 0}, {-EINVAL, 0} },
  25         {"socket", {0, BPF_PROG_TYPE_SOCKET_FILTER, 0}, {-EINVAL, 0} },
  26         {"kprobe/", {0, BPF_PROG_TYPE_KPROBE, 0}, {-EINVAL, 0} },
  27         {"kretprobe/", {0, BPF_PROG_TYPE_KPROBE, 0}, {-EINVAL, 0} },
  28         {"classifier", {0, BPF_PROG_TYPE_SCHED_CLS, 0}, {-EINVAL, 0} },
  29         {"action", {0, BPF_PROG_TYPE_SCHED_ACT, 0}, {-EINVAL, 0} },
  30         {"tracepoint/", {0, BPF_PROG_TYPE_TRACEPOINT, 0}, {-EINVAL, 0} },
  31         {
  32                 "raw_tracepoint/",
  33                 {0, BPF_PROG_TYPE_RAW_TRACEPOINT, 0},
  34                 {-EINVAL, 0},
  35         },
  36         {"xdp", {0, BPF_PROG_TYPE_XDP, 0}, {-EINVAL, 0} },
  37         {"perf_event", {0, BPF_PROG_TYPE_PERF_EVENT, 0}, {-EINVAL, 0} },
  38         {"lwt_in", {0, BPF_PROG_TYPE_LWT_IN, 0}, {-EINVAL, 0} },
  39         {"lwt_out", {0, BPF_PROG_TYPE_LWT_OUT, 0}, {-EINVAL, 0} },
  40         {"lwt_xmit", {0, BPF_PROG_TYPE_LWT_XMIT, 0}, {-EINVAL, 0} },
  41         {"lwt_seg6local", {0, BPF_PROG_TYPE_LWT_SEG6LOCAL, 0}, {-EINVAL, 0} },
  42         {
  43                 "cgroup_skb/ingress",
  44                 {0, BPF_PROG_TYPE_CGROUP_SKB, 0},
  45                 {0, BPF_CGROUP_INET_INGRESS},
  46         },
  47         {
  48                 "cgroup_skb/egress",
  49                 {0, BPF_PROG_TYPE_CGROUP_SKB, 0},
  50                 {0, BPF_CGROUP_INET_EGRESS},
  51         },
  52         {"cgroup/skb", {0, BPF_PROG_TYPE_CGROUP_SKB, 0}, {-EINVAL, 0} },
  53         {
  54                 "cgroup/sock",
  55                 {0, BPF_PROG_TYPE_CGROUP_SOCK, 0},
  56                 {0, BPF_CGROUP_INET_SOCK_CREATE},
  57         },
  58         {
  59                 "cgroup/post_bind4",
  60                 {0, BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND},
  61                 {0, BPF_CGROUP_INET4_POST_BIND},
  62         },
  63         {
  64                 "cgroup/post_bind6",
  65                 {0, BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND},
  66                 {0, BPF_CGROUP_INET6_POST_BIND},
  67         },
  68         {
  69                 "cgroup/dev",
  70                 {0, BPF_PROG_TYPE_CGROUP_DEVICE, 0},
  71                 {0, BPF_CGROUP_DEVICE},
  72         },
  73         {"sockops", {0, BPF_PROG_TYPE_SOCK_OPS, 0}, {0, BPF_CGROUP_SOCK_OPS} },
  74         {
  75                 "sk_skb/stream_parser",
  76                 {0, BPF_PROG_TYPE_SK_SKB, 0},
  77                 {0, BPF_SK_SKB_STREAM_PARSER},
  78         },
  79         {
  80                 "sk_skb/stream_verdict",
  81                 {0, BPF_PROG_TYPE_SK_SKB, 0},
  82                 {0, BPF_SK_SKB_STREAM_VERDICT},
  83         },
  84         {"sk_skb", {0, BPF_PROG_TYPE_SK_SKB, 0}, {-EINVAL, 0} },
  85         {"sk_msg", {0, BPF_PROG_TYPE_SK_MSG, 0}, {0, BPF_SK_MSG_VERDICT} },
  86         {"lirc_mode2", {0, BPF_PROG_TYPE_LIRC_MODE2, 0}, {0, BPF_LIRC_MODE2} },
  87         {
  88                 "flow_dissector",
  89                 {0, BPF_PROG_TYPE_FLOW_DISSECTOR, 0},
  90                 {0, BPF_FLOW_DISSECTOR},
  91         },
  92         {
  93                 "cgroup/bind4",
  94                 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND},
  95                 {0, BPF_CGROUP_INET4_BIND},
  96         },
  97         {
  98                 "cgroup/bind6",
  99                 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND},
 100                 {0, BPF_CGROUP_INET6_BIND},
 101         },
 102         {
 103                 "cgroup/connect4",
 104                 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT},
 105                 {0, BPF_CGROUP_INET4_CONNECT},
 106         },
 107         {
 108                 "cgroup/connect6",
 109                 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT},
 110                 {0, BPF_CGROUP_INET6_CONNECT},
 111         },
 112         {
 113                 "cgroup/sendmsg4",
 114                 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG},
 115                 {0, BPF_CGROUP_UDP4_SENDMSG},
 116         },
 117         {
 118                 "cgroup/sendmsg6",
 119                 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG},
 120                 {0, BPF_CGROUP_UDP6_SENDMSG},
 121         },
 122         {
 123                 "cgroup/recvmsg4",
 124                 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG},
 125                 {0, BPF_CGROUP_UDP4_RECVMSG},
 126         },
 127         {
 128                 "cgroup/recvmsg6",
 129                 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG},
 130                 {0, BPF_CGROUP_UDP6_RECVMSG},
 131         },
 132         {
 133                 "cgroup/sysctl",
 134                 {0, BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_CGROUP_SYSCTL},
 135                 {0, BPF_CGROUP_SYSCTL},
 136         },
 137         {
 138                 "cgroup/getsockopt",
 139                 {0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT},
 140                 {0, BPF_CGROUP_GETSOCKOPT},
 141         },
 142         {
 143                 "cgroup/setsockopt",
 144                 {0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT},
 145                 {0, BPF_CGROUP_SETSOCKOPT},
 146         },
 147 };
 148 
 149 static int test_prog_type_by_name(const struct sec_name_test *test)
 150 {
 151         enum bpf_attach_type expected_attach_type;
 152         enum bpf_prog_type prog_type;
 153         int rc;
 154 
 155         rc = libbpf_prog_type_by_name(test->sec_name, &prog_type,
 156                                       &expected_attach_type);
 157 
 158         if (rc != test->expected_load.rc) {
 159                 warnx("prog: unexpected rc=%d for %s", rc, test->sec_name);
 160                 return -1;
 161         }
 162 
 163         if (rc)
 164                 return 0;
 165 
 166         if (prog_type != test->expected_load.prog_type) {
 167                 warnx("prog: unexpected prog_type=%d for %s", prog_type,
 168                       test->sec_name);
 169                 return -1;
 170         }
 171 
 172         if (expected_attach_type != test->expected_load.expected_attach_type) {
 173                 warnx("prog: unexpected expected_attach_type=%d for %s",
 174                       expected_attach_type, test->sec_name);
 175                 return -1;
 176         }
 177 
 178         return 0;
 179 }
 180 
 181 static int test_attach_type_by_name(const struct sec_name_test *test)
 182 {
 183         enum bpf_attach_type attach_type;
 184         int rc;
 185 
 186         rc = libbpf_attach_type_by_name(test->sec_name, &attach_type);
 187 
 188         if (rc != test->expected_attach.rc) {
 189                 warnx("attach: unexpected rc=%d for %s", rc, test->sec_name);
 190                 return -1;
 191         }
 192 
 193         if (rc)
 194                 return 0;
 195 
 196         if (attach_type != test->expected_attach.attach_type) {
 197                 warnx("attach: unexpected attach_type=%d for %s", attach_type,
 198                       test->sec_name);
 199                 return -1;
 200         }
 201 
 202         return 0;
 203 }
 204 
 205 static int run_test_case(const struct sec_name_test *test)
 206 {
 207         if (test_prog_type_by_name(test))
 208                 return -1;
 209         if (test_attach_type_by_name(test))
 210                 return -1;
 211         return 0;
 212 }
 213 
 214 static int run_tests(void)
 215 {
 216         int passes = 0;
 217         int fails = 0;
 218         int i;
 219 
 220         for (i = 0; i < ARRAY_SIZE(tests); ++i) {
 221                 if (run_test_case(&tests[i]))
 222                         ++fails;
 223                 else
 224                         ++passes;
 225         }
 226         printf("Summary: %d PASSED, %d FAILED\n", passes, fails);
 227         return fails ? -1 : 0;
 228 }
 229 
 230 int main(int argc, char **argv)
 231 {
 232         return run_tests();
 233 }

/* [<][>][^][v][top][bottom][index][help] */