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
::SimplePlugin
;
20 use PVE
::Network
::SDN
::Zones
::Plugin
;
22 PVE
::Network
::SDN
::Zones
::VlanPlugin-
>register();
23 PVE
::Network
::SDN
::Zones
::QinQPlugin-
>register();
24 PVE
::Network
::SDN
::Zones
::VxlanPlugin-
>register();
25 PVE
::Network
::SDN
::Zones
::EvpnPlugin-
>register();
26 PVE
::Network
::SDN
::Zones
::FaucetPlugin-
>register();
27 PVE
::Network
::SDN
::Zones
::SimplePlugin-
>register();
28 PVE
::Network
::SDN
::Zones
::Plugin-
>init();
30 my $local_network_sdn_file = "/etc/network/interfaces.d/sdn";
32 sub sdn_zones_config
{
33 my ($cfg, $id, $noerr) = @_;
35 die "no sdn zone ID specified\n" if !$id;
37 my $scfg = $cfg->{ids
}->{$id};
38 die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);
44 my $config = cfs_read_file
("sdn/zones.cfg");
48 sub get_plugin_config
{
50 my $zoneid = $vnet->{zone
};
51 my $zone_cfg = PVE
::Network
::SDN
::Zones
::config
();
52 return $zone_cfg->{ids
}->{$zoneid};
58 cfs_write_file
("sdn/zones.cfg", $cfg);
64 return sort keys %{$cfg->{ids
}};
67 sub complete_sdn_zone
{
68 my ($cmdname, $pname, $cvalue) = @_;
70 my $cfg = PVE
::Network
::SDN
::config
();
72 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::sdn_zones_ids
($cfg) ];
76 my ($zoneid, $running) = @_;
80 my $cfg = PVE
::Network
::SDN
::config
();
83 $cfg = PVE
::Network
::SDN
::Zones
::config
();
86 my $zone = PVE
::Network
::SDN
::Zones
::sdn_zones_config
($cfg, $zoneid, 1);
92 sub generate_etc_network_config
{
94 my $cfg = PVE
::Network
::SDN
::config
();
96 my $version = $cfg->{version
};
97 my $vnet_cfg = $cfg->{vnets
};
98 my $zone_cfg = $cfg->{zones
};
99 my $subnet_cfg = $cfg->{subnets
};
100 my $controller_cfg = $cfg->{controllers
};
101 return if !$vnet_cfg && !$zone_cfg;
103 my $interfaces_config = PVE
::INotify
::read_file
('interfaces');
105 #generate configuration
107 my $nodename = PVE
::INotify
::nodename
();
109 for my $id (sort keys %{$vnet_cfg->{ids
}}) {
110 my $vnet = $vnet_cfg->{ids
}->{$id};
111 my $zone = $vnet->{zone
};
114 warn "can't generate vnet '$id': no zone assigned!\n";
118 my $plugin_config = $zone_cfg->{ids
}->{$zone};
120 if (!defined($plugin_config)) {
121 warn "can't generate vnet '$id': zone $zone don't exist\n";
125 next if defined($plugin_config->{nodes
}) && !$plugin_config->{nodes
}->{$nodename};
128 if (my $controllerid = $plugin_config->{controller
}) {
129 $controller = $controller_cfg->{ids
}->{$controllerid};
132 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
134 $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $controller, $controller_cfg, $subnet_cfg, $interfaces_config, $config);
137 warn "zone $zone : vnet $id : $err\n";
142 my $raw_network_config = "\#version:$version\n";
143 foreach my $iface (sort keys %$config) {
144 $raw_network_config .= "\n";
145 $raw_network_config .= "auto $iface\n";
146 $raw_network_config .= "iface $iface\n";
147 foreach my $option (@{$config->{$iface}}) {
148 $raw_network_config .= "\t$option\n";
152 return $raw_network_config;
155 sub write_etc_network_config
{
156 my ($rawconfig) = @_;
158 return if !$rawconfig;
160 my $writefh = IO
::File-
>new($local_network_sdn_file,">");
161 print $writefh $rawconfig;
165 sub read_etc_network_config_version
{
166 my $versionstr = PVE
::Tools
::file_read_firstline
($local_network_sdn_file);
168 return if !defined($versionstr);
170 if ($versionstr =~ m/^\#version:(\d+)$/) {
177 my $cmd = ['ifquery', '-a', '-c', '-o','json'];
180 my $reader = sub { $result .= shift };
183 run_command
($cmd, outfunc
=> $reader);
186 my $resultjson = decode_json
($result);
189 foreach my $interface (@$resultjson) {
190 my $name = $interface->{name
};
191 $interfaces->{$name} = {
192 status
=> $interface->{status
},
193 config
=> $interface->{config
},
194 config_status
=> $interface->{config_status
},
201 my $warned_about_reload;
205 my $err_config = undef;
207 my $local_version = PVE
::Network
::SDN
::Zones
::read_etc_network_config_version
();
208 my $cfg = PVE
::Network
::SDN
::config
();
209 my $sdn_version = $cfg->{version
};
211 return if !$sdn_version;
213 if (!$local_version) {
214 $err_config = "local sdn network configuration is not yet generated, please reload";
215 if (!$warned_about_reload) {
216 $warned_about_reload = 1;
217 warn "$err_config\n";
219 } elsif ($local_version < $sdn_version) {
220 $err_config = "local sdn network configuration is too old, please reload";
221 if (!$warned_about_reload) {
222 $warned_about_reload = 1;
223 warn "$err_config\n";
226 $warned_about_reload = 0;
229 my $status = ifquery_check
();
232 my $vnet_cfg = $cfg->{vnets
};
233 my $zone_cfg = $cfg->{zones
};
234 my $nodename = PVE
::INotify
::nodename
();
236 my $vnet_status = {};
237 my $zone_status = {};
239 for my $id (sort keys %{$zone_cfg->{ids
}}) {
240 $zone_status->{$id}->{status
} = $err_config ?
'pending' : 'available';
243 foreach my $id (sort keys %{$vnet_cfg->{ids
}}) {
244 my $vnet = $vnet_cfg->{ids
}->{$id};
245 my $zone = $vnet->{zone
};
248 my $plugin_config = $zone_cfg->{ids
}->{$zone};
249 next if defined($plugin_config->{nodes
}) && !$plugin_config->{nodes
}->{$nodename};
251 $vnet_status->{$id}->{zone
} = $zone;
252 $vnet_status->{$id}->{status
} = 'available';
255 $vnet_status->{$id}->{status
} = 'pending';
256 $vnet_status->{$id}->{statusmsg
} = $err_config;
260 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
261 my $err_msg = $plugin->status($plugin_config, $zone, $id, $vnet, $status);
262 if (@{$err_msg} > 0) {
263 $vnet_status->{$id}->{status
} = 'error';
264 $vnet_status->{$id}->{statusmsg
} = join(',', @{$err_msg});
265 $zone_status->{$id}->{status
} = 'error';
269 return ($zone_status, $vnet_status);
273 my ($iface, $bridge) = @_;
275 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($bridge, 1);
276 if (!$vnet) { # fallback for classic bridge
277 PVE
::Network
::tap_create
($iface, $bridge);
281 my $plugin_config = get_plugin_config
($vnet);
282 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
283 $plugin->tap_create($plugin_config, $vnet, $iface, $bridge);
287 my ($veth, $vethpeer, $bridge, $hwaddr) = @_;
289 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($bridge, 1);
290 if (!$vnet) { # fallback for classic bridge
291 PVE
::Network
::veth_create
($veth, $vethpeer, $bridge, $hwaddr);
295 my $plugin_config = get_plugin_config
($vnet);
296 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
297 $plugin->veth_create($plugin_config, $vnet, $veth, $vethpeer, $bridge, $hwaddr);
301 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
303 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($bridge, 1);
304 if (!$vnet) { # fallback for classic bridge
305 PVE
::Network
::tap_plug
($iface, $bridge, $tag, $firewall, $trunks, $rate);
309 my $plugin_config = get_plugin_config
($vnet);
310 my $nodename = PVE
::INotify
::nodename
();
312 die "vnet $bridge is not allowed on this node\n"
313 if $plugin_config->{nodes
} && !defined($plugin_config->{nodes
}->{$nodename});
315 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
316 $plugin->tap_plug($plugin_config, $vnet, $tag, $iface, $bridge, $firewall, $trunks, $rate);