Sub topic 4
Choosing a Load-Balancing Hash
This chapter assumes you know what a LAG (link aggregation group, also called a port channel) is and that frames from one conversation must stay in order. It also assumes you can read basic MAC, IP, and TCP/UDP header fields. Here you learn how a switch or host turns packet fields into a choice of member link, and how to pick the inputs so traffic spreads well.
What the hash does
A LAG does not split a single frame across links, and it does not usually send frames round-robin. Reordering frames within one conversation would break many protocols and hurt TCP performance. Instead, the sender computes a hash over selected header fields and uses the result to choose one member link.
The hash is a function that turns a set of inputs into a number. Two properties matter:
- The same inputs always give the same number, so a given flow stays on one member and arrives in order.
- Different inputs give numbers that are spread roughly evenly, so many flows spread across the members.
The number is reduced to a member index, often with a modulo or a lookup table. With two members, the result is 0 or 1.
Why one flow stays on one link
Because the inputs for a single flow never change, the result never changes. A single TCP transfer between two hosts therefore uses one member link, and its throughput is capped at that link's speed. Two 1 Gbps links in a bundle do not give one file copy 2 Gbps.
The bundle helps when there are many flows. With enough distinct flows, the hash spreads them across members and the total throughput approaches the sum of the links.
The following Python sketch models the idea. It is a simplified model, not what any switch ASIC runs, but it shows the behavior.
import zlib
def pick_member(src_ip, dst_ip, src_port, dst_port, proto, n_links):
key = f"{src_ip}|{dst_ip}|{src_port}|{dst_port}|{proto}".encode()
return zlib.crc32(key) % n_links
# The same flow always maps to the same member link.
flow = ("10.0.0.5", "10.0.1.9", 50000, 443, "tcp")
print(pick_member(*flow, n_links=2) == pick_member(*flow, n_links=2))The program prints True. If you change the source port, the member may change. That is how a client opening many connections to one server can still use several links, but only if the hash includes the port.
Hash input options
Platforms let you choose which fields feed the hash. The names vary, but the common choices are these.
| Input fields | Layer | Typical name | Spreads traffic well when |
|---|---|---|---|
| Source MAC | 2 | `src-mac` | Many different sending hosts |
| Destination MAC | 2 | `dst-mac` | Many different receiving hosts |
| Source and destination MAC | 2 | `src-dst-mac` | Many host pairs |
| Source IP | 3 | `src-ip` | Many different source addresses |
| Destination IP | 3 | `dst-ip` | Many different destinations |
| Source and destination IP | 3 | `src-dst-ip` | Many IP pairs |
| IPs plus TCP/UDP ports | 4 | `src-dst-port` or `layer3+4` | Few IP pairs but many connections |
Not every platform offers every option. Check the options on your hardware with the help text on the global command, since the available keywords differ between models and software versions.
Matching the hash to your traffic
Pick the hash by asking: which fields differ between the flows that cross this bundle? The fields with the most variety give the best spread.
Router or gateway behind the bundle
If a bundle connects a switch to a single router, almost every frame leaving the router has the router's MAC address as the source. A source-MAC hash on the traffic coming from the router sends everything to one member. A better choice is destination MAC or destination IP, which vary with the receiving hosts. Frames entering the bundle from the router side are decided by the router's own hash, so each end of the bundle chooses its own policy.
Server with many clients
A server talking to thousands of clients has one server address and many client addresses. A source-destination IP hash works, because the client addresses vary. If clients sit behind a NAT (network address translation) device, many clients share one IP address, and an IP-only hash loses variety. Adding Layer 4 ports restores it, because each connection uses a different source port.
Two hosts, many connections
A backup job between two hosts, using one IP pair, gives an IP-only hash a single input. Every connection lands on one member. Include TCP/UDP ports so parallel connections can spread out. A single connection is still limited to one link.
Traffic that cannot be hashed on ports
Some traffic has no usable port information. Fragmented IP packets after the first fragment carry no port numbers, and encrypted tunnels such as IPsec ESP hide them. Tunnel traffic often shows one outer IP pair, so the hash sees one flow. Platforms that look at ports handle this differently, so test with your real traffic.
Asymmetry is normal
Each end of a bundle chooses a member link for the frames it sends. The switch's hash controls traffic from the switch toward the server. The server's hash controls traffic from the server toward the switch. The two directions of one conversation can use different member links, and that is fine.
This means a configuration change on one device does not fix imbalance in the other direction. If the load is uneven on the switch-to-server direction, change the switch. If it is uneven the other way, change the server.
Configuring the hash
On a Cisco IOS style switch, the policy is usually a global setting that applies to all port channels on the switch, though some platforms allow it per bundle.
! Global: choose the hash inputs (available options vary by platform)
port-channel load-balance src-dst-ip
!
interface range GigabitEthernet0/1 - 2
switchport mode trunk
switchport trunk allowed vlan 10,20
channel-group 1 mode active
!
interface Port-channel1
switchport mode trunk
switchport trunk allowed vlan 10,20The port-channel load-balance line sets the hash. The rest creates the bundle and is covered in a later chapter. To see which hash is active, run:
show etherchannel load-balanceOn a Linux bond, the matching setting is xmit_hash_policy.
ip link add bond0 type bond mode 802.3ad miimon 100 lacp_rate fast xmit_hash_policy layer3+4
ip link set eth0 down
ip link set eth0 master bond0
ip link set eth1 down
ip link set eth1 master bond0
ip link set bond0 up
cat /proc/net/bonding/bond0Linux offers layer2 (the default, using MAC addresses), layer2+3, layer3+4, and others. The status file shows the policy under "Transmit Hash Policy". Remember that layer3+4 is not fully compliant with the 802.3ad standard in every case, because fragmented traffic can be spread differently from unfragmented traffic. In practice many deployments use it anyway, and the kernel documentation describes the trade-off.
Edge cases and gotchas
- Link counts that are not a power of two. With 3 members, some hash implementations map flows unevenly, because the hash value space does not divide evenly. Some platforms use buckets (for example 8) and assign them to members, so 3 links get 3, 3, and 2 buckets. Even a perfect hash then gives a 3:3:2 split. Using 2, 4, or 8 members avoids this.
- Few flows. Hashing is statistical. With only a handful of flows, two heavy flows can land on the same member while another member is idle. No hash setting fixes a traffic pattern that has too few flows.
- Elephant flows. One very large flow can saturate its member link even when the other members are idle. The hash cannot divide it.
- Changing members. When a member fails or is added, the mapping changes, and some flows move to different links. Flows that move may see brief reordering or loss. Later chapters cover this.
- Hash polarization. If two devices in series use the same hash on similar inputs, the second device may only ever see traffic that fell into one hash bucket on the first, so it spreads poorly. Some platforms add a device-specific seed to avoid this.
A worked decision
Suppose a file server with a two-link bond serves 200 clients in the same VLAN, and the clients connect through a switch.
- On the switch, traffic toward the server comes from many client MACs and IPs. Source MAC or source IP gives good variety. Choose
src-dst-ipor a source-based option. - On the server, traffic goes toward many client IPs. A
layer2+3orlayer3+4policy gives variety. Chooselayer3+4, since clients may also use many ports. - After a day of load, compare transmit counters on both members at each end. If one member carries far more bytes, check whether a few clients dominate the traffic.
Now suppose the same server is reached only through a router. All frames toward the server carry the router's source MAC. A switch policy that uses only source MAC would put everything on one member. Switching to an IP-based or port-based policy fixes this.
Exercise
A two-link LAG connects a switch to a single router. The switch's hash is set to source MAC only, and all traffic from the router to the switch's downstream hosts uses one link. Why does this happen, and which change helps?
Show answer
Every frame arriving from the router has the router's MAC as its source, so the hash gets the same input every time and always picks the same member. Change the hash to include fields that vary, such as destination MAC, source and destination IP, or IPs plus ports. Note that the hash on the switch controls only the frames the switch transmits, so check which device is sending the unbalanced traffic before changing settings.
Exercise
Two hosts run one large TCP transfer over a LAG of two 1 Gbps links. The hash includes IP addresses and ports. What throughput can the single transfer reach, and how could you use both links?
Show answer
At most about 1 Gbps, because one TCP connection has fixed addresses and ports, so every packet hashes to the same member. To use both links, split the work across several connections with different source ports, for example a parallel transfer tool, and use a hash that includes Layer 4 ports.