sch_ets.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * net/sched/sch_ets.c Enhanced Transmission Selection scheduler
  4. *
  5. * Description
  6. * -----------
  7. *
  8. * The Enhanced Transmission Selection scheduler is a classful queuing
  9. * discipline that merges functionality of PRIO and DRR qdiscs in one scheduler.
  10. * ETS makes it easy to configure a set of strict and bandwidth-sharing bands to
  11. * implement the transmission selection described in 802.1Qaz.
  12. *
  13. * Although ETS is technically classful, it's not possible to add and remove
  14. * classes at will. Instead one specifies number of classes, how many are
  15. * PRIO-like and how many DRR-like, and quanta for the latter.
  16. *
  17. * Algorithm
  18. * ---------
  19. *
  20. * The strict classes, if any, are tried for traffic first: first band 0, if it
  21. * has no traffic then band 1, etc.
  22. *
  23. * When there is no traffic in any of the strict queues, the bandwidth-sharing
  24. * ones are tried next. Each band is assigned a deficit counter, initialized to
  25. * "quantum" of that band. ETS maintains a list of active bandwidth-sharing
  26. * bands whose qdiscs are non-empty. A packet is dequeued from the band at the
  27. * head of the list if the packet size is smaller or equal to the deficit
  28. * counter. If the counter is too small, it is increased by "quantum" and the
  29. * scheduler moves on to the next band in the active list.
  30. */
  31. #include <linux/module.h>
  32. #include <net/gen_stats.h>
  33. #include <net/netlink.h>
  34. #include <net/pkt_cls.h>
  35. #include <net/pkt_sched.h>
  36. #include <net/sch_generic.h>
  37. struct ets_class {
  38. struct list_head alist; /* In struct ets_sched.active. */
  39. struct Qdisc *qdisc;
  40. u32 quantum;
  41. u32 deficit;
  42. struct gnet_stats_basic_sync bstats;
  43. struct gnet_stats_queue qstats;
  44. };
  45. struct ets_sched {
  46. struct list_head active;
  47. struct tcf_proto __rcu *filter_list;
  48. struct tcf_block *block;
  49. unsigned int nbands;
  50. unsigned int nstrict;
  51. u8 prio2band[TC_PRIO_MAX + 1];
  52. struct ets_class classes[TCQ_ETS_MAX_BANDS];
  53. };
  54. static const struct nla_policy ets_policy[TCA_ETS_MAX + 1] = {
  55. [TCA_ETS_NBANDS] = { .type = NLA_U8 },
  56. [TCA_ETS_NSTRICT] = { .type = NLA_U8 },
  57. [TCA_ETS_QUANTA] = { .type = NLA_NESTED },
  58. [TCA_ETS_PRIOMAP] = { .type = NLA_NESTED },
  59. };
  60. static const struct nla_policy ets_priomap_policy[TCA_ETS_MAX + 1] = {
  61. [TCA_ETS_PRIOMAP_BAND] = { .type = NLA_U8 },
  62. };
  63. static const struct nla_policy ets_quanta_policy[TCA_ETS_MAX + 1] = {
  64. [TCA_ETS_QUANTA_BAND] = { .type = NLA_U32 },
  65. };
  66. static const struct nla_policy ets_class_policy[TCA_ETS_MAX + 1] = {
  67. [TCA_ETS_QUANTA_BAND] = { .type = NLA_U32 },
  68. };
  69. static int ets_quantum_parse(struct Qdisc *sch, const struct nlattr *attr,
  70. unsigned int *quantum,
  71. struct netlink_ext_ack *extack)
  72. {
  73. *quantum = nla_get_u32(attr);
  74. if (!*quantum) {
  75. NL_SET_ERR_MSG(extack, "ETS quantum cannot be zero");
  76. return -EINVAL;
  77. }
  78. return 0;
  79. }
  80. static struct ets_class *
  81. ets_class_from_arg(struct Qdisc *sch, unsigned long arg)
  82. {
  83. struct ets_sched *q = qdisc_priv(sch);
  84. if (arg == 0 || arg > q->nbands)
  85. return NULL;
  86. return &q->classes[arg - 1];
  87. }
  88. static u32 ets_class_id(struct Qdisc *sch, const struct ets_class *cl)
  89. {
  90. struct ets_sched *q = qdisc_priv(sch);
  91. int band = cl - q->classes;
  92. return TC_H_MAKE(sch->handle, band + 1);
  93. }
  94. static void ets_offload_change(struct Qdisc *sch)
  95. {
  96. struct net_device *dev = qdisc_dev(sch);
  97. struct ets_sched *q = qdisc_priv(sch);
  98. struct tc_ets_qopt_offload qopt;
  99. unsigned int w_psum_prev = 0;
  100. unsigned int q_psum = 0;
  101. unsigned int q_sum = 0;
  102. unsigned int quantum;
  103. unsigned int w_psum;
  104. unsigned int weight;
  105. unsigned int i;
  106. if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
  107. return;
  108. qopt.command = TC_ETS_REPLACE;
  109. qopt.handle = sch->handle;
  110. qopt.parent = sch->parent;
  111. qopt.replace_params.bands = q->nbands;
  112. qopt.replace_params.qstats = &sch->qstats;
  113. memcpy(&qopt.replace_params.priomap,
  114. q->prio2band, sizeof(q->prio2band));
  115. for (i = 0; i < q->nbands; i++)
  116. q_sum += q->classes[i].quantum;
  117. for (i = 0; i < q->nbands; i++) {
  118. quantum = q->classes[i].quantum;
  119. q_psum += quantum;
  120. w_psum = quantum ? q_psum * 100 / q_sum : 0;
  121. weight = w_psum - w_psum_prev;
  122. w_psum_prev = w_psum;
  123. qopt.replace_params.quanta[i] = quantum;
  124. qopt.replace_params.weights[i] = weight;
  125. }
  126. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_ETS, &qopt);
  127. }
  128. static void ets_offload_destroy(struct Qdisc *sch)
  129. {
  130. struct net_device *dev = qdisc_dev(sch);
  131. struct tc_ets_qopt_offload qopt;
  132. if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
  133. return;
  134. qopt.command = TC_ETS_DESTROY;
  135. qopt.handle = sch->handle;
  136. qopt.parent = sch->parent;
  137. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_ETS, &qopt);
  138. }
  139. static void ets_offload_graft(struct Qdisc *sch, struct Qdisc *new,
  140. struct Qdisc *old, unsigned long arg,
  141. struct netlink_ext_ack *extack)
  142. {
  143. struct net_device *dev = qdisc_dev(sch);
  144. struct tc_ets_qopt_offload qopt;
  145. qopt.command = TC_ETS_GRAFT;
  146. qopt.handle = sch->handle;
  147. qopt.parent = sch->parent;
  148. qopt.graft_params.band = arg - 1;
  149. qopt.graft_params.child_handle = new->handle;
  150. qdisc_offload_graft_helper(dev, sch, new, old, TC_SETUP_QDISC_ETS,
  151. &qopt, extack);
  152. }
  153. static int ets_offload_dump(struct Qdisc *sch)
  154. {
  155. struct tc_ets_qopt_offload qopt;
  156. qopt.command = TC_ETS_STATS;
  157. qopt.handle = sch->handle;
  158. qopt.parent = sch->parent;
  159. qopt.stats.bstats = &sch->bstats;
  160. qopt.stats.qstats = &sch->qstats;
  161. return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_ETS, &qopt);
  162. }
  163. static bool ets_class_is_strict(struct ets_sched *q, const struct ets_class *cl)
  164. {
  165. unsigned int band = cl - q->classes;
  166. return band < q->nstrict;
  167. }
  168. static int ets_class_change(struct Qdisc *sch, u32 classid, u32 parentid,
  169. struct nlattr **tca, unsigned long *arg,
  170. struct netlink_ext_ack *extack)
  171. {
  172. struct ets_class *cl = ets_class_from_arg(sch, *arg);
  173. struct ets_sched *q = qdisc_priv(sch);
  174. struct nlattr *opt = tca[TCA_OPTIONS];
  175. struct nlattr *tb[TCA_ETS_MAX + 1];
  176. unsigned int quantum;
  177. int err;
  178. /* Classes can be added and removed only through Qdisc_ops.change
  179. * interface.
  180. */
  181. if (!cl) {
  182. NL_SET_ERR_MSG(extack, "Fine-grained class addition and removal is not supported");
  183. return -EOPNOTSUPP;
  184. }
  185. if (!opt) {
  186. NL_SET_ERR_MSG(extack, "ETS options are required for this operation");
  187. return -EINVAL;
  188. }
  189. err = nla_parse_nested(tb, TCA_ETS_MAX, opt, ets_class_policy, extack);
  190. if (err < 0)
  191. return err;
  192. if (!tb[TCA_ETS_QUANTA_BAND])
  193. /* Nothing to configure. */
  194. return 0;
  195. if (ets_class_is_strict(q, cl)) {
  196. NL_SET_ERR_MSG(extack, "Strict bands do not have a configurable quantum");
  197. return -EINVAL;
  198. }
  199. err = ets_quantum_parse(sch, tb[TCA_ETS_QUANTA_BAND], &quantum,
  200. extack);
  201. if (err)
  202. return err;
  203. sch_tree_lock(sch);
  204. cl->quantum = quantum;
  205. sch_tree_unlock(sch);
  206. ets_offload_change(sch);
  207. return 0;
  208. }
  209. static int ets_class_graft(struct Qdisc *sch, unsigned long arg,
  210. struct Qdisc *new, struct Qdisc **old,
  211. struct netlink_ext_ack *extack)
  212. {
  213. struct ets_class *cl = ets_class_from_arg(sch, arg);
  214. if (!new) {
  215. new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  216. ets_class_id(sch, cl), NULL);
  217. if (!new)
  218. new = &noop_qdisc;
  219. else
  220. qdisc_hash_add(new, true);
  221. }
  222. *old = qdisc_replace(sch, new, &cl->qdisc);
  223. ets_offload_graft(sch, new, *old, arg, extack);
  224. return 0;
  225. }
  226. static struct Qdisc *ets_class_leaf(struct Qdisc *sch, unsigned long arg)
  227. {
  228. struct ets_class *cl = ets_class_from_arg(sch, arg);
  229. return cl->qdisc;
  230. }
  231. static unsigned long ets_class_find(struct Qdisc *sch, u32 classid)
  232. {
  233. unsigned long band = TC_H_MIN(classid);
  234. struct ets_sched *q = qdisc_priv(sch);
  235. if (band - 1 >= q->nbands)
  236. return 0;
  237. return band;
  238. }
  239. static void ets_class_qlen_notify(struct Qdisc *sch, unsigned long arg)
  240. {
  241. struct ets_class *cl = ets_class_from_arg(sch, arg);
  242. struct ets_sched *q = qdisc_priv(sch);
  243. /* We get notified about zero-length child Qdiscs as well if they are
  244. * offloaded. Those aren't on the active list though, so don't attempt
  245. * to remove them.
  246. */
  247. if (!ets_class_is_strict(q, cl) && sch->q.qlen)
  248. list_del(&cl->alist);
  249. }
  250. static int ets_class_dump(struct Qdisc *sch, unsigned long arg,
  251. struct sk_buff *skb, struct tcmsg *tcm)
  252. {
  253. struct ets_class *cl = ets_class_from_arg(sch, arg);
  254. struct ets_sched *q = qdisc_priv(sch);
  255. struct nlattr *nest;
  256. tcm->tcm_parent = TC_H_ROOT;
  257. tcm->tcm_handle = ets_class_id(sch, cl);
  258. tcm->tcm_info = cl->qdisc->handle;
  259. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  260. if (!nest)
  261. goto nla_put_failure;
  262. if (!ets_class_is_strict(q, cl)) {
  263. if (nla_put_u32(skb, TCA_ETS_QUANTA_BAND, cl->quantum))
  264. goto nla_put_failure;
  265. }
  266. return nla_nest_end(skb, nest);
  267. nla_put_failure:
  268. nla_nest_cancel(skb, nest);
  269. return -EMSGSIZE;
  270. }
  271. static int ets_class_dump_stats(struct Qdisc *sch, unsigned long arg,
  272. struct gnet_dump *d)
  273. {
  274. struct ets_class *cl = ets_class_from_arg(sch, arg);
  275. struct Qdisc *cl_q = cl->qdisc;
  276. if (gnet_stats_copy_basic(d, NULL, &cl_q->bstats, true) < 0 ||
  277. qdisc_qstats_copy(d, cl_q) < 0)
  278. return -1;
  279. return 0;
  280. }
  281. static void ets_qdisc_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  282. {
  283. struct ets_sched *q = qdisc_priv(sch);
  284. int i;
  285. if (arg->stop)
  286. return;
  287. for (i = 0; i < q->nbands; i++) {
  288. if (!tc_qdisc_stats_dump(sch, i + 1, arg))
  289. break;
  290. }
  291. }
  292. static struct tcf_block *
  293. ets_qdisc_tcf_block(struct Qdisc *sch, unsigned long cl,
  294. struct netlink_ext_ack *extack)
  295. {
  296. struct ets_sched *q = qdisc_priv(sch);
  297. if (cl) {
  298. NL_SET_ERR_MSG(extack, "ETS classid must be zero");
  299. return NULL;
  300. }
  301. return q->block;
  302. }
  303. static unsigned long ets_qdisc_bind_tcf(struct Qdisc *sch, unsigned long parent,
  304. u32 classid)
  305. {
  306. return ets_class_find(sch, classid);
  307. }
  308. static void ets_qdisc_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  309. {
  310. }
  311. static struct ets_class *ets_classify(struct sk_buff *skb, struct Qdisc *sch,
  312. int *qerr)
  313. {
  314. struct ets_sched *q = qdisc_priv(sch);
  315. u32 band = skb->priority;
  316. struct tcf_result res;
  317. struct tcf_proto *fl;
  318. int err;
  319. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  320. if (TC_H_MAJ(skb->priority) != sch->handle) {
  321. fl = rcu_dereference_bh(q->filter_list);
  322. err = tcf_classify(skb, NULL, fl, &res, false);
  323. #ifdef CONFIG_NET_CLS_ACT
  324. switch (err) {
  325. case TC_ACT_STOLEN:
  326. case TC_ACT_QUEUED:
  327. case TC_ACT_TRAP:
  328. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  329. fallthrough;
  330. case TC_ACT_SHOT:
  331. return NULL;
  332. }
  333. #endif
  334. if (!fl || err < 0) {
  335. if (TC_H_MAJ(band))
  336. band = 0;
  337. return &q->classes[q->prio2band[band & TC_PRIO_MAX]];
  338. }
  339. band = res.classid;
  340. }
  341. band = TC_H_MIN(band) - 1;
  342. if (band >= q->nbands)
  343. return &q->classes[q->prio2band[0]];
  344. return &q->classes[band];
  345. }
  346. static int ets_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  347. struct sk_buff **to_free)
  348. {
  349. unsigned int len = qdisc_pkt_len(skb);
  350. struct ets_sched *q = qdisc_priv(sch);
  351. struct ets_class *cl;
  352. int err = 0;
  353. bool first;
  354. cl = ets_classify(skb, sch, &err);
  355. if (!cl) {
  356. if (err & __NET_XMIT_BYPASS)
  357. qdisc_qstats_drop(sch);
  358. __qdisc_drop(skb, to_free);
  359. return err;
  360. }
  361. first = !cl->qdisc->q.qlen;
  362. err = qdisc_enqueue(skb, cl->qdisc, to_free);
  363. if (unlikely(err != NET_XMIT_SUCCESS)) {
  364. if (net_xmit_drop_count(err)) {
  365. cl->qstats.drops++;
  366. qdisc_qstats_drop(sch);
  367. }
  368. return err;
  369. }
  370. if (first && !ets_class_is_strict(q, cl)) {
  371. list_add_tail(&cl->alist, &q->active);
  372. cl->deficit = cl->quantum;
  373. }
  374. sch->qstats.backlog += len;
  375. sch->q.qlen++;
  376. return err;
  377. }
  378. static struct sk_buff *
  379. ets_qdisc_dequeue_skb(struct Qdisc *sch, struct sk_buff *skb)
  380. {
  381. qdisc_bstats_update(sch, skb);
  382. qdisc_qstats_backlog_dec(sch, skb);
  383. sch->q.qlen--;
  384. return skb;
  385. }
  386. static struct sk_buff *ets_qdisc_dequeue(struct Qdisc *sch)
  387. {
  388. struct ets_sched *q = qdisc_priv(sch);
  389. struct ets_class *cl;
  390. struct sk_buff *skb;
  391. unsigned int band;
  392. unsigned int len;
  393. while (1) {
  394. for (band = 0; band < q->nstrict; band++) {
  395. cl = &q->classes[band];
  396. skb = qdisc_dequeue_peeked(cl->qdisc);
  397. if (skb)
  398. return ets_qdisc_dequeue_skb(sch, skb);
  399. }
  400. if (list_empty(&q->active))
  401. goto out;
  402. cl = list_first_entry(&q->active, struct ets_class, alist);
  403. skb = cl->qdisc->ops->peek(cl->qdisc);
  404. if (!skb) {
  405. qdisc_warn_nonwc(__func__, cl->qdisc);
  406. goto out;
  407. }
  408. len = qdisc_pkt_len(skb);
  409. if (len <= cl->deficit) {
  410. cl->deficit -= len;
  411. skb = qdisc_dequeue_peeked(cl->qdisc);
  412. if (unlikely(!skb))
  413. goto out;
  414. if (cl->qdisc->q.qlen == 0)
  415. list_del(&cl->alist);
  416. return ets_qdisc_dequeue_skb(sch, skb);
  417. }
  418. cl->deficit += cl->quantum;
  419. list_move_tail(&cl->alist, &q->active);
  420. }
  421. out:
  422. return NULL;
  423. }
  424. static int ets_qdisc_priomap_parse(struct nlattr *priomap_attr,
  425. unsigned int nbands, u8 *priomap,
  426. struct netlink_ext_ack *extack)
  427. {
  428. const struct nlattr *attr;
  429. int prio = 0;
  430. u8 band;
  431. int rem;
  432. int err;
  433. err = __nla_validate_nested(priomap_attr, TCA_ETS_MAX,
  434. ets_priomap_policy, NL_VALIDATE_STRICT,
  435. extack);
  436. if (err)
  437. return err;
  438. nla_for_each_nested(attr, priomap_attr, rem) {
  439. switch (nla_type(attr)) {
  440. case TCA_ETS_PRIOMAP_BAND:
  441. if (prio > TC_PRIO_MAX) {
  442. NL_SET_ERR_MSG_MOD(extack, "Too many priorities in ETS priomap");
  443. return -EINVAL;
  444. }
  445. band = nla_get_u8(attr);
  446. if (band >= nbands) {
  447. NL_SET_ERR_MSG_MOD(extack, "Invalid band number in ETS priomap");
  448. return -EINVAL;
  449. }
  450. priomap[prio++] = band;
  451. break;
  452. default:
  453. WARN_ON_ONCE(1); /* Validate should have caught this. */
  454. return -EINVAL;
  455. }
  456. }
  457. return 0;
  458. }
  459. static int ets_qdisc_quanta_parse(struct Qdisc *sch, struct nlattr *quanta_attr,
  460. unsigned int nbands, unsigned int nstrict,
  461. unsigned int *quanta,
  462. struct netlink_ext_ack *extack)
  463. {
  464. const struct nlattr *attr;
  465. int band = nstrict;
  466. int rem;
  467. int err;
  468. err = __nla_validate_nested(quanta_attr, TCA_ETS_MAX,
  469. ets_quanta_policy, NL_VALIDATE_STRICT,
  470. extack);
  471. if (err < 0)
  472. return err;
  473. nla_for_each_nested(attr, quanta_attr, rem) {
  474. switch (nla_type(attr)) {
  475. case TCA_ETS_QUANTA_BAND:
  476. if (band >= nbands) {
  477. NL_SET_ERR_MSG_MOD(extack, "ETS quanta has more values than bands");
  478. return -EINVAL;
  479. }
  480. err = ets_quantum_parse(sch, attr, &quanta[band++],
  481. extack);
  482. if (err)
  483. return err;
  484. break;
  485. default:
  486. WARN_ON_ONCE(1); /* Validate should have caught this. */
  487. return -EINVAL;
  488. }
  489. }
  490. return 0;
  491. }
  492. static int ets_qdisc_change(struct Qdisc *sch, struct nlattr *opt,
  493. struct netlink_ext_ack *extack)
  494. {
  495. unsigned int quanta[TCQ_ETS_MAX_BANDS] = {0};
  496. struct Qdisc *queues[TCQ_ETS_MAX_BANDS];
  497. struct ets_sched *q = qdisc_priv(sch);
  498. struct nlattr *tb[TCA_ETS_MAX + 1];
  499. unsigned int oldbands = q->nbands;
  500. u8 priomap[TC_PRIO_MAX + 1];
  501. unsigned int nstrict = 0;
  502. unsigned int nbands;
  503. unsigned int i;
  504. int err;
  505. err = nla_parse_nested(tb, TCA_ETS_MAX, opt, ets_policy, extack);
  506. if (err < 0)
  507. return err;
  508. if (!tb[TCA_ETS_NBANDS]) {
  509. NL_SET_ERR_MSG_MOD(extack, "Number of bands is a required argument");
  510. return -EINVAL;
  511. }
  512. nbands = nla_get_u8(tb[TCA_ETS_NBANDS]);
  513. if (nbands < 1 || nbands > TCQ_ETS_MAX_BANDS) {
  514. NL_SET_ERR_MSG_MOD(extack, "Invalid number of bands");
  515. return -EINVAL;
  516. }
  517. /* Unless overridden, traffic goes to the last band. */
  518. memset(priomap, nbands - 1, sizeof(priomap));
  519. if (tb[TCA_ETS_NSTRICT]) {
  520. nstrict = nla_get_u8(tb[TCA_ETS_NSTRICT]);
  521. if (nstrict > nbands) {
  522. NL_SET_ERR_MSG_MOD(extack, "Invalid number of strict bands");
  523. return -EINVAL;
  524. }
  525. }
  526. if (tb[TCA_ETS_PRIOMAP]) {
  527. err = ets_qdisc_priomap_parse(tb[TCA_ETS_PRIOMAP],
  528. nbands, priomap, extack);
  529. if (err)
  530. return err;
  531. }
  532. if (tb[TCA_ETS_QUANTA]) {
  533. err = ets_qdisc_quanta_parse(sch, tb[TCA_ETS_QUANTA],
  534. nbands, nstrict, quanta, extack);
  535. if (err)
  536. return err;
  537. }
  538. /* If there are more bands than strict + quanta provided, the remaining
  539. * ones are ETS with quantum of MTU. Initialize the missing values here.
  540. */
  541. for (i = nstrict; i < nbands; i++) {
  542. if (!quanta[i])
  543. quanta[i] = psched_mtu(qdisc_dev(sch));
  544. }
  545. /* Before commit, make sure we can allocate all new qdiscs */
  546. for (i = oldbands; i < nbands; i++) {
  547. queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  548. ets_class_id(sch, &q->classes[i]),
  549. extack);
  550. if (!queues[i]) {
  551. while (i > oldbands)
  552. qdisc_put(queues[--i]);
  553. return -ENOMEM;
  554. }
  555. }
  556. sch_tree_lock(sch);
  557. WRITE_ONCE(q->nbands, nbands);
  558. for (i = nstrict; i < q->nstrict; i++) {
  559. if (q->classes[i].qdisc->q.qlen) {
  560. list_add_tail(&q->classes[i].alist, &q->active);
  561. q->classes[i].deficit = quanta[i];
  562. }
  563. }
  564. for (i = q->nbands; i < oldbands; i++) {
  565. if (i >= q->nstrict && q->classes[i].qdisc->q.qlen)
  566. list_del(&q->classes[i].alist);
  567. qdisc_tree_flush_backlog(q->classes[i].qdisc);
  568. }
  569. WRITE_ONCE(q->nstrict, nstrict);
  570. memcpy(q->prio2band, priomap, sizeof(priomap));
  571. for (i = 0; i < q->nbands; i++)
  572. WRITE_ONCE(q->classes[i].quantum, quanta[i]);
  573. for (i = oldbands; i < q->nbands; i++) {
  574. q->classes[i].qdisc = queues[i];
  575. if (q->classes[i].qdisc != &noop_qdisc)
  576. qdisc_hash_add(q->classes[i].qdisc, true);
  577. }
  578. sch_tree_unlock(sch);
  579. ets_offload_change(sch);
  580. for (i = q->nbands; i < oldbands; i++) {
  581. qdisc_put(q->classes[i].qdisc);
  582. q->classes[i].qdisc = NULL;
  583. WRITE_ONCE(q->classes[i].quantum, 0);
  584. q->classes[i].deficit = 0;
  585. gnet_stats_basic_sync_init(&q->classes[i].bstats);
  586. memset(&q->classes[i].qstats, 0, sizeof(q->classes[i].qstats));
  587. }
  588. return 0;
  589. }
  590. static int ets_qdisc_init(struct Qdisc *sch, struct nlattr *opt,
  591. struct netlink_ext_ack *extack)
  592. {
  593. struct ets_sched *q = qdisc_priv(sch);
  594. int err, i;
  595. if (!opt)
  596. return -EINVAL;
  597. err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
  598. if (err)
  599. return err;
  600. INIT_LIST_HEAD(&q->active);
  601. for (i = 0; i < TCQ_ETS_MAX_BANDS; i++)
  602. INIT_LIST_HEAD(&q->classes[i].alist);
  603. return ets_qdisc_change(sch, opt, extack);
  604. }
  605. static void ets_qdisc_reset(struct Qdisc *sch)
  606. {
  607. struct ets_sched *q = qdisc_priv(sch);
  608. int band;
  609. for (band = q->nstrict; band < q->nbands; band++) {
  610. if (q->classes[band].qdisc->q.qlen)
  611. list_del(&q->classes[band].alist);
  612. }
  613. for (band = 0; band < q->nbands; band++)
  614. qdisc_reset(q->classes[band].qdisc);
  615. }
  616. static void ets_qdisc_destroy(struct Qdisc *sch)
  617. {
  618. struct ets_sched *q = qdisc_priv(sch);
  619. int band;
  620. ets_offload_destroy(sch);
  621. tcf_block_put(q->block);
  622. for (band = 0; band < q->nbands; band++)
  623. qdisc_put(q->classes[band].qdisc);
  624. }
  625. static int ets_qdisc_dump(struct Qdisc *sch, struct sk_buff *skb)
  626. {
  627. struct ets_sched *q = qdisc_priv(sch);
  628. struct nlattr *opts;
  629. struct nlattr *nest;
  630. u8 nbands, nstrict;
  631. int band;
  632. int prio;
  633. int err;
  634. err = ets_offload_dump(sch);
  635. if (err)
  636. return err;
  637. opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
  638. if (!opts)
  639. goto nla_err;
  640. nbands = READ_ONCE(q->nbands);
  641. if (nla_put_u8(skb, TCA_ETS_NBANDS, nbands))
  642. goto nla_err;
  643. nstrict = READ_ONCE(q->nstrict);
  644. if (nstrict && nla_put_u8(skb, TCA_ETS_NSTRICT, nstrict))
  645. goto nla_err;
  646. if (nbands > nstrict) {
  647. nest = nla_nest_start(skb, TCA_ETS_QUANTA);
  648. if (!nest)
  649. goto nla_err;
  650. for (band = nstrict; band < nbands; band++) {
  651. if (nla_put_u32(skb, TCA_ETS_QUANTA_BAND,
  652. READ_ONCE(q->classes[band].quantum)))
  653. goto nla_err;
  654. }
  655. nla_nest_end(skb, nest);
  656. }
  657. nest = nla_nest_start(skb, TCA_ETS_PRIOMAP);
  658. if (!nest)
  659. goto nla_err;
  660. for (prio = 0; prio <= TC_PRIO_MAX; prio++) {
  661. if (nla_put_u8(skb, TCA_ETS_PRIOMAP_BAND,
  662. READ_ONCE(q->prio2band[prio])))
  663. goto nla_err;
  664. }
  665. nla_nest_end(skb, nest);
  666. return nla_nest_end(skb, opts);
  667. nla_err:
  668. nla_nest_cancel(skb, opts);
  669. return -EMSGSIZE;
  670. }
  671. static const struct Qdisc_class_ops ets_class_ops = {
  672. .change = ets_class_change,
  673. .graft = ets_class_graft,
  674. .leaf = ets_class_leaf,
  675. .find = ets_class_find,
  676. .qlen_notify = ets_class_qlen_notify,
  677. .dump = ets_class_dump,
  678. .dump_stats = ets_class_dump_stats,
  679. .walk = ets_qdisc_walk,
  680. .tcf_block = ets_qdisc_tcf_block,
  681. .bind_tcf = ets_qdisc_bind_tcf,
  682. .unbind_tcf = ets_qdisc_unbind_tcf,
  683. };
  684. static struct Qdisc_ops ets_qdisc_ops __read_mostly = {
  685. .cl_ops = &ets_class_ops,
  686. .id = "ets",
  687. .priv_size = sizeof(struct ets_sched),
  688. .enqueue = ets_qdisc_enqueue,
  689. .dequeue = ets_qdisc_dequeue,
  690. .peek = qdisc_peek_dequeued,
  691. .change = ets_qdisc_change,
  692. .init = ets_qdisc_init,
  693. .reset = ets_qdisc_reset,
  694. .destroy = ets_qdisc_destroy,
  695. .dump = ets_qdisc_dump,
  696. .owner = THIS_MODULE,
  697. };
  698. MODULE_ALIAS_NET_SCH("ets");
  699. static int __init ets_init(void)
  700. {
  701. return register_qdisc(&ets_qdisc_ops);
  702. }
  703. static void __exit ets_exit(void)
  704. {
  705. unregister_qdisc(&ets_qdisc_ops);
  706. }
  707. module_init(ets_init);
  708. module_exit(ets_exit);
  709. MODULE_LICENSE("GPL");
  710. MODULE_DESCRIPTION("Enhanced Transmission Selection(ETS) scheduler");