sch_qfq.c 43 KB

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