Day 13: ECMP and Anycast
Goal: Two servers under the same IP address, both announced via OSPF. You see how the kernel spreads traffic across equal-cost paths and why the hash setting decides whether the spreading happens at all. And you learn the difference between a failure that anycast absorbs and one it does not even notice · Duration: approx. 75 min · Prerequisite: Day 10
Briefing
- ECMP (Equal-Cost Multi-Path): If there are several paths with equal cost to a destination, the router installs all of them and spreads the traffic. Not packet by packet, or TCP segments would arrive out of order, but per flow: a hash over the addresses (and, if you want, the ports) decides which path a flow gets.
- Anycast: The same address is announced from several places. Every router sends packets to the nearest one, and at equal distance to several via ECMP. This is how the DNS root servers, public resolvers like 1.1.1.1 and many load balancer platforms work.
- Anycast is routing, not a health check. The network knows whether the path to a server is up, not whether the service on it is running.
┌────┐ eth1 ┌──────┐
eth1 ─┤ r1 ├──────┤ srva │ lo 10.200.0.1
┌───┐ eth0 ┌───┐ ╱ └────┘ └──────┘
│ h ├───────┤ c │ (same address!)
└───┘ .10 └───┘ ╲ ┌────┐ eth1 ┌──────┐
10.50.0.0/24 eth2 ─┤ r2 ├──────┤ srvb │ lo 10.200.0.1
└────┘ └──────┘
c–r1 10.51.1.0/30, c–r2 10.51.2.0/30, r1–srva 10.52.1.0/24, r2–srvb 10.52.2.0/24
Drill 1: Two Servers, One Address
The anycast address sits on the loopback device of both servers. That
way each server answers packets to 10.200.0.1, no matter
which interface they arrive on.
# on netz
for n in h c r1 r2 srva srvb; do sudo ip netns add $n; sudo ip -n $n link set lo up; done
sudo ip -n c link add eth0 type veth peer name eth0 netns h
sudo ip -n c link add eth1 type veth peer name eth0 netns r1
sudo ip -n c link add eth2 type veth peer name eth0 netns r2
sudo ip -n r1 link add eth1 type veth peer name eth0 netns srva
sudo ip -n r2 link add eth1 type veth peer name eth0 netns srvb
sudo ip -n h addr add 10.50.0.10/24 dev eth0
sudo ip -n c addr add 10.50.0.1/24 dev eth0
sudo ip -n c addr add 10.51.1.1/30 dev eth1
sudo ip -n r1 addr add 10.51.1.2/30 dev eth0
sudo ip -n c addr add 10.51.2.1/30 dev eth2
sudo ip -n r2 addr add 10.51.2.2/30 dev eth0
sudo ip -n r1 addr add 10.52.1.1/24 dev eth1
sudo ip -n srva addr add 10.52.1.10/24 dev eth0
sudo ip -n r2 addr add 10.52.2.1/24 dev eth1
sudo ip -n srvb addr add 10.52.2.10/24 dev eth0
sudo ip -n srva addr add 10.200.0.1/32 dev lo
sudo ip -n srvb addr add 10.200.0.1/32 dev lo
for d in eth0 eth1 eth2; do sudo ip -n c link set $d up; done
for n in r1 r2; do sudo ip -n $n link set eth0 up; sudo ip -n $n link set eth1 up; done
for n in h srva srvb; do sudo ip -n $n link set eth0 up; done
sudo ip -n h route add default via 10.50.0.1
sudo ip -n srva route add default via 10.52.1.1
sudo ip -n srvb route add default via 10.52.2.1
for r in c r1 r2; do sudo ip netns exec $r sysctl -qw net.ipv4.ip_forward=1; done
sudo ip netns exec srva ip -br addr show loExpected:
lo UNKNOWN 127.0.0.1/8 10.200.0.1/32 ::1/128
Save your progress? Sign up for free
Drill 2: Announcing Anycast via OSPF
c, r1 and r2 speak OSPF as on
Day 10. What is new is the announcement: r1 and
r2 know the anycast address through a static route to their
server and pass it into OSPF with redistribute static. The
links to the servers themselves are not part of OSPF.
# on netz
for r in c r1 r2; do
sudo mkdir -p /etc/frr/$r
sudo sed 's/^ospfd=no/ospfd=yes/' /etc/frr/daemons | sudo tee /etc/frr/$r/daemons >/dev/null
sudo touch /etc/frr/$r/frr.conf /etc/frr/$r/vtysh.conf
sudo chown -R frr:frr /etc/frr/$r
sudo systemctl start frr@$r
done
sudo vtysh -N c -c 'configure terminal' -c 'router ospf' -c 'ospf router-id 10.50.0.1' -c 'exit' -c 'interface eth0' -c 'ip ospf area 0' -c 'ip ospf passive' -c 'exit' -c 'interface eth1' -c 'ip ospf area 0' -c 'ip ospf network point-to-point' -c 'exit' -c 'interface eth2' -c 'ip ospf area 0' -c 'ip ospf network point-to-point' -c 'end' -c 'write memory' >/dev/null
sudo vtysh -N r1 -c 'configure terminal' -c 'ip route 10.200.0.1/32 10.52.1.10' -c 'router ospf' -c 'ospf router-id 10.51.1.2' -c 'redistribute static' -c 'exit' -c 'interface eth0' -c 'ip ospf area 0' -c 'ip ospf network point-to-point' -c 'end' -c 'write memory' >/dev/null
sudo vtysh -N r2 -c 'configure terminal' -c 'ip route 10.200.0.1/32 10.52.2.10' -c 'router ospf' -c 'ospf router-id 10.51.2.2' -c 'redistribute static' -c 'exit' -c 'interface eth0' -c 'ip ospf area 0' -c 'ip ospf network point-to-point' -c 'end' -c 'write memory' >/dev/null
sleep 20
sudo vtysh -N c -c 'show ip ospf neighbor' | grep -c Full
sudo vtysh -N c -c 'show ip route 10.200.0.1/32'
sudo ip -n c route show 10.200.0.1Expected:
2
Routing entry for 10.200.0.1/32
Known via "ospf", distance 110, metric 20, best
Last update 00:00:05 ago
Flags: Selected
Status: Installed
* 10.51.1.2, via eth1, weight 1
* 10.51.2.2, via eth2, weight 1
10.200.0.1 nhid 16 proto ospf metric 20
nexthop via 10.51.1.2 dev eth1 weight 1
nexthop via 10.51.2.2 dev eth2 weight 1
One route, two next hops: ECMP. In the kernel it appears as a nexthop
group (nhid 16). In the OSPF database the address shows up
twice, as an external route (type 5), because it does
not originate in OSPF but was redistributed into it:
# on netz
sudo vtysh -N c -c 'show ip ospf database external' | grep -E 'Link State ID|Advertising|Forward|Metric Type|Metric:'Expected:
Link State ID: 10.200.0.1 (External Network Number)
Advertising Router: 10.51.1.2
Metric Type: 2 (Larger than any link state path)
Metric: 20
Forward Address: 0.0.0.0
Link State ID: 10.200.0.1 (External Network Number)
Advertising Router: 10.51.2.2
Metric Type: 2 (Larger than any link state path)
Metric: 20
Forward Address: 0.0.0.0
Tried it: In a first version, the server links were in OSPF as passive interfaces. After a failure and the return of the link to
srva,r1then announced the address withForward Address: 10.52.1.10, whiler2kept0.0.0.0. Forc, the path viar1was then more expensive, and ECMP did not come back. Keeping the server links outside OSPF keeps both announcements identical.
Save your progress? Sign up for free
Drill 3: The Hash Decides
On both servers, a web server that states its name:
# on netz
mkdir -p ~/anycast/a ~/anycast/b
echo "Antwort von srva" > ~/anycast/a/index.html
echo "Antwort von srvb" > ~/anycast/b/index.html
sudo ip netns exec srva sudo -u ubuntu python3 -m http.server 8080 --directory /home/ubuntu/anycast/a >/dev/null 2>&1 &
sudo ip netns exec srvb sudo -u ubuntu python3 -m http.server 8080 --directory /home/ubuntu/anycast/b >/dev/null 2>&1 &
sleep 1
sudo ip netns exec h curl -s http://10.200.0.1:8080/Expected: Antwort von srvb (reply from
srvb) (or srva)
Port 8080, because ports below 1024 are not allowed for the user
ubuntu. Now 20 requests:
# on netz
sudo ip netns exec c sysctl net.ipv4.fib_multipath_hash_policy
for i in $(seq 20); do sudo ip netns exec h curl -s http://10.200.0.1:8080/; done | sort | uniq -cExpected:
net.ipv4.fib_multipath_hash_policy = 0
20 Antwort von srvb
Two servers, but all 20 requests land on the same one. The default is
policy 0: the hash covers only source and destination
address (layer 3). From h to 10.200.0.1 that
is always the same pair, so always the same path. With many clients this
spreads out, with a single one never. ip route get predicts
which path any conceivable flow takes:
# on netz
for sp in 40001 40002 40003 40004; do sudo ip -n c route get 10.200.0.1 from 10.50.0.10 iif eth0 ipproto tcp sport $sp dport 8080 | head -1; doneExpected:
10.200.0.1 from 10.50.0.10 via 10.51.2.2 dev eth2
10.200.0.1 from 10.50.0.10 via 10.51.2.2 dev eth2
10.200.0.1 from 10.50.0.10 via 10.51.2.2 dev eth2
10.200.0.1 from 10.50.0.10 via 10.51.2.2 dev eth2
With policy 1 the kernel adds the ports (layer 4, five values: addresses, ports, protocol):
# on netz
sudo ip netns exec c sysctl -w net.ipv4.fib_multipath_hash_policy=1
for i in $(seq 20); do sudo ip netns exec h curl -s http://10.200.0.1:8080/; done | sort | uniq -c
for sp in 40001 40002 40003 40004; do sudo ip -n c route get 10.200.0.1 from 10.50.0.10 iif eth0 ipproto tcp sport $sp dport 8080 | head -1; doneExpected:
net.ipv4.fib_multipath_hash_policy = 1
10 Antwort von srva
10 Antwort von srvb
10.200.0.1 from 10.50.0.10 via 10.51.1.2 dev eth1
10.200.0.1 from 10.50.0.10 via 10.51.1.2 dev eth1
10.200.0.1 from 10.50.0.10 via 10.51.2.2 dev eth2
10.200.0.1 from 10.50.0.10 via 10.51.2.2 dev eth2
Every curl uses a new source port, every flow lands
somewhere else, 10 to 10. A connection still always stays on its path,
because its five values do not change. Policy 1 is the right choice for
routers in front of services. Policy 0 has its place when fragments are
involved: only the first fragment carries ports (Day 8), the others
would get a different hash with policy 1.
Save your progress? Sign up for free
Drill 4: The Nearest Server
Anycast means “to the nearest,” and nearest means shortest path. If
the link to r2 becomes more expensive, srva is
closer for c:
# on netz
sudo vtysh -N c -c 'configure terminal' -c 'interface eth2' -c 'ip ospf cost 100' -c 'end' >/dev/null
sleep 3
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep -E 'metric|\*'
for i in $(seq 20); do sudo ip netns exec h curl -s http://10.200.0.1:8080/; done | sort | uniq -c
sudo vtysh -N c -c 'configure terminal' -c 'interface eth2' -c 'no ip ospf cost' -c 'end' >/dev/null
sleep 3
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep '\*'Expected:
Known via "ospf", distance 110, metric 20, best
* 10.51.1.2, via eth1, weight 1
20 Antwort von srva
* 10.51.1.2, via eth1, weight 1
* 10.51.2.2, via eth2, weight 1
All 20 at srva. The metric stays at 20: external routes
of type 2 (E2) carry only the metric they were
redistributed with. The internal cost to the announcing router only
counts when these metrics are equal; then the closer one wins. In a
large network with sites in Frankfurt and Berlin, every client lands at
the server near it this way, without knowing about it.
Save your progress? Sign up for free
Drill 5: A Path Fails
The link from r1 to srva goes down:
# on netz
sudo ip -n r1 link set eth1 down
sleep 2
sudo vtysh -N r1 -c 'show ip route 10.200.0.1/32'
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep '\*'
for i in $(seq 20); do sudo ip netns exec h curl -s -m 1 http://10.200.0.1:8080/; done | sort | uniq -cExpected:
Routing entry for 10.200.0.1/32
Known via "ospf", distance 110, metric 20, best
Last update 00:00:24 ago
Flags: Selected
Status: Installed
* 10.51.1.1, via eth0, weight 1
* 10.51.2.2, via eth2, weight 1
20 Antwort von srvb
The static route on r1 becomes invalid as soon as its
next hop 10.52.1.10 is no longer reachable. r1
stops announcing the address and now only knows it itself via OSPF
through c to srvb. c has just one
path left, and all 20 requests succeed. Anycast absorbs the
failure of the path.
# on netz
sudo ip -n r1 link set eth1 up
sleep 5
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep '\*'Expected:
* 10.51.1.2, via eth1, weight 1
* 10.51.2.2, via eth2, weight 1
Save your progress? Sign up for free
Drill 6: A Service Fails
Now the link is up, but the web server on srva dies. The
pattern with ^python3 matches only the Python process, not
your own shell (Day 7):
# on netz
for p in $(pgrep -f '^python3 -m http.server 8080 --directory /home/ubuntu/anycast/a'); do sudo kill $p; done
for i in $(seq 20); do sudo ip netns exec h curl -s -m 1 -o /dev/null -w '%{http_code}\n' http://10.200.0.1:8080/; done | sort | uniq -cExpected:
8 000
12 200
Eight out of twenty requests fail (000 = no connection).
For the network everything is fine: srva is reachable, the
route is up, only nobody answers on port 8080. The hash keeps sending
part of the flows there. Anycast does not notice the failure of
the service.
The solution is a health check that withdraws the announcement when the service stops answering. In production a small watchdog does this (a script, ExaBGP, the health checks of a load balancer). Here by hand:
# on netz
sudo vtysh -N r1 -c 'configure terminal' -c 'no ip route 10.200.0.1/32 10.52.1.10' -c 'end' -c 'write memory' >/dev/null
sleep 2
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep '\*'
for i in $(seq 20); do sudo ip netns exec h curl -s -m 1 -o /dev/null -w '%{http_code}\n' http://10.200.0.1:8080/; done | sort | uniq -cExpected:
* 10.51.2.2, via eth2, weight 1
20 200
And back, first the service, then the announcement. The other way
around, srva would get traffic before it is ready:
# on netz
sudo ip netns exec srva sudo -u ubuntu python3 -m http.server 8080 --directory /home/ubuntu/anycast/a >/dev/null 2>&1 &
sleep 1
sudo vtysh -N r1 -c 'configure terminal' -c 'ip route 10.200.0.1/32 10.52.1.10' -c 'end' -c 'write memory' >/dev/null
sleep 3
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep '\*'
for i in $(seq 20); do sudo ip netns exec h curl -s http://10.200.0.1:8080/; done | sort | uniq -cExpected:
* 10.51.1.2, via eth1, weight 1
* 10.51.2.2, via eth2, weight 1
11 Antwort von srva
9 Antwort von srvb
Save your progress? Sign up for free
Exercise: Speed drills
- How many next hops does
chave in the kernel for10.200.0.1? Just the number. Target time: 20 s - Which hash policy is currently in effect in
c? Target time: 15 s - Which routers announce
10.200.0.1? Target time: 30 s
Solution
# on netz
sudo ip -n c route show 10.200.0.1 | grep -c nexthop
sudo ip netns exec c sysctl -n net.ipv4.fib_multipath_hash_policy
sudo vtysh -N c -c 'show ip ospf database external 10.200.0.1' | grep AdvertisingExpected:
2
1
Advertising Router: 10.51.1.2
Advertising Router: 10.51.2.2
Save your progress? Sign up for free
Challenge
Operations wants to get rid of the spreading: srva
should get everything, srvb should only step in when the
path to srva fails. Change exactly one router to do this
and prove both: the preference and the stepping in.
Hint: OSPF compares external routes of type 2 by their metric first.
Solution
r2 announces the address with a worse metric:
# on netz
sudo vtysh -N r2 -c 'configure terminal' -c 'router ospf' -c 'redistribute static metric 100' -c 'end' -c 'write memory' >/dev/null
sleep 3
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep -E 'metric|\*'
sudo vtysh -N c -c 'show ip ospf database external' | grep -E 'Advertising|Metric:'
for i in $(seq 20); do sudo ip netns exec h curl -s http://10.200.0.1:8080/; done | sort | uniq -cExpected:
Known via "ospf", distance 110, metric 20, best
* 10.51.1.2, via eth1, weight 1
Advertising Router: 10.51.1.2
Metric: 20
Advertising Router: 10.51.2.2
Metric: 100
20 Antwort von srva
The stepping in:
# on netz
sudo ip -n r1 link set eth1 down
sleep 2
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep -E 'metric|\*'
for i in $(seq 20); do sudo ip netns exec h curl -s -m 1 http://10.200.0.1:8080/; done | sort | uniq -c
sudo ip -n r1 link set eth1 up
sleep 5
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep -E 'metric|\*'Expected:
Known via "ospf", distance 110, metric 100, best
* 10.51.2.2, via eth2, weight 1
20 Antwort von srvb
Known via "ospf", distance 110, metric 20, best
* 10.51.1.2, via eth1, weight 1
The same principle as primary and backup link on Day 12, just with OSPF instead of BGP. If you announce with BGP, you use AS path prepending or MED for this.
Save your progress? Sign up for free
Checkpoint
# on netz
sudo vtysh -N c -c 'show ip route 10.200.0.1/32' | grep -c '\*'
sudo grep 'redistribute static' /etc/frr/r1/frr.conf /etc/frr/r2/frr.conf
sudo ip netns exec h curl -s http://10.200.0.1:8080/Expected:
1
/etc/frr/r1/frr.conf: redistribute static
/etc/frr/r2/frr.conf: redistribute static metric 100
Antwort von srva
Clean-up
# on netz
for p in $(pgrep -f '^python3 -m http.server 8080'); do sudo kill $p; done
sudo systemctl stop frr@c frr@r1 frr@r2
sudo rm -rf /etc/frr/c /etc/frr/r1 /etc/frr/r2
for n in h c r1 r2 srva srvb; do sudo ip netns del $n; done
rm -rf ~/anycast
ip netns list | wc -l
pgrep -af '^python3' || echo "keine Labor-Prozesse"Expected:
0
keine Labor-Prozesse
Practice note
fib_multipath_hash_policy=1on routers in front of services. With the default 0, ECMP spreads only by address, and a single large client (a proxy, a NAT gateway) always lands on the same path.- ECMP spreads flows, not load. One flow at 10 Gbit/s and nine small ones do not share two paths evenly. For real load balancing by utilization you need a load balancer.
- Anycast only with a health check. The announcement must be tied to the service, not to the machine. Otherwise a server with a dead service swallows its share of the traffic, and nobody in the network notices.
- When the number of paths changes, the hash changes.
When a server comes back, some existing flows are moved to it, and their
TCP connections break because it does not know them. For long-lived
connections, resilient nexthop groups
(
ip nexthop … type resilient) help, which move only as many flows as necessary, or a load balancer with connection state.