Issue eBPF kernel programs with ldc?
data pulverizer
data.pulverizer at gmail.com
Sun Jan 5 16:27:58 UTC 2025
On Sunday, 5 January 2025 at 14:27:57 UTC, Richard (Rikki) Andrew
Cattermole wrote:
> On 06/01/2025 3:17 AM, data pulverizer wrote:
>
>> long bpf_trace_printk(const(char)* fmt, uint fmt_size, ...);
>
>> libbpf: failed to find BTF for extern 'bpf_trace_printk': -2
>
>
> Compare your D definition against:
>
> ```c
> static long (* const bpf_trace_printk)(const char *fmt, __u32
> fmt_size, ...) = (void *) 6;
> ```
>
> https://github.com/libbpf/libbpf/blob/c5f22aca0f3aa855daa159b2777472b35e721804/src/bpf_helper_defs.h#L185
>
> The reason it cannot find it, is because its looking for a
> function, when it is a global variable.
I have updated the kernel code to:
```d
@system:
@nogc:
extern(C):
@(ldc.attributes.section("license")) __gshared immutable(char)[3]
LICENSE = "GPL";
import core.stdc.stdlib;
import core.stdc.stdarg;
import ldc.attributes;
// Define a type alias for the function signature
alias BpfTracePrintk = long function(const(char)* fmt, uint
fmt_size, ...);
// Declare and initialize the function pointer
__gshared static BpfTracePrintk bpf_trace_printk =
cast(BpfTracePrintk) 6;
@(ldc.attributes.section("xdp_md"))
struct xdp_md {
uint data;
uint data_end;
uint data_meta;
uint ingress_ifindex;
uint rx_queue_index;
uint egress_ifindex;
}
@(ldc.attributes.section("xdp")) int xdp_prog(xdp_md* ctx)
{
bpf_trace_printk("Hello, eBPF!\n", 14);
return 0;
}
```
And getting a different error:
```sh
$ ldc2 --O2 --betterC -c -g --nogc --fno-moduleinfo --march=bpf
hello_world.d -of hello_world_d.o
$ ldc2 --betterC -L-lbpf -L-lelf -L-lz -of=loader_d loader_d.d
$ sudo ./loader_d
libbpf: elf: skipping unrecognized data section(21) .eh_frame
libbpf: elf: skipping relo section(22) .rel.eh_frame for
section(21) .eh_frame
libbpf: BTF loading error: -22
libbpf: -- BEGIN BTF LOAD LOG ---
magic: 0xeb9f
version: 1
flags: 0x0
hdr_len: 24
type_off: 0
type_len: 312
str_off: 312
str_len: 374
btf_total_size: 710
[1] PTR hello_world.xdp_md* type_id=2 Invalid name
-- END BTF LOAD LOG --
libbpf: Error loading .BTF into kernel: -22. BTF is optional,
ignoring.
libbpf: prog 'xdp_prog': BPF program load failed: Invalid argument
libbpf: prog 'xdp_prog': -- BEGIN PROG LOAD LOG --
unknown opcode 8d
processed 0 insns (limit 1000000) max_states_per_insn 0
total_states 0 peak_states 0 mark_read 0
-- END PROG LOAD LOG --
libbpf: prog 'xdp_prog': failed to load: -22
libbpf: failed to load object 'hello_world_d.o'
ERROR: loading BPF object file failed
```
I updated `xdp_md` when it complained about it, it looks like it
might be having trouble with the type name being prefixed with
the module name `hello_world`?
More information about the Digitalmars-d-learn
mailing list