foniod / redbpf

Rust library for building and running BPF/eBPF modules
Apache License 2.0
1.71k stars 136 forks source link

RingBuf maps not working: bpf_create_map_xattr: Invalid argument (os error 22) #327

Closed GermanCoding closed 2 years ago

GermanCoding commented 2 years ago

Hello again 😄 ,

commit 51e118df6514188220afabe9128081e49ea0b543 indeed fixed issue #325, however since that commit Ring Buffer maps no longer work. No matter what output size I choose, the OS always reports Invalid Argument error.

Minimal reproducer probe:


#![no_std]
#![no_main]

use redbpf_probes::ringbuf::RingBufMap;
use redbpf_probes::xdp::prelude::*;

// Ring buffers technically require kernel 5.9+, but it doesn't matter for this example
program!(0xFFFFFFFE, "GPL");

// 4096 is both power of two and page size on this system
#[map]
static mut DEMO: RingBufMap<u64> = RingBufMap::with_buffer_size(4096);

#[xdp]
unsafe fn xdp_test(ctx: XdpContext) -> XdpResult {
    Ok(XdpAction::Pass)
}

Loading this probe with log level set to debug prints:


May 05 13:58:43.427 DEBUG redbpf::btf: `Pointer' type should not have a name but its name is `*mut xdp_md'. erase the name
May 05 13:58:43.427 DEBUG redbpf::btf: `RingBufMap<u64>' is an invalid name. invalid characters are substituted with underscores
May 05 13:58:43.427 DEBUG redbpf::btf: `PhantomData<u64>' is an invalid name. invalid characters are substituted with underscores
May 05 13:58:43.427 DEBUG redbpf::btf: `MaybeUninit<u64>' is an invalid name. invalid characters are substituted with underscores
May 05 13:58:43.427 DEBUG redbpf::btf: `()' is an invalid name. invalid characters are substituted with underscores
May 05 13:58:43.427 DEBUG redbpf::btf: `ManuallyDrop<u64>' is an invalid name. invalid characters are substituted with underscores
May 05 13:58:43.427 DEBUG redbpf::btf: `maps/DEMO' is an invalid name. invalid characters are substituted with underscores
May 05 13:58:43.428 DEBUG redbpf::btf: [1] Pointer '*mut xdp_md' type_id=2
May 05 13:58:43.428 DEBUG redbpf::btf: [2] Structure 'xdp_md' size=24 vlen=6
    'data' type_id=3 bits_offset=0
    'data_end' type_id=3 bits_offset=32
    'data_meta' type_id=3 bits_offset=64
    'ingress_ifindex' type_id=3 bits_offset=96
    'rx_queue_index' type_id=3 bits_offset=128
    'egress_ifindex' type_id=3 bits_offset=160
May 05 13:58:43.428 DEBUG redbpf::btf: [3] Integer 'u32' size=4 encoding=(none) bits_offset=0 nr_bits=32
May 05 13:58:43.428 DEBUG redbpf::btf: [4] FunctionProtocol '(anon)' ret_type_id=5 vlen=1
May 05 13:58:43.428 DEBUG redbpf::btf: [5] Enumeration 'XdpAction' size=4 vlen=5
May 05 13:58:43.428 DEBUG redbpf::btf: [6] Function 'outer_xdp_test' type_id=4
May 05 13:58:43.428 DEBUG redbpf::btf: [7] Integer 'u8' size=1 encoding=(none) bits_offset=0 nr_bits=8
May 05 13:58:43.428 DEBUG redbpf::btf: [8] Array '(anon)' type_id=7 index_type_id=9 nr_elems=4
May 05 13:58:43.428 DEBUG redbpf::btf: [9] Integer '__ARRAY_SIZE_TYPE__' size=4 encoding=(none) bits_offset=0 nr_bits=32
May 05 13:58:43.428 DEBUG redbpf::btf: [10] Variable '_license' type_id=8
May 05 13:58:43.428 DEBUG redbpf::btf: [11] Variable '_version' type_id=3
May 05 13:58:43.428 DEBUG redbpf::btf: [12] Structure 'RingBufMap<u64>' size=20 vlen=2
    'def' type_id=13 bits_offset=0
    '_event' type_id=14 bits_offset=0
May 05 13:58:43.428 DEBUG redbpf::btf: [13] Structure 'bpf_map_def' size=20 vlen=5
    'type_' type_id=3 bits_offset=0
    'key_size' type_id=3 bits_offset=32
    'value_size' type_id=3 bits_offset=64
    'max_entries' type_id=3 bits_offset=96
    'map_flags' type_id=3 bits_offset=128
May 05 13:58:43.428 DEBUG redbpf::btf: [14] Structure 'PhantomData<u64>' size=0 vlen=0
May 05 13:58:43.428 DEBUG redbpf::btf: [15] Variable 'DEMO' type_id=12
May 05 13:58:43.428 DEBUG redbpf::btf: [16] Union 'MaybeUninit<u64>' size=8 vlen=2
    'uninit' type_id=17 bits_offset=0
    'value' type_id=18 bits_offset=0
May 05 13:58:43.428 DEBUG redbpf::btf: [17] Integer '()' size=0 encoding=(none) bits_offset=0 nr_bits=0
May 05 13:58:43.428 DEBUG redbpf::btf: [18] Structure 'ManuallyDrop<u64>' size=8 vlen=1
    'value' type_id=19 bits_offset=0
May 05 13:58:43.428 DEBUG redbpf::btf: [19] Integer 'u64' size=8 encoding=(none) bits_offset=0 nr_bits=64
May 05 13:58:43.428 DEBUG redbpf::btf: [20] Variable 'MAP_VALUE_ALIGN_DEMO' type_id=16
May 05 13:58:43.428 DEBUG redbpf::btf: [21] Structure '____btf_map_DEMO' size=16 vlen=2
    'key' type_id=3 bits_offset=0
    'value' type_id=19 bits_offset=64
May 05 13:58:43.428 DEBUG redbpf::btf: [22] Variable 'MAP_BTF_DEMO' type_id=21
May 05 13:58:43.428 DEBUG redbpf::btf: [23] DataSection '.rodata' size=8 vlen=1
    type_id=20 offset=0 size=8
May 05 13:58:43.428 DEBUG redbpf::btf: [24] DataSection 'license' size=4 vlen=1
    type_id=10 offset=0 size=4
May 05 13:58:43.428 DEBUG redbpf::btf: [25] DataSection 'maps.ext' size=16 vlen=1
    type_id=22 offset=0 size=16
May 05 13:58:43.428 DEBUG redbpf::btf: [26] DataSection 'maps/DEMO' size=20 vlen=1
    type_id=15 offset=0 size=20
May 05 13:58:43.428 DEBUG redbpf::btf: [27] DataSection 'version' size=4 vlen=1
    type_id=11 offset=0 size=4
May 05 13:58:43.428 DEBUG redbpf: Map `DEMO' has BTF info. MapBtfTypeId { btf_fd: 9, key_type_id: 3, value_type_id: 19 }
May 05 13:58:43.428 ERROR redbpf: error on bpf_create_map_xattr. failed to load map `DEMO`: Invalid argument (os error 22)
thread 'main' panicked at 'Failed to load xdp_test.elf: ParseError(Map)', src/main.rs:155:45
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This issue was introduced via commit 51e118df6514188220afabe9128081e49ea0b543. Prior to that commit I would only get this "invalid argument OS error" error when attempting to create a RingBuf map that is not a power of two. Now it seems to appear for every RingBuf map, no matter what size I choose.

I've quickly skimmed over other map types and it seems only the RingBuf ones cause panics, others seem to work fine.

Again, thanks for redBPF and the quick fix 👍

rsdy commented 2 years ago

Hm, RingBuf is a reasonably new code to begin with, so it's super helpful you can test this in real life. @rhdxmr I can't see an obvious reason in https://github.com/foniod/redbpf/commit/51e118df6514188220afabe9128081e49ea0b543 for this to happen. Do you have any ideas?