From: Stefan Hanreich <s.hanreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH proxmox 2/4] network-types: add a type for network interface altnames
Date: Thu, 12 Nov 2026 18:10:58 +0100 [thread overview]
Message-ID: <20261112171102.674232-3-s.hanreich@proxmox.com> (raw)
In-Reply-To: <20261112171102.674232-1-s.hanreich@proxmox.com>
Altnames can be up to 127 bytes long, so places that accept either a
name or an altname, like the interface of a firewall rule, cannot use
NetworkInterfaceName.
NetworkInterfaceAltname uses the same format with the length limit of
altnames. Every name is a valid altname as well, so add a conversion
from NetworkInterfaceName.
Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
---
.../src/network_interface.rs | 80 +++++++++++++++++++
1 file changed, 80 insertions(+)
diff --git a/proxmox-network-types/src/network_interface.rs b/proxmox-network-types/src/network_interface.rs
index 24972058..fa6d2e37 100644
--- a/proxmox-network-types/src/network_interface.rs
+++ b/proxmox-network-types/src/network_interface.rs
@@ -9,6 +9,10 @@
//! * `api_types::NetworkInterfaceName`: names in the API and in configuration files, and names
//! that are created or passed to `ip`. Not for values that can also be altnames, and not for
//! reading names from the kernel, which can be outside of the API format.
+//!
+//! * `api_types::NetworkInterfaceAltname`: values that can be a name or an altname, e.g. the
+//! interface of a firewall rule. Resolve it to the name where only names work, e.g. for
+//! `iifname` in nftables.
/// API types for network interface names.
///
@@ -23,6 +27,21 @@
///
/// assert!("veth999999999i31".parse::<NetworkInterfaceName>().is_err());
/// ```
+///
+/// Values that are either the name or an altname of a network interface, e.g. the interface of a
+/// firewall rule, are represented by
+/// [`NetworkInterfaceAltname`](api_types::NetworkInterfaceAltname):
+///
+/// ```
+/// # use proxmox_network_types::network_interface::api_types::{
+/// # NetworkInterfaceAltname, NetworkInterfaceName,
+/// # };
+/// let altname: NetworkInterfaceAltname = "enx0123456789abcd".parse().unwrap();
+/// assert_eq!(altname.as_str(), "enx0123456789abcd");
+///
+/// let name: NetworkInterfaceName = "eth0".parse().unwrap();
+/// assert_eq!(NetworkInterfaceAltname::from(name).as_str(), "eth0");
+/// ```
#[cfg(feature = "api-types")]
pub mod api_types {
use proxmox_schema::{api, api_string_type, api_types::SAFE_ID_FORMAT};
@@ -30,6 +49,9 @@ pub mod api_types {
/// The size of the kernel buffer for network interface names, including the terminating NUL.
const IFNAMSIZ: usize = 16;
+ /// The size of the kernel buffer for altnames, including the terminating NUL.
+ const ALTIFNAMSIZ: usize = 128;
+
api_string_type! {
#[api(format: &SAFE_ID_FORMAT, min_length: 1, max_length: IFNAMSIZ - 1)]
/// The name of a network interface.
@@ -44,6 +66,27 @@ pub mod api_types {
proxmox_serde::forward_deserialize_to_from_str!(NetworkInterfaceName);
proxmox_serde::forward_serialize_to_display!(NetworkInterfaceName);
+ api_string_type! {
+ #[api(format: &SAFE_ID_FORMAT, min_length: 1, max_length: ALTIFNAMSIZ - 1)]
+ /// The name or an alternative name (altname) of a network interface.
+ ///
+ /// Same format as network interface names, but with the altname limit of 127 bytes. It
+ /// has to be resolved to the name of the interface where only names are accepted.
+ #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
+ #[repr(transparent)]
+ pub struct NetworkInterfaceAltname(String);
+ }
+
+ proxmox_serde::forward_deserialize_to_from_str!(NetworkInterfaceAltname);
+ proxmox_serde::forward_serialize_to_display!(NetworkInterfaceAltname);
+
+ impl From<NetworkInterfaceName> for NetworkInterfaceAltname {
+ fn from(name: NetworkInterfaceName) -> Self {
+ // SAFETY: both schemas use the same format, and names are shorter than altnames
+ unsafe { Self::from_string_unchecked(name.into_string()) }
+ }
+ }
+
#[cfg(test)]
mod tests {
use super::*;
@@ -98,5 +141,42 @@ pub mod api_types {
);
}
}
+
+ #[test]
+ fn parse_network_interface_altname() {
+ let longest = "a".repeat(ALTIFNAMSIZ - 1);
+
+ for name in [
+ "eth0",
+ "bond0.100",
+ "uplink-a",
+ "abcdefghijklmnop",
+ "enx0123456789abcd",
+ longest.as_str(),
+ ] {
+ let parsed: NetworkInterfaceAltname = name.parse().expect(name);
+ assert_eq!(parsed.as_str(), name);
+ assert_eq!(parsed.to_string(), name);
+ }
+
+ let too_long = "a".repeat(ALTIFNAMSIZ);
+
+ for name in ["", ".", "-eth0", "eth 0", "eth0:1", too_long.as_str()] {
+ assert!(name.parse::<NetworkInterfaceAltname>().is_err(), "{name:?}");
+ }
+ }
+
+ #[test]
+ fn deserialize_network_interface_altname() {
+ let name: NetworkInterfaceAltname =
+ serde_plain::from_str("abcdefghijklmnop").expect("valid altname");
+ assert_eq!(name.as_str(), "abcdefghijklmnop");
+ assert_eq!(
+ serde_plain::to_string(&name).expect("serializable"),
+ "abcdefghijklmnop"
+ );
+
+ assert!(serde_plain::from_str::<NetworkInterfaceAltname>("eth0:1").is_err());
+ }
}
}
--
2.47.3
next prev parent reply other threads:[~2026-11-12 17:11 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 17:10 [PATCH proxmox 0/4] network-types: add types for network interface names Stefan Hanreich
2026-11-12 17:10 ` [PATCH proxmox 1/4] network-types: add a type " Stefan Hanreich
2026-11-12 17:10 ` Stefan Hanreich [this message]
2026-11-12 17:10 ` [PATCH proxmox 3/4] network-types: add types for names reported by the kernel Stefan Hanreich
2026-11-12 17:11 ` [PATCH proxmox 4/4] network-types: add a type for names of existing configurations Stefan Hanreich
2026-11-12 17:36 ` Stefan Hanreich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261112171102.674232-3-s.hanreich@proxmox.com \
--to=s.hanreich@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox