xref: /linux/net/shaper/shaper.c (revision 7cee43fcb0c3f71441d2faaa8c2202b6a88b6bef)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/bits.h>
4 #include <linux/bitfield.h>
5 #include <linux/idr.h>
6 #include <linux/kernel.h>
7 #include <linux/netdevice.h>
8 #include <linux/netlink.h>
9 #include <linux/skbuff.h>
10 #include <linux/xarray.h>
11 #include <net/devlink.h>
12 #include <net/net_shaper.h>
13 
14 #include "shaper_nl_gen.h"
15 
16 #include "../core/dev.h"
17 
18 #define NET_SHAPER_SCOPE_SHIFT	26
19 #define NET_SHAPER_ID_MASK	GENMASK(NET_SHAPER_SCOPE_SHIFT - 1, 0)
20 #define NET_SHAPER_SCOPE_MASK	GENMASK(31, NET_SHAPER_SCOPE_SHIFT)
21 
22 #define NET_SHAPER_ID_UNSPEC NET_SHAPER_ID_MASK
23 
24 struct net_shaper_hierarchy {
25 	struct xarray shapers;
26 };
27 
28 struct net_shaper_nl_ctx {
29 	struct net_shaper_binding binding;
30 	netdevice_tracker dev_tracker;
31 	unsigned long start_index;
32 };
33 
34 static struct net_shaper_binding *net_shaper_binding_from_ctx(void *ctx)
35 {
36 	return &((struct net_shaper_nl_ctx *)ctx)->binding;
37 }
38 
39 static struct net_shaper_hierarchy *
40 net_shaper_hierarchy(struct net_shaper_binding *binding)
41 {
42 	/* Pairs with WRITE_ONCE() in net_shaper_hierarchy_setup. */
43 	if (binding->type == NET_SHAPER_BINDING_TYPE_NETDEV)
44 		return READ_ONCE(binding->netdev->net_shaper_hierarchy);
45 
46 	/* No other type supported yet. */
47 	return NULL;
48 }
49 
50 static struct net_shaper_hierarchy *
51 net_shaper_hierarchy_rcu(struct net_shaper_binding *binding)
52 {
53 	/* Readers look up the device and take a ref, then take RCU lock
54 	 * later at which point netdev may have been unregistered and flushed.
55 	 * READ_ONCE() pairs with WRITE_ONCE() in net_shaper_hierarchy_setup.
56 	 */
57 	if (binding->type == NET_SHAPER_BINDING_TYPE_NETDEV &&
58 	    READ_ONCE(binding->netdev->reg_state) <= NETREG_REGISTERED)
59 		return READ_ONCE(binding->netdev->net_shaper_hierarchy);
60 
61 	/* No other type supported yet. */
62 	return NULL;
63 }
64 
65 static const struct net_shaper_ops *
66 net_shaper_ops(struct net_shaper_binding *binding)
67 {
68 	if (binding->type == NET_SHAPER_BINDING_TYPE_NETDEV)
69 		return binding->netdev->netdev_ops->net_shaper_ops;
70 
71 	/* No other type supported yet. */
72 	return NULL;
73 }
74 
75 /* Count the number of [multi] attributes of the given type. */
76 static int net_shaper_list_len(struct genl_info *info, int type)
77 {
78 	struct nlattr *attr;
79 	int rem, cnt = 0;
80 
81 	nla_for_each_attr_type(attr, type, genlmsg_data(info->genlhdr),
82 			       genlmsg_len(info->genlhdr), rem)
83 		cnt++;
84 	return cnt;
85 }
86 
87 static int net_shaper_handle_size(void)
88 {
89 	return nla_total_size(nla_total_size(sizeof(u32)) +
90 			      nla_total_size(sizeof(u32)));
91 }
92 
93 static int net_shaper_fill_binding(struct sk_buff *msg,
94 				   const struct net_shaper_binding *binding,
95 				   u32 type)
96 {
97 	/* Should never happen, as currently only NETDEV is supported. */
98 	if (WARN_ON_ONCE(binding->type != NET_SHAPER_BINDING_TYPE_NETDEV))
99 		return -EINVAL;
100 
101 	if (nla_put_u32(msg, type, binding->netdev->ifindex))
102 		return -EMSGSIZE;
103 
104 	return 0;
105 }
106 
107 static int net_shaper_fill_handle(struct sk_buff *msg,
108 				  const struct net_shaper_handle *handle,
109 				  u32 type)
110 {
111 	struct nlattr *handle_attr;
112 
113 	if (handle->scope == NET_SHAPER_SCOPE_UNSPEC)
114 		return 0;
115 
116 	handle_attr = nla_nest_start(msg, type);
117 	if (!handle_attr)
118 		return -EMSGSIZE;
119 
120 	if (nla_put_u32(msg, NET_SHAPER_A_HANDLE_SCOPE, handle->scope) ||
121 	    (handle->scope >= NET_SHAPER_SCOPE_QUEUE &&
122 	     nla_put_u32(msg, NET_SHAPER_A_HANDLE_ID, handle->id)))
123 		goto handle_nest_cancel;
124 
125 	nla_nest_end(msg, handle_attr);
126 	return 0;
127 
128 handle_nest_cancel:
129 	nla_nest_cancel(msg, handle_attr);
130 	return -EMSGSIZE;
131 }
132 
133 static int
134 net_shaper_fill_one(struct sk_buff *msg,
135 		    const struct net_shaper_binding *binding,
136 		    const struct net_shaper *shaper,
137 		    const struct genl_info *info)
138 {
139 	void *hdr;
140 
141 	hdr = genlmsg_iput(msg, info);
142 	if (!hdr)
143 		return -EMSGSIZE;
144 
145 	if (net_shaper_fill_binding(msg, binding, NET_SHAPER_A_IFINDEX) ||
146 	    net_shaper_fill_handle(msg, &shaper->parent,
147 				   NET_SHAPER_A_PARENT) ||
148 	    net_shaper_fill_handle(msg, &shaper->handle,
149 				   NET_SHAPER_A_HANDLE) ||
150 	    ((shaper->bw_min || shaper->bw_max || shaper->burst) &&
151 	     nla_put_u32(msg, NET_SHAPER_A_METRIC, shaper->metric)) ||
152 	    (shaper->bw_min &&
153 	     nla_put_uint(msg, NET_SHAPER_A_BW_MIN, shaper->bw_min)) ||
154 	    (shaper->bw_max &&
155 	     nla_put_uint(msg, NET_SHAPER_A_BW_MAX, shaper->bw_max)) ||
156 	    (shaper->burst &&
157 	     nla_put_uint(msg, NET_SHAPER_A_BURST, shaper->burst)) ||
158 	    (shaper->priority &&
159 	     nla_put_u32(msg, NET_SHAPER_A_PRIORITY, shaper->priority)) ||
160 	    (shaper->weight &&
161 	     nla_put_u32(msg, NET_SHAPER_A_WEIGHT, shaper->weight)))
162 		goto nla_put_failure;
163 
164 	genlmsg_end(msg, hdr);
165 
166 	return 0;
167 
168 nla_put_failure:
169 	genlmsg_cancel(msg, hdr);
170 	return -EMSGSIZE;
171 }
172 
173 /* Initialize the context fetching the relevant device and
174  * acquiring a reference to it.
175  */
176 static int net_shaper_ctx_setup(const struct genl_info *info, int type,
177 				struct net_shaper_nl_ctx *ctx)
178 {
179 	struct net *ns = genl_info_net(info);
180 	struct net_device *dev;
181 	int ifindex;
182 
183 	if (GENL_REQ_ATTR_CHECK(info, type))
184 		return -EINVAL;
185 
186 	ifindex = nla_get_u32(info->attrs[type]);
187 	dev = netdev_get_by_index(ns, ifindex, &ctx->dev_tracker, GFP_KERNEL);
188 	if (!dev) {
189 		NL_SET_BAD_ATTR(info->extack, info->attrs[type]);
190 		return -ENOENT;
191 	}
192 
193 	if (!dev->netdev_ops->net_shaper_ops) {
194 		NL_SET_BAD_ATTR(info->extack, info->attrs[type]);
195 		netdev_put(dev, &ctx->dev_tracker);
196 		return -EOPNOTSUPP;
197 	}
198 
199 	ctx->binding.type = NET_SHAPER_BINDING_TYPE_NETDEV;
200 	ctx->binding.netdev = dev;
201 	return 0;
202 }
203 
204 /* Like net_shaper_ctx_setup(), but for "write" handlers (never for dumps!)
205  * Acquires the lock protecting the hierarchy (instance lock for netdev).
206  */
207 static int net_shaper_ctx_setup_lock(const struct genl_info *info, int type,
208 				     struct net_shaper_nl_ctx *ctx)
209 {
210 	struct net *ns = genl_info_net(info);
211 	struct net_device *dev;
212 	int ifindex;
213 
214 	if (GENL_REQ_ATTR_CHECK(info, type))
215 		return -EINVAL;
216 
217 	ifindex = nla_get_u32(info->attrs[type]);
218 	dev = netdev_get_by_index_lock(ns, ifindex);
219 	if (!dev) {
220 		NL_SET_BAD_ATTR(info->extack, info->attrs[type]);
221 		return -ENOENT;
222 	}
223 
224 	if (!dev->netdev_ops->net_shaper_ops) {
225 		NL_SET_BAD_ATTR(info->extack, info->attrs[type]);
226 		netdev_unlock(dev);
227 		return -EOPNOTSUPP;
228 	}
229 
230 	ctx->binding.type = NET_SHAPER_BINDING_TYPE_NETDEV;
231 	ctx->binding.netdev = dev;
232 	return 0;
233 }
234 
235 static void net_shaper_ctx_cleanup(struct net_shaper_nl_ctx *ctx)
236 {
237 	if (ctx->binding.type == NET_SHAPER_BINDING_TYPE_NETDEV)
238 		netdev_put(ctx->binding.netdev, &ctx->dev_tracker);
239 }
240 
241 static void net_shaper_ctx_cleanup_unlock(struct net_shaper_nl_ctx *ctx)
242 {
243 	if (ctx->binding.type == NET_SHAPER_BINDING_TYPE_NETDEV)
244 		netdev_unlock(ctx->binding.netdev);
245 }
246 
247 static u32 net_shaper_handle_to_index(const struct net_shaper_handle *handle)
248 {
249 	return FIELD_PREP(NET_SHAPER_SCOPE_MASK, handle->scope) |
250 		FIELD_PREP(NET_SHAPER_ID_MASK, handle->id);
251 }
252 
253 static void net_shaper_index_to_handle(u32 index,
254 				       struct net_shaper_handle *handle)
255 {
256 	handle->scope = FIELD_GET(NET_SHAPER_SCOPE_MASK, index);
257 	handle->id = FIELD_GET(NET_SHAPER_ID_MASK, index);
258 }
259 
260 static void net_shaper_default_parent(const struct net_shaper_handle *handle,
261 				      struct net_shaper_handle *parent)
262 {
263 	switch (handle->scope) {
264 	case NET_SHAPER_SCOPE_UNSPEC:
265 	case NET_SHAPER_SCOPE_NETDEV:
266 	case __NET_SHAPER_SCOPE_MAX:
267 		parent->scope = NET_SHAPER_SCOPE_UNSPEC;
268 		break;
269 
270 	case NET_SHAPER_SCOPE_QUEUE:
271 	case NET_SHAPER_SCOPE_NODE:
272 		parent->scope = NET_SHAPER_SCOPE_NETDEV;
273 		break;
274 	}
275 	parent->id = 0;
276 }
277 
278 /* MARK_0 is already in use due to XA_FLAGS_ALLOC. The VALID mark is set on
279  * an entry only after the device-side configuration has completed
280  * successfully (see net_shaper_commit()). Lookups and dumps must filter on
281  * this mark to avoid exposing tentative entries inserted by
282  * net_shaper_pre_insert() while the driver call is still in flight.
283  */
284 #define NET_SHAPER_VALID	XA_MARK_1
285 
286 static struct net_shaper *
287 net_shaper_lookup(struct net_shaper_binding *binding,
288 		  const struct net_shaper_handle *handle)
289 {
290 	u32 index = net_shaper_handle_to_index(handle);
291 	struct net_shaper_hierarchy *hierarchy;
292 
293 	hierarchy = net_shaper_hierarchy_rcu(binding);
294 	if (!hierarchy || !xa_get_mark(&hierarchy->shapers, index,
295 				       NET_SHAPER_VALID))
296 		return NULL;
297 
298 	return xa_load(&hierarchy->shapers, index);
299 }
300 
301 /* Allocate on demand the per device shaper's hierarchy container.
302  * Called under the lock protecting the hierarchy (instance lock for netdev)
303  */
304 static struct net_shaper_hierarchy *
305 net_shaper_hierarchy_setup(struct net_shaper_binding *binding)
306 {
307 	struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding);
308 
309 	if (hierarchy)
310 		return hierarchy;
311 
312 	hierarchy = kmalloc_obj(*hierarchy);
313 	if (!hierarchy)
314 		return NULL;
315 
316 	/* The flag is required for ID allocation */
317 	xa_init_flags(&hierarchy->shapers, XA_FLAGS_ALLOC);
318 
319 	switch (binding->type) {
320 	case NET_SHAPER_BINDING_TYPE_NETDEV:
321 		/* Pairs with READ_ONCE in net_shaper_hierarchy. */
322 		WRITE_ONCE(binding->netdev->net_shaper_hierarchy, hierarchy);
323 		break;
324 	}
325 	return hierarchy;
326 }
327 
328 /* Prepare the hierarchy container to actually insert the given shaper, doing
329  * in advance the needed allocations.
330  */
331 static int net_shaper_pre_insert(struct net_shaper_binding *binding,
332 				 struct net_shaper_handle *handle,
333 				 struct netlink_ext_ack *extack)
334 {
335 	struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding);
336 	struct net_shaper *prev, *cur;
337 	bool id_allocated = false;
338 	int ret, index;
339 
340 	if (!hierarchy)
341 		return -ENOMEM;
342 
343 	index = net_shaper_handle_to_index(handle);
344 	cur = xa_load(&hierarchy->shapers, index);
345 	if (cur)
346 		return 0;
347 
348 	/* Allocated a new id, if needed. */
349 	if (handle->scope == NET_SHAPER_SCOPE_NODE &&
350 	    handle->id == NET_SHAPER_ID_UNSPEC) {
351 		u32 min, max;
352 
353 		handle->id = NET_SHAPER_ID_MASK - 1;
354 		max = net_shaper_handle_to_index(handle);
355 		handle->id = 0;
356 		min = net_shaper_handle_to_index(handle);
357 
358 		ret = xa_alloc(&hierarchy->shapers, &index, NULL,
359 			       XA_LIMIT(min, max), GFP_KERNEL);
360 		if (ret < 0) {
361 			NL_SET_ERR_MSG(extack, "Can't allocate new id for NODE shaper");
362 			return ret;
363 		}
364 
365 		net_shaper_index_to_handle(index, handle);
366 		id_allocated = true;
367 	}
368 
369 	cur = kzalloc_obj(*cur);
370 	if (!cur) {
371 		ret = -ENOMEM;
372 		goto free_id;
373 	}
374 
375 	/* Insert as 'tentative' (no VALID mark). The mark will be set by
376 	 * net_shaper_commit() once the driver-side configuration succeeds.
377 	 */
378 	prev = xa_store(&hierarchy->shapers, index, cur, GFP_KERNEL);
379 	if (xa_err(prev)) {
380 		NL_SET_ERR_MSG(extack, "Can't insert shaper into device store");
381 		kfree_rcu(cur, rcu);
382 		ret = xa_err(prev);
383 		goto free_id;
384 	}
385 	return 0;
386 
387 free_id:
388 	if (id_allocated)
389 		xa_erase(&hierarchy->shapers, index);
390 	return ret;
391 }
392 
393 /* Commit the tentative insert with the actual values.
394  * Must be called only after a successful net_shaper_pre_insert().
395  */
396 static void net_shaper_commit(struct net_shaper_binding *binding,
397 			      int nr_shapers, const struct net_shaper *shapers)
398 {
399 	struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding);
400 	struct net_shaper *cur;
401 	int index;
402 	int i;
403 
404 	xa_lock(&hierarchy->shapers);
405 	for (i = 0; i < nr_shapers; ++i) {
406 		index = net_shaper_handle_to_index(&shapers[i].handle);
407 
408 		cur = xa_load(&hierarchy->shapers, index);
409 		if (WARN_ON_ONCE(!cur))
410 			continue;
411 
412 		/* Successful update: drop the tentative mark
413 		 * and update the hierarchy container.
414 		 */
415 		__xa_set_mark(&hierarchy->shapers, index, NET_SHAPER_VALID);
416 		*cur = shapers[i];
417 	}
418 	xa_unlock(&hierarchy->shapers);
419 }
420 
421 /* Rollback all the tentative inserts from the hierarchy. */
422 static void net_shaper_rollback(struct net_shaper_binding *binding)
423 {
424 	struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding);
425 	struct net_shaper *cur;
426 	unsigned long index;
427 
428 	if (!hierarchy)
429 		return;
430 
431 	xa_lock(&hierarchy->shapers);
432 	xa_for_each(&hierarchy->shapers, index, cur) {
433 		if (xa_get_mark(&hierarchy->shapers, index, NET_SHAPER_VALID))
434 			continue;
435 		__xa_erase(&hierarchy->shapers, index);
436 		kfree(cur);
437 	}
438 	xa_unlock(&hierarchy->shapers);
439 }
440 
441 static int net_shaper_parse_handle(const struct nlattr *attr,
442 				   const struct genl_info *info,
443 				   struct net_shaper_handle *handle)
444 {
445 	struct nlattr *tb[NET_SHAPER_A_HANDLE_MAX + 1];
446 	struct nlattr *id_attr;
447 	u32 id = 0;
448 	int ret;
449 
450 	ret = nla_parse_nested(tb, NET_SHAPER_A_HANDLE_MAX, attr,
451 			       net_shaper_handle_nl_policy, info->extack);
452 	if (ret < 0)
453 		return ret;
454 
455 	if (NL_REQ_ATTR_CHECK(info->extack, attr, tb,
456 			      NET_SHAPER_A_HANDLE_SCOPE))
457 		return -EINVAL;
458 
459 	handle->scope = nla_get_u32(tb[NET_SHAPER_A_HANDLE_SCOPE]);
460 
461 	/* The default id for NODE scope shapers is an invalid one
462 	 * to help the 'group' operation discriminate between new
463 	 * NODE shaper creation (ID_UNSPEC) and reuse of existing
464 	 * shaper (any other value).
465 	 */
466 	id_attr = tb[NET_SHAPER_A_HANDLE_ID];
467 	if (id_attr)
468 		id = nla_get_u32(id_attr);
469 	else if (handle->scope == NET_SHAPER_SCOPE_NODE)
470 		id = NET_SHAPER_ID_UNSPEC;
471 
472 	handle->id = id;
473 	return 0;
474 }
475 
476 static int net_shaper_validate_caps(struct net_shaper_binding *binding,
477 				    struct nlattr **tb,
478 				    const struct genl_info *info,
479 				    struct net_shaper *shaper)
480 {
481 	const struct net_shaper_ops *ops = net_shaper_ops(binding);
482 	struct nlattr *bad = NULL;
483 	unsigned long caps = 0;
484 
485 	ops->capabilities(binding, shaper->handle.scope, &caps);
486 
487 	if (tb[NET_SHAPER_A_PRIORITY] &&
488 	    !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_PRIORITY)))
489 		bad = tb[NET_SHAPER_A_PRIORITY];
490 	if (tb[NET_SHAPER_A_WEIGHT] &&
491 	    !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_WEIGHT)))
492 		bad = tb[NET_SHAPER_A_WEIGHT];
493 	if (tb[NET_SHAPER_A_BW_MIN] &&
494 	    !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MIN)))
495 		bad = tb[NET_SHAPER_A_BW_MIN];
496 	if (tb[NET_SHAPER_A_BW_MAX] &&
497 	    !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MAX)))
498 		bad = tb[NET_SHAPER_A_BW_MAX];
499 	if (tb[NET_SHAPER_A_BURST] &&
500 	    !(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_BURST)))
501 		bad = tb[NET_SHAPER_A_BURST];
502 
503 	if (!caps)
504 		bad = tb[NET_SHAPER_A_HANDLE];
505 
506 	if (bad) {
507 		NL_SET_BAD_ATTR(info->extack, bad);
508 		return -EOPNOTSUPP;
509 	}
510 
511 	if (shaper->handle.scope == NET_SHAPER_SCOPE_QUEUE &&
512 	    binding->type == NET_SHAPER_BINDING_TYPE_NETDEV &&
513 	    shaper->handle.id >= binding->netdev->real_num_tx_queues) {
514 		NL_SET_ERR_MSG_FMT(info->extack,
515 				   "Not existing queue id %d max %d",
516 				   shaper->handle.id,
517 				   binding->netdev->real_num_tx_queues);
518 		return -ENOENT;
519 	}
520 
521 	/* The metric is really used only if there is *any* rate-related
522 	 * setting, either in current attributes set or in pre-existing
523 	 * values.
524 	 */
525 	if (shaper->burst || shaper->bw_min || shaper->bw_max) {
526 		u32 metric_cap = NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS +
527 				 shaper->metric;
528 
529 		/* The metric test can fail even when the user did not
530 		 * specify the METRIC attribute. Pointing to rate related
531 		 * attribute will be confusing, as the attribute itself
532 		 * could be indeed supported, with a different metric.
533 		 * Be more specific.
534 		 */
535 		if (!(caps & BIT(metric_cap))) {
536 			NL_SET_ERR_MSG_FMT(info->extack, "Bad metric %d",
537 					   shaper->metric);
538 			return -EOPNOTSUPP;
539 		}
540 	}
541 	return 0;
542 }
543 
544 static int net_shaper_parse_info(struct net_shaper_binding *binding,
545 				 struct nlattr **tb,
546 				 const struct genl_info *info,
547 				 struct net_shaper *shaper,
548 				 bool *exists)
549 {
550 	struct net_shaper *old;
551 	int ret;
552 
553 	/* The shaper handle is the only mandatory attribute. */
554 	if (NL_REQ_ATTR_CHECK(info->extack, NULL, tb, NET_SHAPER_A_HANDLE))
555 		return -EINVAL;
556 
557 	ret = net_shaper_parse_handle(tb[NET_SHAPER_A_HANDLE], info,
558 				      &shaper->handle);
559 	if (ret)
560 		return ret;
561 
562 	if (shaper->handle.scope == NET_SHAPER_SCOPE_UNSPEC) {
563 		NL_SET_BAD_ATTR(info->extack, tb[NET_SHAPER_A_HANDLE]);
564 		return -EINVAL;
565 	}
566 
567 	/* Fetch existing hierarchy, if any, so that user provide info will
568 	 * incrementally update the existing shaper configuration.
569 	 */
570 	old = net_shaper_lookup(binding, &shaper->handle);
571 	if (old)
572 		*shaper = *old;
573 	*exists = !!old;
574 
575 	if (tb[NET_SHAPER_A_METRIC])
576 		shaper->metric = nla_get_u32(tb[NET_SHAPER_A_METRIC]);
577 
578 	if (tb[NET_SHAPER_A_BW_MIN])
579 		shaper->bw_min = nla_get_uint(tb[NET_SHAPER_A_BW_MIN]);
580 
581 	if (tb[NET_SHAPER_A_BW_MAX])
582 		shaper->bw_max = nla_get_uint(tb[NET_SHAPER_A_BW_MAX]);
583 
584 	if (tb[NET_SHAPER_A_BURST])
585 		shaper->burst = nla_get_uint(tb[NET_SHAPER_A_BURST]);
586 
587 	if (tb[NET_SHAPER_A_PRIORITY])
588 		shaper->priority = nla_get_u32(tb[NET_SHAPER_A_PRIORITY]);
589 
590 	if (tb[NET_SHAPER_A_WEIGHT])
591 		shaper->weight = nla_get_u32(tb[NET_SHAPER_A_WEIGHT]);
592 
593 	ret = net_shaper_validate_caps(binding, tb, info, shaper);
594 	if (ret < 0)
595 		return ret;
596 
597 	return 0;
598 }
599 
600 static int net_shaper_validate_nesting(struct net_shaper_binding *binding,
601 				       const struct net_shaper *shaper,
602 				       struct netlink_ext_ack *extack)
603 {
604 	const struct net_shaper_ops *ops = net_shaper_ops(binding);
605 	unsigned long caps = 0;
606 
607 	ops->capabilities(binding, shaper->handle.scope, &caps);
608 	if (!(caps & BIT(NET_SHAPER_A_CAPS_SUPPORT_NESTING))) {
609 		NL_SET_ERR_MSG_FMT(extack,
610 				   "Nesting not supported for scope %d",
611 				   shaper->handle.scope);
612 		return -EOPNOTSUPP;
613 	}
614 	return 0;
615 }
616 
617 /* Fetch the existing leaf and update it with the user-provided
618  * attributes.
619  */
620 static int net_shaper_parse_leaf(struct net_shaper_binding *binding,
621 				 const struct nlattr *attr,
622 				 const struct genl_info *info,
623 				 const struct net_shaper *node,
624 				 struct net_shaper *shaper)
625 {
626 	struct nlattr *tb[NET_SHAPER_A_WEIGHT + 1];
627 	bool exists;
628 	int ret;
629 
630 	ret = nla_parse_nested(tb, NET_SHAPER_A_WEIGHT, attr,
631 			       net_shaper_leaf_info_nl_policy, info->extack);
632 	if (ret < 0)
633 		return ret;
634 
635 	ret = net_shaper_parse_info(binding, tb, info, shaper, &exists);
636 	if (ret < 0)
637 		return ret;
638 
639 	if (shaper->handle.scope != NET_SHAPER_SCOPE_QUEUE) {
640 		NL_SET_BAD_ATTR(info->extack, tb[NET_SHAPER_A_HANDLE]);
641 		return -EINVAL;
642 	}
643 
644 	if (node->handle.scope == NET_SHAPER_SCOPE_NODE) {
645 		ret = net_shaper_validate_nesting(binding, shaper,
646 						  info->extack);
647 		if (ret < 0)
648 			return ret;
649 	}
650 
651 	if (!exists)
652 		net_shaper_default_parent(&shaper->handle, &shaper->parent);
653 	return 0;
654 }
655 
656 /* Alike net_parse_shaper_info(), but additionally allow the user specifying
657  * the shaper's parent handle.
658  */
659 static int net_shaper_parse_node(struct net_shaper_binding *binding,
660 				 struct nlattr **tb,
661 				 const struct genl_info *info,
662 				 struct net_shaper *shaper)
663 {
664 	bool exists;
665 	int ret;
666 
667 	ret = net_shaper_parse_info(binding, tb, info, shaper, &exists);
668 	if (ret)
669 		return ret;
670 
671 	if (shaper->handle.scope != NET_SHAPER_SCOPE_NODE &&
672 	    shaper->handle.scope != NET_SHAPER_SCOPE_NETDEV) {
673 		NL_SET_BAD_ATTR(info->extack, tb[NET_SHAPER_A_HANDLE]);
674 		return -EINVAL;
675 	}
676 
677 	if (tb[NET_SHAPER_A_PARENT]) {
678 		ret = net_shaper_parse_handle(tb[NET_SHAPER_A_PARENT], info,
679 					      &shaper->parent);
680 		if (ret)
681 			return ret;
682 
683 		if (shaper->parent.scope != NET_SHAPER_SCOPE_NODE &&
684 		    shaper->parent.scope != NET_SHAPER_SCOPE_NETDEV) {
685 			NL_SET_BAD_ATTR(info->extack, tb[NET_SHAPER_A_PARENT]);
686 			return -EINVAL;
687 		}
688 	}
689 	return 0;
690 }
691 
692 static int net_shaper_generic_pre(struct genl_info *info, int type)
693 {
694 	struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)info->ctx;
695 
696 	BUILD_BUG_ON(sizeof(*ctx) > sizeof(info->ctx));
697 
698 	return net_shaper_ctx_setup(info, type, ctx);
699 }
700 
701 int net_shaper_nl_pre_doit(const struct genl_split_ops *ops,
702 			   struct sk_buff *skb, struct genl_info *info)
703 {
704 	return net_shaper_generic_pre(info, NET_SHAPER_A_IFINDEX);
705 }
706 
707 static void net_shaper_generic_post(struct genl_info *info)
708 {
709 	net_shaper_ctx_cleanup((struct net_shaper_nl_ctx *)info->ctx);
710 }
711 
712 void net_shaper_nl_post_doit(const struct genl_split_ops *ops,
713 			     struct sk_buff *skb, struct genl_info *info)
714 {
715 	net_shaper_generic_post(info);
716 }
717 
718 int net_shaper_nl_pre_doit_write(const struct genl_split_ops *ops,
719 				struct sk_buff *skb, struct genl_info *info)
720 {
721 	struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)info->ctx;
722 
723 	BUILD_BUG_ON(sizeof(*ctx) > sizeof(info->ctx));
724 
725 	return net_shaper_ctx_setup_lock(info, NET_SHAPER_A_IFINDEX, ctx);
726 }
727 
728 void net_shaper_nl_post_doit_write(const struct genl_split_ops *ops,
729 				   struct sk_buff *skb, struct genl_info *info)
730 {
731 	net_shaper_ctx_cleanup_unlock((struct net_shaper_nl_ctx *)info->ctx);
732 }
733 
734 int net_shaper_nl_pre_dumpit(struct netlink_callback *cb)
735 {
736 	struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)cb->ctx;
737 	const struct genl_info *info = genl_info_dump(cb);
738 
739 	return net_shaper_ctx_setup(info, NET_SHAPER_A_IFINDEX, ctx);
740 }
741 
742 int net_shaper_nl_post_dumpit(struct netlink_callback *cb)
743 {
744 	net_shaper_ctx_cleanup((struct net_shaper_nl_ctx *)cb->ctx);
745 	return 0;
746 }
747 
748 int net_shaper_nl_cap_pre_doit(const struct genl_split_ops *ops,
749 			       struct sk_buff *skb, struct genl_info *info)
750 {
751 	return net_shaper_generic_pre(info, NET_SHAPER_A_CAPS_IFINDEX);
752 }
753 
754 void net_shaper_nl_cap_post_doit(const struct genl_split_ops *ops,
755 				 struct sk_buff *skb, struct genl_info *info)
756 {
757 	net_shaper_generic_post(info);
758 }
759 
760 int net_shaper_nl_cap_pre_dumpit(struct netlink_callback *cb)
761 {
762 	struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)cb->ctx;
763 
764 	return net_shaper_ctx_setup(genl_info_dump(cb),
765 				    NET_SHAPER_A_CAPS_IFINDEX, ctx);
766 }
767 
768 int net_shaper_nl_cap_post_dumpit(struct netlink_callback *cb)
769 {
770 	struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)cb->ctx;
771 
772 	net_shaper_ctx_cleanup(ctx);
773 	return 0;
774 }
775 
776 int net_shaper_nl_get_doit(struct sk_buff *skb, struct genl_info *info)
777 {
778 	struct net_shaper_binding *binding;
779 	struct net_shaper_handle handle;
780 	struct net_shaper *shaper;
781 	struct sk_buff *msg;
782 	int ret;
783 
784 	if (GENL_REQ_ATTR_CHECK(info, NET_SHAPER_A_HANDLE))
785 		return -EINVAL;
786 
787 	binding = net_shaper_binding_from_ctx(info->ctx);
788 	ret = net_shaper_parse_handle(info->attrs[NET_SHAPER_A_HANDLE], info,
789 				      &handle);
790 	if (ret < 0)
791 		return ret;
792 
793 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
794 	if (!msg)
795 		return -ENOMEM;
796 
797 	rcu_read_lock();
798 	shaper = net_shaper_lookup(binding, &handle);
799 	if (!shaper) {
800 		NL_SET_BAD_ATTR(info->extack,
801 				info->attrs[NET_SHAPER_A_HANDLE]);
802 		rcu_read_unlock();
803 		ret = -ENOENT;
804 		goto free_msg;
805 	}
806 
807 	ret = net_shaper_fill_one(msg, binding, shaper, info);
808 	rcu_read_unlock();
809 	if (ret)
810 		goto free_msg;
811 
812 	return genlmsg_reply(msg, info);
813 
814 free_msg:
815 	nlmsg_free(msg);
816 	return ret;
817 }
818 
819 int net_shaper_nl_get_dumpit(struct sk_buff *skb,
820 			     struct netlink_callback *cb)
821 {
822 	struct net_shaper_nl_ctx *ctx = (struct net_shaper_nl_ctx *)cb->ctx;
823 	const struct genl_info *info = genl_info_dump(cb);
824 	struct net_shaper_hierarchy *hierarchy;
825 	struct net_shaper_binding *binding;
826 	struct net_shaper *shaper;
827 	int ret = 0;
828 
829 	/* Don't error out dumps performed before any set operation. */
830 	binding = net_shaper_binding_from_ctx(ctx);
831 
832 	rcu_read_lock();
833 	hierarchy = net_shaper_hierarchy_rcu(binding);
834 	if (!hierarchy)
835 		goto out_unlock;
836 
837 	for (; (shaper = xa_find(&hierarchy->shapers, &ctx->start_index,
838 				 U32_MAX, NET_SHAPER_VALID));
839 	     ctx->start_index++) {
840 		ret = net_shaper_fill_one(skb, binding, shaper, info);
841 		if (ret)
842 			break;
843 	}
844 out_unlock:
845 	rcu_read_unlock();
846 
847 	return ret;
848 }
849 
850 int net_shaper_nl_set_doit(struct sk_buff *skb, struct genl_info *info)
851 {
852 	struct net_shaper_hierarchy *hierarchy;
853 	struct net_shaper_binding *binding;
854 	const struct net_shaper_ops *ops;
855 	struct net_shaper_handle handle;
856 	struct net_shaper shaper = {};
857 	bool exists;
858 	int ret;
859 
860 	binding = net_shaper_binding_from_ctx(info->ctx);
861 
862 	ret = net_shaper_parse_info(binding, info->attrs, info, &shaper,
863 				    &exists);
864 	if (ret)
865 		return ret;
866 
867 	if (!exists)
868 		net_shaper_default_parent(&shaper.handle, &shaper.parent);
869 
870 	hierarchy = net_shaper_hierarchy_setup(binding);
871 	if (!hierarchy)
872 		return -ENOMEM;
873 
874 	/* The 'set' operation can't create node-scope shapers. */
875 	handle = shaper.handle;
876 	if (handle.scope == NET_SHAPER_SCOPE_NODE &&
877 	    !net_shaper_lookup(binding, &handle))
878 		return -ENOENT;
879 
880 	ret = net_shaper_pre_insert(binding, &handle, info->extack);
881 	if (ret)
882 		return ret;
883 
884 	ops = net_shaper_ops(binding);
885 	ret = ops->set(binding, &shaper, info->extack);
886 	if (ret) {
887 		net_shaper_rollback(binding);
888 		return ret;
889 	}
890 
891 	net_shaper_commit(binding, 1, &shaper);
892 
893 	return 0;
894 }
895 
896 static int __net_shaper_delete(struct net_shaper_binding *binding,
897 			       struct net_shaper *shaper,
898 			       struct netlink_ext_ack *extack)
899 {
900 	struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding);
901 	struct net_shaper_handle parent_handle, handle = shaper->handle;
902 	const struct net_shaper_ops *ops = net_shaper_ops(binding);
903 	int ret;
904 
905 again:
906 	parent_handle = shaper->parent;
907 
908 	ret = ops->delete(binding, &handle, extack);
909 	if (ret < 0)
910 		return ret;
911 
912 	xa_erase(&hierarchy->shapers, net_shaper_handle_to_index(&handle));
913 	kfree_rcu(shaper, rcu);
914 
915 	/* Eventually delete the parent, if it is left over with no leaves. */
916 	if (parent_handle.scope == NET_SHAPER_SCOPE_NODE) {
917 		shaper = net_shaper_lookup(binding, &parent_handle);
918 		if (shaper && !--shaper->leaves) {
919 			handle = parent_handle;
920 			goto again;
921 		}
922 	}
923 	return 0;
924 }
925 
926 static int net_shaper_handle_cmp(const struct net_shaper_handle *a,
927 				 const struct net_shaper_handle *b)
928 {
929 	/* Must avoid holes in struct net_shaper_handle. */
930 	BUILD_BUG_ON(sizeof(*a) != 8);
931 
932 	return memcmp(a, b, sizeof(*a));
933 }
934 
935 static int net_shaper_parent_from_leaves(int leaves_count,
936 					 const struct net_shaper *leaves,
937 					 struct net_shaper *node,
938 					 struct netlink_ext_ack *extack)
939 {
940 	struct net_shaper_handle parent = leaves[0].parent;
941 	int i;
942 
943 	for (i = 1; i < leaves_count; ++i) {
944 		if (net_shaper_handle_cmp(&leaves[i].parent, &parent)) {
945 			NL_SET_ERR_MSG_FMT(extack, "All the leaves shapers must have the same old parent");
946 			return -EINVAL;
947 		}
948 	}
949 
950 	node->parent = parent;
951 	return 0;
952 }
953 
954 static int __net_shaper_group(struct net_shaper_binding *binding,
955 			      bool update_node, int leaves_count,
956 			      struct net_shaper *leaves,
957 			      struct net_shaper *node,
958 			      struct netlink_ext_ack *extack)
959 {
960 	const struct net_shaper_ops *ops = net_shaper_ops(binding);
961 	struct net_shaper_handle leaf_handle;
962 	struct net_shaper *parent = NULL;
963 	bool new_node = false;
964 	int i, ret;
965 
966 	if (node->handle.scope == NET_SHAPER_SCOPE_NODE) {
967 		struct net_shaper *cur = NULL;
968 
969 		new_node = node->handle.id == NET_SHAPER_ID_UNSPEC;
970 
971 		if (!new_node) {
972 			cur = net_shaper_lookup(binding, &node->handle);
973 			if (!cur) {
974 				/* The related attribute is not available
975 				 * when reaching here from the delete() op.
976 				 */
977 				NL_SET_ERR_MSG_FMT(extack,
978 						   "Node shaper %d:%d does not exist",
979 						   node->handle.scope,
980 						   node->handle.id);
981 				return -ENOENT;
982 			}
983 		}
984 
985 		/* When unspecified, the node parent scope is inherited from
986 		 * the leaves.
987 		 */
988 		if (node->parent.scope == NET_SHAPER_SCOPE_UNSPEC) {
989 			ret = net_shaper_parent_from_leaves(leaves_count,
990 							    leaves, node,
991 							    extack);
992 			if (ret)
993 				return ret;
994 		}
995 
996 		if (cur && net_shaper_handle_cmp(&cur->parent,
997 						 &node->parent)) {
998 			NL_SET_ERR_MSG_FMT(extack,
999 					   "Cannot reparent node shaper %d:%d",
1000 					   node->handle.scope,
1001 					   node->handle.id);
1002 			return -EOPNOTSUPP;
1003 		}
1004 
1005 	} else {
1006 		net_shaper_default_parent(&node->handle, &node->parent);
1007 	}
1008 
1009 	if (node->parent.scope == NET_SHAPER_SCOPE_NODE) {
1010 		parent = net_shaper_lookup(binding, &node->parent);
1011 		if (!parent) {
1012 			NL_SET_ERR_MSG_FMT(extack, "Node parent shaper %d:%d does not exists",
1013 					   node->parent.scope, node->parent.id);
1014 			return -ENOENT;
1015 		}
1016 
1017 		ret = net_shaper_validate_nesting(binding, node, extack);
1018 		if (ret < 0)
1019 			return ret;
1020 	}
1021 
1022 	if (update_node) {
1023 		/* For newly created node scope shaper, the following will
1024 		 * update the handle, due to id allocation.
1025 		 */
1026 		ret = net_shaper_pre_insert(binding, &node->handle, extack);
1027 		if (ret)
1028 			return ret;
1029 	}
1030 
1031 	for (i = 0; i < leaves_count; ++i) {
1032 		leaf_handle = leaves[i].handle;
1033 
1034 		ret = net_shaper_pre_insert(binding, &leaf_handle, extack);
1035 		if (ret)
1036 			goto rollback;
1037 
1038 		if (!net_shaper_handle_cmp(&leaves[i].parent, &node->handle))
1039 			continue;
1040 
1041 		/* The leaves shapers will be nested to the node, update the
1042 		 * linking accordingly.
1043 		 */
1044 		leaves[i].parent = node->handle;
1045 		node->leaves++;
1046 	}
1047 
1048 	ret = ops->group(binding, leaves_count, leaves, node, extack);
1049 	if (ret < 0)
1050 		goto rollback;
1051 
1052 	/* The node's parent gains a new leaf only when the node itself
1053 	 * is created by this group operation
1054 	 */
1055 	if (new_node && parent)
1056 		parent->leaves++;
1057 	if (update_node)
1058 		net_shaper_commit(binding, 1, node);
1059 	net_shaper_commit(binding, leaves_count, leaves);
1060 	return 0;
1061 
1062 rollback:
1063 	net_shaper_rollback(binding);
1064 	return ret;
1065 }
1066 
1067 static int net_shaper_pre_del_node(struct net_shaper_binding *binding,
1068 				   const struct net_shaper *shaper,
1069 				   struct netlink_ext_ack *extack)
1070 {
1071 	struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding);
1072 	struct net_shaper *cur, *leaves, node = {};
1073 	int ret, leaves_count = 0;
1074 	unsigned long index;
1075 	bool update_node;
1076 
1077 	if (!shaper->leaves)
1078 		return 0;
1079 
1080 	/* Fetch the new node information. */
1081 	node.handle = shaper->parent;
1082 	cur = net_shaper_lookup(binding, &node.handle);
1083 	if (cur) {
1084 		node = *cur;
1085 	} else {
1086 		/* A scope NODE shaper can be nested only to the NETDEV scope
1087 		 * shaper without creating the latter, this check may fail only
1088 		 * if the data is in inconsistent status.
1089 		 */
1090 		if (WARN_ON_ONCE(node.handle.scope != NET_SHAPER_SCOPE_NETDEV))
1091 			return -EINVAL;
1092 	}
1093 
1094 	leaves = kzalloc_objs(struct net_shaper, shaper->leaves);
1095 	if (!leaves)
1096 		return -ENOMEM;
1097 
1098 	/* Build the leaves arrays. */
1099 	xa_for_each(&hierarchy->shapers, index, cur) {
1100 		if (net_shaper_handle_cmp(&cur->parent, &shaper->handle))
1101 			continue;
1102 
1103 		if (WARN_ON_ONCE(leaves_count == shaper->leaves)) {
1104 			ret = -EINVAL;
1105 			goto free;
1106 		}
1107 
1108 		leaves[leaves_count++] = *cur;
1109 	}
1110 
1111 	/* When re-linking to the netdev shaper, avoid the eventual, implicit,
1112 	 * creation of the new node, would be surprising since the user is
1113 	 * doing a delete operation.
1114 	 */
1115 	update_node = node.handle.scope != NET_SHAPER_SCOPE_NETDEV;
1116 	ret = __net_shaper_group(binding, update_node, leaves_count,
1117 				 leaves, &node, extack);
1118 
1119 free:
1120 	kfree(leaves);
1121 	return ret;
1122 }
1123 
1124 int net_shaper_nl_delete_doit(struct sk_buff *skb, struct genl_info *info)
1125 {
1126 	struct net_shaper_hierarchy *hierarchy;
1127 	struct net_shaper_binding *binding;
1128 	struct net_shaper_handle handle;
1129 	struct net_shaper *shaper;
1130 	int ret;
1131 
1132 	if (GENL_REQ_ATTR_CHECK(info, NET_SHAPER_A_HANDLE))
1133 		return -EINVAL;
1134 
1135 	binding = net_shaper_binding_from_ctx(info->ctx);
1136 
1137 	ret = net_shaper_parse_handle(info->attrs[NET_SHAPER_A_HANDLE], info,
1138 				      &handle);
1139 	if (ret)
1140 		return ret;
1141 
1142 	hierarchy = net_shaper_hierarchy(binding);
1143 	if (!hierarchy)
1144 		return -ENOENT;
1145 
1146 	shaper = net_shaper_lookup(binding, &handle);
1147 	if (!shaper)
1148 		return -ENOENT;
1149 
1150 	if (handle.scope == NET_SHAPER_SCOPE_NODE) {
1151 		ret = net_shaper_pre_del_node(binding, shaper, info->extack);
1152 		if (ret)
1153 			return ret;
1154 	}
1155 
1156 	return __net_shaper_delete(binding, shaper, info->extack);
1157 }
1158 
1159 static int net_shaper_group_send_reply(struct net_shaper_binding *binding,
1160 				       const struct net_shaper_handle *handle,
1161 				       struct genl_info *info,
1162 				       struct sk_buff *msg)
1163 {
1164 	void *hdr;
1165 
1166 	hdr = genlmsg_iput(msg, info);
1167 	if (!hdr)
1168 		goto free_msg;
1169 
1170 	if (net_shaper_fill_binding(msg, binding, NET_SHAPER_A_IFINDEX) ||
1171 	    net_shaper_fill_handle(msg, handle, NET_SHAPER_A_HANDLE))
1172 		goto free_msg;
1173 
1174 	genlmsg_end(msg, hdr);
1175 
1176 	return genlmsg_reply(msg, info);
1177 
1178 free_msg:
1179 	/* Should never happen as msg is pre-allocated with enough space. */
1180 	WARN_ONCE(true, "calculated message payload length (%d)",
1181 		  net_shaper_handle_size());
1182 	nlmsg_free(msg);
1183 	return -EMSGSIZE;
1184 }
1185 
1186 int net_shaper_nl_group_doit(struct sk_buff *skb, struct genl_info *info)
1187 {
1188 	struct net_shaper **old_nodes, *leaves, node = {};
1189 	struct net_shaper_hierarchy *hierarchy;
1190 	struct net_shaper_binding *binding;
1191 	int i, ret, rem, leaves_count;
1192 	int old_nodes_count = 0;
1193 	struct sk_buff *msg;
1194 	struct nlattr *attr;
1195 
1196 	if (GENL_REQ_ATTR_CHECK(info, NET_SHAPER_A_LEAVES))
1197 		return -EINVAL;
1198 
1199 	binding = net_shaper_binding_from_ctx(info->ctx);
1200 
1201 	/* The group operation is optional. */
1202 	if (!net_shaper_ops(binding)->group)
1203 		return -EOPNOTSUPP;
1204 
1205 	leaves_count = net_shaper_list_len(info, NET_SHAPER_A_LEAVES);
1206 	if (!leaves_count) {
1207 		NL_SET_BAD_ATTR(info->extack,
1208 				info->attrs[NET_SHAPER_A_LEAVES]);
1209 		return -EINVAL;
1210 	}
1211 
1212 	leaves = kcalloc(leaves_count, sizeof(struct net_shaper) +
1213 			 sizeof(struct net_shaper *), GFP_KERNEL);
1214 	if (!leaves)
1215 		return -ENOMEM;
1216 	old_nodes = (void *)&leaves[leaves_count];
1217 
1218 	ret = net_shaper_parse_node(binding, info->attrs, info, &node);
1219 	if (ret)
1220 		goto free_leaves;
1221 
1222 	i = 0;
1223 	nla_for_each_attr_type(attr, NET_SHAPER_A_LEAVES,
1224 			       genlmsg_data(info->genlhdr),
1225 			       genlmsg_len(info->genlhdr), rem) {
1226 		if (WARN_ON_ONCE(i >= leaves_count))
1227 			goto free_leaves;
1228 
1229 		ret = net_shaper_parse_leaf(binding, attr, info,
1230 					    &node, &leaves[i]);
1231 		if (ret)
1232 			goto free_leaves;
1233 		i++;
1234 	}
1235 
1236 	/* Prepare the msg reply in advance, to avoid device operation
1237 	 * rollback on allocation failure.
1238 	 */
1239 	msg = genlmsg_new(net_shaper_handle_size(), GFP_KERNEL);
1240 	if (!msg)
1241 		goto free_leaves;
1242 
1243 	hierarchy = net_shaper_hierarchy_setup(binding);
1244 	if (!hierarchy) {
1245 		ret = -ENOMEM;
1246 		goto free_msg;
1247 	}
1248 
1249 	/* Record the node shapers that this group() operation can make
1250 	 * childless for later cleanup.
1251 	 */
1252 	for (i = 0; i < leaves_count; i++) {
1253 		if (leaves[i].parent.scope == NET_SHAPER_SCOPE_NODE &&
1254 		    net_shaper_handle_cmp(&leaves[i].parent, &node.handle)) {
1255 			struct net_shaper *tmp;
1256 
1257 			tmp = net_shaper_lookup(binding, &leaves[i].parent);
1258 			if (!tmp)
1259 				continue;
1260 
1261 			old_nodes[old_nodes_count++] = tmp;
1262 		}
1263 	}
1264 
1265 	ret = __net_shaper_group(binding, true, leaves_count, leaves, &node,
1266 				 info->extack);
1267 	if (ret)
1268 		goto free_msg;
1269 
1270 	/* Check if we need to delete any node left alone by the new leaves
1271 	 * linkage.
1272 	 */
1273 	for (i = 0; i < old_nodes_count; ++i) {
1274 		struct net_shaper *tmp = old_nodes[i];
1275 
1276 		if (--tmp->leaves > 0)
1277 			continue;
1278 
1279 		/* Errors here are not fatal: the grouping operation is
1280 		 * completed, and user-space can still explicitly clean-up
1281 		 * left-over nodes.
1282 		 */
1283 		__net_shaper_delete(binding, tmp, info->extack);
1284 	}
1285 
1286 	ret = net_shaper_group_send_reply(binding, &node.handle, info, msg);
1287 	if (ret)
1288 		GENL_SET_ERR_MSG_FMT(info, "Can't send reply");
1289 
1290 free_leaves:
1291 	kfree(leaves);
1292 	return ret;
1293 
1294 free_msg:
1295 	kfree_skb(msg);
1296 	goto free_leaves;
1297 }
1298 
1299 static int
1300 net_shaper_cap_fill_one(struct sk_buff *msg,
1301 			struct net_shaper_binding *binding,
1302 			enum net_shaper_scope scope, unsigned long flags,
1303 			const struct genl_info *info)
1304 {
1305 	unsigned long cur;
1306 	void *hdr;
1307 
1308 	hdr = genlmsg_iput(msg, info);
1309 	if (!hdr)
1310 		return -EMSGSIZE;
1311 
1312 	if (net_shaper_fill_binding(msg, binding, NET_SHAPER_A_CAPS_IFINDEX) ||
1313 	    nla_put_u32(msg, NET_SHAPER_A_CAPS_SCOPE, scope))
1314 		goto nla_put_failure;
1315 
1316 	for (cur = NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS;
1317 	     cur <= NET_SHAPER_A_CAPS_MAX; ++cur) {
1318 		if (flags & BIT(cur) && nla_put_flag(msg, cur))
1319 			goto nla_put_failure;
1320 	}
1321 
1322 	genlmsg_end(msg, hdr);
1323 
1324 	return 0;
1325 
1326 nla_put_failure:
1327 	genlmsg_cancel(msg, hdr);
1328 	return -EMSGSIZE;
1329 }
1330 
1331 int net_shaper_nl_cap_get_doit(struct sk_buff *skb, struct genl_info *info)
1332 {
1333 	struct net_shaper_binding *binding;
1334 	const struct net_shaper_ops *ops;
1335 	enum net_shaper_scope scope;
1336 	unsigned long flags = 0;
1337 	struct sk_buff *msg;
1338 	int ret;
1339 
1340 	if (GENL_REQ_ATTR_CHECK(info, NET_SHAPER_A_CAPS_SCOPE))
1341 		return -EINVAL;
1342 
1343 	binding = net_shaper_binding_from_ctx(info->ctx);
1344 	scope = nla_get_u32(info->attrs[NET_SHAPER_A_CAPS_SCOPE]);
1345 	ops = net_shaper_ops(binding);
1346 	ops->capabilities(binding, scope, &flags);
1347 	if (!flags)
1348 		return -EOPNOTSUPP;
1349 
1350 	msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1351 	if (!msg)
1352 		return -ENOMEM;
1353 
1354 	ret = net_shaper_cap_fill_one(msg, binding, scope, flags, info);
1355 	if (ret)
1356 		goto free_msg;
1357 
1358 	return genlmsg_reply(msg, info);
1359 
1360 free_msg:
1361 	nlmsg_free(msg);
1362 	return ret;
1363 }
1364 
1365 int net_shaper_nl_cap_get_dumpit(struct sk_buff *skb,
1366 				 struct netlink_callback *cb)
1367 {
1368 	const struct genl_info *info = genl_info_dump(cb);
1369 	struct net_shaper_binding *binding;
1370 	const struct net_shaper_ops *ops;
1371 	enum net_shaper_scope scope;
1372 	int ret;
1373 
1374 	binding = net_shaper_binding_from_ctx(cb->ctx);
1375 	ops = net_shaper_ops(binding);
1376 	for (scope = 0; scope <= NET_SHAPER_SCOPE_MAX; ++scope) {
1377 		unsigned long flags = 0;
1378 
1379 		ops->capabilities(binding, scope, &flags);
1380 		if (!flags)
1381 			continue;
1382 
1383 		ret = net_shaper_cap_fill_one(skb, binding, scope, flags,
1384 					      info);
1385 		if (ret)
1386 			return ret;
1387 	}
1388 
1389 	return 0;
1390 }
1391 
1392 static void net_shaper_flush(struct net_shaper_binding *binding)
1393 {
1394 	struct net_shaper_hierarchy *hierarchy = net_shaper_hierarchy(binding);
1395 	struct net_shaper *cur;
1396 	unsigned long index;
1397 
1398 	if (!hierarchy)
1399 		return;
1400 
1401 	xa_lock(&hierarchy->shapers);
1402 	xa_for_each(&hierarchy->shapers, index, cur) {
1403 		__xa_erase(&hierarchy->shapers, index);
1404 		kfree(cur);
1405 	}
1406 	xa_unlock(&hierarchy->shapers);
1407 
1408 	kfree(hierarchy);
1409 }
1410 
1411 void net_shaper_flush_netdev(struct net_device *dev)
1412 {
1413 	struct net_shaper_binding binding = {
1414 		.type = NET_SHAPER_BINDING_TYPE_NETDEV,
1415 		.netdev = dev,
1416 	};
1417 
1418 	net_shaper_flush(&binding);
1419 }
1420 
1421 void net_shaper_set_real_num_tx_queues(struct net_device *dev,
1422 				       unsigned int txq)
1423 {
1424 	struct net_shaper_hierarchy *hierarchy;
1425 	struct net_shaper_binding binding;
1426 	int i;
1427 
1428 	binding.type = NET_SHAPER_BINDING_TYPE_NETDEV;
1429 	binding.netdev = dev;
1430 	hierarchy = net_shaper_hierarchy(&binding);
1431 	if (!hierarchy)
1432 		return;
1433 
1434 	/* Only drivers implementing shapers support ensure
1435 	 * the lock is acquired in advance.
1436 	 */
1437 	netdev_assert_locked(dev);
1438 
1439 	/* Take action only when decreasing the tx queue number. */
1440 	for (i = txq; i < dev->real_num_tx_queues; ++i) {
1441 		struct net_shaper_handle handle, parent_handle;
1442 		struct net_shaper *shaper;
1443 		u32 index;
1444 
1445 		handle.scope = NET_SHAPER_SCOPE_QUEUE;
1446 		handle.id = i;
1447 		shaper = net_shaper_lookup(&binding, &handle);
1448 		if (!shaper)
1449 			continue;
1450 
1451 		/* Don't touch the H/W for the queue shaper, the drivers already
1452 		 * deleted the queue and related resources.
1453 		 */
1454 		parent_handle = shaper->parent;
1455 		index = net_shaper_handle_to_index(&handle);
1456 		xa_erase(&hierarchy->shapers, index);
1457 		kfree_rcu(shaper, rcu);
1458 
1459 		/* The recursion on parent does the full job. */
1460 		if (parent_handle.scope != NET_SHAPER_SCOPE_NODE)
1461 			continue;
1462 
1463 		shaper = net_shaper_lookup(&binding, &parent_handle);
1464 		if (shaper && !--shaper->leaves)
1465 			__net_shaper_delete(&binding, shaper, NULL);
1466 	}
1467 }
1468 
1469 static int __init shaper_init(void)
1470 {
1471 	return genl_register_family(&net_shaper_nl_family);
1472 }
1473 
1474 subsys_initcall(shaper_init);
1475