Bài gốc: A First Look at Linux Bridge — tác giả BaconYao, lấy qua Notion public API.
Key Takeaway
Linux Bridge
Introduction
ALinux Bridge is a virtual network device implemented within the Linux kernel that enables Layer 2 (Data Link Layer) packet forwarding between multiple network interfaces on the same host—similar to a physical switch.
It is primarily used to bridge different network interfaces—such as virtual machines, containers, or physical network interfaces—allowing them to communicate within the same Layer 2 broadcast domain.
How It Works
- A Linux Bridge functions as a software-based switch and operates via the Linux kernel’s bridge module.
-
When a packet arrives at the bridge, it checks the
destination MAC addressand consults its internal forwarding table:- If the destination MAC is known, the packet is forwarded to the corresponding interface.
- If unknown, the packet is broadcast to all bridged interfaces (except the one it came from).
-
The bridge supports the
Spanning Tree Protocol (STP)to prevent network loops and ensure network stability.
Practical Use Case
Imagine you have a server running multiple virtual machines that need to communicate with the external network:-
Create a Linux bridge (e.g.,
br0). -
Add the host’s physical network interface (
eth0) and the virtual machines’ network interfaces (vnet0,vnet1) tobr0. -
Assign an IP address to
br0if Layer 3 communication is required. -
Now, the virtual machines can communicate with the external network through
br0and with each other directly.
Userspace Tools
-
bridge-utils: Contains the legacy
brctltool. Source code is available in the bridge-utils repository. - iproute2: A modern network configuration suite. Source code can be found in the iproute2 repository.
Forwarding Database (FDB)
Introduction
TheForwarding Database (FDB) plays a key role in a Linux Bridge by keeping track of each connected device’s MAC address and the specific interface (port) it is associated with. When the bridge receives an Ethernet frame, it consults the FDB to determine how to forward the packet:
- If the destination MAC address exists in the FDB, the packet is forwarded to the corresponding interface.
-
If the destination MAC address is not found in the FDB, the packet is
floodedto all bridged interfaces (except the source interface). -
The FDB also supports
dynamic learningandaging, ensuring the table stays current and efficient.Aging Time
Aging Time
Introduction
-
How It Works:
-
When a bridge device (e.g.,
br0) receives a packet from an interface (e.g.,veth6), it records the source MAC address and the associated interface, creating a dynamic FDB entry (e.g.,c6:e9:9b:3e:43:4a dev veth6 master br0). - Each dynamic entry has an ageing timer. If no packets are received from that MAC address within the ageing time, the entry is removed to prevent the FDB from being cluttered with stale data.
-
The
defaultageing time is usually300 seconds(5 minutes), but this value can be customized.
-
When a bridge device (e.g.,
-
Why Ageing Time Matters:
-
Resource Efficiency: The FDB has limited capacity. Removing outdated entries conserves memory and system resources. -
Network Adaptability: Devices may change interfaces or MAC addresses (e.g., during a container restart). The ageing mechanism ensures the FDB reflects the current network topology. -
Avoiding Incorrect Forwarding: Keeping stale MAC entries could cause packets to be misrouted to interfaces that no longer exist or are inactive.
-
-
Impact:
-
For example, the dynamic entry
c6:e9:9b:3e:43:4a dev veth6 master br0will be subject to ageing. If no packets are sent or received viaveth6during the ageing period, the entry may be removed. -
Static entries (marked as
permanent, e.g.,e6:42:3e:0e:65:6b dev veth6 master br0 permanent) are not affected by ageing time because they are manually configured or fixed by the system.
-
For example, the dynamic entry
How to Prevent or Extend Ageing Time
-
Increase the Ageing Time
-
Extend the duration before dynamic entries expire.
-
echo 60000 > /sys/class/net/br0/bridge/ageing_time -
ip link set br0 type bridge ageing_time 600
-
-
Extend the duration before dynamic entries expire.
-
Convert Dynamic Entries to Static (Permanent)
-
Prevent specific MAC entries from being aged out. Manually set a MAC address as permanent:
-
bridge fdb add c6:e9:9b:3e:43:4a dev veth6 master permanent- Note: If the entry already exists as dynamic, you may need to delete it first before adding it as permanent.
-
-
Prevent specific MAC entries from being aged out. Manually set a MAC address as permanent:
-
Maintain Network Activity
- Keep sending traffic through the interface periodically to reset the ageing timer and keep dynamic entries active.
-
Disable the Ageing Mechanism
-
Stop ageing entirely by setting the ageing time to
0.-
echo 0 > /sys/class/net/br0/bridge/ageing_time -
ip link set br0 type bridge ageing_time 0
-
-
Stop ageing entirely by setting the ageing time to
-
How It Works:
Structure of the FDB
The FDB is typically organized as a table with the following key fields:- MAC Address: The 48-bit Ethernet MAC address of the device.
-
Interface (Port):
The bridge interface through which the MAC address was learned (e.g.,
eth0,vnet0). -
Flags:
Indicate the type and behavior of the entry:
-
self: The entry belongs to the bridge device itself (e.g., multicast or the bridge’s own address). -
permanent: A statically configured entry that will not age out. -
(No flag): Implies a
dynamically learnedentry, which is subject to ageing and will expire if inactive.
-
Real FDB case
Real FDB case
Case BackgroundSuppose you have a server running a Linux Bridge
br0, which bridges the following interfaces:-
eth0: Physical network interface connected to the external network. -
vnet0: Virtual network interface for the first virtual machine. -
vnet1: Virtual network interface for the second virtual machine.
bridge fdb show command, simulating the FDB content:Setting Up a Linux Bridge Test Environment
1. vagrant + virtualbox + ubuntu-24.04
Once the environment is set up, you should see network information similar to the followingip information
ip information
2. Network Namespace
Create four Network Namespaces
Create four pairs of VETH interfaces
Attach VETH to Network Namespace and Assign IP Address
3. Linux Bridge
Create a Linux Bridge
Create a bridge named
br0 on the host.
Connect the veth interfaces to the Linux Bridge.
Connect the host’s veth interfaces to the After completion, you’ll see something like the image on the right— each veth interface now shows the
br0 Linux Bridge using the master interface setting.master br0 attribute.
By the way, you can also use the legacy
brctl command or the bridge command to check the bridge status.
Bring interfaces up
Enable (bring up) all veth interfaces and thebr0 interface on the host
The diagram above shows that all links are in the forwarding state, which means communication can now begin.Experiment Walkthrough
Introduction
In this experiment, we will initiate a
ping command from network namespace ns1 to ns4, using the Linux Bridge as an intermediary to forward the packets.The diagram on the left provides a high-level view to help understand how the packets, components, and commands correspond to the layers of the OSI model.If
ns1’s ARP table does not have an entry for ns4’s IP address, it will first send an ARP Request packet through the Linux Bridge. The bridge then handles forwarding this request so that ns1 can learn ns4’s MAC address before sending actual data packets.If the Linux Bridge’s FDB also lacks an entry for
ns4’s MAC address, it will flood the ARP request —broadcasting it out to all other interfaces except the one it was received on—so that ns4 can receive the request and respond.ns4 replies with an ARP Reply, so the Linux Bridge updates its FDB with ns4’s MAC address and interface mapping. It then forwards the ARP Reply back to the original requester, ns1.At this point,
ns1 has all the necessary information and can send the ICMP Echo Request packet to the Linux Bridge, which will forward it to ns4.Similarly, the ICMP Reply from
ns4 will be forwarded back to ns1 through the Linux Bridge.Let’s get started with the real experiment!
Before starting the experiment, let’s review the current environment. As shown in the diagram below, each network namespace is configured as follows.
Analyzing the Forwarding Database (FDB) of the Linux Bridge device br0.
After completing the setup steps, before running the ping command, let’s analyze the current Forwarding Database (FDB) on the host.
We start by checking the FDB of br0— the MAC address table—which records the MAC addresses learned on each interface of the bridge, along with their corresponding ports, VLAN IDs, and related details.
Analyze
veth0-
42:a3:b1:71:35:5c:The statically configured MAC address appears twice:
-
Once associated with VLAN 1 (
vlan 1 master br0 permanent). -
Once without VLAN information (
master br0 permanent). -
This is likely the MAC address of the
veth0device, statically bound to the bridge.
-
Once associated with VLAN 1 (
-
33:33:00:00:00:01:The
IPv6multicast address (all-nodes multicast) is marked asself permanent, indicating it is a fixed multicast address handled by the bridge device itself. -
01:00:5e:00:00:01:The
IPv4multicast address (all-hosts multicast) is likewise marked asself permanent, indicating it is a fixed multicast address managed by the bridge device itself. -
33:33:ff:71:35:5c:The
IPv6solicited-node multicast address (derived from the last part of42:a3:b1:71:35:5c) is marked asself permanent. It is used for IPv6Neighbor Discovery Protocol (NDP).
br0-
33:33:00:00:00:01, 01:00:5e:00:00:01:The bridge device’s own
IPv6andIPv4multicast addresses. - 01:00:5e:00:00:6a, 33:33:00:00:00:6a:Additional multicast addresses that may be associated with specific multicast groups (e.g., for certain protocols or services).
-
33:33:ff:dc:15:1c:The bridge device’s
IPv6solicited-node multicast address, generated based on16:f0:f3:dc:15:1c. -
16:f0:f3:dc:15:1c:The MAC address of the bridge device
br0itself, statically configured, appearing both with and without VLAN 1 association.
Let’s ping ns4 from ns1
As shown in the right diagram, in
ns2, ns3, and ns4, we use tcpdump -vvvvln -i <interface> to monitor packets on each network namespace interface.

After successfully executing the
ping command, we can observe that:-
ns2andns3receive the ARP packets, but since the requested IP address (Request who-has) is not theirs, they do not respond. -
ns4, being the target, replies with an ARP Reply to informns1, then receives the ICMP packet fromns1and finally responds with the ICMP reply.
Revisit br0’s Forwarding Database (FDB)
After running the ping command, let’s check if there are any changes in the
FDB.Since we focused on communication between ns1 and ns4, we observe changes related to veth0, veth6, and br0:-
New entries with
master br0appear forveth0andveth6:-
When
veth0sends ICMP packets toveth6, the Linux Bridgebr0dynamically learns and records their MAC addresses in the FDB. -
This way, the FDB contains MAC addresses for both
veth0andveth6, enabling efficient forwarding of subsequent packets.
-
When