]> git.proxmox.com Git - pve-container.git/blame_incremental - src/lxcnetaddbr
network: let the common tap-plug helper add fdb entries
[pve-container.git] / src / lxcnetaddbr
... / ...
CommitLineData
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
7
8use PVE::LXC;
9use PVE::Tools qw(run_command);
10use PVE::Network;
11use PVE::ProcFSTools;
12
13my $have_sdn;
14eval {
15 require PVE::Network::SDN::Zones;
16 $have_sdn = 1;
17};
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';
27
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
33my $conf = PVE::LXC::Config->load_config($vmid);
34
35my $netconf;
36if ($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
41die "unable to find network definition for interface '$iface'\n"
42 if !defined($netconf);
43
44my $net = PVE::LXC::Config->parse_lxc_network($netconf);
45
46my $tag = $net->{tag};
47my $firewall = $net->{firewall};
48my $bridge = $net->{bridge};
49my $trunks = $net->{trunks};
50my $rate = $net->{rate};
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");
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, { mac => $net->{hwaddr}});
69 }
70}
71
72exit 0;