From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.ivanov@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 303A374B2F
 for <pve-devel@lists.proxmox.com>; Mon, 19 Apr 2021 20:11:44 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 247E31924A
 for <pve-devel@lists.proxmox.com>; Mon, 19 Apr 2021 20:11:44 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id E53941923F
 for <pve-devel@lists.proxmox.com>; Mon, 19 Apr 2021 20:11:42 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B97F045AE6
 for <pve-devel@lists.proxmox.com>; Mon, 19 Apr 2021 20:11:42 +0200 (CEST)
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Mon, 19 Apr 2021 20:11:28 +0200
Message-Id: <20210419181128.4047-1-s.ivanov@proxmox.com>
X-Mailer: git-send-email 2.20.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.058 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH manager] api: network: fix #3385 check for used
 ports before writing
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 19 Apr 2021 18:11:44 -0000

Currently the check for used ports for bonds and bridges happens while
rendering '/etc/network/interfaces.new' in PVE::Inotify (pve-common).
However at that stage the new/updated interface is already merged with
the old settings, making it impossible to indicate where a NIC is
currently used.

The code is adapted from the renderer in
PVE::Inotify::__write_etc_network_interfaces.

Tested on a virtual PVE instance.

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
 PVE/API2/Network.pm | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/PVE/API2/Network.pm b/PVE/API2/Network.pm
index 9ac289c4..2d8c4ca1 100644
--- a/PVE/API2/Network.pm
+++ b/PVE/API2/Network.pm
@@ -301,6 +301,39 @@ my $check_duplicate_gateway6 = sub {
     return &$check_duplicate($config, $newiface, 'gateway6', 'Default ipv6 gateway');
 };
 
+my $check_duplicate_ports = sub {
+    my ($config, $newiface, $newparam) = @_;
+
+    my $param_name;
+    my $get_portlist = sub {
+	my ($param) = @_;
+	my $ports = '';
+	for my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
+	    if ($param->{$k}) {
+		$ports .= " $param->{$k}";
+		$param_name //= $k;
+	    }
+	}
+	return PVE::Tools::split_list($ports);
+    };
+
+    my $new_ports = {};
+    for my $p ($get_portlist->($newparam)) {
+	$new_ports->{$p} = 1;
+    }
+    return if !(keys %$new_ports);
+
+    for my $iface (keys %$config) {
+	next if $iface eq $newiface;
+
+	my $d = $config->{$iface};
+	for my $p ($get_portlist->($d)) {
+	    raise_param_exc({ $param_name => "$p is already used on interface '$iface'." })
+		if $new_ports->{$p};
+	}
+    }
+};
+
 sub ipv6_tobin {
     return Net::IP::ip_iptobin(Net::IP::ip_expand_address(shift, 6), 6);
 }
@@ -387,6 +420,8 @@ __PACKAGE__->register_method({
 	    &$check_duplicate_gateway6($ifaces, $iface)
 		if $param->{gateway6};
 
+	    $check_duplicate_ports->($ifaces, $iface, $param);
+
 	    $map_cidr_to_address_netmask->($param);
 
 	    &$check_ipv6_settings($param->{address6}, int($param->{netmask6}))
@@ -488,6 +523,8 @@ __PACKAGE__->register_method({
 	    &$check_duplicate_gateway6($ifaces, $iface)
 		if $param->{gateway6};
 
+	    $check_duplicate_ports->($ifaces, $iface, $param);
+
 	    if ($param->{address}) {
 		push @$families, 'inet' if !grep(/^inet$/, @$families);
 	    } else {
-- 
2.20.1