shaper.c 37 KB

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