-
Notifications
You must be signed in to change notification settings - Fork 89
Add API for Seg6 Encapsulation #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+117
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,89 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use std::{ | ||
| env, | ||
| net::{AddrParseError, Ipv6Addr}, | ||
| }; | ||
|
|
||
| use ipnetwork::Ipv6Network; | ||
| use netlink_packet_route::route::Seg6Mode; | ||
| use rtnetlink::{new_connection, Error, Handle, RouteMessageBuilder}; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), ()> { | ||
| let args: Vec<String> = env::args().collect(); | ||
| if args.len() < 5 { | ||
| usage(); | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let dst = args[1].parse::<Ipv6Network>().unwrap_or_else(|_| { | ||
| eprintln!("invalid destination address"); | ||
| std::process::exit(1); | ||
| }); | ||
|
|
||
| let gateway: Ipv6Addr = args[2].parse().unwrap_or_else(|_| { | ||
| eprintln!("invalid gateway"); | ||
| std::process::exit(1); | ||
| }); | ||
|
|
||
| let mode: Seg6Mode = match args[3].as_str() { | ||
| "inline" => Seg6Mode::Inline, | ||
| "encap" => Seg6Mode::Encap, | ||
| _ => { | ||
| eprintln!("invalid SRv6 mode"); | ||
| std::process::exit(1); | ||
| } | ||
| }; | ||
|
|
||
| let segments = args[4..] | ||
| .iter() | ||
| .map(|seg| seg.parse::<Ipv6Addr>()) | ||
| .collect::<Result<Vec<Ipv6Addr>, AddrParseError>>() | ||
| .unwrap_or_else(|_| { | ||
| eprintln!("invalid SRv6 segments"); | ||
| std::process::exit(1); | ||
| }); | ||
|
|
||
| let (connection, handle, _) = new_connection().unwrap(); | ||
|
Aperence marked this conversation as resolved.
|
||
| tokio::spawn(connection); | ||
|
|
||
| if let Err(e) = | ||
| add_route_seg6(&dst, &gateway, mode, segments, handle.clone()).await | ||
| { | ||
| eprintln!("{e}"); | ||
|
Aperence marked this conversation as resolved.
|
||
| } else { | ||
| println!("Route has been added"); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn add_route_seg6( | ||
| dst: &Ipv6Network, | ||
| gateway: &Ipv6Addr, | ||
| mode: Seg6Mode, | ||
| segments: Vec<Ipv6Addr>, | ||
| handle: Handle, | ||
| ) -> Result<(), Error> { | ||
| let route = RouteMessageBuilder::<Ipv6Addr>::new() | ||
| .destination_prefix(dst.ip(), dst.prefix()) | ||
| .gateway(*gateway) | ||
| .output_seg6(mode, segments) | ||
| .build(); | ||
| handle.route().add(route).execute().await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn usage() { | ||
| eprintln!( | ||
| "\ | ||
| usage: | ||
| cargo run --example add_route_seg6 -- <dst> <gateway> <mode> <segments> | ||
|
|
||
| Note that you need to run this program as root: | ||
|
|
||
| env CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER='sudo -E' \\ | ||
| cargo run --example add_route_seg6 -- <dst> <gateway> \ | ||
| <mode> <segments>" | ||
| ); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.