sch_qfq.c 42 KB

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