]> git.proxmox.com Git - pve-container.git/blame - src/lxcnetaddbr
network: let the common tap-plug helper add fdb entries
[pve-container.git] / src / lxcnetaddbr
CommitLineData
8a04b6c7
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
d6811d3f 6exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
4ed2b825 7
8a04b6c7
DM
8use PVE::LXC;
9use PVE::Tools qw(run_command);
10use PVE::Network;
11use PVE::ProcFSTools;
a1ff8c37 12
ab2ec461
AD
13my $have_sdn;
14eval {
15 require PVE::Network::SDN::Zones;
16 $have_sdn = 1;
17};
8a04b6c7
DM
18
19die "got unexpected argument count\n" if scalar(@ARGV) != 5;
20
21my ($vmid, $arg2, $arg3, $type, $iface) = @ARGV;
22
23die "got unexpected argument ($arg2 != net)\n" if $arg2 ne 'net';
24die "got unexpected argument ($arg3 != up)\n" if $arg3 ne 'up';
25
26die "got unexpected argument ($type != veth)\n" if $type ne 'veth';
a1ff8c37 27
8a04b6c7
DM
28die "got unexpected environment" if $vmid ne $ENV{LXC_NAME};
29
30die "missing vmid parameter\n" if !$vmid;
31die "missing iface parameter\n" if !$iface;
32
67afe46e 33my $conf = PVE::LXC::Config->load_config($vmid);
8a04b6c7 34
27916659 35my $netconf;
18862537 36if ($iface =~ m/^veth(\d+)i(\d+)$/) {
27916659
DM
37 die "got unexpected interface name '$iface'\n" if $1 ne $vmid;
38 $netconf = $conf->{"net$2"};
8a04b6c7
DM
39}
40
41die "unable to find network definition for interface '$iface'\n"
27916659
DM
42 if !defined($netconf);
43
1b4cf758 44my $net = PVE::LXC::Config->parse_lxc_network($netconf);
a1ff8c37 45
8a04b6c7
DM
46my $tag = $net->{tag};
47my $firewall = $net->{firewall};
48my $bridge = $net->{bridge};
23eb2244 49my $trunks = $net->{trunks};
380962c7 50my $rate = $net->{rate};
8a04b6c7
DM
51
52die "missing bridge configuration" if !$bridge;
53
54if (-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");
ab2ec461 63
a1ff8c37 64 if ($have_sdn) {
d21e45d5
TL
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);
ab2ec461 67 } else {
362a3fb9 68 PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate, { mac => $net->{hwaddr}});
ab2ec461 69 }
8a04b6c7
DM
70}
71
72exit 0;