]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Zones/VlanPlugin.pm
93a60a0301bd7ee51acd0a8bcf41222689375583
[pve-network.git] / PVE / Network / SDN / Zones / VlanPlugin.pm
1 package PVE::Network::SDN::Zones::VlanPlugin;
2
3 use strict;
4 use warnings;
5 use PVE::Network::SDN::Zones::Plugin;
6 use PVE::Exception qw(raise raise_param_exc);
7
8 use base('PVE::Network::SDN::Zones::Plugin');
9
10 sub type {
11 return 'vlan';
12 }
13
14 PVE::JSONSchema::register_format('pve-sdn-vlanrange', \&pve_verify_sdn_vlanrange);
15 sub pve_verify_sdn_vlanrange {
16 my ($vlanstr) = @_;
17
18 PVE::Network::SDN::Zones::Plugin::parse_tag_number_or_range($vlanstr, '4096');
19
20 return $vlanstr;
21 }
22
23 sub properties {
24 return {
25 'bridge' => {
26 type => 'string',
27 },
28 };
29 }
30
31 sub options {
32
33 return {
34 nodes => { optional => 1},
35 'bridge' => { optional => 0 },
36 mtu => { optional => 1 },
37 dns => { optional => 1 },
38 reversedns => { optional => 1 },
39 dnszone => { optional => 1 },
40 ipam => { optional => 0 },
41 };
42 }
43
44 # Plugin implementation
45 sub generate_sdn_config {
46 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $controller_cfg, $subnet_cfg, $interfaces_config, $config) = @_;
47
48 my $bridge = $plugin_config->{bridge};
49 PVE::Network::SDN::Zones::Plugin::find_bridge($bridge);
50
51 my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge);
52 my $is_ovs = PVE::Network::SDN::Zones::Plugin::is_ovs($bridge);
53
54 my $tag = $vnet->{tag};
55 my $alias = $vnet->{alias};
56 my $mtu = $plugin_config->{mtu};
57
58 my $vnet_uplink = "ln_".$vnetid;
59 my $vnet_uplinkpeer = "pr_".$vnetid;
60
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";
71 push @iface_config, "ovs_mtu $mtu" if $mtu;
72 if($vnet->{vlanaware}) {
73 push @iface_config, "ovs_options vlan_mode=dot1q-tunnel other_config:qinq-ethtype=802.1q tag=$tag";
74 } else {
75 push @iface_config, "ovs_options tag=$tag";
76 }
77 push(@{$config->{$vnet_uplink}}, @iface_config) if !$config->{$vnet_uplink};
78
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
84 } elsif ($vlan_aware) {
85 # eth0----vlanaware bridge vmbr0--(vmbr0.X tag)---->vnet---->vm
86 $vnet_uplink = "$bridge.$tag";
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
94 my @bridge_ifaces = PVE::Network::SDN::Zones::Plugin::get_bridge_ifaces($bridge);
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};
116 }
117
118 #vnet bridge
119 @iface_config = ();
120 push @iface_config, "bridge_ports $vnet_uplink";
121 push @iface_config, "bridge_stp off";
122 push @iface_config, "bridge_fd 0";
123 if($vnet->{vlanaware}) {
124 push @iface_config, "bridge-vlan-aware yes";
125 push @iface_config, "bridge-vids 2-4094";
126 }
127 push @iface_config, "mtu $mtu" if $mtu;
128 push @iface_config, "alias $alias" if $alias;
129 push(@{$config->{$vnetid}}, @iface_config) if !$config->{$vnetid};
130
131 return $config;
132 }
133
134 sub status {
135 my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_;
136
137 my $bridge = $plugin_config->{bridge};
138
139 my $err_msg = [];
140 if (!-d "/sys/class/net/$bridge") {
141 push @$err_msg, "missing $bridge";
142 return $err_msg;
143 }
144
145 my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge);
146 my $is_ovs = PVE::Network::SDN::Zones::Plugin::is_ovs($bridge);
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 }
169 }
170 return $err_msg;
171 }
172
173 sub vnet_update_hook {
174 my ($class, $vnet_cfg, $vnetid, $zone_cfg) = @_;
175
176 my $vnet = $vnet_cfg->{ids}->{$vnetid};
177 my $tag = $vnet->{tag};
178
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;
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 }
190 }
191
192 1;
193
194