Skip to content

Commit d147357

Browse files
Alexei StarovoitovMartin KaFai Lau
authored andcommitted
libbpf: Allow specifying 64-bit integers in map BTF.
__uint() macro that is used to specify map attributes like: __uint(type, BPF_MAP_TYPE_ARRAY); __uint(map_flags, BPF_F_MMAPABLE); It is limited to 32-bit, since BTF_KIND_ARRAY has u32 "number of elements" field in "struct btf_array". Introduce __ulong() macro that allows specifying values bigger than 32-bit. In map definition "map_extra" is the only u64 field, so far. Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent cf2c2e4 commit d147357

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

tools/lib/bpf/bpf_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define __uint(name, val) int (*name)[val]
1414
#define __type(name, val) typeof(val) *name
1515
#define __array(name, val) typeof(val) *name[]
16+
#define __ulong(name, val) enum { ___bpf_concat(__unique_value, __COUNTER__) = val } name
1617

1718
/*
1819
* Helper macro to place programs, maps, license in

tools/lib/bpf/libbpf.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,46 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf,
23352335
return true;
23362336
}
23372337

2338+
static bool get_map_field_long(const char *map_name, const struct btf *btf,
2339+
const struct btf_member *m, __u64 *res)
2340+
{
2341+
const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
2342+
const char *name = btf__name_by_offset(btf, m->name_off);
2343+
2344+
if (btf_is_ptr(t)) {
2345+
__u32 res32;
2346+
bool ret;
2347+
2348+
ret = get_map_field_int(map_name, btf, m, &res32);
2349+
if (ret)
2350+
*res = (__u64)res32;
2351+
return ret;
2352+
}
2353+
2354+
if (!btf_is_enum(t) && !btf_is_enum64(t)) {
2355+
pr_warn("map '%s': attr '%s': expected ENUM or ENUM64, got %s.\n",
2356+
map_name, name, btf_kind_str(t));
2357+
return false;
2358+
}
2359+
2360+
if (btf_vlen(t) != 1) {
2361+
pr_warn("map '%s': attr '%s': invalid __ulong\n",
2362+
map_name, name);
2363+
return false;
2364+
}
2365+
2366+
if (btf_is_enum(t)) {
2367+
const struct btf_enum *e = btf_enum(t);
2368+
2369+
*res = e->val;
2370+
} else {
2371+
const struct btf_enum64 *e = btf_enum64(t);
2372+
2373+
*res = btf_enum64_value(e);
2374+
}
2375+
return true;
2376+
}
2377+
23382378
static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name)
23392379
{
23402380
int len;
@@ -2568,9 +2608,9 @@ int parse_btf_map_def(const char *map_name, struct btf *btf,
25682608
map_def->pinning = val;
25692609
map_def->parts |= MAP_DEF_PINNING;
25702610
} else if (strcmp(name, "map_extra") == 0) {
2571-
__u32 map_extra;
2611+
__u64 map_extra;
25722612

2573-
if (!get_map_field_int(map_name, btf, m, &map_extra))
2613+
if (!get_map_field_long(map_name, btf, m, &map_extra))
25742614
return -EINVAL;
25752615
map_def->map_extra = map_extra;
25762616
map_def->parts |= MAP_DEF_MAP_EXTRA;

0 commit comments

Comments
 (0)