Skip to content

chenhengqi/libxdp-sys

Repository files navigation

libxdp-sys

Rust FFI bindings for the libxdp C library, enabling safe and efficient interaction with XDP (eXpress Data Path) features from Rust.

Overview

libxdp-sys provides low-level, auto-generated Rust bindings to the libxdp C library. These bindings allow Rust programs to interact with XDP objects, manage XDP programs, and utilize advanced networking features exposed by libxdp.

This crate is intended for use as a building block for higher-level Rust libraries and applications that require direct access to XDP functionality. It is not a high-level API; instead, it exposes the raw C interface as closely as possible.

Features

  • Bindings to libxdp and related XDP/BPF types and functions
  • Optional vendored build of libbpf via the vendored feature
  • By default uses precompiled BPF objects via the use_precompiled_bpf feature
  • Compatible with modern Rust editions

Usage

Add libxdp-sys to your Cargo.toml:

[dependencies]
libxdp-sys = "0.2"

Enable optional features as needed:

[features]
vendored = ["libbpf-sys/vendored"]
vendored-libelf = ["libbpf-sys/vendored-libelf"]
use_precompiled_bpf = [] # Use precompiled BPF objects instead of building at compile time

use_precompiled_bpf Feature

If you enable the use_precompiled_bpf feature, the crate will use prebuilt BPF object files (found in src/prebuilt_ebpf) instead of compiling BPF code during the build process. This can speed up builds and improve reproducibility, especially in CI environments or when cross-compiling.

Example usage:

use libxdp_sys::*;

// Access a constant
let headroom = XDP_PACKET_HEADROOM;

// Use a struct and call a method
let insn = bpf_insn {
	code: BPF_LD as u8,
	_bitfield_align_1: [],
	_bitfield_1: bpf_insn::new_bitfield_1(1, 2),
	off: 0,
	imm: 0,
};
let dst_reg = insn.dst_reg();

Building

This crate uses a build.rs script to generate bindings at build time. For most users, enabling the vendored feature will build libbpf from source automatically.

Testing

Run integration tests with:

cargo test

License

libxdp-sys is dual-licensed under LGPL-2.1 or BSD-2-Clause, matching the upstream libxdp library.

Links

Authors

About

Rust bindings for libxdp

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

Languages