1 package PVE
::Network
::SDN
::Zones
;
8 use PVE
::Tools
qw(extract_param dir_glob_regex run_command);
9 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
::SimplePlugin
;
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
::SimplePlugin-
>register();
27 PVE
::Network
::SDN
::Zones
::Plugin-
>init();
29 my $local_network_sdn_file = "/etc/network/interfaces.d/sdn";
31 sub sdn_zones_config
{
32 my ($cfg, $id, $noerr) = @_;
34 die "no sdn zone ID specified\n" if !$id;
36 my $scfg = $cfg->{ids
}->{$id};
37 die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);
43 my $config = cfs_read_file
("sdn/zones.cfg");
47 sub get_plugin_config
{
49 my $zoneid = $vnet->{zone
};
50 my $zone_cfg = PVE
::Network
::SDN
::Zones
::config
();
51 return $zone_cfg->{ids
}->{$zoneid};
57 cfs_write_file
("sdn/zones.cfg", $cfg);
63 return sort keys %{$cfg->{ids
}};
66 sub complete_sdn_zone
{
67 my ($cmdname, $pname, $cvalue) = @_;
69 my $cfg = PVE
::Network
::SDN
::running_config
();
71 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::sdn_zones_ids
($cfg) ];
75 my ($zoneid, $running) = @_;
79 my $cfg = PVE
::Network
::SDN
::running_config
();
82 $cfg = PVE
::Network
::SDN
::Zones
::config
();
85 my $zone = PVE
::Network
::SDN
::Zones
::sdn_zones_config
($cfg, $zoneid, 1);
91 sub generate_etc_network_config
{
93 my $cfg = PVE
::Network
::SDN
::running_config
();
95 my $version = $cfg->{version
};
96 my $vnet_cfg = $cfg->{vnets
};
97 my $zone_cfg = $cfg->{zones
};
98 my $subnet_cfg = $cfg->{subnets
};
99 my $controller_cfg = $cfg->{controllers
};
100 return if !$vnet_cfg && !$zone_cfg;
102 my $interfaces_config = PVE
::INotify
::read_file
('interfaces');
104 #generate configuration
106 my $nodename = PVE
::INotify
::nodename
();
108 for my $id (sort keys %{$vnet_cfg->{ids
}}) {
109 my $vnet = $vnet_cfg->{ids
}->{$id};
110 my $zone = $vnet->{zone
};
113 warn "can't generate vnet '$id': no zone assigned!\n";
117 my $plugin_config = $zone_cfg->{ids
}->{$zone};
119 if (!defined($plugin_config)) {
120 warn "can't generate vnet '$id': zone $zone don't exist\n";
124 next if defined($plugin_config->{nodes
}) && !$plugin_config->{nodes
}->{$nodename};
127 if (my $controllerid = $plugin_config->{controller
}) {
128 $controller = $controller_cfg->{ids
}->{$controllerid};
131 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
133 $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $controller, $controller_cfg, $subnet_cfg, $interfaces_config, $config);
136 warn "zone $zone : vnet $id : $err\n";
141 my $raw_network_config = "\#version:$version\n";
142 foreach my $iface (sort keys %$config) {
143 $raw_network_config .= "\n";
144 $raw_network_config .= "auto $iface\n";
145 $raw_network_config .= "iface $iface\n";
146 foreach my $option (@{$config->{$iface}}) {
147 $raw_network_config .= "\t$option\n";
151 return $raw_network_config;
154 sub write_etc_network_config
{
155 my ($rawconfig) = @_;
157 return if !$rawconfig;
159 my $writefh = IO
::File-
>new($local_network_sdn_file,">");
160 print $writefh $rawconfig;
164 sub read_etc_network_config_version
{
165 my $versionstr = PVE
::Tools
::file_read_firstline
($local_network_sdn_file);
167 return if !defined($versionstr);
169 if ($versionstr =~ m/^\#version:(\d+)$/) {
176 my $cmd = ['ifquery', '-a', '-c', '-o','json'];
179 my $reader = sub { $result .= shift };
182 run_command
($cmd, outfunc
=> $reader);
185 my $resultjson = decode_json
($result);
188 foreach my $interface (@$resultjson) {
189 my $name = $interface->{name
};
190 $interfaces->{$name} = {
191 status
=> $interface->{status
},
192 config
=> $interface->{config
},
193 config_status
=> $interface->{config_status
},
200 my $warned_about_reload;
204 my $err_config = undef;
206 my $local_version = PVE
::Network
::SDN
::Zones
::read_etc_network_config_version
();
207 my $cfg = PVE
::Network
::SDN
::running_config
();
208 my $sdn_version = $cfg->{version
};
210 return if !$sdn_version;
212 if (!$local_version) {
213 $err_config = "local sdn network configuration is not yet generated, please reload";
214 if (!$warned_about_reload) {
215 $warned_about_reload = 1;
216 warn "$err_config\n";
218 } elsif ($local_version < $sdn_version) {
219 $err_config = "local sdn network configuration is too old, please reload";
220 if (!$warned_about_reload) {
221 $warned_about_reload = 1;
222 warn "$err_config\n";
225 $warned_about_reload = 0;
228 my $status = ifquery_check
();
231 my $vnet_cfg = $cfg->{vnets
};
232 my $zone_cfg = $cfg->{zones
};
233 my $nodename = PVE
::INotify
::nodename
();
235 my $vnet_status = {};
236 my $zone_status = {};
238 for my $id (sort keys %{$zone_cfg->{ids
}}) {
239 $zone_status->{$id}->{status
} = $err_config ?
'pending' : 'available';
242 foreach my $id (sort keys %{$vnet_cfg->{ids
}}) {
243 my $vnet = $vnet_cfg->{ids
}->{$id};
244 my $zone = $vnet->{zone
};
247 my $plugin_config = $zone_cfg->{ids
}->{$zone};
248 next if defined($plugin_config->{nodes
}) && !$plugin_config->{nodes
}->{$nodename};
250 $vnet_status->{$id}->{zone
} = $zone;
251 $vnet_status->{$id}->{status
} = 'available';
254 $vnet_status->{$id}->{status
} = 'pending';
255 $vnet_status->{$id}->{statusmsg
} = $err_config;
259 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
260 my $err_msg = $plugin->status($plugin_config, $zone, $id, $vnet, $status);
261 if (@{$err_msg} > 0) {
262 $vnet_status->{$id}->{status
} = 'error';
263 $vnet_status->{$id}->{statusmsg
} = join(',', @{$err_msg});
264 $zone_status->{$id}->{status
} = 'error';
268 return ($zone_status, $vnet_status);
272 my ($iface, $bridge) = @_;
274 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($bridge, 1);
275 if (!$vnet) { # fallback for classic bridge
276 PVE
::Network
::tap_create
($iface, $bridge);
280 my $plugin_config = get_plugin_config
($vnet);
281 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
282 $plugin->tap_create($plugin_config, $vnet, $iface, $bridge);
286 my ($veth, $vethpeer, $bridge, $hwaddr) = @_;
288 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($bridge, 1);
289 if (!$vnet) { # fallback for classic bridge
290 PVE
::Network
::veth_create
($veth, $vethpeer, $bridge, $hwaddr);
294 my $plugin_config = get_plugin_config
($vnet);
295 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
296 $plugin->veth_create($plugin_config, $vnet, $veth, $vethpeer, $bridge, $hwaddr);
300 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
302 my $vnet = PVE
::Network
::SDN
::Vnets
::get_vnet
($bridge, 1);
303 if (!$vnet) { # fallback for classic bridge
304 PVE
::Network
::tap_plug
($iface, $bridge, $tag, $firewall, $trunks, $rate);
308 my $plugin_config = get_plugin_config
($vnet);
309 my $nodename = PVE
::INotify
::nodename
();
311 die "vnet $bridge is not allowed on this node\n"
312 if $plugin_config->{nodes
} && !defined($plugin_config->{nodes
}->{$nodename});
314 my $plugin = PVE
::Network
::SDN
::Zones
::Plugin-
>lookup($plugin_config->{type
});
315 $plugin->tap_plug($plugin_config, $vnet, $tag, $iface, $bridge, $firewall, $trunks, $rate);