fix: Fix AF_XDP not working (#9)

This commit is contained in:
DaLaw2 2025-10-15 15:51:37 +08:00 committed by GitHub
parent 97d511d17a
commit d93b43f463
8 changed files with 51 additions and 44 deletions

2
.gitignore vendored
View File

@ -11,4 +11,6 @@ target/
.idea
logs
.env
.log
.txt
net-guardia/static/web

View File

@ -16,3 +16,6 @@ which = "8.0.0"
[[bin]]
name = "net-guardia-egress"
path = "src/main.rs"
test = false
doctest = false
bench = false

View File

@ -16,3 +16,6 @@ which = "8.0.0"
[[bin]]
name = "net-guardia-ingress"
path = "src/main.rs"
test = false
doctest = false
bench = false

View File

@ -19,7 +19,7 @@ static PROGRAM_ARRAY: ProgramArray = ProgramArray::with_max_entries(8, 0);
#[map]
static PARSED_PACKET: PerCpuArray<Event> = PerCpuArray::with_max_entries(1, 0);
#[map]
static XSKS_MAP: XskMap = XskMap::with_max_entries(64, 0);
static XSKS_MAP: XskMap = XskMap::pinned(64, 0);
#[xdp]
pub fn net_guardia(ctx: XdpContext) -> u32 {

View File

@ -259,17 +259,9 @@ fn build_frontend() {
panic!("FRONTEND_DIR environment variable is required but not set");
};
let Some(node_bin_dir) = env::var_os("NODE_PATH") else {
panic!("NODE_BIN_DIR environment variable is required but not set");
};
let project_root = env::var("CARGO_MANIFEST_DIR").unwrap();
let static_dir = PathBuf::from(project_root).join("static").join("web");
let frontend_dir = PathBuf::from(frontend_dir);
let node_bin_dir = PathBuf::from(node_bin_dir);
let npm_path = node_bin_dir.join("npm");
let npx_path = node_bin_dir.join("npx");
if !frontend_dir.exists() {
panic!("Frontend directory {:?} does not exist", frontend_dir);
@ -305,17 +297,9 @@ fn build_frontend() {
return;
}
let current_path = env::var("PATH").unwrap_or_default();
let new_path = if current_path.is_empty() {
node_bin_dir.to_string_lossy().to_string()
} else {
format!("{}:{}", node_bin_dir.to_string_lossy(), current_path)
};
let mut cmd = Command::new(&npm_path);
let mut cmd = Command::new("npm");
cmd.arg("install")
.current_dir(&frontend_dir)
.env("PATH", &new_path);
.current_dir(&frontend_dir);
let status = cmd
.status()
@ -324,10 +308,9 @@ fn build_frontend() {
panic!("npm install failed with exit code: {:?}", status.code());
}
let mut cmd = Command::new(&npx_path);
let mut cmd = Command::new("npx");
cmd.args(["next", "build"])
.current_dir(&frontend_dir)
.env("PATH", &new_path);
.current_dir(&frontend_dir);
let status = cmd
.status()

View File

@ -1,7 +1,7 @@
pub mod access_control;
pub mod service;
pub mod statistics;
pub mod xdp_manager;
pub mod xsk_manager;
use std::sync::Arc;
@ -13,7 +13,7 @@ use tokio::sync::oneshot;
use crate::core::ebpf::access_control::AccessControl;
use crate::core::ebpf::service::Service;
use crate::core::ebpf::statistics::Statistics;
use crate::core::ebpf::xdp_manager::XskManager;
use crate::core::ebpf::xsk_manager::XskManager;
use crate::core::infrastructure::app_config::AppConfig;
use crate::model::error::system::SystemError;
use crate::model::error::Error;

View File

@ -1,3 +1,4 @@
use std::error::Error as StdError;
use std::ffi::CString;
use std::num::NonZero;
use std::os::fd::AsRawFd;
@ -12,7 +13,7 @@ use parking_lot::Mutex;
use tokio::select;
use tokio::sync::oneshot;
use tokio::time::sleep;
use xsk_rs::config::{BindFlags, FrameSize, Interface, QueueSize, SocketConfig, UmemConfig};
use xsk_rs::config::{BindFlags, FrameSize, Interface, QueueSize, SocketConfig, UmemConfig, LibbpfFlags};
use xsk_rs::{CompQueue, FillQueue, FrameDesc, RxQueue, Socket, TxQueue, Umem};
use crate::core::infrastructure::app_config::AppConfig;
@ -94,7 +95,8 @@ impl Xsk {
let socket_config = SocketConfig::builder()
.tx_queue_size(tx_queue_size)
.rx_queue_size(rx_queue_size)
.bind_flags(BindFlags::empty())
.bind_flags(BindFlags::XDP_ZEROCOPY)
.libbpf_flags(LibbpfFlags::XSK_LIBBPF_FLAGS_INHIBIT_PROG_LOAD)
.build();
let interface = Interface::new(ifname);

View File

@ -5,24 +5,38 @@ use crate::utils::static_files::StaticFiles;
pub async fn default_route(req: HttpRequest) -> impl Responder {
let request_path = req.path();
let request_path = if request_path == "/" {
"/index.html"
let file_system_path = if request_path == "/" {
"web/index.html".to_string()
} else {
&request_path
format!("web{}", request_path)
};
let file_system_path = format!("web{}", request_path);
match StaticFiles::get(&*file_system_path) {
Some(content) => {
let mime_type = from_path(file_system_path).first_or_octet_stream();
HttpResponse::Ok()
.content_type(mime_type.as_ref())
.body(content.data.into_owned())
}
None => match StaticFiles::get("index.html") {
Some(index) => HttpResponse::Ok()
.content_type("text/html")
.body(index.data.into_owned()),
None => HttpResponse::NotFound().body("404 Not Found"),
},
if let Some(content) = StaticFiles::get(&file_system_path) {
let mime_type = from_path(&file_system_path).first_or_octet_stream();
return HttpResponse::Ok()
.content_type(mime_type.as_ref())
.body(content.data.into_owned());
}
}
let html_path = format!("{}.html", file_system_path);
if let Some(content) = StaticFiles::get(&html_path) {
return HttpResponse::Ok()
.content_type("text/html")
.body(content.data.into_owned());
}
let index_path = format!("{}/index.html", file_system_path);
if let Some(content) = StaticFiles::get(&index_path) {
return HttpResponse::Ok()
.content_type("text/html")
.body(content.data.into_owned());
}
match StaticFiles::get("web/404.html") {
Some(page) => HttpResponse::NotFound()
.content_type("text/html")
.body(page.data.into_owned()),
None => HttpResponse::NotFound().body("404 Not Found"),
}
}