sch_qfq.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /*
  2. * net/sched/sch_qfq.c Quick Fair Queueing Plus Scheduler.
  3. *
  4. * Copyright (c) 2009 Fabio Checconi, Luigi Rizzo, and Paolo Valente.
  5. * Copyright (c) 2012 Paolo Valente.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/bitops.h>
  14. #include <linux/errno.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/pkt_sched.h>
  17. #include <net/sch_generic.h>
  18. #include <net/pkt_sched.h>
  19. #include <net/pkt_cls.h>
  20. /* Quick Fair Queueing Plus
  21. ========================
  22. Sources:
  23. [1] Paolo Valente,
  24. "Reducing the Execution Time of Fair-Queueing Schedulers."
  25. http://algo.ing.unimo.it/people/paolo/agg-sched/agg-sched.pdf
  26. Sources for QFQ:
  27. [2] Fabio Checconi, Luigi Rizzo, and Paolo Valente: "QFQ: Efficient
  28. Packet Scheduling with Tight Bandwidth Distribution Guarantees."
  29. See also:
  30. http://retis.sssup.it/~fabio/linux/qfq/
  31. */
  32. /*
  33. QFQ+ divides classes into aggregates of at most MAX_AGG_CLASSES
  34. classes. Each aggregate is timestamped with a virtual start time S
  35. and a virtual finish time F, and scheduled according to its
  36. timestamps. S and F are computed as a function of a system virtual
  37. time function V. The classes within each aggregate are instead
  38. scheduled with DRR.
  39. To speed up operations, QFQ+ divides also aggregates into a limited
  40. number of groups. Which group a class belongs to depends on the
  41. ratio between the maximum packet length for the class and the weight
  42. of the class. Groups have their own S and F. In the end, QFQ+
  43. schedules groups, then aggregates within groups, then classes within
  44. aggregates. See [1] and [2] for a full description.
  45. Virtual time computations.
  46. S, F and V are all computed in fixed point arithmetic with
  47. FRAC_BITS decimal bits.
  48. QFQ_MAX_INDEX is the maximum index allowed for a group. We need
  49. one bit per index.
  50. QFQ_MAX_WSHIFT is the maximum power of two supported as a weight.
  51. The layout of the bits is as below:
  52. [ MTU_SHIFT ][ FRAC_BITS ]
  53. [ MAX_INDEX ][ MIN_SLOT_SHIFT ]
  54. ^.__grp->index = 0
  55. *.__grp->slot_shift
  56. where MIN_SLOT_SHIFT is derived by difference from the others.
  57. The max group index corresponds to Lmax/w_min, where
  58. Lmax=1<<MTU_SHIFT, w_min = 1 .
  59. From this, and knowing how many groups (MAX_INDEX) we want,
  60. we can derive the shift corresponding to each group.
  61. Because we often need to compute
  62. F = S + len/w_i and V = V + len/wsum
  63. instead of storing w_i store the value
  64. inv_w = (1<<FRAC_BITS)/w_i
  65. so we can do F = S + len * inv_w * wsum.
  66. We use W_TOT in the formulas so we can easily move between
  67. static and adaptive weight sum.
  68. The per-scheduler-instance data contain all the data structures
  69. for the scheduler: bitmaps and bucket lists.
  70. */
  71. /*
  72. * Maximum number of consecutive slots occupied by backlogged classes
  73. * inside a group.
  74. */
  75. #define QFQ_MAX_SLOTS 32
  76. /*
  77. * Shifts used for aggregate<->group mapping. We allow class weights that are
  78. * in the range [1, 2^MAX_WSHIFT], and we try to map each aggregate i to the
  79. * group with the smallest index that can support the L_i / r_i configured
  80. * for the classes in the aggregate.
  81. *
  82. * grp->index is the index of the group; and grp->slot_shift
  83. * is the shift for the corresponding (scaled) sigma_i.
  84. */
  85. #define QFQ_MAX_INDEX 24
  86. #define QFQ_MAX_WSHIFT 10
  87. #define QFQ_MAX_WEIGHT (1<<QFQ_MAX_WSHIFT) /* see qfq_slot_insert */
  88. #define QFQ_MAX_WSUM (64*QFQ_MAX_WEIGHT)
  89. #define FRAC_BITS 30 /* fixed point arithmetic */
  90. #define ONE_FP (1UL << FRAC_BITS)
  91. #define IWSUM (ONE_FP/QFQ_MAX_WSUM)
  92. #define QFQ_MTU_SHIFT 16 /* to support TSO/GSO */
  93. #define QFQ_MIN_LMAX 512 /* see qfq_slot_insert */
  94. #define QFQ_MAX_AGG_CLASSES 8 /* max num classes per aggregate allowed */
  95. /*
  96. * Possible group states. These values are used as indexes for the bitmaps
  97. * array of struct qfq_queue.
  98. */
  99. enum qfq_state { ER, IR, EB, IB, QFQ_MAX_STATE };
  100. struct qfq_group;
  101. struct qfq_aggregate;
  102. struct qfq_class {
  103. struct Qdisc_class_common common;
  104. unsigned int refcnt;
  105. unsigned int filter_cnt;
  106. struct gnet_stats_basic_packed bstats;
  107. struct gnet_stats_queue qstats;
  108. struct gnet_stats_rate_est rate_est;
  109. struct Qdisc *qdisc;
  110. struct list_head alist; /* Link for active-classes list. */
  111. struct qfq_aggregate *agg; /* Parent aggregate. */
  112. int deficit; /* DRR deficit counter. */
  113. };
  114. struct qfq_aggregate {
  115. struct hlist_node next; /* Link for the slot list. */
  116. u64 S, F; /* flow timestamps (exact) */
  117. /* group we belong to. In principle we would need the index,
  118. * which is log_2(lmax/weight), but we never reference it
  119. * directly, only the group.
  120. */
  121. struct qfq_group *grp;
  122. /* these are copied from the flowset. */
  123. u32 class_weight; /* Weight of each class in this aggregate. */
  124. /* Max pkt size for the classes in this aggregate, DRR quantum. */
  125. int lmax;
  126. u32 inv_w; /* ONE_FP/(sum of weights of classes in aggr.). */
  127. u32 budgetmax; /* Max budget for this aggregate. */
  128. u32 initial_budget, budget; /* Initial and current budget. */
  129. int num_classes; /* Number of classes in this aggr. */
  130. struct list_head active; /* DRR queue of active classes. */
  131. struct hlist_node nonfull_next; /* See nonfull_aggs in qfq_sched. */
  132. };
  133. struct qfq_group {
  134. u64 S, F; /* group timestamps (approx). */
  135. unsigned int slot_shift; /* Slot shift. */
  136. unsigned int index; /* Group index. */
  137. unsigned int front; /* Index of the front slot. */
  138. unsigned long full_slots; /* non-empty slots */
  139. /* Array of RR lists of active aggregates. */
  140. struct hlist_head slots[QFQ_MAX_SLOTS];
  141. };
  142. struct qfq_sched {
  143. struct tcf_proto *filter_list;
  144. struct Qdisc_class_hash clhash;
  145. u64 oldV, V; /* Precise virtual times. */
  146. struct qfq_aggregate *in_serv_agg; /* Aggregate being served. */
  147. u32 num_active_agg; /* Num. of active aggregates */
  148. u32 wsum; /* weight sum */
  149. unsigned long bitmaps[QFQ_MAX_STATE]; /* Group bitmaps. */
  150. struct qfq_group groups[QFQ_MAX_INDEX + 1]; /* The groups. */
  151. u32 min_slot_shift; /* Index of the group-0 bit in the bitmaps. */
  152. u32 max_agg_classes; /* Max number of classes per aggr. */
  153. struct hlist_head nonfull_aggs; /* Aggs with room for more classes. */
  154. };
  155. /*
  156. * Possible reasons why the timestamps of an aggregate are updated
  157. * enqueue: the aggregate switches from idle to active and must scheduled
  158. * for service
  159. * requeue: the aggregate finishes its budget, so it stops being served and
  160. * must be rescheduled for service
  161. */
  162. enum update_reason {enqueue, requeue};
  163. static struct qfq_class *qfq_find_class(struct Qdisc *sch, u32 classid)
  164. {
  165. struct qfq_sched *q = qdisc_priv(sch);
  166. struct Qdisc_class_common *clc;
  167. clc = qdisc_class_find(&q->clhash, classid);
  168. if (clc == NULL)
  169. return NULL;
  170. return container_of(clc, struct qfq_class, common);
  171. }
  172. static void qfq_purge_queue(struct qfq_class *cl)
  173. {
  174. unsigned int len = cl->qdisc->q.qlen;
  175. qdisc_reset(cl->qdisc);
  176. qdisc_tree_decrease_qlen(cl->qdisc, len);
  177. }
  178. static const struct nla_policy qfq_policy[TCA_QFQ_MAX + 1] = {
  179. [TCA_QFQ_WEIGHT] = { .type = NLA_U32 },
  180. [TCA_QFQ_LMAX] = { .type = NLA_U32 },
  181. };
  182. /*
  183. * Calculate a flow index, given its weight and maximum packet length.
  184. * index = log_2(maxlen/weight) but we need to apply the scaling.
  185. * This is used only once at flow creation.
  186. */
  187. static int qfq_calc_index(u32 inv_w, unsigned int maxlen, u32 min_slot_shift)
  188. {
  189. u64 slot_size = (u64)maxlen * inv_w;
  190. unsigned long size_map;
  191. int index = 0;
  192. size_map = slot_size >> min_slot_shift;
  193. if (!size_map)
  194. goto out;
  195. index = __fls(size_map) + 1; /* basically a log_2 */
  196. index -= !(slot_size - (1ULL << (index + min_slot_shift - 1)));
  197. if (index < 0)
  198. index = 0;
  199. out:
  200. pr_debug("qfq calc_index: W = %lu, L = %u, I = %d\n",
  201. (unsigned long) ONE_FP/inv_w, maxlen, index);
  202. return index;
  203. }
  204. static void qfq_deactivate_agg(struct qfq_sched *, struct qfq_aggregate *);
  205. static void qfq_activate_agg(struct qfq_sched *, struct qfq_aggregate *,
  206. enum update_reason);
  207. static void qfq_init_agg(struct qfq_sched *q, struct qfq_aggregate *agg,
  208. u32 lmax, u32 weight)
  209. {
  210. INIT_LIST_HEAD(&agg->active);
  211. hlist_add_head(&agg->nonfull_next, &q->nonfull_aggs);
  212. agg->lmax = lmax;
  213. agg->class_weight = weight;
  214. }
  215. static struct qfq_aggregate *qfq_find_agg(struct qfq_sched *q,
  216. u32 lmax, u32 weight)
  217. {
  218. struct qfq_aggregate *agg;
  219. hlist_for_each_entry(agg, &q->nonfull_aggs, nonfull_next)
  220. if (agg->lmax == lmax && agg->class_weight == weight)
  221. return agg;
  222. return NULL;
  223. }
  224. /* Update aggregate as a function of the new number of classes. */
  225. static void qfq_update_agg(struct qfq_sched *q, struct qfq_aggregate *agg,
  226. int new_num_classes)
  227. {
  228. u32 new_agg_weight;
  229. if (new_num_classes == q->max_agg_classes)
  230. hlist_del_init(&agg->nonfull_next);
  231. if (agg->num_classes > new_num_classes &&
  232. new_num_classes == q->max_agg_classes - 1) /* agg no more full */
  233. hlist_add_head(&agg->nonfull_next, &q->nonfull_aggs);
  234. agg->budgetmax = new_num_classes * agg->lmax;
  235. new_agg_weight = agg->class_weight * new_num_classes;
  236. agg->inv_w = ONE_FP/new_agg_weight;
  237. if (agg->grp == NULL) {
  238. int i = qfq_calc_index(agg->inv_w, agg->budgetmax,
  239. q->min_slot_shift);
  240. agg->grp = &q->groups[i];
  241. }
  242. q->wsum +=
  243. (int) agg->class_weight * (new_num_classes - agg->num_classes);
  244. agg->num_classes = new_num_classes;
  245. }
  246. /* Add class to aggregate. */
  247. static void qfq_add_to_agg(struct qfq_sched *q,
  248. struct qfq_aggregate *agg,
  249. struct qfq_class *cl)
  250. {
  251. cl->agg = agg;
  252. qfq_update_agg(q, agg, agg->num_classes+1);
  253. if (cl->qdisc->q.qlen > 0) { /* adding an active class */
  254. list_add_tail(&cl->alist, &agg->active);
  255. if (list_first_entry(&agg->active, struct qfq_class, alist) ==
  256. cl && q->in_serv_agg != agg) /* agg was inactive */
  257. qfq_activate_agg(q, agg, enqueue); /* schedule agg */
  258. }
  259. }
  260. static struct qfq_aggregate *qfq_choose_next_agg(struct qfq_sched *);
  261. static void qfq_destroy_agg(struct qfq_sched *q, struct qfq_aggregate *agg)
  262. {
  263. if (!hlist_unhashed(&agg->nonfull_next))
  264. hlist_del_init(&agg->nonfull_next);
  265. if (q->in_serv_agg == agg)
  266. q->in_serv_agg = qfq_choose_next_agg(q);
  267. kfree(agg);
  268. }
  269. /* Deschedule class from within its parent aggregate. */
  270. static void qfq_deactivate_class(struct qfq_sched *q, struct qfq_class *cl)
  271. {
  272. struct qfq_aggregate *agg = cl->agg;
  273. list_del(&cl->alist); /* remove from RR queue of the aggregate */
  274. if (list_empty(&agg->active)) /* agg is now inactive */
  275. qfq_deactivate_agg(q, agg);
  276. }
  277. /* Remove class from its parent aggregate. */
  278. static void qfq_rm_from_agg(struct qfq_sched *q, struct qfq_class *cl)
  279. {
  280. struct qfq_aggregate *agg = cl->agg;
  281. cl->agg = NULL;
  282. if (agg->num_classes == 1) { /* agg being emptied, destroy it */
  283. qfq_destroy_agg(q, agg);
  284. return;
  285. }
  286. qfq_update_agg(q, agg, agg->num_classes-1);
  287. }
  288. /* Deschedule class and remove it from its parent aggregate. */
  289. static void qfq_deact_rm_from_agg(struct qfq_sched *q, struct qfq_class *cl)
  290. {
  291. if (cl->qdisc->q.qlen > 0) /* class is active */
  292. qfq_deactivate_class(q, cl);
  293. qfq_rm_from_agg(q, cl);
  294. }
  295. /* Move class to a new aggregate, matching the new class weight and/or lmax */
  296. static int qfq_change_agg(struct Qdisc *sch, struct qfq_class *cl, u32 weight,
  297. u32 lmax)
  298. {
  299. struct qfq_sched *q = qdisc_priv(sch);
  300. struct qfq_aggregate *new_agg = qfq_find_agg(q, lmax, weight);
  301. if (new_agg == NULL) { /* create new aggregate */
  302. new_agg = kzalloc(sizeof(*new_agg), GFP_ATOMIC);
  303. if (new_agg == NULL)
  304. return -ENOBUFS;
  305. qfq_init_agg(q, new_agg, lmax, weight);
  306. }
  307. qfq_deact_rm_from_agg(q, cl);
  308. qfq_add_to_agg(q, new_agg, cl);
  309. return 0;
  310. }
  311. static int qfq_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
  312. struct nlattr **tca, unsigned long *arg)
  313. {
  314. struct qfq_sched *q = qdisc_priv(sch);
  315. struct qfq_class *cl = (struct qfq_class *)*arg;
  316. bool existing = false;
  317. struct nlattr *tb[TCA_QFQ_MAX + 1];
  318. struct qfq_aggregate *new_agg = NULL;
  319. u32 weight, lmax, inv_w;
  320. int err;
  321. int delta_w;
  322. if (tca[TCA_OPTIONS] == NULL) {
  323. pr_notice("qfq: no options\n");
  324. return -EINVAL;
  325. }
  326. err = nla_parse_nested(tb, TCA_QFQ_MAX, tca[TCA_OPTIONS], qfq_policy);
  327. if (err < 0)
  328. return err;
  329. if (tb[TCA_QFQ_WEIGHT]) {
  330. weight = nla_get_u32(tb[TCA_QFQ_WEIGHT]);
  331. if (!weight || weight > (1UL << QFQ_MAX_WSHIFT)) {
  332. pr_notice("qfq: invalid weight %u\n", weight);
  333. return -EINVAL;
  334. }
  335. } else
  336. weight = 1;
  337. if (tb[TCA_QFQ_LMAX]) {
  338. lmax = nla_get_u32(tb[TCA_QFQ_LMAX]);
  339. if (lmax < QFQ_MIN_LMAX || lmax > (1UL << QFQ_MTU_SHIFT)) {
  340. pr_notice("qfq: invalid max length %u\n", lmax);
  341. return -EINVAL;
  342. }
  343. } else
  344. lmax = psched_mtu(qdisc_dev(sch));
  345. inv_w = ONE_FP / weight;
  346. weight = ONE_FP / inv_w;
  347. if (cl != NULL &&
  348. lmax == cl->agg->lmax &&
  349. weight == cl->agg->class_weight)
  350. return 0; /* nothing to change */
  351. delta_w = weight - (cl ? cl->agg->class_weight : 0);
  352. if (q->wsum + delta_w > QFQ_MAX_WSUM) {
  353. pr_notice("qfq: total weight out of range (%d + %u)\n",
  354. delta_w, q->wsum);
  355. return -EINVAL;
  356. }
  357. if (cl != NULL) { /* modify existing class */
  358. if (tca[TCA_RATE]) {
  359. err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
  360. qdisc_root_sleeping_lock(sch),
  361. tca[TCA_RATE]);
  362. if (err)
  363. return err;
  364. }
  365. existing = true;
  366. goto set_change_agg;
  367. }
  368. /* create and init new class */
  369. cl = kzalloc(sizeof(struct qfq_class), GFP_KERNEL);
  370. if (cl == NULL)
  371. return -ENOBUFS;
  372. cl->refcnt = 1;
  373. cl->common.classid = classid;
  374. cl->deficit = lmax;
  375. cl->qdisc = qdisc_create_dflt(sch->dev_queue,
  376. &pfifo_qdisc_ops, classid);
  377. if (cl->qdisc == NULL)
  378. cl->qdisc = &noop_qdisc;
  379. if (tca[TCA_RATE]) {
  380. err = gen_new_estimator(&cl->bstats, &cl->rate_est,
  381. qdisc_root_sleeping_lock(sch),
  382. tca[TCA_RATE]);
  383. if (err)
  384. goto destroy_class;
  385. }
  386. sch_tree_lock(sch);
  387. qdisc_class_hash_insert(&q->clhash, &cl->common);
  388. sch_tree_unlock(sch);
  389. qdisc_class_hash_grow(sch, &q->clhash);
  390. set_change_agg:
  391. sch_tree_lock(sch);
  392. new_agg = qfq_find_agg(q, lmax, weight);
  393. if (new_agg == NULL) { /* create new aggregate */
  394. sch_tree_unlock(sch);
  395. new_agg = kzalloc(sizeof(*new_agg), GFP_KERNEL);
  396. if (new_agg == NULL) {
  397. err = -ENOBUFS;
  398. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  399. goto destroy_class;
  400. }
  401. sch_tree_lock(sch);
  402. qfq_init_agg(q, new_agg, lmax, weight);
  403. }
  404. if (existing)
  405. qfq_deact_rm_from_agg(q, cl);
  406. qfq_add_to_agg(q, new_agg, cl);
  407. sch_tree_unlock(sch);
  408. *arg = (unsigned long)cl;
  409. return 0;
  410. destroy_class:
  411. qdisc_destroy(cl->qdisc);
  412. kfree(cl);
  413. return err;
  414. }
  415. static void qfq_destroy_class(struct Qdisc *sch, struct qfq_class *cl)
  416. {
  417. struct qfq_sched *q = qdisc_priv(sch);
  418. qfq_rm_from_agg(q, cl);
  419. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  420. qdisc_destroy(cl->qdisc);
  421. kfree(cl);
  422. }
  423. static int qfq_delete_class(struct Qdisc *sch, unsigned long arg)
  424. {
  425. struct qfq_sched *q = qdisc_priv(sch);
  426. struct qfq_class *cl = (struct qfq_class *)arg;
  427. if (cl->filter_cnt > 0)
  428. return -EBUSY;
  429. sch_tree_lock(sch);
  430. qfq_purge_queue(cl);
  431. qdisc_class_hash_remove(&q->clhash, &cl->common);
  432. BUG_ON(--cl->refcnt == 0);
  433. /*
  434. * This shouldn't happen: we "hold" one cops->get() when called
  435. * from tc_ctl_tclass; the destroy method is done from cops->put().
  436. */
  437. sch_tree_unlock(sch);
  438. return 0;
  439. }
  440. static unsigned long qfq_get_class(struct Qdisc *sch, u32 classid)
  441. {
  442. struct qfq_class *cl = qfq_find_class(sch, classid);
  443. if (cl != NULL)
  444. cl->refcnt++;
  445. return (unsigned long)cl;
  446. }
  447. static void qfq_put_class(struct Qdisc *sch, unsigned long arg)
  448. {
  449. struct qfq_class *cl = (struct qfq_class *)arg;
  450. if (--cl->refcnt == 0)
  451. qfq_destroy_class(sch, cl);
  452. }
  453. static struct tcf_proto **qfq_tcf_chain(struct Qdisc *sch, unsigned long cl)
  454. {
  455. struct qfq_sched *q = qdisc_priv(sch);
  456. if (cl)
  457. return NULL;
  458. return &q->filter_list;
  459. }
  460. static unsigned long qfq_bind_tcf(struct Qdisc *sch, unsigned long parent,
  461. u32 classid)
  462. {
  463. struct qfq_class *cl = qfq_find_class(sch, classid);
  464. if (cl != NULL)
  465. cl->filter_cnt++;
  466. return (unsigned long)cl;
  467. }
  468. static void qfq_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  469. {
  470. struct qfq_class *cl = (struct qfq_class *)arg;
  471. cl->filter_cnt--;
  472. }
  473. static int qfq_graft_class(struct Qdisc *sch, unsigned long arg,
  474. struct Qdisc *new, struct Qdisc **old)
  475. {
  476. struct qfq_class *cl = (struct qfq_class *)arg;
  477. if (new == NULL) {
  478. new = qdisc_create_dflt(sch->dev_queue,
  479. &pfifo_qdisc_ops, cl->common.classid);
  480. if (new == NULL)
  481. new = &noop_qdisc;
  482. }
  483. sch_tree_lock(sch);
  484. qfq_purge_queue(cl);
  485. *old = cl->qdisc;
  486. cl->qdisc = new;
  487. sch_tree_unlock(sch);
  488. return 0;
  489. }
  490. static struct Qdisc *qfq_class_leaf(struct Qdisc *sch, unsigned long arg)
  491. {
  492. struct qfq_class *cl = (struct qfq_class *)arg;
  493. return cl->qdisc;
  494. }
  495. static int qfq_dump_class(struct Qdisc *sch, unsigned long arg,
  496. struct sk_buff *skb, struct tcmsg *tcm)
  497. {
  498. struct qfq_class *cl = (struct qfq_class *)arg;
  499. struct nlattr *nest;
  500. tcm->tcm_parent = TC_H_ROOT;
  501. tcm->tcm_handle = cl->common.classid;
  502. tcm->tcm_info = cl->qdisc->handle;
  503. nest = nla_nest_start(skb, TCA_OPTIONS);
  504. if (nest == NULL)
  505. goto nla_put_failure;
  506. if (nla_put_u32(skb, TCA_QFQ_WEIGHT, cl->agg->class_weight) ||
  507. nla_put_u32(skb, TCA_QFQ_LMAX, cl->agg->lmax))
  508. goto nla_put_failure;
  509. return nla_nest_end(skb, nest);
  510. nla_put_failure:
  511. nla_nest_cancel(skb, nest);
  512. return -EMSGSIZE;
  513. }
  514. static int qfq_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  515. struct gnet_dump *d)
  516. {
  517. struct qfq_class *cl = (struct qfq_class *)arg;
  518. struct tc_qfq_stats xstats;
  519. memset(&xstats, 0, sizeof(xstats));
  520. cl->qdisc->qstats.qlen = cl->qdisc->q.qlen;
  521. xstats.weight = cl->agg->class_weight;
  522. xstats.lmax = cl->agg->lmax;
  523. if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
  524. gnet_stats_copy_rate_est(d, &cl->bstats, &cl->rate_est) < 0 ||
  525. gnet_stats_copy_queue(d, &cl->qdisc->qstats) < 0)
  526. return -1;
  527. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  528. }
  529. static void qfq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  530. {
  531. struct qfq_sched *q = qdisc_priv(sch);
  532. struct qfq_class *cl;
  533. unsigned int i;
  534. if (arg->stop)
  535. return;
  536. for (i = 0; i < q->clhash.hashsize; i++) {
  537. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
  538. if (arg->count < arg->skip) {
  539. arg->count++;
  540. continue;
  541. }
  542. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  543. arg->stop = 1;
  544. return;
  545. }
  546. arg->count++;
  547. }
  548. }
  549. }
  550. static struct qfq_class *qfq_classify(struct sk_buff *skb, struct Qdisc *sch,
  551. int *qerr)
  552. {
  553. struct qfq_sched *q = qdisc_priv(sch);
  554. struct qfq_class *cl;
  555. struct tcf_result res;
  556. int result;
  557. if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
  558. pr_debug("qfq_classify: found %d\n", skb->priority);
  559. cl = qfq_find_class(sch, skb->priority);
  560. if (cl != NULL)
  561. return cl;
  562. }
  563. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  564. result = tc_classify(skb, q->filter_list, &res);
  565. if (result >= 0) {
  566. #ifdef CONFIG_NET_CLS_ACT
  567. switch (result) {
  568. case TC_ACT_QUEUED:
  569. case TC_ACT_STOLEN:
  570. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  571. case TC_ACT_SHOT:
  572. return NULL;
  573. }
  574. #endif
  575. cl = (struct qfq_class *)res.class;
  576. if (cl == NULL)
  577. cl = qfq_find_class(sch, res.classid);
  578. return cl;
  579. }
  580. return NULL;
  581. }
  582. /* Generic comparison function, handling wraparound. */
  583. static inline int qfq_gt(u64 a, u64 b)
  584. {
  585. return (s64)(a - b) > 0;
  586. }
  587. /* Round a precise timestamp to its slotted value. */
  588. static inline u64 qfq_round_down(u64 ts, unsigned int shift)
  589. {
  590. return ts & ~((1ULL << shift) - 1);
  591. }
  592. /* return the pointer to the group with lowest index in the bitmap */
  593. static inline struct qfq_group *qfq_ffs(struct qfq_sched *q,
  594. unsigned long bitmap)
  595. {
  596. int index = __ffs(bitmap);
  597. return &q->groups[index];
  598. }
  599. /* Calculate a mask to mimic what would be ffs_from(). */
  600. static inline unsigned long mask_from(unsigned long bitmap, int from)
  601. {
  602. return bitmap & ~((1UL << from) - 1);
  603. }
  604. /*
  605. * The state computation relies on ER=0, IR=1, EB=2, IB=3
  606. * First compute eligibility comparing grp->S, q->V,
  607. * then check if someone is blocking us and possibly add EB
  608. */
  609. static int qfq_calc_state(struct qfq_sched *q, const struct qfq_group *grp)
  610. {
  611. /* if S > V we are not eligible */
  612. unsigned int state = qfq_gt(grp->S, q->V);
  613. unsigned long mask = mask_from(q->bitmaps[ER], grp->index);
  614. struct qfq_group *next;
  615. if (mask) {
  616. next = qfq_ffs(q, mask);
  617. if (qfq_gt(grp->F, next->F))
  618. state |= EB;
  619. }
  620. return state;
  621. }
  622. /*
  623. * In principle
  624. * q->bitmaps[dst] |= q->bitmaps[src] & mask;
  625. * q->bitmaps[src] &= ~mask;
  626. * but we should make sure that src != dst
  627. */
  628. static inline void qfq_move_groups(struct qfq_sched *q, unsigned long mask,
  629. int src, int dst)
  630. {
  631. q->bitmaps[dst] |= q->bitmaps[src] & mask;
  632. q->bitmaps[src] &= ~mask;
  633. }
  634. static void qfq_unblock_groups(struct qfq_sched *q, int index, u64 old_F)
  635. {
  636. unsigned long mask = mask_from(q->bitmaps[ER], index + 1);
  637. struct qfq_group *next;
  638. if (mask) {
  639. next = qfq_ffs(q, mask);
  640. if (!qfq_gt(next->F, old_F))
  641. return;
  642. }
  643. mask = (1UL << index) - 1;
  644. qfq_move_groups(q, mask, EB, ER);
  645. qfq_move_groups(q, mask, IB, IR);
  646. }
  647. /*
  648. * perhaps
  649. *
  650. old_V ^= q->V;
  651. old_V >>= q->min_slot_shift;
  652. if (old_V) {
  653. ...
  654. }
  655. *
  656. */
  657. static void qfq_make_eligible(struct qfq_sched *q)
  658. {
  659. unsigned long vslot = q->V >> q->min_slot_shift;
  660. unsigned long old_vslot = q->oldV >> q->min_slot_shift;
  661. if (vslot != old_vslot) {
  662. unsigned long mask = (1UL << fls(vslot ^ old_vslot)) - 1;
  663. qfq_move_groups(q, mask, IR, ER);
  664. qfq_move_groups(q, mask, IB, EB);
  665. }
  666. }
  667. /*
  668. * The index of the slot in which the aggregate is to be inserted must
  669. * not be higher than QFQ_MAX_SLOTS-2. There is a '-2' and not a '-1'
  670. * because the start time of the group may be moved backward by one
  671. * slot after the aggregate has been inserted, and this would cause
  672. * non-empty slots to be right-shifted by one position.
  673. *
  674. * If the weight and lmax (max_pkt_size) of the classes do not change,
  675. * then QFQ+ does meet the above contraint according to the current
  676. * values of its parameters. In fact, if the weight and lmax of the
  677. * classes do not change, then, from the theory, QFQ+ guarantees that
  678. * the slot index is never higher than
  679. * 2 + QFQ_MAX_AGG_CLASSES * ((1<<QFQ_MTU_SHIFT)/QFQ_MIN_LMAX) *
  680. * (QFQ_MAX_WEIGHT/QFQ_MAX_WSUM) = 2 + 8 * 128 * (1 / 64) = 18
  681. *
  682. * When the weight of a class is increased or the lmax of the class is
  683. * decreased, a new aggregate with smaller slot size than the original
  684. * parent aggregate of the class may happen to be activated. The
  685. * activation of this aggregate should be properly delayed to when the
  686. * service of the class has finished in the ideal system tracked by
  687. * QFQ+. If the activation of the aggregate is not delayed to this
  688. * reference time instant, then this aggregate may be unjustly served
  689. * before other aggregates waiting for service. This may cause the
  690. * above bound to the slot index to be violated for some of these
  691. * unlucky aggregates.
  692. *
  693. * Instead of delaying the activation of the new aggregate, which is
  694. * quite complex, the following inaccurate but simple solution is used:
  695. * if the slot index is higher than QFQ_MAX_SLOTS-2, then the
  696. * timestamps of the aggregate are shifted backward so as to let the
  697. * slot index become equal to QFQ_MAX_SLOTS-2.
  698. */
  699. static void qfq_slot_insert(struct qfq_group *grp, struct qfq_aggregate *agg,
  700. u64 roundedS)
  701. {
  702. u64 slot = (roundedS - grp->S) >> grp->slot_shift;
  703. unsigned int i; /* slot index in the bucket list */
  704. if (unlikely(slot > QFQ_MAX_SLOTS - 2)) {
  705. u64 deltaS = roundedS - grp->S -
  706. ((u64)(QFQ_MAX_SLOTS - 2)<<grp->slot_shift);
  707. agg->S -= deltaS;
  708. agg->F -= deltaS;
  709. slot = QFQ_MAX_SLOTS - 2;
  710. }
  711. i = (grp->front + slot) % QFQ_MAX_SLOTS;
  712. hlist_add_head(&agg->next, &grp->slots[i]);
  713. __set_bit(slot, &grp->full_slots);
  714. }
  715. /* Maybe introduce hlist_first_entry?? */
  716. static struct qfq_aggregate *qfq_slot_head(struct qfq_group *grp)
  717. {
  718. return hlist_entry(grp->slots[grp->front].first,
  719. struct qfq_aggregate, next);
  720. }
  721. /*
  722. * remove the entry from the slot
  723. */
  724. static void qfq_front_slot_remove(struct qfq_group *grp)
  725. {
  726. struct qfq_aggregate *agg = qfq_slot_head(grp);
  727. BUG_ON(!agg);
  728. hlist_del(&agg->next);
  729. if (hlist_empty(&grp->slots[grp->front]))
  730. __clear_bit(0, &grp->full_slots);
  731. }
  732. /*
  733. * Returns the first aggregate in the first non-empty bucket of the
  734. * group. As a side effect, adjusts the bucket list so the first
  735. * non-empty bucket is at position 0 in full_slots.
  736. */
  737. static struct qfq_aggregate *qfq_slot_scan(struct qfq_group *grp)
  738. {
  739. unsigned int i;
  740. pr_debug("qfq slot_scan: grp %u full %#lx\n",
  741. grp->index, grp->full_slots);
  742. if (grp->full_slots == 0)
  743. return NULL;
  744. i = __ffs(grp->full_slots); /* zero based */
  745. if (i > 0) {
  746. grp->front = (grp->front + i) % QFQ_MAX_SLOTS;
  747. grp->full_slots >>= i;
  748. }
  749. return qfq_slot_head(grp);
  750. }
  751. /*
  752. * adjust the bucket list. When the start time of a group decreases,
  753. * we move the index down (modulo QFQ_MAX_SLOTS) so we don't need to
  754. * move the objects. The mask of occupied slots must be shifted
  755. * because we use ffs() to find the first non-empty slot.
  756. * This covers decreases in the group's start time, but what about
  757. * increases of the start time ?
  758. * Here too we should make sure that i is less than 32
  759. */
  760. static void qfq_slot_rotate(struct qfq_group *grp, u64 roundedS)
  761. {
  762. unsigned int i = (grp->S - roundedS) >> grp->slot_shift;
  763. grp->full_slots <<= i;
  764. grp->front = (grp->front - i) % QFQ_MAX_SLOTS;
  765. }
  766. static void qfq_update_eligible(struct qfq_sched *q)
  767. {
  768. struct qfq_group *grp;
  769. unsigned long ineligible;
  770. ineligible = q->bitmaps[IR] | q->bitmaps[IB];
  771. if (ineligible) {
  772. if (!q->bitmaps[ER]) {
  773. grp = qfq_ffs(q, ineligible);
  774. if (qfq_gt(grp->S, q->V))
  775. q->V = grp->S;
  776. }
  777. qfq_make_eligible(q);
  778. }
  779. }
  780. /* Dequeue head packet of the head class in the DRR queue of the aggregate. */
  781. static void agg_dequeue(struct qfq_aggregate *agg,
  782. struct qfq_class *cl, unsigned int len)
  783. {
  784. qdisc_dequeue_peeked(cl->qdisc);
  785. cl->deficit -= (int) len;
  786. if (cl->qdisc->q.qlen == 0) /* no more packets, remove from list */
  787. list_del(&cl->alist);
  788. else if (cl->deficit < qdisc_pkt_len(cl->qdisc->ops->peek(cl->qdisc))) {
  789. cl->deficit += agg->lmax;
  790. list_move_tail(&cl->alist, &agg->active);
  791. }
  792. }
  793. static inline struct sk_buff *qfq_peek_skb(struct qfq_aggregate *agg,
  794. struct qfq_class **cl,
  795. unsigned int *len)
  796. {
  797. struct sk_buff *skb;
  798. *cl = list_first_entry(&agg->active, struct qfq_class, alist);
  799. skb = (*cl)->qdisc->ops->peek((*cl)->qdisc);
  800. if (skb == NULL)
  801. WARN_ONCE(1, "qfq_dequeue: non-workconserving leaf\n");
  802. else
  803. *len = qdisc_pkt_len(skb);
  804. return skb;
  805. }
  806. /* Update F according to the actual service received by the aggregate. */
  807. static inline void charge_actual_service(struct qfq_aggregate *agg)
  808. {
  809. /* compute the service received by the aggregate */
  810. u32 service_received = agg->initial_budget - agg->budget;
  811. agg->F = agg->S + (u64)service_received * agg->inv_w;
  812. }
  813. static struct sk_buff *qfq_dequeue(struct Qdisc *sch)
  814. {
  815. struct qfq_sched *q = qdisc_priv(sch);
  816. struct qfq_aggregate *in_serv_agg = q->in_serv_agg;
  817. struct qfq_class *cl;
  818. struct sk_buff *skb = NULL;
  819. /* next-packet len, 0 means no more active classes in in-service agg */
  820. unsigned int len = 0;
  821. if (in_serv_agg == NULL)
  822. return NULL;
  823. if (!list_empty(&in_serv_agg->active))
  824. skb = qfq_peek_skb(in_serv_agg, &cl, &len);
  825. /*
  826. * If there are no active classes in the in-service aggregate,
  827. * or if the aggregate has not enough budget to serve its next
  828. * class, then choose the next aggregate to serve.
  829. */
  830. if (len == 0 || in_serv_agg->budget < len) {
  831. charge_actual_service(in_serv_agg);
  832. /* recharge the budget of the aggregate */
  833. in_serv_agg->initial_budget = in_serv_agg->budget =
  834. in_serv_agg->budgetmax;
  835. if (!list_empty(&in_serv_agg->active))
  836. /*
  837. * Still active: reschedule for
  838. * service. Possible optimization: if no other
  839. * aggregate is active, then there is no point
  840. * in rescheduling this aggregate, and we can
  841. * just keep it as the in-service one. This
  842. * should be however a corner case, and to
  843. * handle it, we would need to maintain an
  844. * extra num_active_aggs field.
  845. */
  846. qfq_activate_agg(q, in_serv_agg, requeue);
  847. else if (sch->q.qlen == 0) { /* no aggregate to serve */
  848. q->in_serv_agg = NULL;
  849. return NULL;
  850. }
  851. /*
  852. * If we get here, there are other aggregates queued:
  853. * choose the new aggregate to serve.
  854. */
  855. in_serv_agg = q->in_serv_agg = qfq_choose_next_agg(q);
  856. skb = qfq_peek_skb(in_serv_agg, &cl, &len);
  857. }
  858. if (!skb)
  859. return NULL;
  860. sch->q.qlen--;
  861. qdisc_bstats_update(sch, skb);
  862. agg_dequeue(in_serv_agg, cl, len);
  863. in_serv_agg->budget -= len;
  864. q->V += (u64)len * IWSUM;
  865. pr_debug("qfq dequeue: len %u F %lld now %lld\n",
  866. len, (unsigned long long) in_serv_agg->F,
  867. (unsigned long long) q->V);
  868. return skb;
  869. }
  870. static struct qfq_aggregate *qfq_choose_next_agg(struct qfq_sched *q)
  871. {
  872. struct qfq_group *grp;
  873. struct qfq_aggregate *agg, *new_front_agg;
  874. u64 old_F;
  875. qfq_update_eligible(q);
  876. q->oldV = q->V;
  877. if (!q->bitmaps[ER])
  878. return NULL;
  879. grp = qfq_ffs(q, q->bitmaps[ER]);
  880. old_F = grp->F;
  881. agg = qfq_slot_head(grp);
  882. /* agg starts to be served, remove it from schedule */
  883. qfq_front_slot_remove(grp);
  884. new_front_agg = qfq_slot_scan(grp);
  885. if (new_front_agg == NULL) /* group is now inactive, remove from ER */
  886. __clear_bit(grp->index, &q->bitmaps[ER]);
  887. else {
  888. u64 roundedS = qfq_round_down(new_front_agg->S,
  889. grp->slot_shift);
  890. unsigned int s;
  891. if (grp->S == roundedS)
  892. return agg;
  893. grp->S = roundedS;
  894. grp->F = roundedS + (2ULL << grp->slot_shift);
  895. __clear_bit(grp->index, &q->bitmaps[ER]);
  896. s = qfq_calc_state(q, grp);
  897. __set_bit(grp->index, &q->bitmaps[s]);
  898. }
  899. qfq_unblock_groups(q, grp->index, old_F);
  900. return agg;
  901. }
  902. /*
  903. * Assign a reasonable start time for a new aggregate in group i.
  904. * Admissible values for \hat(F) are multiples of \sigma_i
  905. * no greater than V+\sigma_i . Larger values mean that
  906. * we had a wraparound so we consider the timestamp to be stale.
  907. *
  908. * If F is not stale and F >= V then we set S = F.
  909. * Otherwise we should assign S = V, but this may violate
  910. * the ordering in EB (see [2]). So, if we have groups in ER,
  911. * set S to the F_j of the first group j which would be blocking us.
  912. * We are guaranteed not to move S backward because
  913. * otherwise our group i would still be blocked.
  914. */
  915. static void qfq_update_start(struct qfq_sched *q, struct qfq_aggregate *agg)
  916. {
  917. unsigned long mask;
  918. u64 limit, roundedF;
  919. int slot_shift = agg->grp->slot_shift;
  920. roundedF = qfq_round_down(agg->F, slot_shift);
  921. limit = qfq_round_down(q->V, slot_shift) + (1ULL << slot_shift);
  922. if (!qfq_gt(agg->F, q->V) || qfq_gt(roundedF, limit)) {
  923. /* timestamp was stale */
  924. mask = mask_from(q->bitmaps[ER], agg->grp->index);
  925. if (mask) {
  926. struct qfq_group *next = qfq_ffs(q, mask);
  927. if (qfq_gt(roundedF, next->F)) {
  928. if (qfq_gt(limit, next->F))
  929. agg->S = next->F;
  930. else /* preserve timestamp correctness */
  931. agg->S = limit;
  932. return;
  933. }
  934. }
  935. agg->S = q->V;
  936. } else /* timestamp is not stale */
  937. agg->S = agg->F;
  938. }
  939. /*
  940. * Update the timestamps of agg before scheduling/rescheduling it for
  941. * service. In particular, assign to agg->F its maximum possible
  942. * value, i.e., the virtual finish time with which the aggregate
  943. * should be labeled if it used all its budget once in service.
  944. */
  945. static inline void
  946. qfq_update_agg_ts(struct qfq_sched *q,
  947. struct qfq_aggregate *agg, enum update_reason reason)
  948. {
  949. if (reason != requeue)
  950. qfq_update_start(q, agg);
  951. else /* just charge agg for the service received */
  952. agg->S = agg->F;
  953. agg->F = agg->S + (u64)agg->budgetmax * agg->inv_w;
  954. }
  955. static void qfq_schedule_agg(struct qfq_sched *, struct qfq_aggregate *);
  956. static int qfq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  957. {
  958. struct qfq_sched *q = qdisc_priv(sch);
  959. struct qfq_class *cl;
  960. struct qfq_aggregate *agg;
  961. int err = 0;
  962. cl = qfq_classify(skb, sch, &err);
  963. if (cl == NULL) {
  964. if (err & __NET_XMIT_BYPASS)
  965. sch->qstats.drops++;
  966. kfree_skb(skb);
  967. return err;
  968. }
  969. pr_debug("qfq_enqueue: cl = %x\n", cl->common.classid);
  970. if (unlikely(cl->agg->lmax < qdisc_pkt_len(skb))) {
  971. pr_debug("qfq: increasing maxpkt from %u to %u for class %u",
  972. cl->agg->lmax, qdisc_pkt_len(skb), cl->common.classid);
  973. err = qfq_change_agg(sch, cl, cl->agg->class_weight,
  974. qdisc_pkt_len(skb));
  975. if (err)
  976. return err;
  977. }
  978. err = qdisc_enqueue(skb, cl->qdisc);
  979. if (unlikely(err != NET_XMIT_SUCCESS)) {
  980. pr_debug("qfq_enqueue: enqueue failed %d\n", err);
  981. if (net_xmit_drop_count(err)) {
  982. cl->qstats.drops++;
  983. sch->qstats.drops++;
  984. }
  985. return err;
  986. }
  987. bstats_update(&cl->bstats, skb);
  988. ++sch->q.qlen;
  989. agg = cl->agg;
  990. /* if the queue was not empty, then done here */
  991. if (cl->qdisc->q.qlen != 1) {
  992. if (unlikely(skb == cl->qdisc->ops->peek(cl->qdisc)) &&
  993. list_first_entry(&agg->active, struct qfq_class, alist)
  994. == cl && cl->deficit < qdisc_pkt_len(skb))
  995. list_move_tail(&cl->alist, &agg->active);
  996. return err;
  997. }
  998. /* schedule class for service within the aggregate */
  999. cl->deficit = agg->lmax;
  1000. list_add_tail(&cl->alist, &agg->active);
  1001. if (list_first_entry(&agg->active, struct qfq_class, alist) != cl)
  1002. return err; /* aggregate was not empty, nothing else to do */
  1003. /* recharge budget */
  1004. agg->initial_budget = agg->budget = agg->budgetmax;
  1005. qfq_update_agg_ts(q, agg, enqueue);
  1006. if (q->in_serv_agg == NULL)
  1007. q->in_serv_agg = agg;
  1008. else if (agg != q->in_serv_agg)
  1009. qfq_schedule_agg(q, agg);
  1010. return err;
  1011. }
  1012. /*
  1013. * Schedule aggregate according to its timestamps.
  1014. */
  1015. static void qfq_schedule_agg(struct qfq_sched *q, struct qfq_aggregate *agg)
  1016. {
  1017. struct qfq_group *grp = agg->grp;
  1018. u64 roundedS;
  1019. int s;
  1020. roundedS = qfq_round_down(agg->S, grp->slot_shift);
  1021. /*
  1022. * Insert agg in the correct bucket.
  1023. * If agg->S >= grp->S we don't need to adjust the
  1024. * bucket list and simply go to the insertion phase.
  1025. * Otherwise grp->S is decreasing, we must make room
  1026. * in the bucket list, and also recompute the group state.
  1027. * Finally, if there were no flows in this group and nobody
  1028. * was in ER make sure to adjust V.
  1029. */
  1030. if (grp->full_slots) {
  1031. if (!qfq_gt(grp->S, agg->S))
  1032. goto skip_update;
  1033. /* create a slot for this agg->S */
  1034. qfq_slot_rotate(grp, roundedS);
  1035. /* group was surely ineligible, remove */
  1036. __clear_bit(grp->index, &q->bitmaps[IR]);
  1037. __clear_bit(grp->index, &q->bitmaps[IB]);
  1038. } else if (!q->bitmaps[ER] && qfq_gt(roundedS, q->V))
  1039. q->V = roundedS;
  1040. grp->S = roundedS;
  1041. grp->F = roundedS + (2ULL << grp->slot_shift);
  1042. s = qfq_calc_state(q, grp);
  1043. __set_bit(grp->index, &q->bitmaps[s]);
  1044. pr_debug("qfq enqueue: new state %d %#lx S %lld F %lld V %lld\n",
  1045. s, q->bitmaps[s],
  1046. (unsigned long long) agg->S,
  1047. (unsigned long long) agg->F,
  1048. (unsigned long long) q->V);
  1049. skip_update:
  1050. qfq_slot_insert(grp, agg, roundedS);
  1051. }
  1052. /* Update agg ts and schedule agg for service */
  1053. static void qfq_activate_agg(struct qfq_sched *q, struct qfq_aggregate *agg,
  1054. enum update_reason reason)
  1055. {
  1056. qfq_update_agg_ts(q, agg, reason);
  1057. qfq_schedule_agg(q, agg);
  1058. }
  1059. static void qfq_slot_remove(struct qfq_sched *q, struct qfq_group *grp,
  1060. struct qfq_aggregate *agg)
  1061. {
  1062. unsigned int i, offset;
  1063. u64 roundedS;
  1064. roundedS = qfq_round_down(agg->S, grp->slot_shift);
  1065. offset = (roundedS - grp->S) >> grp->slot_shift;
  1066. i = (grp->front + offset) % QFQ_MAX_SLOTS;
  1067. hlist_del(&agg->next);
  1068. if (hlist_empty(&grp->slots[i]))
  1069. __clear_bit(offset, &grp->full_slots);
  1070. }
  1071. /*
  1072. * Called to forcibly deschedule an aggregate. If the aggregate is
  1073. * not in the front bucket, or if the latter has other aggregates in
  1074. * the front bucket, we can simply remove the aggregate with no other
  1075. * side effects.
  1076. * Otherwise we must propagate the event up.
  1077. */
  1078. static void qfq_deactivate_agg(struct qfq_sched *q, struct qfq_aggregate *agg)
  1079. {
  1080. struct qfq_group *grp = agg->grp;
  1081. unsigned long mask;
  1082. u64 roundedS;
  1083. int s;
  1084. if (agg == q->in_serv_agg) {
  1085. charge_actual_service(agg);
  1086. q->in_serv_agg = qfq_choose_next_agg(q);
  1087. return;
  1088. }
  1089. agg->F = agg->S;
  1090. qfq_slot_remove(q, grp, agg);
  1091. if (!grp->full_slots) {
  1092. __clear_bit(grp->index, &q->bitmaps[IR]);
  1093. __clear_bit(grp->index, &q->bitmaps[EB]);
  1094. __clear_bit(grp->index, &q->bitmaps[IB]);
  1095. if (test_bit(grp->index, &q->bitmaps[ER]) &&
  1096. !(q->bitmaps[ER] & ~((1UL << grp->index) - 1))) {
  1097. mask = q->bitmaps[ER] & ((1UL << grp->index) - 1);
  1098. if (mask)
  1099. mask = ~((1UL << __fls(mask)) - 1);
  1100. else
  1101. mask = ~0UL;
  1102. qfq_move_groups(q, mask, EB, ER);
  1103. qfq_move_groups(q, mask, IB, IR);
  1104. }
  1105. __clear_bit(grp->index, &q->bitmaps[ER]);
  1106. } else if (hlist_empty(&grp->slots[grp->front])) {
  1107. agg = qfq_slot_scan(grp);
  1108. roundedS = qfq_round_down(agg->S, grp->slot_shift);
  1109. if (grp->S != roundedS) {
  1110. __clear_bit(grp->index, &q->bitmaps[ER]);
  1111. __clear_bit(grp->index, &q->bitmaps[IR]);
  1112. __clear_bit(grp->index, &q->bitmaps[EB]);
  1113. __clear_bit(grp->index, &q->bitmaps[IB]);
  1114. grp->S = roundedS;
  1115. grp->F = roundedS + (2ULL << grp->slot_shift);
  1116. s = qfq_calc_state(q, grp);
  1117. __set_bit(grp->index, &q->bitmaps[s]);
  1118. }
  1119. }
  1120. qfq_update_eligible(q);
  1121. }
  1122. static void qfq_qlen_notify(struct Qdisc *sch, unsigned long arg)
  1123. {
  1124. struct qfq_sched *q = qdisc_priv(sch);
  1125. struct qfq_class *cl = (struct qfq_class *)arg;
  1126. if (cl->qdisc->q.qlen == 0)
  1127. qfq_deactivate_class(q, cl);
  1128. }
  1129. static unsigned int qfq_drop_from_slot(struct qfq_sched *q,
  1130. struct hlist_head *slot)
  1131. {
  1132. struct qfq_aggregate *agg;
  1133. struct qfq_class *cl;
  1134. unsigned int len;
  1135. hlist_for_each_entry(agg, slot, next) {
  1136. list_for_each_entry(cl, &agg->active, alist) {
  1137. if (!cl->qdisc->ops->drop)
  1138. continue;
  1139. len = cl->qdisc->ops->drop(cl->qdisc);
  1140. if (len > 0) {
  1141. if (cl->qdisc->q.qlen == 0)
  1142. qfq_deactivate_class(q, cl);
  1143. return len;
  1144. }
  1145. }
  1146. }
  1147. return 0;
  1148. }
  1149. static unsigned int qfq_drop(struct Qdisc *sch)
  1150. {
  1151. struct qfq_sched *q = qdisc_priv(sch);
  1152. struct qfq_group *grp;
  1153. unsigned int i, j, len;
  1154. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  1155. grp = &q->groups[i];
  1156. for (j = 0; j < QFQ_MAX_SLOTS; j++) {
  1157. len = qfq_drop_from_slot(q, &grp->slots[j]);
  1158. if (len > 0) {
  1159. sch->q.qlen--;
  1160. return len;
  1161. }
  1162. }
  1163. }
  1164. return 0;
  1165. }
  1166. static int qfq_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
  1167. {
  1168. struct qfq_sched *q = qdisc_priv(sch);
  1169. struct qfq_group *grp;
  1170. int i, j, err;
  1171. u32 max_cl_shift, maxbudg_shift, max_classes;
  1172. err = qdisc_class_hash_init(&q->clhash);
  1173. if (err < 0)
  1174. return err;
  1175. if (qdisc_dev(sch)->tx_queue_len + 1 > QFQ_MAX_AGG_CLASSES)
  1176. max_classes = QFQ_MAX_AGG_CLASSES;
  1177. else
  1178. max_classes = qdisc_dev(sch)->tx_queue_len + 1;
  1179. /* max_cl_shift = floor(log_2(max_classes)) */
  1180. max_cl_shift = __fls(max_classes);
  1181. q->max_agg_classes = 1<<max_cl_shift;
  1182. /* maxbudg_shift = log2(max_len * max_classes_per_agg) */
  1183. maxbudg_shift = QFQ_MTU_SHIFT + max_cl_shift;
  1184. q->min_slot_shift = FRAC_BITS + maxbudg_shift - QFQ_MAX_INDEX;
  1185. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  1186. grp = &q->groups[i];
  1187. grp->index = i;
  1188. grp->slot_shift = q->min_slot_shift + i;
  1189. for (j = 0; j < QFQ_MAX_SLOTS; j++)
  1190. INIT_HLIST_HEAD(&grp->slots[j]);
  1191. }
  1192. INIT_HLIST_HEAD(&q->nonfull_aggs);
  1193. return 0;
  1194. }
  1195. static void qfq_reset_qdisc(struct Qdisc *sch)
  1196. {
  1197. struct qfq_sched *q = qdisc_priv(sch);
  1198. struct qfq_class *cl;
  1199. unsigned int i;
  1200. for (i = 0; i < q->clhash.hashsize; i++) {
  1201. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
  1202. if (cl->qdisc->q.qlen > 0)
  1203. qfq_deactivate_class(q, cl);
  1204. qdisc_reset(cl->qdisc);
  1205. }
  1206. }
  1207. sch->q.qlen = 0;
  1208. }
  1209. static void qfq_destroy_qdisc(struct Qdisc *sch)
  1210. {
  1211. struct qfq_sched *q = qdisc_priv(sch);
  1212. struct qfq_class *cl;
  1213. struct hlist_node *next;
  1214. unsigned int i;
  1215. tcf_destroy_chain(&q->filter_list);
  1216. for (i = 0; i < q->clhash.hashsize; i++) {
  1217. hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
  1218. common.hnode) {
  1219. qfq_destroy_class(sch, cl);
  1220. }
  1221. }
  1222. qdisc_class_hash_destroy(&q->clhash);
  1223. }
  1224. static const struct Qdisc_class_ops qfq_class_ops = {
  1225. .change = qfq_change_class,
  1226. .delete = qfq_delete_class,
  1227. .get = qfq_get_class,
  1228. .put = qfq_put_class,
  1229. .tcf_chain = qfq_tcf_chain,
  1230. .bind_tcf = qfq_bind_tcf,
  1231. .unbind_tcf = qfq_unbind_tcf,
  1232. .graft = qfq_graft_class,
  1233. .leaf = qfq_class_leaf,
  1234. .qlen_notify = qfq_qlen_notify,
  1235. .dump = qfq_dump_class,
  1236. .dump_stats = qfq_dump_class_stats,
  1237. .walk = qfq_walk,
  1238. };
  1239. static struct Qdisc_ops qfq_qdisc_ops __read_mostly = {
  1240. .cl_ops = &qfq_class_ops,
  1241. .id = "qfq",
  1242. .priv_size = sizeof(struct qfq_sched),
  1243. .enqueue = qfq_enqueue,
  1244. .dequeue = qfq_dequeue,
  1245. .peek = qdisc_peek_dequeued,
  1246. .drop = qfq_drop,
  1247. .init = qfq_init_qdisc,
  1248. .reset = qfq_reset_qdisc,
  1249. .destroy = qfq_destroy_qdisc,
  1250. .owner = THIS_MODULE,
  1251. };
  1252. static int __init qfq_init(void)
  1253. {
  1254. return register_qdisc(&qfq_qdisc_ops);
  1255. }
  1256. static void __exit qfq_exit(void)
  1257. {
  1258. unregister_qdisc(&qfq_qdisc_ops);
  1259. }
  1260. module_init(qfq_init);
  1261. module_exit(qfq_exit);
  1262. MODULE_LICENSE("GPL");