Skip to content

Commit

Permalink
improve(libbpf-skel): use UnsafeCell for holding owning data of self-…
Browse files Browse the repository at this point in the history
…referential skel

Swicth back to stable self_cell as we stop using unreleased self_cell::MutBorrow, which is also implemented with UnsafeCell internally.
  • Loading branch information
EHfive committed Nov 21, 2024
1 parent 5fc6c09 commit a4a2577
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ opt-level = "z"
strip = true

[features]
default = ["aya", "pkg-config"]
default = ["aya", "libbpf-skel", "pkg-config"]
# Enable IPv6 NAPT
ipv6 = []
# Enable Aya BPF loader, requires Rust>=1.80.0
Expand Down Expand Up @@ -66,7 +66,7 @@ netlink-packet-route = "0.19.0"
netlink-sys = "0.8.6"
prefix-trie = "0.5.1"
rtnetlink = "0.14.1"
self_cell = { version = "1.0.4", git = "https://github.com/Voultapher/self_cell.git", optional = true }
self_cell = { version = "1.0.4", optional = true }
serde = { version = "1.0.215", features = ["derive"] }
tokio = { version = "1.41.1", features = ["macros", "rt", "signal", "sync"] }
toml = { version = "0.8.19", default-features = false, features = ["parse"] }
Expand Down
13 changes: 9 additions & 4 deletions src/skel/einat/libbpf_skel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod skel_build {
use skel_build::types as einat_types;
use skel_build::*;

use std::cell::UnsafeCell;
use std::mem;
use std::net::Ipv4Addr;
#[cfg(feature = "ipv6")]
Expand All @@ -18,7 +19,7 @@ use ipnet::Ipv4Net;
use ipnet::Ipv6Net;
use libbpf_rs::skel::{OpenSkel, SkelBuilder};
use libbpf_rs::{MapHandle, OpenObject};
use self_cell::{self_cell, MutBorrow};
use self_cell::self_cell;

use super::super::libbpf::LibbpfMap;
use super::{EinatConstConfig, EinatEbpf, EinatEbpfInet, EinatRoData};
Expand All @@ -28,7 +29,7 @@ use super::libbpf_common::{attach, detach, EinatLibbpfLinks};

self_cell!(
struct OwnedSkel {
owner: MutBorrow<mem::MaybeUninit<OpenObject>>,
owner: UnsafeCell<mem::MaybeUninit<OpenObject>>,

#[covariant]
dependent: EinatSkel,
Expand Down Expand Up @@ -60,10 +61,14 @@ impl EinatEbpf for EinatLibbpfSkel {
type Links = EinatLibbpfLinks;

fn load(config: EinatConstConfig) -> Result<Self> {
let obj = MutBorrow::new(mem::MaybeUninit::zeroed());
let obj = UnsafeCell::new(mem::MaybeUninit::zeroed());

let skel = OwnedSkel::try_new(obj, |obj| -> Result<_> {
let mut open_skel = EinatSkelBuilder::default().open(obj.borrow_mut())?;
// #Safety
// obj is only used here for construction of the skeleton
let obj = unsafe { &mut *obj.get() };

let mut open_skel = EinatSkelBuilder::default().open(obj)?;
*open_skel.maps.rodata_data =
unsafe { mem::transmute::<EinatRoData, einat_types::rodata>(config.ro_data) };

Expand Down

0 comments on commit a4a2577

Please sign in to comment.