summaryrefslogtreecommitdiff
path: root/src/PVE/Network/SDN/Dhcp.pm
blob: 65e40d40465ade63ecd2b1da05f82267f78ed4a4 (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
package PVE::Network::SDN::Dhcp;

use strict;
use warnings;

use PVE::Cluster;

use PVE::Network::SDN;
use PVE::Network::SDN::Ipams;
use PVE::Network::SDN::Subnets;
use PVE::Network::SDN::Dhcp::Plugin;
use PVE::Network::SDN::Dhcp::Dnsmasq;

use PVE::INotify;

PVE::Network::SDN::Dhcp::Plugin->init();

PVE::Network::SDN::Dhcp::Dnsmasq->register();
PVE::Network::SDN::Dhcp::Dnsmasq->init();

sub plugin_types {
    return PVE::Network::SDN::Dhcp::Plugin->lookup_types();
}

sub add_mapping {
    my ($vnetid, $mac, $ip4, $ip6) = @_;

    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 $dhcptype = $zone->{dhcp};

    my $macdb = PVE::Network::SDN::Ipams::read_macdb();
    my $dhcp_plugin = PVE::Network::SDN::Dhcp::Plugin->lookup($dhcptype);
    $dhcp_plugin->add_ip_mapping($zoneid, $macdb, $mac, $ip4, $ip6);
}

sub remove_mapping {
    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};

    my $dhcp_plugin = PVE::Network::SDN::Dhcp::Plugin->lookup($zone->{dhcp});
    $dhcp_plugin->del_ip_mapping($zoneid, $mac);
}

sub regenerate_config {
    my ($reload) = @_;

    my $cfg = PVE::Network::SDN::running_config();

    my $zone_cfg = $cfg->{zones};
    my $vnet_cfg = $cfg->{vnets};
    my $subnet_cfg = $cfg->{subnets};
    return if !$zone_cfg && !$subnet_cfg;

    my $nodename = PVE::INotify::nodename();

    my $plugins = PVE::Network::SDN::Dhcp::Plugin->lookup_types();

    my $any_zone_needs_dhcp = grep { $_->{dhcp} } values $zone_cfg->{ids}->%*;

    foreach my $plugin_name (@$plugins) {
        my $plugin = PVE::Network::SDN::Dhcp::Plugin->lookup($plugin_name);
        eval { $plugin->before_regenerate(!$any_zone_needs_dhcp) };
        die "Could not run before_regenerate for DHCP plugin $plugin_name $@\n" if $@;
    }

    foreach my $zoneid (sort keys %{ $zone_cfg->{ids} }) {
        my $zone = $zone_cfg->{ids}->{$zoneid};
        next if !$zone->{dhcp};

        my $dhcp_plugin_name = $zone->{dhcp};
        my $dhcp_plugin = PVE::Network::SDN::Dhcp::Plugin->lookup($dhcp_plugin_name);

        die "Could not find DHCP plugin: $dhcp_plugin_name" if !$dhcp_plugin;

        eval { $dhcp_plugin->before_configure($zoneid, $zone) };
        die "Could not run before_configure for DHCP server $zoneid $@\n" if $@;

        for my $vnetid (sort keys %{ $vnet_cfg->{ids} }) {
            my $vnet = $vnet_cfg->{ids}->{$vnetid};
            next if $vnet->{zone} ne $zoneid;

            my $config = [];
            my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid);

            foreach my $subnet_id (sort keys %{$subnets}) {
                my $subnet_config = $subnets->{$subnet_id};
                my $dhcp_ranges = PVE::Network::SDN::Subnets::get_dhcp_ranges($subnet_config);

                my ($zone, $subnet_network, $subnet_mask) = split(/-/, $subnet_id);
                next if $zone ne $zoneid;

                eval { $dhcp_plugin->configure_subnet($config, $zoneid, $vnetid, $subnet_config) };
                warn "Could not configure subnet $subnet_id: $@\n" if $@;

                foreach my $dhcp_range (@$dhcp_ranges) {
                    eval {
                        $dhcp_plugin->configure_range(
                            $config, $zoneid, $vnetid, $subnet_config, $dhcp_range,
                        );
                    };
                    warn "Could not configure DHCP range for $subnet_id: $@\n" if $@;
                }
            }

            eval { $dhcp_plugin->configure_vnet($config, $zoneid, $vnetid, $vnet) };
            warn "Could not configure vnet $vnetid: $@\n" if $@;
        }

        eval { $dhcp_plugin->after_configure($zoneid, !$any_zone_needs_dhcp) };
        warn "Could not run after_configure for DHCP server $zoneid $@\n" if $@;

    }

    foreach my $plugin_name (@$plugins) {
        my $plugin = PVE::Network::SDN::Dhcp::Plugin->lookup($plugin_name);

        eval { $plugin->after_regenerate() };
        warn "Could not run after_regenerate for DHCP plugin $plugin_name $@\n" if $@;
    }
}

1;