tools/bpf: refactor test_btf pretty printing for multiple map value formats

The test_btf pretty print is refactored in order to easily
support multiple map value formats. The next patch will
add __int128 type tests which needs macro guard __SIZEOF_INT128__.
There is no functionality change with this patch.

Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
This commit is contained in:
Yonghong Song 2019-01-15 17:07:49 -08:00 committed by Daniel Borkmann
parent a80eba20ed
commit ce6ec47a10

View file

@ -18,6 +18,7 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <assert.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <bpf/btf.h> #include <bpf/btf.h>
@ -134,6 +135,11 @@ static struct btf_header hdr_tmpl = {
.hdr_len = sizeof(struct btf_header), .hdr_len = sizeof(struct btf_header),
}; };
/* several different mapv kinds(types) supported by pprint */
enum pprint_mapv_kind_t {
PPRINT_MAPV_KIND_BASIC = 0,
};
struct btf_raw_test { struct btf_raw_test {
const char *descr; const char *descr;
const char *str_sec; const char *str_sec;
@ -156,6 +162,7 @@ struct btf_raw_test {
int type_off_delta; int type_off_delta;
int str_off_delta; int str_off_delta;
int str_len_delta; int str_len_delta;
enum pprint_mapv_kind_t mapv_kind;
}; };
#define BTF_STR_SEC(str) \ #define BTF_STR_SEC(str) \
@ -3880,26 +3887,71 @@ static struct btf_pprint_test_meta {
}; };
static size_t get_pprint_mapv_size(enum pprint_mapv_kind_t mapv_kind)
{
if (mapv_kind == PPRINT_MAPV_KIND_BASIC)
return sizeof(struct pprint_mapv);
static void set_pprint_mapv(struct pprint_mapv *v, uint32_t i, assert(0);
}
static void set_pprint_mapv(enum pprint_mapv_kind_t mapv_kind,
void *mapv, uint32_t i,
int num_cpus, int rounded_value_size) int num_cpus, int rounded_value_size)
{ {
int cpu; int cpu;
for (cpu = 0; cpu < num_cpus; cpu++) { if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
v->ui32 = i + cpu; struct pprint_mapv *v = mapv;
v->si32 = -i;
v->unused_bits2a = 3; for (cpu = 0; cpu < num_cpus; cpu++) {
v->bits28 = i; v->ui32 = i + cpu;
v->unused_bits2b = 3; v->si32 = -i;
v->ui64 = i; v->unused_bits2a = 3;
v->aenum = i & 0x03; v->bits28 = i;
v->ui32b = 4; v->unused_bits2b = 3;
v->bits2c = 1; v->ui64 = i;
v = (void *)v + rounded_value_size; v->aenum = i & 0x03;
v->ui32b = 4;
v->bits2c = 1;
v = (void *)v + rounded_value_size;
}
} }
} }
ssize_t get_pprint_expected_line(enum pprint_mapv_kind_t mapv_kind,
char *expected_line, ssize_t line_size,
bool percpu_map, unsigned int next_key,
int cpu, void *mapv)
{
ssize_t nexpected_line = -1;
if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
struct pprint_mapv *v = mapv;
nexpected_line = snprintf(expected_line, line_size,
"%s%u: {%u,0,%d,0x%x,0x%x,0x%x,"
"{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s,"
"%u,0x%x}\n",
percpu_map ? "\tcpu" : "",
percpu_map ? cpu : next_key,
v->ui32, v->si32,
v->unused_bits2a,
v->bits28,
v->unused_bits2b,
v->ui64,
v->ui8a[0], v->ui8a[1],
v->ui8a[2], v->ui8a[3],
v->ui8a[4], v->ui8a[5],
v->ui8a[6], v->ui8a[7],
pprint_enum_str[v->aenum],
v->ui32b,
v->bits2c);
}
return nexpected_line;
}
static int check_line(const char *expected_line, int nexpected_line, static int check_line(const char *expected_line, int nexpected_line,
int expected_line_len, const char *line) int expected_line_len, const char *line)
{ {
@ -3921,10 +3973,10 @@ static int check_line(const char *expected_line, int nexpected_line,
static int do_test_pprint(int test_num) static int do_test_pprint(int test_num)
{ {
const struct btf_raw_test *test = &pprint_test_template[test_num]; const struct btf_raw_test *test = &pprint_test_template[test_num];
enum pprint_mapv_kind_t mapv_kind = test->mapv_kind;
struct bpf_create_map_attr create_attr = {}; struct bpf_create_map_attr create_attr = {};
bool ordered_map, lossless_map, percpu_map; bool ordered_map, lossless_map, percpu_map;
int err, ret, num_cpus, rounded_value_size; int err, ret, num_cpus, rounded_value_size;
struct pprint_mapv *mapv = NULL;
unsigned int key, nr_read_elems; unsigned int key, nr_read_elems;
int map_fd = -1, btf_fd = -1; int map_fd = -1, btf_fd = -1;
unsigned int raw_btf_size; unsigned int raw_btf_size;
@ -3933,6 +3985,7 @@ static int do_test_pprint(int test_num)
char pin_path[255]; char pin_path[255];
size_t line_len = 0; size_t line_len = 0;
char *line = NULL; char *line = NULL;
void *mapv = NULL;
uint8_t *raw_btf; uint8_t *raw_btf;
ssize_t nread; ssize_t nread;
@ -3985,7 +4038,7 @@ static int do_test_pprint(int test_num)
percpu_map = test->percpu_map; percpu_map = test->percpu_map;
num_cpus = percpu_map ? bpf_num_possible_cpus() : 1; num_cpus = percpu_map ? bpf_num_possible_cpus() : 1;
rounded_value_size = round_up(sizeof(struct pprint_mapv), 8); rounded_value_size = round_up(get_pprint_mapv_size(mapv_kind), 8);
mapv = calloc(num_cpus, rounded_value_size); mapv = calloc(num_cpus, rounded_value_size);
if (CHECK(!mapv, "mapv allocation failure")) { if (CHECK(!mapv, "mapv allocation failure")) {
err = -1; err = -1;
@ -3993,7 +4046,7 @@ static int do_test_pprint(int test_num)
} }
for (key = 0; key < test->max_entries; key++) { for (key = 0; key < test->max_entries; key++) {
set_pprint_mapv(mapv, key, num_cpus, rounded_value_size); set_pprint_mapv(mapv_kind, mapv, key, num_cpus, rounded_value_size);
bpf_map_update_elem(map_fd, &key, mapv, 0); bpf_map_update_elem(map_fd, &key, mapv, 0);
} }
@ -4017,13 +4070,13 @@ static int do_test_pprint(int test_num)
ordered_map = test->ordered_map; ordered_map = test->ordered_map;
lossless_map = test->lossless_map; lossless_map = test->lossless_map;
do { do {
struct pprint_mapv *cmapv;
ssize_t nexpected_line; ssize_t nexpected_line;
unsigned int next_key; unsigned int next_key;
void *cmapv;
int cpu; int cpu;
next_key = ordered_map ? nr_read_elems : atoi(line); next_key = ordered_map ? nr_read_elems : atoi(line);
set_pprint_mapv(mapv, next_key, num_cpus, rounded_value_size); set_pprint_mapv(mapv_kind, mapv, next_key, num_cpus, rounded_value_size);
cmapv = mapv; cmapv = mapv;
for (cpu = 0; cpu < num_cpus; cpu++) { for (cpu = 0; cpu < num_cpus; cpu++) {
@ -4056,31 +4109,16 @@ static int do_test_pprint(int test_num)
break; break;
} }
nexpected_line = snprintf(expected_line, sizeof(expected_line), nexpected_line = get_pprint_expected_line(mapv_kind, expected_line,
"%s%u: {%u,0,%d,0x%x,0x%x,0x%x," sizeof(expected_line),
"{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s," percpu_map, next_key,
"%u,0x%x}\n", cpu, cmapv);
percpu_map ? "\tcpu" : "",
percpu_map ? cpu : next_key,
cmapv->ui32, cmapv->si32,
cmapv->unused_bits2a,
cmapv->bits28,
cmapv->unused_bits2b,
cmapv->ui64,
cmapv->ui8a[0], cmapv->ui8a[1],
cmapv->ui8a[2], cmapv->ui8a[3],
cmapv->ui8a[4], cmapv->ui8a[5],
cmapv->ui8a[6], cmapv->ui8a[7],
pprint_enum_str[cmapv->aenum],
cmapv->ui32b,
cmapv->bits2c);
err = check_line(expected_line, nexpected_line, err = check_line(expected_line, nexpected_line,
sizeof(expected_line), line); sizeof(expected_line), line);
if (err == -1) if (err == -1)
goto done; goto done;
cmapv = (void *)cmapv + rounded_value_size; cmapv = cmapv + rounded_value_size;
} }
if (percpu_map) { if (percpu_map) {