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
netmap
C structures. - Feature Flags: Customizable build via feature flags (e.g.,
sys
for core Netmap functionality,tokio-async
for Tokio integration).
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.1.2", = ["sys"] }
# Replace "0.1.2" with the desired version from crates.io
If you intend to use netmap-rs
with Tokio for asynchronous operations, you should also enable the tokio-async
feature:
[]
= { = "0.1.2", = ["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 NetmapBuilder; // Or use netmap_rs::prelude::*;
use Error; // If not using prelude
use sleep;
use Duration;
Note on Netmap Setup:
Using netmap-rs
(with the sys
feature) requires that your system has the Netmap kernel module installed, along with its userspace C libraries and development headers. You will also need clang
installed, as it's used by the build process to generate bindings.
Please refer to the official Netmap project page or the Netmap GitHub repository for instructions on how to compile and install Netmap on your operating system. You may also need appropriate permissions to access network interfaces via Netmap.
Troubleshooting Build Issues (Netmap C Library Not Found):
If the build fails with errors indicating that net/netmap_user.h
or the Netmap library cannot be found, it means the build script for the netmap-min-sys
dependency could not locate your Netmap C installation.
- Standard Paths: The build script checks standard locations like
/usr/local
. - Custom Installation Path: If you have installed Netmap in a custom directory (e.g.,
/opt/netmap
), you need to inform the build system by setting theNETMAP_LOCATION
environment variable before runningcargo build
:
ReplaceNETMAP_LOCATION=/opt/netmap
/opt/netmap
with the root directory of your Netmap installation (this directory should containinclude
andlib
subdirectories for Netmap). - Further Details: For more information on build-time environment variables like
NETMAP_LOCATION
andDISABLE_NETMAP_KERNEL
(for compiling without Netmap), refer to theREADME.md
file within thenetmap-min-sys
crate.
Building Examples
The crate includes several examples in the examples/
directory. To run an example, ensure you have the sys
feature enabled for the crate when building:
(If netmap-rs
is the current crate, you might not need the netmap-rs/
prefix for features).
For example, to run ping_pong
:
# Or if it's a dependency:
# cargo run --example ping_pong --features netmap-rs/sys
Make sure to adapt the interface names used within the examples to your specific setup.
License
This crate is licensed under
- Apache License, Version 2.0, (LICENSE-APACHE).
- MIT license (LICENSE-MIT).