]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/FrrPlugin.pm
move gateway-nodes option to frr plugin and add gateway-external-peers
[pve-network.git] / PVE / Network / SDN / FrrPlugin.pm
1 package PVE::Network::SDN::FrrPlugin;
2
3 use strict;
4 use warnings;
5 use PVE::Network::SDN::Plugin;
6 use PVE::Tools;
7 use PVE::INotify;
8 use PVE::JSONSchema qw(get_standard_option);
9
10 use base('PVE::Network::SDN::Plugin');
11
12 sub type {
13 return 'frr';
14 }
15
16 sub properties {
17 return {
18 'asn' => {
19 type => 'integer',
20 description => "autonomous system number",
21 },
22 'peers' => {
23 description => "peers address list.",
24 type => 'string', #fixme: format
25 },
26 'gateway-nodes' => get_standard_option('pve-node-list'),
27 'gateway-external-peers' => {
28 description => "upstream bgp peers address list.",
29 type => 'string', #fixme: format
30 },
31 };
32 }
33
34 sub options {
35
36 return {
37 'uplink-id' => { optional => 0 },
38 'asn' => { optional => 0 },
39 'peers' => { optional => 0 },
40 'gateway-nodes' => { optional => 1 },
41 'gateway-external-peers' => { optional => 1 },
42 };
43 }
44
45 # Plugin implementation
46 sub generate_frr_config {
47 my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_;
48
49 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
50
51 my $asn = $plugin_config->{asn};
52 my $uplink = $plugin_config->{'uplink-id'};
53 my $gatewaynodes = $plugin_config->{'gateway-nodes'};
54 my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
55
56 return if !$asn;
57
58 my $iface = "uplink$uplink";
59 my $ifaceip = "";
60
61 if($uplinks->{$uplink}->{name}) {
62 $iface = $uplinks->{$uplink}->{name};
63 $ifaceip = PVE::Network::SDN::Plugin::get_first_local_ipv4_from_interface($iface);
64 }
65
66 my $is_gateway = undef;
67 my $local_node = PVE::INotify::nodename();
68
69 foreach my $gatewaynode (PVE::Tools::split_list($gatewaynodes)) {
70 $is_gateway = 1 if $gatewaynode eq $local_node;
71 }
72
73 my @router_config = ();
74
75 push @router_config, "bgp router-id $ifaceip";
76 push @router_config, "no bgp default ipv4-unicast";
77 push @router_config, "no bgp default ipv6-unicast";
78 push @router_config, "coalesce-time 1000";
79
80 foreach my $address (@peers) {
81 next if $address eq $ifaceip;
82 push @router_config, "neighbor $address remote-as $asn";
83 }
84
85 if ($is_gateway) {
86 foreach my $address (@gatewaypeers) {
87 push @router_config, "neighbor $address remote-as external";
88 }
89 }
90 push(@{$config->{router}->{"bgp $asn"}->{""}}, @router_config);
91
92 @router_config = ();
93 foreach my $address (@peers) {
94 next if $address eq $ifaceip;
95 push @router_config, "neighbor $address activate";
96 }
97 push @router_config, "advertise-all-vni";
98 push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @router_config);
99
100 if ($is_gateway) {
101
102 @router_config = ();
103 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
104 #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1)
105 #https://github.com/FRRouting/frr/issues/4905
106 foreach my $address (@gatewaypeers) {
107 push @router_config, "neighbor $address activate";
108 }
109 push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
110 push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
111
112 }
113
114 return $config;
115 }
116
117 sub on_delete_hook {
118 my ($class, $routerid, $sdn_cfg) = @_;
119
120 # verify that transport is associated to this router
121 foreach my $id (keys %{$sdn_cfg->{ids}}) {
122 my $sdn = $sdn_cfg->{ids}->{$id};
123 die "router $routerid is used by $id"
124 if (defined($sdn->{router}) && $sdn->{router} eq $routerid);
125 }
126 }
127
128 sub on_update_hook {
129 my ($class, $routerid, $sdn_cfg) = @_;
130
131 # verify that asn is not already used by another router
132 my $asn = $sdn_cfg->{ids}->{$routerid}->{asn};
133 foreach my $id (keys %{$sdn_cfg->{ids}}) {
134 next if $id eq $routerid;
135 my $sdn = $sdn_cfg->{ids}->{$id};
136 die "asn $asn is already used by $id"
137 if (defined($sdn->{asn}) && $sdn->{asn} eq $asn);
138 }
139 }
140
141 1;
142
143