sch_cbq.c 48 KB

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