]> git.proxmox.com Git - pve-network.git/blobdiff - PVE/Network/SDN/SubnetPlugin.pm
ipams: add mac address
[pve-network.git] / PVE / Network / SDN / SubnetPlugin.pm
index a5d03f6be6c825f32485c5454d7343902d86a7e7..68efeb66654c5b1bd3d74f4fde7d6036825f3907 100644 (file)
@@ -3,13 +3,16 @@ package PVE::Network::SDN::SubnetPlugin;
 use strict;
 use warnings;
 
+use Net::IP;
+use Net::Subnet qw(subnet_matcher);
+
 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
-use base qw(PVE::SectionConfig);
-use PVE::JSONSchema qw(get_standard_option);
 use PVE::Exception qw(raise raise_param_exc);
-use Net::Subnet qw(subnet_matcher);
-use PVE::Network::SDN::Vnets;
+use PVE::JSONSchema qw(get_standard_option);
 use PVE::Network::SDN::Ipams;
+use PVE::Network::SDN::Vnets;
+
+use base qw(PVE::SectionConfig);
 
 PVE::Cluster::cfs_register_file('sdn/subnets.cfg',
                                  sub { __PACKAGE__->parse_config(@_); },
@@ -25,7 +28,13 @@ PVE::JSONSchema::register_format('pve-sdn-subnet-id', \&parse_sdn_subnet_id);
 sub parse_sdn_subnet_id {
     my ($id, $noerr) = @_;
 
-    my $cidr = $id =~ s/-/\//r;
+    my $cidr = "";
+    if($id =~ /\//) {
+       $cidr = $id;
+    } else {
+       my ($zone, $ip, $mask) = split(/-/, $id);
+       $cidr = "$ip/$mask";
+    }
 
     if (!(PVE::JSONSchema::pve_verify_cidrv4($cidr, 1) ||
           PVE::JSONSchema::pve_verify_cidrv6($cidr, 1)))
@@ -91,7 +100,9 @@ sub options {
 sub on_update_hook {
     my ($class, $zone, $subnetid, $subnet, $old_subnet) = @_;
 
-    my $cidr = $subnetid =~ s/-/\//r;
+    my $cidr = $subnet->{cidr};
+    my $mask = $subnet->{mask};
+
     my $subnet_matcher = subnet_matcher($cidr);
 
     my $vnetid = $subnet->{vnet};
@@ -102,16 +113,20 @@ sub on_update_hook {
     my $reversedns = $zone->{reversedns};
 
     my $old_gateway = $old_subnet->{gateway} if $old_subnet;
+    my $mac = undef;
 
     if($vnetid) {
        my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
        raise_param_exc({ vnet => "$vnetid don't exist"}) if !$vnet;
        raise_param_exc({ vnet => "you can't add a subnet on a vlanaware vnet"}) if $vnet->{vlanaware};
+       $mac = $vnet->{mac};
     }
 
-    my ($ip, $mask) = split(/\//, $cidr);
+    my $pointopoint = 1 if Net::IP::ip_is_ipv4($gateway) && $mask == 32;
+
     #for /32 pointopoint, we allow gateway outside the subnet
-    raise_param_exc({ gateway => "$gateway is not in subnet $subnetid"}) if $gateway && !$subnet_matcher->($gateway) && $mask != 32;
+    raise_param_exc({ gateway => "$gateway is not in subnet $cidr"}) if $gateway && !$subnet_matcher->($gateway) && !$pointopoint;
+
 
     if ($ipam) {
        my $ipam_cfg = PVE::Network::SDN::Ipams::config();
@@ -119,7 +134,10 @@ sub on_update_hook {
        my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
        $plugin->add_subnet($plugin_config, $subnetid, $subnet);
 
-       #delete on removal
+       #don't register gateway for pointopoint
+       return if $pointopoint;
+
+       #delete gateway on removal
        if (!defined($gateway) && $old_gateway) {
            eval {
                PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $old_subnet, $old_gateway);
@@ -127,10 +145,12 @@ sub on_update_hook {
            warn if $@;
        }
         if(!$old_gateway || $gateway && $gateway ne $old_gateway) {
-           PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $gateway);
+           my $hostname = "$vnetid-gw";
+           my $description = "$vnetid gw";
+           PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $gateway, $hostname, $mac, $description, 1);
        }
 
-       #delete old ip after update
+       #delete old gateway after update
        if($gateway && $old_gateway && $gateway ne $old_gateway) {
            eval {
                PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $old_subnet, $old_gateway);