1 package PVE
::Network
::SDN
;
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 use PVE
::Network
::SDN
::Plugin
;
12 use PVE
::Network
::SDN
::VnetPlugin
;
13 use PVE
::Network
::SDN
::VlanPlugin
;
14 use PVE
::Network
::SDN
::VxlanPlugin
;
15 use PVE
::Network
::SDN
::FrrPlugin
;
17 PVE
::Network
::SDN
::VnetPlugin-
>register();
18 PVE
::Network
::SDN
::VlanPlugin-
>register();
19 PVE
::Network
::SDN
::VxlanPlugin-
>register();
20 PVE
::Network
::SDN
::FrrPlugin-
>register();
21 PVE
::Network
::SDN
::Plugin-
>init();
25 my ($cfg, $sdnid, $noerr) = @_;
27 die "no sdn ID specified\n" if !$sdnid;
29 my $scfg = $cfg->{ids
}->{$sdnid};
30 die "sdn '$sdnid' does not exists\n" if (!$noerr && !$scfg);
36 my $config = cfs_read_file
("sdn.cfg.new");
37 $config = cfs_read_file
("sdn.cfg") if !keys %{$config->{ids
}};
44 cfs_write_file
("sdn.cfg.new", $cfg);
48 my ($code, $errmsg) = @_;
50 cfs_lock_file
("sdn.cfg.new", undef, $code);
52 $errmsg ?
die "$errmsg: $err" : die $err;
59 return keys %{$cfg->{ids
}};
63 my ($cmdname, $pname, $cvalue) = @_;
65 my $cfg = PVE
::Network
::SDN
::config
();
67 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::sdn_ids
($cfg) ];
72 my $cmd = ['ifquery', '-a', '-c', '-o','json'];
75 my $reader = sub { $result .= shift };
78 run_command
($cmd, outfunc
=> $reader);
81 my $resultjson = decode_json
($result);
84 foreach my $interface (@$resultjson) {
85 my $name = $interface->{name
};
86 $interfaces->{$name} = {
87 status
=> $interface->{status
},
88 config
=> $interface->{config
},
89 config_status
=> $interface->{config_status
},
97 sub generate_etc_network_config
{
99 my $sdn_cfg = PVE
::Cluster
::cfs_read_file
('sdn.cfg');
102 #read main config for physical interfaces
103 my $current_config_file = "/etc/network/interfaces";
104 my $fh = IO
::File-
>new($current_config_file);
105 my $interfaces_config = PVE
::INotify
::read_etc_network_interfaces
(1,$fh);
110 foreach my $id (keys %{$interfaces_config->{ifaces
}}) {
111 my $interface = $interfaces_config->{ifaces
}->{$id};
112 if (my $uplink = $interface->{'uplink-id'}) {
113 die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
114 $interface->{name
} = $id;
115 $uplinks->{$interface->{'uplink-id'}} = $interface;
119 my $vnet_cfg = undef;
120 my $transport_cfg = undef;
122 foreach my $id (keys %{$sdn_cfg->{ids
}}) {
123 if ($sdn_cfg->{ids
}->{$id}->{type
} eq 'vnet') {
124 $vnet_cfg->{ids
}->{$id} = $sdn_cfg->{ids
}->{$id};
126 $transport_cfg->{ids
}->{$id} = $sdn_cfg->{ids
}->{$id};
130 #generate configuration
132 foreach my $id (keys %{$vnet_cfg->{ids
}}) {
133 my $vnet = $vnet_cfg->{ids
}->{$id};
134 my $zone = $vnet->{transportzone
};
137 warn "can't generate vnet $vnet : zone $zone don't exist";
141 my $plugin_config = $transport_cfg->{ids
}->{$zone};
143 if (!defined($plugin_config)) {
144 warn "can't generate vnet $vnet : zone $zone don't exist";
148 my $plugin = PVE
::Network
::SDN
::Plugin-
>lookup($plugin_config->{type
});
149 $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $uplinks, $config);
152 my $raw_network_config = "";
153 foreach my $iface (keys %$config) {
154 $raw_network_config .= "\n";
155 $raw_network_config .= "auto $iface\n";
156 $raw_network_config .= "iface $iface\n";
157 foreach my $option (@{$config->{$iface}}) {
158 $raw_network_config .= "\t$option\n";
162 return $raw_network_config;
165 sub generate_frr_config
{
167 my $sdn_cfg = PVE
::Cluster
::cfs_read_file
('sdn.cfg');
170 #read main config for physical interfaces
171 my $current_config_file = "/etc/network/interfaces";
172 my $fh = IO
::File-
>new($current_config_file);
173 my $interfaces_config = PVE
::INotify
::read_etc_network_interfaces
(1,$fh);
178 foreach my $id (keys %{$interfaces_config->{ifaces
}}) {
179 my $interface = $interfaces_config->{ifaces
}->{$id};
180 if (my $uplink = $interface->{'uplink-id'}) {
181 die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
182 $interface->{name
} = $id;
183 $uplinks->{$interface->{'uplink-id'}} = $interface;
188 my $transport_cfg = undef;
190 foreach my $id (keys %{$sdn_cfg->{ids
}}) {
191 if ($sdn_cfg->{ids
}->{$id}->{type
} eq 'frr') {
192 $frr_cfg->{ids
}->{$id} = $sdn_cfg->{ids
}->{$id};
193 } elsif ($sdn_cfg->{ids
}->{$id}->{type
} ne 'vnet') {
194 $transport_cfg->{ids
}->{$id} = $sdn_cfg->{ids
}->{$id};
198 #generate configuration
201 foreach my $id (keys %{$frr_cfg->{ids
}}) {
202 my $plugin_config = $frr_cfg->{ids
}->{$id};
203 my $asn = $plugin_config->{asn
};
205 my $plugin = PVE
::Network
::SDN
::Plugin-
>lookup($plugin_config->{type
});
206 $plugin->generate_frr_config($plugin_config, $asn, $id, $uplinks, $config);
210 foreach my $id (keys %{$transport_cfg->{ids
}}) {
211 my $plugin_config = $transport_cfg->{ids
}->{$id};
212 my $router = $plugin_config->{router
};
214 my $asn = $frr_cfg->{ids
}->{$router}->{asn
};
216 my $plugin = PVE
::Network
::SDN
::Plugin-
>lookup($plugin_config->{type
});
217 $plugin->generate_frr_config($plugin_config, $asn, $id, $uplinks, $config);
222 my $raw_frr_config = "log syslog informational\n";
223 $raw_frr_config .= "!\n";
226 my $vrfconfig = $config->{vrf
};
227 foreach my $vrf (sort keys %$vrfconfig) {
228 $raw_frr_config .= "$vrf\n";
229 foreach my $option (@{$vrfconfig->{$vrf}}) {
230 $raw_frr_config .= " $option\n";
232 $raw_frr_config .= "!\n";
236 my $routerconfig = $config->{router
};
237 foreach my $router (sort keys %$routerconfig) {
238 $raw_frr_config .= "$router\n";
239 foreach my $option (@{$routerconfig->{$router}}) {
240 $raw_frr_config .= " $option\n";
242 $raw_frr_config .= "!\n";
245 $raw_frr_config .= "line vty\n";
246 $raw_frr_config .= "!\n";
248 return $raw_frr_config;
251 sub write_etc_network_config
{
252 my ($rawconfig) = @_;
254 return if !$rawconfig;
255 my $sdn_interfaces_file = "/etc/network/interfaces.d/sdn";
257 my $writefh = IO
::File-
>new($sdn_interfaces_file,">");
258 print $writefh $rawconfig;
265 my $cluster_sdn_file = "/etc/pve/sdn.cfg";
266 my $local_sdn_file = "/etc/network/interfaces.d/sdn";
267 my $err_config = undef;
269 return if !-e
$cluster_sdn_file;
271 if (!-e
$local_sdn_file) {
272 warn "local sdn network configuration is not yet generated, please reload";
273 $err_config = 'pending';
275 # fixme : use some kind of versioning info?
276 my $cluster_sdn_timestamp = (stat($cluster_sdn_file))[9];
277 my $local_sdn_timestamp = (stat($local_sdn_file))[9];
279 if ($local_sdn_timestamp < $cluster_sdn_timestamp) {
280 warn "local sdn network configuration is too old, please reload";
281 $err_config = 'unknown';
285 my $status = ifquery_check
();
287 my $network_cfg = PVE
::Cluster
::cfs_read_file
('sdn.cfg');
288 my $vnet_cfg = undef;
289 my $transport_cfg = undef;
291 my $vnet_status = {};
292 my $transport_status = {};
294 foreach my $id (keys %{$network_cfg->{ids
}}) {
295 if ($network_cfg->{ids
}->{$id}->{type
} eq 'vnet') {
296 my $transportzone = $network_cfg->{ids
}->{$id}->{transportzone
};
297 $vnet_status->{$id}->{transportzone
} = $transportzone;
298 $transport_status->{$transportzone}->{status
} = 'available' if !defined($transport_status->{$transportzone}->{status
});
301 $vnet_status->{$id}->{status
} = $err_config;
302 $transport_status->{$transportzone}->{status
} = $err_config;
303 } elsif ($status->{$id}->{status
} && $status->{$id}->{status
} eq 'pass') {
304 $vnet_status->{$id}->{status
} = 'available';
305 my $bridgeport = $status->{$id}->{config
}->{'bridge-ports'};
307 if ($status->{$bridgeport}->{status
} && $status->{$bridgeport}->{status
} ne 'pass') {
308 $vnet_status->{$id}->{status
} = 'error';
309 $transport_status->{$transportzone}->{status
} = 'error';
312 $vnet_status->{$id}->{status
} = 'error';
313 $transport_status->{$transportzone}->{status
} = 'error';
317 return($transport_status, $vnet_status);