]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Zones.pm
add vnet vlan-aware option
[pve-network.git] / PVE / Network / SDN / Zones.pm
CommitLineData
f5eabba0
AD
1package PVE::Network::SDN::Zones;
2
3use strict;
4use warnings;
5
6use Data::Dumper;
7use JSON;
8
9use PVE::Tools qw(extract_param dir_glob_regex run_command);
10use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
eb1549e7 11use PVE::Network;
f5eabba0
AD
12
13use PVE::Network::SDN::Vnets;
14use PVE::Network::SDN::Zones::VlanPlugin;
15use PVE::Network::SDN::Zones::QinQPlugin;
16use PVE::Network::SDN::Zones::VxlanPlugin;
17use PVE::Network::SDN::Zones::EvpnPlugin;
18use PVE::Network::SDN::Zones::FaucetPlugin;
19use PVE::Network::SDN::Zones::Plugin;
20
21PVE::Network::SDN::Zones::VlanPlugin->register();
22PVE::Network::SDN::Zones::QinQPlugin->register();
23PVE::Network::SDN::Zones::VxlanPlugin->register();
24PVE::Network::SDN::Zones::EvpnPlugin->register();
25PVE::Network::SDN::Zones::FaucetPlugin->register();
26PVE::Network::SDN::Zones::Plugin->init();
27
0814c9a9 28my $local_network_sdn_file = "/etc/network/interfaces.d/sdn";
f5eabba0
AD
29
30sub sdn_zones_config {
31 my ($cfg, $id, $noerr) = @_;
32
33 die "no sdn zone ID specified\n" if !$id;
34
35 my $scfg = $cfg->{ids}->{$id};
b2d83056 36 die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);
f5eabba0
AD
37
38 return $scfg;
39}
40
41sub config {
f703d2ae 42 my $config = cfs_read_file("sdn/zones.cfg");
f5eabba0
AD
43 return $config;
44}
45
41d40fb1
TL
46sub get_plugin_config {
47 my ($vnet) = @_;
48 my $zoneid = $vnet->{zone};
49 my $zone_cfg = PVE::Network::SDN::Zones::config();
50 return $zone_cfg->{ids}->{$zoneid};
51}
52
f5eabba0
AD
53sub write_config {
54 my ($cfg) = @_;
55
f703d2ae 56 cfs_write_file("sdn/zones.cfg", $cfg);
f5eabba0
AD
57}
58
f5eabba0
AD
59sub sdn_zones_ids {
60 my ($cfg) = @_;
61
62 return keys %{$cfg->{ids}};
63}
64
65sub complete_sdn_zone {
66 my ($cmdname, $pname, $cvalue) = @_;
67
68 my $cfg = PVE::Network::SDN::config();
69
70 return $cmdname eq 'add' ? [] : [ PVE::Network::SDN::sdn_zones_ids($cfg) ];
71}
72
73
74sub generate_etc_network_config {
75
0814c9a9 76 my $version = PVE::Cluster::cfs_read_file('sdn/.version');
f5eabba0 77 my $vnet_cfg = PVE::Cluster::cfs_read_file('sdn/vnets.cfg');
56cdcac9 78 my $zone_cfg = PVE::Cluster::cfs_read_file('sdn/zones.cfg');
4405f2de 79 my $controller_cfg = PVE::Cluster::cfs_read_file('sdn/controllers.cfg');
56cdcac9 80 return if !$vnet_cfg && !$zone_cfg;
f5eabba0 81
ba7ac021 82 my $interfaces_config = PVE::INotify::read_file('interfaces');
f5eabba0
AD
83
84 #generate configuration
85 my $config = {};
c2b9c173
AD
86 my $nodename = PVE::INotify::nodename();
87
e0c19383 88 for my $id (sort keys %{$vnet_cfg->{ids}}) {
f5eabba0 89 my $vnet = $vnet_cfg->{ids}->{$id};
3fd3e917 90 my $zone = $vnet->{zone};
f5eabba0 91
e0c19383
TL
92 if (!$zone) {
93 warn "can't generate vnet '$id': no zone assigned!\n";
f5eabba0
AD
94 next;
95 }
96
56cdcac9 97 my $plugin_config = $zone_cfg->{ids}->{$zone};
f5eabba0
AD
98
99 if (!defined($plugin_config)) {
e0c19383 100 warn "can't generate vnet '$id': zone $zone don't exist\n";
f5eabba0
AD
101 next;
102 }
103
c2b9c173
AD
104 next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
105
e0c19383
TL
106 my $controller;
107 if (my $controllerid = $plugin_config->{controller}) {
108 $controller = $controller_cfg->{ids}->{$controllerid};
4405f2de
AD
109 }
110
f5eabba0 111 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
ba7ac021 112 $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $controller, $interfaces_config, $config);
f5eabba0
AD
113 }
114
0814c9a9 115 my $raw_network_config = "\#version:$version\n";
2d4c068e 116 foreach my $iface (sort keys %$config) {
f5eabba0
AD
117 $raw_network_config .= "\n";
118 $raw_network_config .= "auto $iface\n";
119 $raw_network_config .= "iface $iface\n";
120 foreach my $option (@{$config->{$iface}}) {
121 $raw_network_config .= "\t$option\n";
122 }
123 }
124
125 return $raw_network_config;
126}
127
128sub write_etc_network_config {
129 my ($rawconfig) = @_;
130
131 return if !$rawconfig;
f5eabba0 132
0814c9a9 133 my $writefh = IO::File->new($local_network_sdn_file,">");
f5eabba0
AD
134 print $writefh $rawconfig;
135 $writefh->close();
136}
137
0814c9a9
AD
138sub read_etc_network_config_version {
139 my $versionstr = PVE::Tools::file_read_firstline($local_network_sdn_file);
06c87086
SI
140
141 return if !defined($versionstr);
142
0814c9a9
AD
143 if ($versionstr =~ m/^\#version:(\d+)$/) {
144 return $1;
145 }
146}
147
f5eabba0
AD
148sub ifquery_check {
149
150 my $cmd = ['ifquery', '-a', '-c', '-o','json'];
151
152 my $result = '';
153 my $reader = sub { $result .= shift };
154
155 eval {
156 run_command($cmd, outfunc => $reader);
157 };
158
159 my $resultjson = decode_json($result);
160 my $interfaces = {};
161
162 foreach my $interface (@$resultjson) {
163 my $name = $interface->{name};
164 $interfaces->{$name} = {
165 status => $interface->{status},
166 config => $interface->{config},
167 config_status => $interface->{config_status},
168 };
169 }
170
171 return $interfaces;
172}
173
174# improve me : move status code inside plugins ?
175sub status {
176
f5eabba0
AD
177 my $err_config = undef;
178
6f4d0610
AD
179 my $local_version = PVE::Network::SDN::Zones::read_etc_network_config_version();
180 my $sdn_version = PVE::Cluster::cfs_read_file('sdn/.version');
f5eabba0 181
6f4d0610 182 return if !$sdn_version;
3709a203 183
6f4d0610 184 if (!$local_version) {
3709a203 185 $err_config = "local sdn network configuration is not yet generated, please reload";
6933ca89 186 warn "$err_config\n";
6f4d0610
AD
187 } elsif ($local_version < $sdn_version) {
188 $err_config = "local sdn network configuration is too old, please reload";
189 warn "$err_config\n";
f5eabba0
AD
190 }
191
192 my $status = ifquery_check();
193
194 my $vnet_cfg = PVE::Cluster::cfs_read_file('sdn/vnets.cfg');
c2b9c173
AD
195 my $zone_cfg = PVE::Cluster::cfs_read_file('sdn/zones.cfg');
196 my $nodename = PVE::INotify::nodename();
f5eabba0 197
627b1694 198
f5eabba0 199 my $vnet_status = {};
56cdcac9 200 my $zone_status = {};
f5eabba0 201
70da0442
TL
202 foreach my $id (sort keys %{$vnet_cfg->{ids}}) {
203 my $vnet = $vnet_cfg->{ids}->{$id};
204 my $zone = $vnet->{zone};
627b1694 205 next if !$zone;
627b1694 206
70da0442
TL
207 my $plugin_config = $zone_cfg->{ids}->{$zone};
208 next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
209
210 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
211 $plugin->status($plugin_config, $zone, $id, $vnet, $err_config, $status, $vnet_status, $zone_status);
f5eabba0 212 }
627b1694 213
56cdcac9 214 return($zone_status, $vnet_status);
f5eabba0
AD
215}
216
eb1549e7
AD
217sub tap_create {
218 my ($iface, $bridge) = @_;
219
220 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
a1ef0eb9 221 if (!$vnet) { # fallback for classic bridge
eb1549e7 222 PVE::Network::tap_create($iface, $bridge);
a1ef0eb9 223 return;
eb1549e7
AD
224 }
225
41d40fb1 226 my $plugin_config = get_plugin_config($vnet);
eb1549e7
AD
227 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
228 $plugin->tap_create($plugin_config, $vnet, $iface, $bridge);
229}
230
231sub veth_create {
232 my ($veth, $vethpeer, $bridge, $hwaddr) = @_;
233
234 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
a1ef0eb9 235 if (!$vnet) { # fallback for classic bridge
eb1549e7 236 PVE::Network::veth_create($veth, $vethpeer, $bridge, $hwaddr);
a1ef0eb9 237 return;
eb1549e7
AD
238 }
239
41d40fb1 240 my $plugin_config = get_plugin_config($vnet);
eb1549e7
AD
241 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
242 $plugin->veth_create($plugin_config, $vnet, $veth, $vethpeer, $bridge, $hwaddr);
243}
244
245sub tap_plug {
246 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
247
248 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
a1ef0eb9 249 if (!$vnet) { # fallback for classic bridge
eb1549e7
AD
250 PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
251 return;
252 }
253
41d40fb1 254 my $plugin_config = get_plugin_config($vnet);
eb1549e7
AD
255 my $nodename = PVE::INotify::nodename();
256
41d40fb1
TL
257 die "vnet $bridge is not allowed on this node\n"
258 if $plugin_config->{nodes} && !defined($plugin_config->{nodes}->{$nodename});
2ba9613b 259
2ba9613b 260 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
912fb443 261 $plugin->tap_plug($plugin_config, $vnet, $tag, $iface, $bridge, $firewall, $trunks, $rate);
2ba9613b
AD
262}
263
f5eabba0
AD
2641;
265