sch_cbq.c 47 KB

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