]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Controllers.pm
api: generate 'running-config' state instead of version increase on apply
[pve-network.git] / PVE / Network / SDN / Controllers.pm
CommitLineData
f5eabba0
AD
1package PVE::Network::SDN::Controllers;
2
3use strict;
4use warnings;
5
6use Data::Dumper;
7use JSON;
8
9use PVE::Tools qw(extract_param dir_glob_regex run_command);
10use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
11
12use PVE::Network::SDN::Vnets;
13use PVE::Network::SDN::Zones;
14
fa253735 15use PVE::Network::SDN::Controllers::EvpnPlugin;
f5eabba0
AD
16use PVE::Network::SDN::Controllers::FaucetPlugin;
17use PVE::Network::SDN::Controllers::Plugin;
fa253735 18PVE::Network::SDN::Controllers::EvpnPlugin->register();
f5eabba0
AD
19PVE::Network::SDN::Controllers::FaucetPlugin->register();
20PVE::Network::SDN::Controllers::Plugin->init();
21
22
23sub 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};
b2d83056 29 die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);
f5eabba0
AD
30
31 return $scfg;
32}
33
34sub config {
f703d2ae 35 my $config = cfs_read_file("sdn/controllers.cfg");
f5eabba0
AD
36 $config = cfs_read_file("sdn/controllers.cfg") if !keys %{$config->{ids}};
37 return $config;
38}
39
40sub write_config {
41 my ($cfg) = @_;
42
f703d2ae 43 cfs_write_file("sdn/controllers.cfg", $cfg);
f5eabba0
AD
44}
45
46sub lock_sdn_controllers_config {
47 my ($code, $errmsg) = @_;
48
f703d2ae 49 cfs_lock_file("sdn/controllers.cfg", undef, $code);
f5eabba0
AD
50 if (my $err = $@) {
51 $errmsg ? die "$errmsg: $err" : die $err;
52 }
53}
54
55sub sdn_controllers_ids {
56 my ($cfg) = @_;
57
58 return keys %{$cfg->{ids}};
59}
60
61sub 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
69sub generate_controller_config {
70
5d3e0248
AD
71 my $cfg = PVE::Network::SDN::config();
72 my $vnet_cfg = $cfg->{vnets};
73 my $zone_cfg = $cfg->{zones};
74 my $controller_cfg = $cfg->{controllers};
75
56cdcac9 76 return if !$vnet_cfg && !$zone_cfg && !$controller_cfg;
f5eabba0
AD
77
78 #read main config for physical interfaces
79 my $current_config_file = "/etc/network/interfaces";
80 my $fh = IO::File->new($current_config_file);
81 my $interfaces_config = PVE::INotify::read_etc_network_interfaces(1,$fh);
82 $fh->close();
83
84 #check uplinks
85 my $uplinks = {};
86 foreach my $id (keys %{$interfaces_config->{ifaces}}) {
87 my $interface = $interfaces_config->{ifaces}->{$id};
88 if (my $uplink = $interface->{'uplink-id'}) {
89 die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
90 $interface->{name} = $id;
91 $uplinks->{$interface->{'uplink-id'}} = $interface;
92 }
93 }
94
95 #generate configuration
96 my $config = {};
97
98 foreach my $id (keys %{$controller_cfg->{ids}}) {
99 my $plugin_config = $controller_cfg->{ids}->{$id};
100 my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type});
101 $plugin->generate_controller_config($plugin_config, $plugin_config, $id, $uplinks, $config);
102 }
103
56cdcac9
AD
104 foreach my $id (keys %{$zone_cfg->{ids}}) {
105 my $plugin_config = $zone_cfg->{ids}->{$id};
f5eabba0
AD
106 my $controllerid = $plugin_config->{controller};
107 next if !$controllerid;
7cb9714d 108 my $controller = $controller_cfg->{ids}->{$controllerid};
f5eabba0
AD
109 if ($controller) {
110 my $controller_plugin = PVE::Network::SDN::Controllers::Plugin->lookup($controller->{type});
56cdcac9 111 $controller_plugin->generate_controller_zone_config($plugin_config, $controller, $id, $uplinks, $config);
f5eabba0
AD
112 }
113 }
114
115 foreach my $id (keys %{$vnet_cfg->{ids}}) {
116 my $plugin_config = $vnet_cfg->{ids}->{$id};
56cdcac9
AD
117 my $zoneid = $plugin_config->{zone};
118 next if !$zoneid;
119 my $zone = $zone_cfg->{ids}->{$zoneid};
120 next if !$zone;
121 my $controllerid = $zone->{controller};
f5eabba0
AD
122 next if !$controllerid;
123 my $controller = $controller_cfg->{ids}->{$controllerid};
124 if ($controller) {
125 my $controller_plugin = PVE::Network::SDN::Controllers::Plugin->lookup($controller->{type});
56cdcac9 126 $controller_plugin->generate_controller_vnet_config($plugin_config, $controller, $zoneid, $id, $config);
f5eabba0
AD
127 }
128 }
129
130 return $config;
131}
132
133
134sub reload_controller {
135
5d3e0248
AD
136 my $cfg = PVE::Network::SDN::config();
137 my $controller_cfg = $cfg->{controllers};
138
f5eabba0
AD
139 return if !$controller_cfg;
140
141 foreach my $id (keys %{$controller_cfg->{ids}}) {
142 my $plugin_config = $controller_cfg->{ids}->{$id};
143 my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type});
144 $plugin->reload_controller();
145 }
146}
147
148sub write_controller_config {
149 my ($config) = @_;
150
151 my $controller_cfg = PVE::Cluster::cfs_read_file('sdn/controllers.cfg');
152 return if !$controller_cfg;
153
154 foreach my $id (keys %{$controller_cfg->{ids}}) {
155 my $plugin_config = $controller_cfg->{ids}->{$id};
156 my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type});
157 $plugin->write_controller_config($plugin_config, $config);
158 }
159}
160
1611;
162