nlink - Rust IP utilities
A modern Rust implementation of Linux network management tools, providing both a library crate and CLI binaries.
Overview
nlink is a from-scratch implementation of Linux netlink-based network management. The primary goal is to provide a high-quality Rust library for programmatic network configuration, with CLI tools serving as proof-of-concept binaries.
Key design principles:
- Library-first: Core functionality lives in a single, well-designed crate
- Async/tokio-native: Built for async Rust from the ground up
- Custom netlink: No dependency on rtnetlink or netlink-packet-* crates
- Type-safe: Leverage Rust's type system for correctness
- Modern CLI: Not a drop-in replacement for iproute2 - free to improve
Installation
Add to your Cargo.toml:
# Core netlink functionality
= "0.1"
# With additional features
= { = "0.1", = ["sockdiag", "tuntap", "tc", "output"] }
# All features
= { = "0.1", = ["full"] }
Features
| Feature | Description |
|---|---|
sockdiag |
Socket diagnostics via NETLINK_SOCK_DIAG |
tuntap |
TUN/TAP device management |
tc |
Traffic control utilities |
output |
JSON/text output formatting |
namespace_watcher |
Filesystem-based namespace watching via inotify |
full |
All features enabled |
Using as a Library
use ;
use ;
async
Working with Network Namespaces
use ;
use namespace;
async
Namespace-aware Event Monitoring
use ;
async
Watching Namespace Changes
Two complementary approaches for monitoring network namespace lifecycle:
use ;
use ;
async
Namespace-aware TC Operations
For TC operations in namespaces, use the *_by_index methods to avoid reading
/sys/class/net/ from the host namespace:
use namespace;
use NetemConfig;
use Duration;
async
Reading Existing TC Configurations
The library provides strongly-typed parsing for qdisc options, useful for detecting existing TC configurations:
use ;
use QdiscOptions;
async
Monitoring TC Statistics
Track throughput and statistics changes over time:
use ;
use Duration;
async
Error Handling with Context
The library provides rich error types with context support for better debugging:
use ;
async
Library Modules
nlink::netlink - Core netlink functionality
- High-level API:
Connectionwith convenience query methods (get_links(),get_addresses(), etc.) - Link state management:
set_link_up(),set_link_down(),set_link_mtu(),del_link() - Namespace support:
Connection::new_in_namespace_path()andnamespacemodule helpers - Namespace watching:
NamespaceWatcher(inotify) andNamespaceEventSubscriber(netlink) - Event monitoring:
EventStreamfor real-time network change notifications - Strongly-typed messages:
LinkMessage,AddressMessage,RouteMessage,TcMessage - TC options parsing: Typed access to qdisc parameters (fq_codel, htb, tbf, netem, etc.)
- Netem loss models: Support for Gilbert-Intuitive and Gilbert-Elliot state-based loss
- Statistics tracking:
StatsSnapshotandStatsTrackerfor rate calculation - Error handling: Semantic error types with
is_not_found(),is_permission_denied(), etc. - Low-level access:
MessageBuilderfor custom netlink messages
nlink::util - Shared utilities
- Argument parsing (
get_u8,get_u16,get_u32,get_rate,get_size) - Address utilities (parse/format IP addresses and prefixes)
- Name resolution (protocol names, scope names, table names)
- Interface name/index mapping
nlink::sockdiag - Socket diagnostics (feature: sockdiag)
- Query TCP, UDP, Unix, and other socket types
- Filter by state, port, address, and other criteria
- Retrieve detailed socket information (memory, TCP info, etc.)
nlink::tuntap - TUN/TAP devices (feature: tuntap)
- Create and manage TUN/TAP virtual network devices
- Set device ownership and permissions
- Async read/write support
nlink::tc - Traffic control (feature: tc)
- Qdisc option builders for htb, fq_codel, tbf, netem, etc.
- Handle parsing and formatting
- Class and filter builders
nlink::output - Output formatting (feature: output)
- Text and JSON output modes
Printabletrait for consistent formatting- Configurable options (stats, details, color, numeric)
Binaries
ip
Network interface and routing management:
# List interfaces
# Create interfaces (each type is a subcommand with specific options)
# Delete interfaces
# Modify interfaces
# Show addresses
# Add/remove addresses
# Show routes
# Add/remove routes
# Show neighbors
# Add/remove neighbors
# Show policy routing rules
# Add/remove rules
# Query route for a destination
# Flush neighbor entries
# Monitor netlink events (link, address, route, neighbor changes)
# Multicast addresses
# VRF (Virtual Routing and Forwarding)
# XFRM (IPSec)
# Network namespaces
# Tunnels (GRE, IPIP, SIT, VTI)
tc
Traffic control (qdisc, class, filter):
# List qdiscs
# Add qdiscs with type-specific options
# Replace/change qdiscs
# Netem - network emulation (delay, loss, reorder, corrupt, duplicate)
# Delete qdiscs
# List classes
# Add HTB classes with rate limiting
# Monitor TC events
# List filters
Building
Requires Rust 1.85+ (edition 2024).
# Build all crates and binaries
# Run ip command
# Run tc command
Project Status
The library API is production-ready for network monitoring and querying. Currently implemented:
-
Core netlink socket and connection handling
-
Message building with nested attributes
-
Link operations (show, add, del, set)
-
Link types: dummy, veth, bridge, bond, vlan, vxlan, macvlan, ipvlan, vrf, gre, ipip, sit, wireguard
-
Address operations (show, add, del)
-
Route operations (show, add, del, replace)
-
Neighbor operations (show, add, del, replace)
-
Policy routing rules (ip rule show, add, del, flush)
-
Event monitoring (ip monitor) for link, address, route, neighbor changes
-
TC qdisc operations (show, add, del, replace, change)
-
TC qdisc types: fq_codel, htb, tbf, prio, sfq, netem (with full parameter support)
-
TC netem qdisc (delay, loss, reorder, corrupt, duplicate, rate limiting)
-
TC class operations with HTB parameters (rate, ceil, burst, prio, quantum)
-
TC monitor for qdisc/class/filter events
-
TC filter operations (show, add, del)
-
TC filter types: u32 (match ip/ip6/tcp/udp/icmp), flower, basic, fw, bpf
-
TC actions: gact (pass/drop/pipe), mirred (mirror/redirect), police (rate limiting)
-
Network namespace support (ip netns list, add, del, exec, identify, pids, monitor, set, attach)
-
Tunnel management (ip tunnel show, add, del, change) for GRE, IPIP, SIT, VTI
-
Route lookup (ip route get)
-
Neighbor flush (ip neigh flush)
-
Multicast addresses (ip maddress show)
-
VRF management (ip vrf show, exec, identify, pids)
-
XFRM/IPSec framework (ip xfrm state/policy show, count)
Library features:
- High-level event stream API (
EventStream,NetworkEvent) - Convenience query methods (
get_links(),get_addresses(),get_qdiscs(), etc.) - Link state management (
set_link_up(),set_link_down(),set_link_mtu(),del_link()) - Namespace-aware connections (
Connection::new_in_namespace_path(),namespacemodule) - Namespace-aware event monitoring (
EventStream::builder().namespace()) - Namespace-aware TC operations (
add_qdisc_by_index(), etc.) - Namespace watching (
NamespaceWatchervia inotify,NamespaceEventSubscribervia netlink) - Typed TC options parsing (fq_codel, htb, tbf, netem, prio, sfq)
- Statistics helpers with rate calculation (
StatsSnapshot,StatsTracker) - Thread-safe
Connection(Send + Sync)
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.