sch_qfq.c 42 KB

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