Skip to content

ECMP routing on Linux: multipath, hash policy, and per-flow load distribution

How Linux distributes traffic across equal-cost multipath routes, the hash algorithms that decide which flow takes which path, and the operational levers that keep flows pinned.

ECMP (Equal-Cost Multipath) lets the kernel pick between several equally-good next-hops for a single destination prefix. It is how Linux routers split traffic across two ISPs, how BGP-driven leaf-spine fabrics scale, and how Calico/Cilium spread pod traffic across multiple host paths. The hard part is not setting it up — ip route add with multiple nexthop arguments takes one minute — but understanding which flows land on which path, why a flow re-hashes when a path goes away, and how fib_multipath_hash_policy changes the answer. This article walks through the model, the hash policies, and the operational levers.

How to verify

Inspect ECMP routes and current hash policy:

ip route show
ip route get 1.1.1.1
sysctl net.ipv4.fib_multipath_hash_policy
sysctl net.ipv4.fib_multipath_use_neigh
ip -s route show 0.0.0.0/0
ip nexthop list
ip route get 1.1.1.1 from 10.0.0.5 dport 443 sport 12345 ipproto tcp

ip route get with the full 5-tuple is the single most useful debugging command: it tells you exactly which path a hypothetical flow would take, given the current hash policy. Run it on both ends to confirm symmetry.

What’s happening

When a route has multiple nexthop entries with equal weight, the kernel computes a hash of selected packet fields and uses it to index into the next-hop list. The hash is stable per-flow — every packet of the same flow takes the same path, which is what TCP needs. The fields included in the hash are controlled by net.ipv4.fib_multipath_hash_policy:

  • 0 (default): hash on source IP + destination IP only (L3 hash)
  • 1: hash on source IP + destination IP + source port + destination port + IP protocol (L4 hash)
  • 2: hash on inner-packet headers when the outer is a tunnel (GRE, VXLAN encap-aware)
  • 3: hash on a mix including flow-label for IPv6

For IPv6, the equivalent sysctl is net.ipv6.fib_multipath_hash_policy.

The implication is that L3-only hashing fans out traffic by IP pair — two clients each behind NAT to the same source IP will pick the same path. L4 hashing fans out by 5-tuple, which is what most production deployments want for routers facing many small flows.

When a path goes away, every flow on that path re-hashes against the remaining set. With basic ECMP, this means flows that were on surviving paths can also move — bad for stateful middleboxes. Consistent hashing (fib_multipath_use_neigh=1 plus kernel ≥ 4.4 nexthop objects) keeps surviving flows where they were and only re-hashes the ones from the dead path. This requires the kernel ip nexthop object model, not classic ip route.

The procedure

  1. Add a basic ECMP default route across two uplinks.

    ip route add default \
      nexthop via 192.0.2.1   dev eth0 weight 1 \
      nexthop via 198.51.100.1 dev eth1 weight 1

    With weight 1 on each, traffic splits 50/50 (on average). For asymmetric WANs, set weights proportionally (e.g., weight 3 and weight 1 for a 3:1 split).

  2. Enable L4 hashing. Without this, flows from the same client IP all land on one path:

    sysctl -w net.ipv4.fib_multipath_hash_policy=1
    sysctl -w net.ipv6.fib_multipath_hash_policy=1

    Persist:

    cat > /etc/sysctl.d/65-ecmp.conf <<'EOF'
    net.ipv4.fib_multipath_hash_policy = 1
    net.ipv6.fib_multipath_hash_policy = 1
    EOF
  3. Use nexthop objects for consistent hashing. Modern kernels (≥ 4.19) support the ip nexthop object model that survives next-hop liveness changes more gracefully:

    ip nexthop add id 1 via 192.0.2.1 dev eth0
    ip nexthop add id 2 via 198.51.100.1 dev eth1
    ip nexthop add id 100 group 1/2
    ip route add default nhid 100

    This separates next-hop definition from route attachment, and the kernel uses a more stable hash when group members change.

  4. Set use_neigh to drop dead paths automatically. When the ARP/ND entry for a next-hop transitions to FAILED, the kernel excludes it from the ECMP group:

    sysctl -w net.ipv4.fib_multipath_use_neigh=1

    This relies on arp_accept and gc_thresh defaults being sane; on a heavily loaded box you may need to raise net.ipv4.neigh.default.gc_thresh3.

  5. Verify the distribution. Trigger 1000 distinct synthetic flows and check the FIB counters:

    for i in $(seq 1 1000); do
      curl -s --interface 10.0.0.5 -o /dev/null https://203.0.113.$((RANDOM % 254)):443/
    done
    ip -s -s link show eth0
    ip -s -s link show eth1

    Counters on the two egress NICs should be roughly equal (within 5% on a fast box).

  6. For BGP-driven ECMP (data centre fabrics), enable multipath in FRR.

    router bgp 65001
     maximum-paths 4
     maximum-paths ibgp 4

    Combined with bgp bestpath as-path multipath-relax, this lets FRR install 4-way ECMP from multiple ToR uplinks.

Operational notes

  • Per-flow ECMP is not equal-bytes ECMP. A single 10 Gbps elephant flow pins one path; nine other 100 Mbps flows on the other path do not balance it. For elephant-flow problems, consider per-packet ECMP — which Linux does not do by default because it breaks TCP — or DC-style flowlet switching, which requires control-plane signalling.
  • MTU must match across all ECMP paths or you get black-hole behaviour for large packets on the lower-MTU path. Path MTU discovery sometimes recovers; sometimes it does not.
  • NAT/SNAT downstream of ECMP changes the hash. If your router does outbound SNAT after ECMP, the upstream router sees a single source IP and hashes accordingly — which may collapse all your flows onto one path. Choose where the NAT happens carefully.
  • VXLAN/GRE outer headers hash by the underlay 5-tuple. Inside a tunnel, all flows share the same outer src/dst/UDP-port (because the outer source port is derived per-tunnel, not per-inner-flow), so the underlay ECMP hashes them all onto one path. Mitigation: enable VXLAN-encapsulated UDP source-port hashing on the encapsulator.
  • ip route show truncates next-hop lists when there are many; use ip -d route show for the full output, and ip nexthop list for the modern object model.

In the engagements we run, ECMP is everywhere — dual-WAN routers, BGP-driven leaf-spine fabrics, Cilium BPF-mode kubelets — and the configuration above is the canonical shape. We default to L4 hashing, nexthop objects for groups that change membership, and BFD on every BGP peering so a dead path leaves the ECMP set within a second. The pattern lives in the same Ansible role library used by the managed-operations practice, alongside BGP and OSPF configs.