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