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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
package PVE::Network::SDN::Zones;
use strict;
use warnings;
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;
use PVE::Network::SDN::Vnets;
use PVE::Network::SDN::Zones::VlanPlugin;
use PVE::Network::SDN::Zones::QinQPlugin;
use PVE::Network::SDN::Zones::VxlanPlugin;
use PVE::Network::SDN::Zones::EvpnPlugin;
use PVE::Network::SDN::Zones::FaucetPlugin;
use PVE::Network::SDN::Zones::SimplePlugin;
use PVE::Network::SDN::Zones::Plugin;
PVE::Network::SDN::Zones::VlanPlugin->register();
PVE::Network::SDN::Zones::QinQPlugin->register();
PVE::Network::SDN::Zones::VxlanPlugin->register();
PVE::Network::SDN::Zones::EvpnPlugin->register();
PVE::Network::SDN::Zones::FaucetPlugin->register();
PVE::Network::SDN::Zones::SimplePlugin->register();
PVE::Network::SDN::Zones::Plugin->init();
my $local_network_sdn_file = "/etc/network/interfaces.d/sdn";
my $default_mtu = 1500;
sub sdn_zones_config {
my ($cfg, $id, $noerr) = @_;
die "no sdn zone 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 ($running) = @_;
if ($running) {
my $cfg = PVE::Network::SDN::running_config();
return $cfg->{zones};
}
return cfs_read_file("sdn/zones.cfg");
}
sub get_plugin_config {
my ($vnet) = @_;
my $zoneid = $vnet->{zone};
my $zone_cfg = PVE::Network::SDN::Zones::config();
return $zone_cfg->{ids}->{$zoneid};
}
sub write_config {
my ($cfg) = @_;
cfs_write_file("sdn/zones.cfg", $cfg);
}
sub sdn_zones_ids {
my ($cfg) = @_;
my @sorted_ids = sort keys $cfg->{ids}->%*;
return @sorted_ids;
}
sub complete_sdn_zone {
my ($cmdname, $pname, $cvalue) = @_;
my $cfg = PVE::Network::SDN::running_config();
return $cmdname eq 'add' ? [] : [PVE::Network::SDN::sdn_zones_ids($cfg)];
}
sub get_zone {
my ($zoneid, $running) = @_;
my $cfg = PVE::Network::SDN::Zones::config($running);
my $zone = PVE::Network::SDN::Zones::sdn_zones_config($cfg, $zoneid, 1);
return $zone;
}
sub get_vnets {
my ($zoneid, $running) = @_;
return if !$zoneid;
my $vnets_config = PVE::Network::SDN::Vnets::config($running);
my $vnets = undef;
for my $vnetid (keys %{ $vnets_config->{ids} }) {
my $vnet = PVE::Network::SDN::Vnets::sdn_vnets_config($vnets_config, $vnetid);
next if !$vnet->{zone} || $vnet->{zone} ne $zoneid;
$vnets->{$vnetid} = $vnet;
}
return $vnets;
}
sub generate_etc_network_config {
my $cfg = PVE::Network::SDN::running_config();
my $version = $cfg->{version};
my $vnet_cfg = $cfg->{vnets};
my $zone_cfg = $cfg->{zones};
my $subnet_cfg = $cfg->{subnets};
my $controller_cfg = $cfg->{controllers};
return if !$vnet_cfg && !$zone_cfg;
my $interfaces_config = PVE::INotify::read_file('interfaces');
#generate configuration
my $config = {};
my $nodename = PVE::INotify::nodename();
for my $id (sort keys %{ $vnet_cfg->{ids} }) {
my $vnet = $vnet_cfg->{ids}->{$id};
my $zone = $vnet->{zone};
if (!$zone) {
warn "can't generate vnet '$id': no zone assigned!\n";
next;
}
my $plugin_config = $zone_cfg->{ids}->{$zone};
if (!defined($plugin_config)) {
warn "can't generate vnet '$id': zone $zone don't exist\n";
next;
}
next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
my $controller;
if (my $controllerid = $plugin_config->{controller}) {
$controller = $controller_cfg->{ids}->{$controllerid};
}
my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
eval {
$plugin->generate_sdn_config(
$plugin_config,
$zone,
$id,
$vnet,
$controller,
$controller_cfg,
$subnet_cfg,
$interfaces_config,
$config,
);
};
if (my $err = $@) {
warn "zone $zone : vnet $id : $err\n";
next;
}
}
my $raw_network_config = "\#version:$version\n";
foreach my $iface (sort keys %$config) {
$raw_network_config .= "\n";
$raw_network_config .= "auto $iface\n";
$raw_network_config .= "iface $iface\n";
foreach my $option (@{ $config->{$iface} }) {
$raw_network_config .= "\t$option\n";
}
}
return $raw_network_config;
}
sub write_etc_network_config {
my ($rawconfig) = @_;
return if !$rawconfig;
my $writefh = IO::File->new($local_network_sdn_file, ">");
print $writefh $rawconfig;
$writefh->close();
}
sub read_etc_network_config_version {
my $versionstr = PVE::Tools::file_read_firstline($local_network_sdn_file);
return if !defined($versionstr);
if ($versionstr =~ m/^\#version:(\d+)$/) {
return $1;
}
}
sub ifquery_check {
my $cmd = ['ifquery', '-a', '-c', '-o', 'json'];
my $result = '';
my $reader = sub { $result .= shift };
eval { run_command($cmd, outfunc => $reader); };
my $resultjson = decode_json($result);
my $interfaces = {};
foreach my $interface (@$resultjson) {
my $name = $interface->{name};
$interfaces->{$name} = {
status => $interface->{status},
config => $interface->{config},
config_status => $interface->{config_status},
};
}
return $interfaces;
}
my $warned_about_reload;
sub status {
my $err_config = undef;
my $local_version = PVE::Network::SDN::Zones::read_etc_network_config_version();
my $cfg = PVE::Network::SDN::running_config();
my $sdn_version = $cfg->{version};
return if !$sdn_version;
if (!$local_version) {
$err_config = "local sdn network configuration is not yet generated, please reload";
if (!$warned_about_reload) {
$warned_about_reload = 1;
warn "$err_config\n";
}
} elsif ($local_version < $sdn_version) {
$err_config = "local sdn network configuration is too old, please reload";
if (!$warned_about_reload) {
$warned_about_reload = 1;
warn "$err_config\n";
}
} else {
$warned_about_reload = 0;
}
my $status = ifquery_check();
my $vnet_cfg = $cfg->{vnets};
my $zone_cfg = $cfg->{zones};
my $nodename = PVE::INotify::nodename();
my $vnet_status = {};
my $zone_status = {};
for my $id (sort keys %{ $zone_cfg->{ids} }) {
next
if defined($zone_cfg->{ids}->{$id}->{nodes})
&& !$zone_cfg->{ids}->{$id}->{nodes}->{$nodename};
$zone_status->{$id}->{status} = $err_config ? 'pending' : 'available';
}
foreach my $id (sort keys %{ $vnet_cfg->{ids} }) {
my $vnet = $vnet_cfg->{ids}->{$id};
my $zone = $vnet->{zone};
next if !defined($zone);
my $plugin_config = $zone_cfg->{ids}->{$zone};
if (!defined($plugin_config)) {
$vnet_status->{$id}->{status} = 'error';
$vnet_status->{$id}->{statusmsg} = "unknown zone '$zone' configured";
next;
}
next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
$vnet_status->{$id}->{zone} = $zone;
$vnet_status->{$id}->{status} = 'available';
if ($err_config) {
$vnet_status->{$id}->{status} = 'pending';
$vnet_status->{$id}->{statusmsg} = $err_config;
next;
}
my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
my $err_msg = $plugin->status($plugin_config, $zone, $id, $vnet, $status);
if (@{$err_msg} > 0) {
$vnet_status->{$id}->{status} = 'error';
$vnet_status->{$id}->{statusmsg} = join(',', @{$err_msg});
$zone_status->{$zone}->{status} = 'error';
}
}
return ($zone_status, $vnet_status);
}
sub tap_create {
my ($iface, $bridge) = @_;
my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge, 1);
if (!$vnet) { # fallback for classic bridge
PVE::Network::tap_create($iface, $bridge);
return;
}
my $plugin_config = get_plugin_config($vnet);
my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
$plugin->tap_create($plugin_config, $vnet, $iface, $bridge);
}
sub veth_create {
my ($veth, $vethpeer, $bridge, $hwaddr) = @_;
my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge, 1);
if (!$vnet) { # fallback for classic bridge
PVE::Network::veth_create($veth, $vethpeer, $bridge, $hwaddr);
return;
}
my $plugin_config = get_plugin_config($vnet);
my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
$plugin->veth_create($plugin_config, $vnet, $veth, $vethpeer, $bridge, $hwaddr);
}
sub tap_plug {
my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge, 1);
if (!$vnet) { # fallback for classic bridge
my $interfaces_config = PVE::INotify::read_file('interfaces');
my $opts = {};
$opts->{learning} = 0
if $interfaces_config->{ifaces}->{$bridge}
&& $interfaces_config->{ifaces}->{$bridge}->{'bridge-disable-mac-learning'};
PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate, $opts);
return;
}
my $plugin_config = get_plugin_config($vnet);
my $nodename = PVE::INotify::nodename();
die "vnet $bridge is not allowed on this node\n"
if $plugin_config->{nodes} && !defined($plugin_config->{nodes}->{$nodename});
my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
$plugin->tap_plug($plugin_config, $vnet, $tag, $iface, $bridge, $firewall, $trunks, $rate);
}
sub add_bridge_fdb {
my ($iface, $macaddr, $bridge) = @_;
my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge, 1);
if (!$vnet) { # fallback for classic bridge
PVE::Network::add_bridge_fdb($iface, $macaddr);
return;
}
my $plugin_config = get_plugin_config($vnet);
my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
$plugin->add_bridge_fdb($plugin_config, $iface, $macaddr);
}
sub del_bridge_fdb {
my ($iface, $macaddr, $bridge) = @_;
my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge, 1);
if (!$vnet) { # fallback for classic bridge
PVE::Network::del_bridge_fdb($iface, $macaddr);
return;
}
my $plugin_config = get_plugin_config($vnet);
my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
$plugin->del_bridge_fdb($plugin_config, $iface, $macaddr);
}
sub get_mtu {
my ($zone_config) = @_;
my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($zone_config->{type});
return $plugin->get_mtu($zone_config) // $default_mtu;
}
1;
|