Skip to contentSkip to conversation

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

PATCH In review Zhefu Peng 2 participants 5 messages Nov 19 – Nov 23

Subsystems (3)
lore
Share & tools
lore

Zhefu Peng

author
#0 lore raw

v6 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, under vxlan->hash_lock, 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/7, 0/7, 7/7
- with v5 of patch 1: 7/7, 7/7, 7/7
- with this version: 14/14, 14/14, 14/14
- with this version and CONFIG_PROVE_LOCKING: 7/7 for each case, no lockdep splats

v6:
- update the remote under vxlan->hash_lock and check the entry again once the lock is held, so concurrent snoops cannot mix remote_ip and remote_ifindex (Sashiko)
- selftest: pin pings to an allowed CPU instead of CPU 0, skip when setup_ns fails, and drop the CPU pinning from the IPv6 case, which does not test the route cache (Sashiko)

v5: https://lore.kernel.org/netdev/[email protected]/
- 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                |  59 ++++-
 tools/testing/selftests/net/Makefile          |   1 +
 .../selftests/net/test_vxlan_snoop_move.sh    | 222 ++++++++++++++++++
 3 files changed, 274 insertions(+), 8 deletions(-)
 create mode 100755 tools/testing/selftests/net/test_vxlan_snoop_move.sh
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH v6 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.

Two CPUs can snoop the same MAC from different remotes at the same time, and the plain stores to remote_ip and remote_ifindex can then mix, for example a global address with the interface of a link-local one. With the early return above, such an entry is never repaired. Update the remote under vxlan->hash_lock, which vxlan_fdb_replace() and the other writers of these fields hold, and check the entry again once the lock is held.

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 | 59 +++++++++++++++++++++++++++++-----
 1 file changed, 51 insertions(+), 8 deletions(-)
drivers/net/vxlan/vxlan_core.c +51 -8
index d11028b37..8738be39e 100644
@@ -1434,6 +1434,51 @@ static int vxlan_fdb_get(struct sk_buff *skb,
 	return err;
 }
 
+static bool vxlan_snoop_same_remote(const struct vxlan_rdst *rdst,
+				    const union vxlan_addr *src_ip,
+				    u32 ifindex)
+{
+	return vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
+	       (!ifindex || rdst->remote_ifindex == ifindex);
+}
+
+static enum skb_drop_reason vxlan_snoop_move(struct net_device *dev,
+					     struct vxlan_fdb *f,
+					     const u8 *src_mac,
+					     union vxlan_addr *src_ip,
+					     u32 ifindex)
+{
+	struct vxlan_dev *vxlan = netdev_priv(dev);
+	struct vxlan_rdst *rdst;
+
+	lockdep_assert_held(&vxlan->hash_lock);
+
+	/* The entry may have changed before the lock was taken */
+	if (hlist_unhashed(&f->fdb_node))
+		return SKB_NOT_DROPPED_YET;
+
+	if (rcu_access_pointer(f->nh) ||
+	    (f->state & (NUD_PERMANENT | NUD_NOARP)))
+		return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
+
+	rdst = first_remote_rcu(f);
+	if (vxlan_snoop_same_remote(rdst, src_ip, ifindex))
+		return SKB_NOT_DROPPED_YET;
+
+	if (net_ratelimit())
+		netdev_info(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);
+
+	return SKB_NOT_DROPPED_YET;
+}
+
 /* Watch incoming packets to learn mapping between Ethernet address
  * and Tunnel endpoint.
  */
@@ -1461,6 +1506,7 @@ static enum skb_drop_reason vxlan_snoop(struct net_device *dev,
 	if (likely(f)) {
 		struct vxlan_rdst *rdst = first_remote_rcu(f);
 		unsigned long now = jiffies;
+		enum skb_drop_reason reason;
 
 		if (READ_ONCE(f->updated) != now)
 			WRITE_ONCE(f->updated, now);
@@ -1469,21 +1515,18 @@ static enum skb_drop_reason vxlan_snoop(struct net_device *dev,
 		if (rcu_access_pointer(f->nh))
 			return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
 
-		if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
-			   rdst->remote_ifindex == ifindex))
+		if (likely(vxlan_snoop_same_remote(rdst, src_ip, ifindex)))
 			return SKB_NOT_DROPPED_YET;
 
 		/* Don't migrate static entries, drop packets */
 		if (f->state & (NUD_PERMANENT | NUD_NOARP))
 			return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
 
-		if (net_ratelimit())
-			netdev_info(dev,
-				    "%pM migrated from %pIS to %pIS\n",
-				    src_mac, &rdst->remote_ip.sa, &src_ip->sa);
+		spin_lock(&vxlan->hash_lock);
+		reason = vxlan_snoop_move(dev, f, src_mac, src_ip, ifindex);
+		spin_unlock(&vxlan->hash_lock);
 
-		rdst->remote_ip = *src_ip;
-		vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
+		return reason;
 	} else {
 		/* learned new entry */
 		spin_lock(&vxlan->hash_lock);
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: 1 · Low: 1

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 patch does not close the stale dst_cache problem it says it fixes.
- [Low] The re-check under the lock in vxlan_snoop_move() runs its tests in a different order from the lockless fast path in vxlan_snoop().

--

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

When vxlan_snoop() sees a known MAC arrive from a different remote, it now resets the remote's dst_cache, updates remote_ifindex and clears offloaded, matching what vxlan_fdb_replace() does. It compares remote_ifindex only for IPv6 link-local sources. The remote update now runs in a new helper, vxlan_snoop_move(), which holds vxlan->hash_lock and checks the entry again once the lock is taken.

Quoted 5 lines
drivers/net/vxlan/vxlan_core.c +0 -0
index d11028b374721..8738be39e6019 100644
@@ -1434,6 +1434,51 @@ static int vxlan_fdb_get(struct sk_buff *skb,

[ ... ]

Quoted 12 lines
drivers/net/vxlan/vxlan_core.c +12 -0
+static enum skb_drop_reason vxlan_snoop_move(struct net_device *dev,
+					     struct vxlan_fdb *f,
+					     const u8 *src_mac,
+					     union vxlan_addr *src_ip,
+					     u32 ifindex)
+{
+	struct vxlan_dev *vxlan = netdev_priv(dev);
+	struct vxlan_rdst *rdst;
+
+	lockdep_assert_held(&vxlan->hash_lock);
+
+	/* The entry may have changed before the lock was taken */

drivers/net/vxlan/vxlan_core.c static int vxlan_fdb_get(struct sk_buff *skb, · line 1466

Quoted 10 lines
drivers/net/vxlan/vxlan_core.c +10 -0
+	if (hlist_unhashed(&f->fdb_node))
+		return SKB_NOT_DROPPED_YET;
+
+	if (rcu_access_pointer(f->nh) ||
+	    (f->state & (NUD_PERMANENT | NUD_NOARP)))
+		return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
+
+	rdst = first_remote_rcu(f);
+	if (vxlan_snoop_same_remote(rdst, src_ip, ifindex))
+		return SKB_NOT_DROPPED_YET;

[Severity: Low]
Is this check order intended? The lockless path in vxlan_snoop() calls vxlan_snoop_same_remote() before the NUD_PERMANENT | NUD_NOARP test. Here the static-state test runs first.

Consider this sequence:

CPU A in vxlan_snoop() sees a dynamic entry with a different remote and calls spin_lock(&vxlan->hash_lock).

Before CPU A gets the lock, "bridge fdb replace <mac> dst <src_ip> permanent" runs vxlan_fdb_update_existing() under hash_lock. That sets f->state = NUD_PERMANENT, and vxlan_fdb_replace() sets remote_ip = src_ip.

CPU A then takes the lock, and vxlan_snoop_move() returns SKB_DROP_REASON_VXLAN_ENTRY_EXISTS.

The packet is dropped, even though it came from the remote the static entry now points to. The lockless path would have accepted the same packet.

Would it make sense to move the same-remote check ahead of the static-state check? The nh check would have to stay first, since first_remote_rcu() is not valid for nexthop entries.

drivers/net/vxlan/vxlan_core.c static int vxlan_fdb_get(struct sk_buff *skb, · line 1475

Quoted 9 lines
drivers/net/vxlan/vxlan_core.c +9 -0
+
+	if (net_ratelimit())
+		netdev_info(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;

[Severity: Medium]
Does this fully close the stale dst_cache window the patch is meant to fix?

vxlan_xmit_one() does not take hash_lock. It copies the remote into the lookup key well before it looks at the dst_cache:

drivers/net/vxlan/vxlan_core.c:vxlan_xmit_one() {
    ...
		ifindex = rdst->remote_ifindex;

		if (addr_family == AF_INET) {
			key.u.ipv4.src = cfg->saddr.sin.sin_addr.s_addr;
			key.u.ipv4.dst = rdst->remote_ip.sin.sin_addr.s_addr;
    ...
}

Later it calls udp_tunnel_dst_lookup(), which does dst_cache_get_ip4(), then ip_route_output_key() on a miss, then dst_cache_set_ip4().

Cache validity is purely time based:

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;
}

Here is a possible sequence:

CPU1 (vxlan_xmit_one) CPU2 (vxlan_snoop_move)
key.u.ipv4.dst = old remote A

                                 dst_cache_reset()  /* reset_ts = J0 */
                                 rdst->remote_ip = B

<jiffy tick, now J0+1>
dst_cache_get_ip4()
miss, refresh_ts = J0+1
ip_route_output_key() for A
dst_cache_set_ip4(route for A)

After that, every later lookup on CPU1 passes the time_after(refresh_ts, reset_ts) test. The IPv4 obsolete check only fails when the route genid changes.

Would packets with outer daddr B then keep using the next hop chosen for A? That is the blackhole the commit message describes, and it would last until something like "ip route flush cache" or another reset.

Doing the stores before dst_cache_reset() does not seem to help, since CPU1 can still read A before either of them. Would this need a re-check or a generation check between the route lookup and dst_cache_set, or an rdst replaced via RCU?

vxlan_fdb_replace() and the other dst_cache_reset() users follow the same pattern.

Quoted 5 lines
drivers/net/vxlan/vxlan_core.c +5 -0
+	rdst->offloaded = false;
+	vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
+
+	return SKB_NOT_DROPPED_YET;
+}

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119095256.42005-1-ilskdw%40gmail.com
author
#3 lore raw
Quoted 4 lines

[Severity: Low]
Is this check order intended? The lockless path in vxlan_snoop() calls vxlan_snoop_same_remote() before the NUD_PERMANENT | NUD_NOARP test. Here the static-state test runs first.

No, it should match the fast path. I'll swap the two checks, keeping the nexthop check first, if there is another revision.

Quoted 3 lines

[Severity: Medium]
Does this fully close the stale dst_cache window the patch is meant to fix?

No. This is the window discussed for v2 and v5:

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

A transmit that copied the old remote into its key before the reset can refill the cache in a later jiffy. vxlan_fdb_replace() and the other dst_cache_reset() users have the same window. Closing it needs a generation check in the transmit path or replacing the remote through RCU, which would change vxlan_fdb_replace() as well, so I would rather propose it separately for net-next. This patch fixes the deterministic case, where every move keeps the stale route.

author
#4 lore raw

PATCH v6 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. Its pings run on one CPU, because the route cache is per CPU and only the CPU that filled it hits the stale entry. This case 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. Both remotes are on-link, so this case checks the interface, not the route cache.

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.

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    | 222 ++++++++++++++++++
 2 files changed, 223 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 +222 -0

222 lines not on this page · raw message

Signature · 1 line
2.54.0