bpf: update BPF_MAP_CREATE decoding

Implement decoding of map_flags and inner_map_fd fields of union bpf_attr
for BPF_MAP_CREATE command introduced by linux kernel commits
v4.6-rc1~91^2~108^2~6 and v4.12-rc1~64^3~373^2~2, respectively.

* configure.ac: Check for inner_map_fd member of union bpf_attr
instead of max_entries.
* xlat/bpf_map_flags.in: New file.
* bpf.c: Include "xlat/bpf_map_flags.h".
(decode_BPF_MAP_CREATE): Add map_flags and inner_map_fd fields
to the structure, print them.
* tests/bpf.c: Update macro guards of BPF_MAP_CREATE decoder test.
(init_BPF_MAP_CREATE_first, print_BPF_MAP_CREATE_attr): Update expected
output.
(init_BPF_MAP_CREATE_attr): Initialize map_flags and inner_map_fd
fields, update offset.
This commit is contained in:
Дмитрий Левин 2017-07-27 00:44:31 +00:00
parent c024f1a60f
commit 269e7d2a2c
4 changed files with 19 additions and 11 deletions

6
bpf.c
View File

@ -35,6 +35,7 @@
#include "xlat/bpf_commands.h"
#include "xlat/bpf_map_types.h"
#include "xlat/bpf_map_flags.h"
#include "xlat/bpf_prog_types.h"
#include "xlat/bpf_prog_flags.h"
#include "xlat/bpf_map_update_elem_flags.h"
@ -88,7 +89,8 @@ decode_attr_extra_data(struct tcb *const tcp,
DEF_BPF_CMD_DECODER(BPF_MAP_CREATE)
{
struct {
uint32_t map_type, key_size, value_size, max_entries;
uint32_t map_type, key_size, value_size, max_entries,
map_flags, inner_map_fd;
} attr = {};
const unsigned int len = size < sizeof(attr) ? size : sizeof(attr);
@ -99,6 +101,8 @@ DEF_BPF_CMD_DECODER(BPF_MAP_CREATE)
PRINT_FIELD_U(", ", attr, key_size);
PRINT_FIELD_U(", ", attr, value_size);
PRINT_FIELD_U(", ", attr, max_entries);
PRINT_FIELD_FLAGS(", ", attr, map_flags, bpf_map_flags, "BPF_F_???");
PRINT_FIELD_FD(", ", attr, inner_map_fd, tcp);
decode_attr_extra_data(tcp, data, size, sizeof(attr));
tprints("}");

View File

@ -431,7 +431,7 @@ AC_CHECK_HEADERS([linux/bpf.h], [
st_CHECK_UNION_BPF_ATTR([attach_flags])
st_CHECK_UNION_BPF_ATTR([bpf_fd])
st_CHECK_UNION_BPF_ATTR([flags])
st_CHECK_UNION_BPF_ATTR([max_entries])
st_CHECK_UNION_BPF_ATTR([inner_map_fd])
st_CHECK_UNION_BPF_ATTR([prog_flags])
])

View File

@ -34,7 +34,7 @@
&& (defined HAVE_UNION_BPF_ATTR_ATTACH_FLAGS \
|| defined HAVE_UNION_BPF_ATTR_BPF_FD \
|| defined HAVE_UNION_BPF_ATTR_FLAGS \
|| defined HAVE_UNION_BPF_ATTR_MAX_ENTRIES \
|| defined HAVE_UNION_BPF_ATTR_INNER_MAP_FD \
|| defined HAVE_UNION_BPF_ATTR_PROG_FLAGS)
# include <stddef.h>
@ -183,7 +183,7 @@ sys_bpf(kernel_ulong_t cmd, kernel_ulong_t attr, kernel_ulong_t size)
init_ ## cmd_ ## _attr, print_ ## cmd_ ## _attr) \
/* End of TEST_BPF definition. */
# ifdef HAVE_UNION_BPF_ATTR_MAX_ENTRIES
# ifdef HAVE_UNION_BPF_ATTR_INNER_MAP_FD
static unsigned int
init_BPF_MAP_CREATE_first(const unsigned long eop)
@ -199,8 +199,8 @@ init_BPF_MAP_CREATE_first(const unsigned long eop)
static void
print_BPF_MAP_CREATE_first(const unsigned long addr)
{
printf("map_type=BPF_MAP_TYPE_ARRAY, key_size=0"
", value_size=0, max_entries=0");
printf("map_type=BPF_MAP_TYPE_ARRAY, key_size=0, value_size=0"
", max_entries=0, map_flags=0, inner_map_fd=0");
}
static unsigned int
@ -210,10 +210,12 @@ init_BPF_MAP_CREATE_attr(const unsigned long eop)
.map_type = 1,
.key_size = 4,
.value_size = 8,
.max_entries = 256
.max_entries = 256,
.map_flags = 1,
.inner_map_fd = -1
};
static const unsigned int offset =
offsetofend(union bpf_attr, max_entries);
offsetofend(union bpf_attr, inner_map_fd);
const unsigned long addr = eop - offset;
memcpy((void *) addr, &attr, offset);
@ -224,10 +226,11 @@ static void
print_BPF_MAP_CREATE_attr(const unsigned long addr)
{
printf("map_type=BPF_MAP_TYPE_HASH, key_size=4"
", value_size=8, max_entries=256");
", value_size=8, max_entries=256"
", map_flags=BPF_F_NO_PREALLOC, inner_map_fd=-1");
}
# endif /* HAVE_UNION_BPF_ATTR_MAX_ENTRIES */
# endif /* HAVE_UNION_BPF_ATTR_INNER_MAP_FD */
# ifdef HAVE_UNION_BPF_ATTR_FLAGS
@ -544,7 +547,7 @@ main(void)
page_size = get_page_size();
end_of_page = (unsigned long) tail_alloc(1) + 1;
# ifdef HAVE_UNION_BPF_ATTR_MAX_ENTRIES
# ifdef HAVE_UNION_BPF_ATTR_INNER_MAP_FD
TEST_BPF(BPF_MAP_CREATE);
# endif

1
xlat/bpf_map_flags.in Normal file
View File

@ -0,0 +1 @@
BPF_F_NO_PREALLOC 1