Sub topic 4
Choosing a load-balancing hash
This chapter assumes you know how a port channel bundles member links and why a single flow stays on one member. You should also be comfortable reading basic TCP/UDP port and IP address fields.
What the hash does
A switch cannot send each frame down whichever member link is free. Frames of one conversation could then arrive out of order. Instead, it picks header fields from each frame, combines them into a number (a hash), and maps that number to a member link. Every frame with the same field values takes the same link.
The hash policy is the list of fields that feed this calculation. You choose it, so you control how evenly traffic spreads.
Common hash inputs
| Policy | Fields used | Spreads well when |
|---|---|---|
| src-mac, dst-mac | Ethernet addresses | Many different hosts on the same VLAN |
| src-ip, dst-ip | IP addresses | Many different hosts across subnets |
| src-dst-ip | Both IP addresses together | Many source and destination pairs |
| Layer 3+4 | IPs plus TCP/UDP ports | Few hosts but many connections |
Available options vary by platform. Some switches also offer source-only or destination-only variants.
Matching the policy to the traffic
Look at what actually crosses the bundle.
- Switch to a router. All frames from remote hosts carry the router's MAC address as the source. A MAC-based hash then sees one pair and uses one link. Use an IP-based policy.
- Server to one backup target. Two IP addresses make one pair, so IP-only hashing pins everything to one link. Add ports so each connection hashes separately.
- Many clients to one server. Source IP hashing works, because the client addresses vary.
Adding ports gives the best spread, but it needs a switch that can read Layer 4 fields. It also means the fragments of a fragmented IP packet may carry no port numbers. Some platforms then hash them differently from the first fragment.
Seeing the effect
This script models the idea. A fixed flow always lands on one member, and many flows with different source ports spread out.
import zlib
from collections import Counter
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
# One flow: always the same member.
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))
# Many flows between the same two hosts, varying only the source port.
counts = Counter(
pick_member("10.0.0.5", "10.0.1.9", port, 443, "tcp", 2)
for port in range(50000, 50100)
)
print(counts)If you hashed only the two IP addresses, all 100 flows would land on one member. Including the ports is what spreads them.
Setting and checking the policy
On a Cisco IOS style switch, the hash is usually a global setting that applies to every port channel on the switch.
port-channel load-balance src-dst-ip
!
show etherchannel load-balanceThe second command prints the active policy. On Linux, the equivalent is the xmit_hash_policy option, for example layer3+4, as in the bond example from the Linux chapter.
Limits to remember
- Hashing balances flows, not bytes. One large flow can still fill one link while another is idle.
- With few flows, the spread can be uneven by chance.
- A member count that is not a power of two can distribute unevenly on some hardware.
- Each switch hashes its own outgoing traffic. The two ends of a bundle can use different policies, so check both.
Exercise
Two file servers exchange backups over a two-link port channel. Only one pair of IP addresses is involved, but there are many parallel TCP connections. Which hash policy should you choose, and why would src-dst-ip be a poor fit?
Show answer
Choose a policy that includes Layer 4 ports (Layer 3+4). With only one IP pair, src-dst-ip produces the same hash for every connection, so all traffic uses one member link. Adding the TCP ports gives each connection its own hash, so connections spread across both links.