sch_cbq.c 48 KB

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