summaryrefslogtreecommitdiff
path: root/src/PVE/Network/SDN/Controllers.pm
blob: 167d3eac6e207d9550abb041390e65d68686a573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package PVE::Network::SDN::Controllers;

use strict;
use warnings;

use Data::Dumper;
use JSON;

use PVE::Tools qw(extract_param dir_glob_regex run_command);
use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);

use PVE::Network::SDN::Vnets;
use PVE::Network::SDN::Zones;

use PVE::Network::SDN::Controllers::EvpnPlugin;
use PVE::Network::SDN::Controllers::BgpPlugin;
use PVE::Network::SDN::Controllers::IsisPlugin;
use PVE::Network::SDN::Controllers::FaucetPlugin;
use PVE::Network::SDN::Controllers::Plugin;
PVE::Network::SDN::Controllers::EvpnPlugin->register();
PVE::Network::SDN::Controllers::BgpPlugin->register();
PVE::Network::SDN::Controllers::IsisPlugin->register();
PVE::Network::SDN::Controllers::FaucetPlugin->register();
PVE::Network::SDN::Controllers::Plugin->init();


sub sdn_controllers_config {
    my ($cfg, $id, $noerr) = @_;

    die "no sdn controller ID specified\n" if !$id;

    my $scfg = $cfg->{ids}->{$id};
    die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);

    return $scfg;
}

sub config {
    my $config = cfs_read_file("sdn/controllers.cfg");
    $config = cfs_read_file("sdn/controllers.cfg") if !keys %{$config->{ids}};
    return $config;
}

sub write_config {
    my ($cfg) = @_;

    cfs_write_file("sdn/controllers.cfg", $cfg);
}

sub lock_sdn_controllers_config {
    my ($code, $errmsg) = @_;

    cfs_lock_file("sdn/controllers.cfg", undef, $code);
    if (my $err = $@) {
        $errmsg ? die "$errmsg: $err" : die $err;
    }
}

sub sdn_controllers_ids {
    my ($cfg) = @_;

    return sort keys %{$cfg->{ids}};
}

sub complete_sdn_controller {
    my ($cmdname, $pname, $cvalue) = @_;

    my $cfg = PVE::Network::SDN::running_config();

    return  $cmdname eq 'add' ? [] : [ PVE::Network::SDN::sdn_controllers_ids($cfg) ];
}

sub generate_controller_config {

    my $cfg = PVE::Network::SDN::running_config();
    my $vnet_cfg = $cfg->{vnets};
    my $zone_cfg = $cfg->{zones};
    my $controller_cfg = $cfg->{controllers};

    return if !$vnet_cfg && !$zone_cfg && !$controller_cfg;

    # read main config for physical interfaces
    my $current_config_file = "/etc/network/interfaces";
    my $fh = IO::File->new($current_config_file) or die "failed to open $current_config_file - $!\n";
    my $interfaces_config = PVE::INotify::read_etc_network_interfaces($current_config_file, $fh);
    $fh->close();

    # check uplinks
    my $uplinks = {};
    foreach my $id (keys %{$interfaces_config->{ifaces}}) {
	my $interface = $interfaces_config->{ifaces}->{$id};
	if (my $uplink = $interface->{'uplink-id'}) {
	    die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
	    $interface->{name} = $id;
	    $uplinks->{$interface->{'uplink-id'}} = $interface;
	}
    }

    # generate configuration
    my $config = {};

    foreach my $id (sort keys %{$controller_cfg->{ids}}) {
	my $plugin_config = $controller_cfg->{ids}->{$id};
	my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type});
	$plugin->generate_controller_config($plugin_config, $controller_cfg, $id, $uplinks, $config);
    }

    foreach my $id (sort keys %{$zone_cfg->{ids}}) {
	my $plugin_config = $zone_cfg->{ids}->{$id};
	my $controllerid = $plugin_config->{controller};
	next if !$controllerid;
	my $controller = $controller_cfg->{ids}->{$controllerid};
	if ($controller) {
	    my $controller_plugin = PVE::Network::SDN::Controllers::Plugin->lookup($controller->{type});
	    $controller_plugin->generate_controller_zone_config($plugin_config, $controller, $controller_cfg, $id, $uplinks, $config);
	}
    }

    foreach my $id (sort keys %{$vnet_cfg->{ids}}) {
	my $plugin_config = $vnet_cfg->{ids}->{$id};
	my $zoneid = $plugin_config->{zone};
	next if !$zoneid;
	my $zone = $zone_cfg->{ids}->{$zoneid};
	next if !$zone;
	my $controllerid = $zone->{controller};
	next if !$controllerid;
	my $controller = $controller_cfg->{ids}->{$controllerid};
	if ($controller) {
	    my $controller_plugin = PVE::Network::SDN::Controllers::Plugin->lookup($controller->{type});
	    $controller_plugin->generate_controller_vnet_config($plugin_config, $controller, $zone, $zoneid, $id, $config);
	}
    }

    return $config;
}


sub reload_controller {

    my $cfg = PVE::Network::SDN::running_config();
    my $controller_cfg = $cfg->{controllers};

    return if !$controller_cfg;

    foreach my $id (keys %{$controller_cfg->{ids}}) {
	my $plugin_config = $controller_cfg->{ids}->{$id};
	my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type});
	$plugin->reload_controller();
    }
}

sub generate_controller_rawconfig {
    my ($config) = @_;

    my $cfg = PVE::Network::SDN::running_config();
    my $controller_cfg = $cfg->{controllers};
    return if !$controller_cfg;

    my $rawconfig = "";
    foreach my $id (keys %{$controller_cfg->{ids}}) {
	my $plugin_config = $controller_cfg->{ids}->{$id};
	my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type});
	$rawconfig .= $plugin->generate_controller_rawconfig($plugin_config, $config);
    }
    return $rawconfig;
}

sub write_controller_config {
    my ($config) = @_;

    my $cfg = PVE::Network::SDN::running_config();
    my $controller_cfg = $cfg->{controllers};
    return if !$controller_cfg;

    foreach my $id (keys %{$controller_cfg->{ids}}) {
	my $plugin_config = $controller_cfg->{ids}->{$id};
	my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type});
	$plugin->write_controller_config($plugin_config, $config);
    }
}

1;