netmap-rs
netmap-rs provides safe, zero-cost abstractions for Netmap kernel-bypass networking in Rust. It aims to offer high-performance packet I/O by leveraging Netmap's efficient memory-mapped ring buffers.
Features
- Zero-copy packet I/O: Directly access packet buffers in memory shared with the kernel.
- High Performance: Designed for low-latency and high-throughput applications.
- Safe Abstractions: Provides a safe Rust API over the underlying
netmapC structures. - Feature Flags: Customizable build via feature flags (e.g.,
sysfor core Netmap functionality,tokio-asyncfor Tokio integration).
Prerequisites
System Requirements
IMPORTANT: This crate requires the Netmap C library to be installed on your system. Without it, the sys feature will not work.
Installing Netmap C Library
On Linux
-
Install build dependencies:
# Ubuntu/Debian # CentOS/RHEL -
Download and build netmap:
-
Load the kernel module:
# Verify it's loaded
On FreeBSD
Netmap is included by default in FreeBSD 11+. No additional installation is required.
Custom Installation Paths
If you installed netmap in a non-standard location, set the NETMAP_LOCATION environment variable:
# Then build your project
Adding netmap-rs to your project
To use netmap-rs in your project, add it to your Cargo.toml.
Crucially, for most use cases, you will need to enable the sys feature. This feature compiles and links against the necessary netmap C libraries and enables the core structures like NetmapBuilder, Netmap, TxRing, and RxRing.
[]
= { = "0.3", = ["sys"] }
If you intend to use netmap-rs with Tokio for asynchronous operations, you should also enable the tokio-async feature:
[]
= { = "0.3", = ["sys", "tokio-async"] }
Basic Usage Example
Here's a basic example of how to open a Netmap interface, send, and receive a packet. This example assumes you have a loopback interface or a setup where packets sent on an interface can be received on it.
use *;
use sleep;
use Duration;
Troubleshooting
Build Errors
"netmap_user.h not found"
This means the Netmap C library is not installed or not found. Make sure to:
- Install the Netmap C library (see Prerequisites section)
- Set
NETMAP_LOCATIONif installed in a non-standard path
"undefined reference to nm_open"
This indicates the Netmap library is not being linked properly. Ensure:
- The
sysfeature is enabled in Cargo.toml - Netmap is properly installed with the library files
Feature Flag Issues
If you get errors like "NetmapBuilder not found", make sure you have enabled the sys feature:
[]
= { = "0.3", = ["sys"] }
Runtime Errors
"Failed to open interface"
Common causes:
-
Permission issues: You need root/sudo access to use netmap
-
Interface doesn't exist: Check available interfaces with:
-
Netmap kernel module not loaded:
-
Driver not supported: Not all network drivers support netmap. Check supported drivers:
"Operation would block"
This is normal behavior when the ring buffer is full or empty. Implement proper retry logic in your application.
Advanced Usage
Thread-per-Ring Pattern
For maximum performance, dedicate threads to individual rings:
use *;
use thread;
Async Support
Enable the tokio-async feature for async/await support:
use *;
use ;
async
Examples
The examples/ directory contains several complete examples:
ping_pong.rs- Basic send/receive examplesliding_window_arq.rs- Reliable delivery with ARQfec.rs- Forward Error Correctionthread_per_ring.rs- Thread-per-ring pattern
Run examples with:
Performance Tips
- Use batch operations where possible to amortize system call overhead
- Pin threads to cores using
core_affinityfor consistent performance - Pre-allocate buffers to avoid allocation during packet processing
- Use multiple rings to leverage multi-core systems
- Consider NUMA topology when pinning threads to cores
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
AUTHOR
-
Meshack Bahati Ouma - CS major (Maseno University (Kenya))
-
Email: bahatikylemeshack@gmail.com
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgments
- The Netmap project for the excellent kernel-bypass networking framework
- The Rust community for the safe systems programming language