Skip to content

Commit

Permalink
Add rustfmt to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Oct 10, 2024
1 parent 05a7f39 commit 7d9e2ba
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:

- uses: dtolnay/rust-toolchain@nightly
with:
components: rust-src
components: rust-src,rustfmt

- uses: dtolnay/rust-toolchain@stable
with:
Expand Down
4 changes: 4 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
reorder_imports = true
unstable_features = true
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ esac

cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" ${ADDITIONAL_ARGS}
pushd test
cargo +nightly fmt --all -- --check
cargo build --package test
cargo build --package test --release
# We cannot run clippy over the whole workspace at once due to feature unification. Since both test
Expand Down
64 changes: 15 additions & 49 deletions {{project-name}}-ebpf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ fn try_{{crate_name}}(ctx: RetProbeContext) -> Result<u32, u32> {
Ok(0)
}
{%- when "fentry" %}
use aya_ebpf::{
macros::fentry,
programs::FEntryContext,
};
use aya_ebpf::{macros::fentry, programs::FEntryContext};
use aya_log_ebpf::info;

#[fentry(function="{{fn_name}}")]
#[fentry(function = "{{fn_name}}")]
pub fn {{crate_name}}(ctx: FEntryContext) -> u32 {
match try_{{crate_name}}(ctx) {
Ok(ret) => ret,
Expand All @@ -53,13 +50,10 @@ fn try_{{crate_name}}(ctx: FEntryContext) -> Result<u32, u32> {
Ok(0)
}
{%- when "fexit" %}
use aya_ebpf::{
macros::fexit,
programs::FExitContext,
};
use aya_ebpf::{macros::fexit, programs::FExitContext};
use aya_log_ebpf::info;

#[fexit(function="{{fn_name}}")]
#[fexit(function = "{{fn_name}}")]
pub fn {{crate_name}}(ctx: FExitContext) -> u32 {
match try_{{crate_name}}(ctx) {
Ok(ret) => ret,
Expand All @@ -72,10 +66,7 @@ fn try_{{crate_name}}(ctx: FExitContext) -> Result<u32, u32> {
Ok(0)
}
{%- when "uprobe" %}
use aya_ebpf::{
macros::uprobe,
programs::ProbeContext,
};
use aya_ebpf::{macros::uprobe, programs::ProbeContext};
use aya_log_ebpf::info;

#[uprobe]
Expand All @@ -91,10 +82,7 @@ fn try_{{crate_name}}(ctx: ProbeContext) -> Result<u32, u32> {
Ok(0)
}
{%- when "uretprobe" %}
use aya_ebpf::{
macros::uretprobe,
programs::RetProbeContext,
};
use aya_ebpf::{macros::uretprobe, programs::RetProbeContext};
use aya_log_ebpf::info;

#[uretprobe]
Expand All @@ -110,10 +98,7 @@ fn try_{{crate_name}}(ctx: RetProbeContext) -> Result<u32, u32> {
Ok(0)
}
{%- when "sock_ops" %}
use aya_ebpf::{
macros::sock_ops,
programs::SockOpsContext,
};
use aya_ebpf::{macros::sock_ops, programs::SockOpsContext};
use aya_log_ebpf::info;

#[sock_ops]
Expand All @@ -135,7 +120,6 @@ use aya_ebpf::{
programs::SkMsgContext,
};
use aya_log_ebpf::info;

use {{crate_name}}_common::SockKey;

#[map]
Expand Down Expand Up @@ -186,10 +170,7 @@ fn try_{{crate_name}}(ctx: TcContext) -> Result<i32, i32> {
Ok(TC_ACT_PIPE)
}
{%- when "cgroup_skb" %}
use aya_ebpf::{
macros::cgroup_skb,
programs::SkBuffContext,
};
use aya_ebpf::{macros::cgroup_skb, programs::SkBuffContext};
use aya_log_ebpf::info;

#[cgroup_skb]
Expand All @@ -205,10 +186,7 @@ fn try_{{crate_name}}(ctx: SkBuffContext) -> Result<i32, i32> {
Ok(0)
}
{%- when "tracepoint" %}
use aya_ebpf::{
macros::tracepoint,
programs::TracePointContext,
};
use aya_ebpf::{macros::tracepoint, programs::TracePointContext};
use aya_log_ebpf::info;

#[tracepoint]
Expand All @@ -224,10 +202,7 @@ fn try_{{crate_name}}(ctx: TracePointContext) -> Result<u32, u32> {
Ok(0)
}
{%- when "lsm" %}
use aya_ebpf::{
macros::lsm,
programs::LsmContext,
};
use aya_ebpf::{macros::lsm, programs::LsmContext};
use aya_log_ebpf::info;

#[lsm(hook = "{{lsm_hook}}")]
Expand All @@ -243,13 +218,10 @@ fn try_{{lsm_hook}}(ctx: LsmContext) -> Result<i32, i32> {
Ok(0)
}
{%- when "tp_btf" %}
use aya_ebpf::{
macros::btf_tracepoint,
programs::BtfTracePointContext,
};
use aya_ebpf::{macros::btf_tracepoint, programs::BtfTracePointContext};
use aya_log_ebpf::info;

#[btf_tracepoint(function="{{tracepoint_name}}")]
#[btf_tracepoint(function = "{{tracepoint_name}}")]
pub fn {{tracepoint_name}}(ctx: BtfTracePointContext) -> i32 {
match try_{{tracepoint_name}}(ctx) {
Ok(ret) => ret,
Expand All @@ -262,20 +234,14 @@ fn try_{{tracepoint_name}}(ctx: BtfTracePointContext) -> Result<i32, i32> {
Ok(0)
}
{%- when "socket_filter" %}
use aya_ebpf::{
macros::socket_filter,
programs::SkBuffContext,
};
use aya_ebpf::{macros::socket_filter, programs::SkBuffContext};

#[socket_filter]
pub fn {{crate_name}}(_ctx: SkBuffContext) -> i64 {
0
}
{%- when "cgroup_sysctl" %}
use aya_ebpf::{
macros::cgroup_sysctl,
programs::SysctlContext,
};
use aya_ebpf::{macros::cgroup_sysctl, programs::SysctlContext};
use aya_log_ebpf::info;

#[cgroup_sysctl]
Expand Down Expand Up @@ -310,7 +276,7 @@ fn try_{{crate_name}}(ctx: SockoptContext) -> Result<i32, i32> {
use aya_ebpf::{macros::raw_tracepoint, programs::RawTracePointContext};
use aya_log_ebpf::info;

#[raw_tracepoint(tracepoint="{{tracepoint_name}}")]
#[raw_tracepoint(tracepoint = "{{tracepoint_name}}")]
pub fn {{crate_name}}(ctx: RawTracePointContext) -> i32 {
match try_{{crate_name}}(ctx) {
Ok(ret) => ret,
Expand Down
32 changes: 19 additions & 13 deletions {{project-name}}/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ use aya::programs::UProbe;
{%- when "sock_ops" -%}
use aya::programs::{SockOps, links::CgroupAttachMode};
{%- when "sk_msg" -%}
use aya::maps::SockHash;
use aya::programs::SkMsg;
use aya::{maps::SockHash, programs::SkMsg};
use {{crate_name}}_common::SockKey;
{%- when "xdp" -%}
use anyhow::Context;
use anyhow::Context as _;
use aya::programs::{Xdp, XdpFlags};
{%- when "classifier" -%}
use aya::programs::{tc, SchedClassifier, TcAttachType};
{%- when "cgroup_skb" -%}
use aya::programs::{CgroupSkb, CgroupSkbAttachType, links::CgroupAttachMode};
use aya::programs::{links::CgroupAttachMode, CgroupSkb, CgroupSkbAttachType};
{%- when "cgroup_sysctl" -%}
use aya::programs::{CgroupSysctl, links::CgroupAttachMode};
use aya::programs::{links::CgroupAttachMode, CgroupSysctl};
{%- when "cgroup_sockopt" -%}
use aya::programs::{CgroupSockopt, links::CgroupAttachMode};
use aya::programs::{links::CgroupAttachMode, CgroupSockopt};
{%- when "tracepoint" -%}
use aya::programs::TracePoint;
{%- when "lsm" -%}
Expand All @@ -39,12 +38,12 @@ use aya::programs::SocketFilter;
{%- when "raw_tracepoint" -%}
use aya::programs::RawTracePoint;
{%- endcase %}
use aya::{include_bytes_aligned, Ebpf};
use aya_log::EbpfLogger;
{% if program_types_with_opts contains program_type -%}
use clap::Parser;
{% endif -%}
use log::{info, warn, debug};

#[rustfmt::skip]
use log::{debug, info, warn};
use tokio::signal;

{% if program_types_with_opts contains program_type -%}
Expand Down Expand Up @@ -85,8 +84,11 @@ async fn main() -> Result<(), anyhow::Error> {
// runtime. This approach is recommended for most real-world use cases. If you would
// like to specify the eBPF program at runtime rather than at compile-time, you can
// reach for `Bpf::load_file` instead.
let mut ebpf = Ebpf::load(include_bytes_aligned!(concat!(env!("OUT_DIR"), "/{{project-name}}")))?;
if let Err(e) = EbpfLogger::init(&mut ebpf) {
let mut ebpf = aya::Ebpf::load(aya::include_bytes_aligned!(concat!(
env!("OUT_DIR"),
"/test"
)))?;
if let Err(e) = aya_log::EbpfLogger::init(&mut ebpf) {
// This can happen if you remove all log statements from your eBPF program.
warn!("failed to initialize eBPF logger: {}", e);
}
Expand Down Expand Up @@ -115,7 +117,7 @@ async fn main() -> Result<(), anyhow::Error> {
program.load()?;
program.attach(cgroup, CgroupAttachMode::default())?;
{%- when "sk_msg" -%}
let sock_map: SockHash::<_, SockKey> = ebpf.map("{{sock_map}}").unwrap().try_into()?;
let sock_map: SockHash<_, SockKey> = ebpf.map("{{sock_map}}").unwrap().try_into()?;
let map_fd = sock_map.fd().try_clone()?;

let prog: &mut SkMsg = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?;
Expand All @@ -138,7 +140,11 @@ async fn main() -> Result<(), anyhow::Error> {
let program: &mut CgroupSkb = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?;
let cgroup = std::fs::File::open(opt.cgroup_path)?;
program.load()?;
program.attach(cgroup, CgroupSkbAttachType::{{direction}}, CgroupAttachMode::default())?;
program.attach(
cgroup,
CgroupSkbAttachType::{{direction}},
CgroupAttachMode::default(),
)?;
{%- when "tracepoint" -%}
let program: &mut TracePoint = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?;
program.load()?;
Expand Down

0 comments on commit 7d9e2ba

Please sign in to comment.