sch_qfq.c 42 KB

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