Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpf: Maps live in maps section #248

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion bpf/aya-bpf-macros/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ impl Map {
}

pub fn expand(&self) -> Result<TokenStream> {
let section_name = format!("maps/{}", self.name);
let section_name = "maps".to_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libbpf expects maps live in .maps section.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only expects them in .maps BTF maps.

But that being said, I think we need a way to specify it by using this macro.

I'm not suggesting it's the right PR to do this, it might be add as part of BTF map support.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The legacy one is deprecated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The legacy, bpf_map_def style of map definition is the only style that we support in aya-bpf right now.

But that being said, I think we need a way to specify it by using this macro.

Not really. If we supported BTF defs, all maps would use BTF map defs.
I'm not sure if libbpf's deprecation notice means support will be removed from the kernel (I doubt it) or whether it's to give users a push to use BTF.

Anyhow, userspace support for BTF maps is proposed in #140 but still needs work.
Support in aya-bpf for BTF maps is sadly not as simple as the BTF generated for Rust programs by LLVM isn't correct.

let name = &self.name;
let item = &self.item;
Ok(quote! {
#[no_mangle]
#[link_section = #section_name]
#[export_name = #name]
#item
})
}
Expand Down
37 changes: 37 additions & 0 deletions test/cases/020_elf/000_maps/map_test.ebpf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! ```cargo
//! [dependencies]
//! aya-bpf = { path = "../../../../bpf/aya-bpf" }
//! ```

#![no_std]
#![no_main]

use aya_bpf::{
bindings::xdp_action,
macros::{map, xdp},
programs::XdpContext,
maps::Array,
};

#[map]
static mut FOO: Array<u32> = Array::<u32>::with_max_entries(10, 0);

#[map(name = "BAR")]
static mut BAZ: Array<u32> = Array::<u32>::with_max_entries(10, 0);

#[xdp]
pub fn pass(ctx: XdpContext) -> u32 {
match unsafe { try_pass(ctx) } {
Ok(ret) => ret,
Err(_) => xdp_action::XDP_ABORTED,
}
}

unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> {
Ok(xdp_action::XDP_PASS)
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
unsafe { core::hint::unreachable_unchecked() }
}
Binary file added test/cases/020_elf/000_maps/map_test.o
Binary file not shown.
25 changes: 25 additions & 0 deletions test/cases/020_elf/000_maps/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh
# SUMMARY: Check that maps are correctly represented in ELF files
# LABELS:

set -ex

# Source libraries. Uncomment if needed/defined
#. "${RT_LIB}"
. "${RT_PROJECT_ROOT}/_lib/lib.sh"

NAME=map_test

clean_up() {
rm -rf ebpf user ${NAME}.o
}

trap clean_up EXIT

# Test code goes here
compile_ebpf ${NAME}.ebpf.rs

readelf --sections ${NAME}.o | grep -q "maps"
readelf --syms ${NAME}.o | grep -q "BAR"

exit 0
36 changes: 36 additions & 0 deletions test/cases/020_elf/group.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh
# SUMMARY: Tests to check ELF from aya-bpf
# LABELS:

# Source libraries. Uncomment if needed/defined
# . "${RT_LIB}"
. "${RT_PROJECT_ROOT}/_lib/lib.sh"

set -e

group_init() {
# Group initialisation code goes here
return 0
}

group_deinit() {
# Group de-initialisation code goes here
return 0
}

CMD=$1
case $CMD in
init)
group_init
res=$?
;;
deinit)
group_deinit
res=$?
;;
*)
res=1
;;
esac

exit $res