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