Add template for socket_filter program type

This patch adds template for socket_filter.
e.g.

```sh
cargo generate --path ~/dev/aya-template \
         --name my-test \
	 -d program_type=socket_filter
```
pull/33/head
Kenjiro Nakayama 3 years ago
parent c41fb5ef0d
commit 8dcda393c4

@ -26,6 +26,7 @@ jobs:
- uprobe - uprobe
- uretprobe - uretprobe
- sock_ops - sock_ops
- sock_filter
- sk_msg - sk_msg
- xdp - xdp
- classifier - classifier

@ -13,6 +13,7 @@ choices = [
"uprobe", "uprobe",
"uretprobe", "uretprobe",
"sock_ops", "sock_ops",
"sock_filter",
"sk_msg", "sk_msg",
"xdp", "xdp",
"classifier", "classifier",

@ -245,6 +245,16 @@ pub fn {{tracepoint_name}}(ctx: BtfTracePointContext) -> i32 {
unsafe fn try_{{tracepoint_name}}(_ctx: BtfTracePointContext) -> Result<i32, i32> { unsafe fn try_{{tracepoint_name}}(_ctx: BtfTracePointContext) -> Result<i32, i32> {
Ok(0) Ok(0)
} }
{%- when "socket_filter" %}
use aya_bpf::{
macros::socket_filter,
programs::SkBuffContext,
};
#[socket_filter(name="{{crate_name}}")]
pub fn {{crate_name}}(_ctx: SkBuffContext) -> i64 {
return 0
}
{%- endcase %} {%- endcase %}
#[panic_handler] #[panic_handler]

@ -28,6 +28,10 @@ use aya::programs::TracePoint;
use aya::{programs::Lsm, Btf}; use aya::{programs::Lsm, Btf};
{%- when "tp_btf" -%} {%- when "tp_btf" -%}
use aya::{programs::BtfTracePoint, Btf}; use aya::{programs::BtfTracePoint, Btf};
{%- when "socket_filter" -%}
use std::net::TcpStream;
use std::os::unix::io::AsRawFd;
use aya::programs::SocketFilter;
{%- endcase %} {%- endcase %}
use log::info; use log::info;
use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode}; use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
@ -131,6 +135,11 @@ async fn main() -> Result<(), anyhow::Error> {
let program: &mut BtfTracePoint = bpf.program_mut("{{tracepoint_name}}").unwrap().try_into()?; let program: &mut BtfTracePoint = bpf.program_mut("{{tracepoint_name}}").unwrap().try_into()?;
program.load("{{tracepoint_name}}", &btf)?; program.load("{{tracepoint_name}}", &btf)?;
program.attach()?; program.attach()?;
{%- when "socket_filter" -%}
let client = TcpStream::connect("127.0.0.1:1234")?;
let prog: &mut SocketFilter = bpf.program_mut("{{crate_name}}").unwrap().try_into()?;
prog.load()?;
prog.attach(client.as_raw_fd())?;
{%- endcase %} {%- endcase %}
info!("Waiting for Ctrl-C..."); info!("Waiting for Ctrl-C...");

Loading…
Cancel
Save