Debian Codegraph @

net/shaper/shaper.c

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