-
Notifications
You must be signed in to change notification settings - Fork 86
link: Support changing bridge VLAN filtering #143
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
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,122 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use futures_util::stream::TryStreamExt; | ||
| use rtnetlink::{ | ||
| new_connection, | ||
| packet_route::{ | ||
| link::{BridgeVlanInfoFlags, LinkAttribute, LinkExtentMask}, | ||
| AddressFamily, | ||
| }, | ||
| Handle, LinkBridge, LinkBridgeVlan, LinkDummy, LinkUnspec, | ||
| }; | ||
|
|
||
| async fn create_bridge_and_get_index(handle: &Handle) -> Result<u32, String> { | ||
| handle | ||
| .link() | ||
| .add( | ||
| LinkBridge::new("my-bridge0") | ||
| .vlan_filtering(true) | ||
| .up() | ||
| .build(), | ||
| ) | ||
| .execute() | ||
| .await | ||
| .map_err(|e| format!("{e}"))?; | ||
|
|
||
| let mut bridge_links = handle | ||
| .link() | ||
| .get() | ||
| .match_name("my-bridge0".to_string()) | ||
| .execute(); | ||
| if let Some(bridge_link) = | ||
| bridge_links.try_next().await.map_err(|e| format!("{e}"))? | ||
| { | ||
| Ok(bridge_link.header.index) | ||
| } else { | ||
| Err("failed to find my-bridge0".into()) | ||
| } | ||
| } | ||
|
|
||
| async fn create_dummy_and_attach_to_bridge( | ||
| handle: &Handle, | ||
| bridge_index: u32, | ||
| ) -> Result<u32, rtnetlink::Error> { | ||
| handle | ||
| .link() | ||
| .add(LinkDummy::new("my-dummy0").build()) | ||
| .execute() | ||
| .await?; | ||
|
|
||
| handle | ||
| .link() | ||
| .set( | ||
| LinkUnspec::new_with_name("my-dummy0") | ||
| .controller(bridge_index) | ||
| .down() | ||
| .build(), | ||
| ) | ||
| .execute() | ||
| .await?; | ||
|
|
||
| let mut dummy_links = handle | ||
| .link() | ||
| .get() | ||
| .match_name("my-dummy0".to_string()) | ||
| .execute(); | ||
| if let Some(dummy_link) = dummy_links.try_next().await? { | ||
| Ok(dummy_link.header.index) | ||
| } else { | ||
| panic!("failed to find my-dummy0") | ||
| } | ||
| } | ||
|
|
||
| async fn set_bridge_vlan( | ||
| handle: &Handle, | ||
| port_index: u32, | ||
| ) -> Result<(), rtnetlink::Error> { | ||
| let message = LinkBridgeVlan::new(port_index) | ||
| .vlan(10, BridgeVlanInfoFlags::Pvid) | ||
| .vlan_range_start(20, BridgeVlanInfoFlags::empty()) | ||
| .vlan_range_end(30, BridgeVlanInfoFlags::empty()) | ||
| .build(); | ||
|
|
||
| handle.link().set(message).execute().await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let (connection, handle, _) = new_connection().unwrap(); | ||
| tokio::spawn(connection); | ||
|
|
||
| let bridge_index = create_bridge_and_get_index(&handle).await?; | ||
|
|
||
| let port_index = | ||
| create_dummy_and_attach_to_bridge(&handle, bridge_index).await?; | ||
| set_bridge_vlan(&handle, port_index).await?; | ||
|
|
||
| let mut dump_link = handle | ||
| .link() | ||
| .get() | ||
| .set_filter_mask( | ||
| AddressFamily::Bridge, | ||
| vec![LinkExtentMask::BrvlanCompressed], | ||
| ) | ||
| .execute(); | ||
| while let Some(link_msg) = dump_link.try_next().await? { | ||
| // With set_filter_mask(), we cannot use match_name to filter due to | ||
| // linux kernel limitation. | ||
| if !link_msg.attributes.as_slice().iter().any( | ||
| |a| matches!(a, LinkAttribute::IfName(name) if name == "my-dummy0"), | ||
| ) { | ||
| continue; | ||
| } | ||
| for nla in link_msg.attributes { | ||
| if let LinkAttribute::AfSpecBridge(i) = &nla { | ||
| println!("{:?}", i); | ||
| return Ok(()); | ||
| } | ||
| } | ||
| } | ||
| Err("failed to find bridge vlan info for my-dummy0".into()) | ||
| } | ||
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
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,61 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use crate::{ | ||
| packet_route::{ | ||
| link::{ | ||
| AfSpecBridge, BridgeVlanInfo, BridgeVlanInfoFlags, LinkAttribute, | ||
| }, | ||
| AddressFamily, | ||
| }, | ||
| LinkMessageBuilder, | ||
| }; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct LinkBridgeVlan; | ||
|
|
||
| impl LinkBridgeVlan { | ||
| pub fn new(port_index: u32) -> LinkMessageBuilder<Self> { | ||
| LinkMessageBuilder::<LinkBridgeVlan>::default() | ||
| .index(port_index) | ||
| .interface_family(AddressFamily::Bridge) | ||
| } | ||
| } | ||
|
|
||
| impl LinkMessageBuilder<LinkBridgeVlan> { | ||
| /// Append arbitrary [LinkAttribute::AfSpecBridge] | ||
| pub fn append_af_spec(self, spec: AfSpecBridge) -> Self { | ||
| let mut ret = self; | ||
|
|
||
| for attr in ret.extra_attriutes.iter_mut() { | ||
| if let LinkAttribute::AfSpecBridge(specs) = attr { | ||
| specs.push(spec); | ||
| return ret; | ||
| } | ||
| } | ||
|
|
||
| ret.append_extra_attribute(LinkAttribute::AfSpecBridge(vec![spec])) | ||
| } | ||
|
|
||
| pub fn vlan(self, vid: u16, flags: BridgeVlanInfoFlags) -> Self { | ||
| self.append_af_spec(AfSpecBridge::VlanInfo(BridgeVlanInfo { | ||
| vid, | ||
| flags, | ||
| })) | ||
| } | ||
|
|
||
| /// Helper function by adding [BridgeVlanInfoFlags::RangeBegin] | ||
| /// automatically to `flags` | ||
| pub fn vlan_range_start( | ||
| self, | ||
| vid: u16, | ||
| flags: BridgeVlanInfoFlags, | ||
| ) -> Self { | ||
| self.vlan(vid, flags | BridgeVlanInfoFlags::RangeBegin) | ||
| } | ||
|
|
||
| /// Helper function by adding [BridgeVlanInfoFlags::RangeEnd] | ||
| /// automatically to `flags` | ||
| pub fn vlan_range_end(self, vid: u16, flags: BridgeVlanInfoFlags) -> Self { | ||
| self.vlan(vid, flags | BridgeVlanInfoFlags::RangeEnd) | ||
| } | ||
| } |
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
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
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.