1use teto_dpdk::config::FStackConfig;
10use teto_dpdk::fstack::ffi::{
11 init_fstack, create_udp_socket, run_fstack, UdpMessage, FStackUdpSocket,
12};
13use cxx::UniquePtr;
14
15static mut GLOBAL_SOCKET: Option<*const FStackUdpSocket> = None;
16
17fn on_packet(_fd: i32, msg: &UdpMessage) {
18 println!(
19 "[UDP] Received {} bytes from {}:{}",
20 msg.payload.len(), msg.src_ip, msg.src_port
21 );
22 unsafe {
23 if let Some(ptr) = GLOBAL_SOCKET {
24 (*ptr).send_to(&msg.payload, &msg.src_ip, msg.src_port);
25 }
26 }
27}
28
29fn main() {
30 let cfg = FStackConfig::for_docker();
32
33 println!("Initializing F-Stack...");
34 init_fstack(&cfg.config_args(), &cfg.eal_args());
35
36 let bind_ip = "0.0.0.0".to_string();
37 let bind_port = 8080u16;
38
39 println!("Creating UDP socket on {}:{}...", bind_ip, bind_port);
40 let socket: UniquePtr<FStackUdpSocket> =
41 create_udp_socket(&bind_ip, bind_port, on_packet);
42
43 unsafe {
44 GLOBAL_SOCKET = Some(&*socket as *const FStackUdpSocket);
45 }
46
47 println!("Starting F-Stack UDP event loop...");
48 run_fstack(&socket);
49}