sch_qfq.c 42 KB

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