]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Zones.pm
84ce60ce185c380f929f91330275a59a7b6d3963
[pve-network.git] / PVE / Network / SDN / Zones.pm
1 package PVE::Network::SDN::Zones;
2
3 use strict;
4 use warnings;
5
6 use Data::Dumper;
7 use JSON;
8
9 use PVE::Tools qw(extract_param dir_glob_regex run_command);
10 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
11
12 use PVE::Network::SDN::Vnets;
13 use PVE::Network::SDN::Zones::VlanPlugin;
14 use PVE::Network::SDN::Zones::QinQPlugin;
15 use PVE::Network::SDN::Zones::VxlanPlugin;
16 use PVE::Network::SDN::Zones::EvpnPlugin;
17 use PVE::Network::SDN::Zones::FaucetPlugin;
18 use PVE::Network::SDN::Zones::Plugin;
19
20 PVE::Network::SDN::Zones::VlanPlugin->register();
21 PVE::Network::SDN::Zones::QinQPlugin->register();
22 PVE::Network::SDN::Zones::VxlanPlugin->register();
23 PVE::Network::SDN::Zones::EvpnPlugin->register();
24 PVE::Network::SDN::Zones::FaucetPlugin->register();
25 PVE::Network::SDN::Zones::Plugin->init();
26
27
28 sub sdn_zones_config {
29 my ($cfg, $id, $noerr) = @_;
30
31 die "no sdn zone ID specified\n" if !$id;
32
33 my $scfg = $cfg->{ids}->{$id};
34 die "sdn '$id' does not exists\n" if (!$noerr && !$scfg);
35
36 return $scfg;
37 }
38
39 sub config {
40 my $config = cfs_read_file("sdn/zones.cfg.new");
41 $config = cfs_read_file("sdn/zones.cfg") if !keys %{$config->{ids}};
42 return $config;
43 }
44
45 sub write_config {
46 my ($cfg) = @_;
47
48 cfs_write_file("sdn/zones.cfg.new", $cfg);
49 }
50
51 sub lock_sdn_zones_config {
52 my ($code, $errmsg) = @_;
53
54 cfs_lock_file("sdn/zones.cfg.new", undef, $code);
55 if (my $err = $@) {
56 $errmsg ? die "$errmsg: $err" : die $err;
57 }
58 }
59
60 sub sdn_zones_ids {
61 my ($cfg) = @_;
62
63 return keys %{$cfg->{ids}};
64 }
65
66 sub complete_sdn_zone {
67 my ($cmdname, $pname, $cvalue) = @_;
68
69 my $cfg = PVE::Network::SDN::config();
70
71 return $cmdname eq 'add' ? [] : [ PVE::Network::SDN::sdn_zones_ids($cfg) ];
72 }
73
74
75 sub generate_etc_network_config {
76
77 my $vnet_cfg = PVE::Cluster::cfs_read_file('sdn/vnets.cfg');
78 my $zone_cfg = PVE::Cluster::cfs_read_file('sdn/zones.cfg');
79 return if !$vnet_cfg && !$zone_cfg;
80
81 #read main config for physical interfaces
82 my $current_config_file = "/etc/network/interfaces";
83 my $fh = IO::File->new($current_config_file);
84 my $interfaces_config = PVE::INotify::read_etc_network_interfaces(1,$fh);
85 $fh->close();
86
87 #check uplinks
88 my $uplinks = {};
89 foreach my $id (keys %{$interfaces_config->{ifaces}}) {
90 my $interface = $interfaces_config->{ifaces}->{$id};
91 if (my $uplink = $interface->{'uplink-id'}) {
92 die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
93 $interface->{name} = $id;
94 $uplinks->{$interface->{'uplink-id'}} = $interface;
95 }
96 }
97
98 #generate configuration
99 my $config = {};
100 my $nodename = PVE::INotify::nodename();
101
102 foreach my $id (keys %{$vnet_cfg->{ids}}) {
103 my $vnet = $vnet_cfg->{ids}->{$id};
104 my $zone = $vnet->{zone};
105
106 if(!$zone) {
107 warn "can't generate vnet $vnet : zone $zone don't exist";
108 next;
109 }
110
111 my $plugin_config = $zone_cfg->{ids}->{$zone};
112
113 if (!defined($plugin_config)) {
114 warn "can't generate vnet $vnet : zone $zone don't exist";
115 next;
116 }
117
118 next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
119
120 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
121 $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $uplinks, $config);
122 }
123
124 my $raw_network_config = "";
125 foreach my $iface (keys %$config) {
126 $raw_network_config .= "\n";
127 $raw_network_config .= "auto $iface\n";
128 $raw_network_config .= "iface $iface\n";
129 foreach my $option (@{$config->{$iface}}) {
130 $raw_network_config .= "\t$option\n";
131 }
132 }
133
134 return $raw_network_config;
135 }
136
137 sub write_etc_network_config {
138 my ($rawconfig) = @_;
139
140 return if !$rawconfig;
141 my $sdn_interfaces_file = "/etc/network/interfaces.d/sdn";
142
143 my $writefh = IO::File->new($sdn_interfaces_file,">");
144 print $writefh $rawconfig;
145 $writefh->close();
146 }
147
148 sub 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 ?
175 sub status {
176
177 my $cluster_vnet_file = "/etc/pve/sdn/vnets.cfg";
178 my $cluster_zone_file = "/etc/pve/sdn/zones.cfg";
179 my $local_sdn_file = "/etc/network/interfaces.d/sdn";
180 my $err_config = undef;
181
182 return if !-e $cluster_vnet_file && !-e $cluster_zone_file;
183
184 if (!-e $local_sdn_file) {
185
186 $err_config = "local sdn network configuration is not yet generated, please reload";
187 warn $err_config;
188 } else {
189 # fixme : use some kind of versioning info?
190 my $cluster_vnet_timestamp = (stat($cluster_vnet_file))[9];
191 my $cluster_zone_timestamp = (stat($cluster_zone_file))[9];
192 my $local_sdn_timestamp = (stat($local_sdn_file))[9];
193
194 if ($local_sdn_timestamp < $cluster_vnet_timestamp || $local_sdn_timestamp < $cluster_zone_timestamp) {
195 $err_config = "local sdn network configuration is too old, please reload";
196 warn $err_config;
197 }
198 }
199
200 my $status = ifquery_check();
201
202 my $vnet_cfg = PVE::Cluster::cfs_read_file('sdn/vnets.cfg');
203 my $zone_cfg = PVE::Cluster::cfs_read_file('sdn/zones.cfg');
204 my $nodename = PVE::INotify::nodename();
205
206 my $vnet_status = {};
207 my $zone_status = {};
208
209 foreach my $id (keys %{$vnet_cfg->{ids}}) {
210 my $zone = $vnet_cfg->{ids}->{$id}->{zone};
211 next if defined($zone_cfg->{ids}->{$zone}->{nodes}) && !$zone_cfg->{ids}->{$zone}->{nodes}->{$nodename};
212
213 $vnet_status->{$id}->{zone} = $zone;
214 $zone_status->{$zone}->{status} = 'available' if !defined($zone_status->{$zone}->{status});
215
216 if($err_config) {
217 $vnet_status->{$id}->{status} = 'pending';
218 $vnet_status->{$id}->{statusmsg} = $err_config;
219 $zone_status->{$zone}->{status} = 'pending';
220 } elsif ($status->{$id}->{status} && $status->{$id}->{status} eq 'pass') {
221 $vnet_status->{$id}->{status} = 'available';
222 my $bridgeport = $status->{$id}->{config}->{'bridge-ports'};
223
224 if ($status->{$bridgeport}->{status} && $status->{$bridgeport}->{status} ne 'pass') {
225 $vnet_status->{$id}->{status} = 'error';
226 $vnet_status->{$id}->{statusmsg} = 'configuration not fully applied';
227 $zone_status->{$zone}->{status} = 'error';
228 }
229 } else {
230 $vnet_status->{$id}->{status} = 'error';
231 $vnet_status->{$id}->{statusmsg} = 'missing';
232 $zone_status->{$zone}->{status} = 'error';
233 }
234 }
235 return($zone_status, $vnet_status);
236 }
237
238 1;
239