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

libbpf-rs: Few changes to recent patches #336

Merged
merged 2 commits into from
Jan 26, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/capable/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ fn main() {
.source(SRC)
.build_and_generate(&out)
.unwrap();
println!("cargo:rerun-if-changed={}", SRC);
println!("cargo:rerun-if-changed={SRC}");
}
2 changes: 1 addition & 1 deletion examples/capable/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn _handle_event(opts: Command, event: capable_bss_types::event) {
}

fn handle_lost_events(cpu: i32, count: u64) {
eprintln!("Lost {} events on CPU {}", count, cpu);
eprintln!("Lost {count} events on CPU {cpu}");
}

fn main() -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion examples/runqslower/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ fn main() {
.source(SRC)
.build_and_generate(&out)
.unwrap();
println!("cargo:rerun-if-changed={}", SRC);
println!("cargo:rerun-if-changed={SRC}");
}
2 changes: 1 addition & 1 deletion examples/runqslower/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn handle_event(_cpu: i32, data: &[u8]) {
}

fn handle_lost_events(cpu: i32, count: u64) {
eprintln!("Lost {} events on CPU {}", count, cpu);
eprintln!("Lost {count} events on CPU {cpu}");
}

fn main() -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion examples/tc_port_whitelist/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ fn main() {
.source(SRC)
.build_and_generate(&out)
.unwrap();
println!("cargo:rerun-if-changed={}", SRC);
println!("cargo:rerun-if-changed={SRC}");
}
28 changes: 14 additions & 14 deletions examples/tc_port_whitelist/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,28 @@ fn main() -> Result<()> {

if opts.query {
match custom.query() {
Err(e) => println!("failed to find custom hook: {}", e),
Ok(prog_id) => println!("found custom hook prog_id: {}", prog_id),
Err(e) => println!("failed to find custom hook: {e}"),
Ok(prog_id) => println!("found custom hook prog_id: {prog_id}"),
}
match egress.query() {
Err(e) => println!("failed to find custom hook: {}", e),
Ok(prog_id) => println!("found custom hook prog_id: {}", prog_id),
Err(e) => println!("failed to find custom hook: {e}"),
Ok(prog_id) => println!("found custom hook prog_id: {prog_id}"),
}
match ingress.query() {
Err(e) => println!("failed to find custom hook: {}", e),
Ok(prog_id) => println!("found custom hook prog_id: {}", prog_id),
Err(e) => println!("failed to find custom hook: {e}"),
Ok(prog_id) => println!("found custom hook prog_id: {prog_id}"),
}
}

if opts.detach {
if let Err(e) = ingress.detach() {
println!("failed to detach ingress hook {}", e);
println!("failed to detach ingress hook {e}");
}
if let Err(e) = egress.detach() {
println!("failed to detach egress hook {}", e);
println!("failed to detach egress hook {e}");
}
if let Err(e) = custom.detach() {
println!("failed to detach custom hook {}", e);
println!("failed to detach custom hook {e}");
}
}

Expand All @@ -110,27 +110,27 @@ fn main() -> Result<()> {
let key = (i as u32).to_ne_bytes();
let val = port.to_ne_bytes();
if let Err(e) = skel.maps_mut().ports().update(&key, &val, MapFlags::ANY) {
bail!("Example limited to 10 ports: {}", e);
bail!("Example limited to 10 ports: {e}");
}
}
ingress.create()?;

if let Err(e) = egress.attach() {
println!("failed to attach egress hook {}", e);
println!("failed to attach egress hook {e}");
}

if let Err(e) = ingress.attach() {
println!("failed to attach ingress hook {}", e);
println!("failed to attach ingress hook {e}");
}

if let Err(e) = custom.attach() {
println!("failed to attach custom hook {}", e);
println!("failed to attach custom hook {e}");
}
}

if opts.destroy {
if let Err(e) = destroy_all.destroy() {
println!("failed to destroy all {}", e);
println!("failed to destroy all {e}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/tproxy/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fn main() {
.clang_args("-Wno-compare-distinct-pointer-types")
.build_and_generate(&out)
.unwrap();
println!("cargo:rerun-if-changed={}", SRC);
println!("cargo:rerun-if-changed={SRC}");
}
4 changes: 2 additions & 2 deletions examples/tproxy/src/bin/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ fn handle_client(client: TcpStream) -> Result<()> {
let peer_addr = client.peer_addr().context("Failed to get peer addr")?;

println!("New connection:");
println!("\tlocal: {}", local_addr);
println!("\tpeer: {}", peer_addr);
println!("\tlocal: {local_addr}");
println!("\tpeer: {peer_addr}");
println!();

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions examples/tproxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ fn main() -> Result<()> {
}

if let Err(e) = ingress.detach() {
eprintln!("Failed to detach prog: {}", e);
eprintln!("Failed to detach prog: {e}");
}
if let Err(e) = ingress.destroy() {
eprintln!("Failed to destroy TC hook: {}", e);
eprintln!("Failed to destroy TC hook: {e}");
}

Ok(())
Expand Down
38 changes: 14 additions & 24 deletions libbpf-cargo/src/btf/btf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,12 @@ impl Btf {
};

match t.encoding {
btf::BtfIntEncoding::Signed => format!("i{}", width),
btf::BtfIntEncoding::Signed => format!("i{width}"),
btf::BtfIntEncoding::Bool => {
assert!(t.bits as usize == (std::mem::size_of::<bool>() * 8));
"bool".to_string()
}
btf::BtfIntEncoding::Char | btf::BtfIntEncoding::None => format!("u{}", width),
btf::BtfIntEncoding::Char | btf::BtfIntEncoding::None => format!("u{width}"),
}
}
BtfType::Float(t) => {
Expand All @@ -570,12 +570,12 @@ impl Btf {
_ => bail!("Invalid float width"),
};

format!("f{}", width)
format!("f{width}")
}
BtfType::Ptr(t) => {
let pointee_ty = self.type_declaration(t.pointee_type)?;

format!("*mut {}", pointee_ty)
format!("*mut {pointee_ty}")
}
BtfType::Array(t) => {
let val_ty = self.type_declaration(t.val_type_id)?;
Expand Down Expand Up @@ -832,16 +832,11 @@ impl Btf {
)?;

if padding != 0 {
agg_content.push(format!(
r#" __pad_{offset}: [u8; {padding}],"#,
offset = offset,
padding = padding,
));
agg_content
.push(format!(r#" __pad_{offset}: [u8; {padding}],"#,));

impl_default.push(format!(
r#" __pad_{offset}: [u8::default(); {padding}]"#,
offset = offset,
padding = padding,
));
}

Expand Down Expand Up @@ -877,7 +872,7 @@ impl Btf {
field_ty_str.clone()
};

agg_content.push(format!(r#" pub {}: {},"#, field_name, field_ty_str));
agg_content.push(format!(r#" pub {field_name}: {field_ty_str},"#));
}

if t.is_struct {
Expand All @@ -903,7 +898,7 @@ impl Btf {
let aggregate_type = if t.is_struct { "struct" } else { "union" };
let packed_repr = if packed { ", packed" } else { "" };

writeln!(def, r#"#[repr(C{})]"#, packed_repr)?;
writeln!(def, r#"#[repr(C{packed_repr})]"#)?;
writeln!(
def,
r#"pub {agg_type} {name} {{"#,
Expand All @@ -912,7 +907,7 @@ impl Btf {
)?;

for field in agg_content {
writeln!(def, "{}", field)?;
writeln!(def, "{field}")?;
}
writeln!(def, "}}")?;

Expand All @@ -922,7 +917,7 @@ impl Btf {
writeln!(def, r#" fn default() -> Self {{"#)?;
writeln!(def, r#" {} {{"#, t.name)?;
for impl_def in impl_default {
writeln!(def, r#"{},"#, impl_def)?;
writeln!(def, r#"{impl_def},"#)?;
}
writeln!(def, r#" }}"#)?;
writeln!(def, r#" }}"#)?;
Expand Down Expand Up @@ -967,13 +962,8 @@ impl Btf {
}

writeln!(def, r#"#[derive(Debug, Copy, Clone, PartialEq, Eq)]"#)?;
writeln!(
def,
r#"#[repr({signed}{repr_size})]"#,
signed = signed,
repr_size = repr_size,
)?;
writeln!(def, r#"pub enum {name} {{"#, name = t.name,)?;
writeln!(def, r#"#[repr({signed}{repr_size})]"#)?;
writeln!(def, r#"pub enum {name} {{"#, name = t.name)?;

for value in &t.values {
writeln!(
Expand Down Expand Up @@ -1012,7 +1002,7 @@ impl Btf {

writeln!(def, r#"#[derive(Debug, Copy, Clone)]"#)?;
writeln!(def, r#"#[repr(C)]"#)?;
writeln!(def, r#"pub struct {} {{"#, sec_name,)?;
writeln!(def, r#"pub struct {sec_name} {{"#)?;

let mut offset: u32 = 0;
for datasec_var in &t.vars {
Expand All @@ -1039,7 +1029,7 @@ impl Btf {
false,
)?;
if padding != 0 {
writeln!(def, r#" __pad_{}: [u8; {}],"#, offset, padding,)?;
writeln!(def, r#" __pad_{offset}: [u8; {padding}],"#)?;
}

// Set `offset` to end of current var
Expand Down
6 changes: 3 additions & 3 deletions libbpf-cargo/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn strip_dwarf_info(file: &Path) -> Result<()> {
let mut linker =
libbpf_rs::Linker::new(file).context("Failed to instantiate libbpf object file linker")?;
linker
.add(temp_file)
.add_file(temp_file)
.context("Failed to add object file to BPF linker")?;
linker.link().context("Failed to link object file")?;
Ok(())
Expand Down Expand Up @@ -146,7 +146,7 @@ fn compile_one(debug: bool, source: &Path, out: &Path, clang: &Path, options: &s
"aarch64" => "arm64",
_ => std::env::consts::ARCH,
};
cmd.arg(format!("-D__TARGET_ARCH_{}", arch));
cmd.arg(format!("-D__TARGET_ARCH_{arch}"));
}

cmd.arg("-g")
Expand Down Expand Up @@ -227,7 +227,7 @@ pub fn build(
if debug && !to_compile.is_empty() {
println!("Found bpf progs to compile:");
for obj in &to_compile {
println!("\t{:?}", obj);
println!("\t{obj:?}");
}
} else if to_compile.is_empty() {
bail!("Did not find any bpf progs to compile");
Expand Down
Loading