]>
Commit | Line | Data |
---|---|---|
86d22462 | 1 | package PVE::Network::SDN; |
92b6f291 AD |
2 | |
3 | use strict; | |
4 | use warnings; | |
434125ce | 5 | |
92b6f291 | 6 | use Data::Dumper; |
c665cefc | 7 | use JSON; |
434125ce TL |
8 | |
9 | use PVE::Tools qw(extract_param dir_glob_regex run_command); | |
92b6f291 | 10 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); |
86d22462 AD |
11 | use PVE::Network::SDN::Plugin; |
12 | use PVE::Network::SDN::VnetPlugin; | |
13 | use PVE::Network::SDN::VlanPlugin; | |
14 | use PVE::Network::SDN::VxlanMulticastPlugin; | |
92b6f291 | 15 | |
86d22462 AD |
16 | PVE::Network::SDN::VnetPlugin->register(); |
17 | PVE::Network::SDN::VlanPlugin->register(); | |
18 | PVE::Network::SDN::VxlanMulticastPlugin->register(); | |
19 | PVE::Network::SDN::Plugin->init(); | |
92b6f291 AD |
20 | |
21 | ||
6bffe819 AD |
22 | sub sdn_config { |
23 | my ($cfg, $sdnid, $noerr) = @_; | |
92b6f291 | 24 | |
6bffe819 | 25 | die "no sdn ID specified\n" if !$sdnid; |
92b6f291 | 26 | |
6bffe819 AD |
27 | my $scfg = $cfg->{ids}->{$sdnid}; |
28 | die "sdn '$sdnid' does not exists\n" if (!$noerr && !$scfg); | |
92b6f291 AD |
29 | |
30 | return $scfg; | |
31 | } | |
32 | ||
33 | sub config { | |
6bffe819 AD |
34 | my $config = cfs_read_file("sdn.cfg.new"); |
35 | $config = cfs_read_file("sdn.cfg") if !keys %{$config->{ids}}; | |
39d04c82 | 36 | return $config; |
92b6f291 AD |
37 | } |
38 | ||
39 | sub write_config { | |
40 | my ($cfg) = @_; | |
41 | ||
6bffe819 | 42 | cfs_write_file("sdn.cfg.new", $cfg); |
92b6f291 AD |
43 | } |
44 | ||
6bffe819 | 45 | sub lock_sdn_config { |
92b6f291 AD |
46 | my ($code, $errmsg) = @_; |
47 | ||
6bffe819 | 48 | cfs_lock_file("sdn.cfg.new", undef, $code); |
0c5021ad | 49 | if (my $err = $@) { |
92b6f291 AD |
50 | $errmsg ? die "$errmsg: $err" : die $err; |
51 | } | |
52 | } | |
53 | ||
6bffe819 | 54 | sub sdn_ids { |
92b6f291 AD |
55 | my ($cfg) = @_; |
56 | ||
57 | return keys %{$cfg->{ids}}; | |
58 | } | |
59 | ||
6bffe819 | 60 | sub complete_sdn { |
92b6f291 AD |
61 | my ($cmdname, $pname, $cvalue) = @_; |
62 | ||
86d22462 | 63 | my $cfg = PVE::Network::SDN::config(); |
92b6f291 | 64 | |
6bffe819 | 65 | return $cmdname eq 'add' ? [] : [ PVE::Network::SDN::sdn_ids($cfg) ]; |
92b6f291 AD |
66 | } |
67 | ||
c665cefc AD |
68 | sub status { |
69 | ||
70 | my $cmd = ['ifquery', '-a', '-c', '-o','json']; | |
c665cefc | 71 | |
0c5021ad TL |
72 | my $result = ''; |
73 | my $reader = sub { $result .= shift }; | |
c665cefc AD |
74 | |
75 | eval { | |
6e9fff39 | 76 | run_command($cmd, outfunc => $reader); |
c665cefc AD |
77 | }; |
78 | ||
6e9fff39 | 79 | my $resultjson = decode_json($result); |
c665cefc AD |
80 | my $interfaces = {}; |
81 | ||
82 | foreach my $interface (@$resultjson) { | |
6e9fff39 AD |
83 | my $name = $interface->{name}; |
84 | $interfaces->{$name} = { | |
85 | status => $interface->{status}, | |
86 | config => $interface->{config}, | |
87 | config_status => $interface->{config_status}, | |
88 | }; | |
c665cefc AD |
89 | } |
90 | ||
91 | return $interfaces; | |
92 | } | |
93 | ||
92b6f291 | 94 | 1; |