]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Plugin.pm
add controller_reload
[pve-network.git] / PVE / Network / SDN / Plugin.pm
CommitLineData
86d22462 1package PVE::Network::SDN::Plugin;
6939693f
AD
2
3use strict;
4use warnings;
5
6use PVE::Tools;
7use PVE::JSONSchema;
8use PVE::Cluster;
9
10use Data::Dumper;
eec580bf 11use PVE::JSONSchema qw(get_standard_option);
6939693f
AD
12use base qw(PVE::SectionConfig);
13
6bffe819 14PVE::Cluster::cfs_register_file('sdn.cfg',
39d04c82
AD
15 sub { __PACKAGE__->parse_config(@_); });
16
6bffe819 17PVE::Cluster::cfs_register_file('sdn.cfg.new',
6939693f
AD
18 sub { __PACKAGE__->parse_config(@_); },
19 sub { __PACKAGE__->write_config(@_); });
20
6bffe819
AD
21PVE::JSONSchema::register_standard_option('pve-sdn-id', {
22 description => "The SDN object identifier.",
23 type => 'string', format => 'pve-sdn-id',
fe61b14c
AD
24});
25
6bffe819
AD
26PVE::JSONSchema::register_format('pve-sdn-id', \&parse_sdn_id);
27sub parse_sdn_id {
28 my ($sdnid, $noerr) = @_;
fe61b14c 29
6bffe819 30 if ($sdnid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
fe61b14c 31 return undef if $noerr;
6bffe819 32 die "SDN object ID '$sdnid' contains illegal characters\n";
fe61b14c 33 }
6bffe819 34 return $sdnid;
fe61b14c
AD
35}
36
6939693f
AD
37my $defaultData = {
38
39 propertyList => {
7d35eaf5 40 type => {
6939693f
AD
41 description => "Plugin type.",
42 type => 'string', format => 'pve-configid',
43 type => 'string',
44 },
6bffe819
AD
45 sdn => get_standard_option('pve-sdn-id',
46 { completion => \&PVE::Network::SDN::complete_sdn }),
6939693f
AD
47 },
48};
49
50sub private {
51 return $defaultData;
52}
53
54sub parse_section_header {
55 my ($class, $line) = @_;
56
57 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
6bffe819 58 my ($type, $sdnid) = (lc($1), $2);
6939693f
AD
59 my $errmsg = undef; # set if you want to skip whole section
60 eval { PVE::JSONSchema::pve_verify_configid($type); };
61 $errmsg = $@ if $@;
62 my $config = {}; # to return additional attributes
6bffe819 63 return ($type, $sdnid, $errmsg, $config);
6939693f
AD
64 }
65 return undef;
66}
67
6bffe819 68sub generate_sdn_config {
6939693f
AD
69 my ($class, $plugin_config, $node, $data, $ctime) = @_;
70
71 die "please implement inside plugin";
72}
73
8fb1ee7f 74sub generate_controller_config {
074d270b 75 my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_;
32602a38
AD
76
77 die "please implement inside plugin";
78}
79
ad03c543
AD
80sub generate_controller_vnet_config {
81 my ($class, $plugin_config, $controller, $transportid, $vnetid, $config) = @_;
82
83}
84
8fb1ee7f
AD
85sub write_controller_config {
86 my ($class, $plugin_config, $config) = @_;
87
88 die "please implement inside plugin";
89}
90
fa609bdd
AD
91sub controller_reload {
92 my ($class) = @_;
93
94 die "please implement inside plugin";
95}
96
fe0c6b9e 97sub on_delete_hook {
6bffe819 98 my ($class, $sndid, $scfg) = @_;
e8d5906e
AD
99
100 # do nothing by default
101}
102
103sub on_update_hook {
6bffe819 104 my ($class, $sdnid, $scfg) = @_;
fe0c6b9e
AD
105
106 # do nothing by default
107}
108
6939693f
AD
109#helpers
110sub parse_tag_number_or_range {
111 my ($str, $max, $tag) = @_;
112
113 my @elements = split(/,/, $str);
114 my $count = 0;
115 my $allowed = undef;
116
117 die "extraneous commas in list\n" if $str ne join(',', @elements);
118 foreach my $item (@elements) {
119 if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
120 $count += 2;
121 my ($port1, $port2) = ($1, $2);
122 die "invalid port '$port1'\n" if $port1 > $max;
123 die "invalid port '$port2'\n" if $port2 > $max;
124 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
125
126 if ($tag && $tag >= $port1 && $tag <= $port2){
127 $allowed = 1;
128 last;
129 }
130
131 } elsif ($item =~ m/^([0-9]+)$/) {
132 $count += 1;
133 my $port = $1;
134 die "invalid port '$port'\n" if $port > $max;
135
136 if ($tag && $tag == $port){
137 $allowed = 1;
138 last;
139 }
140 }
141 }
142 die "tag $tag is not allowed" if $tag && !$allowed;
143
144 return (scalar(@elements) > 1);
145}
146
3ee45e4c
AD
147#to be move to Network.pm helper
148sub get_first_local_ipv4_from_interface {
149 my ($interface) = @_;
150
151 my $cmd = ['/sbin/ip', 'address', 'show', 'dev', $interface];
152
153 my $IP = "";
154
155 my $code = sub {
156 my $line = shift;
157
158 if ($line =~ m!^\s*inet\s+($PVE::Tools::IPRE)(?:/\d+|\s+peer\s+)!) {
159 $IP = $1;
160 return;
161 }
162 };
163
164 PVE::Tools::run_command($cmd, outfunc => $code);
165
166 return $IP;
167}
168
6939693f 1691;