public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Hanreich <s.hanreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH proxmox-ve-rs 3/7] ve-config: guest: accept network devices without a MAC address
Date: Tue, 24 Nov 2026 18:57:13 +0100	[thread overview]
Message-ID: <20261124175718.512428-4-s.hanreich@proxmox.com> (raw)
In-Reply-To: <20261124175718.512428-1-s.hanreich@proxmox.com>

qemu-server and pve-container accept network devices without a MAC
address and generate a random one each time they parse the device. The
API always stores a MAC address, but a manual edit or a restored backup
can lack it. proxmox-firewall failed to parse such a config, and with it
the whole firewall configuration of the node, if the guest had a
firewall config.

Make the MAC address optional for both guest types, and return an
Option from mac_address(), so that users have to decide how to handle
a device without one.

Also take the model as default key, as qemu-server does, so that a
device like 'virtio,bridge=vmbr0' without a MAC address can be parsed.

As unknown models are accepted, a bare unknown word now parses as a
network device as well, so test an invalid value with a property that
neither guest type has instead.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
---
 proxmox-ve-config/src/guest/vm.rs | 56 ++++++++++++++++++++-----------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/proxmox-ve-config/src/guest/vm.rs b/proxmox-ve-config/src/guest/vm.rs
index e809c2e0..ec6cbc91 100644
--- a/proxmox-ve-config/src/guest/vm.rs
+++ b/proxmox-ve-config/src/guest/vm.rs
@@ -77,7 +77,7 @@ impl FromStr for NetworkDeviceModel {
 pub struct QemuNetworkDevice {
     model: NetworkDeviceModel,
     #[serde(rename = "macaddr")]
-    mac_address: MacAddress,
+    mac_address: Option<MacAddress>,
     firewall: Option<bool>,
 }
 
@@ -93,7 +93,7 @@ impl ApiType for QemuNetworkDevice {
             ),
             (
                 "macaddr",
-                false,
+                true,
                 &StringSchema::new("mac address for this network device").schema(),
             ),
             (
@@ -104,6 +104,7 @@ impl ApiType for QemuNetworkDevice {
         ]),
     )
     .additional_properties(true)
+    .default_key("model")
     .key_alias_info(KeyAliasInfo::new(
         "model",
         &sorted!([
@@ -207,7 +208,7 @@ pub struct LxcNetworkDevice {
     #[serde(rename = "type", default = "default_lxc_network_device_model")]
     ty: NetworkDeviceModel,
     #[serde(rename = "hwaddr")]
-    mac_address: MacAddress,
+    mac_address: Option<MacAddress>,
     // never read, requiring it tells container network devices apart from QEMU ones
     #[serde(rename = "name")]
     _name: String,
@@ -228,7 +229,7 @@ impl ApiType for LxcNetworkDevice {
             ),
             (
                 "hwaddr",
-                false,
+                true,
                 &StringSchema::new("mac address for this network device").schema(),
             ),
             (
@@ -276,8 +277,8 @@ impl NetworkDevice {
         }
     }
 
-    /// Returns the MAC address of the network device
-    pub fn mac_address(&self) -> MacAddress {
+    /// Returns the MAC address of the network device, if set in the configuration.
+    pub fn mac_address(&self) -> Option<MacAddress> {
         match self {
             NetworkDevice::Qemu(qemu_network_device) => qemu_network_device.mac_address,
             NetworkDevice::Lxc(lxc_network_device) => lxc_network_device.mac_address,
@@ -469,7 +470,7 @@ mod tests {
             network_device,
             NetworkDevice::Qemu(QemuNetworkDevice {
                 model: NetworkDeviceModel::VirtIO,
-                mac_address: MacAddress::new([0xAA, 0xAA, 0xAA, 0x17, 0x19, 0x81]),
+                mac_address: Some(MacAddress::new([0xAA, 0xAA, 0xAA, 0x17, 0x19, 0x81])),
                 firewall: Some(true),
             })
         );
@@ -482,7 +483,7 @@ mod tests {
             network_device,
             NetworkDevice::Qemu(QemuNetworkDevice {
                 model: NetworkDeviceModel::VirtIO,
-                mac_address: MacAddress::new([0xAA, 0xAA, 0xAA, 0x17, 0x19, 0x81]),
+                mac_address: Some(MacAddress::new([0xAA, 0xAA, 0xAA, 0x17, 0x19, 0x81])),
                 firewall: None,
             })
         );
@@ -497,7 +498,7 @@ mod tests {
             network_device,
             NetworkDevice::Qemu(QemuNetworkDevice {
                 model: NetworkDeviceModel::VirtIO,
-                mac_address: MacAddress::new([0xAA, 0xAA, 0xAA, 0x17, 0x19, 0x81]),
+                mac_address: Some(MacAddress::new([0xAA, 0xAA, 0xAA, 0x17, 0x19, 0x81])),
                 firewall: Some(true),
             })
         );
@@ -514,16 +515,33 @@ mod tests {
             NetworkDevice::Lxc(LxcNetworkDevice {
                 ty: NetworkDeviceModel::Veth,
                 _name: "eth0".to_string(),
-                mac_address: MacAddress::new([0xAA, 0xAA, 0xAA, 0xE2, 0x3E, 0x24]),
+                mac_address: Some(MacAddress::new([0xAA, 0xAA, 0xAA, 0xE2, 0x3E, 0x24])),
                 firewall: Some(false),
                 ip: Some(LxcIpv4Addr::Dhcp),
                 ip6: None,
             })
         );
 
-        "model=virtio"
-            .parse::<NetworkDevice>()
-            .expect_err("invalid network configuration");
+        network_device = "virtio,bridge=public"
+            .parse()
+            .expect("valid network configuration");
+
+        assert_eq!(
+            network_device,
+            NetworkDevice::Qemu(QemuNetworkDevice {
+                model: NetworkDeviceModel::VirtIO,
+                mac_address: None,
+                firewall: None,
+            })
+        );
+
+        network_device = "model=virtio".parse().expect("valid network configuration");
+        assert_eq!(network_device.mac_address(), None);
+
+        network_device = "name=eth0,bridge=public"
+            .parse()
+            .expect("valid network configuration");
+        assert_eq!(network_device.mac_address(), None);
 
         network_device = "name=eth0,bridge=public,firewall=1,hwaddr=AA:AA:AA:E2:3E:25"
             .parse()
@@ -534,7 +552,7 @@ mod tests {
             NetworkDevice::Lxc(LxcNetworkDevice {
                 ty: NetworkDeviceModel::Veth,
                 _name: "eth0".to_string(),
-                mac_address: MacAddress::new([0xAA, 0xAA, 0xAA, 0xE2, 0x3E, 0x25]),
+                mac_address: Some(MacAddress::new([0xAA, 0xAA, 0xAA, 0xE2, 0x3E, 0x25])),
                 firewall: Some(true),
                 ip: None,
                 ip6: None,
@@ -629,7 +647,7 @@ vmgenid: 706fbe99-d28b-4047-a9cd-3677c859ca8a"
             network_config.network_devices()[&0],
             NetworkDevice::Qemu(QemuNetworkDevice {
                 model: NetworkDeviceModel::VirtIO,
-                mac_address: MacAddress::new([0xAA, 0xBB, 0xCC, 0xF2, 0xFE, 0x75]),
+                mac_address: Some(MacAddress::new([0xAA, 0xBB, 0xCC, 0xF2, 0xFE, 0x75])),
                 firewall: None,
             })
         );
@@ -658,7 +676,7 @@ unprivileged: 1"
             NetworkDevice::Lxc(LxcNetworkDevice {
                 ty: NetworkDeviceModel::Veth,
                 _name: "eth0".to_string(),
-                mac_address: MacAddress::new([0xBC, 0x24, 0x11, 0x47, 0x83, 0x11]),
+                mac_address: Some(MacAddress::new([0xBC, 0x24, 0x11, 0x47, 0x83, 0x11])),
                 firewall: Some(true),
                 ip: Some(LxcIpv4Addr::Dhcp),
                 ip6: Some(LxcIpv6Addr::Auto),
@@ -670,7 +688,7 @@ unprivileged: 1"
             NetworkDevice::Lxc(LxcNetworkDevice {
                 ty: NetworkDeviceModel::Veth,
                 _name: "eth0".to_string(),
-                mac_address: MacAddress::new([0xBC, 0x24, 0x11, 0x47, 0x83, 0x12]),
+                mac_address: Some(MacAddress::new([0xBC, 0x24, 0x11, 0x47, 0x83, 0x12])),
                 firewall: Some(false),
                 ip: Some(LxcIpv4Addr::Ip(
                     Ipv4Cidr::from_str("123.123.123.123/24").expect("valid ipv4")
@@ -684,7 +702,7 @@ unprivileged: 1"
             NetworkDevice::Lxc(LxcNetworkDevice {
                 ty: NetworkDeviceModel::Veth,
                 _name: "eth0".to_string(),
-                mac_address: MacAddress::new([0xBC, 0x24, 0x11, 0x47, 0x83, 0x13]),
+                mac_address: Some(MacAddress::new([0xBC, 0x24, 0x11, 0x47, 0x83, 0x13])),
                 firewall: Some(true),
                 ip: None,
                 ip6: Some(LxcIpv6Addr::Ip(
@@ -717,7 +735,7 @@ unprivileged: 1"
         assert_eq!(network_config.ignored_keys(), ["net32"]);
 
         // a network device key with an invalid value is still an error
-        NetworkConfig::parse("net0: foo".as_bytes()).expect_err("invalid network device");
+        NetworkConfig::parse("net0: foo=bar".as_bytes()).expect_err("invalid network device");
 
         // which definition the guest uses depends on its validation of the values
         NetworkConfig::parse(
-- 
2.47.3





  parent reply	other threads:[~2026-11-24 17:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 17:57 [PATCH firewall/proxmox{-ve-rs,-firewall} 0/7] firewall: handle guest network devices without a MAC address Stefan Hanreich
2026-11-24 17:57 ` [PATCH proxmox-ve-rs 1/7] ve-config: guest: require the name of container network devices Stefan Hanreich
2026-11-24 17:57 ` [PATCH proxmox-ve-rs 2/7] ve-config: guest: default the type of container network devices to veth Stefan Hanreich
2026-11-24 17:57 ` Stefan Hanreich [this message]
2026-11-24 17:57 ` [PATCH proxmox-firewall 4/7] firewall: guest: handle network devices without a MAC address Stefan Hanreich
2026-11-25  7:28   ` Arthur Bied-Charreton
2026-11-24 17:57 ` [PATCH proxmox-firewall 5/7] tests: add network devices without " Stefan Hanreich
2026-11-24 17:57 ` [PATCH pve-firewall 6/7] firewall: drop the traffic of guest " Stefan Hanreich
2026-11-24 17:57 ` [PATCH pve-firewall 7/7] firewall: do not generate MAC addresses for guest network devices Stefan Hanreich
2026-11-25  7:14 ` [PATCH firewall/proxmox{-ve-rs,-firewall} 0/7] firewall: handle guest network devices without a MAC address Arthur Bied-Charreton

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=20261124175718.512428-4-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal