sch_qfq.c 27 KB

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