Skip to main content

teto_dpdk/
fstack.rs

1#[cxx::bridge(namespace = "teto")]
2pub mod ffi {
3    // --- UDP ---
4
5    struct UdpMessage {
6        payload:  Vec<u8>,
7        src_ip:   String,
8        src_port: u16,
9    }
10
11    // --- TCP ---
12
13    struct TcpMessage {
14        payload:  Vec<u8>,
15        src_ip:   String,
16        src_port: u16,
17    }
18
19    /// Flat representation of TcpSocketOptions for the cxx bridge.
20    /// `-1` means "not set / use default". `0` = false, `1` = true for booleans.
21    struct TcpSocketOptionsFfi {
22        nodelay:                 i32,
23        keepalive:               i32,
24        keepalive_idle_secs:     i32,
25        keepalive_interval_secs: i32,
26        keepalive_count:         i32,
27        recv_buf:                i32,
28        send_buf:                i32,
29        linger_secs:             i32,
30        quickack:                i32,
31        reuse_port:              i32,
32    }
33
34    unsafe extern "C++" {
35        include!("fstack_wrapper.h");
36
37        // --- Shared init ---
38
39        /// `config_args` is passed to `ff_load_config` (e.g. `["teto", "--conf", "config.ini"]`).
40        /// `eal_args` is injected into `dpdk_argv` before `ff_dpdk_init` (e.g. `["--no-pci"]`).
41        fn init_fstack(config_args: &Vec<String>, eal_args: &Vec<String>);
42
43        // --- UDP ---
44
45        type FStackUdpSocket;
46
47        fn create_udp_socket(
48            ip:       &String,
49            port:     u16,
50            callback: fn(i32, &UdpMessage),
51        ) -> UniquePtr<FStackUdpSocket>;
52
53        fn send_to(self: &FStackUdpSocket, payload: &[u8], dest_ip: &String, dest_port: u16);
54
55        fn run_fstack(socket: &FStackUdpSocket);
56
57        // --- TCP ---
58
59        type FStackTcpListener;
60
61        fn create_tcp_listener(
62            ip:            &String,
63            port:          u16,
64            opts:          &TcpSocketOptionsFfi,
65            on_connect:    fn(i32, &String, u16),
66            on_data:       fn(i32, &TcpMessage),
67            on_disconnect: fn(i32),
68        ) -> UniquePtr<FStackTcpListener>;
69
70        fn send_to(self: &FStackTcpListener, fd: i32, payload: &[u8]);
71        fn close_connection(self: &FStackTcpListener, fd: i32);
72
73        fn run_fstack_tcp(listener: &FStackTcpListener);
74
75        /// Register a callback invoked on every F-Stack TCP poll iteration,
76        /// before accept/read. Used by teto-tokio to drain the write command queue.
77        fn set_tcp_tick_callback(cb: fn());
78
79        /// Register a callback invoked on every F-Stack UDP poll iteration,
80        /// before read. Used by teto-tokio to drain the send command queue.
81        fn set_udp_tick_callback(cb: fn());
82    }
83}