sch_qfq.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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. struct hlist_node *n;
  220. hlist_for_each_entry(agg, n, &q->nonfull_aggs, nonfull_next)
  221. if (agg->lmax == lmax && agg->class_weight == weight)
  222. return agg;
  223. return NULL;
  224. }
  225. /* Update aggregate as a function of the new number of classes. */
  226. static void qfq_update_agg(struct qfq_sched *q, struct qfq_aggregate *agg,
  227. int new_num_classes)
  228. {
  229. u32 new_agg_weight;
  230. if (new_num_classes == q->max_agg_classes)
  231. hlist_del_init(&agg->nonfull_next);
  232. if (agg->num_classes > new_num_classes &&
  233. new_num_classes == q->max_agg_classes - 1) /* agg no more full */
  234. hlist_add_head(&agg->nonfull_next, &q->nonfull_aggs);
  235. agg->budgetmax = new_num_classes * agg->lmax;
  236. new_agg_weight = agg->class_weight * new_num_classes;
  237. agg->inv_w = ONE_FP/new_agg_weight;
  238. if (agg->grp == NULL) {
  239. int i = qfq_calc_index(agg->inv_w, agg->budgetmax,
  240. q->min_slot_shift);
  241. agg->grp = &q->groups[i];
  242. }
  243. q->wsum +=
  244. (int) agg->class_weight * (new_num_classes - agg->num_classes);
  245. agg->num_classes = new_num_classes;
  246. }
  247. /* Add class to aggregate. */
  248. static void qfq_add_to_agg(struct qfq_sched *q,
  249. struct qfq_aggregate *agg,
  250. struct qfq_class *cl)
  251. {
  252. cl->agg = agg;
  253. qfq_update_agg(q, agg, agg->num_classes+1);
  254. if (cl->qdisc->q.qlen > 0) { /* adding an active class */
  255. list_add_tail(&cl->alist, &agg->active);
  256. if (list_first_entry(&agg->active, struct qfq_class, alist) ==
  257. cl && q->in_serv_agg != agg) /* agg was inactive */
  258. qfq_activate_agg(q, agg, enqueue); /* schedule agg */
  259. }
  260. }
  261. static struct qfq_aggregate *qfq_choose_next_agg(struct qfq_sched *);
  262. static void qfq_destroy_agg(struct qfq_sched *q, struct qfq_aggregate *agg)
  263. {
  264. if (!hlist_unhashed(&agg->nonfull_next))
  265. hlist_del_init(&agg->nonfull_next);
  266. if (q->in_serv_agg == agg)
  267. q->in_serv_agg = qfq_choose_next_agg(q);
  268. kfree(agg);
  269. }
  270. /* Deschedule class from within its parent aggregate. */
  271. static void qfq_deactivate_class(struct qfq_sched *q, struct qfq_class *cl)
  272. {
  273. struct qfq_aggregate *agg = cl->agg;
  274. list_del(&cl->alist); /* remove from RR queue of the aggregate */
  275. if (list_empty(&agg->active)) /* agg is now inactive */
  276. qfq_deactivate_agg(q, agg);
  277. }
  278. /* Remove class from its parent aggregate. */
  279. static void qfq_rm_from_agg(struct qfq_sched *q, struct qfq_class *cl)
  280. {
  281. struct qfq_aggregate *agg = cl->agg;
  282. cl->agg = NULL;
  283. if (agg->num_classes == 1) { /* agg being emptied, destroy it */
  284. qfq_destroy_agg(q, agg);
  285. return;
  286. }
  287. qfq_update_agg(q, agg, agg->num_classes-1);
  288. }
  289. /* Deschedule class and remove it from its parent aggregate. */
  290. static void qfq_deact_rm_from_agg(struct qfq_sched *q, struct qfq_class *cl)
  291. {
  292. if (cl->qdisc->q.qlen > 0) /* class is active */
  293. qfq_deactivate_class(q, cl);
  294. qfq_rm_from_agg(q, cl);
  295. }
  296. /* Move class to a new aggregate, matching the new class weight and/or lmax */
  297. static int qfq_change_agg(struct Qdisc *sch, struct qfq_class *cl, u32 weight,
  298. u32 lmax)
  299. {
  300. struct qfq_sched *q = qdisc_priv(sch);
  301. struct qfq_aggregate *new_agg = qfq_find_agg(q, lmax, weight);
  302. if (new_agg == NULL) { /* create new aggregate */
  303. new_agg = kzalloc(sizeof(*new_agg), GFP_ATOMIC);
  304. if (new_agg == NULL)
  305. return -ENOBUFS;
  306. qfq_init_agg(q, new_agg, lmax, weight);
  307. }
  308. qfq_deact_rm_from_agg(q, cl);
  309. qfq_add_to_agg(q, new_agg, cl);
  310. return 0;
  311. }
  312. static int qfq_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
  313. struct nlattr **tca, unsigned long *arg)
  314. {
  315. struct qfq_sched *q = qdisc_priv(sch);
  316. struct qfq_class *cl = (struct qfq_class *)*arg;
  317. bool existing = false;
  318. struct nlattr *tb[TCA_QFQ_MAX + 1];
  319. struct qfq_aggregate *new_agg = NULL;
  320. u32 weight, lmax, inv_w;
  321. int err;
  322. int delta_w;
  323. if (tca[TCA_OPTIONS] == NULL) {
  324. pr_notice("qfq: no options\n");
  325. return -EINVAL;
  326. }
  327. err = nla_parse_nested(tb, TCA_QFQ_MAX, tca[TCA_OPTIONS], qfq_policy);
  328. if (err < 0)
  329. return err;
  330. if (tb[TCA_QFQ_WEIGHT]) {
  331. weight = nla_get_u32(tb[TCA_QFQ_WEIGHT]);
  332. if (!weight || weight > (1UL << QFQ_MAX_WSHIFT)) {
  333. pr_notice("qfq: invalid weight %u\n", weight);
  334. return -EINVAL;
  335. }
  336. } else
  337. weight = 1;
  338. if (tb[TCA_QFQ_LMAX]) {
  339. lmax = nla_get_u32(tb[TCA_QFQ_LMAX]);
  340. if (lmax < QFQ_MIN_LMAX || lmax > (1UL << QFQ_MTU_SHIFT)) {
  341. pr_notice("qfq: invalid max length %u\n", lmax);
  342. return -EINVAL;
  343. }
  344. } else
  345. lmax = psched_mtu(qdisc_dev(sch));
  346. inv_w = ONE_FP / weight;
  347. weight = ONE_FP / inv_w;
  348. if (cl != NULL &&
  349. lmax == cl->agg->lmax &&
  350. weight == cl->agg->class_weight)
  351. return 0; /* nothing to change */
  352. delta_w = weight - (cl ? cl->agg->class_weight : 0);
  353. if (q->wsum + delta_w > QFQ_MAX_WSUM) {
  354. pr_notice("qfq: total weight out of range (%d + %u)\n",
  355. delta_w, q->wsum);
  356. return -EINVAL;
  357. }
  358. if (cl != NULL) { /* modify existing class */
  359. if (tca[TCA_RATE]) {
  360. err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
  361. qdisc_root_sleeping_lock(sch),
  362. tca[TCA_RATE]);
  363. if (err)
  364. return err;
  365. }
  366. existing = true;
  367. goto set_change_agg;
  368. }
  369. /* create and init new class */
  370. cl = kzalloc(sizeof(struct qfq_class), GFP_KERNEL);
  371. if (cl == NULL)
  372. return -ENOBUFS;
  373. cl->refcnt = 1;
  374. cl->common.classid = classid;
  375. cl->deficit = lmax;
  376. cl->qdisc = qdisc_create_dflt(sch->dev_queue,
  377. &pfifo_qdisc_ops, classid);
  378. if (cl->qdisc == NULL)
  379. cl->qdisc = &noop_qdisc;
  380. if (tca[TCA_RATE]) {
  381. err = gen_new_estimator(&cl->bstats, &cl->rate_est,
  382. qdisc_root_sleeping_lock(sch),
  383. tca[TCA_RATE]);
  384. if (err)
  385. goto destroy_class;
  386. }
  387. sch_tree_lock(sch);
  388. qdisc_class_hash_insert(&q->clhash, &cl->common);
  389. sch_tree_unlock(sch);
  390. qdisc_class_hash_grow(sch, &q->clhash);
  391. set_change_agg:
  392. sch_tree_lock(sch);
  393. new_agg = qfq_find_agg(q, lmax, weight);
  394. if (new_agg == NULL) { /* create new aggregate */
  395. sch_tree_unlock(sch);
  396. new_agg = kzalloc(sizeof(*new_agg), GFP_KERNEL);
  397. if (new_agg == NULL) {
  398. err = -ENOBUFS;
  399. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  400. goto destroy_class;
  401. }
  402. sch_tree_lock(sch);
  403. qfq_init_agg(q, new_agg, lmax, weight);
  404. }
  405. if (existing)
  406. qfq_deact_rm_from_agg(q, cl);
  407. qfq_add_to_agg(q, new_agg, cl);
  408. sch_tree_unlock(sch);
  409. *arg = (unsigned long)cl;
  410. return 0;
  411. destroy_class:
  412. qdisc_destroy(cl->qdisc);
  413. kfree(cl);
  414. return err;
  415. }
  416. static void qfq_destroy_class(struct Qdisc *sch, struct qfq_class *cl)
  417. {
  418. struct qfq_sched *q = qdisc_priv(sch);
  419. qfq_rm_from_agg(q, cl);
  420. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  421. qdisc_destroy(cl->qdisc);
  422. kfree(cl);
  423. }
  424. static int qfq_delete_class(struct Qdisc *sch, unsigned long arg)
  425. {
  426. struct qfq_sched *q = qdisc_priv(sch);
  427. struct qfq_class *cl = (struct qfq_class *)arg;
  428. if (cl->filter_cnt > 0)
  429. return -EBUSY;
  430. sch_tree_lock(sch);
  431. qfq_purge_queue(cl);
  432. qdisc_class_hash_remove(&q->clhash, &cl->common);
  433. BUG_ON(--cl->refcnt == 0);
  434. /*
  435. * This shouldn't happen: we "hold" one cops->get() when called
  436. * from tc_ctl_tclass; the destroy method is done from cops->put().
  437. */
  438. sch_tree_unlock(sch);
  439. return 0;
  440. }
  441. static unsigned long qfq_get_class(struct Qdisc *sch, u32 classid)
  442. {
  443. struct qfq_class *cl = qfq_find_class(sch, classid);
  444. if (cl != NULL)
  445. cl->refcnt++;
  446. return (unsigned long)cl;
  447. }
  448. static void qfq_put_class(struct Qdisc *sch, unsigned long arg)
  449. {
  450. struct qfq_class *cl = (struct qfq_class *)arg;
  451. if (--cl->refcnt == 0)
  452. qfq_destroy_class(sch, cl);
  453. }
  454. static struct tcf_proto **qfq_tcf_chain(struct Qdisc *sch, unsigned long cl)
  455. {
  456. struct qfq_sched *q = qdisc_priv(sch);
  457. if (cl)
  458. return NULL;
  459. return &q->filter_list;
  460. }
  461. static unsigned long qfq_bind_tcf(struct Qdisc *sch, unsigned long parent,
  462. u32 classid)
  463. {
  464. struct qfq_class *cl = qfq_find_class(sch, classid);
  465. if (cl != NULL)
  466. cl->filter_cnt++;
  467. return (unsigned long)cl;
  468. }
  469. static void qfq_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  470. {
  471. struct qfq_class *cl = (struct qfq_class *)arg;
  472. cl->filter_cnt--;
  473. }
  474. static int qfq_graft_class(struct Qdisc *sch, unsigned long arg,
  475. struct Qdisc *new, struct Qdisc **old)
  476. {
  477. struct qfq_class *cl = (struct qfq_class *)arg;
  478. if (new == NULL) {
  479. new = qdisc_create_dflt(sch->dev_queue,
  480. &pfifo_qdisc_ops, cl->common.classid);
  481. if (new == NULL)
  482. new = &noop_qdisc;
  483. }
  484. sch_tree_lock(sch);
  485. qfq_purge_queue(cl);
  486. *old = cl->qdisc;
  487. cl->qdisc = new;
  488. sch_tree_unlock(sch);
  489. return 0;
  490. }
  491. static struct Qdisc *qfq_class_leaf(struct Qdisc *sch, unsigned long arg)
  492. {
  493. struct qfq_class *cl = (struct qfq_class *)arg;
  494. return cl->qdisc;
  495. }
  496. static int qfq_dump_class(struct Qdisc *sch, unsigned long arg,
  497. struct sk_buff *skb, struct tcmsg *tcm)
  498. {
  499. struct qfq_class *cl = (struct qfq_class *)arg;
  500. struct nlattr *nest;
  501. tcm->tcm_parent = TC_H_ROOT;
  502. tcm->tcm_handle = cl->common.classid;
  503. tcm->tcm_info = cl->qdisc->handle;
  504. nest = nla_nest_start(skb, TCA_OPTIONS);
  505. if (nest == NULL)
  506. goto nla_put_failure;
  507. if (nla_put_u32(skb, TCA_QFQ_WEIGHT, cl->agg->class_weight) ||
  508. nla_put_u32(skb, TCA_QFQ_LMAX, cl->agg->lmax))
  509. goto nla_put_failure;
  510. return nla_nest_end(skb, nest);
  511. nla_put_failure:
  512. nla_nest_cancel(skb, nest);
  513. return -EMSGSIZE;
  514. }
  515. static int qfq_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  516. struct gnet_dump *d)
  517. {
  518. struct qfq_class *cl = (struct qfq_class *)arg;
  519. struct tc_qfq_stats xstats;
  520. memset(&xstats, 0, sizeof(xstats));
  521. cl->qdisc->qstats.qlen = cl->qdisc->q.qlen;
  522. xstats.weight = cl->agg->class_weight;
  523. xstats.lmax = cl->agg->lmax;
  524. if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
  525. gnet_stats_copy_rate_est(d, &cl->bstats, &cl->rate_est) < 0 ||
  526. gnet_stats_copy_queue(d, &cl->qdisc->qstats) < 0)
  527. return -1;
  528. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  529. }
  530. static void qfq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  531. {
  532. struct qfq_sched *q = qdisc_priv(sch);
  533. struct qfq_class *cl;
  534. struct hlist_node *n;
  535. unsigned int i;
  536. if (arg->stop)
  537. return;
  538. for (i = 0; i < q->clhash.hashsize; i++) {
  539. hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
  540. if (arg->count < arg->skip) {
  541. arg->count++;
  542. continue;
  543. }
  544. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  545. arg->stop = 1;
  546. return;
  547. }
  548. arg->count++;
  549. }
  550. }
  551. }
  552. static struct qfq_class *qfq_classify(struct sk_buff *skb, struct Qdisc *sch,
  553. int *qerr)
  554. {
  555. struct qfq_sched *q = qdisc_priv(sch);
  556. struct qfq_class *cl;
  557. struct tcf_result res;
  558. int result;
  559. if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
  560. pr_debug("qfq_classify: found %d\n", skb->priority);
  561. cl = qfq_find_class(sch, skb->priority);
  562. if (cl != NULL)
  563. return cl;
  564. }
  565. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  566. result = tc_classify(skb, q->filter_list, &res);
  567. if (result >= 0) {
  568. #ifdef CONFIG_NET_CLS_ACT
  569. switch (result) {
  570. case TC_ACT_QUEUED:
  571. case TC_ACT_STOLEN:
  572. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  573. case TC_ACT_SHOT:
  574. return NULL;
  575. }
  576. #endif
  577. cl = (struct qfq_class *)res.class;
  578. if (cl == NULL)
  579. cl = qfq_find_class(sch, res.classid);
  580. return cl;
  581. }
  582. return NULL;
  583. }
  584. /* Generic comparison function, handling wraparound. */
  585. static inline int qfq_gt(u64 a, u64 b)
  586. {
  587. return (s64)(a - b) > 0;
  588. }
  589. /* Round a precise timestamp to its slotted value. */
  590. static inline u64 qfq_round_down(u64 ts, unsigned int shift)
  591. {
  592. return ts & ~((1ULL << shift) - 1);
  593. }
  594. /* return the pointer to the group with lowest index in the bitmap */
  595. static inline struct qfq_group *qfq_ffs(struct qfq_sched *q,
  596. unsigned long bitmap)
  597. {
  598. int index = __ffs(bitmap);
  599. return &q->groups[index];
  600. }
  601. /* Calculate a mask to mimic what would be ffs_from(). */
  602. static inline unsigned long mask_from(unsigned long bitmap, int from)
  603. {
  604. return bitmap & ~((1UL << from) - 1);
  605. }
  606. /*
  607. * The state computation relies on ER=0, IR=1, EB=2, IB=3
  608. * First compute eligibility comparing grp->S, q->V,
  609. * then check if someone is blocking us and possibly add EB
  610. */
  611. static int qfq_calc_state(struct qfq_sched *q, const struct qfq_group *grp)
  612. {
  613. /* if S > V we are not eligible */
  614. unsigned int state = qfq_gt(grp->S, q->V);
  615. unsigned long mask = mask_from(q->bitmaps[ER], grp->index);
  616. struct qfq_group *next;
  617. if (mask) {
  618. next = qfq_ffs(q, mask);
  619. if (qfq_gt(grp->F, next->F))
  620. state |= EB;
  621. }
  622. return state;
  623. }
  624. /*
  625. * In principle
  626. * q->bitmaps[dst] |= q->bitmaps[src] & mask;
  627. * q->bitmaps[src] &= ~mask;
  628. * but we should make sure that src != dst
  629. */
  630. static inline void qfq_move_groups(struct qfq_sched *q, unsigned long mask,
  631. int src, int dst)
  632. {
  633. q->bitmaps[dst] |= q->bitmaps[src] & mask;
  634. q->bitmaps[src] &= ~mask;
  635. }
  636. static void qfq_unblock_groups(struct qfq_sched *q, int index, u64 old_F)
  637. {
  638. unsigned long mask = mask_from(q->bitmaps[ER], index + 1);
  639. struct qfq_group *next;
  640. if (mask) {
  641. next = qfq_ffs(q, mask);
  642. if (!qfq_gt(next->F, old_F))
  643. return;
  644. }
  645. mask = (1UL << index) - 1;
  646. qfq_move_groups(q, mask, EB, ER);
  647. qfq_move_groups(q, mask, IB, IR);
  648. }
  649. /*
  650. * perhaps
  651. *
  652. old_V ^= q->V;
  653. old_V >>= q->min_slot_shift;
  654. if (old_V) {
  655. ...
  656. }
  657. *
  658. */
  659. static void qfq_make_eligible(struct qfq_sched *q)
  660. {
  661. unsigned long vslot = q->V >> q->min_slot_shift;
  662. unsigned long old_vslot = q->oldV >> q->min_slot_shift;
  663. if (vslot != old_vslot) {
  664. unsigned long mask = (1UL << fls(vslot ^ old_vslot)) - 1;
  665. qfq_move_groups(q, mask, IR, ER);
  666. qfq_move_groups(q, mask, IB, EB);
  667. }
  668. }
  669. /*
  670. * The index of the slot in which the aggregate is to be inserted must
  671. * not be higher than QFQ_MAX_SLOTS-2. There is a '-2' and not a '-1'
  672. * because the start time of the group may be moved backward by one
  673. * slot after the aggregate has been inserted, and this would cause
  674. * non-empty slots to be right-shifted by one position.
  675. *
  676. * If the weight and lmax (max_pkt_size) of the classes do not change,
  677. * then QFQ+ does meet the above contraint according to the current
  678. * values of its parameters. In fact, if the weight and lmax of the
  679. * classes do not change, then, from the theory, QFQ+ guarantees that
  680. * the slot index is never higher than
  681. * 2 + QFQ_MAX_AGG_CLASSES * ((1<<QFQ_MTU_SHIFT)/QFQ_MIN_LMAX) *
  682. * (QFQ_MAX_WEIGHT/QFQ_MAX_WSUM) = 2 + 8 * 128 * (1 / 64) = 18
  683. *
  684. * When the weight of a class is increased or the lmax of the class is
  685. * decreased, a new aggregate with smaller slot size than the original
  686. * parent aggregate of the class may happen to be activated. The
  687. * activation of this aggregate should be properly delayed to when the
  688. * service of the class has finished in the ideal system tracked by
  689. * QFQ+. If the activation of the aggregate is not delayed to this
  690. * reference time instant, then this aggregate may be unjustly served
  691. * before other aggregates waiting for service. This may cause the
  692. * above bound to the slot index to be violated for some of these
  693. * unlucky aggregates.
  694. *
  695. * Instead of delaying the activation of the new aggregate, which is
  696. * quite complex, the following inaccurate but simple solution is used:
  697. * if the slot index is higher than QFQ_MAX_SLOTS-2, then the
  698. * timestamps of the aggregate are shifted backward so as to let the
  699. * slot index become equal to QFQ_MAX_SLOTS-2.
  700. */
  701. static void qfq_slot_insert(struct qfq_group *grp, struct qfq_aggregate *agg,
  702. u64 roundedS)
  703. {
  704. u64 slot = (roundedS - grp->S) >> grp->slot_shift;
  705. unsigned int i; /* slot index in the bucket list */
  706. if (unlikely(slot > QFQ_MAX_SLOTS - 2)) {
  707. u64 deltaS = roundedS - grp->S -
  708. ((u64)(QFQ_MAX_SLOTS - 2)<<grp->slot_shift);
  709. agg->S -= deltaS;
  710. agg->F -= deltaS;
  711. slot = QFQ_MAX_SLOTS - 2;
  712. }
  713. i = (grp->front + slot) % QFQ_MAX_SLOTS;
  714. hlist_add_head(&agg->next, &grp->slots[i]);
  715. __set_bit(slot, &grp->full_slots);
  716. }
  717. /* Maybe introduce hlist_first_entry?? */
  718. static struct qfq_aggregate *qfq_slot_head(struct qfq_group *grp)
  719. {
  720. return hlist_entry(grp->slots[grp->front].first,
  721. struct qfq_aggregate, next);
  722. }
  723. /*
  724. * remove the entry from the slot
  725. */
  726. static void qfq_front_slot_remove(struct qfq_group *grp)
  727. {
  728. struct qfq_aggregate *agg = qfq_slot_head(grp);
  729. BUG_ON(!agg);
  730. hlist_del(&agg->next);
  731. if (hlist_empty(&grp->slots[grp->front]))
  732. __clear_bit(0, &grp->full_slots);
  733. }
  734. /*
  735. * Returns the first aggregate in the first non-empty bucket of the
  736. * group. As a side effect, adjusts the bucket list so the first
  737. * non-empty bucket is at position 0 in full_slots.
  738. */
  739. static struct qfq_aggregate *qfq_slot_scan(struct qfq_group *grp)
  740. {
  741. unsigned int i;
  742. pr_debug("qfq slot_scan: grp %u full %#lx\n",
  743. grp->index, grp->full_slots);
  744. if (grp->full_slots == 0)
  745. return NULL;
  746. i = __ffs(grp->full_slots); /* zero based */
  747. if (i > 0) {
  748. grp->front = (grp->front + i) % QFQ_MAX_SLOTS;
  749. grp->full_slots >>= i;
  750. }
  751. return qfq_slot_head(grp);
  752. }
  753. /*
  754. * adjust the bucket list. When the start time of a group decreases,
  755. * we move the index down (modulo QFQ_MAX_SLOTS) so we don't need to
  756. * move the objects. The mask of occupied slots must be shifted
  757. * because we use ffs() to find the first non-empty slot.
  758. * This covers decreases in the group's start time, but what about
  759. * increases of the start time ?
  760. * Here too we should make sure that i is less than 32
  761. */
  762. static void qfq_slot_rotate(struct qfq_group *grp, u64 roundedS)
  763. {
  764. unsigned int i = (grp->S - roundedS) >> grp->slot_shift;
  765. grp->full_slots <<= i;
  766. grp->front = (grp->front - i) % QFQ_MAX_SLOTS;
  767. }
  768. static void qfq_update_eligible(struct qfq_sched *q)
  769. {
  770. struct qfq_group *grp;
  771. unsigned long ineligible;
  772. ineligible = q->bitmaps[IR] | q->bitmaps[IB];
  773. if (ineligible) {
  774. if (!q->bitmaps[ER]) {
  775. grp = qfq_ffs(q, ineligible);
  776. if (qfq_gt(grp->S, q->V))
  777. q->V = grp->S;
  778. }
  779. qfq_make_eligible(q);
  780. }
  781. }
  782. /* Dequeue head packet of the head class in the DRR queue of the aggregate. */
  783. static void agg_dequeue(struct qfq_aggregate *agg,
  784. struct qfq_class *cl, unsigned int len)
  785. {
  786. qdisc_dequeue_peeked(cl->qdisc);
  787. cl->deficit -= (int) len;
  788. if (cl->qdisc->q.qlen == 0) /* no more packets, remove from list */
  789. list_del(&cl->alist);
  790. else if (cl->deficit < qdisc_pkt_len(cl->qdisc->ops->peek(cl->qdisc))) {
  791. cl->deficit += agg->lmax;
  792. list_move_tail(&cl->alist, &agg->active);
  793. }
  794. }
  795. static inline struct sk_buff *qfq_peek_skb(struct qfq_aggregate *agg,
  796. struct qfq_class **cl,
  797. unsigned int *len)
  798. {
  799. struct sk_buff *skb;
  800. *cl = list_first_entry(&agg->active, struct qfq_class, alist);
  801. skb = (*cl)->qdisc->ops->peek((*cl)->qdisc);
  802. if (skb == NULL)
  803. WARN_ONCE(1, "qfq_dequeue: non-workconserving leaf\n");
  804. else
  805. *len = qdisc_pkt_len(skb);
  806. return skb;
  807. }
  808. /* Update F according to the actual service received by the aggregate. */
  809. static inline void charge_actual_service(struct qfq_aggregate *agg)
  810. {
  811. /* compute the service received by the aggregate */
  812. u32 service_received = agg->initial_budget - agg->budget;
  813. agg->F = agg->S + (u64)service_received * agg->inv_w;
  814. }
  815. static struct sk_buff *qfq_dequeue(struct Qdisc *sch)
  816. {
  817. struct qfq_sched *q = qdisc_priv(sch);
  818. struct qfq_aggregate *in_serv_agg = q->in_serv_agg;
  819. struct qfq_class *cl;
  820. struct sk_buff *skb = NULL;
  821. /* next-packet len, 0 means no more active classes in in-service agg */
  822. unsigned int len = 0;
  823. if (in_serv_agg == NULL)
  824. return NULL;
  825. if (!list_empty(&in_serv_agg->active))
  826. skb = qfq_peek_skb(in_serv_agg, &cl, &len);
  827. /*
  828. * If there are no active classes in the in-service aggregate,
  829. * or if the aggregate has not enough budget to serve its next
  830. * class, then choose the next aggregate to serve.
  831. */
  832. if (len == 0 || in_serv_agg->budget < len) {
  833. charge_actual_service(in_serv_agg);
  834. /* recharge the budget of the aggregate */
  835. in_serv_agg->initial_budget = in_serv_agg->budget =
  836. in_serv_agg->budgetmax;
  837. if (!list_empty(&in_serv_agg->active))
  838. /*
  839. * Still active: reschedule for
  840. * service. Possible optimization: if no other
  841. * aggregate is active, then there is no point
  842. * in rescheduling this aggregate, and we can
  843. * just keep it as the in-service one. This
  844. * should be however a corner case, and to
  845. * handle it, we would need to maintain an
  846. * extra num_active_aggs field.
  847. */
  848. qfq_activate_agg(q, in_serv_agg, requeue);
  849. else if (sch->q.qlen == 0) { /* no aggregate to serve */
  850. q->in_serv_agg = NULL;
  851. return NULL;
  852. }
  853. /*
  854. * If we get here, there are other aggregates queued:
  855. * choose the new aggregate to serve.
  856. */
  857. in_serv_agg = q->in_serv_agg = qfq_choose_next_agg(q);
  858. skb = qfq_peek_skb(in_serv_agg, &cl, &len);
  859. }
  860. if (!skb)
  861. return NULL;
  862. sch->q.qlen--;
  863. qdisc_bstats_update(sch, skb);
  864. agg_dequeue(in_serv_agg, cl, len);
  865. in_serv_agg->budget -= len;
  866. q->V += (u64)len * IWSUM;
  867. pr_debug("qfq dequeue: len %u F %lld now %lld\n",
  868. len, (unsigned long long) in_serv_agg->F,
  869. (unsigned long long) q->V);
  870. return skb;
  871. }
  872. static struct qfq_aggregate *qfq_choose_next_agg(struct qfq_sched *q)
  873. {
  874. struct qfq_group *grp;
  875. struct qfq_aggregate *agg, *new_front_agg;
  876. u64 old_F;
  877. qfq_update_eligible(q);
  878. q->oldV = q->V;
  879. if (!q->bitmaps[ER])
  880. return NULL;
  881. grp = qfq_ffs(q, q->bitmaps[ER]);
  882. old_F = grp->F;
  883. agg = qfq_slot_head(grp);
  884. /* agg starts to be served, remove it from schedule */
  885. qfq_front_slot_remove(grp);
  886. new_front_agg = qfq_slot_scan(grp);
  887. if (new_front_agg == NULL) /* group is now inactive, remove from ER */
  888. __clear_bit(grp->index, &q->bitmaps[ER]);
  889. else {
  890. u64 roundedS = qfq_round_down(new_front_agg->S,
  891. grp->slot_shift);
  892. unsigned int s;
  893. if (grp->S == roundedS)
  894. return agg;
  895. grp->S = roundedS;
  896. grp->F = roundedS + (2ULL << grp->slot_shift);
  897. __clear_bit(grp->index, &q->bitmaps[ER]);
  898. s = qfq_calc_state(q, grp);
  899. __set_bit(grp->index, &q->bitmaps[s]);
  900. }
  901. qfq_unblock_groups(q, grp->index, old_F);
  902. return agg;
  903. }
  904. /*
  905. * Assign a reasonable start time for a new aggregate in group i.
  906. * Admissible values for \hat(F) are multiples of \sigma_i
  907. * no greater than V+\sigma_i . Larger values mean that
  908. * we had a wraparound so we consider the timestamp to be stale.
  909. *
  910. * If F is not stale and F >= V then we set S = F.
  911. * Otherwise we should assign S = V, but this may violate
  912. * the ordering in EB (see [2]). So, if we have groups in ER,
  913. * set S to the F_j of the first group j which would be blocking us.
  914. * We are guaranteed not to move S backward because
  915. * otherwise our group i would still be blocked.
  916. */
  917. static void qfq_update_start(struct qfq_sched *q, struct qfq_aggregate *agg)
  918. {
  919. unsigned long mask;
  920. u64 limit, roundedF;
  921. int slot_shift = agg->grp->slot_shift;
  922. roundedF = qfq_round_down(agg->F, slot_shift);
  923. limit = qfq_round_down(q->V, slot_shift) + (1ULL << slot_shift);
  924. if (!qfq_gt(agg->F, q->V) || qfq_gt(roundedF, limit)) {
  925. /* timestamp was stale */
  926. mask = mask_from(q->bitmaps[ER], agg->grp->index);
  927. if (mask) {
  928. struct qfq_group *next = qfq_ffs(q, mask);
  929. if (qfq_gt(roundedF, next->F)) {
  930. if (qfq_gt(limit, next->F))
  931. agg->S = next->F;
  932. else /* preserve timestamp correctness */
  933. agg->S = limit;
  934. return;
  935. }
  936. }
  937. agg->S = q->V;
  938. } else /* timestamp is not stale */
  939. agg->S = agg->F;
  940. }
  941. /*
  942. * Update the timestamps of agg before scheduling/rescheduling it for
  943. * service. In particular, assign to agg->F its maximum possible
  944. * value, i.e., the virtual finish time with which the aggregate
  945. * should be labeled if it used all its budget once in service.
  946. */
  947. static inline void
  948. qfq_update_agg_ts(struct qfq_sched *q,
  949. struct qfq_aggregate *agg, enum update_reason reason)
  950. {
  951. if (reason != requeue)
  952. qfq_update_start(q, agg);
  953. else /* just charge agg for the service received */
  954. agg->S = agg->F;
  955. agg->F = agg->S + (u64)agg->budgetmax * agg->inv_w;
  956. }
  957. static void qfq_schedule_agg(struct qfq_sched *, struct qfq_aggregate *);
  958. static int qfq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  959. {
  960. struct qfq_sched *q = qdisc_priv(sch);
  961. struct qfq_class *cl;
  962. struct qfq_aggregate *agg;
  963. int err = 0;
  964. cl = qfq_classify(skb, sch, &err);
  965. if (cl == NULL) {
  966. if (err & __NET_XMIT_BYPASS)
  967. sch->qstats.drops++;
  968. kfree_skb(skb);
  969. return err;
  970. }
  971. pr_debug("qfq_enqueue: cl = %x\n", cl->common.classid);
  972. if (unlikely(cl->agg->lmax < qdisc_pkt_len(skb))) {
  973. pr_debug("qfq: increasing maxpkt from %u to %u for class %u",
  974. cl->agg->lmax, qdisc_pkt_len(skb), cl->common.classid);
  975. err = qfq_change_agg(sch, cl, cl->agg->class_weight,
  976. qdisc_pkt_len(skb));
  977. if (err)
  978. return err;
  979. }
  980. err = qdisc_enqueue(skb, cl->qdisc);
  981. if (unlikely(err != NET_XMIT_SUCCESS)) {
  982. pr_debug("qfq_enqueue: enqueue failed %d\n", err);
  983. if (net_xmit_drop_count(err)) {
  984. cl->qstats.drops++;
  985. sch->qstats.drops++;
  986. }
  987. return err;
  988. }
  989. bstats_update(&cl->bstats, skb);
  990. ++sch->q.qlen;
  991. agg = cl->agg;
  992. /* if the queue was not empty, then done here */
  993. if (cl->qdisc->q.qlen != 1) {
  994. if (unlikely(skb == cl->qdisc->ops->peek(cl->qdisc)) &&
  995. list_first_entry(&agg->active, struct qfq_class, alist)
  996. == cl && cl->deficit < qdisc_pkt_len(skb))
  997. list_move_tail(&cl->alist, &agg->active);
  998. return err;
  999. }
  1000. /* schedule class for service within the aggregate */
  1001. cl->deficit = agg->lmax;
  1002. list_add_tail(&cl->alist, &agg->active);
  1003. if (list_first_entry(&agg->active, struct qfq_class, alist) != cl)
  1004. return err; /* aggregate was not empty, nothing else to do */
  1005. /* recharge budget */
  1006. agg->initial_budget = agg->budget = agg->budgetmax;
  1007. qfq_update_agg_ts(q, agg, enqueue);
  1008. if (q->in_serv_agg == NULL)
  1009. q->in_serv_agg = agg;
  1010. else if (agg != q->in_serv_agg)
  1011. qfq_schedule_agg(q, agg);
  1012. return err;
  1013. }
  1014. /*
  1015. * Schedule aggregate according to its timestamps.
  1016. */
  1017. static void qfq_schedule_agg(struct qfq_sched *q, struct qfq_aggregate *agg)
  1018. {
  1019. struct qfq_group *grp = agg->grp;
  1020. u64 roundedS;
  1021. int s;
  1022. roundedS = qfq_round_down(agg->S, grp->slot_shift);
  1023. /*
  1024. * Insert agg in the correct bucket.
  1025. * If agg->S >= grp->S we don't need to adjust the
  1026. * bucket list and simply go to the insertion phase.
  1027. * Otherwise grp->S is decreasing, we must make room
  1028. * in the bucket list, and also recompute the group state.
  1029. * Finally, if there were no flows in this group and nobody
  1030. * was in ER make sure to adjust V.
  1031. */
  1032. if (grp->full_slots) {
  1033. if (!qfq_gt(grp->S, agg->S))
  1034. goto skip_update;
  1035. /* create a slot for this agg->S */
  1036. qfq_slot_rotate(grp, roundedS);
  1037. /* group was surely ineligible, remove */
  1038. __clear_bit(grp->index, &q->bitmaps[IR]);
  1039. __clear_bit(grp->index, &q->bitmaps[IB]);
  1040. } else if (!q->bitmaps[ER] && qfq_gt(roundedS, q->V))
  1041. q->V = roundedS;
  1042. grp->S = roundedS;
  1043. grp->F = roundedS + (2ULL << grp->slot_shift);
  1044. s = qfq_calc_state(q, grp);
  1045. __set_bit(grp->index, &q->bitmaps[s]);
  1046. pr_debug("qfq enqueue: new state %d %#lx S %lld F %lld V %lld\n",
  1047. s, q->bitmaps[s],
  1048. (unsigned long long) agg->S,
  1049. (unsigned long long) agg->F,
  1050. (unsigned long long) q->V);
  1051. skip_update:
  1052. qfq_slot_insert(grp, agg, roundedS);
  1053. }
  1054. /* Update agg ts and schedule agg for service */
  1055. static void qfq_activate_agg(struct qfq_sched *q, struct qfq_aggregate *agg,
  1056. enum update_reason reason)
  1057. {
  1058. qfq_update_agg_ts(q, agg, reason);
  1059. qfq_schedule_agg(q, agg);
  1060. }
  1061. static void qfq_slot_remove(struct qfq_sched *q, struct qfq_group *grp,
  1062. struct qfq_aggregate *agg)
  1063. {
  1064. unsigned int i, offset;
  1065. u64 roundedS;
  1066. roundedS = qfq_round_down(agg->S, grp->slot_shift);
  1067. offset = (roundedS - grp->S) >> grp->slot_shift;
  1068. i = (grp->front + offset) % QFQ_MAX_SLOTS;
  1069. hlist_del(&agg->next);
  1070. if (hlist_empty(&grp->slots[i]))
  1071. __clear_bit(offset, &grp->full_slots);
  1072. }
  1073. /*
  1074. * Called to forcibly deschedule an aggregate. If the aggregate is
  1075. * not in the front bucket, or if the latter has other aggregates in
  1076. * the front bucket, we can simply remove the aggregate with no other
  1077. * side effects.
  1078. * Otherwise we must propagate the event up.
  1079. */
  1080. static void qfq_deactivate_agg(struct qfq_sched *q, struct qfq_aggregate *agg)
  1081. {
  1082. struct qfq_group *grp = agg->grp;
  1083. unsigned long mask;
  1084. u64 roundedS;
  1085. int s;
  1086. if (agg == q->in_serv_agg) {
  1087. charge_actual_service(agg);
  1088. q->in_serv_agg = qfq_choose_next_agg(q);
  1089. return;
  1090. }
  1091. agg->F = agg->S;
  1092. qfq_slot_remove(q, grp, agg);
  1093. if (!grp->full_slots) {
  1094. __clear_bit(grp->index, &q->bitmaps[IR]);
  1095. __clear_bit(grp->index, &q->bitmaps[EB]);
  1096. __clear_bit(grp->index, &q->bitmaps[IB]);
  1097. if (test_bit(grp->index, &q->bitmaps[ER]) &&
  1098. !(q->bitmaps[ER] & ~((1UL << grp->index) - 1))) {
  1099. mask = q->bitmaps[ER] & ((1UL << grp->index) - 1);
  1100. if (mask)
  1101. mask = ~((1UL << __fls(mask)) - 1);
  1102. else
  1103. mask = ~0UL;
  1104. qfq_move_groups(q, mask, EB, ER);
  1105. qfq_move_groups(q, mask, IB, IR);
  1106. }
  1107. __clear_bit(grp->index, &q->bitmaps[ER]);
  1108. } else if (hlist_empty(&grp->slots[grp->front])) {
  1109. agg = qfq_slot_scan(grp);
  1110. roundedS = qfq_round_down(agg->S, grp->slot_shift);
  1111. if (grp->S != roundedS) {
  1112. __clear_bit(grp->index, &q->bitmaps[ER]);
  1113. __clear_bit(grp->index, &q->bitmaps[IR]);
  1114. __clear_bit(grp->index, &q->bitmaps[EB]);
  1115. __clear_bit(grp->index, &q->bitmaps[IB]);
  1116. grp->S = roundedS;
  1117. grp->F = roundedS + (2ULL << grp->slot_shift);
  1118. s = qfq_calc_state(q, grp);
  1119. __set_bit(grp->index, &q->bitmaps[s]);
  1120. }
  1121. }
  1122. qfq_update_eligible(q);
  1123. }
  1124. static void qfq_qlen_notify(struct Qdisc *sch, unsigned long arg)
  1125. {
  1126. struct qfq_sched *q = qdisc_priv(sch);
  1127. struct qfq_class *cl = (struct qfq_class *)arg;
  1128. if (cl->qdisc->q.qlen == 0)
  1129. qfq_deactivate_class(q, cl);
  1130. }
  1131. static unsigned int qfq_drop_from_slot(struct qfq_sched *q,
  1132. struct hlist_head *slot)
  1133. {
  1134. struct qfq_aggregate *agg;
  1135. struct hlist_node *n;
  1136. struct qfq_class *cl;
  1137. unsigned int len;
  1138. hlist_for_each_entry(agg, n, slot, next) {
  1139. list_for_each_entry(cl, &agg->active, alist) {
  1140. if (!cl->qdisc->ops->drop)
  1141. continue;
  1142. len = cl->qdisc->ops->drop(cl->qdisc);
  1143. if (len > 0) {
  1144. if (cl->qdisc->q.qlen == 0)
  1145. qfq_deactivate_class(q, cl);
  1146. return len;
  1147. }
  1148. }
  1149. }
  1150. return 0;
  1151. }
  1152. static unsigned int qfq_drop(struct Qdisc *sch)
  1153. {
  1154. struct qfq_sched *q = qdisc_priv(sch);
  1155. struct qfq_group *grp;
  1156. unsigned int i, j, len;
  1157. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  1158. grp = &q->groups[i];
  1159. for (j = 0; j < QFQ_MAX_SLOTS; j++) {
  1160. len = qfq_drop_from_slot(q, &grp->slots[j]);
  1161. if (len > 0) {
  1162. sch->q.qlen--;
  1163. return len;
  1164. }
  1165. }
  1166. }
  1167. return 0;
  1168. }
  1169. static int qfq_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
  1170. {
  1171. struct qfq_sched *q = qdisc_priv(sch);
  1172. struct qfq_group *grp;
  1173. int i, j, err;
  1174. u32 max_cl_shift, maxbudg_shift, max_classes;
  1175. err = qdisc_class_hash_init(&q->clhash);
  1176. if (err < 0)
  1177. return err;
  1178. if (qdisc_dev(sch)->tx_queue_len + 1 > QFQ_MAX_AGG_CLASSES)
  1179. max_classes = QFQ_MAX_AGG_CLASSES;
  1180. else
  1181. max_classes = qdisc_dev(sch)->tx_queue_len + 1;
  1182. /* max_cl_shift = floor(log_2(max_classes)) */
  1183. max_cl_shift = __fls(max_classes);
  1184. q->max_agg_classes = 1<<max_cl_shift;
  1185. /* maxbudg_shift = log2(max_len * max_classes_per_agg) */
  1186. maxbudg_shift = QFQ_MTU_SHIFT + max_cl_shift;
  1187. q->min_slot_shift = FRAC_BITS + maxbudg_shift - QFQ_MAX_INDEX;
  1188. for (i = 0; i <= QFQ_MAX_INDEX; i++) {
  1189. grp = &q->groups[i];
  1190. grp->index = i;
  1191. grp->slot_shift = q->min_slot_shift + i;
  1192. for (j = 0; j < QFQ_MAX_SLOTS; j++)
  1193. INIT_HLIST_HEAD(&grp->slots[j]);
  1194. }
  1195. INIT_HLIST_HEAD(&q->nonfull_aggs);
  1196. return 0;
  1197. }
  1198. static void qfq_reset_qdisc(struct Qdisc *sch)
  1199. {
  1200. struct qfq_sched *q = qdisc_priv(sch);
  1201. struct qfq_class *cl;
  1202. struct hlist_node *n;
  1203. unsigned int i;
  1204. for (i = 0; i < q->clhash.hashsize; i++) {
  1205. hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
  1206. if (cl->qdisc->q.qlen > 0)
  1207. qfq_deactivate_class(q, cl);
  1208. qdisc_reset(cl->qdisc);
  1209. }
  1210. }
  1211. sch->q.qlen = 0;
  1212. }
  1213. static void qfq_destroy_qdisc(struct Qdisc *sch)
  1214. {
  1215. struct qfq_sched *q = qdisc_priv(sch);
  1216. struct qfq_class *cl;
  1217. struct hlist_node *n, *next;
  1218. unsigned int i;
  1219. tcf_destroy_chain(&q->filter_list);
  1220. for (i = 0; i < q->clhash.hashsize; i++) {
  1221. hlist_for_each_entry_safe(cl, n, next, &q->clhash.hash[i],
  1222. common.hnode) {
  1223. qfq_destroy_class(sch, cl);
  1224. }
  1225. }
  1226. qdisc_class_hash_destroy(&q->clhash);
  1227. }
  1228. static const struct Qdisc_class_ops qfq_class_ops = {
  1229. .change = qfq_change_class,
  1230. .delete = qfq_delete_class,
  1231. .get = qfq_get_class,
  1232. .put = qfq_put_class,
  1233. .tcf_chain = qfq_tcf_chain,
  1234. .bind_tcf = qfq_bind_tcf,
  1235. .unbind_tcf = qfq_unbind_tcf,
  1236. .graft = qfq_graft_class,
  1237. .leaf = qfq_class_leaf,
  1238. .qlen_notify = qfq_qlen_notify,
  1239. .dump = qfq_dump_class,
  1240. .dump_stats = qfq_dump_class_stats,
  1241. .walk = qfq_walk,
  1242. };
  1243. static struct Qdisc_ops qfq_qdisc_ops __read_mostly = {
  1244. .cl_ops = &qfq_class_ops,
  1245. .id = "qfq",
  1246. .priv_size = sizeof(struct qfq_sched),
  1247. .enqueue = qfq_enqueue,
  1248. .dequeue = qfq_dequeue,
  1249. .peek = qdisc_peek_dequeued,
  1250. .drop = qfq_drop,
  1251. .init = qfq_init_qdisc,
  1252. .reset = qfq_reset_qdisc,
  1253. .destroy = qfq_destroy_qdisc,
  1254. .owner = THIS_MODULE,
  1255. };
  1256. static int __init qfq_init(void)
  1257. {
  1258. return register_qdisc(&qfq_qdisc_ops);
  1259. }
  1260. static void __exit qfq_exit(void)
  1261. {
  1262. unregister_qdisc(&qfq_qdisc_ops);
  1263. }
  1264. module_init(qfq_init);
  1265. module_exit(qfq_exit);
  1266. MODULE_LICENSE("GPL");