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