Skip to content

Commit

Permalink
bpf: Maps live in maps section
Browse files Browse the repository at this point in the history
This forces all maps to the maps section so we remain compatible with
libbpf. This requires aya-rs#181 to avoid breaking userspace.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
  • Loading branch information
dave-tucker committed May 3, 2022
1 parent 5472ac0 commit f12c026
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
4 changes: 3 additions & 1 deletion bpf/aya-bpf-macros/src/expand.rs
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();
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
@@ -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
@@ -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
@@ -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

0 comments on commit f12c026

Please sign in to comment.