0001
0002
0003
0004 #include <ctype.h>
0005 #include <test_progs.h>
0006 #include <bpf/btf.h>
0007
0008
0009
0010
0011 static void uppercase(char *s)
0012 {
0013 for (; *s != '\0'; s++)
0014 *s = toupper(*s);
0015 }
0016
0017
0018
0019
0020
0021 static void test_libbpf_bpf_attach_type_str(void)
0022 {
0023 struct btf *btf;
0024 const struct btf_type *t;
0025 const struct btf_enum *e;
0026 int i, n, id;
0027
0028 btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
0029 if (!ASSERT_OK_PTR(btf, "btf_parse"))
0030 return;
0031
0032
0033 id = btf__find_by_name_kind(btf, "bpf_attach_type", BTF_KIND_ENUM);
0034 if (!ASSERT_GT(id, 0, "bpf_attach_type_id"))
0035 goto cleanup;
0036 t = btf__type_by_id(btf, id);
0037 e = btf_enum(t);
0038 n = btf_vlen(t);
0039 for (i = 0; i < n; e++, i++) {
0040 enum bpf_attach_type attach_type = (enum bpf_attach_type)e->val;
0041 const char *attach_type_name;
0042 const char *attach_type_str;
0043 char buf[256];
0044
0045 if (attach_type == __MAX_BPF_ATTACH_TYPE)
0046 continue;
0047
0048 attach_type_name = btf__str_by_offset(btf, e->name_off);
0049 attach_type_str = libbpf_bpf_attach_type_str(attach_type);
0050 ASSERT_OK_PTR(attach_type_str, attach_type_name);
0051
0052 snprintf(buf, sizeof(buf), "BPF_%s", attach_type_str);
0053 uppercase(buf);
0054
0055 ASSERT_STREQ(buf, attach_type_name, "exp_str_value");
0056 }
0057
0058 cleanup:
0059 btf__free(btf);
0060 }
0061
0062
0063
0064
0065
0066 static void test_libbpf_bpf_link_type_str(void)
0067 {
0068 struct btf *btf;
0069 const struct btf_type *t;
0070 const struct btf_enum *e;
0071 int i, n, id;
0072
0073 btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
0074 if (!ASSERT_OK_PTR(btf, "btf_parse"))
0075 return;
0076
0077
0078 id = btf__find_by_name_kind(btf, "bpf_link_type", BTF_KIND_ENUM);
0079 if (!ASSERT_GT(id, 0, "bpf_link_type_id"))
0080 goto cleanup;
0081 t = btf__type_by_id(btf, id);
0082 e = btf_enum(t);
0083 n = btf_vlen(t);
0084 for (i = 0; i < n; e++, i++) {
0085 enum bpf_link_type link_type = (enum bpf_link_type)e->val;
0086 const char *link_type_name;
0087 const char *link_type_str;
0088 char buf[256];
0089
0090 if (link_type == MAX_BPF_LINK_TYPE)
0091 continue;
0092
0093 link_type_name = btf__str_by_offset(btf, e->name_off);
0094 link_type_str = libbpf_bpf_link_type_str(link_type);
0095 ASSERT_OK_PTR(link_type_str, link_type_name);
0096
0097 snprintf(buf, sizeof(buf), "BPF_LINK_TYPE_%s", link_type_str);
0098 uppercase(buf);
0099
0100 ASSERT_STREQ(buf, link_type_name, "exp_str_value");
0101 }
0102
0103 cleanup:
0104 btf__free(btf);
0105 }
0106
0107
0108
0109
0110
0111 static void test_libbpf_bpf_map_type_str(void)
0112 {
0113 struct btf *btf;
0114 const struct btf_type *t;
0115 const struct btf_enum *e;
0116 int i, n, id;
0117
0118 btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
0119 if (!ASSERT_OK_PTR(btf, "btf_parse"))
0120 return;
0121
0122
0123 id = btf__find_by_name_kind(btf, "bpf_map_type", BTF_KIND_ENUM);
0124 if (!ASSERT_GT(id, 0, "bpf_map_type_id"))
0125 goto cleanup;
0126 t = btf__type_by_id(btf, id);
0127 e = btf_enum(t);
0128 n = btf_vlen(t);
0129 for (i = 0; i < n; e++, i++) {
0130 enum bpf_map_type map_type = (enum bpf_map_type)e->val;
0131 const char *map_type_name;
0132 const char *map_type_str;
0133 char buf[256];
0134
0135 map_type_name = btf__str_by_offset(btf, e->name_off);
0136 map_type_str = libbpf_bpf_map_type_str(map_type);
0137 ASSERT_OK_PTR(map_type_str, map_type_name);
0138
0139 snprintf(buf, sizeof(buf), "BPF_MAP_TYPE_%s", map_type_str);
0140 uppercase(buf);
0141
0142 ASSERT_STREQ(buf, map_type_name, "exp_str_value");
0143 }
0144
0145 cleanup:
0146 btf__free(btf);
0147 }
0148
0149
0150
0151
0152
0153 static void test_libbpf_bpf_prog_type_str(void)
0154 {
0155 struct btf *btf;
0156 const struct btf_type *t;
0157 const struct btf_enum *e;
0158 int i, n, id;
0159
0160 btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
0161 if (!ASSERT_OK_PTR(btf, "btf_parse"))
0162 return;
0163
0164
0165 id = btf__find_by_name_kind(btf, "bpf_prog_type", BTF_KIND_ENUM);
0166 if (!ASSERT_GT(id, 0, "bpf_prog_type_id"))
0167 goto cleanup;
0168 t = btf__type_by_id(btf, id);
0169 e = btf_enum(t);
0170 n = btf_vlen(t);
0171 for (i = 0; i < n; e++, i++) {
0172 enum bpf_prog_type prog_type = (enum bpf_prog_type)e->val;
0173 const char *prog_type_name;
0174 const char *prog_type_str;
0175 char buf[256];
0176
0177 prog_type_name = btf__str_by_offset(btf, e->name_off);
0178 prog_type_str = libbpf_bpf_prog_type_str(prog_type);
0179 ASSERT_OK_PTR(prog_type_str, prog_type_name);
0180
0181 snprintf(buf, sizeof(buf), "BPF_PROG_TYPE_%s", prog_type_str);
0182 uppercase(buf);
0183
0184 ASSERT_STREQ(buf, prog_type_name, "exp_str_value");
0185 }
0186
0187 cleanup:
0188 btf__free(btf);
0189 }
0190
0191
0192
0193
0194 void test_libbpf_str(void)
0195 {
0196 if (test__start_subtest("bpf_attach_type_str"))
0197 test_libbpf_bpf_attach_type_str();
0198
0199 if (test__start_subtest("bpf_link_type_str"))
0200 test_libbpf_bpf_link_type_str();
0201
0202 if (test__start_subtest("bpf_map_type_str"))
0203 test_libbpf_bpf_map_type_str();
0204
0205 if (test__start_subtest("bpf_prog_type_str"))
0206 test_libbpf_bpf_prog_type_str();
0207 }