]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Zones/SimplePlugin.pm
vnets: allow duplicate tags in differents zones
[pve-network.git] / PVE / Network / SDN / Zones / SimplePlugin.pm
CommitLineData
880ae857
AD
1package PVE::Network::SDN::Zones::SimplePlugin;
2
3use strict;
4use warnings;
5use PVE::Network::SDN::Zones::Plugin;
1d44ce70 6use PVE::Exception qw(raise raise_param_exc);
5ca07ed9
AD
7use PVE::Cluster;
8use PVE::Tools;
880ae857
AD
9
10use base('PVE::Network::SDN::Zones::Plugin');
11
12sub type {
13 return 'simple';
14}
15
4ad78442
AD
16sub properties {
17 return {
18 dns => {
19 type => 'string',
20 description => "dns api server",
21 },
22 reversedns => {
23 type => 'string',
24 description => "reverse dns api server",
25 },
26 dnszone => {
27 type => 'string', format => 'dns-name',
28 description => "dns domain zone ex: mydomain.com",
331e2330 29 }
4ad78442
AD
30 };
31}
32
880ae857 33sub options {
880ae857 34 return {
efe1459b 35 nodes => { optional => 1},
4ad78442
AD
36 mtu => { optional => 1 },
37 dns => { optional => 1 },
38 reversedns => { optional => 1 },
39 dnszone => { optional => 1 },
331e2330 40 ipam => { optional => 0 },
880ae857
AD
41 };
42}
43
44# Plugin implementation
45sub generate_sdn_config {
7024ec2b 46 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $subnet_cfg, $interfaces_config, $config) = @_;
880ae857 47
efe1459b
TL
48 return $config if$config->{$vnetid}; # nothing to do
49
880ae857
AD
50 my $ipv4 = $vnet->{ipv4};
51 my $ipv6 = $vnet->{ipv6};
52 my $mac = $vnet->{mac};
53 my $alias = $vnet->{alias};
54 my $mtu = $plugin_config->{mtu} if $plugin_config->{mtu};
55
efe1459b 56 # vnet bridge
880ae857 57 my @iface_config = ();
7024ec2b 58
e612faf6 59 my $address = {};
5d3e0248 60 my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
e612faf6
AD
61 foreach my $subnetid (sort keys %{$subnets}) {
62 my $subnet = $subnets->{$subnetid};
e8736dac
AD
63 my $cidr = $subnet->{cidr};
64 my $mask = $subnet->{mask};
65
e612faf6
AD
66 my $gateway = $subnet->{gateway};
67 if ($gateway) {
68 push @iface_config, "address $gateway" if !defined($address->{$gateway});
69 $address->{$gateway} = 1;
70 }
71 #add route for /32 pointtopoint
e612faf6 72 push @iface_config, "up ip route add $cidr dev $vnetid" if $mask == 32;
53b2cc90
AD
73 if ($subnet->{snat}) {
74 #find outgoing interface
75 my ($outip, $outiface) = PVE::Network::SDN::Zones::Plugin::get_local_route_ip('8.8.8.8');
76 if ($outip && $outiface) {
77 #use snat, faster than masquerade
78 push @iface_config, "post-up iptables -t nat -A POSTROUTING -s '$cidr' -o $outiface -j SNAT --to-source $outip";
79 push @iface_config, "post-down iptables -t nat -D POSTROUTING -s '$cidr' -o $outiface -j SNAT --to-source $outip";
80 #add conntrack zone once on outgoing interface
81 push @iface_config, "post-up iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1";
82 push @iface_config, "post-down iptables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1";
83 }
84 }
7024ec2b
AD
85 }
86
880ae857
AD
87 push @iface_config, "hwaddress $mac" if $mac;
88 push @iface_config, "bridge_ports none";
89 push @iface_config, "bridge_stp off";
90 push @iface_config, "bridge_fd 0";
efe1459b 91 if ($vnet->{vlanaware}) {
880ae857
AD
92 push @iface_config, "bridge-vlan-aware yes";
93 push @iface_config, "bridge-vids 2-4094";
94 }
95 push @iface_config, "mtu $mtu" if $mtu;
96 push @iface_config, "alias $alias" if $alias;
efe1459b
TL
97
98 push @{$config->{$vnetid}}, @iface_config;
880ae857
AD
99
100 return $config;
101}
102
103sub status {
104 my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_;
105
880ae857 106 # ifaces to check
efe1459b
TL
107 my $ifaces = [ $vnetid ];
108 my $err_msg = [];
880ae857
AD
109 foreach my $iface (@{$ifaces}) {
110 if (!$status->{$iface}->{status}) {
111 push @$err_msg, "missing $iface";
efe1459b 112 } elsif ($status->{$iface}->{status} ne 'pass') {
880ae857
AD
113 push @$err_msg, "error iface $iface";
114 }
115 }
116 return $err_msg;
117}
118
1d44ce70 119
5ca07ed9 120sub vnet_update_hook {
88d9562b 121 my ($class, $vnet_cfg, $vnetid, $zone_cfg) = @_;
5ca07ed9 122
88d9562b
AD
123 my $vnet = $vnet_cfg->{ids}->{$vnetid};
124 my $tag = $vnet->{tag};
125
126 raise_param_exc({ tag => "vlan tag is not allowed on simple zone"}) if defined($tag);
5ca07ed9
AD
127
128 if (!defined($vnet->{mac})) {
129 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
130 $vnet->{mac} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
131 }
1d44ce70
AD
132}
133
880ae857
AD
1341;
135
136