[pve-devel] [PATCH pve-network 2/2] add network transport api
Alexandre Derumier
aderumier at odiso.com
Tue Apr 2 12:09:10 CEST 2019
---
PVE/API2/Makefile | 5 +
PVE/API2/Network/Makefile | 8 ++
PVE/API2/Network/Transport.pm | 235 ++++++++++++++++++++++++++++++++++++
PVE/Makefile | 1 +
PVE/Network/Plugin.pm | 9 +-
PVE/Network/Transport.pm | 62 ++++++++++
PVE/Network/VlanPlugin.pm | 7 +-
PVE/Network/VxlanMulticastPlugin.pm | 2 +-
8 files changed, 321 insertions(+), 8 deletions(-)
create mode 100644 PVE/API2/Makefile
create mode 100644 PVE/API2/Network/Makefile
create mode 100644 PVE/API2/Network/Transport.pm
create mode 100644 PVE/Network/Transport.pm
diff --git a/PVE/API2/Makefile b/PVE/API2/Makefile
new file mode 100644
index 0000000..5bc7988
--- /dev/null
+++ b/PVE/API2/Makefile
@@ -0,0 +1,5 @@
+
+
+.PHONY: install
+install:
+ make -C Network install
diff --git a/PVE/API2/Network/Makefile b/PVE/API2/Network/Makefile
new file mode 100644
index 0000000..92fa5a7
--- /dev/null
+++ b/PVE/API2/Network/Makefile
@@ -0,0 +1,8 @@
+SOURCES=Transport.pm
+
+
+PERL5DIR=${DESTDIR}/usr/share/perl5
+
+.PHONY: install
+install:
+ for i in ${SOURCES}; do install -D -m 0644 $$i ${PERL5DIR}/PVE/API2/Network/$$i; done
diff --git a/PVE/API2/Network/Transport.pm b/PVE/API2/Network/Transport.pm
new file mode 100644
index 0000000..bddae40
--- /dev/null
+++ b/PVE/API2/Network/Transport.pm
@@ -0,0 +1,235 @@
+package PVE::API2::Network::Transport;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use PVE::Tools qw(extract_param);
+use PVE::Cluster qw(cfs_read_file cfs_write_file);
+use PVE::Network::Transport;
+use PVE::Network::Plugin;
+use PVE::Network::VlanPlugin;
+use PVE::Network::VxlanMulticastPlugin;
+use Storable qw(dclone);
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::RPCEnvironment;
+
+use PVE::RESTHandler;
+
+use base qw(PVE::RESTHandler);
+
+my $transport_type_enum = PVE::Network::Plugin->lookup_types();
+
+my $api_transport_config = sub {
+ my ($cfg, $transportid) = @_;
+
+ my $scfg = dclone(PVE::Network::Transport::transport_config($cfg, $transportid));
+ $scfg->{transport} = $transportid;
+ $scfg->{digest} = $cfg->{digest};
+
+ return $scfg;
+};
+
+__PACKAGE__->register_method ({
+ name => 'index',
+ path => '',
+ method => 'GET',
+ description => "Transport index.",
+ permissions => {
+ description => "Only list entries where you have 'NetworkTransport.Audit' or 'NetworkTransport.Allocate' permissions on '/networktransports/<transport>'",
+ user => 'all',
+ },
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ type => {
+ description => "Only list transport of specific type",
+ type => 'string',
+ enum => $transport_type_enum,
+ optional => 1,
+ },
+ },
+ },
+ returns => {
+ type => 'array',
+ items => {
+ type => "object",
+ properties => { transport => { type => 'string'} },
+ },
+ links => [ { rel => 'child', href => "{transport}" } ],
+ },
+ code => sub {
+ my ($param) = @_;
+
+ my $rpcenv = PVE::RPCEnvironment::get();
+ my $authuser = $rpcenv->get_user();
+
+
+ my $cfg = PVE::Network::Transport::config();
+
+ my @sids = PVE::Network::Transport::transports_ids($cfg);
+ my $res = [];
+ foreach my $transportid (@sids) {
+# my $privs = [ 'NetworkTransport.Audit', 'NetworkTransport.Allocate' ];
+# next if !$rpcenv->check_any($authuser, "/network/transports/$transportid", $privs, 1);
+
+ my $scfg = &$api_transport_config($cfg, $transportid);
+ next if $param->{type} && $param->{type} ne $scfg->{type};
+ push @$res, $scfg;
+ }
+
+ return $res;
+ }});
+
+__PACKAGE__->register_method ({
+ name => 'read',
+ path => '{transport}',
+ method => 'GET',
+ description => "Read transport configuration.",
+# permissions => {
+# check => ['perm', '/network/transports/{transport}', ['NetworkTransport.Allocate']],
+# },
+
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ transport => get_standard_option('pve-transport-id'),
+ },
+ },
+ returns => { type => 'object' },
+ code => sub {
+ my ($param) = @_;
+
+ my $cfg = PVE::Network::Transport::config();
+
+ return &$api_transport_config($cfg, $param->{transport});
+ }});
+
+__PACKAGE__->register_method ({
+ name => 'create',
+ protected => 1,
+ path => '',
+ method => 'POST',
+ description => "Create a new network transport.",
+# permissions => {
+# check => ['perm', '/network/transports', ['NetworkTransport.Allocate']],
+# },
+ parameters => PVE::Network::Plugin->createSchema(),
+ returns => { type => 'null' },
+ code => sub {
+ my ($param) = @_;
+
+ my $type = extract_param($param, 'type');
+ my $transportid = extract_param($param, 'transport');
+
+ my $plugin = PVE::Network::Plugin->lookup($type);
+ my $opts = $plugin->check_config($transportid, $param, 1, 1);
+
+ PVE::Network::Transport::lock_transport_config(
+ sub {
+
+ my $cfg = PVE::Network::Transport::config();
+
+ if (my $scfg = PVE::Network::Transport::transport_config($cfg, $transportid, 1)) {
+ die "network transport ID '$transportid' already defined\n";
+ }
+
+ $cfg->{ids}->{$transportid} = $opts;
+
+ #improveme:
+ #check local configuration of all nodes for conflict
+
+ PVE::Network::Transport::write_config($cfg);
+
+ }, "create network transport failed");
+
+ return undef;
+ }});
+
+__PACKAGE__->register_method ({
+ name => 'update',
+ protected => 1,
+ path => '{transport}',
+ method => 'PUT',
+ description => "Update network transport configuration.",
+# permissions => {
+# check => ['perm', '/network/transports', ['NetworkTransport.Allocate']],
+# },
+ parameters => PVE::Network::Plugin->updateSchema(),
+ returns => { type => 'null' },
+ code => sub {
+ my ($param) = @_;
+
+ my $transportid = extract_param($param, 'transport');
+ my $digest = extract_param($param, 'digest');
+
+ PVE::Network::Transport::lock_transport_config(
+ sub {
+
+ my $cfg = PVE::Network::Transport::config();
+
+ PVE::SectionConfig::assert_if_modified($cfg, $digest);
+
+ my $scfg = PVE::Network::Transport::transport_config($cfg, $transportid);
+
+ my $plugin = PVE::Network::Plugin->lookup($scfg->{type});
+ my $opts = $plugin->check_config($transportid, $param, 0, 1);
+
+ foreach my $k (%$opts) {
+ $scfg->{$k} = $opts->{$k};
+ }
+ #improveme:
+ #add vlan/vxlan check on existingvnets
+ #check local configuration of all nodes for conflict
+ PVE::Network::Transport::write_config($cfg);
+
+ }, "update network transport failed");
+
+ return undef;
+ }});
+
+__PACKAGE__->register_method ({
+ name => 'delete',
+ protected => 1,
+ path => '{transport}', # /network/transports/{transport}
+ method => 'DELETE',
+ description => "Delete network transport configuration.",
+# permissions => {
+# check => ['perm', '/network/transports', ['NetworkTransport.Allocate']],
+# },
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ transport => get_standard_option('pve-transport-id', {
+ completion => \&PVE::Network::Transport::complete_transport,
+ }),
+ },
+ },
+ returns => { type => 'null' },
+ code => sub {
+ my ($param) = @_;
+
+ my $transportid = extract_param($param, 'transport');
+
+ PVE::Network::Transport::lock_transport_config(
+ sub {
+
+ my $cfg = PVE::Network::Transport::config();
+
+ my $scfg = PVE::Network::Transport::transport_config($cfg, $transportid);
+
+# my $plugin = PVE::Network::Plugin->lookup($scfg->{type});
+# $plugin->on_delete_hook($transportid, $scfg);
+
+ delete $cfg->{ids}->{$transportid};
+ #improveme:
+ #check that vnet don't use this transport
+ PVE::Network::Transport::write_config($cfg);
+
+ }, "delete network transport failed");
+
+
+ return undef;
+ }});
+
+1;
diff --git a/PVE/Makefile b/PVE/Makefile
index 626c2c5..1fb961d 100644
--- a/PVE/Makefile
+++ b/PVE/Makefile
@@ -1,3 +1,4 @@
.PHONY: install
install:
make -C Network install
+ make -C API2 install
diff --git a/PVE/Network/Plugin.pm b/PVE/Network/Plugin.pm
index b186d8f..36cd2ed 100644
--- a/PVE/Network/Plugin.pm
+++ b/PVE/Network/Plugin.pm
@@ -8,7 +8,7 @@ use PVE::JSONSchema;
use PVE::Cluster;
use Data::Dumper;
-
+use PVE::JSONSchema qw(get_standard_option);
use base qw(PVE::SectionConfig);
PVE::Cluster::cfs_register_file('network/transports.cfg',
@@ -23,11 +23,8 @@ my $defaultData = {
type => 'string', format => 'pve-configid',
type => 'string',
},
- 'uplink-id' => {
- type => 'integer',
- minimum => 1, maximum => 4096,
- description => 'Uplink interface',
- },
+ transport => get_standard_option('pve-transport-id',
+ { completion => \&PVE::Network::Transport::complete_transport }),
},
};
diff --git a/PVE/Network/Transport.pm b/PVE/Network/Transport.pm
new file mode 100644
index 0000000..1b8a2ca
--- /dev/null
+++ b/PVE/Network/Transport.pm
@@ -0,0 +1,62 @@
+package PVE::Network::Transport;
+
+use strict;
+use warnings;
+use Data::Dumper;
+use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
+use PVE::Network::Plugin;
+use PVE::Network::VlanPlugin;
+use PVE::Network::VxlanMulticastPlugin;
+
+PVE::Network::VlanPlugin->register();
+PVE::Network::VxlanMulticastPlugin->register();
+PVE::Network::Plugin->init();
+
+
+sub transport_config {
+ my ($cfg, $transportid, $noerr) = @_;
+
+ die "no transport ID specified\n" if !$transportid;
+
+ my $scfg = $cfg->{ids}->{$transportid};
+ die "transport '$transportid' does not exists\n" if (!$noerr && !$scfg);
+
+ return $scfg;
+}
+
+sub config {
+
+ return cfs_read_file("network/transports.cfg");
+}
+
+sub write_config {
+ my ($cfg) = @_;
+
+ cfs_write_file("network/transports.cfg", $cfg);
+}
+
+sub lock_transport_config {
+ my ($code, $errmsg) = @_;
+
+ cfs_lock_file("network/transports.cfg", undef, $code);
+ my $err = $@;
+ if ($err) {
+ $errmsg ? die "$errmsg: $err" : die $err;
+ }
+}
+
+sub transports_ids {
+ my ($cfg) = @_;
+
+ return keys %{$cfg->{ids}};
+}
+
+sub complete_transport {
+ my ($cmdname, $pname, $cvalue) = @_;
+
+ my $cfg = PVE::Network::Transport::config();
+
+ return $cmdname eq 'add' ? [] : [ PVE::Network::Transport::transports_ids($cfg) ];
+}
+
+1;
diff --git a/PVE/Network/VlanPlugin.pm b/PVE/Network/VlanPlugin.pm
index 6f76f18..7d64549 100644
--- a/PVE/Network/VlanPlugin.pm
+++ b/PVE/Network/VlanPlugin.pm
@@ -21,6 +21,11 @@ sub pve_verify_network_vlanrange {
sub properties {
return {
+ 'uplink-id' => {
+ type => 'integer',
+ minimum => 1, maximum => 4096,
+ description => 'Uplink interface',
+ },
'vlan-allowed' => {
type => 'string', format => 'pve-network-vlanrange',
description => "Allowed vlan range",
@@ -42,7 +47,7 @@ sub properties {
sub options {
return {
- 'uplink-id' => { fixed => 1 },
+ 'uplink-id' => { optional => 1 },
'vlan-allowed' => { optional => 1 },
'vlan-protocol' => { optional => 1 },
'vlan-aware' => { optional => 1 },
diff --git a/PVE/Network/VxlanMulticastPlugin.pm b/PVE/Network/VxlanMulticastPlugin.pm
index 87499d8..3aa6e35 100644
--- a/PVE/Network/VxlanMulticastPlugin.pm
+++ b/PVE/Network/VxlanMulticastPlugin.pm
@@ -36,7 +36,7 @@ sub properties {
sub options {
return {
- 'uplink-id' => { fixed => 1 },
+ 'uplink-id' => { optional => 1 },
'multicast-address' => { fixed => 1 },
'vxlan-allowed' => { optional => 1 },
};
--
2.11.0
More information about the pve-devel
mailing list