]> git.proxmox.com Git - pve-container.git/blob - src/lxcnetaddbr
e496b23d89ddca833898a7433243236f259fbc7e
[pve-container.git] / src / lxcnetaddbr
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
7
8 use PVE::LXC;
9 use PVE::Tools qw(run_command);
10 use PVE::Network;
11 use PVE::ProcFSTools;
12
13 my $have_sdn;
14 eval {
15 require PVE::Network::SDN::Zones;
16 $have_sdn = 1;
17 };
18
19 die "got unexpected argument count\n" if scalar(@ARGV) != 5;
20
21 my ($vmid, $arg2, $arg3, $type, $iface) = @ARGV;
22
23 die "got unexpected argument ($arg2 != net)\n" if $arg2 ne 'net';
24 die "got unexpected argument ($arg3 != up)\n" if $arg3 ne 'up';
25
26 die "got unexpected argument ($type != veth)\n" if $type ne 'veth';
27
28 die "got unexpected environment" if $vmid ne $ENV{LXC_NAME};
29
30 die "missing vmid parameter\n" if !$vmid;
31 die "missing iface parameter\n" if !$iface;
32
33 my $conf = PVE::LXC::Config->load_config($vmid);
34
35 my $netconf;
36 if ($iface =~ m/^veth(\d+)i(\d+)$/) {
37 die "got unexpected interface name '$iface'\n" if $1 ne $vmid;
38 $netconf = $conf->{"net$2"};
39 }
40
41 die "unable to find network definition for interface '$iface'\n"
42 if !defined($netconf);
43
44 my $net = PVE::LXC::Config->parse_lxc_network($netconf);
45
46 my $tag = $net->{tag};
47 my $firewall = $net->{firewall};
48 my $bridge = $net->{bridge};
49 my $trunks = $net->{trunks};
50 my $rate = $net->{rate};
51
52 die "missing bridge configuration" if !$bridge;
53
54 if (-d "/sys/class/net/$iface") {
55
56 my $bridgemtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
57 die "bridge '$bridge' does not exist\n" if !$bridgemtu;
58 #avoid insecure dependency;
59 ($bridgemtu) = $bridgemtu =~ /(\d+)/;
60
61 PVE::Tools::run_command("/sbin/ip link set dev $iface up mtu $bridgemtu");
62 PVE::Tools::run_command("/sbin/ip addr add 0.0.0.0/0 dev $iface");
63
64 if ($have_sdn) {
65 PVE::Network::SDN::Zones::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
66 PVE::Network::SDN::Zones::add_bridge_fdb($iface, $net->{hwaddr}, $bridge, $firewall);
67 } else {
68 PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
69 PVE::Network::add_bridge_fdb($iface, $net->{hwaddr}, $net->{firewall}); # early returns if brport has learning on
70 }
71 }
72
73 exit 0;