1 package PVE
::Network
::SDN
::Controllers
;
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
;
15 use PVE
::Network
::SDN
::Controllers
::EvpnPlugin
;
16 use PVE
::Network
::SDN
::Controllers
::FaucetPlugin
;
17 use PVE
::Network
::SDN
::Controllers
::Plugin
;
18 PVE
::Network
::SDN
::Controllers
::EvpnPlugin-
>register();
19 PVE
::Network
::SDN
::Controllers
::FaucetPlugin-
>register();
20 PVE
::Network
::SDN
::Controllers
::Plugin-
>init();
23 sub sdn_controllers_config
{
24 my ($cfg, $id, $noerr) = @_;
26 die "no sdn controller ID specified\n" if !$id;
28 my $scfg = $cfg->{ids
}->{$id};
29 die "sdn '$id' does not exists\n" if (!$noerr && !$scfg);
35 my $config = cfs_read_file
("sdn/controllers.cfg.new");
36 $config = cfs_read_file
("sdn/controllers.cfg") if !keys %{$config->{ids
}};
43 cfs_write_file
("sdn/controllers.cfg.new", $cfg);
46 sub lock_sdn_controllers_config
{
47 my ($code, $errmsg) = @_;
49 cfs_lock_file
("sdn/controllers.cfg.new", undef, $code);
51 $errmsg ?
die "$errmsg: $err" : die $err;
55 sub sdn_controllers_ids
{
58 return keys %{$cfg->{ids
}};
61 sub complete_sdn_controller
{
62 my ($cmdname, $pname, $cvalue) = @_;
64 my $cfg = PVE
::Network
::SDN
::config
();
66 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::sdn_controllers_ids
($cfg) ];
69 sub generate_controller_config
{
71 my $vnet_cfg = PVE
::Cluster
::cfs_read_file
('sdn/vnets.cfg');
72 my $zone_cfg = PVE
::Cluster
::cfs_read_file
('sdn/zones.cfg');
73 my $controller_cfg = PVE
::Cluster
::cfs_read_file
('sdn/controllers.cfg');
74 return if !$vnet_cfg && !$zone_cfg && !$controller_cfg;
76 #read main config for physical interfaces
77 my $current_config_file = "/etc/network/interfaces";
78 my $fh = IO
::File-
>new($current_config_file);
79 my $interfaces_config = PVE
::INotify
::read_etc_network_interfaces
(1,$fh);
84 foreach my $id (keys %{$interfaces_config->{ifaces
}}) {
85 my $interface = $interfaces_config->{ifaces
}->{$id};
86 if (my $uplink = $interface->{'uplink-id'}) {
87 die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
88 $interface->{name
} = $id;
89 $uplinks->{$interface->{'uplink-id'}} = $interface;
93 #generate configuration
96 foreach my $id (keys %{$controller_cfg->{ids
}}) {
97 my $plugin_config = $controller_cfg->{ids
}->{$id};
98 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
99 $plugin->generate_controller_config($plugin_config, $plugin_config, $id, $uplinks, $config);
102 foreach my $id (keys %{$zone_cfg->{ids
}}) {
103 my $plugin_config = $zone_cfg->{ids
}->{$id};
104 my $controllerid = $plugin_config->{controller
};
105 next if !$controllerid;
106 my $controller = $controller_cfg->{ids
}->{$controllerid};
108 my $controller_plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($controller->{type
});
109 $controller_plugin->generate_controller_zone_config($plugin_config, $controller, $id, $uplinks, $config);
113 foreach my $id (keys %{$vnet_cfg->{ids
}}) {
114 my $plugin_config = $vnet_cfg->{ids
}->{$id};
115 my $zoneid = $plugin_config->{zone
};
117 my $zone = $zone_cfg->{ids
}->{$zoneid};
119 my $controllerid = $zone->{controller
};
120 next if !$controllerid;
121 my $controller = $controller_cfg->{ids
}->{$controllerid};
123 my $controller_plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($controller->{type
});
124 $controller_plugin->generate_controller_vnet_config($plugin_config, $controller, $zoneid, $id, $config);
132 sub reload_controller
{
134 my $controller_cfg = PVE
::Cluster
::cfs_read_file
('sdn/controllers.cfg');
135 return if !$controller_cfg;
137 foreach my $id (keys %{$controller_cfg->{ids
}}) {
138 my $plugin_config = $controller_cfg->{ids
}->{$id};
139 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
140 $plugin->reload_controller();
144 sub write_controller_config
{
147 my $controller_cfg = PVE
::Cluster
::cfs_read_file
('sdn/controllers.cfg');
148 return if !$controller_cfg;
150 foreach my $id (keys %{$controller_cfg->{ids
}}) {
151 my $plugin_config = $controller_cfg->{ids
}->{$id};
152 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
153 $plugin->write_controller_config($plugin_config, $config);