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