Skip to contentSkip to conversation

vxlan: reset the remote's dst cache when snooping moves a MAC

PATCH Superseded Zhefu Peng 2 participants 6 messages Nov 15 – Nov 19

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Zhefu Peng

author
#0 lore raw

v5 cover vxlan: reset the remote's dst cache when snooping moves a MAC

When a learning VXLAN device snoops a known MAC from another VTEP, it rewrites the remote's IP address in place but keeps the remote's dst_cache and remote_ifindex. Traffic to the moved MAC can then use the route cached for the previous remote and be blackholed. Patch 1 updates the remote the way vxlan_fdb_replace() does, and compares remote_ifindex in the early return only for link-local sources, so a "via" device set on a dynamic entry is kept. Patch 2 adds a selftest.

Results on net with 1, 2 and 4 CPUs, per selftest case (move behind a router, move to link-local, via kept):
- without patch 1: 0/8, 0/8, 8/8
- with v4 of patch 1: 8/8, 8/8, 0/8
- with this version: 16/16, 16/16, 16/16

v5:
- compare remote_ifindex in the early return only for link-local sources, so traffic from the same remote no longer resets a "via" device to 0 (Sashiko)
- selftest: add a case for the above and keep lines within 80 columns

v4: https://lore.kernel.org/netdev/[email protected]/
- add the selftest (Nicolas Dichtel)

v3: https://lore.kernel.org/netdev/[email protected]/
- use my real name in From: and Signed-off-by:

v2: https://lore.kernel.org/netdev/[email protected]/
- also update remote_ifindex and clear offloaded, as vxlan_fdb_replace() does
- add the IPv6 link-local case to the changelog and a second Fixes tag

v1: https://lore.kernel.org/netdev/[email protected]/

Zhefu Peng (2):
vxlan: reset the remote's dst cache when snooping moves a MAC
selftests: net: add a test for vxlan snooping moving a MAC

 drivers/net/vxlan/vxlan_core.c                |   5 +-
 tools/testing/selftests/net/Makefile          |   1 +
 .../selftests/net/test_vxlan_snoop_move.sh    | 199 ++++++++++++++++++
 3 files changed, 204 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/net/test_vxlan_snoop_move.sh
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH v5 1/2 vxlan: reset the remote's dst cache when snooping moves a MAC

vxlan_snoop() handles a known MAC that now arrives from another remote by rewriting the remote's IP address in place. It leaves the remote's dst_cache alone, so the next packet to that MAC is routed with the dst cached for the previous remote. When the two remotes have different next hops, the packet leaves the wrong way. For example, if the previous remote was on-link and the new one is behind a router, the device resolves the new remote's address on the local link, the neighbour stays incomplete, and traffic to the MAC is blackholed until the cached route is invalidated, such as by "ip route flush cache".

The same branch also leaves remote_ifindex unchanged, although the early return compares it and vxlan_xmit_one() uses it as the output interface. When a MAC moves between an IPv6 link-local and another address, the remote keeps the previous interface, and every later packet from that remote enters the migration branch again.

vxlan_fdb_replace(), which makes the same change through netlink, already resets the cache, updates remote_ifindex and clears offloaded. Do the same when snooping.

vxlan_snoop() knows the interface only for IPv6 link-local sources and uses 0 for all others, but the early return compares remote_ifindex for every source. A packet from the remote of a dynamic entry that was added with a "via" device therefore enters the migration branch as well, which would now reset the device to 0. Compare remote_ifindex only for link-local sources.

The selftest added in the next patch moves a MAC from an on-link IPv4 remote to one behind a router, and from a global IPv6 remote to a link-local one. Both cases fail without this patch and pass with it. A third case checks that the "via" device of a dynamic entry stays set when its remote sends traffic.

This was found by our AI-driven automated live migration tests. The fix and this changelog were written with the help of an AI coding assistant.

Fixes 0c1d70af924b ("net: use dst_cache for vxlan device")
Fixes 87613de95041 ("vxlan: fix snooping for link-local IPv6 addresses")
---
 drivers/net/vxlan/vxlan_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/vxlan/vxlan_core.c +4 -1
index d11028b37..1cfb34fa2 100644
@@ -1470,7 +1470,7 @@ static enum skb_drop_reason vxlan_snoop(struct net_device *dev,
 			return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
 
 		if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
-			   rdst->remote_ifindex == ifindex))
+			   (!ifindex || rdst->remote_ifindex == ifindex)))
 			return SKB_NOT_DROPPED_YET;
 
 		/* Don't migrate static entries, drop packets */
@@ -1482,7 +1482,10 @@ static enum skb_drop_reason vxlan_snoop(struct net_device *dev,
 				    "%pM migrated from %pIS to %pIS\n",
 				    src_mac, &rdst->remote_ip.sa, &src_ip->sa);
 
+		dst_cache_reset(&rdst->dst_cache);
 		rdst->remote_ip = *src_ip;
+		rdst->remote_ifindex = ifindex;
+		rdst->offloaded = false;
 		vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
 	} else {
 		/* learned new entry */
Signature · 1 line
2.54.0
#2 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The new dst_cache_reset() in vxlan_snoop() does not fully stop a stale route from being cached for the new remote.
- [Medium] vxlan_snoop() now writes two dependent fields, rdst->remote_ip and rdst->remote_ifindex, as separate plain stores with no hash_lock.

--

vxlan: reset the remote's dst cache when snooping moves a MAC

When a learned MAC migrates to a new remote, vxlan_snoop() now resets the remote's dst_cache, updates remote_ifindex and clears offloaded. This matches what vxlan_fdb_replace() already does. The early return also changes so that remote_ifindex is only compared for IPv6 link-local sources.

Quoted 14 lines
drivers/net/vxlan/vxlan_core.c +1 -1
index d11028b374721..1cfb34fa23604 100644
@@ -1470,7 +1470,7 @@ static enum skb_drop_reason vxlan_snoop(struct net_device *dev,
 			return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
 
 		if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
-			   rdst->remote_ifindex == ifindex))
+			   (!ifindex || rdst->remote_ifindex == ifindex)))
 			return SKB_NOT_DROPPED_YET;
 
 		/* Don't migrate static entries, drop packets */

drivers/net/vxlan/vxlan_core.c static enum skb_drop_reason vxlan_snoop(struct net_device *dev, · line 1486

Quoted 5 lines
drivers/net/vxlan/vxlan_core.c +1 -0
@@ -1482,7 +1482,10 @@ static enum skb_drop_reason vxlan_snoop(struct net_device *dev,
 				    "%pM migrated from %pIS to %pIS\n",
 				    src_mac, &rdst->remote_ip.sa, &src_ip->sa);
 
+		dst_cache_reset(&rdst->dst_cache);
 		rdst->remote_ip = *src_ip;

[Severity: Medium]
Can a transmitter on another CPU still cache the route to the old remote after this reset?

vxlan_snoop() holds only RCU here. vxlan_xmit_one() reads the same fields without a lock and then uses the cache:

drivers/net/vxlan/vxlan_core.c:vxlan_xmit_one() {
    ...
		ifindex = rdst->remote_ifindex;
    ...
			key.u.ipv4.dst = rdst->remote_ip.sin.sin_addr.s_addr;
    ...
		dst_cache = &rdst->dst_cache;
    ...
}

dst_cache_reset() only stores jiffies in reset_ts. A cached entry is checked against that timestamp at jiffy granularity only:

net/core/dst_cache.c:dst_cache_per_cpu_get() {
    ...
	if (unlikely(!time_after(idst->refresh_ts,
				 READ_ONCE(dst_cache->reset_ts)) ||
    ...
fail:
	idst->refresh_ts = jiffies;
	return NULL;
}

On a cache hit, udp_tunnel_dst_lookup() returns the dst without comparing it to key->u.ipv4.dst.

Consider this interleaving:

CPU1 (vxlan_xmit_one) CPU0 (vxlan_snoop)
reads old rdst->remote_ip
dst_cache_reset() at jiffy J rdst->remote_ip = *src_ip
dst_cache_get() at J+1, misses,
refresh_ts = J+1
looks up route to old remote,
caches it

After that, every transmit on CPU1 to this MAC uses the old remote's dst with the new remote's address. Later packets from the new remote take the early return, so snooping never resets the cache again.

Is that the same blackhole the commit message describes, just reached through a race? Reordering the stores, even with smp_wmb(), doesn't seem to close the window. vxlan_fdb_replace() does the same reset followed by in-place stores, so it has the same window.

drivers/net/vxlan/vxlan_core.c static enum skb_drop_reason vxlan_snoop(struct net_device *dev, · line 1487

Quoted 1 line
drivers/net/vxlan/vxlan_core.c +1 -0
+		rdst->remote_ifindex = ifindex;

[Severity: Medium]
Can concurrent snoops of the same MAC leave remote_ip and remote_ifindex inconsistent?

These are two plain stores with no hash_lock. RX for one VXLAN device can run on several CPUs at once, for example:

CPU1: snoop from fe80::1 on ifindex 5
CPU0: snoop from 2001:db8::1, ifindex 0

If the stores interleave, the entry can end up as {remote_ip = 2001:db8::1, remote_ifindex = 5}.

With the new early-return check:

	(!ifindex || rdst->remote_ifindex == ifindex)

later packets from 2001:db8::1 return early, so the stale ifindex is never repaired. f->updated also keeps being refreshed, so the entry doesn't age out.

vxlan_xmit_one() then uses ifindex 5 as the output interface for the global address. If that address isn't routed via device 5, the route lookup fails and traffic to the MAC is dropped.

This looks like the state the commit message says it fixes ("the remote keeps the previous interface"). vxlan_fdb_replace(), which writes these fields under hash_lock, can mix with this lockless snoop in the same way.

Quoted 4 lines
drivers/net/vxlan/vxlan_core.c +1 -0
+		rdst->offloaded = false;
 		vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
 	} else {
 		/* learned new entry */
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261115040609.61134-1-ilskdw%40gmail.com
author
#3 lore raw
Quoted 3 lines

[Severity: Medium]
Can a transmitter on another CPU still cache the route to the old remote after this reset?

Yes, as answered for v2: dst_cache tracks freshness in jiffies, so a transmit that built its key before the reset can refill the cache in a later jiffy. vxlan_fdb_replace() has the same window. Closing it means replacing the remote, or its dst_cache, through RCU instead of updating it in place, which would also change vxlan_fdb_replace(). I would rather do that, if wanted, as a separate net-next change. Without this patch every move uses the stale route; with it only this race does.

Quoted 3 lines

[Severity: Medium]
Can concurrent snoops of the same MAC leave remote_ip and remote_ifindex inconsistent?

Yes. v6 updates the remote under vxlan->hash_lock and checks the entry again once the lock is held, so concurrent snoops are serialized with each other and with vxlan_fdb_replace(). v6 also addresses the three notes on patch 2.

pw-bot: cr

author
#4 lore raw

PATCH v5 2/2 selftests: net: add a test for vxlan snooping moving a MAC

In reply to Zhefu Peng

Add a test for a learning VXLAN device that snoops a known MAC.

The first case moves the MAC from an on-link IPv4 remote to a remote behind a router, and checks that traffic to the MAC still flows. This fails while the device keeps the route cached for the previous remote.

The second case moves the MAC from a global IPv6 remote to a link-local one, and checks that the FDB entry records the new output interface.

The third case adds a dynamic entry with a "via" device, and checks that traffic from the entry's remote leaves the device in place.

The first two cases ping from CPU 0. The route cache is per CPU, and only the CPU that filled it hits the stale entry.

This test was written with the help of an AI coding assistant.

---
 tools/testing/selftests/net/Makefile          |   1 +
 .../selftests/net/test_vxlan_snoop_move.sh    | 199 ++++++++++++++++++
 2 files changed, 200 insertions(+)
 create mode 100755 tools/testing/selftests/net/test_vxlan_snoop_move.sh
tools/testing/selftests/net/Makefile +1 -0
index baea3e7f0..69d440c5b 100644
@@ -118,6 +118,7 @@ TEST_PROGS := \
 	test_vxlan_mdb.sh \
 	test_vxlan_nh.sh \
 	test_vxlan_nolocalbypass.sh \
+	test_vxlan_snoop_move.sh \
 	test_vxlan_under_vrf.sh \
 	test_vxlan_vnifilter_notify.sh \
 	test_vxlan_vnifiltering.sh \
tools/testing/selftests/net/test_vxlan_snoop_move.sh new file +199 -0

199 lines not on this page · raw message

Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH net v5 2/2] selftests: net: add a test for vxlan snooping moving a MAC

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] ping_from_cpu0() in tools/testing/selftests/net/test_vxlan_snoop_move.sh always pins to…
- [Low] test_move_to_link_local() pins its pings to CPU 0.
- [Low] test_move_behind_router (line 48), test_move_to_link_local (line
105) and test_same_remote_keeps_via (line 159) ignore the return value…

--

selftests: net: add a test for vxlan snooping moving a MAC

This adds test_vxlan_snoop_move.sh, with three cases for a learning VXLAN device that snoops a known MAC. The first moves the MAC from an IPv4 remote to one behind a router. The second moves it from a global IPv6 remote to a link-local one. The third checks that a dynamic entry keeps its "via" device. The first two cases pin their pings to CPU 0 because the route cache is per CPU.

Quoted 6 lines
tools/testing/selftests/net/test_vxlan_snoop_move.sh new file +0 -0
new file mode 100755
index 0000000000000..5b78310439d3a
@@ -0,0 +1,199 @@

[ ... ]

tools/testing/selftests/net/test_vxlan_snoop_move.sh line 31

Quoted 6 lines
tools/testing/selftests/net/test_vxlan_snoop_move.sh new file +6 -0
+ping_from_cpu0()
+{
+	local ns=$1; shift
+
+	taskset -c 0 ip netns exec "$ns" ping -c 3 -W 1 -i 0.2 "$@" &> /dev/null
+}

[Severity: Low]
What happens here when CPU 0 is not in the allowed CPU set? That can happen under a cpuset cgroup that excludes CPU 0, or when CPU 0 is offline.

In that case sched_setaffinity() fails with EINVAL, taskset exits non-zero and ping never runs. That status goes straight to check_err in test_move_behind_router() and test_move_to_link_local(). Both cases then report "ping before the move failed" on a working kernel instead of skipping.

The require_command taskset near the end of the script only checks that the binary is installed. It doesn't check that CPU 0 is usable.

The pinning only needs one CPU to fill the per-CPU dst_cache and then hit it. Could this pick any CPU from the current affinity mask, or skip when CPU 0 can't be used? srv6_iptunnel_cache.sh hard-codes taskset -c 0 the same way.

[ ... ]

tools/testing/selftests/net/test_vxlan_snoop_move.sh line 49

Quoted 6 lines
tools/testing/selftests/net/test_vxlan_snoop_move.sh new file +6 -0
+test_move_behind_router()
+{
+	RET=0
+
+	setup_ns sw ex a r b
+	defer cleanup_ns "$sw" "$ex" "$a" "$r" "$b"

[Severity: Low]
Should the return value of setup_ns be checked here?

In lib.sh, setup_ns() returns $ksft_skip when ip netns add fails. This test keeps going anyway, so every later ip -n and bridge -n command fails. The case then reports "ping before the move failed" instead of SKIP.

The setup_ns calls in test_move_to_link_local() and test_same_remote_keeps_via() have the same problem. The second one reports "ping from the remote failed".

Most net selftests don't check setup_ns either, though bareudp.sh, fou_gre_gro.sh and nsh_gso.sh do. So this matches common practice, but the result is still reported wrongly.

[ ... ]

Quoted 2 lines
tools/testing/selftests/net/test_vxlan_snoop_move.sh new file +2 -0
+test_move_to_link_local()
+{

[ ... ]

Quoted 9 lines
tools/testing/selftests/net/test_vxlan_snoop_move.sh new file +9 -0
+	ip -n "$ex" addr add 2001:db8:1::1/64 dev e0 nodad
+	ip -n "$ex" addr add fe80::1/64 dev e0 nodad
+	ip -n "$a" addr add 2001:db8:1::2/64 dev a0 nodad
+	ip -n "$b" addr add fe80::b/64 dev b0 nodad
+
+	ip -n "$ex" link add vx0 type vxlan id 42 dstport 4789 \
+		local 2001:db8:1::1 learning
+	bridge -n "$ex" fdb append 00:00:00:00:00:00 dev vx0 dst 2001:db8:1::2
+	bridge -n "$ex" fdb append 00:00:00:00:00:00 dev vx0 dst fe80::b via e0

[ ... ]

tools/testing/selftests/net/test_vxlan_snoop_move.sh line 150

Quoted 12 lines
tools/testing/selftests/net/test_vxlan_snoop_move.sh new file +12 -0
+	ping_from_cpu0 "$ex" 192.168.0.10
+	check_err $? "ping before the move failed"
+
+	ip -n "$a" link set vx0 down
+	ip -n "$b" link set vx0 up
+	ip netns exec "$b" ping -c 1 -W 1 192.168.0.1 &> /dev/null
+
+	bridge -n "$ex" fdb show dev vx0 | grep -q "$MAC dst fe80::b via e0 "
+	check_err $? "fdb entry did not move to the new remote and interface"
+
+	ping_from_cpu0 "$ex" 192.168.0.10
+	check_err $? "ping after the move failed"

[Severity: Low]
Can this post-move ping tell a stale IPv6 dst_cache from a reset one?

The commit message says:

The first two cases ping from CPU 0. The route cache is per CPU, and only the CPU that filled it hits the stale entry.

In this topology the old remote (2001:db8:1::2) and the new remote (fe80::b) are both on-link through e0 in $ex. The pre-move ping fills CPU 0's rdst->dst_cache with a non-gateway on-link route on e0. Nothing changes the routes in $ex between the two pings.

udp_tunnel6_dst_lookup() returns the cached dst without checking oif or daddr:

net/ipv6/ip6_udp_tunnel.c:udp_tunnel6_dst_lookup() {
    ...
		dst = dst_cache_get_ip6(dst_cache, saddr);
		if (dst)
			return dst;
    ...
}

On an on-link route the next hop is the packet's own daddr. So the stale dst would deliver to fe80::b just as a fresh lookup would.

Suppose the dst_cache_reset(&rdst->dst_cache) call in vxlan_snoop() were removed but the remote_ifindex update were kept. Wouldn't this case still pass? It looks like only the "via e0" grep tests anything here, and the CPU 0 pinning has no effect.

The IPv4 case still covers dst_cache_reset() itself. Would giving the old and new IPv6 remotes different egress paths cover the IPv6 cache path? One option is to put one of them behind a router, as in the first case. Otherwise, could the commit message be reworded so it doesn't suggest the second case covers stale routes?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261115040609.61134-1-ilskdw%40gmail.com