Setup development environment with libbpf

This commit is contained in:
h3xduck
2021-11-20 21:07:23 -05:00
parent 8e7fd92dc4
commit 53da2d141d
313 changed files with 563362 additions and 0 deletions

View File

@@ -0,0 +1 @@
../../../../../vmlinux/vmlinux.h

View File

@@ -0,0 +1,15 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
SEC("xdp")
int xdp_pass(struct xdp_md *ctx)
{
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
int pkt_sz = data_end - data;
bpf_printk("packet size: %d", pkt_sz);
return XDP_PASS;
}
char __license[] SEC("license") = "GPL";

View File

@@ -0,0 +1,57 @@
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::{thread, time};
use anyhow::{bail, Result};
use structopt::StructOpt;
#[path = "bpf/.output/xdppass.skel.rs"]
mod xdppass;
use xdppass::*;
#[derive(Debug, StructOpt)]
struct Command {
/// Interface index to attach XDP program
#[structopt(default_value = "0")]
ifindex: i32,
}
fn bump_memlock_rlimit() -> Result<()> {
let rlimit = libc::rlimit {
rlim_cur: 128 << 20,
rlim_max: 128 << 20,
};
if unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlimit) } != 0 {
bail!("Failed to increase rlimit");
}
Ok(())
}
fn main() -> Result<()> {
let opts = Command::from_args();
bump_memlock_rlimit()?;
let skel_builder = XdppassSkelBuilder::default();
let open_skel = skel_builder.open()?;
let mut skel = open_skel.load()?;
let link = skel.progs_mut().xdp_pass().attach_xdp(opts.ifindex)?;
skel.links = XdppassLinks {
xdp_pass: Some(link),
};
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
})?;
while running.load(Ordering::SeqCst) {
eprint!(".");
thread::sleep(time::Duration::from_secs(1));
}
Ok(())
}