]>
Commit | Line | Data |
---|---|---|
f5eabba0 AD |
1 | package PVE::Network::SDN::Controllers; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use Data::Dumper; | |
7 | use JSON; | |
8 | ||
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); | |
11 | ||
12 | use PVE::Network::SDN::Vnets; | |
13 | use PVE::Network::SDN::Zones; | |
14 | ||
fa253735 | 15 | use PVE::Network::SDN::Controllers::EvpnPlugin; |
f5eabba0 AD |
16 | use PVE::Network::SDN::Controllers::FaucetPlugin; |
17 | use PVE::Network::SDN::Controllers::Plugin; | |
fa253735 | 18 | PVE::Network::SDN::Controllers::EvpnPlugin->register(); |
f5eabba0 AD |
19 | PVE::Network::SDN::Controllers::FaucetPlugin->register(); |
20 | PVE::Network::SDN::Controllers::Plugin->init(); | |
21 | ||
22 | ||
23 | sub sdn_controllers_config { | |
24 | my ($cfg, $id, $noerr) = @_; | |
25 | ||
26 | die "no sdn controller ID specified\n" if !$id; | |
27 | ||
28 | my $scfg = $cfg->{ids}->{$id}; | |
29 | die "sdn '$id' does not exists\n" if (!$noerr && !$scfg); | |
30 | ||
31 | return $scfg; | |
32 | } | |
33 | ||
34 | sub config { | |
35 | my $config = cfs_read_file("sdn/controllers.cfg.new"); | |
36 | $config = cfs_read_file("sdn/controllers.cfg") if !keys %{$config->{ids}}; | |
37 | return $config; | |
38 | } | |
39 | ||
40 | sub write_config { | |
41 | my ($cfg) = @_; | |
42 | ||
43 | cfs_write_file("sdn/controllers.cfg.new", $cfg); | |
44 | } | |
45 | ||
46 | sub lock_sdn_controllers_config { | |
47 | my ($code, $errmsg) = @_; | |
48 | ||
49 | cfs_lock_file("sdn/controllers.cfg.new", undef, $code); | |
50 | if (my $err = $@) { | |
51 | $errmsg ? die "$errmsg: $err" : die $err; | |
52 | } | |
53 | } | |
54 | ||
55 | sub sdn_controllers_ids { | |
56 | my ($cfg) = @_; | |
57 | ||
58 | return keys %{$cfg->{ids}}; | |
59 | } | |
60 | ||
61 | sub complete_sdn_controller { | |
62 | my ($cmdname, $pname, $cvalue) = @_; | |
63 | ||
64 | my $cfg = PVE::Network::SDN::config(); | |
65 | ||
66 | return $cmdname eq 'add' ? [] : [ PVE::Network::SDN::sdn_controllers_ids($cfg) ]; | |
67 | } | |
68 | ||
69 | sub generate_controller_config { | |
70 | ||
71 | my $vnet_cfg = PVE::Cluster::cfs_read_file('sdn/vnets.cfg'); | |
72 | my $transport_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 && !$transport_cfg && !$controller_cfg; | |
75 | ||
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); | |
80 | $fh->close(); | |
81 | ||
82 | #check uplinks | |
83 | my $uplinks = {}; | |
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; | |
90 | } | |
91 | } | |
92 | ||
93 | #generate configuration | |
94 | my $config = {}; | |
95 | ||
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); | |
100 | } | |
101 | ||
102 | foreach my $id (keys %{$transport_cfg->{ids}}) { | |
103 | my $plugin_config = $transport_cfg->{ids}->{$id}; | |
104 | my $controllerid = $plugin_config->{controller}; | |
105 | next if !$controllerid; | |
106 | my $controller = $transport_cfg->{ids}->{$controllerid}; | |
107 | if ($controller) { | |
108 | my $controller_plugin = PVE::Network::SDN::Controllers::Plugin->lookup($controller->{type}); | |
109 | $controller_plugin->generate_controller_transport_config($plugin_config, $controller, $id, $uplinks, $config); | |
110 | } | |
111 | } | |
112 | ||
113 | foreach my $id (keys %{$vnet_cfg->{ids}}) { | |
114 | my $plugin_config = $vnet_cfg->{ids}->{$id}; | |
3fd3e917 | 115 | my $transportid = $plugin_config->{zone}; |
f5eabba0 AD |
116 | next if !$transportid; |
117 | my $transport = $transport_cfg->{ids}->{$transportid}; | |
118 | next if !$transport; | |
119 | my $controllerid = $transport->{controller}; | |
120 | next if !$controllerid; | |
121 | my $controller = $controller_cfg->{ids}->{$controllerid}; | |
122 | if ($controller) { | |
123 | my $controller_plugin = PVE::Network::SDN::Controllers::Plugin->lookup($controller->{type}); | |
124 | $controller_plugin->generate_controller_vnet_config($plugin_config, $controller, $transportid, $id, $config); | |
125 | } | |
126 | } | |
127 | ||
128 | return $config; | |
129 | } | |
130 | ||
131 | ||
132 | sub reload_controller { | |
133 | ||
134 | my $controller_cfg = PVE::Cluster::cfs_read_file('sdn/controllers.cfg'); | |
135 | return if !$controller_cfg; | |
136 | ||
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(); | |
141 | } | |
142 | } | |
143 | ||
144 | sub write_controller_config { | |
145 | my ($config) = @_; | |
146 | ||
147 | my $controller_cfg = PVE::Cluster::cfs_read_file('sdn/controllers.cfg'); | |
148 | return if !$controller_cfg; | |
149 | ||
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); | |
154 | } | |
155 | } | |
156 | ||
157 | 1; | |
158 |