网桥

来自 Alpine Linux

本文档描述了如何在 Alpine Linux 中配置网络桥接接口。

使用 brctl

此材料已过时...

根据上游bridge-utils 已被弃用。请使用 bridge 代替。(讨论

网桥使用 brctl 命令手动管理。

Usage: brctl COMMAND [BRIDGE [INTERFACE]]

Manage ethernet bridges

Commands:
	show			Show a list of bridges
	addbr BRIDGE		Create BRIDGE
	delbr BRIDGE		Delete BRIDGE
	addif BRIDGE IFACE	Add IFACE to BRIDGE
	delif BRIDGE IFACE	Delete IFACE from BRIDGE
	setageing BRIDGE TIME		Set ageing time
	setfd BRIDGE TIME		Set bridge forward delay
	sethello BRIDGE TIME		Set hello time
	setmaxage BRIDGE TIME		Set max message age
	setpathcost BRIDGE COST		Set path cost
	setportprio BRIDGE PRIO		Set port priority
	setbridgeprio BRIDGE PRIO	Set bridge priority
	stp BRIDGE [1|0]		STP on/off

手动创建网桥接口 br0

brctl addbr br0

将接口 eth0 和 eth1 添加到 br0

brctl addif br0 eth0 brctl addif br0 eth1

注意:您需要在添加的接口上将链接状态设置为up

ip link set dev eth0 up ip link set dev eth1 up

配置文件

安装配置网桥的脚本。

apk add bridge

然后在 /etc/network/interfaces 中使用 bridge-ports 关键字配置桥接。

警告: 通常您不会将 IP 地址分配给桥接接口(在我们的示例中为 eth0 和 eth1),而是分配给网桥本身 (br0)。


在此示例中,使用地址 192.168.0.1/24。

auto br0
iface br0 inet static
	bridge-ports eth0 eth1
	bridge-stp 0
	address 192.168.0.1
	netmask 255.255.255.0

您可以使用以下关键字设置各种选项

bridge-ports
设置桥接端口 (ethX) 或 none 表示没有物理接口
bridge-aging
设置老化时间
bridge-fd
设置网桥转发延迟
bridge-hello
设置 hello 时间
bridge-maxage
设置网桥最大消息生存期
bridge-pathcost
设置路径成本
bridge-portprio
设置端口优先级
bridge-bridgeprio
设置网桥优先级
bridge-stp
STP 开/关

使用 pre-up/post-down

如果您希望能够单独控制网桥接口,则需要使用 pre-up/post-down 钩子。

示例 /etc/network/interfaces

auto br0
iface br0 inet static
	pre-up brctl addbr br0
	pre-up echo 0 > /proc/sys/net/bridge/bridge-nf-call-arptables
	pre-up echo 0 > /proc/sys/net/bridge/bridge-nf-call-iptables
	pre-up echo 0 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
	address 192.168.0.253
	netmask 255.255.255.0
	gateway 192.168.0.254
	post-down brctl delbr br0
	
auto eth0
iface eth0 inet manual
	up ip link set $IFACE up
	up brctl addif br0 $IFACE
	down brctl delif br0 $IFACE || true
	down ip link set $IFACE down
	
auto eth1
iface eth1 inet manual
	up ip link set $IFACE up
	up brctl addif br0 $IFACE
	down brctl delif br0 $IFACE || true
	down ip link set $IFACE down

这样,您可以使用:ifup br0 创建 br0。您可以使用 ifup eth0, ifdown eth0 向网桥添加/删除单个接口。

为 Xen dom0 桥接

在 dom0 中桥接有点特殊,因为它包括将真实接口(即 ethX)与虚拟接口(即 vifX.Y)桥接。在网桥创建时,虚拟接口不存在,并且将在 domU 启动时由 Xen 工具栈添加(有关如何将虚拟接口链接到正确的网桥,请参阅 Xen 文档)。

特点

  • 网桥由单个物理接口组成
  • 物理接口没有 IP,并且是手动配置的
  • 网桥将具有 IP 地址,并且将是自动的,从而启动物理接口

这转化为示例配置 /etc/network/interfaces

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
	address 192.168.0.253
	netmask 255.255.255.0
	gateway 192.168.0.254
        bridge_ports eth0
        bridge_stp 0

domU OS 启动后,将添加虚拟接口,可以使用以下命令检查工作网桥

brctl show

ifconfig -a

为 KVM 桥接

示例 /etc/network/interfaces

注意: 我个人删除了 eth0 声明,没有任何问题。
auto br0
iface br0 inet dhcp
  bridge_ports eth0
  bridge_stp 0

允许 DHCP 通过 iptables 的小程序

注意: 我尝试了 #3 中提到的使用 pre-up/post-down,但对我来说效果不佳
注意: 通常它会在 /etc/rc.local 中,如此处所述
# Run local.d scripts on boot.
rc-update add local

# Write the script.
cat >> /etc/local.d/iptables_dhcp_kvm.start << EOM
echo 0 > /proc/sys/net/bridge/bridge-nf-call-arptables
echo 0 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 0 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
exit 0
EOM

# local.d script must be executable.
chmod a+x /etc/local.d/iptables_dhcp_kvm.start

为 QEMU 桥接

/etc/network/interfaces 替换为以下内容

auto lo
iface lo inet loopback

auto br0
iface br0 inet dhcp
  bridge_ports eth0
  bridge_stp 0

要启用 DHCP 并让 QEMU 使用我们上面创建的网桥,请运行

# Install the bridge tools
apk add bridge

# Load kernel modules needed for KVM bridging.
printf 'tun\ntap\n' >> /etc/modules

# Allow Qemu to use our bridge.
echo 'allow br0' > /etc/qemu/bridge.conf

# Write some sysctl knobs to allow bridging to work.
printf '# Enable bridge forwarding.
net.ipv4.conf.br0_bc_forwarding=1
# Ignore iptables on bridge interfaces.
net.bridge.bridge-nf-call-iptables=0
' >> /etc/sysctl.d/bridging.conf

# Apply sysctl config edits.
sysctl -p

重启后,您可以像这样使用网桥(假设您有一个名为 felix-pojtigners-theia.qcow2qcow2 镜像)

qemu-system-x86_64 -m 4096 -accel kvm -net nic -net bridge,br=br0 -boot d -drive format=qcow2,file=felix-pojtingers-theia.qcow2

如果您没有为客户机获得 DHCP 响应(假设您在 eth0 连接到的物理网络上运行了 DHCP 服务器),您可以使用 tshark 进行调试

tshark -i eth0  -Y "bootp.option.type == 53"