selftests/bpf: remove logic duplication in test_verifier.c
Subsystems (5)
PATCH 1/1 selftests/bpf: remove logic duplication in test_verifier.c
test_verifier tests can specify single- and multi-runs tests. Internally logic of handling them is duplicated. Get rid of it by making single run retval specification to be a first retvals spec.
--- tools/testing/selftests/bpf/test_verifier.c | 37 ++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/test_verifier.c +18 -19
@@ -86,7 +86,7 @@ struct bpf_test {int fixup_sk_storage_map[MAX_FIXUPS ];const char *errstr;const char *errstr_unpriv; -uint32_t retval, retval_unpriv, insn_processed; +uint32_t insn_processed;int prog_len;enum {UNDEF ,
@@ -95,16 +95,24 @@ struct bpf_test { } result, result_unpriv;enum bpf_prog_type prog_type;uint8_t flags; -__u8 data[TEST_DATA_LEN ];void (*fill_helper)(struct bpf_test *self);uint8_t runs; -struct { -uint32_t retval, retval_unpriv; -union { -__u8 data[TEST_DATA_LEN ]; -__u64 data64[TEST_DATA_LEN /8 ]; +union { +struct { +uint32_t retval, retval_unpriv; +union { +__u8 data[TEST_DATA_LEN ]; +__u64 data64[TEST_DATA_LEN /8 ]; + }; }; - } retvals[MAX_TEST_RUNS ]; +struct { +uint32_t retval, retval_unpriv; +union { +__u8 data[TEST_DATA_LEN ]; +__u64 data64[TEST_DATA_LEN /8 ]; + }; + } retvals[MAX_TEST_RUNS ]; + };enum bpf_attach_type expected_attach_type; };
@@ -949,17 +957,8 @@ static void do_test_single(struct bpf_test *test, bool unpriv,uint32_t expected_val;int i; -if (!test->runs) { - expected_val = unpriv && test->retval_unpriv ? - test->retval_unpriv : test->retval; - - err =do_prog_test_run (fd_prog, unpriv, expected_val, - test->data,sizeof (test->data)); -if (err) - run_errs++; -else - run_successes++; - } +if (!test->runs) + test->runs =1 ;for (i =0 ; i < test->runs; i++) {if (unpriv && test->retvals[i].retval_unpriv)
Signature · 1 line
2.17.1
Quoted 7 lines · Andrii Nakryiko
test_verifiertests can specify single- and multi-runs tests. Internally logic of handling them is duplicated. Get rid of it by making single run retval specification to be a first retvals spec.Cc Krzesimir Nowak <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>
Looks good, one nit below.
Quoted 21 lines
--- tools/testing/selftests/bpf/test_verifier.c | 37 ++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-)tools/testing/selftests/bpf/test_verifier.c +1
-1@@ -86,7 +86,7 @@ struct bpf_test {int fixup_sk_storage_map[MAX_FIXUPS ];const char *errstr;const char *errstr_unpriv; -uint32_t retval, retval_unpriv, insn_processed; +uint32_t insn_processed;int prog_len;enum {UNDEF ,@@ -95,16 +95,24 @@ struct bpf_test { } result, result_unpriv;enum bpf_prog_type prog_type;uint8_t flags;
tools/testing/selftests/bpf/test_verifier.c struct bpf_test { · line 101
Quoted 10 lines
tools/testing/selftests/bpf/test_verifier.c +2
-6-__u8 data[TEST_DATA_LEN ];void (*fill_helper)(struct bpf_test *self);uint8_t runs; -struct { -uint32_t retval, retval_unpriv; -union { -__u8 data[TEST_DATA_LEN ]; -__u64 data64[TEST_DATA_LEN /8 ]; + union { + struct {
Maybe consider moving the struct definition outside to further the removal of the duplication?
Quoted 41 lines
tools/testing/selftests/bpf/test_verifier.c +15
-13+uint32_t retval, retval_unpriv; +union { +__u8 data[TEST_DATA_LEN ]; +__u64 data64[TEST_DATA_LEN /8 ]; + }; }; - } retvals[MAX_TEST_RUNS ]; +struct { +uint32_t retval, retval_unpriv; +union { +__u8 data[TEST_DATA_LEN ]; +__u64 data64[TEST_DATA_LEN /8 ]; + }; + } retvals[MAX_TEST_RUNS ]; + };enum bpf_attach_type expected_attach_type; };@@ -949,17 +957,8 @@ static void do_test_single(struct bpf_test *test, bool unpriv,uint32_t expected_val;int i; -if (!test->runs) { - expected_val = unpriv && test->retval_unpriv ? - test->retval_unpriv : test->retval; - - err =do_prog_test_run (fd_prog, unpriv, expected_val, - test->data,sizeof (test->data)); -if (err) - run_errs++; -else - run_successes++; - } +if (!test->runs) + test->runs =1 ;for (i =0 ; i < test->runs; i++) {if (unpriv && test->retvals[i].retval_unpriv) --2.17.1
Signature · 5 lines
Kinvolk GmbH | Adalbertstr.6a, 10999 Berlin | tel: +491755589364 Geschäftsführer/Directors: Alban Crequy, Chris Kühl, Iago López Galeiras Registergericht/Court of registration: Amtsgericht Charlottenburg Registernummer/Registration number: HRB 171414 B Ust-ID-Nummer/VAT ID number: DE302207000
Quoted 40 lines · Krzesimir Nowak
Quoted 7 lines · Andrii Nakryiko
test_verifiertests can specify single- and multi-runs tests. Internally logic of handling them is duplicated. Get rid of it by making single run retval specification to be a first retvals spec.Cc Krzesimir Nowak <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>Looks good, one nit below.
Acked-by Krzesimir Nowak <[email protected]>Quoted 26 lines
--- tools/testing/selftests/bpf/test_verifier.c | 37 ++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-)tools/testing/selftests/bpf/test_verifier.c +1
-4@@ -86,7 +86,7 @@ struct bpf_test {int fixup_sk_storage_map[MAX_FIXUPS ];const char *errstr;const char *errstr_unpriv; -uint32_t retval, retval_unpriv, insn_processed; +uint32_t insn_processed;int prog_len;enum {UNDEF ,@@ -95,16 +95,24 @@ struct bpf_test { } result, result_unpriv;enum bpf_prog_type prog_type;uint8_t flags; -__u8 data[TEST_DATA_LEN ];void (*fill_helper)(struct bpf_test *self);uint8_t runs; -struct { -uint32_t retval, retval_unpriv;
Quoted 8 lines
tools/testing/selftests/bpf/test_verifier.c struct bpf_test { · line 101
Quoted 5 lines
tools/testing/selftests/bpf/test_verifier.c +2
-3-union { -__u8 data[TEST_DATA_LEN ]; -__u64 data64[TEST_DATA_LEN /8 ]; + union { + struct {Maybe consider moving the struct definition outside to further the removal of the duplication?
Can't do that because then retval/retval_unpriv/data won't be accessible as a normal field of struct bpf_test. It has to be in anonymous structs/unions, unfortunately.
I tried the following, but that also didn't work:
union {
struct bpf_test_retval {
uint32_t retval, retval_unpriv;
union {
__u8 data[TEST_DATA_LEN];
__u64 data64[TEST_DATA_LEN / 8];
};
};
struct bpf_test_retval retvals[MAX_TEST_RUNS];
};This also made retval/retval_unpriv to not behave as normal fields of struct bpf_test.
Quoted 50 lines
Quoted 40 lines
tools/testing/selftests/bpf/test_verifier.c +15
-13+uint32_t retval, retval_unpriv; +union { +__u8 data[TEST_DATA_LEN ]; +__u64 data64[TEST_DATA_LEN /8 ]; + }; }; - } retvals[MAX_TEST_RUNS ]; +struct { +uint32_t retval, retval_unpriv; +union { +__u8 data[TEST_DATA_LEN ]; +__u64 data64[TEST_DATA_LEN /8 ]; + }; + } retvals[MAX_TEST_RUNS ]; + };enum bpf_attach_type expected_attach_type; };@@ -949,17 +957,8 @@ static void do_test_single(struct bpf_test *test, bool unpriv,uint32_t expected_val;int i; -if (!test->runs) { - expected_val = unpriv && test->retval_unpriv ? - test->retval_unpriv : test->retval; - - err =do_prog_test_run (fd_prog, unpriv, expected_val, - test->data,sizeof (test->data)); -if (err) - run_errs++; -else - run_successes++; - } +if (!test->runs) + test->runs =1 ;for (i =0 ; i < test->runs; i++) {if (unpriv && test->retvals[i].retval_unpriv) --Quoted 2 lines
2.17.1
--
Kinvolk GmbH | Adalbertstr.6a, 10999 Berlin | tel: +491755589364 Geschäftsführer/Directors: Alban Crequy, Chris Kühl, Iago López Galeiras Registergericht/Court of registration: Amtsgericht Charlottenburg Registernummer/Registration number: HRB 171414 B Ust-ID-Nummer/VAT ID number: DE302207000
Quoted 51 lines · Andrii Nakryiko
Quoted 40 lines · Krzesimir Nowak
Quoted 7 lines · Andrii Nakryiko
test_verifiertests can specify single- and multi-runs tests. Internally logic of handling them is duplicated. Get rid of it by making single run retval specification to be a first retvals spec.Cc Krzesimir Nowak <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>Looks good, one nit below.
Acked-by Krzesimir Nowak <[email protected]>Quoted 26 lines
--- tools/testing/selftests/bpf/test_verifier.c | 37 ++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-)tools/testing/selftests/bpf/test_verifier.c +1
-4@@ -86,7 +86,7 @@ struct bpf_test {int fixup_sk_storage_map[MAX_FIXUPS ];const char *errstr;const char *errstr_unpriv; -uint32_t retval, retval_unpriv, insn_processed; +uint32_t insn_processed;int prog_len;enum {UNDEF ,@@ -95,16 +95,24 @@ struct bpf_test { } result, result_unpriv;enum bpf_prog_type prog_type;uint8_t flags; -__u8 data[TEST_DATA_LEN ];void (*fill_helper)(struct bpf_test *self);uint8_t runs; -struct { -uint32_t retval, retval_unpriv;Quoted 8 lines
tools/testing/selftests/bpf/test_verifier.c struct bpf_test { · line 101
Quoted 5 lines
tools/testing/selftests/bpf/test_verifier.c +2
-3-union { -__u8 data[TEST_DATA_LEN ]; -__u64 data64[TEST_DATA_LEN /8 ]; + union { + struct {Maybe consider moving the struct definition outside to further the removal of the duplication?
Quoted 4 lines
Can't do that because then retval/retval_unpriv/data won't be accessible as a normal field of struct
bpf_test. It has to be in anonymous structs/unions, unfortunately.
Ah, right.
Meh.
I tried something like this:
#define BPF_DATA_STRUCT \
struct { \
uint32_t retval, retval_unpriv; \
union { \
__u8 data[TEST_DATA_LEN]; \
__u64 data64[TEST_DATA_LEN / 8]; \
}; \
}and then:
union {
BPF_DATA_STRUCT;
BPF_DATA_STRUCT retvals[MAX_TEST_RUNS];
};And that seems to compile at least. But question is: is this acceptably ugly or unacceptably ugly? :)
Quoted 67 lines
I tried the following, but that also didn't work:
union { struct bpf_test_retval { uint32_t retval, retval_unpriv; union { __u8 data[TEST_DATA_LEN]; __u64 data64[TEST_DATA_LEN / 8]; }; }; struct bpf_test_retval retvals[MAX_TEST_RUNS]; };This also made retval/retval_unpriv to not behave as normal fields of struct
bpf_test.Quoted 50 lines
Quoted 40 lines
tools/testing/selftests/bpf/test_verifier.c +15
-13+uint32_t retval, retval_unpriv; +union { +__u8 data[TEST_DATA_LEN ]; +__u64 data64[TEST_DATA_LEN /8 ]; + }; }; - } retvals[MAX_TEST_RUNS ]; +struct { +uint32_t retval, retval_unpriv; +union { +__u8 data[TEST_DATA_LEN ]; +__u64 data64[TEST_DATA_LEN /8 ]; + }; + } retvals[MAX_TEST_RUNS ]; + };enum bpf_attach_type expected_attach_type; };@@ -949,17 +957,8 @@ static void do_test_single(struct bpf_test *test, bool unpriv,uint32_t expected_val;int i; -if (!test->runs) { - expected_val = unpriv && test->retval_unpriv ? - test->retval_unpriv : test->retval; - - err =do_prog_test_run (fd_prog, unpriv, expected_val, - test->data,sizeof (test->data)); -if (err) - run_errs++; -else - run_successes++; - } +if (!test->runs) + test->runs =1 ;for (i =0 ; i < test->runs; i++) {if (unpriv && test->retvals[i].retval_unpriv) --Quoted 2 lines
2.17.1
--
Kinvolk GmbH | Adalbertstr.6a, 10999 Berlin | tel: +491755589364 Geschäftsführer/Directors: Alban Crequy, Chris Kühl, Iago López Galeiras Registergericht/Court of registration: Amtsgericht Charlottenburg Registernummer/Registration number: HRB 171414 B Ust-ID-Nummer/VAT ID number: DE302207000
Signature · 5 lines
Kinvolk GmbH | Adalbertstr.6a, 10999 Berlin | tel: +491755589364 Geschäftsführer/Directors: Alban Crequy, Chris Kühl, Iago López Galeiras Registergericht/Court of registration: Amtsgericht Charlottenburg Registernummer/Registration number: HRB 171414 B Ust-ID-Nummer/VAT ID number: DE302207000
Daniel Borkmann
maintainerQuoted 75 lines · Krzesimir Nowak
Quoted 51 lines · Andrii Nakryiko
Quoted 40 lines · Krzesimir Nowak
Quoted 7 lines · Andrii Nakryiko
test_verifiertests can specify single- and multi-runs tests. Internally logic of handling them is duplicated. Get rid of it by making single run retval specification to be a first retvals spec.Cc Krzesimir Nowak <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>Looks good, one nit below.
Acked-by Krzesimir Nowak <[email protected]>Quoted 26 lines
--- tools/testing/selftests/bpf/test_verifier.c | 37 ++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-)tools/testing/selftests/bpf/test_verifier.c +1
-4@@ -86,7 +86,7 @@ struct bpf_test {int fixup_sk_storage_map[MAX_FIXUPS ];const char *errstr;const char *errstr_unpriv; -uint32_t retval, retval_unpriv, insn_processed; +uint32_t insn_processed;int prog_len;enum {UNDEF ,@@ -95,16 +95,24 @@ struct bpf_test { } result, result_unpriv;enum bpf_prog_type prog_type;uint8_t flags; -__u8 data[TEST_DATA_LEN ];void (*fill_helper)(struct bpf_test *self);uint8_t runs; -struct { -uint32_t retval, retval_unpriv;Quoted 8 lines
tools/testing/selftests/bpf/test_verifier.c struct bpf_test { · line 101
Quoted 5 lines
tools/testing/selftests/bpf/test_verifier.c +2
-3-union { -__u8 data[TEST_DATA_LEN ]; -__u64 data64[TEST_DATA_LEN /8 ]; + union { + struct {Maybe consider moving the struct definition outside to further the removal of the duplication?
Quoted 4 lines
Can't do that because then retval/retval_unpriv/data won't be accessible as a normal field of struct
bpf_test. It has to be in anonymous structs/unions, unfortunately.Ah, right.
Meh.
I tried something like this:
#define BPF_DATA_STRUCT \ struct { \ uint32_t retval, retval_unpriv; \ union { \ __u8 data[TEST_DATA_LEN]; \ __u64 data64[TEST_DATA_LEN / 8]; \ }; \ }and then:
Quoted 7 lines
union { BPF_DATA_STRUCT; BPF_DATA_STRUCT retvals[MAX_TEST_RUNS]; };And that seems to compile at least. But question is: is this acceptably ugly or unacceptably ugly? :)
Both a bit ugly, but I'd have a slight preference towards the above, perhaps a bit more readable like:
#define bpf_testdata_struct_t \
struct { \
uint32_t retval, retval_unpriv; \
union { \
__u8 data[TEST_DATA_LEN]; \
__u64 data64[TEST_DATA_LEN / 8]; \
}; \
}
union {
bpf_testdata_struct_t;
bpf_testdata_struct_t retvals[MAX_TEST_RUNS];
};Thanks,
Daniel
Quoted 82 lines · Daniel Borkmann
Quoted 80 lines · Krzesimir Nowak
Quoted 51 lines · Andrii Nakryiko
Quoted 40 lines · Krzesimir Nowak
Quoted 7 lines · Andrii Nakryiko
test_verifiertests can specify single- and multi-runs tests. Internally logic of handling them is duplicated. Get rid of it by making single run retval specification to be a first retvals spec.Cc Krzesimir Nowak <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>Looks good, one nit below.
Acked-by Krzesimir Nowak <[email protected]>Quoted 26 lines
--- tools/testing/selftests/bpf/test_verifier.c | 37 ++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-)tools/testing/selftests/bpf/test_verifier.c +1
-4@@ -86,7 +86,7 @@ struct bpf_test {int fixup_sk_storage_map[MAX_FIXUPS ];const char *errstr;const char *errstr_unpriv; -uint32_t retval, retval_unpriv, insn_processed; +uint32_t insn_processed;int prog_len;enum {UNDEF ,@@ -95,16 +95,24 @@ struct bpf_test { } result, result_unpriv;enum bpf_prog_type prog_type;uint8_t flags; -__u8 data[TEST_DATA_LEN ];void (*fill_helper)(struct bpf_test *self);uint8_t runs; -struct { -uint32_t retval, retval_unpriv;Quoted 8 lines
tools/testing/selftests/bpf/test_verifier.c struct bpf_test { · line 101
Quoted 5 lines
tools/testing/selftests/bpf/test_verifier.c +2
-3-union { -__u8 data[TEST_DATA_LEN ]; -__u64 data64[TEST_DATA_LEN /8 ]; + union { + struct {Maybe consider moving the struct definition outside to further the removal of the duplication?
Quoted 4 lines
Can't do that because then retval/retval_unpriv/data won't be accessible as a normal field of struct
bpf_test. It has to be in anonymous structs/unions, unfortunately.Ah, right.
Meh.
I tried something like this:
#define BPF_DATA_STRUCT \ struct { \ uint32_t retval, retval_unpriv; \ union { \ __u8 data[TEST_DATA_LEN]; \ __u64 data64[TEST_DATA_LEN / 8]; \ }; \ }and then:
union { BPF_DATA_STRUCT; BPF_DATA_STRUCT retvals[MAX_TEST_RUNS]; };
Quoted 5 lines
Quoted 2 lines
And that seems to compile at least. But question is: is this acceptably ugly or unacceptably ugly? :)
Both a bit ugly, but I'd have a slight preference towards the above, perhaps a bit more readable like:
Heh, I had slight preference the other way :) I'll update diff with macro, though.
Quoted 16 lines
#define bpf_testdata_struct_t \ struct { \ uint32_t retval, retval_unpriv; \ union { \ __u8 data[TEST_DATA_LEN]; \ __u64 data64[TEST_DATA_LEN / 8]; \ }; \ } union { bpf_testdata_struct_t; bpf_testdata_struct_t retvals[MAX_TEST_RUNS]; };Thanks,
Daniel