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
|
package PVE::Network::SDN::Zones::Plugin;
use strict;
use warnings;
use PVE::Tools qw(run_command);
use PVE::JSONSchema;
use PVE::Cluster;
use PVE::Network;
use PVE::JSONSchema qw(get_standard_option);
use base qw(PVE::SectionConfig);
PVE::Cluster::cfs_register_file(
'sdn/zones.cfg',
sub { __PACKAGE__->parse_config(@_); },
sub { __PACKAGE__->write_config(@_); },
);
PVE::JSONSchema::register_standard_option(
'pve-sdn-zone-id',
{
description => "The SDN zone object identifier.",
type => 'string',
format => 'pve-sdn-zone-id',
},
);
PVE::JSONSchema::register_format('pve-sdn-zone-id', \&parse_sdn_zone_id);
sub parse_sdn_zone_id {
my ($id, $noerr) = @_;
if ($id !~ m/^[a-z][a-z0-9]*[a-z0-9]$/i) {
return undef if $noerr;
die "zone ID '$id' contains illegal characters\n";
}
die "zone ID '$id' can't be more length than 8 characters\n" if length($id) > 8;
return $id;
}
my $defaultData = {
propertyList => {
type => {
description => "Plugin type.",
type => 'string',
format => 'pve-configid',
type => 'string',
},
nodes => get_standard_option('pve-node-list', { optional => 1 }),
zone => get_standard_option(
'pve-sdn-zone-id',
{
completion => \&PVE::Network::SDN::Zones::complete_sdn_zone,
},
),
ipam => {
type => 'string',
description => "use a specific ipam",
optional => 1,
},
},
};
sub private {
return $defaultData;
}
sub parse_section_header {
my ($class, $line) = @_;
if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
my ($type, $id) = (lc($1), $2);
my $errmsg = undef; # set if you want to skip whole section
eval { PVE::JSONSchema::pve_verify_configid($type); };
$errmsg = $@ if $@;
my $config = {}; # to return additional attributes
return ($type, $id, $errmsg, $config);
}
return undef;
}
sub decode_value {
my ($class, $type, $key, $value) = @_;
if ($key eq 'nodes' || $key eq 'exitnodes') {
my $res = {};
foreach my $node (PVE::Tools::split_list($value)) {
if (PVE::JSONSchema::pve_verify_node_name($node)) {
$res->{$node} = 1;
}
}
return $res;
}
return $value;
}
sub encode_value {
my ($class, $type, $key, $value) = @_;
if ($key eq 'nodes' || $key eq 'exitnodes') {
return join(',', keys(%$value));
}
return $value;
}
sub generate_sdn_config {
my (
$class,
$plugin_config,
$zoneid,
$vnetid,
$vnet,
$controller,
$controller_cfg,
$subnet_cfg,
$interfaces_config,
$config,
) = @_;
die "please implement inside plugin";
}
sub generate_controller_config {
my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
die "please implement inside plugin";
}
sub generate_controller_vnet_config {
my ($class, $plugin_config, $controller, $zoneid, $vnetid, $config) = @_;
}
sub write_controller_config {
my ($class, $plugin_config, $config) = @_;
die "please implement inside plugin";
}
sub controller_reload {
my ($class) = @_;
die "please implement inside plugin";
}
sub on_delete_hook {
my ($class, $zoneid, $vnet_cfg) = @_;
# verify that no vnet are associated to this zone
foreach my $id (keys %{ $vnet_cfg->{ids} }) {
my $vnet = $vnet_cfg->{ids}->{$id};
die "zone $zoneid is used by vnet $id"
if ($vnet->{type} eq 'vnet' && defined($vnet->{zone}) && $vnet->{zone} eq $zoneid);
}
}
sub on_update_hook {
my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
# do nothing by default
}
sub vnet_update_hook {
my ($class, $vnet_cfg, $vnetid, $zone_cfg) = @_;
# do nothing by default
}
#helpers
sub parse_tag_number_or_range {
my ($str, $max, $tag) = @_;
my @elements = split(/,/, $str);
my $count = 0;
my $allowed = undef;
die "extraneous commas in list\n" if $str ne join(',', @elements);
foreach my $item (@elements) {
if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
$count += 2;
my ($port1, $port2) = ($1, $2);
die "invalid port '$port1'\n" if $port1 > $max;
die "invalid port '$port2'\n" if $port2 > $max;
die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n"
if $port1 > $port2;
if ($tag && $tag >= $port1 && $tag <= $port2) {
$allowed = 1;
last;
}
} elsif ($item =~ m/^([0-9]+)$/) {
$count += 1;
my $port = $1;
die "invalid port '$port'\n" if $port > $max;
if ($tag && $tag == $port) {
$allowed = 1;
last;
}
}
}
die "tag $tag is not allowed" if $tag && !$allowed;
return (scalar(@elements) > 1);
}
sub generate_status_message {
my ($class, $vnetid, $status, $ifaces) = @_;
my $err_msg = [];
return ["vnet is not generated. Please check the 'reload network' task log."]
if !$status->{$vnetid}->{status};
foreach my $iface (@{$ifaces}) {
if (!$status->{$iface}->{status}) {
push @$err_msg, "missing $iface";
} elsif ($status->{$iface}->{status} ne 'pass') {
push @$err_msg, "error $iface";
}
}
return $err_msg;
}
sub status {
my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_;
return $class->generate_status_message($vnetid, $status);
}
sub tap_create {
my ($class, $plugin_config, $vnet, $iface, $vnetid) = @_;
PVE::Network::tap_create($iface, $vnetid);
}
sub veth_create {
my ($class, $plugin_config, $vnet, $veth, $vethpeer, $vnetid, $hwaddr) = @_;
PVE::Network::veth_create($veth, $vethpeer, $vnetid, $hwaddr);
}
sub tap_plug {
my ($class, $plugin_config, $vnet, $tag, $iface, $vnetid, $firewall, $trunks, $rate) = @_;
my $vlan_aware =
PVE::Tools::file_read_firstline("/sys/class/net/$vnetid/bridge/vlan_filtering");
die "vm vlans are not allowed on vnet $vnetid" if !$vlan_aware && ($tag || $trunks);
my $opts = {};
$opts->{learning} = 0 if $plugin_config->{'bridge-disable-mac-learning'};
$opts->{isolation} = 1 if $vnet->{'isolate-ports'};
PVE::Network::tap_plug($iface, $vnetid, $tag, $firewall, $trunks, $rate, $opts);
}
sub add_bridge_fdb {
my ($class, $plugin_config, $iface, $macaddr) = @_;
PVE::Network::add_bridge_fdb($iface, $macaddr)
if $plugin_config->{'bridge-disable-mac-learning'};
}
sub del_bridge_fdb {
my ($class, $plugin_config, $iface, $macaddr) = @_;
PVE::Network::del_bridge_fdb($iface, $macaddr)
if $plugin_config->{'bridge-disable-mac-learning'};
}
#helper
sub get_uplink_iface {
my ($interfaces_config, $uplink) = @_;
my $iface = undef;
foreach my $id (keys %{ $interfaces_config->{ifaces} }) {
my $interface = $interfaces_config->{ifaces}->{$id};
if (my $iface_uplink = $interface->{'uplink-id'}) {
next if $iface_uplink ne $uplink;
if ($interface->{type} ne 'eth' && $interface->{type} ne 'bond') {
warn "uplink $uplink is not a physical or bond interface";
next;
}
$iface = $id;
}
}
#create a dummy uplink interface if no uplink found
if (!$iface) {
warn "can't find uplink $uplink in physical interface";
$iface = "uplink${uplink}";
}
return $iface;
}
sub get_local_route_ip {
my ($targetip) = @_;
my $ip = undef;
my $interface = undef;
run_command(
['/sbin/ip', 'route', 'get', $targetip],
outfunc => sub {
if ($_[0] =~ m/src ($PVE::Tools::IPRE)/) {
$ip = $1;
}
if ($_[0] =~ m/dev (\S+)/) {
$interface = $1;
}
},
);
return ($ip, $interface);
}
sub find_local_ip_interface_peers {
my ($peers, $iface) = @_;
my $network_config = PVE::INotify::read_file('interfaces');
my $ifaces = $network_config->{ifaces};
#if iface is defined, return ip if exist (if not,try to find it on other ifaces)
if ($iface) {
my $ip = $ifaces->{$iface}->{address};
return ($ip, $iface) if $ip;
}
#is a local ip member of peers list ?
foreach my $address (@{$peers}) {
while (my $interface = each %$ifaces) {
my $ip = $ifaces->{$interface}->{address};
if ($ip && $ip eq $address) {
return ($ip, $interface);
}
}
}
#if peer is remote, find source with ip route
foreach my $address (@{$peers}) {
my ($ip, $interface) = get_local_route_ip($address);
return ($ip, $interface);
}
}
sub find_bridge {
my ($bridge) = @_;
die "can't find bridge $bridge" if !-d "/sys/class/net/$bridge";
}
sub is_vlanaware {
my ($bridge) = @_;
return PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
}
sub get_bridge_ifaces {
my ($bridge) = @_;
my @bridge_ifaces = ();
my $dir = "/sys/class/net/$bridge/brif";
PVE::Tools::dir_glob_foreach(
$dir,
'(((eth|bond)\d+|en[^.]+)(\.\d+)?)',
sub {
push @bridge_ifaces, $_[0];
},
);
return @bridge_ifaces;
}
sub datacenter_config {
return PVE::Cluster::cfs_read_file('datacenter.cfg');
}
sub get_mtu {
my ($class, $plugin_config) = @_;
die "please implement inside plugin";
}
1;
|