All of lore.kernel.org
 help / color / mirror / Atom feed
* How do I implement bpf_prog_test_run with BPF CO-RE?
@ 2022-01-17  7:15 Pony Sew
  2022-01-19 12:12 ` Mauricio Vásquez Bernal
  2022-01-20 21:57 ` Andrii Nakryiko
  0 siblings, 2 replies; 3+ messages in thread
From: Pony Sew @ 2022-01-17  7:15 UTC (permalink / raw)
  To: bpf

Hello.
It seems like before bpf_prog_test_run is called, you need to call
bpf_prog_load. But in BPF CO-RE program, we already have
<program>_bpf__open and <program>_bpf__load which basically does the
same thing. My code look approximately like this:

-----test1_load.c---------------
#include <cgreen/cgreen.h>

Ensure(Load, load_test){
    /* should I call bpf_prog_load here? */
    bpf_prog_test_run(/* parameters */);
    /* what about bpf_object__close? */
}

TestSuite *load_tests() {
    TestSuite *suite = create_test_suite();
    return suite;
}

-----bpf_firewall.bpf.c---------
 #include "vmlinux.h"
 #include <bpf/bpf_helpers.h>

SEC("xdp_prog")
int xdp_prog_main(struct xdp_md *ctx){
    /* main program */
}

 char LICENSE[] SEC("license") = "GPL";

------bpf_firewall.c---------------
#include <cgreen/cgreen.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include "bpf_firewall.skel.h"

TestSuite *load_tests();

int main(int argc, char **argv) {
    struct bpf_firewall_bpf *obj;
    int err = 0;

    obj = bpf_firewall_bpf__open();

    err = bpf_firewall_bpf__load(obj);

    err = bpf_firewall_bpf__attach(obj);

    TestSuite *suite = create_test_suite();
    add_suite(suite, load_tests());
    run_test_suite(suite, create_text_reporter());

    bpf_firewall_bpf__destroy(obj);
    return 0;
}

How do I implement bpf_prog_test_run with BPF CO-RE?

Best regards,
Poony.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How do I implement bpf_prog_test_run with BPF CO-RE?
  2022-01-17  7:15 How do I implement bpf_prog_test_run with BPF CO-RE? Pony Sew
@ 2022-01-19 12:12 ` Mauricio Vásquez Bernal
  2022-01-20 21:57 ` Andrii Nakryiko
  1 sibling, 0 replies; 3+ messages in thread
From: Mauricio Vásquez Bernal @ 2022-01-19 12:12 UTC (permalink / raw)
  To: Pony Sew; +Cc: bpf

On Mon, Jan 17, 2022 at 2:16 AM Pony Sew <poony20115@gmail.com> wrote:
>
> Hello.
> It seems like before bpf_prog_test_run is called, you need to call
> bpf_prog_load. But in BPF CO-RE program, we already have
> <program>_bpf__open and <program>_bpf__load which basically does the
> same thing. My code look approximately like this:
>
> -----test1_load.c---------------
> #include <cgreen/cgreen.h>
>
> Ensure(Load, load_test){
>     /* should I call bpf_prog_load here? */

bpf_prog_test_run() needs prog_fd. You're already calling
bpf_firewall_bpf__load() in bpf_firewall.c, you can get the program fd
by using
prog_fd = obj->progs.<program>.prog_fd;

>     bpf_prog_test_run(/* parameters */);
>     /* what about bpf_object__close? */
> }
>
> TestSuite *load_tests() {
>     TestSuite *suite = create_test_suite();
>     return suite;
> }
>
> -----bpf_firewall.bpf.c---------
>  #include "vmlinux.h"
>  #include <bpf/bpf_helpers.h>
>
> SEC("xdp_prog")
> int xdp_prog_main(struct xdp_md *ctx){
>     /* main program */
> }
>
>  char LICENSE[] SEC("license") = "GPL";
>
> ------bpf_firewall.c---------------
> #include <cgreen/cgreen.h>
> #include <bpf/libbpf.h>
> #include <bpf/bpf.h>
> #include "bpf_firewall.skel.h"
>
> TestSuite *load_tests();
>
> int main(int argc, char **argv) {
>     struct bpf_firewall_bpf *obj;
>     int err = 0;
>
>     obj = bpf_firewall_bpf__open();
>
>     err = bpf_firewall_bpf__load(obj);
>
>     err = bpf_firewall_bpf__attach(obj);

I don't think you need to call __attach() if you only want to perform
some tests with bpf_prog_test_run.

>
>     TestSuite *suite = create_test_suite();
>     add_suite(suite, load_tests());
>     run_test_suite(suite, create_text_reporter());
>
>     bpf_firewall_bpf__destroy(obj);
>     return 0;
> }
>
> How do I implement bpf_prog_test_run with BPF CO-RE?

You don't need to worry about it. You're using the skeleton and hence
libbpf underneath, it'll take care of performing all CO-RE relocations
when loading the program.

This approach is used many times in tools/testing/selftests/bpf, for
instance tools/testing/selftests/bpf/prog_tests/map_ptr.c does exactly
what you're looking for.

>
> Best regards,
> Poony.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How do I implement bpf_prog_test_run with BPF CO-RE?
  2022-01-17  7:15 How do I implement bpf_prog_test_run with BPF CO-RE? Pony Sew
  2022-01-19 12:12 ` Mauricio Vásquez Bernal
@ 2022-01-20 21:57 ` Andrii Nakryiko
  1 sibling, 0 replies; 3+ messages in thread
From: Andrii Nakryiko @ 2022-01-20 21:57 UTC (permalink / raw)
  To: Pony Sew; +Cc: bpf

On Mon, Jan 17, 2022 at 7:57 AM Pony Sew <poony20115@gmail.com> wrote:
>
> Hello.
> It seems like before bpf_prog_test_run is called, you need to call
> bpf_prog_load. But in BPF CO-RE program, we already have
> <program>_bpf__open and <program>_bpf__load which basically does the
> same thing. My code look approximately like this:
>
> -----test1_load.c---------------
> #include <cgreen/cgreen.h>
>
> Ensure(Load, load_test){
>     /* should I call bpf_prog_load here? */
>     bpf_prog_test_run(/* parameters */);
>     /* what about bpf_object__close? */
> }
>
> TestSuite *load_tests() {
>     TestSuite *suite = create_test_suite();
>     return suite;
> }
>
> -----bpf_firewall.bpf.c---------
>  #include "vmlinux.h"
>  #include <bpf/bpf_helpers.h>
>
> SEC("xdp_prog")
> int xdp_prog_main(struct xdp_md *ctx){
>     /* main program */
> }
>
>  char LICENSE[] SEC("license") = "GPL";
>
> ------bpf_firewall.c---------------
> #include <cgreen/cgreen.h>
> #include <bpf/libbpf.h>
> #include <bpf/bpf.h>
> #include "bpf_firewall.skel.h"
>
> TestSuite *load_tests();
>
> int main(int argc, char **argv) {
>     struct bpf_firewall_bpf *obj;
>     int err = 0;
>
>     obj = bpf_firewall_bpf__open();
>
>     err = bpf_firewall_bpf__load(obj);
>
>     err = bpf_firewall_bpf__attach(obj);
>
>     TestSuite *suite = create_test_suite();
>     add_suite(suite, load_tests());
>     run_test_suite(suite, create_text_reporter());
>
>     bpf_firewall_bpf__destroy(obj);
>     return 0;
> }
>
> How do I implement bpf_prog_test_run with BPF CO-RE?

It has nothing to do with BPF CO-RE. It's a BPF skeleton question.

But regardless, bpf_prog_test_run() expects BPF program FD, which you
can get with bpf_program__fd(skel->progs.xdp_prog_main) and pass the
result to bpf_prog_test_run() call.

>
> Best regards,
> Poony.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-20 21:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-17  7:15 How do I implement bpf_prog_test_run with BPF CO-RE? Pony Sew
2022-01-19 12:12 ` Mauricio Vásquez Bernal
2022-01-20 21:57 ` Andrii Nakryiko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.