Skip to content

Commit 6863102

Browse files
daniel-nolandcathay4t
authored andcommitted
Implement add vrf link (issue #40)
* Implemented add vrf link * Adjusted CI to modprobe vrf for tests
1 parent 6e4fabd commit 6863102

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ jobs:
1515
steps:
1616
- uses: actions/checkout@v3
1717

18+
- name: Install necessary dependencies
19+
run: |
20+
sudo apt update
21+
sudo apt install "linux-modules-extra-$(uname -r)"
22+
sudo modprobe vrf
23+
1824
- name: Install Rust Stable
1925
run: |
2026
rustup override set stable

src/link/add.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use netlink_packet_core::{
1414
use netlink_packet_route::{
1515
link::{
1616
InfoBond, InfoData, InfoKind, InfoMacVlan, InfoMacVtap, InfoVeth,
17-
InfoVlan, InfoVxlan, InfoXfrm, LinkAttribute, LinkFlag, LinkInfo,
18-
LinkMessage, VlanQosMapping,
17+
InfoVlan, InfoVrf, InfoVxlan, InfoXfrm, LinkAttribute, LinkFlag,
18+
LinkInfo, LinkMessage, VlanQosMapping,
1919
},
2020
RouteNetlinkMessage,
2121
};
@@ -787,6 +787,15 @@ impl LinkAddRequest {
787787
request
788788
}
789789

790+
pub fn vrf(self, name: String, table_id: u32) -> Self {
791+
self.name(name.clone())
792+
.link_info(
793+
InfoKind::Vrf,
794+
Some(InfoData::Vrf(vec![InfoVrf::TableId(table_id)])),
795+
)
796+
.append_nla(LinkAttribute::IfName(name))
797+
}
798+
790799
/// Replace existing matching link.
791800
pub fn replace(self) -> Self {
792801
Self {

src/link/test.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
use futures::stream::TryStreamExt;
44
use netlink_packet_route::link::{
5-
InfoData, InfoKind, InfoMacVlan, LinkAttribute, LinkInfo, LinkMessage,
5+
InfoData, InfoKind, InfoMacVlan, InfoVrf, LinkAttribute, LinkInfo,
6+
LinkMessage,
67
};
78
use tokio::runtime::Runtime;
89

@@ -78,6 +79,33 @@ fn create_get_delete_macvlan() {
7879
.unwrap();
7980
}
8081

82+
#[test]
83+
fn create_delete_vrf() {
84+
const VRF_IFACE_NAME: &str = "vrf2222";
85+
const VRF_TABLE: u32 = 2222;
86+
let rt = Runtime::new().unwrap();
87+
let handle = rt.block_on(_create_vrf(VRF_IFACE_NAME, VRF_TABLE));
88+
assert!(handle.is_ok());
89+
90+
let mut handle = handle.unwrap();
91+
let msg = rt.block_on(_get_iface(&mut handle, VRF_IFACE_NAME.to_owned()));
92+
assert!(msg.is_ok());
93+
assert!(has_nla(
94+
msg.as_ref().unwrap(),
95+
&LinkAttribute::IfName(VRF_IFACE_NAME.to_string())
96+
));
97+
assert!(has_nla(
98+
msg.as_ref().unwrap(),
99+
&LinkAttribute::LinkInfo(vec![
100+
LinkInfo::Kind(InfoKind::Vrf),
101+
LinkInfo::Data(InfoData::Vrf(vec![InfoVrf::TableId(VRF_TABLE),]))
102+
])
103+
));
104+
105+
rt.block_on(_del_iface(&mut handle, msg.unwrap().header.index))
106+
.unwrap();
107+
}
108+
81109
fn has_nla(msg: &LinkMessage, nla: &LinkAttribute) -> bool {
82110
msg.attributes.iter().any(|x| x == nla)
83111
}
@@ -128,3 +156,12 @@ async fn _create_macvlan(
128156
req.execute().await?;
129157
Ok(link_handle)
130158
}
159+
160+
async fn _create_vrf(name: &str, table: u32) -> Result<LinkHandle, Error> {
161+
let (conn, handle, _) = new_connection().unwrap();
162+
tokio::spawn(conn);
163+
let link_handle = handle.link();
164+
let req = link_handle.add().vrf(name.to_string(), table);
165+
req.execute().await?;
166+
Ok(link_handle)
167+
}

0 commit comments

Comments
 (0)