]>
git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Ipams/PVEPlugin.pm
1 package PVE
::Network
::SDN
::Ipams
::PVEPlugin
;
6 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_register_file cfs_lock_file);
12 use base
('PVE::Network::SDN::Ipams::Plugin');
15 my $ipamdb_file = "priv/ipam.db";
17 PVE
::Cluster
::cfs_register_file
($ipamdb_file,
18 sub { PVE
::Network
::SDN
::Ipams
::PVEPlugin-
>parse_config(@_); },
19 sub { PVE
::Network
::SDN
::Ipams
::PVEPlugin-
>write_config(@_); });
31 # Plugin implementation
34 my ($class, $plugin_config, $subnetid, $subnet) = @_;
36 my $cidr = $subnetid =~ s/-/\//r
;
37 my $gateway = $subnet->{gateway
};
39 cfs_lock_file
($ipamdb_file, undef, sub {
40 my $config = read_db
();
42 if (!defined($config->{subnets
}->{$cidr})) {
43 $config->{subnets
}->{$cidr}->{ips
} = {};
51 my ($class, $plugin_config, $subnetid, $subnet) = @_;
53 my $cidr = $subnetid =~ s/-/\//r
;
55 cfs_lock_file
($ipamdb_file, undef, sub {
58 my $ips = $db->{subnets
}->{$cidr}->{ips
};
59 die "can't delete subnet, not empty" if keys %{$ips} > 0;
60 delete $db->{subnets
}->{$cidr};
68 my ($class, $plugin_config, $subnetid, $ip, $is_gateway) = @_;
70 my $cidr = $subnetid =~ s/-/\//r
;
72 cfs_lock_file
($ipamdb_file, undef, sub {
75 my $s = $db->{subnets
}->{$cidr};
77 die "ip already exist" if defined($s->{ips
}->{$ip});
79 #verify that ip is valid for this subnet
87 my ($class, $plugin_config, $subnetid, $subnet) = @_;
89 my $cidr = $subnetid =~ s/-/\//r
;
92 cfs_lock_file
($ipamdb_file, undef, sub {
95 my $s = $db->{subnets
}->{$cidr};
96 my $iplist = new NetAddr
::IP
($cidr);
97 my $broadcast = $iplist->broadcast();
101 last if $iplist eq $broadcast;
102 my $ip = $iplist->addr();
103 next if defined($s->{ips
}->{$ip});
108 die "can't find free ip in subnet '$cidr'\n" if !$freeip;
110 $s->{ips
}->{$freeip} = 1;
115 my ($network, $mask) = split(/-/, $subnetid);
116 return "$freeip/$mask";
120 my ($class, $plugin_config, $subnetid, $ip) = @_;
122 my $cidr = $subnetid =~ s/-/\//r
;
124 cfs_lock_file
($ipamdb_file, undef, sub {
127 my $s = $db->{subnets
}->{$cidr};
130 die "IP '$ip' does not exist in IPAM DB\n" if !defined($s->{ips
}->{$ip});
131 delete $s->{ips
}->{$ip};
140 my $db = cfs_read_file
($ipamdb_file);
147 my $json = to_json
($cfg);
148 cfs_write_file
($ipamdb_file, $json);
152 my ($class, $filename, $cfg) = @_;
158 my ($class, $filename, $raw) = @_;
160 $raw = '{}' if !defined($raw) ||$raw eq '';
161 my $cfg = from_json
($raw);