]>
Commit | Line | Data |
---|---|---|
c33dd818 AD |
1 | package PVE::Network::SDN::Subnets; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
70b03506 | 6 | use Net::Subnet qw(subnet_matcher); |
c33dd818 AD |
7 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); |
8 | ||
70b03506 | 9 | use PVE::Network::SDN::Ipams; |
c33dd818 AD |
10 | use PVE::Network::SDN::SubnetPlugin; |
11 | PVE::Network::SDN::SubnetPlugin->register(); | |
12 | PVE::Network::SDN::SubnetPlugin->init(); | |
13 | ||
14 | sub sdn_subnets_config { | |
15 | my ($cfg, $id, $noerr) = @_; | |
16 | ||
17 | die "no sdn subnet ID specified\n" if !$id; | |
18 | ||
19 | my $scfg = $cfg->{ids}->{$id}; | |
20 | die "sdn subnet '$id' does not exist\n" if (!$noerr && !$scfg); | |
21 | ||
22 | return $scfg; | |
23 | } | |
24 | ||
25 | sub config { | |
26 | my $config = cfs_read_file("sdn/subnets.cfg"); | |
27 | } | |
28 | ||
29 | sub write_config { | |
30 | my ($cfg) = @_; | |
31 | ||
32 | cfs_write_file("sdn/subnets.cfg", $cfg); | |
33 | } | |
34 | ||
35 | sub sdn_subnets_ids { | |
36 | my ($cfg) = @_; | |
37 | ||
38 | return keys %{$cfg->{ids}}; | |
39 | } | |
40 | ||
41 | sub complete_sdn_subnet { | |
42 | my ($cmdname, $pname, $cvalue) = @_; | |
43 | ||
44 | my $cfg = PVE::Network::SDN::Subnets::config(); | |
45 | ||
46 | return $cmdname eq 'add' ? [] : [ PVE::Network::SDN::Subnets::sdn_subnets_ids($cfg) ]; | |
47 | } | |
48 | ||
49 | sub get_subnet { | |
50 | my ($subnetid) = @_; | |
51 | ||
52 | my $cfg = PVE::Network::SDN::Subnets::config(); | |
53 | my $subnet = PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $subnetid, 1); | |
54 | return $subnet; | |
55 | } | |
56 | ||
70b03506 AD |
57 | sub find_ip_subnet { |
58 | my ($ip, $subnetslist) = @_; | |
59 | ||
60 | my $subnets_cfg = PVE::Network::SDN::Subnets::config(); | |
61 | my @subnets = PVE::Tools::split_list($subnetslist) if $subnetslist; | |
62 | ||
63 | my $subnet = undef; | |
64 | my $subnetid = undef; | |
65 | ||
66 | foreach my $s (@subnets) { | |
67 | my $subnet_matcher = subnet_matcher($s); | |
68 | next if !$subnet_matcher->($ip); | |
69 | $subnetid = $s =~ s/\//-/r; | |
70 | $subnet = $subnets_cfg->{ids}->{$subnetid}; | |
71 | last; | |
72 | } | |
73 | die "can't find any subnet for ip $ip" if !$subnet; | |
74 | ||
75 | return ($subnetid, $subnet); | |
76 | } | |
77 | ||
78 | sub next_free_ip { | |
79 | my ($subnetid, $subnet) = @_; | |
80 | ||
81 | my $ipamid = $subnet->{ipam}; | |
82 | return if !$ipamid; | |
83 | ||
84 | my $ipam_cfg = PVE::Network::SDN::Ipams::config(); | |
85 | my $plugin_config = $ipam_cfg->{ids}->{$ipamid}; | |
86 | my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type}); | |
87 | my $ip = $plugin->add_next_freeip($plugin_config, $subnetid, $subnet); | |
88 | return $ip; | |
89 | } | |
90 | ||
91 | sub add_ip { | |
92 | my ($subnetid, $subnet, $ip) = @_; | |
93 | ||
94 | my $ipamid = $subnet->{ipam}; | |
95 | return if !$ipamid; | |
96 | ||
97 | my $ipam_cfg = PVE::Network::SDN::Ipams::config(); | |
98 | my $plugin_config = $ipam_cfg->{ids}->{$ipamid}; | |
99 | my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type}); | |
100 | $plugin->add_ip($plugin_config, $subnetid, $ip); | |
101 | } | |
102 | ||
103 | sub del_ip { | |
104 | my ($subnetid, $subnet, $ip) = @_; | |
105 | ||
106 | my $ipamid = $subnet->{ipam}; | |
107 | return if !$ipamid; | |
108 | ||
109 | my $ipam_cfg = PVE::Network::SDN::Ipams::config(); | |
110 | my $plugin_config = $ipam_cfg->{ids}->{$ipamid}; | |
111 | my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type}); | |
112 | $plugin->del_ip($plugin_config, $subnetid, $ip); | |
113 | } | |
114 | ||
c33dd818 | 115 | 1; |