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