Merge pull request #15 from willfindlay/new-lsm-tp-btf

update lsm template and add template for tp_btf
pull/16/head
Alessandro Decina 3 years ago committed by GitHub
commit 509073c454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -30,6 +30,7 @@ jobs:
- cgroup_skb - cgroup_skb
- tracepoint - tracepoint
- lsm - lsm
- tp_btf
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2

@ -5,7 +5,7 @@ ignore = [".github", "test.sh"]
[placeholders.program_type] [placeholders.program_type]
type = "string" type = "string"
prompt = "Which type of eBPF program?" prompt = "Which type of eBPF program?"
choices = ["kprobe", "kretprobe", "uprobe", "uretprobe", "sock_ops", "sk_msg", "xdp", "classifier", "cgroup_skb", "tracepoint", "lsm"] choices = ["kprobe", "kretprobe", "uprobe", "uretprobe", "sock_ops", "sk_msg", "xdp", "classifier", "cgroup_skb", "tracepoint", "lsm", "tp_btf"]
default = "xdp" default = "xdp"
[conditional.'program_type == "kprobe" || program_type == "kretprobe"'.placeholders.kprobe] [conditional.'program_type == "kprobe" || program_type == "kretprobe"'.placeholders.kprobe]
@ -35,7 +35,7 @@ type = "string"
prompt = "Which tracepoint category? (e.g sched, net etc...)" prompt = "Which tracepoint category? (e.g sched, net etc...)"
regex = "[a-z]+" regex = "[a-z]+"
[conditional.'program_type == "tracepoint"'.placeholders.tracepoint_name] [conditional.'program_type == "tracepoint" || program_type == "tp_btf"'.placeholders.tracepoint_name]
type = "string" type = "string"
prompt = "Which tracepoint name? (e.g sched_switch, net_dev_queue)" prompt = "Which tracepoint name? (e.g sched_switch, net_dev_queue)"
regex = "[a-z]+" regex = "[a-z]+"

@ -33,6 +33,9 @@ case "$PROG_TYPE" in
"lsm") "lsm")
ADDITIONAL_ARGS="-d lsm_hook=file_open" ADDITIONAL_ARGS="-d lsm_hook=file_open"
;; ;;
"tp_btf")
ADDITIONAL_ARGS="-d tracepoint_name=net_dev_queue"
;;
*) *)
ADDITIONAL_ARGS='' ADDITIONAL_ARGS=''
esac esac

@ -194,6 +194,23 @@ pub fn {{lsm_hook}}(ctx: LsmContext) -> i32 {
unsafe fn try_{{lsm_hook}}(_ctx: LsmContext) -> Result<i32, i32> { unsafe fn try_{{lsm_hook}}(_ctx: LsmContext) -> Result<i32, i32> {
Ok(0) Ok(0)
} }
{%- when "tp_btf" %}
use aya_bpf::{
macros::btf_tracepoint,
programs::BtfTracePointContext,
};
#[btf_tracepoint(name="{{tracepoint_name}}")]
pub fn {{tracepoint_name}}(ctx: BtfTracePointContext) -> i32 {
match unsafe { try_{{tracepoint_name}}(ctx) } {
Ok(ret) => ret,
Err(ret) => ret,
}
}
unsafe fn try_{{tracepoint_name}}(_ctx: BtfTracePointContext) -> Result<i32, i32> {
Ok(0)
}
{%- endcase %} {%- endcase %}
#[panic_handler] #[panic_handler]

@ -19,7 +19,9 @@ use aya::programs::{CgroupSkb, CgroupSkbAttachType};
{%- when "tracepoint" -%} {%- when "tracepoint" -%}
use aya::programs::TracePoint; use aya::programs::TracePoint;
{%- when "lsm" -%} {%- when "lsm" -%}
use aya::programs::Lsm; use aya::{programs::Lsm, Btf};
{%- when "tp_btf" -%}
use aya::{programs::BtfTracePoint, Btf};
{%- endcase %} {%- endcase %}
use std::{ use std::{
convert::{TryFrom,TryInto}, convert::{TryFrom,TryInto},
@ -94,8 +96,14 @@ fn try_main() -> Result<(), anyhow::Error> {
program.load()?; program.load()?;
program.attach("{{tracepoint_category}}", "{{tracepoint_name}}")?; program.attach("{{tracepoint_category}}", "{{tracepoint_name}}")?;
{%- when "lsm" -%} {%- when "lsm" -%}
let btf = Btf::from_sys_fs()?;
let program: &mut Lsm = bpf.program_mut("{{lsm_hook}}")?.try_into()?; let program: &mut Lsm = bpf.program_mut("{{lsm_hook}}")?.try_into()?;
program.load("{{lsm_hook}}")?; program.load("{{lsm_hook}}", &btf)?;
program.attach()?;
{%- when "tp_btf" -%}
let btf = Btf::from_sys_fs()?;
let program: &mut BtfTracePoint = bpf.program_mut("{{tracepoint_name}}")?.try_into()?;
program.load("{{tracepoint_name}}", &btf)?;
program.attach()?; program.attach()?;
{%- endcase %} {%- endcase %}

Loading…
Cancel
Save