A Rust interface for the Linux AF_XDP address family.
For more information please see the networking docs or a more detailed overview.
This crate builds on the great work of xsk-rs.
``` use xdpsock::{ socket::{BindFlags, SocketConfig, SocketConfigBuilder, XdpFlags}, umem::{UmemConfig, UmemConfigBuilder}, xsk::Xsk2, };
// Configuration let umemconfig = UmemConfigBuilder::new() .framecount(8192) .compqueuesize(4096) .fillqueuesize(4096) .build() .unwrap();
let socketconfig = SocketConfigBuilder::new() .txqueuesize(4096) .rxqueuesize(4096) .bindflags(BindFlags::XDPCOPY) // Check your driver to see if you can use ZEROCOPY .xdpflags(XdpFlags::XDPFLAGSSKBMODE) // Check your driver to see if you can use DRV_MODE .build() .unwrap();
let ntxframes = umemconfig.framecount() / 2;
let mut xsk = Xsk2::new(&ifname, 0, umemconfig, socketconfig, ntxframes as usize);
// Sending a packet
let pkt: Vec
// Receiving a packet let (recvd_pkt, len) = xsk.recv().expect("failed to recv"); ```
A couple can be found in the examples
directory.
The example dev2_to_dev1.rs
sends/receives udp packets using the specified
interface. To run this example:
```
cargo build --release --examples
sudo ip link add veth0 type veth peer name veth1
ip link show
11: veth1@veth0:
sudo ./target/release/examples/dev2todev1 --n-pkts=1000000 --src-ip "192.168.69.1" --src-port 1234 --dest-ip "192.168.69.2" --dest-port 4321 --dev veth1 --dest-mac "82:ff:40:35:17:a2" --src-mac "9e:4f:30:9e:e1:31" rx
sudo ./target/release/examples/dev2todev1 --n-pkts=1000000 --src-ip "192.168.69.1" --src-port 1234 --dest-ip "192.168.69.2" --dest-port 4321 --dev veth0 --dest-mac "82:ff:40:35:17:a2" --src-mac "9e:4f:30:9e:e1:31" tx ```
The example synacker.rs
listens on the specified interface and responds to TCP
syn packets matching a given filter with a SYNACK.
sudo ./target/release/examples/synacker --src-ip "192.168.69.1" --dev "veth1"
The integration tests run using veth-util-rs,
which creates a pair of virtual ethernet interfaces. By default, root
permissions are required to create virtual ethernet interfaces. To avoid
running cargo under root
it's best to first build the tests/examples and run
the binaries directly.
```
cargo build --tests sudo runalltests.sh ```
Linux kernel version 5.11.0.