Skip to contentSkip to conversation

amt: configurable per-tunnel limits, with bounded report work

PATCH New Omar Ramadan 2 participants 5 messages Nov 30 – Dec 1

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 cover amt: configurable per-tunnel limits, with bounded report work

A relay caps one gateway tunnel at 32 group and 128 source records, both compile-time constants. v2 made them configurable [1]. Sashiko and Paolo pointed out that at the new ceilings a single Membership Update can livelock the host: records are applied under tunnel->lock in softirq, and only the packet length bounds them [2].

This series keeps the processing in the RX path and bounds it instead:

1/3 makes the A-B case of amt_lookup_act_srcs() linear. It used to
compare every source in the group with every source in the record.
2/3 charges every group record for the passes it makes over its group
and stops applying records once a report exceeds twice the cost of refreshing the tunnel's whole state. A record for a group the tunnel has no room for does no walk of the group and is charged only for the group lookup, so a host joined to more groups than max_groups keeps its refreshes.
3/3 is v2 with the group ceiling lowered from 1024 to 512. The hashes
keep their 256 buckets, so one tunnel at the ceilings holds about 2.75 MiB [estimated, from pahole sizes]. Since twice a full refresh grows with the limits, it also caps the budget of 2/3 at what one report as large as the device's MTU can cost: a host splits its report to the MTU, so a report names at most mtu / 8 groups and mtu / 4 sources.

Changes in v5 (netdev's Sashiko review of v4 [4]):
- 3/3 (Medium): the MTU bound no longer drops below a legitimate refresh when the relay's own MTU is low. It assumes at least an Ethernet link and covers at least min(max_groups, 187) groups. At the defaults the bound stays 120,320 unless the relay's MTU is below 528, and is then no lower than the 60,160 that refreshing 32 groups costs; v4's message said the defaults always kept it.
- 3/3 (Medium): a lookup that misses walks the whole chain of its bucket, which holds max_sources / hash_buckets entries on average, and was charged as one visit. It is now charged for that many, one at the defaults and 16 at 4096 sources, and the lookups the MTU bound may charge are capped at the sources a report of that size can name. The bound at the ceilings on a 1500-byte link is 440,448, up from v4's 416,256, which left the chain length out.
- 2/3: a record for a group the tunnel has no room for is charged for its group lookup, at most max_groups chain entries; v4 charged it nothing, so one report of such records naming one group could repeat that lookup thousands of times uncharged. The message says the Update is reachable from the network, and the AMT_REC_LOOKUPS comment lists amt_add_srcs() as the seventh lookup.
- The largest legitimate report at the ceilings on that link costs about 357,000, not the 348,000 of v4's cover letter.
- I did not add Fixes: tags: 2/3 and 3/3 are net-next material here, and 1/3 only changes the A-B walk's complexity.

The work one Membership Update may cause, in bucket, source and chain-entry visits [estimated, from the code], at 32 groups / 128 sources and at the ceilings of 512 groups / 4096 sources:

defaults ceilings

  v3                       120,320   3,850,240
  v4                       120,320     416,256 (1500-byte stream link)
  this version             120,320     440,448 (1500-byte stream link)

Measured cost of one Membership Update under tunnel->lock, in a UML guest (KASAN off, so slower than native; compare the columns only), with the relay at the ceilings. v3 and this version were built from the same base and booted alternately on one machine, five rounds each [measured]. The measurement harness now fills the attack's group the way a host does, in records that each fit the link's MTU; v4's harness sent one 16 KB record at MTU 1500, which this version refuses:

one Update, relay at the ceilings v3, ms v5, ms
attack, stream MTU 1500 (64 KiB of empty

  records naming one 4,096-source group)      14.4 - 17.6   2.4 - 3.2
    records applied before the budget stops   119           14
  the same attack, stream MTU 65535           14.4 - 17.7   15.5 - 18.7
    records applied                           119           98
  refresh of 512 groups x 8 sources in
  1,400-byte reports, slowest report          0.19 - 0.28   0.14 - 0.23
  the same refresh in one 64 KiB report       1.9 - 3.9     1.4

The runner was slower than the one v4 was measured on, so compare the columns, not v4's figures. At a relay MTU of 65535 this version does not bound the attack below v3's: the first bound, twice a refresh, now counts lookups by chain length, so it is about 1.5 times v4's 2.0 million there.

Without 2/3 (v2), the attack report held tunnel->lock for about two seconds (measured for v3's cover letter, on another machine).

Deferring report processing to a per-tunnel work item would also take the work out of softirq. I have not done that here: with the work bounded, the deferral would add a queue and its own limits without changing the worst case much. Happy to do it if you prefer.

This depends on the net series "amt: relay tunnel lifetime, validation and request-path hardening" [3], which is in net-next; this is based on ccb81d5e317b, which carries it.

Testing: tools/testing/selftests/net/amt.sh, unmodified, on net-next 24630ff92eff with this series applied, x86_64 under virtme-ng/KVM (4 vCPUs, 4 GiB) with generic KASAN (inline), PROVE_LOCKING, PROVE_RCU and AMT=y: all six tests pass, amt.sh exits 0, and no KASAN, lockdep or WARNING report appeared during the run. amt.sh does not reach the new limits, so this covers the normal paths only; the budget is covered by the measurements above. Each patch builds with W=1 C=1, with IPv6=y and IPv6=n, with no warnings in amt. The struct layouts are unchanged from
v4: pahole gave 200 bytes for amt_group_node and 168 for amt_tunnel_list, so with 256 bucket heads both stay within 4 KiB.

[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/
[3] https://lore.kernel.org/netdev/[email protected]/
[4] https://lore.kernel.org/netdev/[email protected]/

Omar Ramadan (3):
amt: make the A-B source walk linear
amt: bound the source-state work of one Membership Update
amt: make the per-tunnel group and source limits configurable

 drivers/net/amt.c        | 161 +++++++++++++++++++++++++++++++++------
 include/net/amt.h        |  21 +++++
 include/uapi/linux/amt.h |   8 ++
 3 files changed, 167 insertions(+), 23 deletions(-)

base-commit: ccb81d5e317b0ae2cc66ca1c628c0ca907928f94

#1 lore raw

PATCH v5 1/3 amt: make the A-B source walk linear

amt_lookup_act_srcs() computes A-B, the sources of the group's state that a received record does not name, by comparing every source in the group with every source in the record. That runs under tunnel->lock in softirq, and only the packet length bounds the record, so a single Membership Update can already cost |A| * |B| address compares at the current 128-source default.

Mark the group's sources that the record names with one hash lookup per record source, then act on the unmarked ones in a single pass over the group, clearing the marks as it goes. The cost becomes O(|A| + |B|) and the result is unchanged: a marked source is skipped whatever its filter state, exactly as the pairwise compare skipped it.

Found by review (Sashiko, on the limits patch); not triggered at runtime.

Assisted-by LLM
---
 drivers/net/amt.c | 43 ++++++++++++++++++++++---------------------
 include/net/amt.h |  2 ++
 2 files changed, 24 insertions(+), 21 deletions(-)
drivers/net/amt.c +22 -21
index b145ed3ca..79d68bcc7 100644
@@ -1649,7 +1649,7 @@ static void amt_lookup_act_srcs(struct amt_tunnel_list *tunnel,
 	union amt_addr src = {0,};
 	struct hlist_node *t;
 	u16 nsrcs;
-	int i, j;
+	int i;
 
 	if (!v6) {
 		igmp_grec = grec;
@@ -1705,28 +1705,29 @@ static void amt_lookup_act_srcs(struct amt_tunnel_list *tunnel,
 		}
 		break;
 	case AMT_OPS_SUB:
-		/* A-B */
-		for (i = 0; i < amt->hash_buckets; i++) {
-			hlist_for_each_entry_safe(snode, t, &gnode->sources[i],
-						  node) {
-				if (!amt_status_filter(snode, filter))
-					continue;
-				for (j = 0; j < nsrcs; j++) {
-					if (!v6)
-						src.ip4 = igmp_grec->grec_src[j];
+		/* A-B: mark every source of A that B names, then act on the
+		 * unmarked ones in a single pass over the group, clearing the
+		 * marks as we go.  This is O(|A| + |B|) rather than |A| * |B|.
+		 */
+		for (i = 0; i < nsrcs; i++) {
+			if (!v6)
+				src.ip4 = igmp_grec->grec_src[i];
 #if IS_ENABLED(CONFIG_IPV6)
-					else
-						memcpy(&src.ip6,
-						       &mld_grec->grec_src[j],
-						       sizeof(struct in6_addr));
+			else
+				memcpy(&src.ip6, &mld_grec->grec_src[i],
+				       sizeof(struct in6_addr));
 #endif
-					if (amt_addr_equal(&snode->source_addr,
-							   &src))
-						goto out_sub;
-				}
-				amt_act_src(tunnel, gnode, snode, act);
-				continue;
-out_sub:;
+			snode = amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL,
+					       &src);
+			if (snode)
+				snode->sub_mark = true;
+		}
+		for (i = 0; i < amt->hash_buckets; i++) {
+			hlist_for_each_entry(snode, &gnode->sources[i], node) {
+				if (snode->sub_mark)
+					snode->sub_mark = false;
+				else if (amt_status_filter(snode, filter))
+					amt_act_src(tunnel, gnode, snode, act);
 			}
 		}
 		break;
include/net/amt.h +2 -0
index 9ba097127..f36efcdd9 100644
@@ -287,6 +287,8 @@ struct amt_source_node {
 	struct delayed_work     source_timer;
 	union amt_addr		source_addr;
 	enum amt_source_status	status;
+	/* Scratch mark for the A-B source walk, under tunnel_list->lock. */
+	bool			sub_mark;
 #define AMT_SOURCE_OLD	0
 #define AMT_SOURCE_NEW	1
 	u8			flags;

base-commit: ccb81d5e317b0ae2cc66ca1c628c0ca907928f94

#2 lore raw

PATCH v5 2/3 amt: bound the source-state work of one Membership Update

In reply to Omar Ramadan

The relay applies every group record in a Membership Update under tunnel->lock in softirq, and ip_mc_may_pull() is the only bound on how many records it applies. Each record walks its group's hash buckets and sources several times, so a report that names the same full group in many short records costs far more than one that refreshes the whole tunnel. At the current defaults a 1.5 KB Update of about 180 empty records against one group already walks the group's 256 buckets some nine hundred times.

Charge each record for the passes the mode handlers and amt_cleanup_srcs() make over its group and for its per-source lookups, and stop applying records once the total exceeds twice what refreshing every group the tunnel may hold, with every source, would cost. A report that refreshes the tunnel's whole state uses half the budget; one that exceeds it is truncated, as a lost report would be, and the next periodic report restores the state. A record for a group the tunnel has no room for does no walk of the group and is charged only for the lookup that found no group, at most max_groups chain entries. Records the relay refuses therefore cannot use up the budget and truncate the same records from every report, and a report of them cannot repeat that lookup thousands of times uncharged.

Found by review (Sashiko, on the limits patch), not seen at runtime. A gateway that completes the Request and Query exchange can send such an Update, so the cost is reachable from the network.

Assisted-by LLM
---
 drivers/net/amt.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++
 include/net/amt.h | 10 +++++++
 2 files changed, 80 insertions(+)
drivers/net/amt.c +70 -0
index 79d68bcc7..125db76e0 100644
@@ -2157,6 +2157,30 @@ static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
 		amt_del_group(amt, gnode);
 }
 
+/* The source-state work one group record can cause: AMT_REC_WALKS passes
+ * over the group's buckets and sources, which add_srcs may grow by nsrcs,
+ * plus AMT_REC_LOOKUPS hash lookups per record source.
+ */
+static u64 amt_rec_cost(const struct amt_dev *amt, u32 nr_sources, u16 nsrcs)
+{
+	return (u64)AMT_REC_WALKS * (amt->hash_buckets + nr_sources + nsrcs) +
+	       (u64)AMT_REC_LOOKUPS * nsrcs;
+}
+
+/* The most work one Membership Update may cause: twice what refreshing
+ * every group the tunnel may hold, with every source, costs.  The packet
+ * length used to be the only bound, and a report that names one full
+ * group many times costs far more than one that refreshes the whole
+ * tunnel.
+ */
+static u64 amt_report_budget(const struct amt_dev *amt)
+{
+	return 2 * ((u64)AMT_REC_WALKS *
+		    ((u64)amt->max_groups * amt->hash_buckets +
+		     2 * amt->max_sources) +
+		    (u64)AMT_REC_LOOKUPS * amt->max_sources);
+}
+
 static void amt_igmpv3_report_handler(struct amt_dev *amt, struct sk_buff *skb,
 				      struct amt_tunnel_list *tunnel)
 {
@@ -2167,8 +2191,10 @@ static void amt_igmpv3_report_handler(struct amt_dev *amt, struct sk_buff *skb,
 	union amt_addr group, host;
 	struct igmpv3_grec *grec;
 	__be32 saddr;
+	u64 budget = amt_report_budget(amt), work = 0;
 	u16 nsrcs;
 	u16 ngrec;
+	bool full;
 	int i;
 
 	saddr = ip_hdr(skb)->saddr;
@@ -2194,6 +2220,27 @@ static void amt_igmpv3_report_handler(struct amt_dev *amt, struct sk_buff *skb,
 		memset(&host, 0, sizeof(union amt_addr));
 		host.ip4 = saddr;
 		gnode = amt_lookup_group(tunnel, &group, &host, false);
+		/* A record for a group the tunnel has no room for does no
+		 * walk of the group, so it is charged only for the lookup
+		 * above, which visits at most max_groups entries of a
+		 * chain.  Charging it a walk would let a host joined to more
+		 * than max_groups groups lose the same tail records from
+		 * every report; charging it nothing would let one report of
+		 * such records, all naming one group, repeat that lookup
+		 * thousands of times.
+		 */
+		full = !gnode && tunnel->nr_groups >= amt->max_groups;
+		work += full ? amt->max_groups :
+			       amt_rec_cost(amt, gnode ? gnode->nr_sources : 0,
+					    nsrcs);
+		if (work > budget) {
+			netdev_dbg(amt->dev,
+				   "Membership Update over work budget, %u of %u records ignored\n",
+				   ngrec - i, ngrec);
+			break;
+		}
+		if (full)
+			continue;
 		if (!gnode) {
 			gnode = amt_add_group(amt, tunnel, &group, &host,
 					      false);
@@ -2335,8 +2382,10 @@ static void amt_mldv2_report_handler(struct amt_dev *amt, struct sk_buff *skb,
 	union amt_addr group, host;
 	struct mld2_grec *grec;
 	struct in6_addr saddr;
+	u64 budget = amt_report_budget(amt), work = 0;
 	u16 nsrcs;
 	u16 ngrec;
+	bool full;
 	int i;
 
 	saddr = ipv6_hdr(skb)->saddr;
@@ -2362,6 +2411,27 @@ static void amt_mldv2_report_handler(struct amt_dev *amt, struct sk_buff *skb,
 		memset(&host, 0, sizeof(union amt_addr));
 		host.ip6 = saddr;
 		gnode = amt_lookup_group(tunnel, &group, &host, true);
+		/* A record for a group the tunnel has no room for does no
+		 * walk of the group, so it is charged only for the lookup
+		 * above, which visits at most max_groups entries of a
+		 * chain.  Charging it a walk would let a host joined to more
+		 * than max_groups groups lose the same tail records from
+		 * every report; charging it nothing would let one report of
+		 * such records, all naming one group, repeat that lookup
+		 * thousands of times.
+		 */
+		full = !gnode && tunnel->nr_groups >= amt->max_groups;
+		work += full ? amt->max_groups :
+			       amt_rec_cost(amt, gnode ? gnode->nr_sources : 0,
+					    nsrcs);
+		if (work > budget) {
+			netdev_dbg(amt->dev,
+				   "Membership Update over work budget, %u of %u records ignored\n",
+				   ngrec - i, ngrec);
+			break;
+		}
+		if (full)
+			continue;
 		if (!gnode) {
 			gnode = amt_add_group(amt, tunnel, &group, &host,
 					      ETH_P_IPV6);
include/net/amt.h +10 -0
index f36efcdd9..f4ccc871c 100644
@@ -392,6 +392,16 @@ struct amt_dev {
 #define AMT_GC_KICK		2048
 #define AMT_MAX_GROUP		32
 #define AMT_MAX_SOURCE		128
+/* Per group record, an upper bound on the passes the mode handlers plus
+ * amt_cleanup_srcs() make over a group's buckets and sources, and on the
+ * hash lookups they make per record source.  The handlers need at most 5
+ * passes and 6 lookups (EXCLUDE TO_IN and EXCLUDE BLOCK), and amt_add_srcs(),
+ * which runs before every handler, adds one more lookup; the bound is
+ * rounded up so a later pass does not silently exceed it.  See
+ * amt_rec_cost().
+ */
+#define AMT_REC_WALKS		7
+#define AMT_REC_LOOKUPS		8
 #define AMT_HSIZE_SHIFT		8
 #define AMT_HSIZE		(1 << AMT_HSIZE_SHIFT)
 
#3 lore raw

PATCH v5 3/3 amt: make the per-tunnel group and source limits configurable

In reply to Omar Ramadan

A relay caps the state one gateway tunnel can hold at AMT_MAX_GROUP (32) group records and AMT_MAX_SOURCE (128) source records, counted across all of the tunnel's groups. Both are compile-time constants. An SSM receiver uses a source record for every (S,G) it joins, so a gateway that needs more than 32 groups or 128 channels through one tunnel has the excess joins dropped, and the relay operator cannot raise either limit.

Add IFLA_AMT_MAX_GROUPS and IFLA_AMT_MAX_SOURCES, read when the link is created, like IFLA_AMT_MAX_TUNNELS. Zero or an absent attribute keeps the current defaults. Gateways drive these allocations, so the policy caps them at 512 groups and 4096 sources per tunnel, and strict_start_type has it validate them, and any attribute added after them, strictly. Report both limits in amt_fill_info().

The group and source hashes keep their AMT_HSIZE (256) buckets. Sizing them from max_sources would push every group node and tunnel node into kmalloc-8k once max_sources passed 2048, doubling per-group memory; at 4096 sources in one group a chain averages 16 entries, which only makes a lookup longer.

Raising the limits also raises the previous patch's bound on the work of one Membership Update, twice what refreshing every group the tunnel may hold costs. A lookup that misses walks the whole chain of its bucket, which averages max_sources / hash_buckets entries, so the cost now counts each lookup for that many visits: one at the defaults, 16 at 4096 sources. The bound is then about 3.0 million bucket, source and chain visits at 512 groups and 4096 sources, 25 times the default 120,320. Cap it at what one legitimate report can cost. A host splits its report to the MTU, so a report names at most mtu / 8 groups, IGMPv3 records being the smallest, and at most mtu / 4 sources, which caps the lookups it may charge. On a 1500-byte stream link that caps one Update at about 440,000 visits at the ceilings, 3.7 times the default, above the roughly 357,000 that the largest legitimate report costs. A gateway sizes its reports to its own MTU, not to the relay's, so the cap assumes at least an Ethernet link so a relay whose own MTU was lowered does not truncate a legitimate refresh. At the defaults that covers all 32 groups: the previous bound stays unless that MTU is below 528, which lowers it to no less than the 60,160 that refreshing 32 groups costs. A report larger than the relay's own MTU, from a gateway on a jumbo link to a relay on a standard one, has the records past the bound ignored at high limits, as an over-budget report already does.

Assisted-by LLM
---
 drivers/net/amt.c        | 68 +++++++++++++++++++++++++++++++++-------
 include/net/amt.h        |  9 ++++++
 include/uapi/linux/amt.h |  8 +++++
 3 files changed, 73 insertions(+), 12 deletions(-)
drivers/net/amt.c +56 -12
index 125db76e0..b3e43fc1e 100644
@@ -2157,6 +2157,15 @@ static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
 		amt_del_group(amt, gnode);
 }
 
+/* What one source lookup can cost: a miss walks the whole chain of its
+ * bucket, which holds max_sources / hash_buckets entries on average once
+ * the tunnel may hold more sources than the group has buckets.
+ */
+static u32 amt_lookup_cost(const struct amt_dev *amt)
+{
+	return DIV_ROUND_UP(amt->max_sources, amt->hash_buckets);
+}
+
 /* The source-state work one group record can cause: AMT_REC_WALKS passes
  * over the group's buckets and sources, which add_srcs may grow by nsrcs,
  * plus AMT_REC_LOOKUPS hash lookups per record source.
@@ -2164,21 +2173,45 @@ static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
 static u64 amt_rec_cost(const struct amt_dev *amt, u32 nr_sources, u16 nsrcs)
 {
 	return (u64)AMT_REC_WALKS * (amt->hash_buckets + nr_sources + nsrcs) +
-	       (u64)AMT_REC_LOOKUPS * nsrcs;
+	       (u64)AMT_REC_LOOKUPS * amt_lookup_cost(amt) * nsrcs;
+}
+
+/* The work of a report naming @recs groups, each walked in full, plus two
+ * passes over every source the tunnel may hold and @lookups source lookups.
+ */
+static u64 amt_report_cost(const struct amt_dev *amt, u64 recs, u64 lookups)
+{
+	return (u64)AMT_REC_WALKS *
+	       (recs * amt->hash_buckets + 2 * (u64)amt->max_sources) +
+	       (u64)AMT_REC_LOOKUPS * amt_lookup_cost(amt) * lookups;
 }
 
-/* The most work one Membership Update may cause: twice what refreshing
- * every group the tunnel may hold, with every source, costs.  The packet
- * length used to be the only bound, and a report that names one full
- * group many times costs far more than one that refreshes the whole
- * tunnel.
+/* The most work one Membership Update may cause.  The packet length used
+ * to be the only bound, and a report that names one full group many times
+ * costs far more than one that refreshes the whole tunnel.
+ *
+ * Twice what refreshing every group the tunnel may hold costs, but never
+ * more than a report as large as this device's MTU can legitimately cost:
+ * a host splits its report to the MTU (RFC 3376 s4.2.16, RFC 3810
+ * s5.2.15), so one report names at most mtu / 8 groups, IGMPv3 records
+ * being the smallest, or mtu / 4 sources.  The second bound is what limits
+ * the work at high IFLA_AMT_MAX_GROUPS and IFLA_AMT_MAX_SOURCES; at the
+ * defaults the first is the smaller, unless the device's MTU is below 528.
+ * A gateway sizes its reports to its own MTU, not to this device's, so the
+ * second bound assumes at least an Ethernet link: it never covers fewer
+ * than min(max_groups, 187) groups.
  */
 static u64 amt_report_budget(const struct amt_dev *amt)
 {
-	return 2 * ((u64)AMT_REC_WALKS *
-		    ((u64)amt->max_groups * amt->hash_buckets +
-		     2 * amt->max_sources) +
-		    (u64)AMT_REC_LOOKUPS * amt->max_sources);
+	u64 mtu = max_t(u64, READ_ONCE(amt->dev->mtu), ETH_DATA_LEN);
+	u64 recs = max_t(u64, READ_ONCE(amt->dev->mtu) /
+			      sizeof(struct igmpv3_grec),
+			 min_t(u32, amt->max_groups,
+			       ETH_DATA_LEN / sizeof(struct igmpv3_grec)));
+	u64 srcs = min_t(u64, amt->max_sources, mtu / sizeof(__be32));
+
+	return min(2 * amt_report_cost(amt, amt->max_groups, amt->max_sources),
+		   amt_report_cost(amt, recs, srcs));
 }
 
 static void amt_igmpv3_report_handler(struct amt_dev *amt, struct sk_buff *skb,
@@ -3599,6 +3632,7 @@ static void amt_link_setup(struct net_device *dev)
 }
 
 static const struct nla_policy amt_policy[IFLA_AMT_MAX + 1] = {
+	[IFLA_AMT_UNSPEC]	= { .strict_start_type = IFLA_AMT_MAX_GROUPS },
 	[IFLA_AMT_MODE]		= { .type = NLA_U32 },
 	[IFLA_AMT_RELAY_PORT]	= { .type = NLA_U16 },
 	[IFLA_AMT_GATEWAY_PORT]	= { .type = NLA_U16 },
@@ -3607,6 +3641,8 @@ static const struct nla_policy amt_policy[IFLA_AMT_MAX + 1] = {
 	[IFLA_AMT_REMOTE_IP]	= { .len = sizeof_field(struct iphdr, daddr) },
 	[IFLA_AMT_DISCOVERY_IP]	= NLA_POLICY_EXACT_LEN(sizeof(__be32)),
 	[IFLA_AMT_MAX_TUNNELS]	= { .type = NLA_U32 },
+	[IFLA_AMT_MAX_GROUPS]	= NLA_POLICY_MAX(NLA_U32, AMT_MAX_GROUP_LIMIT),
+	[IFLA_AMT_MAX_SOURCES]	= NLA_POLICY_MAX(NLA_U32, AMT_MAX_SOURCE_LIMIT),
 };
 
 static int amt_validate(struct nlattr *tb[], struct nlattr *data[],
@@ -3673,8 +3709,10 @@ static int amt_newlink(struct net_device *dev,
 
 	spin_lock_init(&amt->lock);
 	seqcount_spinlock_init(&amt->key_seq, &amt->lock);
-	amt->max_groups = AMT_MAX_GROUP;
-	amt->max_sources = AMT_MAX_SOURCE;
+	amt->max_groups = nla_get_u32_default(data[IFLA_AMT_MAX_GROUPS], 0) ?:
+			  AMT_MAX_GROUP;
+	amt->max_sources = nla_get_u32_default(data[IFLA_AMT_MAX_SOURCES], 0) ?:
+			   AMT_MAX_SOURCE;
 	amt->hash_buckets = AMT_HSIZE;
 	amt->nr_tunnels = 0;
 	get_random_bytes(&amt->hash_seed, sizeof(amt->hash_seed));
@@ -3790,6 +3828,8 @@ static size_t amt_get_size(const struct net_device *dev)
 	       nla_total_size(sizeof(__u16)) + /* IFLA_AMT_GATEWAY_PORT */
 	       nla_total_size(sizeof(__u32)) + /* IFLA_AMT_LINK */
 	       nla_total_size(sizeof(__u32)) + /* IFLA_MAX_TUNNELS */
+	       nla_total_size(sizeof(__u32)) + /* IFLA_AMT_MAX_GROUPS */
+	       nla_total_size(sizeof(__u32)) + /* IFLA_AMT_MAX_SOURCES */
 	       nla_total_size(sizeof(__be32)) + /* IFLA_AMT_DISCOVERY_IP */
 	       nla_total_size(sizeof(__be32)) + /* IFLA_AMT_REMOTE_IP */
 	       nla_total_size(sizeof(__be32)); /* IFLA_AMT_LOCAL_IP */
@@ -3820,6 +3860,10 @@ static int amt_fill_info(struct sk_buff *skb, const struct net_device *dev)
 			goto nla_put_failure;
 	if (nla_put_u32(skb, IFLA_AMT_MAX_TUNNELS, amt->max_tunnels))
 		goto nla_put_failure;
+	if (nla_put_u32(skb, IFLA_AMT_MAX_GROUPS, amt->max_groups))
+		goto nla_put_failure;
+	if (nla_put_u32(skb, IFLA_AMT_MAX_SOURCES, amt->max_sources))
+		goto nla_put_failure;
 
 	rcu_read_unlock();
 	return 0;
include/net/amt.h +9 -0
index f4ccc871c..f1a2293bf 100644
@@ -392,6 +392,15 @@ struct amt_dev {
 #define AMT_GC_KICK		2048
 #define AMT_MAX_GROUP		32
 #define AMT_MAX_SOURCE		128
+/* Upper bounds for IFLA_AMT_MAX_GROUPS and IFLA_AMT_MAX_SOURCES. Gateways
+ * drive these allocations: at the bounds one tunnel holds about 2 MiB of
+ * group nodes (each a kmalloc-4k object with AMT_HSIZE bucket heads) and
+ * under 1 MiB of source nodes, and max_tunnels multiplies that.  The work
+ * one Membership Update may cause is bounded separately, by
+ * amt_report_budget().
+ */
+#define AMT_MAX_GROUP_LIMIT	512
+#define AMT_MAX_SOURCE_LIMIT	4096
 /* Per group record, an upper bound on the passes the mode handlers plus
  * amt_cleanup_srcs() make over a group's buckets and sources, and on the
  * hash lookups they make per record source.  The handlers need at most 5
include/uapi/linux/amt.h +8 -0
index 2dccff417..a106d7a07 100644
@@ -54,6 +54,14 @@ enum {
 	IFLA_AMT_DISCOVERY_IP,
 	/* This attribute specify number of maximum tunnel. */
 	IFLA_AMT_MAX_TUNNELS,
+	/* Maximum number of group records one tunnel may hold.
+	 * Zero or absent means the default of 32.
+	 */
+	IFLA_AMT_MAX_GROUPS,
+	/* Maximum number of source records one tunnel may hold, counted
+	 * across all of its groups. Zero or absent means the default of 128.
+	 */
+	IFLA_AMT_MAX_SOURCES,
 	__IFLA_AMT_MAX,
 };
 
#4 lore raw

Re: [PATCH net-next v5 3/3] amt: make the per-tunnel group and source limits configurable

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] At the default limits, amt_report_budget() (drivers/net/amt.c) cuts the per-Update work budget to exactly one full refresh, with no…
- [Low] The comment on the new amt_report_cost() in drivers/net/amt.c describes the wrong units.

--

amt: make the per-tunnel group and source limits configurable

This patch adds IFLA_AMT_MAX_GROUPS and IFLA_AMT_MAX_SOURCES so a relay operator can raise the per-tunnel group and source limits at link creation. It also rescales the per-Membership-Update work budget in amt_report_budget(). The budget now accounts for longer hash chains and is capped at what one MTU-sized report can legitimately cost.

Quoted 4 lines
drivers/net/amt.c +0 -0
index 125db76e0acf7..b3e43fc1e6374 100644

[ ... ]

Quoted 6 lines
drivers/net/amt.c +1 -1
@@ -2164,21 +2173,45 @@ static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
 static u64 amt_rec_cost(const struct amt_dev *amt, u32 nr_sources, u16 nsrcs)
 {
 	return (u64)AMT_REC_WALKS * (amt->hash_buckets + nr_sources + nsrcs) +
-	       (u64)AMT_REC_LOOKUPS * nsrcs;
+	       (u64)AMT_REC_LOOKUPS * amt_lookup_cost(amt) * nsrcs;

drivers/net/amt.c static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb, · line 2186

Quoted 10 lines
drivers/net/amt.c +10 -0
+}
+
+/* The work of a report naming @recs groups, each walked in full, plus two
+ * passes over every source the tunnel may hold and @lookups source lookups.
+ */
+static u64 amt_report_cost(const struct amt_dev *amt, u64 recs, u64 lookups)
+{
+	return (u64)AMT_REC_WALKS *
+	       (recs * amt->hash_buckets + 2 * (u64)amt->max_sources) +
+	       (u64)AMT_REC_LOOKUPS * amt_lookup_cost(amt) * lookups;

[Severity: Low]
This isn't a bug, but does this comment describe the units the code actually charges?

The code charges AMT_REC_WALKS * (recs * hash_buckets + 2 * max_sources). That is 7 bucket walks per group and 14 visits per possible source, not "two passes". It also charges AMT_REC_LOOKUPS * amt_lookup_cost(amt) per unit of @lookups, which is 8 chain-scaled lookups.

The only caller, amt_report_budget(), passes source counts as @lookups (amt->max_sources, or srcs), not lookup counts.

The comment on amt_rec_cost() just above names the AMT_REC_WALKS and AMT_REC_LOOKUPS multipliers. Could this one do the same? As written it is off by a factor of about 7 to 8 for anyone checking the bound figures in the commit message.

Quoted 10 lines
drivers/net/amt.c +3 -5
 }
 
-/* The most work one Membership Update may cause: twice what refreshing
- * every group the tunnel may hold, with every source, costs.  The packet
- * length used to be the only bound, and a report that names one full
- * group many times costs far more than one that refreshes the whole
- * tunnel.
+/* The most work one Membership Update may cause.  The packet length used
+ * to be the only bound, and a report that names one full group many times
+ * costs far more than one that refreshes the whole tunnel.

[ ... ]

Quoted 5 lines

static u64 amt_report_budget(const struct amt_dev *amt) {

drivers/net/amt.c +0 -3
-	return 2 * ((u64)AMT_REC_WALKS *
-		    ((u64)amt->max_groups * amt->hash_buckets +
-		     2 * amt->max_sources) +

drivers/net/amt.c static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb, · line 2214

Quoted 10 lines
drivers/net/amt.c +9 -1
-		    (u64)AMT_REC_LOOKUPS * amt->max_sources);
+	u64 mtu = max_t(u64, READ_ONCE(amt->dev->mtu), ETH_DATA_LEN);
+	u64 recs = max_t(u64, READ_ONCE(amt->dev->mtu) /
+			      sizeof(struct igmpv3_grec),
+			 min_t(u32, amt->max_groups,
+			       ETH_DATA_LEN / sizeof(struct igmpv3_grec)));
+	u64 srcs = min_t(u64, amt->max_sources, mtu / sizeof(__be32));
+
+	return min(2 * amt_report_cost(amt, amt->max_groups, amt->max_sources),
+		   amt_report_cost(amt, recs, srcs));

[Severity: Medium]
At the default limits, can this leave the budget with no slack when the relay's MTU is below 528?

srcs uses the Ethernet-floored mtu, but recs uses the raw READ_ONCE(amt->dev->mtu). recs is floored only at min(max_groups, 187). With max_groups 32, max_sources 128 and hash_buckets 256, amt_lookup_cost() is 1. When dev->mtu < 264, recs is 32, and the second term is:

    7 * (32 * 256 + 2 * 128) + 8 * 1 * 128 = 60160

That is exactly what refreshing 32 groups holding 128 sources costs. For 264 <= mtu < 528 the slack is only 1792 * (mtu / 8 - 32). Before this patch the budget was 120320 at any MTU.

This setup is reachable if the relay MTU is lowered (dev->min_mtu is ETH_MIN_MTU + AMT_RELAY_HLEN) or the stream device has a small MTU. A gateway on an Ethernet link sizes its reports to its own MTU, so ordinary reports can then go over budget in two cases.

First, a host joined to more than 32 groups. Each record for a group the tunnel has no room for adds amt->max_groups to work.

Second, records that name more sources than the relay holds. For example, 32 groups with 5 sources each costs
7 * (8192 + 128 + 160) + 8 * 160 = 60640, which is more than 60160.

In both cases amt_igmpv3_report_handler() hits this:

    if (work > budget) {
        netdev_dbg(amt->dev,
                   "Membership Update over work budget, %u of %u records ignored\n",
                   ngrec - i, ngrec);
        break;
    }

amt_mldv2_report_handler() uses the same budget and charges. The tail records for groups the relay already holds are skipped, so those groups are not refreshed and expire on their group timer. With the same report layout, this repeats every cycle.

The handler's comment says the cheap full charge exists so a host does not "lose the same tail records from every report". The commit message says the Ethernet assumption keeps "a relay whose own MTU was lowered" from truncating "a legitimate refresh".

Would computing recs from the Ethernet-floored mtu, as srcs already is, keep the old 120320 bound at the defaults for every MTU?

Quoted 3 lines

}

static void amt_igmpv3_report_handler(struct amt_dev *amt, struct sk_buff *skb,

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130095059.1301582-1-omar%40blockcast.net