All of lore.kernel.org
 help / color / mirror / Atom feed
* How to manually construct a struct bpf_program instance?
@ 2023-09-20  2:09 Shuyi Cheng
  2023-09-20 13:00 ` Eduard Zingerman
  0 siblings, 1 reply; 2+ messages in thread
From: Shuyi Cheng @ 2023-09-20  2:09 UTC (permalink / raw)
  To: bpf




Hello.

I found that libbpf can only construct struct bpf_program instances 
through skeleton. Can I manually construct struct bpf_program instances?

We now load our eBPF program by putting the eBPF bytecode into the elf 
file, and then letting libbpf open the elf file [1]. But adding a map to 
an elf file is a more complicated matter [2]. Therefore, we hope to 
create a bpf_program instance through something like struct bpf_program 
*bpf_program_new(void *insns, int insns_cnt). After creating the struct 
bpf_program instance, we can call bpf_program__attach to load our eBPF 
program bytecode.


[1] https://github.com/aliyun/coolbpf/blob/master/lwcb/bpfir/src/object.rs
[2] https://github.com/aliyun/coolbpf/issues/38

Thanks in advance!

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

* Re: How to manually construct a struct bpf_program instance?
  2023-09-20  2:09 How to manually construct a struct bpf_program instance? Shuyi Cheng
@ 2023-09-20 13:00 ` Eduard Zingerman
  0 siblings, 0 replies; 2+ messages in thread
From: Eduard Zingerman @ 2023-09-20 13:00 UTC (permalink / raw)
  To: Shuyi Cheng, bpf

On Wed, 2023-09-20 at 10:09 +0800, Shuyi Cheng wrote:
> Hello.
> 
> I found that libbpf can only construct struct bpf_program instances 
> through skeleton. Can I manually construct struct bpf_program instances?

Hi Shuyi,

I'm not an expert in libbpf's internals, but looking for places where
`struct bpf_program` objects are initialized in libbpf.c is see the
following call chain:
- bpf_object__open_mem (public) or 
  bpf_object__open_file (public)
  - bpf_object_open (internal)
    - bpf_object__elf_collect (internal)
      - bpf_object__add_programs (internal)
        - bpf_object__init_prog (internal)
          fills struct bpf_program fields

Both bpf_object__open_{mem,file} assume operation on ELF object,
the rest of the functions is internal. So, it appears that answer to
you question is "no", you will need to create ".maps" section in ELF
in line with libbpf's expectations (not sure if these expectations are
documented).

> We now load our eBPF program by putting the eBPF bytecode into the elf 
> file, and then letting libbpf open the elf file [1]. But adding a map to 
> an elf file is a more complicated matter [2]. Therefore, we hope to 
> create a bpf_program instance through something like struct bpf_program 
> *bpf_program_new(void *insns, int insns_cnt). After creating the struct 
> bpf_program instance, we can call bpf_program__attach to load our eBPF 
> program bytecode.

On the other hand, if all you want is to create a program from given
set of instructions there is a function `bpf_prog_load()` which
returns program fd. As well as function `bpf_map_create()` which
returns map fd. You can take a look at [3] to see how these functions
are used. (However, you will have to take care of line info, BTF etc
in case you use those).

[3] https://github.com/torvalds/linux/blob/master/tools/testing/selftests/bpf/test_verifier.c

> 
> [1] https://github.com/aliyun/coolbpf/blob/master/lwcb/bpfir/src/object.rs
> [2] https://github.com/aliyun/coolbpf/issues/38
> 
> Thanks in advance!
> 

Thanks,
Eduard

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

end of thread, other threads:[~2023-09-20 13:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-20  2:09 How to manually construct a struct bpf_program instance? Shuyi Cheng
2023-09-20 13:00 ` Eduard Zingerman

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.