sch_qfq.c 42 KB

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