public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Petr Vorel <pvorel@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 2/2] BPF: Sanity check creating and updating maps
Date: Wed, 24 Jul 2019 11:18:51 +0200	[thread overview]
Message-ID: <20190724091851.GA4917@dell5510> (raw)
In-Reply-To: <20190724080328.16145-3-rpalethorpe@suse.com>

Hi Richie,

> Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>

LGTM with minor comments.

...
> diff --git a/testcases/kernel/syscalls/bpf/bpf_map01.c b/testcases/kernel/syscalls/bpf/bpf_map01.c
...
> +#include <limits.h>
> +#include <string.h>
> +
> +#include "config.h"
> +#include "tst_test.h"
> +#include "lapi/bpf.h"
> +
> +#define KEY_SZ 8
> +#define VAL_SZ 1024
> +
> +struct map_type {
> +	uint32_t id;
> +	char *name;
> +};
> +
> +static const struct map_type map_types[] = {
> +	{BPF_MAP_TYPE_HASH, "hash"},
> +	{BPF_MAP_TYPE_ARRAY, "array"}
> +};
> +
> +static void *key;
> +static void *val0;
> +static void *val1;
> +
> +static void setup(void)
> +{
> +	key = SAFE_MALLOC(KEY_SZ);
> +	memset(key, 0, (size_t) KEY_SZ);
> +	val0 = SAFE_MALLOC(VAL_SZ);
> +	val1 = SAFE_MALLOC(VAL_SZ);
> +	memset(val1, 0, (size_t) VAL_SZ);
> +}
> +
> +void run(unsigned int n)
> +{
> +	int fd, i;
> +	union bpf_attr attr;
> +	memset(&attr, 0, sizeof(attr));
> +	attr.map_type = map_types[n].id;
> +	attr.key_size = n == 0 ? KEY_SZ : 4;
Out of curiosity why 4? As whole test is working for KEY_SZ >= 1 (but for second
test it's needed to be 4).

> +	attr.value_size = VAL_SZ;
> +	attr.max_entries = 1;
+ we usually use struct test_case + description at the test start, but I guess
it's ok like this.

> +
> +	if ((fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr))) == -1) {
> +		tst_brk(TFAIL | TERRNO, "Failed to create %s map",
> +			map_types[n].name);
> +	} else {
> +		tst_res(TPASS, "Created %s map", map_types[n].name);
> +	}
> +
> +	if (n == 0)
> +		memcpy(key, "12345678", KEY_SZ);
> +	else
> +		memset(key, 0, 4);
> +
> +	memset(&attr, 0, sizeof(attr));
> +	attr.map_fd = fd;
> +	attr.key = ptr_to_u64(key);
> +	attr.value = ptr_to_u64(val1);
> +
> +	TEST(bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)));
> +	if (n == 0) {
> +		if (TST_RET != -1 || TST_ERR != ENOENT) {
> +			tst_res(TFAIL | TTERRNO,
> +				"Empty hash map lookup should fail with ENOENT");
> +		} else {
> +			tst_res(TPASS | TTERRNO, "Empty hash map lookup");
> +		}
> +	} else if (TST_RET != -1) {
> +		for (i = 0;;) {
> +			if (*(char *) val1 != 0) {
> +				tst_res(TFAIL,
> +					"Preallocated array map val not zero");
I guess here is missing break.

> +			} else if (++i >= VAL_SZ) {
> +				tst_res(TPASS,
> +					"Preallocated array map lookup");
> +				break;
> +			}
> +		}
> +	} else {
> +		tst_res(TFAIL | TERRNO, "Prellocated array map lookup");
> +	}
...

Kind regards,
Petr

  reply	other threads:[~2019-07-24  9:18 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-24  8:03 [LTP] [PATCH 0/2] [RFC] BPF testing Richard Palethorpe
2019-07-24  8:03 ` [LTP] [PATCH 1/2] Essential headers for BPF map creation Richard Palethorpe
2019-07-24  9:27   ` Petr Vorel
2019-07-24  9:55     ` Richard Palethorpe
2019-07-24  8:03 ` [LTP] [PATCH 2/2] BPF: Sanity check creating and updating maps Richard Palethorpe
2019-07-24  9:18   ` Petr Vorel [this message]
2019-07-24 12:15     ` Richard Palethorpe
2019-07-24  9:30 ` [LTP] [PATCH 0/2] [RFC] BPF testing Petr Vorel
2019-07-25 14:23 ` Cyril Hrubis
2019-07-29 10:02   ` Richard Palethorpe
2019-07-30 13:44 ` [LTP] [PATCH v2 1/4] BPF: Essential headers for map creation Richard Palethorpe
2019-07-30 13:44   ` [LTP] [PATCH v2 2/4] BPF: Sanity check creating and updating maps Richard Palethorpe
2019-07-30 13:44   ` [LTP] [PATCH v2 3/4] BPF: Essential headers for a basic program Richard Palethorpe
2019-07-30 13:44   ` [LTP] [PATCH v2 4/4] BPF: Sanity check creating a program Richard Palethorpe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190724091851.GA4917@dell5510 \
    --to=pvorel@suse.cz \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox