sch_cbq.c 48 KB

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