]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Zones/QinQPlugin.pm
7b75476a88014a3f73921c54edbf79fc453b2628
[pve-network.git] / PVE / Network / SDN / Zones / QinQPlugin.pm
1 package PVE::Network::SDN::Zones::QinQPlugin;
2
3 use strict;
4 use warnings;
5
6 use PVE::Exception qw(raise raise_param_exc);
7
8 use PVE::Network::SDN::Zones::Plugin;
9
10 use base('PVE::Network::SDN::Zones::Plugin');
11
12 sub type {
13 return 'qinq';
14 }
15
16 sub properties {
17 return {
18 tag => {
19 type => 'integer',
20 minimum => 0,
21 description => "Service-VLAN Tag",
22 },
23 mtu => {
24 type => 'integer',
25 description => "MTU",
26 optional => 1,
27 },
28 'vlan-protocol' => {
29 type => 'string',
30 enum => ['802.1q', '802.1ad'],
31 default => '802.1q',
32 optional => 1,
33 }
34 };
35 }
36
37 sub options {
38 return {
39 nodes => { optional => 1},
40 'tag' => { optional => 0 },
41 'bridge' => { optional => 0 },
42 'mtu' => { optional => 1 },
43 'vlan-protocol' => { optional => 1 },
44 dns => { optional => 1 },
45 reversedns => { optional => 1 },
46 dnszone => { optional => 1 },
47 ipam => { optional => 1 },
48 };
49 }
50
51 # Plugin implementation
52 sub generate_sdn_config {
53 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $controller_cfg, $subnet_cfg, $interfaces_config, $config) = @_;
54
55 my ($bridge, $mtu, $stag) = $plugin_config->@{'bridge', 'mtu', 'tag'};
56 my $vlanprotocol = $plugin_config->{'vlan-protocol'};
57
58 PVE::Network::SDN::Zones::Plugin::find_bridge($bridge);
59
60 my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge);
61 my $is_ovs = PVE::Network::SDN::Zones::Plugin::is_ovs($bridge);
62
63 my @iface_config = ();
64 my $zone_notag_uplink = "ln_${zoneid}";
65 my $zone_notag_uplinkpeer = "pr_${zoneid}";
66 my $zone = "z_${zoneid}";
67
68 my $vnet_bridge_ports = "";
69 if (my $ctag = $vnet->{tag}) {
70 $vnet_bridge_ports = "$zone.$ctag";
71 } else {
72 $vnet_bridge_ports = $zone_notag_uplinkpeer;
73 }
74
75 my $zone_bridge_ports = "";
76 if ($is_ovs) {
77 # ovs--->ovsintport(dot1q-tunnel tag)------->vlanawarebrige-----(tag)--->vnet
78
79 $vlanprotocol = "802.1q" if !$vlanprotocol;
80 my $svlan_iface = "sv_".$zoneid;
81
82 # ovs dot1q-tunnel port
83 @iface_config = ();
84 push @iface_config, "ovs_type OVSIntPort";
85 push @iface_config, "ovs_bridge $bridge";
86 push @iface_config, "ovs_mtu $mtu" if $mtu;
87 push @iface_config, "ovs_options vlan_mode=dot1q-tunnel tag=$stag other_config:qinq-ethtype=$vlanprotocol";
88 push(@{$config->{$svlan_iface}}, @iface_config) if !$config->{$svlan_iface};
89
90 # redefine main ovs bridge, ifupdown2 will merge ovs_ports
91 @{$config->{$bridge}}[0] = "ovs_ports" if !@{$config->{$bridge}}[0];
92 my @ovs_ports = split / / , @{$config->{$bridge}}[0];
93 @{$config->{$bridge}}[0] .= " $svlan_iface" if !grep( $_ eq $svlan_iface, @ovs_ports );
94
95 $zone_bridge_ports = $svlan_iface;
96
97 } elsif ($vlan_aware) {
98 # VLAN_aware_brige-(tag)----->vlanwarebridge-(tag)----->vnet
99
100 if ($vlanprotocol) {
101 @iface_config = ();
102 push @iface_config, "bridge-vlan-protocol $vlanprotocol";
103 push(@{$config->{$bridge}}, @iface_config) if !$config->{$bridge};
104 }
105
106 $zone_bridge_ports = "$bridge.$stag";
107
108 } else {
109 # eth--->eth.x(svlan)----->vlanwarebridge-(tag)----->vnet---->vnet
110
111 my @bridge_ifaces = PVE::Network::SDN::Zones::Plugin::get_bridge_ifaces($bridge);
112
113 for my $bridge_iface (@bridge_ifaces) {
114 # use named vlan interface to avoid too long names
115 my $svlan_iface = "sv_$zoneid";
116
117 # svlan
118 @iface_config = ();
119 push @iface_config, "vlan-raw-device $bridge_iface";
120 push @iface_config, "vlan-id $stag";
121 push @iface_config, "vlan-protocol $vlanprotocol" if $vlanprotocol;
122 push(@{$config->{$svlan_iface}}, @iface_config) if !$config->{$svlan_iface};
123
124 $zone_bridge_ports = $svlan_iface;
125 last;
126 }
127 }
128
129 # veth peer for notag vnet
130 @iface_config = ();
131 push @iface_config, "link-type veth";
132 push @iface_config, "veth-peer-name $zone_notag_uplinkpeer";
133 push(@{$config->{$zone_notag_uplink}}, @iface_config) if !$config->{$zone_notag_uplink};
134
135 @iface_config = ();
136 push @iface_config, "link-type veth";
137 push @iface_config, "veth-peer-name $zone_notag_uplink";
138 push(@{$config->{$zone_notag_uplinkpeer}}, @iface_config) if !$config->{$zone_notag_uplinkpeer};
139
140 # zone vlan aware bridge
141 @iface_config = ();
142 push @iface_config, "mtu $mtu" if $mtu;
143 push @iface_config, "bridge-stp off";
144 push @iface_config, "bridge-ports $zone_bridge_ports $zone_notag_uplink";
145 push @iface_config, "bridge-fd 0";
146 push @iface_config, "bridge-vlan-aware yes";
147 push @iface_config, "bridge-vids 2-4094";
148 push(@{$config->{$zone}}, @iface_config) if !$config->{$zone};
149
150 # vnet bridge
151 @iface_config = ();
152 push @iface_config, "bridge_ports $vnet_bridge_ports";
153 push @iface_config, "bridge_stp off";
154 push @iface_config, "bridge_fd 0";
155 if($vnet->{vlanaware}) {
156 push @iface_config, "bridge-vlan-aware yes";
157 push @iface_config, "bridge-vids 2-4094";
158 }
159 push @iface_config, "mtu $mtu" if $mtu;
160 push @iface_config, "alias $vnet->{alias}" if $vnet->{alias};
161 push(@{$config->{$vnetid}}, @iface_config) if !$config->{$vnetid};
162 }
163
164 sub status {
165 my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_;
166
167 my $bridge = $plugin_config->{bridge};
168 my $err_msg = [];
169
170 if (!-d "/sys/class/net/$bridge") {
171 push @$err_msg, "missing $bridge";
172 return $err_msg;
173 }
174
175 my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge);
176
177 my $tag = $vnet->{tag};
178 my $vnet_uplink = "ln_".$vnetid;
179 my $vnet_uplinkpeer = "pr_".$vnetid;
180 my $zone_notag_uplink = "ln_".$zone;
181 my $zone_notag_uplinkpeer = "pr_".$zone;
182 my $zonebridge = "z_$zone";
183
184 # ifaces to check
185 my $ifaces = [ $vnetid, $bridge ];
186
187 push @$ifaces, $zonebridge;
188 push @$ifaces, $zone_notag_uplink;
189 push @$ifaces, $zone_notag_uplinkpeer;
190
191 if (!$vlan_aware) {
192 my $svlan_iface = "sv_$zone";
193 push @$ifaces, $svlan_iface;
194 }
195
196 foreach my $iface (@{$ifaces}) {
197 if (!$status->{$iface}->{status}) {
198 push @$err_msg, "missing $iface";
199 } elsif ($status->{$iface}->{status} ne 'pass') {
200 push @$err_msg, "error $iface";
201 }
202 }
203 return $err_msg;
204 }
205
206 sub vnet_update_hook {
207 my ($class, $vnet_cfg, $vnetid, $zone_cfg) = @_;
208
209 my $vnet = $vnet_cfg->{ids}->{$vnetid};
210
211 my $tag = $vnet->{tag};
212 raise_param_exc({ tag => "VLAN tag maximal value is 4096" }) if $tag && $tag > 4096;
213
214 # verify that tag is not already defined in another vnet on same zone
215 for my $id (sort keys %{$vnet_cfg->{ids}}) {
216 next if $id eq $vnetid;
217 my $other_vnet = $vnet_cfg->{ids}->{$id};
218 next if $vnet->{zone} ne $other_vnet->{zone};
219 my $other_tag = $other_vnet->{tag};
220 if ($tag) {
221 raise_param_exc({ tag => "tag $tag already exist in zone $vnet->{zone} vnet $id"})
222 if $other_tag && $tag eq $other_tag;
223 } else {
224 raise_param_exc({ tag => "tag-less vnet already exists in zone $vnet->{zone} vnet $id"})
225 if !$other_tag;
226 }
227 }
228 }
229
230 1;
231
232