]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/VlanPlugin.pm
make sdn controller plugin generic
[pve-network.git] / PVE / Network / SDN / VlanPlugin.pm
1 package PVE::Network::SDN::VlanPlugin;
2
3 use strict;
4 use warnings;
5 use PVE::Network::SDN::Plugin;
6
7 use base('PVE::Network::SDN::Plugin');
8
9 sub type {
10 return 'vlan';
11 }
12
13 sub plugindata {
14 return {
15 role => 'transport',
16 };
17 }
18
19 PVE::JSONSchema::register_format('pve-sdn-vlanrange', \&pve_verify_sdn_vlanrange);
20 sub pve_verify_sdn_vlanrange {
21 my ($vlanstr) = @_;
22
23 PVE::Network::SDN::Plugin::parse_tag_number_or_range($vlanstr, '4096');
24
25 return $vlanstr;
26 }
27
28 sub properties {
29 return {
30 'uplink-id' => {
31 type => 'integer',
32 minimum => 1, maximum => 4096,
33 description => 'Uplink interface',
34 },
35 'vlan-allowed' => {
36 type => 'string', format => 'pve-sdn-vlanrange',
37 description => "Allowed vlan range",
38 },
39 'vlan-aware' => {
40 type => 'boolean',
41 description => "enable 802.1q stacked vlan",
42 },
43 'vlan-protocol' => {
44 type => 'string',
45 enum => ['802.1q', '802.1ad'],
46 default => '802.1q',
47 optional => 1,
48 description => "vlan protocol",
49 }
50 };
51 }
52
53 sub options {
54
55 return {
56 'uplink-id' => { optional => 0 },
57 'vlan-allowed' => { optional => 1 },
58 'vlan-protocol' => { optional => 1 },
59 'vlan-aware' => { optional => 1 },
60
61 };
62 }
63
64 # Plugin implementation
65 sub generate_sdn_config {
66 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $uplinks, $config) = @_;
67
68 my $tag = $vnet->{tag};
69 my $mtu = $vnet->{mtu};
70 my $alias = $vnet->{alias};
71 my $vlanaware = $plugin_config->{'vlan-aware'};
72 my $vlanprotocol = $plugin_config->{'vlan-protocol'};
73 my $uplink = $plugin_config->{'uplink-id'};
74 my $vlanallowed = $plugin_config->{'vlan-allowed'};
75
76 die "missing vlan tag" if !$tag;
77
78 my $iface = $uplinks->{$uplink}->{name};
79 $iface = "uplink${uplink}" if !$iface;
80 $iface .= ".$tag";
81
82 #tagged interface
83 my @iface_config = ();
84 push @iface_config, "vlan-protocol $vlanprotocol" if $vlanprotocol;
85 push @iface_config, "mtu $mtu" if $mtu;
86 push(@{$config->{$iface}}, @iface_config) if !$config->{$iface};
87
88 #vnet bridge
89 @iface_config = ();
90 push @iface_config, "bridge_ports $iface";
91 push @iface_config, "bridge_stp off";
92 push @iface_config, "bridge_fd 0";
93 push @iface_config, "bridge-vlan-aware yes" if $vlanaware;
94 push @iface_config, "mtu $mtu" if $mtu;
95 push @iface_config, "alias $alias" if $alias;
96 push(@{$config->{$vnetid}}, @iface_config) if !$config->{$vnetid};
97
98 return $config;
99 }
100
101 sub on_delete_hook {
102 my ($class, $transportid, $sdn_cfg) = @_;
103
104 # verify that no vnet are associated to this transport
105 foreach my $id (keys %{$sdn_cfg->{ids}}) {
106 my $sdn = $sdn_cfg->{ids}->{$id};
107 die "transport $transportid is used by vnet $id"
108 if ($sdn->{type} eq 'vnet' && defined($sdn->{transportzone}) && $sdn->{transportzone} eq $transportid);
109 }
110 }
111
112 sub on_update_hook {
113 my ($class, $transportid, $sdn_cfg) = @_;
114
115 my $transport = $sdn_cfg->{ids}->{$transportid};
116
117 # verify that vlan-allowed don't conflict with another vlan-allowed transport
118
119 # verify that vlan-allowed is matching currently vnet tag in this transport
120 my $vlanallowed = $transport->{'vlan-allowed'};
121 if ($vlanallowed) {
122 foreach my $id (keys %{$sdn_cfg->{ids}}) {
123 my $sdn = $sdn_cfg->{ids}->{$id};
124 if ($sdn->{type} eq 'vnet' && defined($sdn->{tag})) {
125 if(defined($sdn->{transportzone}) && $sdn->{transportzone} eq $transportid) {
126 my $tag = $sdn->{tag};
127 eval {
128 PVE::Network::SDN::Plugin::parse_tag_number_or_range($vlanallowed, '4096', $tag);
129 };
130 if($@) {
131 die "vlan $tag is not allowed in transport $transportid";
132 }
133 }
134 }
135 }
136 }
137 }
138
139 1;
140
141