sch_qfq.c 41 KB

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