]>
Commit | Line | Data |
---|---|---|
f5eabba0 | 1 | package PVE::Network::SDN::Zones::VlanPlugin; |
f8140d53 AD |
2 | |
3 | use strict; | |
4 | use warnings; | |
f5eabba0 | 5 | use PVE::Network::SDN::Zones::Plugin; |
1d44ce70 | 6 | use PVE::Exception qw(raise raise_param_exc); |
f8140d53 | 7 | |
f5eabba0 | 8 | use base('PVE::Network::SDN::Zones::Plugin'); |
f8140d53 AD |
9 | |
10 | sub type { | |
11 | return 'vlan'; | |
12 | } | |
13 | ||
6bffe819 AD |
14 | PVE::JSONSchema::register_format('pve-sdn-vlanrange', \&pve_verify_sdn_vlanrange); |
15 | sub pve_verify_sdn_vlanrange { | |
f8140d53 AD |
16 | my ($vlanstr) = @_; |
17 | ||
f5eabba0 | 18 | PVE::Network::SDN::Zones::Plugin::parse_tag_number_or_range($vlanstr, '4096'); |
f8140d53 AD |
19 | |
20 | return $vlanstr; | |
21 | } | |
22 | ||
23 | sub properties { | |
24 | return { | |
938ebef7 AD |
25 | 'bridge' => { |
26 | type => 'string', | |
eec580bf | 27 | }, |
f8140d53 AD |
28 | }; |
29 | } | |
30 | ||
31 | sub options { | |
32 | ||
33 | return { | |
65cb893e | 34 | nodes => { optional => 1}, |
938ebef7 | 35 | 'bridge' => { optional => 0 }, |
4ad78442 AD |
36 | mtu => { optional => 1 }, |
37 | dns => { optional => 1 }, | |
38 | reversedns => { optional => 1 }, | |
39 | dnszone => { optional => 1 }, | |
57a335c4 | 40 | ipam => { optional => 1 }, |
f8140d53 AD |
41 | }; |
42 | } | |
43 | ||
44 | # Plugin implementation | |
6bffe819 | 45 | sub generate_sdn_config { |
efffa0ff | 46 | my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $controller_cfg, $subnet_cfg, $interfaces_config, $config) = @_; |
7d1035b2 AD |
47 | |
48 | my $bridge = $plugin_config->{bridge}; | |
fdf22d5f | 49 | PVE::Network::SDN::Zones::Plugin::find_bridge($bridge); |
ae3f4de8 | 50 | |
fdf22d5f AD |
51 | my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge); |
52 | my $is_ovs = PVE::Network::SDN::Zones::Plugin::is_ovs($bridge); | |
7d1035b2 AD |
53 | |
54 | my $tag = $vnet->{tag}; | |
55 | my $alias = $vnet->{alias}; | |
c559eac1 | 56 | my $mtu = $plugin_config->{mtu}; |
7d1035b2 | 57 | |
0be2f7ec AD |
58 | my $vnet_uplink = "ln_".$vnetid; |
59 | my $vnet_uplinkpeer = "pr_".$vnetid; | |
7d1035b2 | 60 | |
0be2f7ec AD |
61 | my @iface_config = (); |
62 | ||
63 | if($is_ovs) { | |
64 | ||
65 | # keep vmbrXvY for compatibility with existing network | |
66 | # eth0----ovs vmbr0--(ovsintport tag)---->vnet---->vm | |
67 | ||
68 | @iface_config = (); | |
69 | push @iface_config, "ovs_type OVSIntPort"; | |
70 | push @iface_config, "ovs_bridge $bridge"; | |
82159388 | 71 | push @iface_config, "ovs_mtu $mtu" if $mtu; |
607db889 | 72 | if($vnet->{vlanaware}) { |
f2239836 | 73 | push @iface_config, "ovs_options vlan_mode=dot1q-tunnel other_config:qinq-ethtype=802.1q tag=$tag"; |
607db889 AD |
74 | } else { |
75 | push @iface_config, "ovs_options tag=$tag"; | |
76 | } | |
0be2f7ec AD |
77 | push(@{$config->{$vnet_uplink}}, @iface_config) if !$config->{$vnet_uplink}; |
78 | ||
880f6d61 AD |
79 | #redefine main ovs bridge, ifupdown2 will merge ovs_ports |
80 | @iface_config = (); | |
81 | push @iface_config, "ovs_ports $vnet_uplink"; | |
82 | push(@{$config->{$bridge}}, @iface_config); | |
83 | ||
0be2f7ec AD |
84 | } elsif ($vlan_aware) { |
85 | # eth0----vlanaware bridge vmbr0--(vmbr0.X tag)---->vnet---->vm | |
0f700635 | 86 | $vnet_uplink = "$bridge.$tag"; |
0be2f7ec AD |
87 | } else { |
88 | ||
89 | # keep vmbrXvY for compatibility with existing network | |
90 | # eth0<---->eth0.X----vmbr0v10------vnet---->vm | |
91 | ||
92 | my $bridgevlan = $bridge."v".$tag; | |
93 | ||
fdf22d5f | 94 | my @bridge_ifaces = PVE::Network::SDN::Zones::Plugin::get_bridge_ifaces($bridge); |
0be2f7ec AD |
95 | |
96 | my $bridge_ports = ""; | |
97 | foreach my $bridge_iface (@bridge_ifaces) { | |
98 | $bridge_ports .= " $bridge_iface.$tag"; | |
99 | } | |
100 | ||
101 | @iface_config = (); | |
102 | push @iface_config, "link-type veth"; | |
103 | push @iface_config, "veth-peer-name $vnet_uplinkpeer"; | |
104 | push(@{$config->{$vnet_uplink}}, @iface_config) if !$config->{$vnet_uplink}; | |
105 | ||
106 | @iface_config = (); | |
107 | push @iface_config, "link-type veth"; | |
108 | push @iface_config, "veth-peer-name $vnet_uplink"; | |
109 | push(@{$config->{$vnet_uplinkpeer}}, @iface_config) if !$config->{$vnet_uplinkpeer}; | |
110 | ||
111 | @iface_config = (); | |
112 | push @iface_config, "bridge_ports $bridge_ports $vnet_uplinkpeer"; | |
113 | push @iface_config, "bridge_stp off"; | |
114 | push @iface_config, "bridge_fd 0"; | |
115 | push(@{$config->{$bridgevlan}}, @iface_config) if !$config->{$bridgevlan}; | |
7d1035b2 AD |
116 | } |
117 | ||
0be2f7ec AD |
118 | #vnet bridge |
119 | @iface_config = (); | |
120 | push @iface_config, "bridge_ports $vnet_uplink"; | |
7d1035b2 AD |
121 | push @iface_config, "bridge_stp off"; |
122 | push @iface_config, "bridge_fd 0"; | |
912fb443 AD |
123 | if($vnet->{vlanaware}) { |
124 | push @iface_config, "bridge-vlan-aware yes"; | |
125 | push @iface_config, "bridge-vids 2-4094"; | |
126 | } | |
7d1035b2 AD |
127 | push @iface_config, "mtu $mtu" if $mtu; |
128 | push @iface_config, "alias $alias" if $alias; | |
0be2f7ec | 129 | push(@{$config->{$vnetid}}, @iface_config) if !$config->{$vnetid}; |
7d1035b2 AD |
130 | |
131 | return $config; | |
f8140d53 AD |
132 | } |
133 | ||
6ed053c1 | 134 | sub status { |
4d7cc94f | 135 | my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_; |
6ed053c1 AD |
136 | |
137 | my $bridge = $plugin_config->{bridge}; | |
4d7cc94f AD |
138 | |
139 | my $err_msg = []; | |
140 | if (!-d "/sys/class/net/$bridge") { | |
141 | push @$err_msg, "missing $bridge"; | |
142 | return $err_msg; | |
6ed053c1 | 143 | } |
4d7cc94f | 144 | |
fdf22d5f AD |
145 | my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge); |
146 | my $is_ovs = PVE::Network::SDN::Zones::Plugin::is_ovs($bridge); | |
4d7cc94f AD |
147 | |
148 | my $tag = $vnet->{tag}; | |
149 | my $vnet_uplink = "ln_".$vnetid; | |
150 | my $vnet_uplinkpeer = "pr_".$vnetid; | |
151 | ||
152 | # ifaces to check | |
153 | my $ifaces = [ $vnetid, $bridge ]; | |
154 | if($is_ovs) { | |
155 | push @$ifaces, $vnet_uplink; | |
156 | } elsif (!$vlan_aware) { | |
157 | my $bridgevlan = $bridge."v".$tag; | |
158 | push @$ifaces, $bridgevlan; | |
159 | push @$ifaces, $vnet_uplink; | |
160 | push @$ifaces, $vnet_uplinkpeer; | |
161 | } | |
162 | ||
163 | foreach my $iface (@{$ifaces}) { | |
164 | if (!$status->{$iface}->{status}) { | |
165 | push @$err_msg, "missing $iface"; | |
166 | } elsif ($status->{$iface}->{status} ne 'pass') { | |
167 | push @$err_msg, "error iface $iface"; | |
168 | } | |
0f700635 | 169 | } |
4d7cc94f | 170 | return $err_msg; |
6ed053c1 AD |
171 | } |
172 | ||
5ca07ed9 | 173 | sub vnet_update_hook { |
88d9562b AD |
174 | my ($class, $vnet_cfg, $vnetid, $zone_cfg) = @_; |
175 | ||
176 | my $vnet = $vnet_cfg->{ids}->{$vnetid}; | |
177 | my $tag = $vnet->{tag}; | |
1d44ce70 | 178 | |
5ca07ed9 AD |
179 | raise_param_exc({ tag => "missing vlan tag"}) if !defined($vnet->{tag}); |
180 | raise_param_exc({ tag => "vlan tag max value is 4096"}) if $vnet->{tag} > 4096; | |
88d9562b AD |
181 | |
182 | # verify that tag is not already defined in another vnet on same zone | |
183 | foreach my $id (keys %{$vnet_cfg->{ids}}) { | |
184 | next if $id eq $vnetid; | |
185 | my $othervnet = $vnet_cfg->{ids}->{$id}; | |
186 | my $other_tag = $othervnet->{tag}; | |
187 | next if $vnet->{zone} ne $othervnet->{zone}; | |
188 | raise_param_exc({ tag => "tag $tag already exist in vnet $id"}) if $other_tag && $tag eq $other_tag; | |
189 | } | |
1d44ce70 AD |
190 | } |
191 | ||
f8140d53 AD |
192 | 1; |
193 | ||
194 |