sch_qfq.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. * net/sched/sch_qfq.c Quick Fair Queueing Scheduler.
  3. *
  4. * Copyright (c) 2009 Fabio Checconi, Luigi Rizzo, and Paolo Valente.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/bitops.h>
  13. #include <linux/errno.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/pkt_sched.h>
  16. #include <net/sch_generic.h>
  17. #include <net/pkt_sched.h>
  18. #include <net/pkt_cls.h>
  19. /* Quick Fair Queueing
  20. ===================
  21. Sources:
  22. Fabio Checconi, Luigi Rizzo, and Paolo Valente: "QFQ: Efficient
  23. Packet Scheduling with Tight Bandwidth Distribution Guarantees."
  24. See also:
  25. http://retis.sssup.it/~fabio/linux/qfq/
  26. */
  27. /*
  28. Virtual time computations.
  29. S, F and V are all computed in fixed point arithmetic with
  30. FRAC_BITS decimal bits.
  31. QFQ_MAX_INDEX is the maximum index allowed for a group. We need
  32. one bit per index.
  33. QFQ_MAX_WSHIFT is the maximum power of two supported as a weight.
  34. The layout of the bits is as below:
  35. [ MTU_SHIFT ][ FRAC_BITS ]
  36. [ MAX_INDEX ][ MIN_SLOT_SHIFT ]
  37. ^.__grp->index = 0
  38. *.__grp->slot_shift
  39. where MIN_SLOT_SHIFT is derived by difference from the others.
  40. The max group index corresponds to Lmax/w_min, where
  41. Lmax=1<<MTU_SHIFT, w_min = 1 .
  42. From this, and knowing how many groups (MAX_INDEX) we want,
  43. we can derive the shift corresponding to each group.
  44. Because we often need to compute
  45. F = S + len/w_i and V = V + len/wsum
  46. instead of storing w_i store the value
  47. inv_w = (1<<FRAC_BITS)/w_i
  48. so we can do F = S + len * inv_w * wsum.
  49. We use W_TOT in the formulas so we can easily move between
  50. static and adaptive weight sum.
  51. The per-scheduler-instance data contain all the data structures
  52. for the scheduler: bitmaps and bucket lists.
  53. */
  54. /*
  55. * Maximum number of consecutive slots occupied by backlogged classes
  56. * inside a group.
  57. */
  58. #define QFQ_MAX_SLOTS 32
  59. /*
  60. * Shifts used for class<->group mapping. We allow class weights that are
  61. * in the range [1, 2^MAX_WSHIFT], and we try to map each class i to the
  62. * group with the smallest index that can support the L_i / r_i configured
  63. * for the class.
  64. *
  65. * grp->index is the index of the group; and grp->slot_shift
  66. * is the shift for the corresponding (scaled) sigma_i.
  67. */
  68. #define QFQ_MAX_INDEX 19
  69. #define QFQ_MAX_WSHIFT 16
  70. #define QFQ_MAX_WEIGHT (1<<QFQ_MAX_WSHIFT)
  71. #define QFQ_MAX_WSUM (2*QFQ_MAX_WEIGHT)
  72. #define FRAC_BITS 30 /* fixed point arithmetic */
  73. #define ONE_FP (1UL << FRAC_BITS)
  74. #define IWSUM (ONE_FP/QFQ_MAX_WSUM)
  75. #define QFQ_MTU_SHIFT 11
  76. #define QFQ_MIN_SLOT_SHIFT (FRAC_BITS + QFQ_MTU_SHIFT - QFQ_MAX_INDEX)
  77. /*
  78. * Possible group states. These values are used as indexes for the bitmaps
  79. * array of struct qfq_queue.
  80. */
  81. enum qfq_state { ER, IR, EB, IB, QFQ_MAX_STATE };
  82. struct qfq_group;
  83. struct qfq_class {
  84. struct Qdisc_class_common common;
  85. unsigned int refcnt;
  86. unsigned int filter_cnt;
  87. struct gnet_stats_basic_packed bstats;
  88. struct gnet_stats_queue qstats;
  89. struct gnet_stats_rate_est rate_est;
  90. struct Qdisc *qdisc;
  91. struct hlist_node next; /* Link for the slot list. */
  92. u64 S, F; /* flow timestamps (exact) */
  93. /* group we belong to. In principle we would need the index,
  94. * which is log_2(lmax/weight), but we never reference it
  95. * directly, only the group.
  96. */
  97. struct qfq_group *grp;
  98. /* these are copied from the flowset. */
  99. u32 inv_w; /* ONE_FP/weight */
  100. u32 lmax; /* Max packet size for this flow. */
  101. };
  102. struct qfq_group {
  103. u64 S, F; /* group timestamps (approx). */
  104. unsigned int slot_shift; /* Slot shift. */
  105. unsigned int index; /* Group index. */
  106. unsigned int front; /* Index of the front slot. */
  107. unsigned long full_slots; /* non-empty slots */
  108. /* Array of RR lists of active classes. */
  109. struct hlist_head slots[QFQ_MAX_SLOTS];
  110. };
  111. struct qfq_sched {
  112. struct tcf_proto *filter_list;
  113. struct Qdisc_class_hash clhash;
  114. u64 V; /* Precise virtual time. */
  115. u32 wsum; /* weight sum */
  116. unsigned long bitmaps[QFQ_MAX_STATE]; /* Group bitmaps. */
  117. struct qfq_group groups[QFQ_MAX_INDEX + 1]; /* The groups. */
  118. };
  119. static struct qfq_class *qfq_find_class(struct Qdisc *sch, u32 classid)
  120. {
  121. struct qfq_sched *q = qdisc_priv(sch);
  122. struct Qdisc_class_common *clc;
  123. clc = qdisc_class_find(&q->clhash, classid);
  124. if (clc == NULL)
  125. return NULL;
  126. return container_of(clc, struct qfq_class, common);
  127. }
  128. static void qfq_purge_queue(struct qfq_class *cl)
  129. {
  130. unsigned int len = cl->qdisc->q.qlen;
  131. qdisc_reset(cl->qdisc);
  132. qdisc_tree_decrease_qlen(cl->qdisc, len);
  133. }
  134. static const struct nla_policy qfq_policy[TCA_QFQ_MAX + 1] = {
  135. [TCA_QFQ_WEIGHT] = { .type = NLA_U32 },
  136. [TCA_QFQ_LMAX] = { .type = NLA_U32 },
  137. };
  138. /*
  139. * Calculate a flow index, given its weight and maximum packet length.
  140. * index = log_2(maxlen/weight) but we need to apply the scaling.
  141. * This is used only once at flow creation.
  142. */
  143. static int qfq_calc_index(u32 inv_w, unsigned int maxlen)
  144. {
  145. u64 slot_size = (u64)maxlen * inv_w;
  146. unsigned long size_map;
  147. int index = 0;
  148. size_map = slot_size >> QFQ_MIN_SLOT_SHIFT;
  149. if (!size_map)
  150. goto out;
  151. index = __fls(size_map) + 1; /* basically a log_2 */
  152. index -= !(slot_size - (1ULL << (index + QFQ_MIN_SLOT_SHIFT - 1)));
  153. if (index < 0)
  154. index = 0;
  155. out:
  156. pr_debug("qfq calc_index: W = %lu, L = %u, I = %d\n",
  157. (unsigned long) ONE_FP/inv_w, maxlen, index);
  158. return index;
  159. }
  160. /* Length of the next packet (0 if the queue is empty). */
  161. static unsigned int qdisc_peek_len(struct Qdisc *sch)
  162. {
  163. struct sk_buff *skb;
  164. skb = sch->ops->peek(sch);
  165. return skb ? qdisc_pkt_len(skb) : 0;
  166. }
  167. static void qfq_deactivate_class(struct qfq_sched *, struct qfq_class *);
  168. static void qfq_activate_class(struct qfq_sched *q, struct qfq_class *cl,
  169. unsigned int len);
  170. static void qfq_update_class_params(struct qfq_sched *q, struct qfq_class *cl,
  171. u32 lmax, u32 inv_w, int delta_w)
  172. {
  173. int i;
  174. /* update qfq-specific data */
  175. cl->lmax = lmax;
  176. cl->inv_w = inv_w;
  177. i = qfq_calc_index(cl->inv_w, cl->lmax);
  178. cl->grp = &q->groups[i];
  179. q->wsum += delta_w;
  180. }
  181. static int qfq_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
  182. struct nlattr **tca, unsigned long *arg)
  183. {
  184. struct qfq_sched *q = qdisc_priv(sch);
  185. struct qfq_class *cl = (struct qfq_class *)*arg;
  186. struct nlattr *tb[TCA_QFQ_MAX + 1];
  187. u32 weight, lmax, inv_w;
  188. int i, err;
  189. int delta_w;
  190. if (tca[TCA_OPTIONS] == NULL) {
  191. pr_notice("qfq: no options\n");
  192. return -EINVAL;
  193. }
  194. err = nla_parse_nested(tb, TCA_QFQ_MAX, tca[TCA_OPTIONS], qfq_policy);
  195. if (err < 0)
  196. return err;
  197. if (tb[TCA_QFQ_WEIGHT]) {
  198. weight = nla_get_u32(tb[TCA_QFQ_WEIGHT]);
  199. if (!weight || weight > (1UL << QFQ_MAX_WSHIFT)) {
  200. pr_notice("qfq: invalid weight %u\n", weight);
  201. return -EINVAL;
  202. }
  203. } else
  204. weight = 1;
  205. inv_w = ONE_FP / weight;
  206. weight = ONE_FP / inv_w;
  207. delta_w = weight - (cl ? ONE_FP / cl->inv_w : 0);
  208. if (q->wsum + delta_w > QFQ_MAX_WSUM) {
  209. pr_notice("qfq: total weight out of range (%u + %u)\n",
  210. delta_w, q->wsum);
  211. return -EINVAL;
  212. }
  213. if (tb[TCA_QFQ_LMAX]) {
  214. lmax = nla_get_u32(tb[TCA_QFQ_LMAX]);
  215. if (!lmax || lmax > (1UL << QFQ_MTU_SHIFT)) {
  216. pr_notice("qfq: invalid max length %u\n", lmax);
  217. return -EINVAL;
  218. }
  219. } else
  220. lmax = 1UL << QFQ_MTU_SHIFT;
  221. if (cl != NULL) {
  222. bool need_reactivation = false;
  223. if (tca[TCA_RATE]) {
  224. err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
  225. qdisc_root_sleeping_lock(sch),
  226. tca[TCA_RATE]);
  227. if (err)
  228. return err;
  229. }
  230. if (lmax == cl->lmax && inv_w == cl->inv_w)
  231. return 0; /* nothing to update */
  232. i = qfq_calc_index(inv_w, lmax);
  233. sch_tree_lock(sch);
  234. if (&q->groups[i] != cl->grp && cl->qdisc->q.qlen > 0) {
  235. /*
  236. * shift cl->F back, to not charge the
  237. * class for the not-yet-served head
  238. * packet
  239. */
  240. cl->F = cl->S;
  241. /* remove class from its slot in the old group */
  242. qfq_deactivate_class(q, cl);
  243. need_reactivation = true;
  244. }
  245. qfq_update_class_params(q, cl, lmax, inv_w, delta_w);
  246. if (need_reactivation) /* activate in new group */
  247. qfq_activate_class(q, cl, qdisc_peek_len(cl->qdisc));
  248. sch_tree_unlock(sch);
  249. return 0;
  250. }
  251. cl = kzalloc(sizeof(struct qfq_class), GFP_KERNEL);
  252. if (cl == NULL)
  253. return -ENOBUFS;
  254. cl->refcnt = 1;
  255. cl->common.classid = classid;
  256. qfq_update_class_params(q, cl, lmax, inv_w, delta_w);
  257. cl->qdisc = qdisc_create_dflt(sch->dev_queue,
  258. &pfifo_qdisc_ops, classid);
  259. if (cl->qdisc == NULL)
  260. cl->qdisc = &noop_qdisc;
  261. if (tca[TCA_RATE]) {
  262. err = gen_new_estimator(&cl->bstats, &cl->rate_est,
  263. qdisc_root_sleeping_lock(sch),
  264. tca[TCA_RATE]);
  265. if (err) {
  266. qdisc_destroy(cl->qdisc);
  267. kfree(cl);
  268. return err;
  269. }
  270. }
  271. sch_tree_lock(sch);
  272. qdisc_class_hash_insert(&q->clhash, &cl->common);
  273. sch_tree_unlock(sch);
  274. qdisc_class_hash_grow(sch, &q->clhash);
  275. *arg = (unsigned long)cl;
  276. return 0;
  277. }
  278. static void qfq_destroy_class(struct Qdisc *sch, struct qfq_class *cl)
  279. {
  280. struct qfq_sched *q = qdisc_priv(sch);
  281. if (cl->inv_w) {
  282. q->wsum -= ONE_FP / cl->inv_w;
  283. cl->inv_w = 0;
  284. }
  285. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  286. qdisc_destroy(cl->qdisc);
  287. kfree(cl);
  288. }
  289. static int qfq_delete_class(struct Qdisc *sch, unsigned long arg)
  290. {
  291. struct qfq_sched *q = qdisc_priv(sch);
  292. struct qfq_class *cl = (struct qfq_class *)arg;
  293. if (cl->filter_cnt > 0)
  294. return -EBUSY;
  295. sch_tree_lock(sch);
  296. qfq_purge_queue(cl);
  297. qdisc_class_hash_remove(&q->clhash, &cl->common);
  298. BUG_ON(--cl->refcnt == 0);
  299. /*
  300. * This shouldn't happen: we "hold" one cops->get() when called
  301. * from tc_ctl_tclass; the destroy method is done from cops->put().
  302. */
  303. sch_tree_unlock(sch);
  304. return 0;
  305. }
  306. static unsigned long qfq_get_class(struct Qdisc *sch, u32 classid)
  307. {
  308. struct qfq_class *cl = qfq_find_class(sch, classid);
  309. if (cl != NULL)
  310. cl->refcnt++;
  311. return (unsigned long)cl;
  312. }
  313. static void qfq_put_class(struct Qdisc *sch, unsigned long arg)
  314. {
  315. struct qfq_class *cl = (struct qfq_class *)arg;
  316. if (--cl->refcnt == 0)
  317. qfq_destroy_class(sch, cl);
  318. }
  319. static struct tcf_proto **qfq_tcf_chain(struct Qdisc *sch, unsigned long cl)
  320. {
  321. struct qfq_sched *q = qdisc_priv(sch);
  322. if (cl)
  323. return NULL;
  324. return &q->filter_list;
  325. }
  326. static unsigned long qfq_bind_tcf(struct Qdisc *sch, unsigned long parent,
  327. u32 classid)
  328. {
  329. struct qfq_class *cl = qfq_find_class(sch, classid);
  330. if (cl != NULL)
  331. cl->filter_cnt++;
  332. return (unsigned long)cl;
  333. }
  334. static void qfq_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  335. {
  336. struct qfq_class *cl = (struct qfq_class *)arg;
  337. cl->filter_cnt--;
  338. }
  339. static int qfq_graft_class(struct Qdisc *sch, unsigned long arg,
  340. struct Qdisc *new, struct Qdisc **old)
  341. {
  342. struct qfq_class *cl = (struct qfq_class *)arg;
  343. if (new == NULL) {
  344. new = qdisc_create_dflt(sch->dev_queue,
  345. &pfifo_qdisc_ops, cl->common.classid);
  346. if (new == NULL)
  347. new = &noop_qdisc;
  348. }
  349. sch_tree_lock(sch);
  350. qfq_purge_queue(cl);
  351. *old = cl->qdisc;
  352. cl->qdisc = new;
  353. sch_tree_unlock(sch);
  354. return 0;
  355. }
  356. static struct Qdisc *qfq_class_leaf(struct Qdisc *sch, unsigned long arg)
  357. {
  358. struct qfq_class *cl = (struct qfq_class *)arg;
  359. return cl->qdisc;
  360. }
  361. static int qfq_dump_class(struct Qdisc *sch, unsigned long arg,
  362. struct sk_buff *skb, struct tcmsg *tcm)
  363. {
  364. struct qfq_class *cl = (struct qfq_class *)arg;
  365. struct nlattr *nest;
  366. tcm->tcm_parent = TC_H_ROOT;
  367. tcm->tcm_handle = cl->common.classid;
  368. tcm->tcm_info = cl->qdisc->handle;
  369. nest = nla_nest_start(skb, TCA_OPTIONS);
  370. if (nest == NULL)
  371. goto nla_put_failure;
  372. if (nla_put_u32(skb, TCA_QFQ_WEIGHT, ONE_FP/cl->inv_w) ||
  373. nla_put_u32(skb, TCA_QFQ_LMAX, cl->lmax))
  374. goto nla_put_failure;
  375. return nla_nest_end(skb, nest);
  376. nla_put_failure:
  377. nla_nest_cancel(skb, nest);
  378. return -EMSGSIZE;
  379. }
  380. static int qfq_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  381. struct gnet_dump *d)
  382. {
  383. struct qfq_class *cl = (struct qfq_class *)arg;
  384. struct tc_qfq_stats xstats;
  385. memset(&xstats, 0, sizeof(xstats));
  386. cl->qdisc->qstats.qlen = cl->qdisc->q.qlen;
  387. xstats.weight = ONE_FP/cl->inv_w;
  388. xstats.lmax = cl->lmax;
  389. if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
  390. gnet_stats_copy_rate_est(d, &cl->bstats, &cl->rate_est) < 0 ||
  391. gnet_stats_copy_queue(d, &cl->qdisc->qstats) < 0)
  392. return -1;
  393. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  394. }
  395. static void qfq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  396. {
  397. struct qfq_sched *q = qdisc_priv(sch);
  398. struct qfq_class *cl;
  399. struct hlist_node *n;
  400. unsigned int i;
  401. if (arg->stop)
  402. return;
  403. for (i = 0; i < q->clhash.hashsize; i++) {
  404. hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
  405. if (arg->count < arg->skip) {
  406. arg->count++;
  407. continue;
  408. }
  409. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  410. arg->stop = 1;
  411. return;
  412. }
  413. arg->count++;
  414. }
  415. }
  416. }
  417. static struct qfq_class *qfq_classify(struct sk_buff *skb, struct Qdisc *sch,
  418. int *qerr)
  419. {
  420. struct qfq_sched *q = qdisc_priv(sch);
  421. struct qfq_class *cl;
  422. struct tcf_result res;
  423. int result;
  424. if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
  425. pr_debug("qfq_classify: found %d\n", skb->priority);
  426. cl = qfq_find_class(sch, skb->priority);
  427. if (cl != NULL)
  428. return cl;
  429. }
  430. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  431. result = tc_classify(skb, q->filter_list, &res);
  432. if (result >= 0) {
  433. #ifdef CONFIG_NET_CLS_ACT
  434. switch (result) {
  435. case TC_ACT_QUEUED:
  436. case TC_ACT_STOLEN:
  437. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  438. case TC_ACT_SHOT:
  439. return NULL;
  440. }
  441. #endif
  442. cl = (struct qfq_class *)res.class;
  443. if (cl == NULL)
  444. cl = qfq_find_class(sch, res.classid);
  445. return cl;
  446. }
  447. return NULL;
  448. }
  449. /* Generic comparison function, handling wraparound. */
  450. static inline int qfq_gt(u64 a, u64 b)
  451. {
  452. return (s64)(a - b) > 0;
  453. }
  454. /* Round a precise timestamp to its slotted value. */
  455. static inline u64 qfq_round_down(u64 ts, unsigned int shift)
  456. {
  457. return ts & ~((1ULL << shift) - 1);
  458. }
  459. /* return the pointer to the group with lowest index in the bitmap */
  460. static inline struct qfq_group *qfq_ffs(struct qfq_sched *q,
  461. unsigned long bitmap)
  462. {
  463. int index = __ffs(bitmap);
  464. return &q->groups[index];
  465. }
  466. /* Calculate a mask to mimic what would be ffs_from(). */
  467. static inline unsigned long mask_from(unsigned long bitmap, int from)
  468. {
  469. return bitmap & ~((1UL << from) - 1);
  470. }
  471. /*
  472. * The state computation relies on ER=0, IR=1, EB=2, IB=3
  473. * First compute eligibility comparing grp->S, q->V,
  474. * then check if someone is blocking us and possibly add EB
  475. */
  476. static int qfq_calc_state(struct qfq_sched *q, const struct qfq_group *grp)
  477. {
  478. /* if S > V we are not eligible */
  479. unsigned int state = qfq_gt(grp->S, q->V);
  480. unsigned long mask = mask_from(q->bitmaps[ER], grp->index);
  481. struct qfq_group *next;
  482. if (mask) {
  483. next = qfq_ffs(q, mask);
  484. if (qfq_gt(grp->F, next->F))
  485. state |= EB;
  486. }
  487. return state;
  488. }
  489. /*
  490. * In principle
  491. * q->bitmaps[dst] |= q->bitmaps[src] & mask;
  492. * q->bitmaps[src] &= ~mask;
  493. * but we should make sure that src != dst
  494. */
  495. static inline void qfq_move_groups(struct qfq_sched *q, unsigned long mask,
  496. int src, int dst)
  497. {
  498. q->bitmaps[dst] |= q->bitmaps[src] & mask;
  499. q->bitmaps[src] &= ~mask;
  500. }
  501. static void qfq_unblock_groups(struct qfq_sched *q, int index, u64 old_F)
  502. {
  503. unsigned long mask = mask_from(q->bitmaps[ER], index + 1);
  504. struct qfq_group *next;
  505. if (mask) {
  506. next = qfq_ffs(q, mask);
  507. if (!qfq_gt(next->F, old_F))
  508. return;
  509. }
  510. mask = (1UL << index) - 1;
  511. qfq_move_groups(q, mask, EB, ER);
  512. qfq_move_groups(q, mask, IB, IR);
  513. }
  514. /*
  515. * perhaps
  516. *
  517. old_V ^= q->V;
  518. old_V >>= QFQ_MIN_SLOT_SHIFT;
  519. if (old_V) {
  520. ...
  521. }
  522. *
  523. */
  524. static void qfq_make_eligible(struct qfq_sched *q, u64 old_V)
  525. {
  526. unsigned long vslot = q->V >> QFQ_MIN_SLOT_SHIFT;
  527. unsigned long old_vslot = old_V >> QFQ_MIN_SLOT_SHIFT;
  528. if (vslot != old_vslot) {
  529. unsigned long mask = (1UL << fls(vslot ^ old_vslot)) - 1;
  530. qfq_move_groups(q, mask, IR, ER);
  531. qfq_move_groups(q, mask, IB, EB);
  532. }
  533. }
  534. /*
  535. * XXX we should make sure that slot becomes less than 32.
  536. * This is guaranteed by the input values.
  537. * roundedS is always cl->S rounded on grp->slot_shift bits.
  538. */
  539. static void qfq_slot_insert(struct qfq_group *grp, struct qfq_class *cl,
  540. u64 roundedS)
  541. {
  542. u64 slot = (roundedS - grp->S) >> grp->slot_shift;
  543. unsigned int i = (grp->front + slot) % QFQ_MAX_SLOTS;
  544. hlist_add_head(&cl->next, &grp->slots[i]);
  545. __set_bit(slot, &grp->full_slots);
  546. }
  547. /* Maybe introduce hlist_first_entry?? */
  548. static struct qfq_class *qfq_slot_head(struct qfq_group *grp)
  549. {
  550. return hlist_entry(grp->slots[grp->front].first,
  551. struct qfq_class, next);
  552. }
  553. /*
  554. * remove the entry from the slot
  555. */
  556. static void qfq_front_slot_remove(struct qfq_group *grp)
  557. {
  558. struct qfq_class *cl = qfq_slot_head(grp);
  559. BUG_ON(!cl);
  560. hlist_del(&cl->next);
  561. if (hlist_empty(&grp->slots[grp->front]))
  562. __clear_bit(0, &grp->full_slots);
  563. }
  564. /*
  565. * Returns the first full queue in a group. As a side effect,
  566. * adjust the bucket list so the first non-empty bucket is at
  567. * position 0 in full_slots.
  568. */
  569. static struct qfq_class *qfq_slot_scan(struct qfq_group *grp)
  570. {
  571. unsigned int i;
  572. pr_debug("qfq slot_scan: grp %u full %#lx\n",
  573. grp->index, grp->full_slots);
  574. if (grp->full_slots == 0)
  575. return NULL;
  576. i = __ffs(grp->full_slots); /* zero based */
  577. if (i > 0) {
  578. grp->front = (grp->front + i) % QFQ_MAX_SLOTS;
  579. grp->full_slots >>= i;
  580. }
  581. return qfq_slot_head(grp);
  582. }
  583. /*
  584. * adjust the bucket list. When the start time of a group decreases,
  585. * we move the index down (modulo QFQ_MAX_SLOTS) so we don't need to
  586. * move the objects. The mask of occupied slots must be shifted
  587. * because we use ffs() to find the first non-empty slot.
  588. * This covers decreases in the group's start time, but what about
  589. * increases of the start time ?
  590. * Here too we should make sure that i is less than 32
  591. */
  592. static void qfq_slot_rotate(struct qfq_group *grp, u64 roundedS)
  593. {
  594. unsigned int i = (grp->S - roundedS) >> grp->slot_shift;
  595. grp->full_slots <<= i;
  596. grp->front = (grp->front - i) % QFQ_MAX_SLOTS;
  597. }
  598. static void qfq_update_eligible(struct qfq_sched *q, u64 old_V)
  599. {
  600. struct qfq_group *grp;
  601. unsigned long ineligible;
  602. ineligible = q->bitmaps[IR] | q->bitmaps[IB];
  603. if (ineligible) {
  604. if (!q->bitmaps[ER]) {
  605. grp = qfq_ffs(q, ineligible);
  606. if (qfq_gt(grp->S, q->V))
  607. q->V = grp->S;
  608. }
  609. qfq_make_eligible(q, old_V);
  610. }
  611. }
  612. /*
  613. * Updates the class, returns true if also the group needs to be updated.
  614. */
  615. static bool qfq_update_class(struct qfq_group *grp, struct qfq_class *cl)
  616. {
  617. unsigned int len = qdisc_peek_len(cl->qdisc);
  618. cl->S = cl->F;
  619. if (!len)
  620. qfq_front_slot_remove(grp); /* queue is empty */
  621. else {
  622. u64 roundedS;
  623. cl->F = cl->S + (u64)len * cl->inv_w;
  624. roundedS = qfq_round_down(cl->S, grp->slot_shift);
  625. if (roundedS == grp->S)
  626. return false;
  627. qfq_front_slot_remove(grp);
  628. qfq_slot_insert(grp, cl, roundedS);
  629. }
  630. return true;
  631. }
  632. static struct sk_buff *qfq_dequeue(struct Qdisc *sch)
  633. {
  634. struct qfq_sched *q = qdisc_priv(sch);
  635. struct qfq_group *grp;
  636. struct qfq_class *cl;
  637. struct sk_buff *skb;
  638. unsigned int len;
  639. u64 old_V;
  640. if (!q->bitmaps[ER])
  641. return NULL;
  642. grp = qfq_ffs(q, q->bitmaps[ER]);
  643. cl = qfq_slot_head(grp);
  644. skb = qdisc_dequeue_peeked(cl->qdisc);
  645. if (!skb) {
  646. WARN_ONCE(1, "qfq_dequeue: non-workconserving leaf\n");
  647. return NULL;
  648. }
  649. sch->q.qlen--;
  650. qdisc_bstats_update(sch, skb);
  651. old_V = q->V;
  652. len = qdisc_pkt_len(skb);
  653. q->V += (u64)len * IWSUM;
  654. pr_debug("qfq dequeue: len %u F %lld now %lld\n",
  655. len, (unsigned long long) cl->F, (unsigned long long) q->V);
  656. if (qfq_update_class(grp, cl)) {
  657. u64 old_F = grp->F;
  658. cl = qfq_slot_scan(grp);
  659. if (!cl)
  660. __clear_bit(grp->index, &q->bitmaps[ER]);
  661. else {
  662. u64 roundedS = qfq_round_down(cl->S, grp->slot_shift);
  663. unsigned int s;
  664. if (grp->S == roundedS)
  665. goto skip_unblock;
  666. grp->S = roundedS;
  667. grp->F = roundedS + (2ULL << grp->slot_shift);
  668. __clear_bit(grp->index, &q->bitmaps[ER]);
  669. s = qfq_calc_state(q, grp);
  670. __set_bit(grp->index, &q->bitmaps[s]);
  671. }
  672. qfq_unblock_groups(q, grp->index, old_F);
  673. }
  674. skip_unblock:
  675. qfq_update_eligible(q, old_V);
  676. return skb;
  677. }
  678. /*
  679. * Assign a reasonable start time for a new flow k in group i.
  680. * Admissible values for \hat(F) are multiples of \sigma_i
  681. * no greater than V+\sigma_i . Larger values mean that
  682. * we had a wraparound so we consider the timestamp to be stale.
  683. *
  684. * If F is not stale and F >= V then we set S = F.
  685. * Otherwise we should assign S = V, but this may violate
  686. * the ordering in ER. So, if we have groups in ER, set S to
  687. * the F_j of the first group j which would be blocking us.
  688. * We are guaranteed not to move S backward because
  689. * otherwise our group i would still be blocked.
  690. */
  691. static void qfq_update_start(struct qfq_sched *q, struct qfq_class *cl)
  692. {
  693. unsigned long mask;
  694. u64 limit, roundedF;
  695. int slot_shift = cl->grp->slot_shift;
  696. roundedF = qfq_round_down(cl->F, slot_shift);
  697. limit = qfq_round_down(q->V, slot_shift) + (1ULL << slot_shift);
  698. if (!qfq_gt(cl->F, q->V) || qfq_gt(roundedF, limit)) {
  699. /* timestamp was stale */
  700. mask = mask_from(q->bitmaps[ER], cl->grp->index);
  701. if (mask) {
  702. struct qfq_group *next = qfq_ffs(q, mask);
  703. if (qfq_gt(roundedF, next->F)) {
  704. if (qfq_gt(limit, next->F))
  705. cl->S = next->F;
  706. else /* preserve timestamp correctness */
  707. cl->S = limit;
  708. return;
  709. }
  710. }
  711. cl->S = q->V;
  712. } else /* timestamp is not stale */
  713. cl->S = cl->F;
  714. }
  715. static int qfq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  716. {
  717. struct qfq_sched *q = qdisc_priv(sch);
  718. struct qfq_class *cl;
  719. int err;
  720. cl = qfq_classify(skb, sch, &err);
  721. if (cl == NULL) {
  722. if (err & __NET_XMIT_BYPASS)
  723. sch->qstats.drops++;
  724. kfree_skb(skb);
  725. return err;
  726. }
  727. pr_debug("qfq_enqueue: cl = %x\n", cl->common.classid);
  728. err = qdisc_enqueue(skb, cl->qdisc);
  729. if (unlikely(err != NET_XMIT_SUCCESS)) {
  730. pr_debug("qfq_enqueue: enqueue failed %d\n", err);
  731. if (net_xmit_drop_count(err)) {
  732. cl->qstats.drops++;
  733. sch->qstats.drops++;
  734. }
  735. return err;
  736. }
  737. bstats_update(&cl->bstats, skb);
  738. ++sch->q.qlen;
  739. /* If the new skb is not the head of queue, then done here. */
  740. if (cl->qdisc->q.qlen != 1)
  741. return err;
  742. /* If reach this point, queue q was idle */
  743. qfq_activate_class(q, cl, qdisc_pkt_len(skb));
  744. return err;
  745. }
  746. /*
  747. * Handle class switch from idle to backlogged.
  748. */
  749. static void qfq_activate_class(struct qfq_sched *q, struct qfq_class *cl,
  750. unsigned int pkt_len)
  751. {
  752. struct qfq_group *grp = cl->grp;
  753. u64 roundedS;
  754. int s;
  755. qfq_update_start(q, cl);
  756. /* compute new finish time and rounded start. */
  757. cl->F = cl->S + (u64)pkt_len * cl->inv_w;
  758. roundedS = qfq_round_down(cl->S, grp->slot_shift);
  759. /*
  760. * insert cl in the correct bucket.
  761. * If cl->S >= grp->S we don't need to adjust the
  762. * bucket list and simply go to the insertion phase.
  763. * Otherwise grp->S is decreasing, we must make room
  764. * in the bucket list, and also recompute the group state.
  765. * Finally, if there were no flows in this group and nobody
  766. * was in ER make sure to adjust V.
  767. */
  768. if (grp->full_slots) {
  769. if (!qfq_gt(grp->S, cl->S))
  770. goto skip_update;
  771. /* create a slot for this cl->S */
  772. qfq_slot_rotate(grp, roundedS);
  773. /* group was surely ineligible, remove */
  774. __clear_bit(grp->index, &q->bitmaps[IR]);
  775. __clear_bit(grp->index, &q->bitmaps[IB]);
  776. } else if (!q->bitmaps[ER] && qfq_gt(roundedS, q->V))
  777. q->V = roundedS;
  778. grp->S = roundedS;
  779. grp->F = roundedS + (2ULL << grp->slot_shift);
  780. s = qfq_calc_state(q, grp);
  781. __set_bit(grp->index, &q->bitmaps[s]);
  782. pr_debug("qfq enqueue: new state %d %#lx S %lld F %lld V %lld\n",
  783. s, q->bitmaps[s],
  784. (unsigned long long) cl->S,
  785. (unsigned long long) cl->F,
  786. (unsigned long long) q->V);
  787. skip_update:
  788. qfq_slot_insert(grp, cl, roundedS);
  789. }
  790. static void qfq_slot_remove(struct qfq_sched *q, struct qfq_group *grp,
  791. struct qfq_class *cl)
  792. {
  793. unsigned int i, offset;
  794. u64 roundedS;
  795. roundedS = qfq_round_down(cl->S, grp->slot_shift);
  796. offset = (roundedS - grp->S) >> grp->slot_shift;
  797. i = (grp->front + offset) % QFQ_MAX_SLOTS;
  798. hlist_del(&cl->next);
  799. if (hlist_empty(&grp->slots[i]))
  800. __clear_bit(offset, &grp->full_slots);
  801. }
  802. /*
  803. * called to forcibly destroy a queue.
  804. * If the queue is not in the front bucket, or if it has
  805. * other queues in the front bucket, we can simply remove
  806. * the queue with no other side effects.
  807. * Otherwise we must propagate the event up.
  808. */
  809. static void qfq_deactivate_class(struct qfq_sched *q, struct qfq_class *cl)
  810. {
  811. struct qfq_group *grp = cl->grp;
  812. unsigned long mask;
  813. u64 roundedS;
  814. int s;
  815. cl->F = cl->S;
  816. qfq_slot_remove(q, grp, cl);
  817. if (!grp->full_slots) {
  818. __clear_bit(grp->index, &q->bitmaps[IR]);
  819. __clear_bit(grp->index, &q->bitmaps[EB]);
  820. __clear_bit(grp->index, &q->bitmaps[IB]);
  821. if (test_bit(grp->index, &q->bitmaps[ER]) &&
  822. !(q->bitmaps[ER] & ~((1UL << grp->index) - 1))) {
  823. mask = q->bitmaps[ER] & ((1UL << grp->index) - 1);
  824. if (mask)
  825. mask = ~((1UL << __fls(mask)) - 1);
  826. else
  827. mask = ~0UL;
  828. qfq_move_groups(q, mask, EB, ER);
  829. qfq_move_groups(q, mask, IB, IR);
  830. }
  831. __clear_bit(grp->index, &q->bitmaps[ER]);
  832. } else if (hlist_empty(&grp->slots[grp->front])) {
  833. cl = qfq_slot_scan(grp);
  834. roundedS = qfq_round_down(cl->S, grp->slot_shift);
  835. if (grp->S != roundedS) {
  836. __clear_bit(grp->index, &q->bitmaps[ER]);
  837. __clear_bit(grp->index, &q->bitmaps[IR]);
  838. __clear_bit(grp->index, &q->bitmaps[EB]);
  839. __clear_bit(grp->index, &q->bitmaps[IB]);
  840. grp->S = roundedS;
  841. grp->F = roundedS + (2ULL << grp->slot_shift);
  842. s = qfq_calc_state(q, grp);
  843. __set_bit(grp->index, &q->bitmaps[s]);
  844. }
  845. }
  846. qfq_update_eligible(q, q->V);
  847. }
  848. static void qfq_qlen_notify(struct Qdisc *sch, unsigned long arg)
  849. {
  850. struct qfq_sched *q = qdisc_priv(sch);
  851. struct qfq_class *cl = (struct qfq_class *)arg;
  852. if (cl->qdisc->q.qlen == 0)
  853. qfq_deactivate_class(q, cl);
  854. }
  855. static unsigned int qfq_drop(struct Qdisc *sch)
  856. {
  857. struct qfq_sched *q = qdisc_priv(sch);
  858. struct qfq_group *grp;
  859. unsigned int i, j, len;
  860. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  861. grp = &q->groups[i];
  862. for (j = 0; j < QFQ_MAX_SLOTS; j++) {
  863. struct qfq_class *cl;
  864. struct hlist_node *n;
  865. hlist_for_each_entry(cl, n, &grp->slots[j], next) {
  866. if (!cl->qdisc->ops->drop)
  867. continue;
  868. len = cl->qdisc->ops->drop(cl->qdisc);
  869. if (len > 0) {
  870. sch->q.qlen--;
  871. if (!cl->qdisc->q.qlen)
  872. qfq_deactivate_class(q, cl);
  873. return len;
  874. }
  875. }
  876. }
  877. }
  878. return 0;
  879. }
  880. static int qfq_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
  881. {
  882. struct qfq_sched *q = qdisc_priv(sch);
  883. struct qfq_group *grp;
  884. int i, j, err;
  885. err = qdisc_class_hash_init(&q->clhash);
  886. if (err < 0)
  887. return err;
  888. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  889. grp = &q->groups[i];
  890. grp->index = i;
  891. grp->slot_shift = QFQ_MTU_SHIFT + FRAC_BITS
  892. - (QFQ_MAX_INDEX - i);
  893. for (j = 0; j < QFQ_MAX_SLOTS; j++)
  894. INIT_HLIST_HEAD(&grp->slots[j]);
  895. }
  896. return 0;
  897. }
  898. static void qfq_reset_qdisc(struct Qdisc *sch)
  899. {
  900. struct qfq_sched *q = qdisc_priv(sch);
  901. struct qfq_group *grp;
  902. struct qfq_class *cl;
  903. struct hlist_node *n, *tmp;
  904. unsigned int i, j;
  905. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  906. grp = &q->groups[i];
  907. for (j = 0; j < QFQ_MAX_SLOTS; j++) {
  908. hlist_for_each_entry_safe(cl, n, tmp,
  909. &grp->slots[j], next) {
  910. qfq_deactivate_class(q, cl);
  911. }
  912. }
  913. }
  914. for (i = 0; i < q->clhash.hashsize; i++) {
  915. hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode)
  916. qdisc_reset(cl->qdisc);
  917. }
  918. sch->q.qlen = 0;
  919. }
  920. static void qfq_destroy_qdisc(struct Qdisc *sch)
  921. {
  922. struct qfq_sched *q = qdisc_priv(sch);
  923. struct qfq_class *cl;
  924. struct hlist_node *n, *next;
  925. unsigned int i;
  926. tcf_destroy_chain(&q->filter_list);
  927. for (i = 0; i < q->clhash.hashsize; i++) {
  928. hlist_for_each_entry_safe(cl, n, next, &q->clhash.hash[i],
  929. common.hnode) {
  930. qfq_destroy_class(sch, cl);
  931. }
  932. }
  933. qdisc_class_hash_destroy(&q->clhash);
  934. }
  935. static const struct Qdisc_class_ops qfq_class_ops = {
  936. .change = qfq_change_class,
  937. .delete = qfq_delete_class,
  938. .get = qfq_get_class,
  939. .put = qfq_put_class,
  940. .tcf_chain = qfq_tcf_chain,
  941. .bind_tcf = qfq_bind_tcf,
  942. .unbind_tcf = qfq_unbind_tcf,
  943. .graft = qfq_graft_class,
  944. .leaf = qfq_class_leaf,
  945. .qlen_notify = qfq_qlen_notify,
  946. .dump = qfq_dump_class,
  947. .dump_stats = qfq_dump_class_stats,
  948. .walk = qfq_walk,
  949. };
  950. static struct Qdisc_ops qfq_qdisc_ops __read_mostly = {
  951. .cl_ops = &qfq_class_ops,
  952. .id = "qfq",
  953. .priv_size = sizeof(struct qfq_sched),
  954. .enqueue = qfq_enqueue,
  955. .dequeue = qfq_dequeue,
  956. .peek = qdisc_peek_dequeued,
  957. .drop = qfq_drop,
  958. .init = qfq_init_qdisc,
  959. .reset = qfq_reset_qdisc,
  960. .destroy = qfq_destroy_qdisc,
  961. .owner = THIS_MODULE,
  962. };
  963. static int __init qfq_init(void)
  964. {
  965. return register_qdisc(&qfq_qdisc_ops);
  966. }
  967. static void __exit qfq_exit(void)
  968. {
  969. unregister_qdisc(&qfq_qdisc_ops);
  970. }
  971. module_init(qfq_init);
  972. module_exit(qfq_exit);
  973. MODULE_LICENSE("GPL");