Skip to main content

monitord/varlink/
network.rs

1//! Varlink proxy for the io.systemd.Network interface.
2//! Adapted from the interface definition in systemd's
3//! `src/shared/varlink-io.systemd.Network.c`.
4
5use serde::{Deserialize, Serialize};
6use zlink::{proxy, ReplyError};
7
8pub const NETWORK_SOCKET_PATH: &str = "/run/systemd/netif/io.systemd.Network";
9
10/// Proxy trait for calling methods on the io.systemd.Network interface.
11#[proxy("io.systemd.Network")]
12pub trait Network {
13    /// Describe all interfaces managed by systemd-networkd.
14    async fn describe(&mut self) -> zlink::Result<Result<DescribeOutput, NetworkError>>;
15}
16
17/// Output parameters for the Describe method.
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct DescribeOutput {
20    /// All network interfaces known to systemd-networkd.
21    #[serde(rename = "Interfaces")]
22    pub interfaces: Option<Vec<Interface>>,
23}
24
25/// Per-interface information returned by io.systemd.Network.Describe.
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct Interface {
28    /// Kernel interface index.
29    #[serde(rename = "Index")]
30    pub index: i64,
31    /// Primary interface name (e.g. "eth0").
32    #[serde(rename = "Name")]
33    pub name: String,
34    /// Administrative state (configured, configuring, pending, …).
35    #[serde(rename = "AdministrativeState")]
36    pub administrative_state: String,
37    /// Operational state (routable, degraded, carrier, …).
38    #[serde(rename = "OperationalState")]
39    pub operational_state: String,
40    /// Carrier state (carrier, no-carrier, …).
41    #[serde(rename = "CarrierState")]
42    pub carrier_state: String,
43    /// Combined address state across all address families.
44    #[serde(rename = "AddressState")]
45    pub address_state: String,
46    /// IPv4-specific address state.
47    #[serde(rename = "IPv4AddressState")]
48    pub ipv4_address_state: String,
49    /// IPv6-specific address state.
50    #[serde(rename = "IPv6AddressState")]
51    pub ipv6_address_state: String,
52    /// Overall online state (online, offline, partial); absent when unknown.
53    #[serde(rename = "OnlineState")]
54    pub online_state: Option<String>,
55    /// Path to the applied .network configuration file; absent for unmanaged interfaces.
56    #[serde(rename = "NetworkFile")]
57    pub network_file: Option<String>,
58    /// Whether this interface is required for the system to be considered online.
59    #[serde(rename = "RequiredForOnline")]
60    pub required_for_online: Option<bool>,
61}
62
63/// Errors that can occur in the io.systemd.Network interface.
64#[derive(Debug, Clone, PartialEq, ReplyError)]
65#[zlink(interface = "io.systemd.Network")]
66pub enum NetworkError {
67    /// The requested interface was not found.
68    NoSuchInterface,
69}
70
71impl std::fmt::Display for NetworkError {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        match self {
74            NetworkError::NoSuchInterface => write!(f, "No such interface"),
75        }
76    }
77}
78
79impl std::error::Error for NetworkError {}