Async netlink library for Linux network configuration.
This crate provides a complete netlink implementation for programmatic network management on Linux. It supports RTNetlink (routing), traffic control, socket diagnostics, and TUN/TAP device management.
Features
sockdiag- Socket diagnostics via NETLINK_SOCK_DIAGtuntap- TUN/TAP device managementtuntap-async- Async TUN/TAP support (impliestuntap)tc- Traffic control utilitiesoutput- JSON/text output formattingfull- All features enabled
Example
use nlink::netlink::{Connection, Protocol};
#[tokio::main]
async fn main() -> nlink::netlink::Result<()> {
let conn = Connection::new(Protocol::Route)?;
// Query interfaces
let links = conn.get_links().await?;
for link in links {
println!("{}: {}", link.ifindex(), link.name.as_deref().unwrap_or("?"));
}
Ok(())
}
Link State Management
use nlink::netlink::{Connection, Protocol};
let conn = Connection::new(Protocol::Route)?;
// Bring an interface up
conn.set_link_up("eth0").await?;
// Bring it down
conn.set_link_down("eth0").await?;
// Set MTU
conn.set_link_mtu("eth0", 9000).await?;
Network Namespace Support
Operations can be performed in specific network namespaces:
use nlink::netlink::{Connection, Protocol};
use nlink::netlink::namespace;
// Connect to a named namespace (created via `ip netns add myns`)
let conn = namespace::connection_for("myns")?;
let links = conn.get_links().await?;
// Or connect to a container's namespace
let conn = namespace::connection_for_pid(container_pid)?;
let links = conn.get_links().await?;
// Or use a path directly
let conn = Connection::new_in_namespace_path(
Protocol::Route,
"/proc/1234/ns/net"
)?;
Event Monitoring
use nlink::netlink::events::{EventStream, NetworkEvent};
let mut stream = EventStream::builder()
.links(true)
.addresses(true)
.build()?;
while let Some(event) = stream.next().await? {
match event {
NetworkEvent::NewLink(link) => println!("New link: {:?}", link.name),
NetworkEvent::NewAddress(addr) => println!("New address: {:?}", addr.address),
_ => {}
}
}