Skip to content

Commit fb183e2

Browse files
etsalKernel Patches Daemon
authored andcommitted
libbpf: add stub for offset-related skeleton padding
Add a stub function for reporting in which offset within a mapping libbpf places the map's data. This will be used in a subsequent patch to support offsetting arena variables within the mapped region. Adjust skeleton generation to account for the new arena memory layout by adding padding corresponding to the offset into the arena map. Add a libbbpf API function to get the data offset within the map's mapping during skeleton generation. Signed-off-by: Emil Tsalapatis <[email protected]>
1 parent d621da9 commit fb183e2

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

tools/bpf/bpftool/gen.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ static int codegen_datasec_def(struct bpf_object *obj,
148148
struct btf *btf,
149149
struct btf_dump *d,
150150
const struct btf_type *sec,
151-
const char *obj_name)
151+
const char *obj_name,
152+
int var_off)
152153
{
153154
const char *sec_name = btf__name_by_offset(btf, sec->name_off);
154155
const struct btf_var_secinfo *sec_var = btf_var_secinfos(sec);
@@ -163,6 +164,17 @@ static int codegen_datasec_def(struct bpf_object *obj,
163164
strip_mods = true;
164165

165166
printf(" struct %s__%s {\n", obj_name, sec_ident);
167+
168+
/*
169+
* Arena variables may be placed in an offset within the section.
170+
* Represent this in the skeleton using a padding struct.
171+
*/
172+
if (var_off > 0) {
173+
printf("\t\tchar __pad%d[%d];\n",
174+
pad_cnt, var_off);
175+
pad_cnt++;
176+
}
177+
166178
for (i = 0; i < vlen; i++, sec_var++) {
167179
const struct btf_type *var = btf__type_by_id(btf, sec_var->type);
168180
const char *var_name = btf__name_by_offset(btf, var->name_off);
@@ -279,6 +291,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
279291
struct bpf_map *map;
280292
const struct btf_type *sec;
281293
char map_ident[256];
294+
int var_off;
282295
int err = 0;
283296

284297
d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL);
@@ -303,7 +316,13 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
303316
printf(" struct %s__%s {\n", obj_name, map_ident);
304317
printf(" } *%s;\n", map_ident);
305318
} else {
306-
err = codegen_datasec_def(obj, btf, d, sec, obj_name);
319+
var_off = bpf_map__data_offset(map);
320+
if (var_off < 0) {
321+
p_err("bpf_map__data_offset called on unmapped map\n");
322+
err = var_off;
323+
goto out;
324+
}
325+
err = codegen_datasec_def(obj, btf, d, sec, obj_name, var_off);
307326
if (err)
308327
goto out;
309328
}

tools/lib/bpf/libbpf.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10552,6 +10552,16 @@ const char *bpf_map__name(const struct bpf_map *map)
1055210552
return map->name;
1055310553
}
1055410554

10555+
int bpf_map__data_offset(const struct bpf_map *map)
10556+
{
10557+
if (!map->mmaped)
10558+
return -EINVAL;
10559+
10560+
/* No offsetting for now. */
10561+
return 0;
10562+
}
10563+
10564+
1055510565
enum bpf_map_type bpf_map__type(const struct bpf_map *map)
1055610566
{
1055710567
return map->def.type;

tools/lib/bpf/libbpf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,15 @@ LIBBPF_API int bpf_map__set_exclusive_program(struct bpf_map *map, struct bpf_pr
13141314
*/
13151315
LIBBPF_API struct bpf_program *bpf_map__exclusive_program(struct bpf_map *map);
13161316

1317+
/*
1318+
* @brief **bpf_map__data_offset** returns the offset of the map's data
1319+
* within the address mapping.
1320+
* @param BPF map whose variable offset we are looking into.
1321+
* @return the offset >= 0 of the map's contents within its mapping; negative
1322+
* error code, otherwise.
1323+
*/
1324+
LIBBPF_API int bpf_map__data_offset(const struct bpf_map *map);
1325+
13171326
struct bpf_xdp_set_link_opts {
13181327
size_t sz;
13191328
int old_fd;

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,5 @@ LIBBPF_1.7.0 {
451451
global:
452452
bpf_map__set_exclusive_program;
453453
bpf_map__exclusive_program;
454+
bpf_map__data_offset;
454455
} LIBBPF_1.6.0;

0 commit comments

Comments
 (0)