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);
13 use PVE
::Network
::SDN
::Vnets
;
14 use PVE
::Network
::SDN
::Zones
::VlanPlugin
;
15 use PVE
::Network
::SDN
::Zones
::QinQPlugin
;
16 use PVE
::Network
::SDN
::Zones
::VxlanPlugin
;
17 use PVE
::Network
::SDN
::Zones
::EvpnPlugin
;
18 use PVE
::Network
::SDN
::Zones
::FaucetPlugin
;
19 use PVE
::Network
::SDN
::Zones
::Plugin
;
21 PVE
::Network
::SDN
::Zones
::VlanPlugin-
>register();
22 PVE
::Network
::SDN
::Zones
::QinQPlugin-
>register();
23 PVE
::Network
::SDN
::Zones
::VxlanPlugin-
>register();
24 PVE
::Network
::SDN
::Zones
::EvpnPlugin-
>register();
25 PVE
::Network
::SDN
::Zones
::FaucetPlugin-
>register();
26 PVE
::Network
::SDN
::Zones
::Plugin-
>init();
28 my $local_network_sdn_file = "/etc/network/interfaces.d/sdn";
30 sub sdn_zones_config
{
31 my ($cfg, $id, $noerr) = @_;
33 die "no sdn zone ID specified\n" if !$id;
35 my $scfg = $cfg->{ids
}->{$id};
36 die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);
42 my $config = cfs_read_file
("sdn/zones.cfg");
46 sub get_plugin_config
{
48 my $zoneid = $vnet->{zone
};
49 my $zone_cfg = PVE
::Network
::SDN
::Zones
::config
();
50 return $zone_cfg->{ids
}->{$zoneid};
56 cfs_write_file
("sdn/zones.cfg", $cfg);
62 return keys %{$cfg->{ids
}};
65 sub complete_sdn_zone
{
66 my ($cmdname, $pname, $cvalue) = @_;
68 my $cfg = PVE
::Network
::SDN
::config
();
70 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::sdn_zones_ids
($cfg) ];
74 sub generate_etc_network_config
{
76 my $version = PVE
::Cluster
::cfs_read_file
('sdn/.version');
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 for my $id (sort keys %{$vnet_cfg->{ids
}}) {
89 my $vnet = $vnet_cfg->{ids
}->{$id};
90 my $zone = $vnet->{zone
};
93 warn "can't generate vnet '$id': no zone assigned!\n";
97 my $plugin_config = $zone_cfg->{ids
}->{$zone};
99 if (!defined($plugin_config)) {
100 warn "can't generate vnet '$id': zone $zone don't exist\n";
104 next if defined($plugin_config->{nodes
}) && !$plugin_config->{nodes
}->{$nodename};
107 if (my $controllerid = $plugin_config->{controller
}) {
108 $controller = $controller_cfg->{ids
}->{$controllerid};
111 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 warn "zone $zone : vnet $id : $@";
121 my $raw_network_config = "\#version:$version\n";
122 foreach my $iface (sort keys %$config) {
123 $raw_network_config .= "\n";
124 $raw_network_config .= "auto $iface\n";
125 $raw_network_config .= "iface $iface\n";
126 foreach my $option (@{$config->{$iface}}) {
127 $raw_network_config .= "\t$option\n";
131 return $raw_network_config;
134 sub write_etc_network_config
{
135 my ($rawconfig) = @_;
137 return if !$rawconfig;
139 my $writefh = IO
::File-
>new($local_network_sdn_file,">");
140 print $writefh $rawconfig;
144 sub read_etc_network_config_version
{
145 my $versionstr = PVE
::Tools
::file_read_firstline
($local_network_sdn_file);
147 return if !defined($versionstr);
149 if ($versionstr =~ m/^\#version:(\d+)$/) {
156 my $cmd = ['ifquery', '-a', '-c', '-o','json'];
159 my $reader = sub { $result .= shift };
162 run_command
($cmd, outfunc
=> $reader);
165 my $resultjson = decode_json
($result);
168 foreach my $interface (@$resultjson) {
169 my $name = $interface->{name
};
170 $interfaces->{$name} = {
171 status
=> $interface->{status
},
172 config
=> $interface->{config
},
173 config_status
=> $interface->{config_status
},
180 # improve me : move status code inside plugins ?
183 my $err_config = undef;
185 my $local_version = PVE
::Network
::SDN
::Zones
::read_etc_network_config_version
();
186 my $sdn_version = PVE
::Cluster
::cfs_read_file
('sdn/.version');
188 return if !$sdn_version;
190 if (!$local_version) {
191 $err_config = "local sdn network configuration is not yet generated, please reload";
192 warn "$err_config\n";
193 } elsif ($local_version < $sdn_version) {
194 $err_config = "local sdn network configuration is too old, please reload";
195 warn "$err_config\n";
198 my $status = ifquery_check
();
200 my $vnet_cfg = PVE
::Cluster
::cfs_read_file
('sdn/vnets.cfg');
201 my $zone_cfg = PVE
::Cluster
::cfs_read_file
('sdn/zones.cfg');
202 my $nodename = PVE
::INotify
::nodename
();
205 my $vnet_status = {};
206 my $zone_status = {};
208 foreach my $id (sort keys %{$vnet_cfg->{ids
}}) {
209 my $vnet = $vnet_cfg->{ids
}->{$id};
210 my $zone = $vnet->{zone
};
213 my $plugin_config = $zone_cfg->{ids
}->{$zone};
214 next if defined($plugin_config->{nodes
}) && !$plugin_config->{nodes
}->{$nodename};
216 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
217 $plugin->status($plugin_config, $zone, $id, $vnet, $err_config, $status, $vnet_status, $zone_status);
220 return($zone_status, $vnet_status);
224 my ($iface, $bridge) = @_;
226 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($bridge);
227 if (!$vnet) { # fallback for classic bridge
228 PVE
::Network
::tap_create
($iface, $bridge);
232 my $plugin_config = get_plugin_config
($vnet);
233 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
234 $plugin->tap_create($plugin_config, $vnet, $iface, $bridge);
238 my ($veth, $vethpeer, $bridge, $hwaddr) = @_;
240 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($bridge);
241 if (!$vnet) { # fallback for classic bridge
242 PVE
::Network
::veth_create
($veth, $vethpeer, $bridge, $hwaddr);
246 my $plugin_config = get_plugin_config
($vnet);
247 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
248 $plugin->veth_create($plugin_config, $vnet, $veth, $vethpeer, $bridge, $hwaddr);
252 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
254 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($bridge);
255 if (!$vnet) { # fallback for classic bridge
256 PVE
::Network
::tap_plug
($iface, $bridge, $tag, $firewall, $trunks, $rate);
260 my $plugin_config = get_plugin_config
($vnet);
261 my $nodename = PVE
::INotify
::nodename
();
263 die "vnet $bridge is not allowed on this node\n"
264 if $plugin_config->{nodes
} && !defined($plugin_config->{nodes
}->{$nodename});
266 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
267 $plugin->tap_plug($plugin_config, $vnet, $tag, $iface, $bridge, $firewall, $trunks, $rate);