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
::BgpPlugin
;
17 use PVE
::Network
::SDN
::Controllers
::FaucetPlugin
;
18 use PVE
::Network
::SDN
::Controllers
::Plugin
;
19 PVE
::Network
::SDN
::Controllers
::EvpnPlugin-
>register();
20 PVE
::Network
::SDN
::Controllers
::BgpPlugin-
>register();
21 PVE
::Network
::SDN
::Controllers
::FaucetPlugin-
>register();
22 PVE
::Network
::SDN
::Controllers
::Plugin-
>init();
25 sub sdn_controllers_config
{
26 my ($cfg, $id, $noerr) = @_;
28 die "no sdn controller ID specified\n" if !$id;
30 my $scfg = $cfg->{ids
}->{$id};
31 die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);
37 my $config = cfs_read_file
("sdn/controllers.cfg");
38 $config = cfs_read_file
("sdn/controllers.cfg") if !keys %{$config->{ids
}};
45 cfs_write_file
("sdn/controllers.cfg", $cfg);
48 sub lock_sdn_controllers_config
{
49 my ($code, $errmsg) = @_;
51 cfs_lock_file
("sdn/controllers.cfg", undef, $code);
53 $errmsg ?
die "$errmsg: $err" : die $err;
57 sub sdn_controllers_ids
{
60 return sort keys %{$cfg->{ids
}};
63 sub complete_sdn_controller
{
64 my ($cmdname, $pname, $cvalue) = @_;
66 my $cfg = PVE
::Network
::SDN
::running_config
();
68 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::sdn_controllers_ids
($cfg) ];
71 sub generate_controller_config
{
73 my $cfg = PVE
::Network
::SDN
::running_config
();
74 my $vnet_cfg = $cfg->{vnets
};
75 my $zone_cfg = $cfg->{zones
};
76 my $controller_cfg = $cfg->{controllers
};
78 return if !$vnet_cfg && !$zone_cfg && !$controller_cfg;
80 #read main config for physical interfaces
81 my $current_config_file = "/etc/network/interfaces";
82 my $fh = IO
::File-
>new($current_config_file);
83 my $interfaces_config = PVE
::INotify
::read_etc_network_interfaces
(1,$fh);
88 foreach my $id (keys %{$interfaces_config->{ifaces
}}) {
89 my $interface = $interfaces_config->{ifaces
}->{$id};
90 if (my $uplink = $interface->{'uplink-id'}) {
91 die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
92 $interface->{name
} = $id;
93 $uplinks->{$interface->{'uplink-id'}} = $interface;
97 # generate configuration
100 foreach my $id (sort keys %{$controller_cfg->{ids
}}) {
101 my $plugin_config = $controller_cfg->{ids
}->{$id};
102 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
103 $plugin->generate_controller_config($plugin_config, $controller_cfg, $id, $uplinks, $config);
106 foreach my $id (sort keys %{$zone_cfg->{ids
}}) {
107 my $plugin_config = $zone_cfg->{ids
}->{$id};
108 my $controllerid = $plugin_config->{controller
};
109 next if !$controllerid;
110 my $controller = $controller_cfg->{ids
}->{$controllerid};
112 my $controller_plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($controller->{type
});
113 $controller_plugin->generate_controller_zone_config($plugin_config, $controller, $controller_cfg, $id, $uplinks, $config);
117 foreach my $id (sort keys %{$vnet_cfg->{ids
}}) {
118 my $plugin_config = $vnet_cfg->{ids
}->{$id};
119 my $zoneid = $plugin_config->{zone
};
121 my $zone = $zone_cfg->{ids
}->{$zoneid};
123 my $controllerid = $zone->{controller
};
124 next if !$controllerid;
125 my $controller = $controller_cfg->{ids
}->{$controllerid};
127 my $controller_plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($controller->{type
});
128 $controller_plugin->generate_controller_vnet_config($plugin_config, $controller, $zone, $zoneid, $id, $config);
136 sub reload_controller
{
138 my $cfg = PVE
::Network
::SDN
::running_config
();
139 my $controller_cfg = $cfg->{controllers
};
141 return if !$controller_cfg;
143 foreach my $id (keys %{$controller_cfg->{ids
}}) {
144 my $plugin_config = $controller_cfg->{ids
}->{$id};
145 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
146 $plugin->reload_controller();
150 sub generate_controller_rawconfig
{
153 my $cfg = PVE
::Network
::SDN
::running_config
();
154 my $controller_cfg = $cfg->{controllers
};
155 return if !$controller_cfg;
158 foreach my $id (keys %{$controller_cfg->{ids
}}) {
159 my $plugin_config = $controller_cfg->{ids
}->{$id};
160 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
161 $rawconfig .= $plugin->generate_controller_rawconfig($plugin_config, $config);
166 sub write_controller_config
{
169 my $cfg = PVE
::Network
::SDN
::running_config
();
170 my $controller_cfg = $cfg->{controllers
};
171 return if !$controller_cfg;
173 foreach my $id (keys %{$controller_cfg->{ids
}}) {
174 my $plugin_config = $controller_cfg->{ids
}->{$id};
175 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
176 $plugin->write_controller_config($plugin_config, $config);