sch_cbq.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/sch_cbq.c Class-Based Queueing discipline.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/skbuff.h>
  14. #include <net/netlink.h>
  15. #include <net/pkt_sched.h>
  16. #include <net/pkt_cls.h>
  17. /* Class-Based Queueing (CBQ) algorithm.
  18. =======================================
  19. Sources: [1] Sally Floyd and Van Jacobson, "Link-sharing and Resource
  20. Management Models for Packet Networks",
  21. IEEE/ACM Transactions on Networking, Vol.3, No.4, 1995
  22. [2] Sally Floyd, "Notes on CBQ and Guaranteed Service", 1995
  23. [3] Sally Floyd, "Notes on Class-Based Queueing: Setting
  24. Parameters", 1996
  25. [4] Sally Floyd and Michael Speer, "Experimental Results
  26. for Class-Based Queueing", 1998, not published.
  27. -----------------------------------------------------------------------
  28. Algorithm skeleton was taken from NS simulator cbq.cc.
  29. If someone wants to check this code against the LBL version,
  30. he should take into account that ONLY the skeleton was borrowed,
  31. the implementation is different. Particularly:
  32. --- The WRR algorithm is different. Our version looks more
  33. reasonable (I hope) and works when quanta are allowed to be
  34. less than MTU, which is always the case when real time classes
  35. have small rates. Note, that the statement of [3] is
  36. incomplete, delay may actually be estimated even if class
  37. per-round allotment is less than MTU. Namely, if per-round
  38. allotment is W*r_i, and r_1+...+r_k = r < 1
  39. delay_i <= ([MTU/(W*r_i)]*W*r + W*r + k*MTU)/B
  40. In the worst case we have IntServ estimate with D = W*r+k*MTU
  41. and C = MTU*r. The proof (if correct at all) is trivial.
  42. --- It seems that cbq-2.0 is not very accurate. At least, I cannot
  43. interpret some places, which look like wrong translations
  44. from NS. Anyone is advised to find these differences
  45. and explain to me, why I am wrong 8).
  46. --- Linux has no EOI event, so that we cannot estimate true class
  47. idle time. Workaround is to consider the next dequeue event
  48. as sign that previous packet is finished. This is wrong because of
  49. internal device queueing, but on a permanently loaded link it is true.
  50. Moreover, combined with clock integrator, this scheme looks
  51. very close to an ideal solution. */
  52. struct cbq_sched_data;
  53. struct cbq_class {
  54. struct Qdisc_class_common common;
  55. struct cbq_class *next_alive; /* next class with backlog in this priority band */
  56. /* Parameters */
  57. unsigned char priority; /* class priority */
  58. unsigned char priority2; /* priority to be used after overlimit */
  59. unsigned char ewma_log; /* time constant for idle time calculation */
  60. u32 defmap;
  61. /* Link-sharing scheduler parameters */
  62. long maxidle; /* Class parameters: see below. */
  63. long offtime;
  64. long minidle;
  65. u32 avpkt;
  66. struct qdisc_rate_table *R_tab;
  67. /* General scheduler (WRR) parameters */
  68. long allot;
  69. long quantum; /* Allotment per WRR round */
  70. long weight; /* Relative allotment: see below */
  71. struct Qdisc *qdisc; /* Ptr to CBQ discipline */
  72. struct cbq_class *split; /* Ptr to split node */
  73. struct cbq_class *share; /* Ptr to LS parent in the class tree */
  74. struct cbq_class *tparent; /* Ptr to tree parent in the class tree */
  75. struct cbq_class *borrow; /* NULL if class is bandwidth limited;
  76. parent otherwise */
  77. struct cbq_class *sibling; /* Sibling chain */
  78. struct cbq_class *children; /* Pointer to children chain */
  79. struct Qdisc *q; /* Elementary queueing discipline */
  80. /* Variables */
  81. unsigned char cpriority; /* Effective priority */
  82. unsigned char delayed;
  83. unsigned char level; /* level of the class in hierarchy:
  84. 0 for leaf classes, and maximal
  85. level of children + 1 for nodes.
  86. */
  87. psched_time_t last; /* Last end of service */
  88. psched_time_t undertime;
  89. long avgidle;
  90. long deficit; /* Saved deficit for WRR */
  91. psched_time_t penalized;
  92. struct gnet_stats_basic_packed bstats;
  93. struct gnet_stats_queue qstats;
  94. struct net_rate_estimator __rcu *rate_est;
  95. struct tc_cbq_xstats xstats;
  96. struct tcf_proto __rcu *filter_list;
  97. struct tcf_block *block;
  98. int filters;
  99. struct cbq_class *defaults[TC_PRIO_MAX + 1];
  100. };
  101. struct cbq_sched_data {
  102. struct Qdisc_class_hash clhash; /* Hash table of all classes */
  103. int nclasses[TC_CBQ_MAXPRIO + 1];
  104. unsigned int quanta[TC_CBQ_MAXPRIO + 1];
  105. struct cbq_class link;
  106. unsigned int activemask;
  107. struct cbq_class *active[TC_CBQ_MAXPRIO + 1]; /* List of all classes
  108. with backlog */
  109. #ifdef CONFIG_NET_CLS_ACT
  110. struct cbq_class *rx_class;
  111. #endif
  112. struct cbq_class *tx_class;
  113. struct cbq_class *tx_borrowed;
  114. int tx_len;
  115. psched_time_t now; /* Cached timestamp */
  116. unsigned int pmask;
  117. struct hrtimer delay_timer;
  118. struct qdisc_watchdog watchdog; /* Watchdog timer,
  119. started when CBQ has
  120. backlog, but cannot
  121. transmit just now */
  122. psched_tdiff_t wd_expires;
  123. int toplevel;
  124. u32 hgenerator;
  125. };
  126. #define L2T(cl, len) qdisc_l2t((cl)->R_tab, len)
  127. static inline struct cbq_class *
  128. cbq_class_lookup(struct cbq_sched_data *q, u32 classid)
  129. {
  130. struct Qdisc_class_common *clc;
  131. clc = qdisc_class_find(&q->clhash, classid);
  132. if (clc == NULL)
  133. return NULL;
  134. return container_of(clc, struct cbq_class, common);
  135. }
  136. #ifdef CONFIG_NET_CLS_ACT
  137. static struct cbq_class *
  138. cbq_reclassify(struct sk_buff *skb, struct cbq_class *this)
  139. {
  140. struct cbq_class *cl;
  141. for (cl = this->tparent; cl; cl = cl->tparent) {
  142. struct cbq_class *new = cl->defaults[TC_PRIO_BESTEFFORT];
  143. if (new != NULL && new != this)
  144. return new;
  145. }
  146. return NULL;
  147. }
  148. #endif
  149. /* Classify packet. The procedure is pretty complicated, but
  150. * it allows us to combine link sharing and priority scheduling
  151. * transparently.
  152. *
  153. * Namely, you can put link sharing rules (f.e. route based) at root of CBQ,
  154. * so that it resolves to split nodes. Then packets are classified
  155. * by logical priority, or a more specific classifier may be attached
  156. * to the split node.
  157. */
  158. static struct cbq_class *
  159. cbq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  160. {
  161. struct cbq_sched_data *q = qdisc_priv(sch);
  162. struct cbq_class *head = &q->link;
  163. struct cbq_class **defmap;
  164. struct cbq_class *cl = NULL;
  165. u32 prio = skb->priority;
  166. struct tcf_proto *fl;
  167. struct tcf_result res;
  168. /*
  169. * Step 1. If skb->priority points to one of our classes, use it.
  170. */
  171. if (TC_H_MAJ(prio ^ sch->handle) == 0 &&
  172. (cl = cbq_class_lookup(q, prio)) != NULL)
  173. return cl;
  174. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  175. for (;;) {
  176. int result = 0;
  177. defmap = head->defaults;
  178. fl = rcu_dereference_bh(head->filter_list);
  179. /*
  180. * Step 2+n. Apply classifier.
  181. */
  182. result = tcf_classify(skb, fl, &res, true);
  183. if (!fl || result < 0)
  184. goto fallback;
  185. cl = (void *)res.class;
  186. if (!cl) {
  187. if (TC_H_MAJ(res.classid))
  188. cl = cbq_class_lookup(q, res.classid);
  189. else if ((cl = defmap[res.classid & TC_PRIO_MAX]) == NULL)
  190. cl = defmap[TC_PRIO_BESTEFFORT];
  191. if (cl == NULL)
  192. goto fallback;
  193. }
  194. if (cl->level >= head->level)
  195. goto fallback;
  196. #ifdef CONFIG_NET_CLS_ACT
  197. switch (result) {
  198. case TC_ACT_QUEUED:
  199. case TC_ACT_STOLEN:
  200. case TC_ACT_TRAP:
  201. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  202. fallthrough;
  203. case TC_ACT_SHOT:
  204. return NULL;
  205. case TC_ACT_RECLASSIFY:
  206. return cbq_reclassify(skb, cl);
  207. }
  208. #endif
  209. if (cl->level == 0)
  210. return cl;
  211. /*
  212. * Step 3+n. If classifier selected a link sharing class,
  213. * apply agency specific classifier.
  214. * Repeat this procdure until we hit a leaf node.
  215. */
  216. head = cl;
  217. }
  218. fallback:
  219. cl = head;
  220. /*
  221. * Step 4. No success...
  222. */
  223. if (TC_H_MAJ(prio) == 0 &&
  224. !(cl = head->defaults[prio & TC_PRIO_MAX]) &&
  225. !(cl = head->defaults[TC_PRIO_BESTEFFORT]))
  226. return head;
  227. return cl;
  228. }
  229. /*
  230. * A packet has just been enqueued on the empty class.
  231. * cbq_activate_class adds it to the tail of active class list
  232. * of its priority band.
  233. */
  234. static inline void cbq_activate_class(struct cbq_class *cl)
  235. {
  236. struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
  237. int prio = cl->cpriority;
  238. struct cbq_class *cl_tail;
  239. cl_tail = q->active[prio];
  240. q->active[prio] = cl;
  241. if (cl_tail != NULL) {
  242. cl->next_alive = cl_tail->next_alive;
  243. cl_tail->next_alive = cl;
  244. } else {
  245. cl->next_alive = cl;
  246. q->activemask |= (1<<prio);
  247. }
  248. }
  249. /*
  250. * Unlink class from active chain.
  251. * Note that this same procedure is done directly in cbq_dequeue*
  252. * during round-robin procedure.
  253. */
  254. static void cbq_deactivate_class(struct cbq_class *this)
  255. {
  256. struct cbq_sched_data *q = qdisc_priv(this->qdisc);
  257. int prio = this->cpriority;
  258. struct cbq_class *cl;
  259. struct cbq_class *cl_prev = q->active[prio];
  260. do {
  261. cl = cl_prev->next_alive;
  262. if (cl == this) {
  263. cl_prev->next_alive = cl->next_alive;
  264. cl->next_alive = NULL;
  265. if (cl == q->active[prio]) {
  266. q->active[prio] = cl_prev;
  267. if (cl == q->active[prio]) {
  268. q->active[prio] = NULL;
  269. q->activemask &= ~(1<<prio);
  270. return;
  271. }
  272. }
  273. return;
  274. }
  275. } while ((cl_prev = cl) != q->active[prio]);
  276. }
  277. static void
  278. cbq_mark_toplevel(struct cbq_sched_data *q, struct cbq_class *cl)
  279. {
  280. int toplevel = q->toplevel;
  281. if (toplevel > cl->level) {
  282. psched_time_t now = psched_get_time();
  283. do {
  284. if (cl->undertime < now) {
  285. q->toplevel = cl->level;
  286. return;
  287. }
  288. } while ((cl = cl->borrow) != NULL && toplevel > cl->level);
  289. }
  290. }
  291. static int
  292. cbq_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  293. struct sk_buff **to_free)
  294. {
  295. struct cbq_sched_data *q = qdisc_priv(sch);
  296. int ret;
  297. struct cbq_class *cl = cbq_classify(skb, sch, &ret);
  298. #ifdef CONFIG_NET_CLS_ACT
  299. q->rx_class = cl;
  300. #endif
  301. if (cl == NULL) {
  302. if (ret & __NET_XMIT_BYPASS)
  303. qdisc_qstats_drop(sch);
  304. __qdisc_drop(skb, to_free);
  305. return ret;
  306. }
  307. ret = qdisc_enqueue(skb, cl->q, to_free);
  308. if (ret == NET_XMIT_SUCCESS) {
  309. sch->q.qlen++;
  310. cbq_mark_toplevel(q, cl);
  311. if (!cl->next_alive)
  312. cbq_activate_class(cl);
  313. return ret;
  314. }
  315. if (net_xmit_drop_count(ret)) {
  316. qdisc_qstats_drop(sch);
  317. cbq_mark_toplevel(q, cl);
  318. cl->qstats.drops++;
  319. }
  320. return ret;
  321. }
  322. /* Overlimit action: penalize leaf class by adding offtime */
  323. static void cbq_overlimit(struct cbq_class *cl)
  324. {
  325. struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
  326. psched_tdiff_t delay = cl->undertime - q->now;
  327. if (!cl->delayed) {
  328. delay += cl->offtime;
  329. /*
  330. * Class goes to sleep, so that it will have no
  331. * chance to work avgidle. Let's forgive it 8)
  332. *
  333. * BTW cbq-2.0 has a crap in this
  334. * place, apparently they forgot to shift it by cl->ewma_log.
  335. */
  336. if (cl->avgidle < 0)
  337. delay -= (-cl->avgidle) - ((-cl->avgidle) >> cl->ewma_log);
  338. if (cl->avgidle < cl->minidle)
  339. cl->avgidle = cl->minidle;
  340. if (delay <= 0)
  341. delay = 1;
  342. cl->undertime = q->now + delay;
  343. cl->xstats.overactions++;
  344. cl->delayed = 1;
  345. }
  346. if (q->wd_expires == 0 || q->wd_expires > delay)
  347. q->wd_expires = delay;
  348. /* Dirty work! We must schedule wakeups based on
  349. * real available rate, rather than leaf rate,
  350. * which may be tiny (even zero).
  351. */
  352. if (q->toplevel == TC_CBQ_MAXLEVEL) {
  353. struct cbq_class *b;
  354. psched_tdiff_t base_delay = q->wd_expires;
  355. for (b = cl->borrow; b; b = b->borrow) {
  356. delay = b->undertime - q->now;
  357. if (delay < base_delay) {
  358. if (delay <= 0)
  359. delay = 1;
  360. base_delay = delay;
  361. }
  362. }
  363. q->wd_expires = base_delay;
  364. }
  365. }
  366. static psched_tdiff_t cbq_undelay_prio(struct cbq_sched_data *q, int prio,
  367. psched_time_t now)
  368. {
  369. struct cbq_class *cl;
  370. struct cbq_class *cl_prev = q->active[prio];
  371. psched_time_t sched = now;
  372. if (cl_prev == NULL)
  373. return 0;
  374. do {
  375. cl = cl_prev->next_alive;
  376. if (now - cl->penalized > 0) {
  377. cl_prev->next_alive = cl->next_alive;
  378. cl->next_alive = NULL;
  379. cl->cpriority = cl->priority;
  380. cl->delayed = 0;
  381. cbq_activate_class(cl);
  382. if (cl == q->active[prio]) {
  383. q->active[prio] = cl_prev;
  384. if (cl == q->active[prio]) {
  385. q->active[prio] = NULL;
  386. return 0;
  387. }
  388. }
  389. cl = cl_prev->next_alive;
  390. } else if (sched - cl->penalized > 0)
  391. sched = cl->penalized;
  392. } while ((cl_prev = cl) != q->active[prio]);
  393. return sched - now;
  394. }
  395. static enum hrtimer_restart cbq_undelay(struct hrtimer *timer)
  396. {
  397. struct cbq_sched_data *q = container_of(timer, struct cbq_sched_data,
  398. delay_timer);
  399. struct Qdisc *sch = q->watchdog.qdisc;
  400. psched_time_t now;
  401. psched_tdiff_t delay = 0;
  402. unsigned int pmask;
  403. now = psched_get_time();
  404. pmask = q->pmask;
  405. q->pmask = 0;
  406. while (pmask) {
  407. int prio = ffz(~pmask);
  408. psched_tdiff_t tmp;
  409. pmask &= ~(1<<prio);
  410. tmp = cbq_undelay_prio(q, prio, now);
  411. if (tmp > 0) {
  412. q->pmask |= 1<<prio;
  413. if (tmp < delay || delay == 0)
  414. delay = tmp;
  415. }
  416. }
  417. if (delay) {
  418. ktime_t time;
  419. time = 0;
  420. time = ktime_add_ns(time, PSCHED_TICKS2NS(now + delay));
  421. hrtimer_start(&q->delay_timer, time, HRTIMER_MODE_ABS_PINNED);
  422. }
  423. __netif_schedule(qdisc_root(sch));
  424. return HRTIMER_NORESTART;
  425. }
  426. /*
  427. * It is mission critical procedure.
  428. *
  429. * We "regenerate" toplevel cutoff, if transmitting class
  430. * has backlog and it is not regulated. It is not part of
  431. * original CBQ description, but looks more reasonable.
  432. * Probably, it is wrong. This question needs further investigation.
  433. */
  434. static inline void
  435. cbq_update_toplevel(struct cbq_sched_data *q, struct cbq_class *cl,
  436. struct cbq_class *borrowed)
  437. {
  438. if (cl && q->toplevel >= borrowed->level) {
  439. if (cl->q->q.qlen > 1) {
  440. do {
  441. if (borrowed->undertime == PSCHED_PASTPERFECT) {
  442. q->toplevel = borrowed->level;
  443. return;
  444. }
  445. } while ((borrowed = borrowed->borrow) != NULL);
  446. }
  447. #if 0
  448. /* It is not necessary now. Uncommenting it
  449. will save CPU cycles, but decrease fairness.
  450. */
  451. q->toplevel = TC_CBQ_MAXLEVEL;
  452. #endif
  453. }
  454. }
  455. static void
  456. cbq_update(struct cbq_sched_data *q)
  457. {
  458. struct cbq_class *this = q->tx_class;
  459. struct cbq_class *cl = this;
  460. int len = q->tx_len;
  461. psched_time_t now;
  462. q->tx_class = NULL;
  463. /* Time integrator. We calculate EOS time
  464. * by adding expected packet transmission time.
  465. */
  466. now = q->now + L2T(&q->link, len);
  467. for ( ; cl; cl = cl->share) {
  468. long avgidle = cl->avgidle;
  469. long idle;
  470. cl->bstats.packets++;
  471. cl->bstats.bytes += len;
  472. /*
  473. * (now - last) is total time between packet right edges.
  474. * (last_pktlen/rate) is "virtual" busy time, so that
  475. *
  476. * idle = (now - last) - last_pktlen/rate
  477. */
  478. idle = now - cl->last;
  479. if ((unsigned long)idle > 128*1024*1024) {
  480. avgidle = cl->maxidle;
  481. } else {
  482. idle -= L2T(cl, len);
  483. /* true_avgidle := (1-W)*true_avgidle + W*idle,
  484. * where W=2^{-ewma_log}. But cl->avgidle is scaled:
  485. * cl->avgidle == true_avgidle/W,
  486. * hence:
  487. */
  488. avgidle += idle - (avgidle>>cl->ewma_log);
  489. }
  490. if (avgidle <= 0) {
  491. /* Overlimit or at-limit */
  492. if (avgidle < cl->minidle)
  493. avgidle = cl->minidle;
  494. cl->avgidle = avgidle;
  495. /* Calculate expected time, when this class
  496. * will be allowed to send.
  497. * It will occur, when:
  498. * (1-W)*true_avgidle + W*delay = 0, i.e.
  499. * idle = (1/W - 1)*(-true_avgidle)
  500. * or
  501. * idle = (1 - W)*(-cl->avgidle);
  502. */
  503. idle = (-avgidle) - ((-avgidle) >> cl->ewma_log);
  504. /*
  505. * That is not all.
  506. * To maintain the rate allocated to the class,
  507. * we add to undertime virtual clock,
  508. * necessary to complete transmitted packet.
  509. * (len/phys_bandwidth has been already passed
  510. * to the moment of cbq_update)
  511. */
  512. idle -= L2T(&q->link, len);
  513. idle += L2T(cl, len);
  514. cl->undertime = now + idle;
  515. } else {
  516. /* Underlimit */
  517. cl->undertime = PSCHED_PASTPERFECT;
  518. if (avgidle > cl->maxidle)
  519. cl->avgidle = cl->maxidle;
  520. else
  521. cl->avgidle = avgidle;
  522. }
  523. if ((s64)(now - cl->last) > 0)
  524. cl->last = now;
  525. }
  526. cbq_update_toplevel(q, this, q->tx_borrowed);
  527. }
  528. static inline struct cbq_class *
  529. cbq_under_limit(struct cbq_class *cl)
  530. {
  531. struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
  532. struct cbq_class *this_cl = cl;
  533. if (cl->tparent == NULL)
  534. return cl;
  535. if (cl->undertime == PSCHED_PASTPERFECT || q->now >= cl->undertime) {
  536. cl->delayed = 0;
  537. return cl;
  538. }
  539. do {
  540. /* It is very suspicious place. Now overlimit
  541. * action is generated for not bounded classes
  542. * only if link is completely congested.
  543. * Though it is in agree with ancestor-only paradigm,
  544. * it looks very stupid. Particularly,
  545. * it means that this chunk of code will either
  546. * never be called or result in strong amplification
  547. * of burstiness. Dangerous, silly, and, however,
  548. * no another solution exists.
  549. */
  550. cl = cl->borrow;
  551. if (!cl) {
  552. this_cl->qstats.overlimits++;
  553. cbq_overlimit(this_cl);
  554. return NULL;
  555. }
  556. if (cl->level > q->toplevel)
  557. return NULL;
  558. } while (cl->undertime != PSCHED_PASTPERFECT && q->now < cl->undertime);
  559. cl->delayed = 0;
  560. return cl;
  561. }
  562. static inline struct sk_buff *
  563. cbq_dequeue_prio(struct Qdisc *sch, int prio)
  564. {
  565. struct cbq_sched_data *q = qdisc_priv(sch);
  566. struct cbq_class *cl_tail, *cl_prev, *cl;
  567. struct sk_buff *skb;
  568. int deficit;
  569. cl_tail = cl_prev = q->active[prio];
  570. cl = cl_prev->next_alive;
  571. do {
  572. deficit = 0;
  573. /* Start round */
  574. do {
  575. struct cbq_class *borrow = cl;
  576. if (cl->q->q.qlen &&
  577. (borrow = cbq_under_limit(cl)) == NULL)
  578. goto skip_class;
  579. if (cl->deficit <= 0) {
  580. /* Class exhausted its allotment per
  581. * this round. Switch to the next one.
  582. */
  583. deficit = 1;
  584. cl->deficit += cl->quantum;
  585. goto next_class;
  586. }
  587. skb = cl->q->dequeue(cl->q);
  588. /* Class did not give us any skb :-(
  589. * It could occur even if cl->q->q.qlen != 0
  590. * f.e. if cl->q == "tbf"
  591. */
  592. if (skb == NULL)
  593. goto skip_class;
  594. cl->deficit -= qdisc_pkt_len(skb);
  595. q->tx_class = cl;
  596. q->tx_borrowed = borrow;
  597. if (borrow != cl) {
  598. #ifndef CBQ_XSTATS_BORROWS_BYTES
  599. borrow->xstats.borrows++;
  600. cl->xstats.borrows++;
  601. #else
  602. borrow->xstats.borrows += qdisc_pkt_len(skb);
  603. cl->xstats.borrows += qdisc_pkt_len(skb);
  604. #endif
  605. }
  606. q->tx_len = qdisc_pkt_len(skb);
  607. if (cl->deficit <= 0) {
  608. q->active[prio] = cl;
  609. cl = cl->next_alive;
  610. cl->deficit += cl->quantum;
  611. }
  612. return skb;
  613. skip_class:
  614. if (cl->q->q.qlen == 0 || prio != cl->cpriority) {
  615. /* Class is empty or penalized.
  616. * Unlink it from active chain.
  617. */
  618. cl_prev->next_alive = cl->next_alive;
  619. cl->next_alive = NULL;
  620. /* Did cl_tail point to it? */
  621. if (cl == cl_tail) {
  622. /* Repair it! */
  623. cl_tail = cl_prev;
  624. /* Was it the last class in this band? */
  625. if (cl == cl_tail) {
  626. /* Kill the band! */
  627. q->active[prio] = NULL;
  628. q->activemask &= ~(1<<prio);
  629. if (cl->q->q.qlen)
  630. cbq_activate_class(cl);
  631. return NULL;
  632. }
  633. q->active[prio] = cl_tail;
  634. }
  635. if (cl->q->q.qlen)
  636. cbq_activate_class(cl);
  637. cl = cl_prev;
  638. }
  639. next_class:
  640. cl_prev = cl;
  641. cl = cl->next_alive;
  642. } while (cl_prev != cl_tail);
  643. } while (deficit);
  644. q->active[prio] = cl_prev;
  645. return NULL;
  646. }
  647. static inline struct sk_buff *
  648. cbq_dequeue_1(struct Qdisc *sch)
  649. {
  650. struct cbq_sched_data *q = qdisc_priv(sch);
  651. struct sk_buff *skb;
  652. unsigned int activemask;
  653. activemask = q->activemask & 0xFF;
  654. while (activemask) {
  655. int prio = ffz(~activemask);
  656. activemask &= ~(1<<prio);
  657. skb = cbq_dequeue_prio(sch, prio);
  658. if (skb)
  659. return skb;
  660. }
  661. return NULL;
  662. }
  663. static struct sk_buff *
  664. cbq_dequeue(struct Qdisc *sch)
  665. {
  666. struct sk_buff *skb;
  667. struct cbq_sched_data *q = qdisc_priv(sch);
  668. psched_time_t now;
  669. now = psched_get_time();
  670. if (q->tx_class)
  671. cbq_update(q);
  672. q->now = now;
  673. for (;;) {
  674. q->wd_expires = 0;
  675. skb = cbq_dequeue_1(sch);
  676. if (skb) {
  677. qdisc_bstats_update(sch, skb);
  678. sch->q.qlen--;
  679. return skb;
  680. }
  681. /* All the classes are overlimit.
  682. *
  683. * It is possible, if:
  684. *
  685. * 1. Scheduler is empty.
  686. * 2. Toplevel cutoff inhibited borrowing.
  687. * 3. Root class is overlimit.
  688. *
  689. * Reset 2d and 3d conditions and retry.
  690. *
  691. * Note, that NS and cbq-2.0 are buggy, peeking
  692. * an arbitrary class is appropriate for ancestor-only
  693. * sharing, but not for toplevel algorithm.
  694. *
  695. * Our version is better, but slower, because it requires
  696. * two passes, but it is unavoidable with top-level sharing.
  697. */
  698. if (q->toplevel == TC_CBQ_MAXLEVEL &&
  699. q->link.undertime == PSCHED_PASTPERFECT)
  700. break;
  701. q->toplevel = TC_CBQ_MAXLEVEL;
  702. q->link.undertime = PSCHED_PASTPERFECT;
  703. }
  704. /* No packets in scheduler or nobody wants to give them to us :-(
  705. * Sigh... start watchdog timer in the last case.
  706. */
  707. if (sch->q.qlen) {
  708. qdisc_qstats_overlimit(sch);
  709. if (q->wd_expires)
  710. qdisc_watchdog_schedule(&q->watchdog,
  711. now + q->wd_expires);
  712. }
  713. return NULL;
  714. }
  715. /* CBQ class maintanance routines */
  716. static void cbq_adjust_levels(struct cbq_class *this)
  717. {
  718. if (this == NULL)
  719. return;
  720. do {
  721. int level = 0;
  722. struct cbq_class *cl;
  723. cl = this->children;
  724. if (cl) {
  725. do {
  726. if (cl->level > level)
  727. level = cl->level;
  728. } while ((cl = cl->sibling) != this->children);
  729. }
  730. this->level = level + 1;
  731. } while ((this = this->tparent) != NULL);
  732. }
  733. static void cbq_normalize_quanta(struct cbq_sched_data *q, int prio)
  734. {
  735. struct cbq_class *cl;
  736. unsigned int h;
  737. if (q->quanta[prio] == 0)
  738. return;
  739. for (h = 0; h < q->clhash.hashsize; h++) {
  740. hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
  741. /* BUGGGG... Beware! This expression suffer of
  742. * arithmetic overflows!
  743. */
  744. if (cl->priority == prio) {
  745. cl->quantum = (cl->weight*cl->allot*q->nclasses[prio])/
  746. q->quanta[prio];
  747. }
  748. if (cl->quantum <= 0 ||
  749. cl->quantum > 32*qdisc_dev(cl->qdisc)->mtu) {
  750. pr_warn("CBQ: class %08x has bad quantum==%ld, repaired.\n",
  751. cl->common.classid, cl->quantum);
  752. cl->quantum = qdisc_dev(cl->qdisc)->mtu/2 + 1;
  753. }
  754. }
  755. }
  756. }
  757. static void cbq_sync_defmap(struct cbq_class *cl)
  758. {
  759. struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
  760. struct cbq_class *split = cl->split;
  761. unsigned int h;
  762. int i;
  763. if (split == NULL)
  764. return;
  765. for (i = 0; i <= TC_PRIO_MAX; i++) {
  766. if (split->defaults[i] == cl && !(cl->defmap & (1<<i)))
  767. split->defaults[i] = NULL;
  768. }
  769. for (i = 0; i <= TC_PRIO_MAX; i++) {
  770. int level = split->level;
  771. if (split->defaults[i])
  772. continue;
  773. for (h = 0; h < q->clhash.hashsize; h++) {
  774. struct cbq_class *c;
  775. hlist_for_each_entry(c, &q->clhash.hash[h],
  776. common.hnode) {
  777. if (c->split == split && c->level < level &&
  778. c->defmap & (1<<i)) {
  779. split->defaults[i] = c;
  780. level = c->level;
  781. }
  782. }
  783. }
  784. }
  785. }
  786. static void cbq_change_defmap(struct cbq_class *cl, u32 splitid, u32 def, u32 mask)
  787. {
  788. struct cbq_class *split = NULL;
  789. if (splitid == 0) {
  790. split = cl->split;
  791. if (!split)
  792. return;
  793. splitid = split->common.classid;
  794. }
  795. if (split == NULL || split->common.classid != splitid) {
  796. for (split = cl->tparent; split; split = split->tparent)
  797. if (split->common.classid == splitid)
  798. break;
  799. }
  800. if (split == NULL)
  801. return;
  802. if (cl->split != split) {
  803. cl->defmap = 0;
  804. cbq_sync_defmap(cl);
  805. cl->split = split;
  806. cl->defmap = def & mask;
  807. } else
  808. cl->defmap = (cl->defmap & ~mask) | (def & mask);
  809. cbq_sync_defmap(cl);
  810. }
  811. static void cbq_unlink_class(struct cbq_class *this)
  812. {
  813. struct cbq_class *cl, **clp;
  814. struct cbq_sched_data *q = qdisc_priv(this->qdisc);
  815. qdisc_class_hash_remove(&q->clhash, &this->common);
  816. if (this->tparent) {
  817. clp = &this->sibling;
  818. cl = *clp;
  819. do {
  820. if (cl == this) {
  821. *clp = cl->sibling;
  822. break;
  823. }
  824. clp = &cl->sibling;
  825. } while ((cl = *clp) != this->sibling);
  826. if (this->tparent->children == this) {
  827. this->tparent->children = this->sibling;
  828. if (this->sibling == this)
  829. this->tparent->children = NULL;
  830. }
  831. } else {
  832. WARN_ON(this->sibling != this);
  833. }
  834. }
  835. static void cbq_link_class(struct cbq_class *this)
  836. {
  837. struct cbq_sched_data *q = qdisc_priv(this->qdisc);
  838. struct cbq_class *parent = this->tparent;
  839. this->sibling = this;
  840. qdisc_class_hash_insert(&q->clhash, &this->common);
  841. if (parent == NULL)
  842. return;
  843. if (parent->children == NULL) {
  844. parent->children = this;
  845. } else {
  846. this->sibling = parent->children->sibling;
  847. parent->children->sibling = this;
  848. }
  849. }
  850. static void
  851. cbq_reset(struct Qdisc *sch)
  852. {
  853. struct cbq_sched_data *q = qdisc_priv(sch);
  854. struct cbq_class *cl;
  855. int prio;
  856. unsigned int h;
  857. q->activemask = 0;
  858. q->pmask = 0;
  859. q->tx_class = NULL;
  860. q->tx_borrowed = NULL;
  861. qdisc_watchdog_cancel(&q->watchdog);
  862. hrtimer_cancel(&q->delay_timer);
  863. q->toplevel = TC_CBQ_MAXLEVEL;
  864. q->now = psched_get_time();
  865. for (prio = 0; prio <= TC_CBQ_MAXPRIO; prio++)
  866. q->active[prio] = NULL;
  867. for (h = 0; h < q->clhash.hashsize; h++) {
  868. hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
  869. qdisc_reset(cl->q);
  870. cl->next_alive = NULL;
  871. cl->undertime = PSCHED_PASTPERFECT;
  872. cl->avgidle = cl->maxidle;
  873. cl->deficit = cl->quantum;
  874. cl->cpriority = cl->priority;
  875. }
  876. }
  877. sch->q.qlen = 0;
  878. }
  879. static int cbq_set_lss(struct cbq_class *cl, struct tc_cbq_lssopt *lss)
  880. {
  881. if (lss->change & TCF_CBQ_LSS_FLAGS) {
  882. cl->share = (lss->flags & TCF_CBQ_LSS_ISOLATED) ? NULL : cl->tparent;
  883. cl->borrow = (lss->flags & TCF_CBQ_LSS_BOUNDED) ? NULL : cl->tparent;
  884. }
  885. if (lss->change & TCF_CBQ_LSS_EWMA)
  886. cl->ewma_log = lss->ewma_log;
  887. if (lss->change & TCF_CBQ_LSS_AVPKT)
  888. cl->avpkt = lss->avpkt;
  889. if (lss->change & TCF_CBQ_LSS_MINIDLE)
  890. cl->minidle = -(long)lss->minidle;
  891. if (lss->change & TCF_CBQ_LSS_MAXIDLE) {
  892. cl->maxidle = lss->maxidle;
  893. cl->avgidle = lss->maxidle;
  894. }
  895. if (lss->change & TCF_CBQ_LSS_OFFTIME)
  896. cl->offtime = lss->offtime;
  897. return 0;
  898. }
  899. static void cbq_rmprio(struct cbq_sched_data *q, struct cbq_class *cl)
  900. {
  901. q->nclasses[cl->priority]--;
  902. q->quanta[cl->priority] -= cl->weight;
  903. cbq_normalize_quanta(q, cl->priority);
  904. }
  905. static void cbq_addprio(struct cbq_sched_data *q, struct cbq_class *cl)
  906. {
  907. q->nclasses[cl->priority]++;
  908. q->quanta[cl->priority] += cl->weight;
  909. cbq_normalize_quanta(q, cl->priority);
  910. }
  911. static int cbq_set_wrr(struct cbq_class *cl, struct tc_cbq_wrropt *wrr)
  912. {
  913. struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
  914. if (wrr->allot)
  915. cl->allot = wrr->allot;
  916. if (wrr->weight)
  917. cl->weight = wrr->weight;
  918. if (wrr->priority) {
  919. cl->priority = wrr->priority - 1;
  920. cl->cpriority = cl->priority;
  921. if (cl->priority >= cl->priority2)
  922. cl->priority2 = TC_CBQ_MAXPRIO - 1;
  923. }
  924. cbq_addprio(q, cl);
  925. return 0;
  926. }
  927. static int cbq_set_fopt(struct cbq_class *cl, struct tc_cbq_fopt *fopt)
  928. {
  929. cbq_change_defmap(cl, fopt->split, fopt->defmap, fopt->defchange);
  930. return 0;
  931. }
  932. static const struct nla_policy cbq_policy[TCA_CBQ_MAX + 1] = {
  933. [TCA_CBQ_LSSOPT] = { .len = sizeof(struct tc_cbq_lssopt) },
  934. [TCA_CBQ_WRROPT] = { .len = sizeof(struct tc_cbq_wrropt) },
  935. [TCA_CBQ_FOPT] = { .len = sizeof(struct tc_cbq_fopt) },
  936. [TCA_CBQ_OVL_STRATEGY] = { .len = sizeof(struct tc_cbq_ovl) },
  937. [TCA_CBQ_RATE] = { .len = sizeof(struct tc_ratespec) },
  938. [TCA_CBQ_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
  939. [TCA_CBQ_POLICE] = { .len = sizeof(struct tc_cbq_police) },
  940. };
  941. static int cbq_opt_parse(struct nlattr *tb[TCA_CBQ_MAX + 1],
  942. struct nlattr *opt,
  943. struct netlink_ext_ack *extack)
  944. {
  945. int err;
  946. if (!opt) {
  947. NL_SET_ERR_MSG(extack, "CBQ options are required for this operation");
  948. return -EINVAL;
  949. }
  950. err = nla_parse_nested_deprecated(tb, TCA_CBQ_MAX, opt,
  951. cbq_policy, extack);
  952. if (err < 0)
  953. return err;
  954. if (tb[TCA_CBQ_WRROPT]) {
  955. const struct tc_cbq_wrropt *wrr = nla_data(tb[TCA_CBQ_WRROPT]);
  956. if (wrr->priority > TC_CBQ_MAXPRIO) {
  957. NL_SET_ERR_MSG(extack, "priority is bigger than TC_CBQ_MAXPRIO");
  958. err = -EINVAL;
  959. }
  960. }
  961. return err;
  962. }
  963. static int cbq_init(struct Qdisc *sch, struct nlattr *opt,
  964. struct netlink_ext_ack *extack)
  965. {
  966. struct cbq_sched_data *q = qdisc_priv(sch);
  967. struct nlattr *tb[TCA_CBQ_MAX + 1];
  968. struct tc_ratespec *r;
  969. int err;
  970. qdisc_watchdog_init(&q->watchdog, sch);
  971. hrtimer_init(&q->delay_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  972. q->delay_timer.function = cbq_undelay;
  973. err = cbq_opt_parse(tb, opt, extack);
  974. if (err < 0)
  975. return err;
  976. if (!tb[TCA_CBQ_RTAB] || !tb[TCA_CBQ_RATE]) {
  977. NL_SET_ERR_MSG(extack, "Rate specification missing or incomplete");
  978. return -EINVAL;
  979. }
  980. r = nla_data(tb[TCA_CBQ_RATE]);
  981. q->link.R_tab = qdisc_get_rtab(r, tb[TCA_CBQ_RTAB], extack);
  982. if (!q->link.R_tab)
  983. return -EINVAL;
  984. err = tcf_block_get(&q->link.block, &q->link.filter_list, sch, extack);
  985. if (err)
  986. goto put_rtab;
  987. err = qdisc_class_hash_init(&q->clhash);
  988. if (err < 0)
  989. goto put_block;
  990. q->link.sibling = &q->link;
  991. q->link.common.classid = sch->handle;
  992. q->link.qdisc = sch;
  993. q->link.q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  994. sch->handle, NULL);
  995. if (!q->link.q)
  996. q->link.q = &noop_qdisc;
  997. else
  998. qdisc_hash_add(q->link.q, true);
  999. q->link.priority = TC_CBQ_MAXPRIO - 1;
  1000. q->link.priority2 = TC_CBQ_MAXPRIO - 1;
  1001. q->link.cpriority = TC_CBQ_MAXPRIO - 1;
  1002. q->link.allot = psched_mtu(qdisc_dev(sch));
  1003. q->link.quantum = q->link.allot;
  1004. q->link.weight = q->link.R_tab->rate.rate;
  1005. q->link.ewma_log = TC_CBQ_DEF_EWMA;
  1006. q->link.avpkt = q->link.allot/2;
  1007. q->link.minidle = -0x7FFFFFFF;
  1008. q->toplevel = TC_CBQ_MAXLEVEL;
  1009. q->now = psched_get_time();
  1010. cbq_link_class(&q->link);
  1011. if (tb[TCA_CBQ_LSSOPT])
  1012. cbq_set_lss(&q->link, nla_data(tb[TCA_CBQ_LSSOPT]));
  1013. cbq_addprio(q, &q->link);
  1014. return 0;
  1015. put_block:
  1016. tcf_block_put(q->link.block);
  1017. put_rtab:
  1018. qdisc_put_rtab(q->link.R_tab);
  1019. return err;
  1020. }
  1021. static int cbq_dump_rate(struct sk_buff *skb, struct cbq_class *cl)
  1022. {
  1023. unsigned char *b = skb_tail_pointer(skb);
  1024. if (nla_put(skb, TCA_CBQ_RATE, sizeof(cl->R_tab->rate), &cl->R_tab->rate))
  1025. goto nla_put_failure;
  1026. return skb->len;
  1027. nla_put_failure:
  1028. nlmsg_trim(skb, b);
  1029. return -1;
  1030. }
  1031. static int cbq_dump_lss(struct sk_buff *skb, struct cbq_class *cl)
  1032. {
  1033. unsigned char *b = skb_tail_pointer(skb);
  1034. struct tc_cbq_lssopt opt;
  1035. opt.flags = 0;
  1036. if (cl->borrow == NULL)
  1037. opt.flags |= TCF_CBQ_LSS_BOUNDED;
  1038. if (cl->share == NULL)
  1039. opt.flags |= TCF_CBQ_LSS_ISOLATED;
  1040. opt.ewma_log = cl->ewma_log;
  1041. opt.level = cl->level;
  1042. opt.avpkt = cl->avpkt;
  1043. opt.maxidle = cl->maxidle;
  1044. opt.minidle = (u32)(-cl->minidle);
  1045. opt.offtime = cl->offtime;
  1046. opt.change = ~0;
  1047. if (nla_put(skb, TCA_CBQ_LSSOPT, sizeof(opt), &opt))
  1048. goto nla_put_failure;
  1049. return skb->len;
  1050. nla_put_failure:
  1051. nlmsg_trim(skb, b);
  1052. return -1;
  1053. }
  1054. static int cbq_dump_wrr(struct sk_buff *skb, struct cbq_class *cl)
  1055. {
  1056. unsigned char *b = skb_tail_pointer(skb);
  1057. struct tc_cbq_wrropt opt;
  1058. memset(&opt, 0, sizeof(opt));
  1059. opt.flags = 0;
  1060. opt.allot = cl->allot;
  1061. opt.priority = cl->priority + 1;
  1062. opt.cpriority = cl->cpriority + 1;
  1063. opt.weight = cl->weight;
  1064. if (nla_put(skb, TCA_CBQ_WRROPT, sizeof(opt), &opt))
  1065. goto nla_put_failure;
  1066. return skb->len;
  1067. nla_put_failure:
  1068. nlmsg_trim(skb, b);
  1069. return -1;
  1070. }
  1071. static int cbq_dump_fopt(struct sk_buff *skb, struct cbq_class *cl)
  1072. {
  1073. unsigned char *b = skb_tail_pointer(skb);
  1074. struct tc_cbq_fopt opt;
  1075. if (cl->split || cl->defmap) {
  1076. opt.split = cl->split ? cl->split->common.classid : 0;
  1077. opt.defmap = cl->defmap;
  1078. opt.defchange = ~0;
  1079. if (nla_put(skb, TCA_CBQ_FOPT, sizeof(opt), &opt))
  1080. goto nla_put_failure;
  1081. }
  1082. return skb->len;
  1083. nla_put_failure:
  1084. nlmsg_trim(skb, b);
  1085. return -1;
  1086. }
  1087. static int cbq_dump_attr(struct sk_buff *skb, struct cbq_class *cl)
  1088. {
  1089. if (cbq_dump_lss(skb, cl) < 0 ||
  1090. cbq_dump_rate(skb, cl) < 0 ||
  1091. cbq_dump_wrr(skb, cl) < 0 ||
  1092. cbq_dump_fopt(skb, cl) < 0)
  1093. return -1;
  1094. return 0;
  1095. }
  1096. static int cbq_dump(struct Qdisc *sch, struct sk_buff *skb)
  1097. {
  1098. struct cbq_sched_data *q = qdisc_priv(sch);
  1099. struct nlattr *nest;
  1100. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  1101. if (nest == NULL)
  1102. goto nla_put_failure;
  1103. if (cbq_dump_attr(skb, &q->link) < 0)
  1104. goto nla_put_failure;
  1105. return nla_nest_end(skb, nest);
  1106. nla_put_failure:
  1107. nla_nest_cancel(skb, nest);
  1108. return -1;
  1109. }
  1110. static int
  1111. cbq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  1112. {
  1113. struct cbq_sched_data *q = qdisc_priv(sch);
  1114. q->link.xstats.avgidle = q->link.avgidle;
  1115. return gnet_stats_copy_app(d, &q->link.xstats, sizeof(q->link.xstats));
  1116. }
  1117. static int
  1118. cbq_dump_class(struct Qdisc *sch, unsigned long arg,
  1119. struct sk_buff *skb, struct tcmsg *tcm)
  1120. {
  1121. struct cbq_class *cl = (struct cbq_class *)arg;
  1122. struct nlattr *nest;
  1123. if (cl->tparent)
  1124. tcm->tcm_parent = cl->tparent->common.classid;
  1125. else
  1126. tcm->tcm_parent = TC_H_ROOT;
  1127. tcm->tcm_handle = cl->common.classid;
  1128. tcm->tcm_info = cl->q->handle;
  1129. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  1130. if (nest == NULL)
  1131. goto nla_put_failure;
  1132. if (cbq_dump_attr(skb, cl) < 0)
  1133. goto nla_put_failure;
  1134. return nla_nest_end(skb, nest);
  1135. nla_put_failure:
  1136. nla_nest_cancel(skb, nest);
  1137. return -1;
  1138. }
  1139. static int
  1140. cbq_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  1141. struct gnet_dump *d)
  1142. {
  1143. struct cbq_sched_data *q = qdisc_priv(sch);
  1144. struct cbq_class *cl = (struct cbq_class *)arg;
  1145. __u32 qlen;
  1146. cl->xstats.avgidle = cl->avgidle;
  1147. cl->xstats.undertime = 0;
  1148. qdisc_qstats_qlen_backlog(cl->q, &qlen, &cl->qstats.backlog);
  1149. if (cl->undertime != PSCHED_PASTPERFECT)
  1150. cl->xstats.undertime = cl->undertime - q->now;
  1151. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
  1152. d, NULL, &cl->bstats) < 0 ||
  1153. gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
  1154. gnet_stats_copy_queue(d, NULL, &cl->qstats, qlen) < 0)
  1155. return -1;
  1156. return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
  1157. }
  1158. static int cbq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  1159. struct Qdisc **old, struct netlink_ext_ack *extack)
  1160. {
  1161. struct cbq_class *cl = (struct cbq_class *)arg;
  1162. if (new == NULL) {
  1163. new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  1164. cl->common.classid, extack);
  1165. if (new == NULL)
  1166. return -ENOBUFS;
  1167. }
  1168. *old = qdisc_replace(sch, new, &cl->q);
  1169. return 0;
  1170. }
  1171. static struct Qdisc *cbq_leaf(struct Qdisc *sch, unsigned long arg)
  1172. {
  1173. struct cbq_class *cl = (struct cbq_class *)arg;
  1174. return cl->q;
  1175. }
  1176. static void cbq_qlen_notify(struct Qdisc *sch, unsigned long arg)
  1177. {
  1178. struct cbq_class *cl = (struct cbq_class *)arg;
  1179. cbq_deactivate_class(cl);
  1180. }
  1181. static unsigned long cbq_find(struct Qdisc *sch, u32 classid)
  1182. {
  1183. struct cbq_sched_data *q = qdisc_priv(sch);
  1184. return (unsigned long)cbq_class_lookup(q, classid);
  1185. }
  1186. static void cbq_destroy_class(struct Qdisc *sch, struct cbq_class *cl)
  1187. {
  1188. struct cbq_sched_data *q = qdisc_priv(sch);
  1189. WARN_ON(cl->filters);
  1190. tcf_block_put(cl->block);
  1191. qdisc_put(cl->q);
  1192. qdisc_put_rtab(cl->R_tab);
  1193. gen_kill_estimator(&cl->rate_est);
  1194. if (cl != &q->link)
  1195. kfree(cl);
  1196. }
  1197. static void cbq_destroy(struct Qdisc *sch)
  1198. {
  1199. struct cbq_sched_data *q = qdisc_priv(sch);
  1200. struct hlist_node *next;
  1201. struct cbq_class *cl;
  1202. unsigned int h;
  1203. #ifdef CONFIG_NET_CLS_ACT
  1204. q->rx_class = NULL;
  1205. #endif
  1206. /*
  1207. * Filters must be destroyed first because we don't destroy the
  1208. * classes from root to leafs which means that filters can still
  1209. * be bound to classes which have been destroyed already. --TGR '04
  1210. */
  1211. for (h = 0; h < q->clhash.hashsize; h++) {
  1212. hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
  1213. tcf_block_put(cl->block);
  1214. cl->block = NULL;
  1215. }
  1216. }
  1217. for (h = 0; h < q->clhash.hashsize; h++) {
  1218. hlist_for_each_entry_safe(cl, next, &q->clhash.hash[h],
  1219. common.hnode)
  1220. cbq_destroy_class(sch, cl);
  1221. }
  1222. qdisc_class_hash_destroy(&q->clhash);
  1223. }
  1224. static int
  1225. cbq_change_class(struct Qdisc *sch, u32 classid, u32 parentid, struct nlattr **tca,
  1226. unsigned long *arg, struct netlink_ext_ack *extack)
  1227. {
  1228. int err;
  1229. struct cbq_sched_data *q = qdisc_priv(sch);
  1230. struct cbq_class *cl = (struct cbq_class *)*arg;
  1231. struct nlattr *opt = tca[TCA_OPTIONS];
  1232. struct nlattr *tb[TCA_CBQ_MAX + 1];
  1233. struct cbq_class *parent;
  1234. struct qdisc_rate_table *rtab = NULL;
  1235. err = cbq_opt_parse(tb, opt, extack);
  1236. if (err < 0)
  1237. return err;
  1238. if (tb[TCA_CBQ_OVL_STRATEGY] || tb[TCA_CBQ_POLICE]) {
  1239. NL_SET_ERR_MSG(extack, "Neither overlimit strategy nor policing attributes can be used for changing class params");
  1240. return -EOPNOTSUPP;
  1241. }
  1242. if (cl) {
  1243. /* Check parent */
  1244. if (parentid) {
  1245. if (cl->tparent &&
  1246. cl->tparent->common.classid != parentid) {
  1247. NL_SET_ERR_MSG(extack, "Invalid parent id");
  1248. return -EINVAL;
  1249. }
  1250. if (!cl->tparent && parentid != TC_H_ROOT) {
  1251. NL_SET_ERR_MSG(extack, "Parent must be root");
  1252. return -EINVAL;
  1253. }
  1254. }
  1255. if (tb[TCA_CBQ_RATE]) {
  1256. rtab = qdisc_get_rtab(nla_data(tb[TCA_CBQ_RATE]),
  1257. tb[TCA_CBQ_RTAB], extack);
  1258. if (rtab == NULL)
  1259. return -EINVAL;
  1260. }
  1261. if (tca[TCA_RATE]) {
  1262. err = gen_replace_estimator(&cl->bstats, NULL,
  1263. &cl->rate_est,
  1264. NULL,
  1265. qdisc_root_sleeping_running(sch),
  1266. tca[TCA_RATE]);
  1267. if (err) {
  1268. NL_SET_ERR_MSG(extack, "Failed to replace specified rate estimator");
  1269. qdisc_put_rtab(rtab);
  1270. return err;
  1271. }
  1272. }
  1273. /* Change class parameters */
  1274. sch_tree_lock(sch);
  1275. if (cl->next_alive != NULL)
  1276. cbq_deactivate_class(cl);
  1277. if (rtab) {
  1278. qdisc_put_rtab(cl->R_tab);
  1279. cl->R_tab = rtab;
  1280. }
  1281. if (tb[TCA_CBQ_LSSOPT])
  1282. cbq_set_lss(cl, nla_data(tb[TCA_CBQ_LSSOPT]));
  1283. if (tb[TCA_CBQ_WRROPT]) {
  1284. cbq_rmprio(q, cl);
  1285. cbq_set_wrr(cl, nla_data(tb[TCA_CBQ_WRROPT]));
  1286. }
  1287. if (tb[TCA_CBQ_FOPT])
  1288. cbq_set_fopt(cl, nla_data(tb[TCA_CBQ_FOPT]));
  1289. if (cl->q->q.qlen)
  1290. cbq_activate_class(cl);
  1291. sch_tree_unlock(sch);
  1292. return 0;
  1293. }
  1294. if (parentid == TC_H_ROOT)
  1295. return -EINVAL;
  1296. if (!tb[TCA_CBQ_WRROPT] || !tb[TCA_CBQ_RATE] || !tb[TCA_CBQ_LSSOPT]) {
  1297. NL_SET_ERR_MSG(extack, "One of the following attributes MUST be specified: WRR, rate or link sharing");
  1298. return -EINVAL;
  1299. }
  1300. rtab = qdisc_get_rtab(nla_data(tb[TCA_CBQ_RATE]), tb[TCA_CBQ_RTAB],
  1301. extack);
  1302. if (rtab == NULL)
  1303. return -EINVAL;
  1304. if (classid) {
  1305. err = -EINVAL;
  1306. if (TC_H_MAJ(classid ^ sch->handle) ||
  1307. cbq_class_lookup(q, classid)) {
  1308. NL_SET_ERR_MSG(extack, "Specified class not found");
  1309. goto failure;
  1310. }
  1311. } else {
  1312. int i;
  1313. classid = TC_H_MAKE(sch->handle, 0x8000);
  1314. for (i = 0; i < 0x8000; i++) {
  1315. if (++q->hgenerator >= 0x8000)
  1316. q->hgenerator = 1;
  1317. if (cbq_class_lookup(q, classid|q->hgenerator) == NULL)
  1318. break;
  1319. }
  1320. err = -ENOSR;
  1321. if (i >= 0x8000) {
  1322. NL_SET_ERR_MSG(extack, "Unable to generate classid");
  1323. goto failure;
  1324. }
  1325. classid = classid|q->hgenerator;
  1326. }
  1327. parent = &q->link;
  1328. if (parentid) {
  1329. parent = cbq_class_lookup(q, parentid);
  1330. err = -EINVAL;
  1331. if (!parent) {
  1332. NL_SET_ERR_MSG(extack, "Failed to find parentid");
  1333. goto failure;
  1334. }
  1335. }
  1336. err = -ENOBUFS;
  1337. cl = kzalloc(sizeof(*cl), GFP_KERNEL);
  1338. if (cl == NULL)
  1339. goto failure;
  1340. err = tcf_block_get(&cl->block, &cl->filter_list, sch, extack);
  1341. if (err) {
  1342. kfree(cl);
  1343. goto failure;
  1344. }
  1345. if (tca[TCA_RATE]) {
  1346. err = gen_new_estimator(&cl->bstats, NULL, &cl->rate_est,
  1347. NULL,
  1348. qdisc_root_sleeping_running(sch),
  1349. tca[TCA_RATE]);
  1350. if (err) {
  1351. NL_SET_ERR_MSG(extack, "Couldn't create new estimator");
  1352. tcf_block_put(cl->block);
  1353. kfree(cl);
  1354. goto failure;
  1355. }
  1356. }
  1357. cl->R_tab = rtab;
  1358. rtab = NULL;
  1359. cl->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, classid,
  1360. NULL);
  1361. if (!cl->q)
  1362. cl->q = &noop_qdisc;
  1363. else
  1364. qdisc_hash_add(cl->q, true);
  1365. cl->common.classid = classid;
  1366. cl->tparent = parent;
  1367. cl->qdisc = sch;
  1368. cl->allot = parent->allot;
  1369. cl->quantum = cl->allot;
  1370. cl->weight = cl->R_tab->rate.rate;
  1371. sch_tree_lock(sch);
  1372. cbq_link_class(cl);
  1373. cl->borrow = cl->tparent;
  1374. if (cl->tparent != &q->link)
  1375. cl->share = cl->tparent;
  1376. cbq_adjust_levels(parent);
  1377. cl->minidle = -0x7FFFFFFF;
  1378. cbq_set_lss(cl, nla_data(tb[TCA_CBQ_LSSOPT]));
  1379. cbq_set_wrr(cl, nla_data(tb[TCA_CBQ_WRROPT]));
  1380. if (cl->ewma_log == 0)
  1381. cl->ewma_log = q->link.ewma_log;
  1382. if (cl->maxidle == 0)
  1383. cl->maxidle = q->link.maxidle;
  1384. if (cl->avpkt == 0)
  1385. cl->avpkt = q->link.avpkt;
  1386. if (tb[TCA_CBQ_FOPT])
  1387. cbq_set_fopt(cl, nla_data(tb[TCA_CBQ_FOPT]));
  1388. sch_tree_unlock(sch);
  1389. qdisc_class_hash_grow(sch, &q->clhash);
  1390. *arg = (unsigned long)cl;
  1391. return 0;
  1392. failure:
  1393. qdisc_put_rtab(rtab);
  1394. return err;
  1395. }
  1396. static int cbq_delete(struct Qdisc *sch, unsigned long arg)
  1397. {
  1398. struct cbq_sched_data *q = qdisc_priv(sch);
  1399. struct cbq_class *cl = (struct cbq_class *)arg;
  1400. if (cl->filters || cl->children || cl == &q->link)
  1401. return -EBUSY;
  1402. sch_tree_lock(sch);
  1403. qdisc_purge_queue(cl->q);
  1404. if (cl->next_alive)
  1405. cbq_deactivate_class(cl);
  1406. if (q->tx_borrowed == cl)
  1407. q->tx_borrowed = q->tx_class;
  1408. if (q->tx_class == cl) {
  1409. q->tx_class = NULL;
  1410. q->tx_borrowed = NULL;
  1411. }
  1412. #ifdef CONFIG_NET_CLS_ACT
  1413. if (q->rx_class == cl)
  1414. q->rx_class = NULL;
  1415. #endif
  1416. cbq_unlink_class(cl);
  1417. cbq_adjust_levels(cl->tparent);
  1418. cl->defmap = 0;
  1419. cbq_sync_defmap(cl);
  1420. cbq_rmprio(q, cl);
  1421. sch_tree_unlock(sch);
  1422. cbq_destroy_class(sch, cl);
  1423. return 0;
  1424. }
  1425. static struct tcf_block *cbq_tcf_block(struct Qdisc *sch, unsigned long arg,
  1426. struct netlink_ext_ack *extack)
  1427. {
  1428. struct cbq_sched_data *q = qdisc_priv(sch);
  1429. struct cbq_class *cl = (struct cbq_class *)arg;
  1430. if (cl == NULL)
  1431. cl = &q->link;
  1432. return cl->block;
  1433. }
  1434. static unsigned long cbq_bind_filter(struct Qdisc *sch, unsigned long parent,
  1435. u32 classid)
  1436. {
  1437. struct cbq_sched_data *q = qdisc_priv(sch);
  1438. struct cbq_class *p = (struct cbq_class *)parent;
  1439. struct cbq_class *cl = cbq_class_lookup(q, classid);
  1440. if (cl) {
  1441. if (p && p->level <= cl->level)
  1442. return 0;
  1443. cl->filters++;
  1444. return (unsigned long)cl;
  1445. }
  1446. return 0;
  1447. }
  1448. static void cbq_unbind_filter(struct Qdisc *sch, unsigned long arg)
  1449. {
  1450. struct cbq_class *cl = (struct cbq_class *)arg;
  1451. cl->filters--;
  1452. }
  1453. static void cbq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  1454. {
  1455. struct cbq_sched_data *q = qdisc_priv(sch);
  1456. struct cbq_class *cl;
  1457. unsigned int h;
  1458. if (arg->stop)
  1459. return;
  1460. for (h = 0; h < q->clhash.hashsize; h++) {
  1461. hlist_for_each_entry(cl, &q->clhash.hash[h], common.hnode) {
  1462. if (arg->count < arg->skip) {
  1463. arg->count++;
  1464. continue;
  1465. }
  1466. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  1467. arg->stop = 1;
  1468. return;
  1469. }
  1470. arg->count++;
  1471. }
  1472. }
  1473. }
  1474. static const struct Qdisc_class_ops cbq_class_ops = {
  1475. .graft = cbq_graft,
  1476. .leaf = cbq_leaf,
  1477. .qlen_notify = cbq_qlen_notify,
  1478. .find = cbq_find,
  1479. .change = cbq_change_class,
  1480. .delete = cbq_delete,
  1481. .walk = cbq_walk,
  1482. .tcf_block = cbq_tcf_block,
  1483. .bind_tcf = cbq_bind_filter,
  1484. .unbind_tcf = cbq_unbind_filter,
  1485. .dump = cbq_dump_class,
  1486. .dump_stats = cbq_dump_class_stats,
  1487. };
  1488. static struct Qdisc_ops cbq_qdisc_ops __read_mostly = {
  1489. .next = NULL,
  1490. .cl_ops = &cbq_class_ops,
  1491. .id = "cbq",
  1492. .priv_size = sizeof(struct cbq_sched_data),
  1493. .enqueue = cbq_enqueue,
  1494. .dequeue = cbq_dequeue,
  1495. .peek = qdisc_peek_dequeued,
  1496. .init = cbq_init,
  1497. .reset = cbq_reset,
  1498. .destroy = cbq_destroy,
  1499. .change = NULL,
  1500. .dump = cbq_dump,
  1501. .dump_stats = cbq_dump_stats,
  1502. .owner = THIS_MODULE,
  1503. };
  1504. static int __init cbq_module_init(void)
  1505. {
  1506. return register_qdisc(&cbq_qdisc_ops);
  1507. }
  1508. static void __exit cbq_module_exit(void)
  1509. {
  1510. unregister_qdisc(&cbq_qdisc_ops);
  1511. }
  1512. module_init(cbq_module_init)
  1513. module_exit(cbq_module_exit)
  1514. MODULE_LICENSE("GPL");