The fabrics configuration consists of two different entity types: fabrics and nodes. This commit adds support for the fabrics inside this section configuration.
There are properties required for all fabrics, regardless of type, which are the ID of the fabric as well as the IP prefix of the fabric. The FabricSection contains those shared properties, in order to avoid having to duplicate them and any methods requiring them for every protocol. This means that every concrete fabric section type is just an instance of the generic type FabricSection<T>. The type parameter is used to define properties, that are specific to a protocol (e.g. area in OSPF). We also provide a generic implementation for e.g. ApiType without having to avoid having to repeat the API definitions and Updater types for the common properties multiple times. We also create the necessary types for updating a FabricSection<T> via FabricSectionUpdater and FabricDeletableProperties, and provide generic implementations for ApiType / Updater on FabricSection / FabricSectionUpdater where possible. This design allows to add new protocols simply by defining their protocol-specific properties in a struct and then using it with the generic FabricSection type. The ID of a fabric is at most 8 alphanumeric characters long and can additionally include hyphens. It cannot start or end with a hyphen. This is because the name of the fabric, will be used as name for network interfaces, which can be at most 15 characters long on Linux. This leaves us enough space for generating interfaces with pre/suffixes. This is analogous to the IDs of many other SDN entities, e.g. VNet where the same restrictions apply. The structs here will later be used in parsing the whole configuration file, located in '/etc/pve/sdn/fabrics.cfg'. Co-authored-by: Gabriel Goller <g.gol...@proxmox.com> Signed-off-by: Stefan Hanreich <s.hanre...@proxmox.com> --- proxmox-ve-config/Cargo.toml | 6 +- proxmox-ve-config/debian/control | 8 ++ proxmox-ve-config/src/sdn/fabric/mod.rs | 1 + .../src/sdn/fabric/section_config/fabric.rs | 133 ++++++++++++++++++ .../src/sdn/fabric/section_config/mod.rs | 1 + proxmox-ve-config/src/sdn/mod.rs | 1 + 6 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 proxmox-ve-config/src/sdn/fabric/mod.rs create mode 100644 proxmox-ve-config/src/sdn/fabric/section_config/fabric.rs create mode 100644 proxmox-ve-config/src/sdn/fabric/section_config/mod.rs diff --git a/proxmox-ve-config/Cargo.toml b/proxmox-ve-config/Cargo.toml index c240a87..7674dc7 100644 --- a/proxmox-ve-config/Cargo.toml +++ b/proxmox-ve-config/Cargo.toml @@ -10,6 +10,8 @@ exclude.workspace = true log = "0.4" anyhow = { workspace = true } nix = "0.26" +regex = { workspace = true } +const_format = { workspace = true } thiserror = { workspace = true } serde = { workspace = true, features = [ "derive" ] } @@ -18,7 +20,7 @@ serde_plain = "1" serde_with = { workspace = true } proxmox-serde = { version = "0.1.2", features = [ "perl" ]} -proxmox-network-types = { workspace = true } -proxmox-schema = "4" +proxmox-network-types = { workspace = true, features = [ "api-types" ] } +proxmox-schema = { workspace = true, features = [ "api-types" ] } proxmox-sys = "0.6.4" proxmox-sortable-macro = "0.1.3" diff --git a/proxmox-ve-config/debian/control b/proxmox-ve-config/debian/control index b76904a..f416316 100644 --- a/proxmox-ve-config/debian/control +++ b/proxmox-ve-config/debian/control @@ -7,14 +7,18 @@ Build-Depends-Arch: cargo:native <!nocheck>, rustc:native <!nocheck>, libstd-rust-dev <!nocheck>, librust-anyhow-1+default-dev <!nocheck>, + librust-const-format-0.2+default-dev <!nocheck>, librust-log-0.4+default-dev <!nocheck>, librust-nix-0.26+default-dev <!nocheck>, + librust-proxmox-network-types-0.1+api-types-dev <!nocheck>, librust-proxmox-network-types-0.1+default-dev <!nocheck>, + librust-proxmox-schema-4+api-types-dev <!nocheck>, librust-proxmox-schema-4+default-dev <!nocheck>, librust-proxmox-serde-0.1+default-dev (>= 0.1.2-~~) <!nocheck>, librust-proxmox-serde-0.1+perl-dev (>= 0.1.2-~~) <!nocheck>, librust-proxmox-sortable-macro-0.1+default-dev (>= 0.1.3-~~) <!nocheck>, librust-proxmox-sys-0.6+default-dev (>= 0.6.4-~~) <!nocheck>, + librust-regex-1+default-dev (>= 1.7-~~) <!nocheck>, librust-serde-1+default-dev <!nocheck>, librust-serde-1+derive-dev <!nocheck>, librust-serde-json-1+default-dev <!nocheck>, @@ -34,14 +38,18 @@ Multi-Arch: same Depends: ${misc:Depends}, librust-anyhow-1+default-dev, + librust-const-format-0.2+default-dev, librust-log-0.4+default-dev, librust-nix-0.26+default-dev, + librust-proxmox-network-types-0.1+api-types-dev, librust-proxmox-network-types-0.1+default-dev, + librust-proxmox-schema-4+api-types-dev, librust-proxmox-schema-4+default-dev, librust-proxmox-serde-0.1+default-dev (>= 0.1.2-~~), librust-proxmox-serde-0.1+perl-dev (>= 0.1.2-~~), librust-proxmox-sortable-macro-0.1+default-dev (>= 0.1.3-~~), librust-proxmox-sys-0.6+default-dev (>= 0.6.4-~~), + librust-regex-1+default-dev (>= 1.7-~~), librust-serde-1+default-dev, librust-serde-1+derive-dev, librust-serde-json-1+default-dev, diff --git a/proxmox-ve-config/src/sdn/fabric/mod.rs b/proxmox-ve-config/src/sdn/fabric/mod.rs new file mode 100644 index 0000000..007be6a --- /dev/null +++ b/proxmox-ve-config/src/sdn/fabric/mod.rs @@ -0,0 +1 @@ +pub mod section_config; diff --git a/proxmox-ve-config/src/sdn/fabric/section_config/fabric.rs b/proxmox-ve-config/src/sdn/fabric/section_config/fabric.rs new file mode 100644 index 0000000..9787d5d --- /dev/null +++ b/proxmox-ve-config/src/sdn/fabric/section_config/fabric.rs @@ -0,0 +1,133 @@ +use const_format::concatcp; +use serde::{Deserialize, Serialize}; + +use proxmox_network_types::ip_address::{Ipv4Cidr, Ipv6Cidr}; +use proxmox_schema::{ + api, api_string_type, const_regex, AllOfSchema, ApiStringFormat, ApiType, ObjectSchema, Schema, + Updater, +}; + +pub const FABRIC_ID_REGEX_STR: &str = r"(?:[a-zA-Z0-9])(?:[a-zA-Z0-9\-]){0,6}(?:[a-zA-Z0-9])?"; + +const_regex! { + pub FABRIC_ID_REGEX = concatcp!(r"^", FABRIC_ID_REGEX_STR, r"$"); +} + +pub const FABRIC_ID_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&FABRIC_ID_REGEX); + +api_string_type! { + /// ID of an SDN fabric. + #[api(format: &FABRIC_ID_FORMAT)] + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] + pub struct FabricId(String); +} + +/// A fabric section in an SDN fabric config. +/// +/// This struct contains all the properties that are required for any fabric, regardless of +/// protocol. Properties that are specific to a protocol can be passed via the type parameter. +/// +/// This is mainly used by the [`Fabric`] and [`super::Section`] enums to specify which types of fabrics can exist, +/// without having to re-define common properties for every fabric. It also simplifies accessing +/// common properties by encapsulating the specific properties to [`FabricSection<T>::properties`]. +#[derive(Debug, Clone, Serialize, Deserialize, Hash)] +pub struct FabricSection<T> { + pub(crate) id: FabricId, + + /// IPv4 Prefix that contains the Node IPs. + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) ip_prefix: Option<Ipv4Cidr>, + + /// IPv6 Prefix that contains the Node IPs. + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) ip6_prefix: Option<Ipv6Cidr>, + + #[serde(flatten)] + pub(crate) properties: T, +} + +impl<T> FabricSection<T> { + /// Get the protocol-specific properties of [FabricSection]. + pub fn properties(&self) -> &T { + &self.properties + } + + /// Get a mutable reference to the protocol-specific properties of [FabricSection]. + pub fn properties_mut(&mut self) -> &mut T { + &mut self.properties + } + + /// Get the id of [FabricSection]. + pub fn id(&self) -> &FabricId { + &self.id + } + + /// Get the ip-prefix (IPv4 CIDR) of [FabricSection]. + pub fn ip_prefix(&self) -> Option<Ipv4Cidr> { + self.ip_prefix + } + + /// Get the ip6-prefix (IPv6 CIDR) of [FabricSection]. + pub fn ip6_prefix(&self) -> Option<Ipv6Cidr> { + self.ip6_prefix + } +} + +const FABRIC_SECTION_SCHEMA: Schema = ObjectSchema::new( + "Common properties for fabrics in an SDN fabric.", + &[ + ("id", false, &FabricId::API_SCHEMA), + ("ip6_prefix", true, &Ipv6Cidr::API_SCHEMA), + ("ip_prefix", true, &Ipv4Cidr::API_SCHEMA), + ], +) +.schema(); + +impl<T: ApiType> ApiType for FabricSection<T> { + const API_SCHEMA: Schema = AllOfSchema::new( + "Fabric in an SDN fabric.", + &[&FABRIC_SECTION_SCHEMA, &T::API_SCHEMA], + ) + .schema(); +} + +/// Updater for a [`FabricSection<T>`] +/// +/// This specifies the updater type for the common properties in [`FabricSection<T>`], as well as +/// provides the delete property for deleting properties on updates. +/// +/// It also provides a blanket implementation of [`Updater`] for any type parameter that implements +/// Updater as well. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct FabricSectionUpdater<T, D> { + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) ip_prefix: Option<Ipv4Cidr>, + + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) ip6_prefix: Option<Ipv6Cidr>, + + #[serde(flatten)] + pub(crate) properties: T, + + #[serde(skip_serializing_if = "Vec::is_empty", default = "Vec::new")] + pub(crate) delete: Vec<FabricDeletableProperties<D>>, +} + +impl<T: Updater, D> Updater for FabricSectionUpdater<T, D> { + fn is_empty(&self) -> bool { + T::is_empty(&self.properties) + && self.ip_prefix.is_none() + && self.ip6_prefix.is_none() + && self.delete.is_empty() + } +} + +/// Deletable properties for a [`FabricSection<T>`] +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", untagged)] +pub enum FabricDeletableProperties<T> { + IpPrefix, + Ip6Prefix, + #[serde(untagged)] + Protocol(T), +} diff --git a/proxmox-ve-config/src/sdn/fabric/section_config/mod.rs b/proxmox-ve-config/src/sdn/fabric/section_config/mod.rs new file mode 100644 index 0000000..8106b6c --- /dev/null +++ b/proxmox-ve-config/src/sdn/fabric/section_config/mod.rs @@ -0,0 +1 @@ +pub mod fabric; diff --git a/proxmox-ve-config/src/sdn/mod.rs b/proxmox-ve-config/src/sdn/mod.rs index cde6fed..7a46db3 100644 --- a/proxmox-ve-config/src/sdn/mod.rs +++ b/proxmox-ve-config/src/sdn/mod.rs @@ -1,4 +1,5 @@ pub mod config; +pub mod fabric; pub mod ipam; use std::{error::Error, fmt::Display, str::FromStr}; -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel