]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/Network/VlanPlugin.pm
063ae4996ebc971621def57af511e6b343b25801
[pve-network.git] / PVE / Network / Network / VlanPlugin.pm
1 package PVE::Network::Network::VlanPlugin;
2
3 use strict;
4 use warnings;
5 use PVE::Network::Network::Plugin;
6
7 use base('PVE::Network::Network::Plugin');
8
9 sub type {
10 return 'vlan';
11 }
12
13 PVE::JSONSchema::register_format('pve-network-vlanrange', \&pve_verify_network_vlanrange);
14 sub pve_verify_network_vlanrange {
15 my ($vlanstr) = @_;
16
17 PVE::Network::Network::Plugin::parse_tag_number_or_range($vlanstr, '4096');
18
19 return $vlanstr;
20 }
21
22 sub properties {
23 return {
24 'uplink-id' => {
25 type => 'integer',
26 minimum => 1, maximum => 4096,
27 description => 'Uplink interface',
28 },
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
47 sub options {
48
49 return {
50 'uplink-id' => { optional => 0 },
51 'vlan-allowed' => { optional => 1 },
52 'vlan-protocol' => { optional => 1 },
53 'vlan-aware' => { optional => 1 },
54
55 };
56 }
57
58 # Plugin implementation
59 sub generate_network_config {
60 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $uplinks) = @_;
61
62 my $tag = $vnet->{tag};
63 my $mtu = $vnet->{mtu};
64 my $alias = $vnet->{alias};
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;
71
72 my $iface = $uplinks->{$uplink}->{name} ? $uplinks->{$uplink}->{name} : "uplink$uplink";
73 $iface .= ".$tag";
74 my $config = "\n";
75 $config .= "auto $iface\n";
76 $config .= "iface $iface inet manual\n";
77 $config .= " vlan-protocol $vlanprotocol\n" if $vlanprotocol;
78 $config .= " mtu $mtu\n" if $mtu;
79 $config .= "\n";
80 $config .= "auto $vnetid\n";
81 $config .= "iface $vnetid inet manual\n";
82 $config .= " bridge_ports $iface\n";
83 $config .= " bridge_stp off\n";
84 $config .= " bridge_fd 0\n";
85 $config .= " bridge-vlan-aware yes \n" if $vlanaware;
86 $config .= " mtu $mtu\n" if $mtu;
87 $config .= " alias $alias\n" if $alias;
88
89 return $config;
90 }
91
92 sub on_delete_hook {
93 my ($class, $transportid, $network_cfg) = @_;
94
95 # verify that no vnet are associated to this transport
96 foreach my $id (keys %{$network_cfg->{ids}}) {
97 my $network = $network_cfg->{ids}->{$id};
98 die "transport $transportid is used by vnet $id"
99 if ($network->{type} eq 'vnet' && defined($network->{transportzone}) && $network->{transportzone} eq $transportid);
100 }
101 }
102
103 sub on_update_hook {
104 my ($class, $transportid, $network_cfg) = @_;
105
106 my $transport = $network_cfg->{ids}->{$transportid};
107
108 # verify that vlan-allowed don't conflict with another vlan-allowed transport
109
110 # verify that vlan-allowed is matching currently vnet tag in this transport
111 my $vlanallowed = $transport->{'vlan-allowed'};
112 if ($vlanallowed) {
113 foreach my $id (keys %{$network_cfg->{ids}}) {
114 my $network = $network_cfg->{ids}->{$id};
115 if ($network->{type} eq 'vnet' && defined($network->{tag})) {
116 if(defined($network->{transportzone}) && $network->{transportzone} eq $transportid) {
117 my $tag = $network->{tag};
118 eval {
119 PVE::Network::Network::Plugin::parse_tag_number_or_range($vlanallowed, '4096', $tag);
120 };
121 if($@) {
122 die "vlan $tag is not allowed in transport $transportid";
123 }
124 }
125 }
126 }
127 }
128 }
129
130 1;
131
132