]> git.proxmox.com Git - pve-network.git/blobdiff - PVE/Network/SDN/Zones/Plugin.pm
zones: add bridge helpers
[pve-network.git] / PVE / Network / SDN / Zones / Plugin.pm
index e4adcbd73927473c7048dd0e2204c267012e733f..9eabe732b784ad2e0caba5f7fb5835c443abbeb0 100644 (file)
@@ -13,9 +13,6 @@ use PVE::JSONSchema qw(get_standard_option);
 use base qw(PVE::SectionConfig);
 
 PVE::Cluster::cfs_register_file('sdn/zones.cfg',
-                                sub { __PACKAGE__->parse_config(@_); });
-
-PVE::Cluster::cfs_register_file('sdn/zones.cfg.new',
                                 sub { __PACKAGE__->parse_config(@_); },
                                 sub { __PACKAGE__->write_config(@_); });
 
@@ -32,7 +29,7 @@ sub parse_sdn_zone_id {
        return undef if $noerr;
        die "zone ID '$id' contains illegal characters\n";
     }
-    die "zone ID '$id' can't be more length than 10 characters\n" if length($id) > 10;
+    die "zone ID '$id' can't be more length than 8 characters\n" if length($id) > 8;
     return $id;
 }
 
@@ -47,6 +44,10 @@ my $defaultData = {
         nodes => get_standard_option('pve-node-list', { optional => 1 }),
         zone => get_standard_option('pve-sdn-zone-id',
             { completion => \&PVE::Network::SDN::Zones::complete_sdn_zone }),
+        ipam => {
+            type => 'string',
+            description => "use a specific ipam",
+        },
     },
 };
 
@@ -54,50 +55,50 @@ sub private {
     return $defaultData;
 }
 
+sub parse_section_header {
+    my ($class, $line) = @_;
+
+    if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
+        my ($type, $id) = (lc($1), $2);
+       my $errmsg = undef; # set if you want to skip whole section
+       eval { PVE::JSONSchema::pve_verify_configid($type); };
+       $errmsg = $@ if $@;
+       my $config = {}; # to return additional attributes
+       return ($type, $id, $errmsg, $config);
+    }
+    return undef;
+}
+
 sub decode_value {
     my ($class, $type, $key, $value) = @_;
 
-    if ($key eq 'nodes') {
-        my $res = {};
+    if ($key eq 'nodes' || $key eq 'exitnodes') {
+       my $res = {};
 
-        foreach my $node (PVE::Tools::split_list($value)) {
-            if (PVE::JSONSchema::pve_verify_node_name($node)) {
-                $res->{$node} = 1;
-            }
-        }
+       foreach my $node (PVE::Tools::split_list($value)) {
+           if (PVE::JSONSchema::pve_verify_node_name($node)) {
+               $res->{$node} = 1;
+           }
+       }
 
-        return $res;
-    } 
+       return $res;
+    }
 
-   return $value;
+    return $value;
 }
 
 sub encode_value {
     my ($class, $type, $key, $value) = @_;
 
-    if ($key eq 'nodes') {
-        return join(',', keys(%$value));
+    if ($key eq 'nodes' || $key eq 'exitnodes') {
+       return join(',', keys(%$value));
     }
 
     return $value;
 }
 
-sub parse_section_header {
-    my ($class, $line) = @_;
-
-    if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
-        my ($type, $id) = (lc($1), $2);
-       my $errmsg = undef; # set if you want to skip whole section
-       eval { PVE::JSONSchema::pve_verify_configid($type); };
-       $errmsg = $@ if $@;
-       my $config = {}; # to return additional attributes
-       return ($type, $id, $errmsg, $config);
-    }
-    return undef;
-}
-
 sub generate_sdn_config {
-    my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $interfaces_config, $config) = @_;
+    my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $controller_cfg, $subnet_cfg, $interfaces_config, $config) = @_;
 
     die "please implement inside plugin";
 }
@@ -142,6 +143,12 @@ sub on_update_hook {
     # do nothing by default
 }
 
+sub vnet_update_hook {
+    my ($class, $vnet_cfg, $vnetid, $zone_cfg) = @_;
+
+    # do nothing by default
+}
+
 #helpers
 sub parse_tag_number_or_range {
     my ($str, $max, $tag) = @_;
@@ -181,69 +188,41 @@ sub parse_tag_number_or_range {
 }
 
 sub status {
-    my ($class, $plugin_config, $zone, $id, $vnet, $err_config, $status, $vnet_status, $zone_status) = @_;
-
-    $vnet_status->{$id}->{zone} = $zone;
-    $zone_status->{$zone}->{status} = 'available' if !defined($zone_status->{$zone}->{status});
-
-    if($err_config) {
-       $vnet_status->{$id}->{status} = 'pending';
-       $vnet_status->{$id}->{statusmsg} = $err_config;
-       $zone_status->{$zone}->{status} = 'pending';
-    } elsif ($status->{$id}->{status} && $status->{$id}->{status} eq 'pass') {
-       $vnet_status->{$id}->{status} = 'available';
-       my $bridgeport = $status->{$id}->{config}->{'bridge-ports'};
-
-       if ($bridgeport && $status->{$bridgeport}->{status} && $status->{$bridgeport}->{status} ne 'pass') {
-           $vnet_status->{$id}->{status} = 'error';
-           $vnet_status->{$id}->{statusmsg} = 'configuration not fully applied';
-           $zone_status->{$zone}->{status} = 'error';
-       }
+    my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_;
 
-    } else {
-       $vnet_status->{$id}->{status} = 'error';
-       $vnet_status->{$id}->{statusmsg} = 'missing';
-       $zone_status->{$zone}->{status} = 'error';
-    }
-}
+    my $err_msg = [];
 
+    # ifaces to check
+    my $ifaces = [ $vnetid ];
 
-sub get_bridge_vlan {
-    my ($class, $plugin_config, $vnetid, $tag) = @_;
-
-    my $bridge = $vnetid;
-    $tag = undef;
-
-    die "bridge $bridge is missing" if !-d "/sys/class/net/$bridge/";
-
-    return ($bridge, $tag);
+    foreach my $iface (@{$ifaces}) {
+        if (!$status->{$iface}->{status}) {
+           push @$err_msg, "missing $iface";
+        } elsif ($status->{$iface}->{status} ne 'pass') {
+           push @$err_msg, "error $iface";
+        }
+    }
+    return $err_msg;
 }
 
+
 sub tap_create {
     my ($class, $plugin_config, $vnet, $iface, $vnetid) = @_;
 
-    my ($bridge, undef) = $class->get_bridge_vlan($plugin_config, $vnetid);
-    die "unable to get bridge setting\n" if !$bridge;
-
-    PVE::Network::tap_create($iface, $bridge);
+    PVE::Network::tap_create($iface, $vnetid);
 }
 
 sub veth_create {
     my ($class, $plugin_config, $vnet, $veth, $vethpeer, $vnetid, $hwaddr) = @_;
 
-    my ($bridge, undef) = $class->get_bridge_vlan($plugin_config, $vnetid);
-    die "unable to get bridge setting\n" if !$bridge;
-
-    PVE::Network::veth_create($veth, $vethpeer, $bridge, $hwaddr);
+    PVE::Network::veth_create($veth, $vethpeer, $vnetid, $hwaddr);
 }
 
 sub tap_plug {
-    my ($class, $plugin_config, $vnet, $iface, $vnetid, $firewall, $rate) = @_;
+    my ($class, $plugin_config, $vnet, $tag, $iface, $vnetid, $firewall, $trunks, $rate) = @_;
 
-    my $tag = $vnet->{tag};
-
-    ($vnetid, $tag) = $class->get_bridge_vlan($plugin_config, $vnetid, $tag);
-    my $trunks = undef;
+    my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$vnetid/bridge/vlan_filtering");
+    die "vm vlans are not allowed on vnet $vnetid" if !$vlan_aware && ($tag || $trunks);
 
     PVE::Network::tap_plug($iface, $vnetid, $tag, $firewall, $trunks, $rate);
 }
@@ -295,10 +274,17 @@ sub get_local_route_ip {
 
 
 sub find_local_ip_interface_peers {
-    my ($peers) = @_;
+    my ($peers, $iface) = @_;
 
     my $network_config = PVE::INotify::read_file('interfaces');
     my $ifaces = $network_config->{ifaces};
+    
+    #if iface is defined, return ip if exist (if not,try to find it on other ifaces)
+    if ($iface) {
+       my $ip = $ifaces->{$iface}->{address};
+       return ($ip,$iface) if $ip;
+    }
+
     #is a local ip member of peers list ?
     foreach my $address (@{$peers}) {
        while (my $interface = each %$ifaces) {
@@ -316,4 +302,34 @@ sub find_local_ip_interface_peers {
     }
 }
 
+sub find_bridge {
+    my ($bridge) = @_;
+
+    die "can't find bridge $bridge" if !-d "/sys/class/net/$bridge";
+}
+
+sub is_vlanaware {
+    my ($bridge) = @_;
+
+    return PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
+}
+
+sub is_ovs {
+    my ($bridge) = @_;
+
+    my $is_ovs = !-d "/sys/class/net/$bridge/brif";
+    return $is_ovs;    
+}
+
+sub get_bridge_ifaces {
+    my ($bridge) = @_;
+
+    my @bridge_ifaces = ();
+    my $dir = "/sys/class/net/$bridge/brif";
+    PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
+       push @bridge_ifaces, $_[0];
+    });
+
+    return @bridge_ifaces;
+}
 1;