summaryrefslogtreecommitdiff
path: root/src/PVE/Network/SDN/Vnets.pm
blob: 4e795f23eb27d05e3b58d57d411534acea9a10ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package PVE::Network::SDN::Vnets;

use strict;
use warnings;

use Net::IP;

use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
use PVE::Network::SDN;
use PVE::Network::SDN::Dhcp;
use PVE::Network::SDN::Subnets;
use PVE::Network::SDN::Zones;

use PVE::Network::SDN::VnetPlugin;
PVE::Network::SDN::VnetPlugin->register();
PVE::Network::SDN::VnetPlugin->init();

sub sdn_vnets_config {
    my ($cfg, $id, $noerr) = @_;

    die "no sdn vnet ID specified\n" if !$id;

    my $scfg = $cfg->{ids}->{$id};
    die "sdn vnet '$id' does not exist\n" if (!$noerr && !$scfg);

    return $scfg;
}

sub config {
    my ($running) = @_;

    if ($running) {
	my $cfg = PVE::Network::SDN::running_config();
	return $cfg->{vnets};
    }

    return cfs_read_file("sdn/vnets.cfg");
}

sub write_config {
    my ($cfg) = @_;

    cfs_write_file("sdn/vnets.cfg", $cfg);
}

sub sdn_vnets_ids {
    my ($cfg) = @_;

    return sort keys %{$cfg->{ids}};
}

sub complete_sdn_vnet {
    my ($cmdname, $pname, $cvalue) = @_;

    my $cfg = PVE::Network::SDN::Vnets::config();

    return  $cmdname eq 'add' ? [] : [ PVE::Network::SDN::Vnets::sdn_vnet_ids($cfg) ];
}

sub get_vnet {
    my ($vnetid, $running) = @_;

    return if !$vnetid;

    my $cfg = PVE::Network::SDN::Vnets::config($running);
    return PVE::Network::SDN::Vnets::sdn_vnets_config($cfg, $vnetid, 1);
}

sub get_subnets {
    my ($vnetid, $running) = @_;

    my $subnets = undef;
    my $subnets_cfg = PVE::Network::SDN::Subnets::config($running);

    foreach my $subnetid (sort keys %{$subnets_cfg->{ids}}) {
	my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($subnets_cfg, $subnetid);
	next if !$subnet->{vnet} || ($vnetid && $subnet->{vnet} ne $vnetid);
	$subnets->{$subnetid} = $subnet;
    }

    return $subnets;
}

sub get_subnet_from_vnet_ip {
    my ($vnetid, $ip) = @_;

    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
    my $zoneid = $vnet->{zone};
    my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);

    my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $subnets);

    return ($zone, $subnetid, $subnet, $ip);
}

sub add_next_free_cidr {
    my ($vnetid, $hostname, $mac, $vmid, $skipdns, $dhcprange, $ipversion) = @_;

    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
    return if !$vnet;

    my $zoneid = $vnet->{zone};
    my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);

    return if !$zone->{ipam} || !$zone->{dhcp};

    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);

    my $ips = {};

    my @ipversions = defined($ipversion) ? ($ipversion) : qw/ 4 6 /;
    for my $ipversion (@ipversions) {
	my $ip = undef;
	my $subnetcount = 0;
	foreach my $subnetid (sort keys %{$subnets}) {
	    my $subnet = $subnets->{$subnetid};
	    my $network = $subnet->{network};

	    next if Net::IP::ip_get_version($network) != $ipversion || $ips->{$ipversion};
	    $subnetcount++;

	    eval {
		$ip = PVE::Network::SDN::Subnets::add_next_free_ip($zone, $subnetid, $subnet, $hostname, $mac, $vmid, $skipdns, $subnet->{'dhcp-range'});
	    };
	    die $@ if $@;

	    if ($ip) {
		$ips->{$ipversion} = $ip;
		last;
	    }
	}

	if (!$ip && $subnetcount > 0) {
	    foreach my $version (sort keys %{$ips}) {
		my $ip = $ips->{$version};
		my ($subnetid, $subnet) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $subnets);

		PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $skipdns);
	    }

	    die "can't find any free ip in zone $zoneid for IPv$ipversion";
	}
    }
}

sub add_ip {
    my ($vnetid, $ip, $hostname, $mac, $vmid, $skipdns) = @_;

    return if !$vnetid;
    
    my ($zone, $subnetid, $subnet) = PVE::Network::SDN::Vnets::get_subnet_from_vnet_ip($vnetid, $ip);
    PVE::Network::SDN::Subnets::add_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $vmid, undef, $skipdns);
}

sub update_ip {
    my ($vnetid, $ip, $hostname, $oldhostname, $mac, $vmid, $skipdns) = @_;

    return if !$vnetid;

    my ($zone, $subnetid, $subnet) = PVE::Network::SDN::Vnets::get_subnet_from_vnet_ip($vnetid, $ip);
    PVE::Network::SDN::Subnets::update_ip($zone, $subnetid, $subnet, $ip, $hostname, $oldhostname, $mac, $vmid, $skipdns);
}

sub del_ip {
    my ($vnetid, $ip, $hostname, $mac, $skipdns) = @_;

    return if !$vnetid;

    my ($zone, $subnetid, $subnet) = PVE::Network::SDN::Vnets::get_subnet_from_vnet_ip($vnetid, $ip);
    PVE::Network::SDN::Subnets::del_ip($zone, $subnetid, $subnet, $ip, $hostname, $mac, $skipdns);
}

sub get_ips_from_mac {
    my ($vnetid, $mac) = @_;

    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
    return if !$vnet;

    my $zoneid = $vnet->{zone};
    my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);

    return if !$zone->{ipam} || !$zone->{dhcp};

    return PVE::Network::SDN::Ipams::get_ips_from_mac($mac, $zoneid, $zone);
}

sub del_ips_from_mac {
    my ($vnetid, $mac, $hostname) = @_;

    my ($ip4, $ip6) = PVE::Network::SDN::Vnets::get_ips_from_mac($vnetid, $mac);
    PVE::Network::SDN::Vnets::del_ip($vnetid, $ip4, $hostname, $mac) if $ip4;
    PVE::Network::SDN::Vnets::del_ip($vnetid, $ip6, $hostname, $mac) if $ip6;

    return ($ip4, $ip6);
}

sub add_dhcp_mapping {
    my ($vnetid, $mac, $vmid, $name) = @_;

    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
    return if !$vnet;
    my $zoneid = $vnet->{zone};
    my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);

    return if !$zone->{ipam} || !$zone->{dhcp};

    my ($ip4, $ip6) = PVE::Network::SDN::Vnets::get_ips_from_mac($vnetid, $mac);
    add_next_free_cidr($vnetid, $name, $mac, "$vmid", undef, 1, 4) if ! $ip4;
    add_next_free_cidr($vnetid, $name, $mac, "$vmid", undef, 1, 6) if ! $ip6;

    ($ip4, $ip6) = PVE::Network::SDN::Vnets::get_ips_from_mac($vnetid, $mac);
    PVE::Network::SDN::Dhcp::add_mapping($vnetid, $mac, $ip4, $ip6) if $ip4 || $ip6;
}

1;