1 package PVE
::Network
::SDN
::Zones
;
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);
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
;
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();
28 sub sdn_zones_config
{
29 my ($cfg, $id, $noerr) = @_;
31 die "no sdn zone ID specified\n" if !$id;
33 my $scfg = $cfg->{ids
}->{$id};
34 die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);
40 my $config = cfs_read_file
("sdn/zones.cfg.new");
41 $config = cfs_read_file
("sdn/zones.cfg") if !keys %{$config->{ids
}};
48 cfs_write_file
("sdn/zones.cfg.new", $cfg);
51 sub lock_sdn_zones_config
{
52 my ($code, $errmsg) = @_;
54 cfs_lock_file
("sdn/zones.cfg.new", undef, $code);
56 $errmsg ?
die "$errmsg: $err" : die $err;
63 return keys %{$cfg->{ids
}};
66 sub complete_sdn_zone
{
67 my ($cmdname, $pname, $cvalue) = @_;
69 my $cfg = PVE
::Network
::SDN
::config
();
71 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::sdn_zones_ids
($cfg) ];
75 sub generate_etc_network_config
{
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 my $controller_cfg = PVE
::Cluster
::cfs_read_file
('sdn/controllers.cfg');
80 return if !$vnet_cfg && !$zone_cfg;
82 my $interfaces_config = PVE
::INotify
::read_file
('interfaces');
84 #generate configuration
86 my $nodename = PVE
::INotify
::nodename
();
88 foreach my $id (keys %{$vnet_cfg->{ids
}}) {
89 my $vnet = $vnet_cfg->{ids
}->{$id};
90 my $zone = $vnet->{zone
};
93 warn "can't generate vnet $vnet : zone $zone don't exist";
97 my $plugin_config = $zone_cfg->{ids
}->{$zone};
99 if (!defined($plugin_config)) {
100 warn "can't generate vnet $vnet : zone $zone don't exist";
104 next if defined($plugin_config->{nodes
}) && !$plugin_config->{nodes
}->{$nodename};
106 my $controller = undef;
107 if($plugin_config->{controller
}) {
108 my $controllerid = $plugin_config->{controller
};
109 $controller = $controller_cfg->{ids
}->{$controllerid};
112 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
113 $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $controller, $interfaces_config, $config);
116 my $raw_network_config = "";
117 foreach my $iface (keys %$config) {
118 $raw_network_config .= "\n";
119 $raw_network_config .= "auto $iface\n";
120 $raw_network_config .= "iface $iface\n";
121 foreach my $option (@{$config->{$iface}}) {
122 $raw_network_config .= "\t$option\n";
126 return $raw_network_config;
129 sub write_etc_network_config
{
130 my ($rawconfig) = @_;
132 return if !$rawconfig;
133 my $sdn_interfaces_file = "/etc/network/interfaces.d/sdn";
135 my $writefh = IO
::File-
>new($sdn_interfaces_file,">");
136 print $writefh $rawconfig;
142 my $cmd = ['ifquery', '-a', '-c', '-o','json'];
145 my $reader = sub { $result .= shift };
148 run_command
($cmd, outfunc
=> $reader);
151 my $resultjson = decode_json
($result);
154 foreach my $interface (@$resultjson) {
155 my $name = $interface->{name
};
156 $interfaces->{$name} = {
157 status
=> $interface->{status
},
158 config
=> $interface->{config
},
159 config_status
=> $interface->{config_status
},
166 # improve me : move status code inside plugins ?
169 my $cluster_vnet_file = "/etc/pve/sdn/vnets.cfg";
170 my $cluster_zone_file = "/etc/pve/sdn/zones.cfg";
171 my $local_sdn_file = "/etc/network/interfaces.d/sdn";
172 my $err_config = undef;
174 return if !-e
$cluster_vnet_file && !-e
$cluster_zone_file;
176 if (!-e
$local_sdn_file) {
178 $err_config = "local sdn network configuration is not yet generated, please reload";
181 # fixme : use some kind of versioning info?
182 my $cluster_vnet_timestamp = (stat($cluster_vnet_file))[9];
183 my $cluster_zone_timestamp = (stat($cluster_zone_file))[9];
184 my $local_sdn_timestamp = (stat($local_sdn_file))[9];
186 if ($local_sdn_timestamp < $cluster_vnet_timestamp || $local_sdn_timestamp < $cluster_zone_timestamp) {
187 $err_config = "local sdn network configuration is too old, please reload";
192 my $status = ifquery_check
();
194 my $vnet_cfg = PVE
::Cluster
::cfs_read_file
('sdn/vnets.cfg');
195 my $zone_cfg = PVE
::Cluster
::cfs_read_file
('sdn/zones.cfg');
196 my $nodename = PVE
::INotify
::nodename
();
199 my $vnet_status = {};
200 my $zone_status = {};
202 foreach my $id (keys %{$vnet_cfg->{ids
}}) {
203 my $vnet = $vnet_cfg->{ids
}->{$id};
204 my $zone = $vnet->{zone
};
207 my $plugin_config = $zone_cfg->{ids
}->{$zone};
208 next if defined($plugin_config->{nodes
}) && !$plugin_config->{nodes
}->{$nodename};
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);
214 return($zone_status, $vnet_status);