xdpsock

A Rust interface for the Linux AF_XDP address family.

API documentation.

For more information please see the networking docs or a more detailed overview.

This crate builds on the great work of xsk-rs.

Basic Usage

``` 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 = vec![]; xsk.send(&pkt);

// Receiving a packet let (recvd_pkt, len) = xsk.recv().expect("failed to recv"); ```

Examples

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: ```

Compile

cargo build --release --examples

Add the veth pair

sudo ip link add veth0 type veth peer name veth1

Get the MAC addresses of the veth pair

ip link show 11: veth1@veth0: mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 link/ether 82:ff:40:35:17:a2 brd ff:ff:ff:ff:ff:ff 12: veth0@veth1: mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 link/ether 9e:4f:30:9e:e1:31 brd ff:ff:ff:ff:ff:ff

Start the receiver

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

In another terminal, start the sender

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"

Running tests

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.

```

tests

cargo build --tests sudo runalltests.sh ```

Compatibility

Linux kernel version 5.11.0.

Check your driver for XDP compatibility