This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LinkAddRequest: Allow adding macvlan on a link
Following adds support to create MACVLAN on a link. This is equivalent to `ip link add NAME name link LINK type macvlan mode MACVLAN_MODE`. But instead of specifying a link name (LINK), we specify a link index and a mode. Veth must be already up for this to work. Following behaviour is expected cause its same for `vxlan` and `vlan`. Signed-off-by: Aditya Rajan <arajan@redhat.com>
- Loading branch information
Showing
2 changed files
with
71 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use futures::stream::TryStreamExt; | ||
use rtnetlink::{new_connection, Error, Handle}; | ||
use std::env; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), String> { | ||
let args: Vec<String> = env::args().collect(); | ||
if args.len() != 2 { | ||
usage(); | ||
return Ok(()); | ||
} | ||
let link_name = &args[1]; | ||
|
||
let (connection, handle, _) = new_connection().unwrap(); | ||
tokio::spawn(connection); | ||
|
||
create_macvlan(handle, link_name.to_string()) | ||
.await | ||
.map_err(|e| format!("{}", e)) | ||
} | ||
|
||
async fn create_macvlan(handle: Handle, veth_name: String) -> Result<(), Error> { | ||
let mut links = handle | ||
.link() | ||
.get() | ||
.set_name_filter(veth_name.clone()) | ||
.execute(); | ||
if let Some(link) = links.try_next().await? { | ||
// hard code mode: 4u32 i.e bridge mode | ||
let request = handle | ||
.link() | ||
.add() | ||
.macvlan("test_macvlan".into(), link.header.index, 4u32); | ||
request.execute().await? | ||
} else { | ||
println!("no link link {} found", veth_name); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn usage() { | ||
eprintln!( | ||
"usage: | ||
cargo run --example create_macvlan -- <link name> | ||
Note that you need to run this program as root. Instead of running cargo as root, | ||
build the example normally: | ||
cd netlink-ip ; cargo build --example create_macvlan | ||
Then find the binary in the target directory: | ||
cd ../target/debug/example ; sudo ./create_macvlan <link_name>" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters