-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(link): add support of creating ipvlan and ipvtap.
feat: - add support of creating ipvlan and ipvtap - add examples.
- Loading branch information
1 parent
6896bae
commit 3f3859b
Showing
3 changed files
with
175 additions
and
4 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,68 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
use futures::stream::TryStreamExt; | ||
use netlink_packet_route::link::IpVlanMode; | ||
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() != 3 { | ||
usage(); | ||
return Ok(()); | ||
} | ||
let link_name = &args[1]; | ||
let mode_str = &args[2]; | ||
let mode = match mode_str.as_str() { | ||
"l2" => IpVlanMode::L2, | ||
"l3" => IpVlanMode::L3, | ||
"l3s" => IpVlanMode::L3S, | ||
_ => { | ||
usage(); | ||
return Ok(()); | ||
} | ||
}; | ||
|
||
let (connection, handle, _) = new_connection().unwrap(); | ||
tokio::spawn(connection); | ||
|
||
create_ipvlan(handle, link_name.to_string(), mode) | ||
.await | ||
.map_err(|e| format!("{e}")) | ||
} | ||
|
||
async fn create_ipvlan( | ||
handle: Handle, | ||
link_name: String, | ||
mode: IpVlanMode, | ||
) -> Result<(), Error> { | ||
let mut links = handle.link().get().match_name(link_name.clone()).execute(); | ||
if let Some(link) = links.try_next().await? { | ||
let request = handle.link().add().ipvlan( | ||
"test_ipvlan".into(), | ||
link.header.index, | ||
mode, | ||
); | ||
request.execute().await? | ||
} else { | ||
println!("no link {link_name} found"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn usage() { | ||
eprintln!( | ||
"usage: | ||
cargo run --example create_ipvlan -- <link_name> <ipvlan_mode> | ||
ipvlan_mode can be one of the following: | ||
l2: L2 mode | ||
l3: L3 mode | ||
l3s: L3S mode | ||
Note that you need to run this program as root. Instead of running cargo as root, | ||
build the example normally: | ||
cargo build --example create_ipvlan | ||
Then find the binary in the target directory: | ||
cd target/debug/examples ; sudo ./create_ipvlan <link_name> <ipvlan_mode>" | ||
); | ||
} |
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,73 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
use futures::stream::TryStreamExt; | ||
use netlink_packet_route::link::IpVtapMode; | ||
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() != 3 { | ||
usage(); | ||
return Ok(()); | ||
} | ||
let link_name = &args[1]; | ||
let mode_str = &args[2]; | ||
let mode = match mode_str.as_str() { | ||
"l2" => IpVtapMode::L2, | ||
"l3" => IpVtapMode::L3, | ||
"l3s" => IpVtapMode::L3S, | ||
_ => { | ||
usage(); | ||
return Ok(()); | ||
} | ||
}; | ||
|
||
let (connection, handle, _) = new_connection().unwrap(); | ||
tokio::spawn(connection); | ||
|
||
create_ipvtap(handle, link_name.to_string(), mode) | ||
.await | ||
.map_err(|e| format!("{e}")) | ||
} | ||
|
||
async fn create_ipvtap( | ||
handle: Handle, | ||
link_name: String, | ||
mode: IpVtapMode, | ||
) -> Result<(), Error> { | ||
let mut links = handle.link().get().match_name(link_name.clone()).execute(); | ||
if let Some(link) = links.try_next().await? { | ||
let request = handle.link().add().ipvtap( | ||
"test_ipvtap".into(), | ||
link.header.index, | ||
mode, | ||
); | ||
request.execute().await? | ||
} else { | ||
println!("no link {link_name} found"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn usage() { | ||
eprintln!( | ||
"usage: | ||
cargo run --example create_ipvtap -- <link_name> <ipvtap_mode> | ||
ipvtap_mode can be one of the following: | ||
l2: L2 mode | ||
l3: L3 mode | ||
l3s: L3S mode | ||
Note that you need to run this program as root. Instead of running cargo as root, | ||
build the example normally: | ||
cargo build --example create_ipvtap | ||
Then find the binary in the target directory: | ||
cd target/debug/examples ; sudo ./create_ipvtap <link_name> <ipvtap_mode>" | ||
); | ||
} |
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