sch_qfq.c 27 KB

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