]>
Commit | Line | Data |
---|---|---|
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} ? $uplinks->{$uplink} : "uplink$uplink"; | |
73 | $iface .= ".$tag"; | |
74 | ||
75 | my $config = "\n"; | |
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"; | |
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"; | |
86 | $config .= " bridge-vlan-aware yes \n" if $vlanaware; | |
87 | $config .= " mtu $mtu\n" if $mtu; | |
88 | $config .= " alias $alias\n" if $alias; | |
89 | ||
90 | return $config; | |
91 | } | |
92 | ||
93 | sub on_delete_hook { | |
94 | my ($class, $transportid, $network_cfg) = @_; | |
95 | ||
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 | } | |
102 | } | |
103 | ||
104 | sub on_update_hook { | |
105 | my ($class, $transportid, $network_cfg) = @_; | |
106 | ||
107 | my $transport = $network_cfg->{ids}->{$transportid}; | |
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 | |
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 | } | |
129 | } | |
130 | ||
131 | 1; | |
132 | ||
133 |