sch_qfq.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. static int qfq_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
  161. struct nlattr **tca, unsigned long *arg)
  162. {
  163. struct qfq_sched *q = qdisc_priv(sch);
  164. struct qfq_class *cl = (struct qfq_class *)*arg;
  165. struct nlattr *tb[TCA_QFQ_MAX + 1];
  166. u32 weight, lmax, inv_w;
  167. int i, err;
  168. if (tca[TCA_OPTIONS] == NULL) {
  169. pr_notice("qfq: no options\n");
  170. return -EINVAL;
  171. }
  172. err = nla_parse_nested(tb, TCA_QFQ_MAX, tca[TCA_OPTIONS], qfq_policy);
  173. if (err < 0)
  174. return err;
  175. if (tb[TCA_QFQ_WEIGHT]) {
  176. weight = nla_get_u32(tb[TCA_QFQ_WEIGHT]);
  177. if (!weight || weight > (1UL << QFQ_MAX_WSHIFT)) {
  178. pr_notice("qfq: invalid weight %u\n", weight);
  179. return -EINVAL;
  180. }
  181. } else
  182. weight = 1;
  183. inv_w = ONE_FP / weight;
  184. weight = ONE_FP / inv_w;
  185. if (q->wsum + weight > QFQ_MAX_WSUM) {
  186. pr_notice("qfq: total weight out of range (%u + %u)\n",
  187. weight, q->wsum);
  188. return -EINVAL;
  189. }
  190. if (tb[TCA_QFQ_LMAX]) {
  191. lmax = nla_get_u32(tb[TCA_QFQ_LMAX]);
  192. if (!lmax || lmax > (1UL << QFQ_MTU_SHIFT)) {
  193. pr_notice("qfq: invalid max length %u\n", lmax);
  194. return -EINVAL;
  195. }
  196. } else
  197. lmax = 1UL << QFQ_MTU_SHIFT;
  198. if (cl != NULL) {
  199. if (tca[TCA_RATE]) {
  200. err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
  201. qdisc_root_sleeping_lock(sch),
  202. tca[TCA_RATE]);
  203. if (err)
  204. return err;
  205. }
  206. sch_tree_lock(sch);
  207. if (tb[TCA_QFQ_WEIGHT]) {
  208. q->wsum = weight - ONE_FP / cl->inv_w;
  209. cl->inv_w = inv_w;
  210. }
  211. sch_tree_unlock(sch);
  212. return 0;
  213. }
  214. cl = kzalloc(sizeof(struct qfq_class), GFP_KERNEL);
  215. if (cl == NULL)
  216. return -ENOBUFS;
  217. cl->refcnt = 1;
  218. cl->common.classid = classid;
  219. cl->lmax = lmax;
  220. cl->inv_w = inv_w;
  221. i = qfq_calc_index(cl->inv_w, cl->lmax);
  222. cl->grp = &q->groups[i];
  223. q->wsum += weight;
  224. cl->qdisc = qdisc_create_dflt(sch->dev_queue,
  225. &pfifo_qdisc_ops, classid);
  226. if (cl->qdisc == NULL)
  227. cl->qdisc = &noop_qdisc;
  228. if (tca[TCA_RATE]) {
  229. err = gen_new_estimator(&cl->bstats, &cl->rate_est,
  230. qdisc_root_sleeping_lock(sch),
  231. tca[TCA_RATE]);
  232. if (err) {
  233. qdisc_destroy(cl->qdisc);
  234. kfree(cl);
  235. return err;
  236. }
  237. }
  238. sch_tree_lock(sch);
  239. qdisc_class_hash_insert(&q->clhash, &cl->common);
  240. sch_tree_unlock(sch);
  241. qdisc_class_hash_grow(sch, &q->clhash);
  242. *arg = (unsigned long)cl;
  243. return 0;
  244. }
  245. static void qfq_destroy_class(struct Qdisc *sch, struct qfq_class *cl)
  246. {
  247. struct qfq_sched *q = qdisc_priv(sch);
  248. if (cl->inv_w) {
  249. q->wsum -= ONE_FP / cl->inv_w;
  250. cl->inv_w = 0;
  251. }
  252. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  253. qdisc_destroy(cl->qdisc);
  254. kfree(cl);
  255. }
  256. static int qfq_delete_class(struct Qdisc *sch, unsigned long arg)
  257. {
  258. struct qfq_sched *q = qdisc_priv(sch);
  259. struct qfq_class *cl = (struct qfq_class *)arg;
  260. if (cl->filter_cnt > 0)
  261. return -EBUSY;
  262. sch_tree_lock(sch);
  263. qfq_purge_queue(cl);
  264. qdisc_class_hash_remove(&q->clhash, &cl->common);
  265. BUG_ON(--cl->refcnt == 0);
  266. /*
  267. * This shouldn't happen: we "hold" one cops->get() when called
  268. * from tc_ctl_tclass; the destroy method is done from cops->put().
  269. */
  270. sch_tree_unlock(sch);
  271. return 0;
  272. }
  273. static unsigned long qfq_get_class(struct Qdisc *sch, u32 classid)
  274. {
  275. struct qfq_class *cl = qfq_find_class(sch, classid);
  276. if (cl != NULL)
  277. cl->refcnt++;
  278. return (unsigned long)cl;
  279. }
  280. static void qfq_put_class(struct Qdisc *sch, unsigned long arg)
  281. {
  282. struct qfq_class *cl = (struct qfq_class *)arg;
  283. if (--cl->refcnt == 0)
  284. qfq_destroy_class(sch, cl);
  285. }
  286. static struct tcf_proto **qfq_tcf_chain(struct Qdisc *sch, unsigned long cl)
  287. {
  288. struct qfq_sched *q = qdisc_priv(sch);
  289. if (cl)
  290. return NULL;
  291. return &q->filter_list;
  292. }
  293. static unsigned long qfq_bind_tcf(struct Qdisc *sch, unsigned long parent,
  294. u32 classid)
  295. {
  296. struct qfq_class *cl = qfq_find_class(sch, classid);
  297. if (cl != NULL)
  298. cl->filter_cnt++;
  299. return (unsigned long)cl;
  300. }
  301. static void qfq_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  302. {
  303. struct qfq_class *cl = (struct qfq_class *)arg;
  304. cl->filter_cnt--;
  305. }
  306. static int qfq_graft_class(struct Qdisc *sch, unsigned long arg,
  307. struct Qdisc *new, struct Qdisc **old)
  308. {
  309. struct qfq_class *cl = (struct qfq_class *)arg;
  310. if (new == NULL) {
  311. new = qdisc_create_dflt(sch->dev_queue,
  312. &pfifo_qdisc_ops, cl->common.classid);
  313. if (new == NULL)
  314. new = &noop_qdisc;
  315. }
  316. sch_tree_lock(sch);
  317. qfq_purge_queue(cl);
  318. *old = cl->qdisc;
  319. cl->qdisc = new;
  320. sch_tree_unlock(sch);
  321. return 0;
  322. }
  323. static struct Qdisc *qfq_class_leaf(struct Qdisc *sch, unsigned long arg)
  324. {
  325. struct qfq_class *cl = (struct qfq_class *)arg;
  326. return cl->qdisc;
  327. }
  328. static int qfq_dump_class(struct Qdisc *sch, unsigned long arg,
  329. struct sk_buff *skb, struct tcmsg *tcm)
  330. {
  331. struct qfq_class *cl = (struct qfq_class *)arg;
  332. struct nlattr *nest;
  333. tcm->tcm_parent = TC_H_ROOT;
  334. tcm->tcm_handle = cl->common.classid;
  335. tcm->tcm_info = cl->qdisc->handle;
  336. nest = nla_nest_start(skb, TCA_OPTIONS);
  337. if (nest == NULL)
  338. goto nla_put_failure;
  339. NLA_PUT_U32(skb, TCA_QFQ_WEIGHT, ONE_FP/cl->inv_w);
  340. NLA_PUT_U32(skb, TCA_QFQ_LMAX, cl->lmax);
  341. return nla_nest_end(skb, nest);
  342. nla_put_failure:
  343. nla_nest_cancel(skb, nest);
  344. return -EMSGSIZE;
  345. }
  346. static int qfq_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  347. struct gnet_dump *d)
  348. {
  349. struct qfq_class *cl = (struct qfq_class *)arg;
  350. struct tc_qfq_stats xstats;
  351. memset(&xstats, 0, sizeof(xstats));
  352. cl->qdisc->qstats.qlen = cl->qdisc->q.qlen;
  353. xstats.weight = ONE_FP/cl->inv_w;
  354. xstats.lmax = cl->lmax;
  355. if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
  356. gnet_stats_copy_rate_est(d, &cl->bstats, &cl->rate_est) < 0 ||
  357. gnet_stats_copy_queue(d, &cl->qdisc->qstats) < 0)
  358. return -1;
  359. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  360. }
  361. static void qfq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  362. {
  363. struct qfq_sched *q = qdisc_priv(sch);
  364. struct qfq_class *cl;
  365. struct hlist_node *n;
  366. unsigned int i;
  367. if (arg->stop)
  368. return;
  369. for (i = 0; i < q->clhash.hashsize; i++) {
  370. hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
  371. if (arg->count < arg->skip) {
  372. arg->count++;
  373. continue;
  374. }
  375. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  376. arg->stop = 1;
  377. return;
  378. }
  379. arg->count++;
  380. }
  381. }
  382. }
  383. static struct qfq_class *qfq_classify(struct sk_buff *skb, struct Qdisc *sch,
  384. int *qerr)
  385. {
  386. struct qfq_sched *q = qdisc_priv(sch);
  387. struct qfq_class *cl;
  388. struct tcf_result res;
  389. int result;
  390. if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
  391. pr_debug("qfq_classify: found %d\n", skb->priority);
  392. cl = qfq_find_class(sch, skb->priority);
  393. if (cl != NULL)
  394. return cl;
  395. }
  396. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  397. result = tc_classify(skb, q->filter_list, &res);
  398. if (result >= 0) {
  399. #ifdef CONFIG_NET_CLS_ACT
  400. switch (result) {
  401. case TC_ACT_QUEUED:
  402. case TC_ACT_STOLEN:
  403. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  404. case TC_ACT_SHOT:
  405. return NULL;
  406. }
  407. #endif
  408. cl = (struct qfq_class *)res.class;
  409. if (cl == NULL)
  410. cl = qfq_find_class(sch, res.classid);
  411. return cl;
  412. }
  413. return NULL;
  414. }
  415. /* Generic comparison function, handling wraparound. */
  416. static inline int qfq_gt(u64 a, u64 b)
  417. {
  418. return (s64)(a - b) > 0;
  419. }
  420. /* Round a precise timestamp to its slotted value. */
  421. static inline u64 qfq_round_down(u64 ts, unsigned int shift)
  422. {
  423. return ts & ~((1ULL << shift) - 1);
  424. }
  425. /* return the pointer to the group with lowest index in the bitmap */
  426. static inline struct qfq_group *qfq_ffs(struct qfq_sched *q,
  427. unsigned long bitmap)
  428. {
  429. int index = __ffs(bitmap);
  430. return &q->groups[index];
  431. }
  432. /* Calculate a mask to mimic what would be ffs_from(). */
  433. static inline unsigned long mask_from(unsigned long bitmap, int from)
  434. {
  435. return bitmap & ~((1UL << from) - 1);
  436. }
  437. /*
  438. * The state computation relies on ER=0, IR=1, EB=2, IB=3
  439. * First compute eligibility comparing grp->S, q->V,
  440. * then check if someone is blocking us and possibly add EB
  441. */
  442. static int qfq_calc_state(struct qfq_sched *q, const struct qfq_group *grp)
  443. {
  444. /* if S > V we are not eligible */
  445. unsigned int state = qfq_gt(grp->S, q->V);
  446. unsigned long mask = mask_from(q->bitmaps[ER], grp->index);
  447. struct qfq_group *next;
  448. if (mask) {
  449. next = qfq_ffs(q, mask);
  450. if (qfq_gt(grp->F, next->F))
  451. state |= EB;
  452. }
  453. return state;
  454. }
  455. /*
  456. * In principle
  457. * q->bitmaps[dst] |= q->bitmaps[src] & mask;
  458. * q->bitmaps[src] &= ~mask;
  459. * but we should make sure that src != dst
  460. */
  461. static inline void qfq_move_groups(struct qfq_sched *q, unsigned long mask,
  462. int src, int dst)
  463. {
  464. q->bitmaps[dst] |= q->bitmaps[src] & mask;
  465. q->bitmaps[src] &= ~mask;
  466. }
  467. static void qfq_unblock_groups(struct qfq_sched *q, int index, u64 old_F)
  468. {
  469. unsigned long mask = mask_from(q->bitmaps[ER], index + 1);
  470. struct qfq_group *next;
  471. if (mask) {
  472. next = qfq_ffs(q, mask);
  473. if (!qfq_gt(next->F, old_F))
  474. return;
  475. }
  476. mask = (1UL << index) - 1;
  477. qfq_move_groups(q, mask, EB, ER);
  478. qfq_move_groups(q, mask, IB, IR);
  479. }
  480. /*
  481. * perhaps
  482. *
  483. old_V ^= q->V;
  484. old_V >>= QFQ_MIN_SLOT_SHIFT;
  485. if (old_V) {
  486. ...
  487. }
  488. *
  489. */
  490. static void qfq_make_eligible(struct qfq_sched *q, u64 old_V)
  491. {
  492. unsigned long vslot = q->V >> QFQ_MIN_SLOT_SHIFT;
  493. unsigned long old_vslot = old_V >> QFQ_MIN_SLOT_SHIFT;
  494. if (vslot != old_vslot) {
  495. unsigned long mask = (1UL << fls(vslot ^ old_vslot)) - 1;
  496. qfq_move_groups(q, mask, IR, ER);
  497. qfq_move_groups(q, mask, IB, EB);
  498. }
  499. }
  500. /*
  501. * XXX we should make sure that slot becomes less than 32.
  502. * This is guaranteed by the input values.
  503. * roundedS is always cl->S rounded on grp->slot_shift bits.
  504. */
  505. static void qfq_slot_insert(struct qfq_group *grp, struct qfq_class *cl,
  506. u64 roundedS)
  507. {
  508. u64 slot = (roundedS - grp->S) >> grp->slot_shift;
  509. unsigned int i = (grp->front + slot) % QFQ_MAX_SLOTS;
  510. hlist_add_head(&cl->next, &grp->slots[i]);
  511. __set_bit(slot, &grp->full_slots);
  512. }
  513. /* Maybe introduce hlist_first_entry?? */
  514. static struct qfq_class *qfq_slot_head(struct qfq_group *grp)
  515. {
  516. return hlist_entry(grp->slots[grp->front].first,
  517. struct qfq_class, next);
  518. }
  519. /*
  520. * remove the entry from the slot
  521. */
  522. static void qfq_front_slot_remove(struct qfq_group *grp)
  523. {
  524. struct qfq_class *cl = qfq_slot_head(grp);
  525. BUG_ON(!cl);
  526. hlist_del(&cl->next);
  527. if (hlist_empty(&grp->slots[grp->front]))
  528. __clear_bit(0, &grp->full_slots);
  529. }
  530. /*
  531. * Returns the first full queue in a group. As a side effect,
  532. * adjust the bucket list so the first non-empty bucket is at
  533. * position 0 in full_slots.
  534. */
  535. static struct qfq_class *qfq_slot_scan(struct qfq_group *grp)
  536. {
  537. unsigned int i;
  538. pr_debug("qfq slot_scan: grp %u full %#lx\n",
  539. grp->index, grp->full_slots);
  540. if (grp->full_slots == 0)
  541. return NULL;
  542. i = __ffs(grp->full_slots); /* zero based */
  543. if (i > 0) {
  544. grp->front = (grp->front + i) % QFQ_MAX_SLOTS;
  545. grp->full_slots >>= i;
  546. }
  547. return qfq_slot_head(grp);
  548. }
  549. /*
  550. * adjust the bucket list. When the start time of a group decreases,
  551. * we move the index down (modulo QFQ_MAX_SLOTS) so we don't need to
  552. * move the objects. The mask of occupied slots must be shifted
  553. * because we use ffs() to find the first non-empty slot.
  554. * This covers decreases in the group's start time, but what about
  555. * increases of the start time ?
  556. * Here too we should make sure that i is less than 32
  557. */
  558. static void qfq_slot_rotate(struct qfq_group *grp, u64 roundedS)
  559. {
  560. unsigned int i = (grp->S - roundedS) >> grp->slot_shift;
  561. grp->full_slots <<= i;
  562. grp->front = (grp->front - i) % QFQ_MAX_SLOTS;
  563. }
  564. static void qfq_update_eligible(struct qfq_sched *q, u64 old_V)
  565. {
  566. struct qfq_group *grp;
  567. unsigned long ineligible;
  568. ineligible = q->bitmaps[IR] | q->bitmaps[IB];
  569. if (ineligible) {
  570. if (!q->bitmaps[ER]) {
  571. grp = qfq_ffs(q, ineligible);
  572. if (qfq_gt(grp->S, q->V))
  573. q->V = grp->S;
  574. }
  575. qfq_make_eligible(q, old_V);
  576. }
  577. }
  578. /* What is length of next packet in queue (0 if queue is empty) */
  579. static unsigned int qdisc_peek_len(struct Qdisc *sch)
  580. {
  581. struct sk_buff *skb;
  582. skb = sch->ops->peek(sch);
  583. return skb ? qdisc_pkt_len(skb) : 0;
  584. }
  585. /*
  586. * Updates the class, returns true if also the group needs to be updated.
  587. */
  588. static bool qfq_update_class(struct qfq_group *grp, struct qfq_class *cl)
  589. {
  590. unsigned int len = qdisc_peek_len(cl->qdisc);
  591. cl->S = cl->F;
  592. if (!len)
  593. qfq_front_slot_remove(grp); /* queue is empty */
  594. else {
  595. u64 roundedS;
  596. cl->F = cl->S + (u64)len * cl->inv_w;
  597. roundedS = qfq_round_down(cl->S, grp->slot_shift);
  598. if (roundedS == grp->S)
  599. return false;
  600. qfq_front_slot_remove(grp);
  601. qfq_slot_insert(grp, cl, roundedS);
  602. }
  603. return true;
  604. }
  605. static struct sk_buff *qfq_dequeue(struct Qdisc *sch)
  606. {
  607. struct qfq_sched *q = qdisc_priv(sch);
  608. struct qfq_group *grp;
  609. struct qfq_class *cl;
  610. struct sk_buff *skb;
  611. unsigned int len;
  612. u64 old_V;
  613. if (!q->bitmaps[ER])
  614. return NULL;
  615. grp = qfq_ffs(q, q->bitmaps[ER]);
  616. cl = qfq_slot_head(grp);
  617. skb = qdisc_dequeue_peeked(cl->qdisc);
  618. if (!skb) {
  619. WARN_ONCE(1, "qfq_dequeue: non-workconserving leaf\n");
  620. return NULL;
  621. }
  622. sch->q.qlen--;
  623. qdisc_bstats_update(sch, skb);
  624. old_V = q->V;
  625. len = qdisc_pkt_len(skb);
  626. q->V += (u64)len * IWSUM;
  627. pr_debug("qfq dequeue: len %u F %lld now %lld\n",
  628. len, (unsigned long long) cl->F, (unsigned long long) q->V);
  629. if (qfq_update_class(grp, cl)) {
  630. u64 old_F = grp->F;
  631. cl = qfq_slot_scan(grp);
  632. if (!cl)
  633. __clear_bit(grp->index, &q->bitmaps[ER]);
  634. else {
  635. u64 roundedS = qfq_round_down(cl->S, grp->slot_shift);
  636. unsigned int s;
  637. if (grp->S == roundedS)
  638. goto skip_unblock;
  639. grp->S = roundedS;
  640. grp->F = roundedS + (2ULL << grp->slot_shift);
  641. __clear_bit(grp->index, &q->bitmaps[ER]);
  642. s = qfq_calc_state(q, grp);
  643. __set_bit(grp->index, &q->bitmaps[s]);
  644. }
  645. qfq_unblock_groups(q, grp->index, old_F);
  646. }
  647. skip_unblock:
  648. qfq_update_eligible(q, old_V);
  649. return skb;
  650. }
  651. /*
  652. * Assign a reasonable start time for a new flow k in group i.
  653. * Admissible values for \hat(F) are multiples of \sigma_i
  654. * no greater than V+\sigma_i . Larger values mean that
  655. * we had a wraparound so we consider the timestamp to be stale.
  656. *
  657. * If F is not stale and F >= V then we set S = F.
  658. * Otherwise we should assign S = V, but this may violate
  659. * the ordering in ER. So, if we have groups in ER, set S to
  660. * the F_j of the first group j which would be blocking us.
  661. * We are guaranteed not to move S backward because
  662. * otherwise our group i would still be blocked.
  663. */
  664. static void qfq_update_start(struct qfq_sched *q, struct qfq_class *cl)
  665. {
  666. unsigned long mask;
  667. uint32_t limit, roundedF;
  668. int slot_shift = cl->grp->slot_shift;
  669. roundedF = qfq_round_down(cl->F, slot_shift);
  670. limit = qfq_round_down(q->V, slot_shift) + (1UL << slot_shift);
  671. if (!qfq_gt(cl->F, q->V) || qfq_gt(roundedF, limit)) {
  672. /* timestamp was stale */
  673. mask = mask_from(q->bitmaps[ER], cl->grp->index);
  674. if (mask) {
  675. struct qfq_group *next = qfq_ffs(q, mask);
  676. if (qfq_gt(roundedF, next->F)) {
  677. cl->S = next->F;
  678. return;
  679. }
  680. }
  681. cl->S = q->V;
  682. } else /* timestamp is not stale */
  683. cl->S = cl->F;
  684. }
  685. static int qfq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  686. {
  687. struct qfq_sched *q = qdisc_priv(sch);
  688. struct qfq_group *grp;
  689. struct qfq_class *cl;
  690. int err;
  691. u64 roundedS;
  692. int s;
  693. cl = qfq_classify(skb, sch, &err);
  694. if (cl == NULL) {
  695. if (err & __NET_XMIT_BYPASS)
  696. sch->qstats.drops++;
  697. kfree_skb(skb);
  698. return err;
  699. }
  700. pr_debug("qfq_enqueue: cl = %x\n", cl->common.classid);
  701. err = qdisc_enqueue(skb, cl->qdisc);
  702. if (unlikely(err != NET_XMIT_SUCCESS)) {
  703. pr_debug("qfq_enqueue: enqueue failed %d\n", err);
  704. if (net_xmit_drop_count(err)) {
  705. cl->qstats.drops++;
  706. sch->qstats.drops++;
  707. }
  708. return err;
  709. }
  710. bstats_update(&cl->bstats, skb);
  711. ++sch->q.qlen;
  712. /* If the new skb is not the head of queue, then done here. */
  713. if (cl->qdisc->q.qlen != 1)
  714. return err;
  715. /* If reach this point, queue q was idle */
  716. grp = cl->grp;
  717. qfq_update_start(q, cl);
  718. /* compute new finish time and rounded start. */
  719. cl->F = cl->S + (u64)qdisc_pkt_len(skb) * cl->inv_w;
  720. roundedS = qfq_round_down(cl->S, grp->slot_shift);
  721. /*
  722. * insert cl in the correct bucket.
  723. * If cl->S >= grp->S we don't need to adjust the
  724. * bucket list and simply go to the insertion phase.
  725. * Otherwise grp->S is decreasing, we must make room
  726. * in the bucket list, and also recompute the group state.
  727. * Finally, if there were no flows in this group and nobody
  728. * was in ER make sure to adjust V.
  729. */
  730. if (grp->full_slots) {
  731. if (!qfq_gt(grp->S, cl->S))
  732. goto skip_update;
  733. /* create a slot for this cl->S */
  734. qfq_slot_rotate(grp, roundedS);
  735. /* group was surely ineligible, remove */
  736. __clear_bit(grp->index, &q->bitmaps[IR]);
  737. __clear_bit(grp->index, &q->bitmaps[IB]);
  738. } else if (!q->bitmaps[ER] && qfq_gt(roundedS, q->V))
  739. q->V = roundedS;
  740. grp->S = roundedS;
  741. grp->F = roundedS + (2ULL << grp->slot_shift);
  742. s = qfq_calc_state(q, grp);
  743. __set_bit(grp->index, &q->bitmaps[s]);
  744. pr_debug("qfq enqueue: new state %d %#lx S %lld F %lld V %lld\n",
  745. s, q->bitmaps[s],
  746. (unsigned long long) cl->S,
  747. (unsigned long long) cl->F,
  748. (unsigned long long) q->V);
  749. skip_update:
  750. qfq_slot_insert(grp, cl, roundedS);
  751. return err;
  752. }
  753. static void qfq_slot_remove(struct qfq_sched *q, struct qfq_group *grp,
  754. struct qfq_class *cl)
  755. {
  756. unsigned int i, offset;
  757. u64 roundedS;
  758. roundedS = qfq_round_down(cl->S, grp->slot_shift);
  759. offset = (roundedS - grp->S) >> grp->slot_shift;
  760. i = (grp->front + offset) % QFQ_MAX_SLOTS;
  761. hlist_del(&cl->next);
  762. if (hlist_empty(&grp->slots[i]))
  763. __clear_bit(offset, &grp->full_slots);
  764. }
  765. /*
  766. * called to forcibly destroy a queue.
  767. * If the queue is not in the front bucket, or if it has
  768. * other queues in the front bucket, we can simply remove
  769. * the queue with no other side effects.
  770. * Otherwise we must propagate the event up.
  771. */
  772. static void qfq_deactivate_class(struct qfq_sched *q, struct qfq_class *cl)
  773. {
  774. struct qfq_group *grp = cl->grp;
  775. unsigned long mask;
  776. u64 roundedS;
  777. int s;
  778. cl->F = cl->S;
  779. qfq_slot_remove(q, grp, cl);
  780. if (!grp->full_slots) {
  781. __clear_bit(grp->index, &q->bitmaps[IR]);
  782. __clear_bit(grp->index, &q->bitmaps[EB]);
  783. __clear_bit(grp->index, &q->bitmaps[IB]);
  784. if (test_bit(grp->index, &q->bitmaps[ER]) &&
  785. !(q->bitmaps[ER] & ~((1UL << grp->index) - 1))) {
  786. mask = q->bitmaps[ER] & ((1UL << grp->index) - 1);
  787. if (mask)
  788. mask = ~((1UL << __fls(mask)) - 1);
  789. else
  790. mask = ~0UL;
  791. qfq_move_groups(q, mask, EB, ER);
  792. qfq_move_groups(q, mask, IB, IR);
  793. }
  794. __clear_bit(grp->index, &q->bitmaps[ER]);
  795. } else if (hlist_empty(&grp->slots[grp->front])) {
  796. cl = qfq_slot_scan(grp);
  797. roundedS = qfq_round_down(cl->S, grp->slot_shift);
  798. if (grp->S != roundedS) {
  799. __clear_bit(grp->index, &q->bitmaps[ER]);
  800. __clear_bit(grp->index, &q->bitmaps[IR]);
  801. __clear_bit(grp->index, &q->bitmaps[EB]);
  802. __clear_bit(grp->index, &q->bitmaps[IB]);
  803. grp->S = roundedS;
  804. grp->F = roundedS + (2ULL << grp->slot_shift);
  805. s = qfq_calc_state(q, grp);
  806. __set_bit(grp->index, &q->bitmaps[s]);
  807. }
  808. }
  809. qfq_update_eligible(q, q->V);
  810. }
  811. static void qfq_qlen_notify(struct Qdisc *sch, unsigned long arg)
  812. {
  813. struct qfq_sched *q = qdisc_priv(sch);
  814. struct qfq_class *cl = (struct qfq_class *)arg;
  815. if (cl->qdisc->q.qlen == 0)
  816. qfq_deactivate_class(q, cl);
  817. }
  818. static unsigned int qfq_drop(struct Qdisc *sch)
  819. {
  820. struct qfq_sched *q = qdisc_priv(sch);
  821. struct qfq_group *grp;
  822. unsigned int i, j, len;
  823. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  824. grp = &q->groups[i];
  825. for (j = 0; j < QFQ_MAX_SLOTS; j++) {
  826. struct qfq_class *cl;
  827. struct hlist_node *n;
  828. hlist_for_each_entry(cl, n, &grp->slots[j], next) {
  829. if (!cl->qdisc->ops->drop)
  830. continue;
  831. len = cl->qdisc->ops->drop(cl->qdisc);
  832. if (len > 0) {
  833. sch->q.qlen--;
  834. if (!cl->qdisc->q.qlen)
  835. qfq_deactivate_class(q, cl);
  836. return len;
  837. }
  838. }
  839. }
  840. }
  841. return 0;
  842. }
  843. static int qfq_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
  844. {
  845. struct qfq_sched *q = qdisc_priv(sch);
  846. struct qfq_group *grp;
  847. int i, j, err;
  848. err = qdisc_class_hash_init(&q->clhash);
  849. if (err < 0)
  850. return err;
  851. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  852. grp = &q->groups[i];
  853. grp->index = i;
  854. grp->slot_shift = QFQ_MTU_SHIFT + FRAC_BITS
  855. - (QFQ_MAX_INDEX - i);
  856. for (j = 0; j < QFQ_MAX_SLOTS; j++)
  857. INIT_HLIST_HEAD(&grp->slots[j]);
  858. }
  859. return 0;
  860. }
  861. static void qfq_reset_qdisc(struct Qdisc *sch)
  862. {
  863. struct qfq_sched *q = qdisc_priv(sch);
  864. struct qfq_group *grp;
  865. struct qfq_class *cl;
  866. struct hlist_node *n, *tmp;
  867. unsigned int i, j;
  868. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  869. grp = &q->groups[i];
  870. for (j = 0; j < QFQ_MAX_SLOTS; j++) {
  871. hlist_for_each_entry_safe(cl, n, tmp,
  872. &grp->slots[j], next) {
  873. qfq_deactivate_class(q, cl);
  874. }
  875. }
  876. }
  877. for (i = 0; i < q->clhash.hashsize; i++) {
  878. hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode)
  879. qdisc_reset(cl->qdisc);
  880. }
  881. sch->q.qlen = 0;
  882. }
  883. static void qfq_destroy_qdisc(struct Qdisc *sch)
  884. {
  885. struct qfq_sched *q = qdisc_priv(sch);
  886. struct qfq_class *cl;
  887. struct hlist_node *n, *next;
  888. unsigned int i;
  889. tcf_destroy_chain(&q->filter_list);
  890. for (i = 0; i < q->clhash.hashsize; i++) {
  891. hlist_for_each_entry_safe(cl, n, next, &q->clhash.hash[i],
  892. common.hnode) {
  893. qfq_destroy_class(sch, cl);
  894. }
  895. }
  896. qdisc_class_hash_destroy(&q->clhash);
  897. }
  898. static const struct Qdisc_class_ops qfq_class_ops = {
  899. .change = qfq_change_class,
  900. .delete = qfq_delete_class,
  901. .get = qfq_get_class,
  902. .put = qfq_put_class,
  903. .tcf_chain = qfq_tcf_chain,
  904. .bind_tcf = qfq_bind_tcf,
  905. .unbind_tcf = qfq_unbind_tcf,
  906. .graft = qfq_graft_class,
  907. .leaf = qfq_class_leaf,
  908. .qlen_notify = qfq_qlen_notify,
  909. .dump = qfq_dump_class,
  910. .dump_stats = qfq_dump_class_stats,
  911. .walk = qfq_walk,
  912. };
  913. static struct Qdisc_ops qfq_qdisc_ops __read_mostly = {
  914. .cl_ops = &qfq_class_ops,
  915. .id = "qfq",
  916. .priv_size = sizeof(struct qfq_sched),
  917. .enqueue = qfq_enqueue,
  918. .dequeue = qfq_dequeue,
  919. .peek = qdisc_peek_dequeued,
  920. .drop = qfq_drop,
  921. .init = qfq_init_qdisc,
  922. .reset = qfq_reset_qdisc,
  923. .destroy = qfq_destroy_qdisc,
  924. .owner = THIS_MODULE,
  925. };
  926. static int __init qfq_init(void)
  927. {
  928. return register_qdisc(&qfq_qdisc_ops);
  929. }
  930. static void __exit qfq_exit(void)
  931. {
  932. unregister_qdisc(&qfq_qdisc_ops);
  933. }
  934. module_init(qfq_init);
  935. module_exit(qfq_exit);
  936. MODULE_LICENSE("GPL");