1 package PVE
::Network
::SDN
;
9 use PVE
::Tools
qw(extract_param dir_glob_regex run_command);
10 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
11 use PVE
::Network
::SDN
::Plugin
;
12 use PVE
::Network
::SDN
::VnetPlugin
;
13 use PVE
::Network
::SDN
::VlanPlugin
;
14 use PVE
::Network
::SDN
::VxlanMulticastPlugin
;
16 PVE
::Network
::SDN
::VnetPlugin-
>register();
17 PVE
::Network
::SDN
::VlanPlugin-
>register();
18 PVE
::Network
::SDN
::VxlanMulticastPlugin-
>register();
19 PVE
::Network
::SDN
::Plugin-
>init();
23 my ($cfg, $sdnid, $noerr) = @_;
25 die "no sdn ID specified\n" if !$sdnid;
27 my $scfg = $cfg->{ids
}->{$sdnid};
28 die "sdn '$sdnid' does not exists\n" if (!$noerr && !$scfg);
34 my $config = cfs_read_file
("sdn.cfg.new");
35 $config = cfs_read_file
("sdn.cfg") if !keys %{$config->{ids
}};
42 cfs_write_file
("sdn.cfg.new", $cfg);
46 my ($code, $errmsg) = @_;
48 cfs_lock_file
("sdn.cfg.new", undef, $code);
50 $errmsg ?
die "$errmsg: $err" : die $err;
57 return keys %{$cfg->{ids
}};
61 my ($cmdname, $pname, $cvalue) = @_;
63 my $cfg = PVE
::Network
::SDN
::config
();
65 return $cmdname eq 'add' ?
[] : [ PVE
::Network
::SDN
::sdn_ids
($cfg) ];
70 my $cmd = ['ifquery', '-a', '-c', '-o','json'];
73 my $reader = sub { $result .= shift };
76 run_command
($cmd, outfunc
=> $reader);
79 my $resultjson = decode_json
($result);
82 foreach my $interface (@$resultjson) {
83 my $name = $interface->{name
};
84 $interfaces->{$name} = {
85 status
=> $interface->{status
},
86 config
=> $interface->{config
},
87 config_status
=> $interface->{config_status
},
95 sub generate_etc_network_config
{
97 my $sdn_cfg = PVE
::Cluster
::cfs_read_file
('sdn.cfg');
100 #read main config for physical interfaces
101 my $current_config_file = "/etc/network/interfaces";
102 my $fh = IO
::File-
>new($current_config_file);
103 my $interfaces_config = PVE
::INotify
::read_etc_network_interfaces
(1,$fh);
108 foreach my $id (keys %{$interfaces_config->{ifaces
}}) {
109 my $interface = $interfaces_config->{ifaces
}->{$id};
110 if (my $uplink = $interface->{'uplink-id'}) {
111 die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
112 $interface->{name
} = $id;
113 $uplinks->{$interface->{'uplink-id'}} = $interface;
117 my $vnet_cfg = undef;
118 my $transport_cfg = undef;
120 foreach my $id (keys %{$sdn_cfg->{ids
}}) {
121 if ($sdn_cfg->{ids
}->{$id}->{type
} eq 'vnet') {
122 $vnet_cfg->{ids
}->{$id} = $sdn_cfg->{ids
}->{$id};
124 $transport_cfg->{ids
}->{$id} = $sdn_cfg->{ids
}->{$id};
128 #generate configuration
130 foreach my $id (keys %{$vnet_cfg->{ids
}}) {
131 my $vnet = $vnet_cfg->{ids
}->{$id};
132 my $zone = $vnet->{transportzone
};
135 warn "can't generate vnet $vnet : zone $zone don't exist";
139 my $plugin_config = $transport_cfg->{ids
}->{$zone};
141 if (!defined($plugin_config)) {
142 warn "can't generate vnet $vnet : zone $zone don't exist";
146 my $plugin = PVE
::Network
::SDN
::Plugin-
>lookup($plugin_config->{type
});
147 $rawconfig .= $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $uplinks);
153 sub write_etc_network_config
{
154 my ($rawconfig) = @_;
156 return if !$rawconfig;
157 my $sdn_interfaces_file = "/etc/network/interfaces.d/sdn";
159 my $writefh = IO
::File-
>new($sdn_interfaces_file,">");
160 print $writefh $rawconfig;