Skip to content

Commit c72f59e

Browse files
committed
support nlmon link type
Introduce nlmon (Netlink monitor) interface support for both creation and query. Usage: ip link add name NAME type nlmon Integration test cases included. Signed-off-by: Gris Ge <cnfourt@gmail.com>
1 parent fa4fd08 commit c72f59e

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/ip/link/add.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::collections::HashMap;
55
use futures_util::TryStreamExt;
66
use iproute_rs::{CliError, parse_mac_str};
77
use rtnetlink::{
8-
LinkDummy, LinkMessageBuilder,
8+
LinkDummy, LinkMessageBuilder, LinkNlmon,
99
packet_route::link::{InfoKind, LinkMessage},
1010
};
1111

@@ -45,6 +45,9 @@ impl LinkAddCommand {
4545
InfoKind::Dummy => {
4646
base_conf.apply(LinkDummy::new(&base_conf.name))?
4747
}
48+
InfoKind::Nlmon => {
49+
base_conf.apply(LinkNlmon::new(&base_conf.name))?
50+
}
4851
InfoKind::Vlan => {
4952
base_conf.apply(base_conf.apply_vlan(&handle).await?)?
5053
}

src/ip/link/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ mod bridge;
55
mod color;
66
mod dummy;
77
mod loopback;
8+
mod nlmon;
89
mod vlan;
910
mod vxlan;

src/ip/link/tests/nlmon.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
use crate::tests::{exec_cmd, ip_rs_exec_cmd};
4+
5+
#[test]
6+
fn test_link_show_nlmon() {
7+
let ifname = "tnlm0";
8+
9+
with_nlmon_iface(ifname, || {
10+
let expected_output = exec_cmd(&["ip", "link", "show", ifname]);
11+
12+
let our_output = ip_rs_exec_cmd(&["link", "show", ifname]);
13+
14+
pretty_assertions::assert_eq!(expected_output, our_output);
15+
})
16+
}
17+
18+
#[test]
19+
fn test_link_detailed_show_nlmon() {
20+
let ifname = "tnlm1";
21+
22+
with_nlmon_iface(ifname, || {
23+
let expected_output = exec_cmd(&["ip", "-d", "link", "show", ifname]);
24+
25+
let our_output = ip_rs_exec_cmd(&["-d", "link", "show", ifname]);
26+
27+
pretty_assertions::assert_eq!(expected_output, our_output);
28+
})
29+
}
30+
31+
#[test]
32+
fn test_link_show_nlmon_json() {
33+
let ifname = "tnlm2";
34+
35+
with_nlmon_iface(ifname, || {
36+
let expected_output = exec_cmd(&["ip", "-j", "link", "show", ifname]);
37+
38+
let our_output = ip_rs_exec_cmd(&["-j", "link", "show", ifname]);
39+
40+
pretty_assertions::assert_eq!(expected_output, our_output);
41+
})
42+
}
43+
44+
#[test]
45+
fn test_link_detailed_show_nlmon_json() {
46+
let ifname = "tnlm3";
47+
48+
with_nlmon_iface(ifname, || {
49+
let expected_output =
50+
exec_cmd(&["ip", "-d", "-j", "link", "show", ifname]);
51+
52+
let our_output = ip_rs_exec_cmd(&["-d", "-j", "link", "show", ifname]);
53+
54+
pretty_assertions::assert_eq!(expected_output, our_output);
55+
})
56+
}
57+
58+
fn with_nlmon_iface<T>(name: &str, test: T)
59+
where
60+
T: FnOnce() + std::panic::UnwindSafe,
61+
{
62+
ip_rs_exec_cmd(&["link", "add", name, "type", "nlmon"]);
63+
exec_cmd(&["ip", "link", "set", name, "up"]);
64+
65+
let result = std::panic::catch_unwind(|| {
66+
test();
67+
});
68+
69+
// clean up
70+
exec_cmd(&["ip", "link", "del", name]);
71+
assert!(result.is_ok())
72+
}

0 commit comments

Comments
 (0)