Skip to contentSkip to conversation

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

PATCH New Zhefu Peng 1 participant 3 messages Nov 26

Subsystems (3)
lore
Share & tools
lore

Zhefu Peng

author
#0 lore raw

v7 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 this version: 14/14, 14/14, 14/14
- with this version and CONFIG_PROVE_LOCKING: 7/7 for each case, no lockdep splats

v7:
- share all checks between the lockless path and the locked recheck in one helper, which also makes their order the same, and drop the redundant lockdep assertion (Paolo Abeni)

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

PATCH v7 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 | 87 +++++++++++++++++++++++++++-------
 1 file changed, 69 insertions(+), 18 deletions(-)
drivers/net/vxlan/vxlan_core.c +69 -18
index d11028b37..584d56d03 100644
@@ -1434,6 +1434,67 @@ static int vxlan_fdb_get(struct sk_buff *skb,
 	return err;
 }
 
+static bool vxlan_snoop_should_move(struct vxlan_fdb *f,
+				    const union vxlan_addr *src_ip,
+				    u32 ifindex,
+				    enum skb_drop_reason *reason)
+{
+	struct vxlan_rdst *rdst;
+
+	*reason = SKB_NOT_DROPPED_YET;
+
+	/* Don't override an fdb with nexthop with a learnt entry */
+	if (rcu_access_pointer(f->nh)) {
+		*reason = SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
+		return false;
+	}
+
+	rdst = first_remote_rcu(f);
+	if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
+		   (!ifindex || rdst->remote_ifindex == ifindex)))
+		return false;
+
+	/* Don't migrate static entries, drop packets */
+	if (f->state & (NUD_PERMANENT | NUD_NOARP)) {
+		*reason = SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
+		return false;
+	}
+
+	return true;
+}
+
+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);
+	enum skb_drop_reason reason;
+	struct vxlan_rdst *rdst;
+
+	/* The entry may have changed before the lock was taken */
+	if (hlist_unhashed(&f->fdb_node))
+		return SKB_NOT_DROPPED_YET;
+
+	if (!vxlan_snoop_should_move(f, src_ip, ifindex, &reason))
+		return reason;
+
+	rdst = first_remote_rcu(f);
+	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.
  */
@@ -1459,31 +1520,21 @@ static enum skb_drop_reason vxlan_snoop(struct net_device *dev,
 
 	f = vxlan_find_mac_rcu(vxlan, cfg, src_mac, vni);
 	if (likely(f)) {
-		struct vxlan_rdst *rdst = first_remote_rcu(f);
+		enum skb_drop_reason reason;
 		unsigned long now = jiffies;
 
 		if (READ_ONCE(f->updated) != now)
 			WRITE_ONCE(f->updated, now);
 
-		/* Don't override an fdb with nexthop with a learnt entry */
-		if (rcu_access_pointer(f->nh))
-			return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
+		if (likely(!vxlan_snoop_should_move(f, src_ip, ifindex,
+						    &reason)))
+			return reason;
 
-		if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
-			   rdst->remote_ifindex == 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
author
#2 lore raw

PATCH v7 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