[][src]Crate rxdp

Rust bindings for working with XDP programs & eBPF maps.

This library has bindings for some of the common, basic operations needed when working with XDP programs & eBPF maps from user-space. It is built on top of libbpf-sys. At the time of writing, it supports only a subset of all the possibe eBPF map types (see tests directory for a good indication of which maps are supported).

Prerequisites

Examples

Create an object from an ELF file

use rxdp;

let obj_path = "/path/to/elf/file";
let obj = match rxdp::XDPObject::new(obj_path) {
    Ok(obj) => {
        println!("Successfully created object from {}", obj_path);
        obj
    },
    Err(err) => panic!("{:?}", err),
};

Set the pinned maps.

Maps that have already been pinned will be loaded from the fs, provided the map name matches the name in the fs. Any new maps in the HashSet will set the pin path so that once the program is loaded, they will get automatically pinned.

let mut pinned_maps = HashSet::new();
let pin_path = None; // Will default to /sys/fs/bpf
pinned_maps.insert("my_map_name".to_string());
obj.pinned_maps(pinned_maps, pin_path).unwrap();

Load the object (programs + maps) into the kernel.

This will consume the XDPObject created above and return an XDPLoadedObject.

let obj = obj.load().unwrap();

Get a reference to a specific XDP program and attach it to an interface

let dev = "eth0";
let flags = rxdp::AttachFlags::SKB_MODE;

let prog = obj.get_program("prog_name").unwrap();
match prog.attach_to_interface(dev, flags) {
    Ok(_) => println!("Successfully attached to {}", dev),
    Err(e) => panic!("{:?}", e),
}

Get access to an underlying eBPF Map

let m: rxdp::Map<u32, u64> = match rxdp::Map::new(&obj, "map_name") {
    Ok(m) => m,
    Err(e) => panic!("{:?}", e),
};

NOTE: the key/value sizes MUST match the key/value sizes defined in the eBPF code, otherwise creating the map will fail.

Perform map operations

let key = 0u32;
let value = 1000u64;
m.update(&key, &value, rxdp::MapFlags::BpfAny).unwrap();
let got = m.lookup(&key).unwrap();
assert_eq!(value, got);

// iterate through all items
for kv in m.items().unwrap() {
    println!("key: {}, value: {}", kv.key, kv.value);
}

Batching support (kernel dependent)

If the kernel supports it, you can do batch operations for update/lookups. You can see if batching is supported:

let m: rxdp::Map<u32, u64> = match rxdp::Map::new(&obj, "map_name").unwrap();
println!("batching supported: {}", m.batching_supported());

Structs

AttachFlags

Flags that control how the XDP program is attached to the interface.

BatchResult

The result of a batch operation.

KeyValue

Holds key/value pair when getting all items from a map.

Map

Handles interacting with the underlying eBPF map.

XDPError

Error information about the attempted BPF operation

XDPLoadedObject

Struct for an XDP object that has been loaded

XDPObject

Convenience wrapper around an XDP object

XDPProgram

Convenience wrapper around an XDP program

Enums

MapFlags

Flags that control map update behaviour.

MapType

Valid eBPF map types

Functions

load_pinned_object

Load a pinned object from a path. Returns the object fd.

Type Definitions

XDPResult