sch_ets.c 20 KB

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