123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127 |
- /*
- * net/sched/sch_cbq.c Class-Based Queueing discipline.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- *
- * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
- *
- */
- #include <linux/config.h>
- #include <linux/module.h>
- #include <asm/uaccess.h>
- #include <asm/system.h>
- #include <linux/bitops.h>
- #include <linux/types.h>
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/string.h>
- #include <linux/mm.h>
- #include <linux/socket.h>
- #include <linux/sockios.h>
- #include <linux/in.h>
- #include <linux/errno.h>
- #include <linux/interrupt.h>
- #include <linux/if_ether.h>
- #include <linux/inet.h>
- #include <linux/netdevice.h>
- #include <linux/etherdevice.h>
- #include <linux/notifier.h>
- #include <net/ip.h>
- #include <net/route.h>
- #include <linux/skbuff.h>
- #include <net/sock.h>
- #include <net/pkt_sched.h>
- /* Class-Based Queueing (CBQ) algorithm.
- =======================================
- Sources: [1] Sally Floyd and Van Jacobson, "Link-sharing and Resource
- Management Models for Packet Networks",
- IEEE/ACM Transactions on Networking, Vol.3, No.4, 1995
- [2] Sally Floyd, "Notes on CBQ and Guaranteed Service", 1995
- [3] Sally Floyd, "Notes on Class-Based Queueing: Setting
- Parameters", 1996
- [4] Sally Floyd and Michael Speer, "Experimental Results
- for Class-Based Queueing", 1998, not published.
- -----------------------------------------------------------------------
- Algorithm skeleton was taken from NS simulator cbq.cc.
- If someone wants to check this code against the LBL version,
- he should take into account that ONLY the skeleton was borrowed,
- the implementation is different. Particularly:
- --- The WRR algorithm is different. Our version looks more
- reasonable (I hope) and works when quanta are allowed to be
- less than MTU, which is always the case when real time classes
- have small rates. Note, that the statement of [3] is
- incomplete, delay may actually be estimated even if class
- per-round allotment is less than MTU. Namely, if per-round
- allotment is W*r_i, and r_1+...+r_k = r < 1
- delay_i <= ([MTU/(W*r_i)]*W*r + W*r + k*MTU)/B
- In the worst case we have IntServ estimate with D = W*r+k*MTU
- and C = MTU*r. The proof (if correct at all) is trivial.
- --- It seems that cbq-2.0 is not very accurate. At least, I cannot
- interpret some places, which look like wrong translations
- from NS. Anyone is advised to find these differences
- and explain to me, why I am wrong 8).
- --- Linux has no EOI event, so that we cannot estimate true class
- idle time. Workaround is to consider the next dequeue event
- as sign that previous packet is finished. This is wrong because of
- internal device queueing, but on a permanently loaded link it is true.
- Moreover, combined with clock integrator, this scheme looks
- very close to an ideal solution. */
- struct cbq_sched_data;
- struct cbq_class
- {
- struct cbq_class *next; /* hash table link */
- struct cbq_class *next_alive; /* next class with backlog in this priority band */
- /* Parameters */
- u32 classid;
- unsigned char priority; /* class priority */
- unsigned char priority2; /* priority to be used after overlimit */
- unsigned char ewma_log; /* time constant for idle time calculation */
- unsigned char ovl_strategy;
- #ifdef CONFIG_NET_CLS_POLICE
- unsigned char police;
- #endif
- u32 defmap;
- /* Link-sharing scheduler parameters */
- long maxidle; /* Class parameters: see below. */
- long offtime;
- long minidle;
- u32 avpkt;
- struct qdisc_rate_table *R_tab;
- /* Overlimit strategy parameters */
- void (*overlimit)(struct cbq_class *cl);
- long penalty;
- /* General scheduler (WRR) parameters */
- long allot;
- long quantum; /* Allotment per WRR round */
- long weight; /* Relative allotment: see below */
- struct Qdisc *qdisc; /* Ptr to CBQ discipline */
- struct cbq_class *split; /* Ptr to split node */
- struct cbq_class *share; /* Ptr to LS parent in the class tree */
- struct cbq_class *tparent; /* Ptr to tree parent in the class tree */
- struct cbq_class *borrow; /* NULL if class is bandwidth limited;
- parent otherwise */
- struct cbq_class *sibling; /* Sibling chain */
- struct cbq_class *children; /* Pointer to children chain */
- struct Qdisc *q; /* Elementary queueing discipline */
- /* Variables */
- unsigned char cpriority; /* Effective priority */
- unsigned char delayed;
- unsigned char level; /* level of the class in hierarchy:
- 0 for leaf classes, and maximal
- level of children + 1 for nodes.
- */
- psched_time_t last; /* Last end of service */
- psched_time_t undertime;
- long avgidle;
- long deficit; /* Saved deficit for WRR */
- unsigned long penalized;
- struct gnet_stats_basic bstats;
- struct gnet_stats_queue qstats;
- struct gnet_stats_rate_est rate_est;
- spinlock_t *stats_lock;
- struct tc_cbq_xstats xstats;
- struct tcf_proto *filter_list;
- int refcnt;
- int filters;
- struct cbq_class *defaults[TC_PRIO_MAX+1];
- };
- struct cbq_sched_data
- {
- struct cbq_class *classes[16]; /* Hash table of all classes */
- int nclasses[TC_CBQ_MAXPRIO+1];
- unsigned quanta[TC_CBQ_MAXPRIO+1];
- struct cbq_class link;
- unsigned activemask;
- struct cbq_class *active[TC_CBQ_MAXPRIO+1]; /* List of all classes
- with backlog */
- #ifdef CONFIG_NET_CLS_POLICE
- struct cbq_class *rx_class;
- #endif
- struct cbq_class *tx_class;
- struct cbq_class *tx_borrowed;
- int tx_len;
- psched_time_t now; /* Cached timestamp */
- psched_time_t now_rt; /* Cached real time */
- unsigned pmask;
- struct timer_list delay_timer;
- struct timer_list wd_timer; /* Watchdog timer,
- started when CBQ has
- backlog, but cannot
- transmit just now */
- long wd_expires;
- int toplevel;
- u32 hgenerator;
- };
- #define L2T(cl,len) ((cl)->R_tab->data[(len)>>(cl)->R_tab->rate.cell_log])
- static __inline__ unsigned cbq_hash(u32 h)
- {
- h ^= h>>8;
- h ^= h>>4;
- return h&0xF;
- }
- static __inline__ struct cbq_class *
- cbq_class_lookup(struct cbq_sched_data *q, u32 classid)
- {
- struct cbq_class *cl;
- for (cl = q->classes[cbq_hash(classid)]; cl; cl = cl->next)
- if (cl->classid == classid)
- return cl;
- return NULL;
- }
- #ifdef CONFIG_NET_CLS_POLICE
- static struct cbq_class *
- cbq_reclassify(struct sk_buff *skb, struct cbq_class *this)
- {
- struct cbq_class *cl, *new;
- for (cl = this->tparent; cl; cl = cl->tparent)
- if ((new = cl->defaults[TC_PRIO_BESTEFFORT]) != NULL && new != this)
- return new;
- return NULL;
- }
- #endif
- /* Classify packet. The procedure is pretty complicated, but
- it allows us to combine link sharing and priority scheduling
- transparently.
- Namely, you can put link sharing rules (f.e. route based) at root of CBQ,
- so that it resolves to split nodes. Then packets are classified
- by logical priority, or a more specific classifier may be attached
- to the split node.
- */
- static struct cbq_class *
- cbq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *head = &q->link;
- struct cbq_class **defmap;
- struct cbq_class *cl = NULL;
- u32 prio = skb->priority;
- struct tcf_result res;
- /*
- * Step 1. If skb->priority points to one of our classes, use it.
- */
- if (TC_H_MAJ(prio^sch->handle) == 0 &&
- (cl = cbq_class_lookup(q, prio)) != NULL)
- return cl;
- *qerr = NET_XMIT_BYPASS;
- for (;;) {
- int result = 0;
- defmap = head->defaults;
- /*
- * Step 2+n. Apply classifier.
- */
- if (!head->filter_list || (result = tc_classify(skb, head->filter_list, &res)) < 0)
- goto fallback;
- if ((cl = (void*)res.class) == NULL) {
- if (TC_H_MAJ(res.classid))
- cl = cbq_class_lookup(q, res.classid);
- else if ((cl = defmap[res.classid&TC_PRIO_MAX]) == NULL)
- cl = defmap[TC_PRIO_BESTEFFORT];
- if (cl == NULL || cl->level >= head->level)
- goto fallback;
- }
- #ifdef CONFIG_NET_CLS_ACT
- switch (result) {
- case TC_ACT_QUEUED:
- case TC_ACT_STOLEN:
- *qerr = NET_XMIT_SUCCESS;
- case TC_ACT_SHOT:
- return NULL;
- }
- #elif defined(CONFIG_NET_CLS_POLICE)
- switch (result) {
- case TC_POLICE_RECLASSIFY:
- return cbq_reclassify(skb, cl);
- case TC_POLICE_SHOT:
- return NULL;
- default:
- break;
- }
- #endif
- if (cl->level == 0)
- return cl;
- /*
- * Step 3+n. If classifier selected a link sharing class,
- * apply agency specific classifier.
- * Repeat this procdure until we hit a leaf node.
- */
- head = cl;
- }
- fallback:
- cl = head;
- /*
- * Step 4. No success...
- */
- if (TC_H_MAJ(prio) == 0 &&
- !(cl = head->defaults[prio&TC_PRIO_MAX]) &&
- !(cl = head->defaults[TC_PRIO_BESTEFFORT]))
- return head;
- return cl;
- }
- /*
- A packet has just been enqueued on the empty class.
- cbq_activate_class adds it to the tail of active class list
- of its priority band.
- */
- static __inline__ void cbq_activate_class(struct cbq_class *cl)
- {
- struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
- int prio = cl->cpriority;
- struct cbq_class *cl_tail;
- cl_tail = q->active[prio];
- q->active[prio] = cl;
- if (cl_tail != NULL) {
- cl->next_alive = cl_tail->next_alive;
- cl_tail->next_alive = cl;
- } else {
- cl->next_alive = cl;
- q->activemask |= (1<<prio);
- }
- }
- /*
- Unlink class from active chain.
- Note that this same procedure is done directly in cbq_dequeue*
- during round-robin procedure.
- */
- static void cbq_deactivate_class(struct cbq_class *this)
- {
- struct cbq_sched_data *q = qdisc_priv(this->qdisc);
- int prio = this->cpriority;
- struct cbq_class *cl;
- struct cbq_class *cl_prev = q->active[prio];
- do {
- cl = cl_prev->next_alive;
- if (cl == this) {
- cl_prev->next_alive = cl->next_alive;
- cl->next_alive = NULL;
- if (cl == q->active[prio]) {
- q->active[prio] = cl_prev;
- if (cl == q->active[prio]) {
- q->active[prio] = NULL;
- q->activemask &= ~(1<<prio);
- return;
- }
- }
- cl = cl_prev->next_alive;
- return;
- }
- } while ((cl_prev = cl) != q->active[prio]);
- }
- static void
- cbq_mark_toplevel(struct cbq_sched_data *q, struct cbq_class *cl)
- {
- int toplevel = q->toplevel;
- if (toplevel > cl->level && !(cl->q->flags&TCQ_F_THROTTLED)) {
- psched_time_t now;
- psched_tdiff_t incr;
- PSCHED_GET_TIME(now);
- incr = PSCHED_TDIFF(now, q->now_rt);
- PSCHED_TADD2(q->now, incr, now);
- do {
- if (PSCHED_TLESS(cl->undertime, now)) {
- q->toplevel = cl->level;
- return;
- }
- } while ((cl=cl->borrow) != NULL && toplevel > cl->level);
- }
- }
- static int
- cbq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- int len = skb->len;
- int ret;
- struct cbq_class *cl = cbq_classify(skb, sch, &ret);
- #ifdef CONFIG_NET_CLS_POLICE
- q->rx_class = cl;
- #endif
- if (cl == NULL) {
- if (ret == NET_XMIT_BYPASS)
- sch->qstats.drops++;
- kfree_skb(skb);
- return ret;
- }
- #ifdef CONFIG_NET_CLS_POLICE
- cl->q->__parent = sch;
- #endif
- if ((ret = cl->q->enqueue(skb, cl->q)) == NET_XMIT_SUCCESS) {
- sch->q.qlen++;
- sch->bstats.packets++;
- sch->bstats.bytes+=len;
- cbq_mark_toplevel(q, cl);
- if (!cl->next_alive)
- cbq_activate_class(cl);
- return ret;
- }
- sch->qstats.drops++;
- cbq_mark_toplevel(q, cl);
- cl->qstats.drops++;
- return ret;
- }
- static int
- cbq_requeue(struct sk_buff *skb, struct Qdisc *sch)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl;
- int ret;
- if ((cl = q->tx_class) == NULL) {
- kfree_skb(skb);
- sch->qstats.drops++;
- return NET_XMIT_CN;
- }
- q->tx_class = NULL;
- cbq_mark_toplevel(q, cl);
- #ifdef CONFIG_NET_CLS_POLICE
- q->rx_class = cl;
- cl->q->__parent = sch;
- #endif
- if ((ret = cl->q->ops->requeue(skb, cl->q)) == 0) {
- sch->q.qlen++;
- sch->qstats.requeues++;
- if (!cl->next_alive)
- cbq_activate_class(cl);
- return 0;
- }
- sch->qstats.drops++;
- cl->qstats.drops++;
- return ret;
- }
- /* Overlimit actions */
- /* TC_CBQ_OVL_CLASSIC: (default) penalize leaf class by adding offtime */
- static void cbq_ovl_classic(struct cbq_class *cl)
- {
- struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
- psched_tdiff_t delay = PSCHED_TDIFF(cl->undertime, q->now);
- if (!cl->delayed) {
- delay += cl->offtime;
- /*
- Class goes to sleep, so that it will have no
- chance to work avgidle. Let's forgive it 8)
- BTW cbq-2.0 has a crap in this
- place, apparently they forgot to shift it by cl->ewma_log.
- */
- if (cl->avgidle < 0)
- delay -= (-cl->avgidle) - ((-cl->avgidle) >> cl->ewma_log);
- if (cl->avgidle < cl->minidle)
- cl->avgidle = cl->minidle;
- if (delay <= 0)
- delay = 1;
- PSCHED_TADD2(q->now, delay, cl->undertime);
- cl->xstats.overactions++;
- cl->delayed = 1;
- }
- if (q->wd_expires == 0 || q->wd_expires > delay)
- q->wd_expires = delay;
- /* Dirty work! We must schedule wakeups based on
- real available rate, rather than leaf rate,
- which may be tiny (even zero).
- */
- if (q->toplevel == TC_CBQ_MAXLEVEL) {
- struct cbq_class *b;
- psched_tdiff_t base_delay = q->wd_expires;
- for (b = cl->borrow; b; b = b->borrow) {
- delay = PSCHED_TDIFF(b->undertime, q->now);
- if (delay < base_delay) {
- if (delay <= 0)
- delay = 1;
- base_delay = delay;
- }
- }
- q->wd_expires = base_delay;
- }
- }
- /* TC_CBQ_OVL_RCLASSIC: penalize by offtime classes in hierarchy, when
- they go overlimit
- */
- static void cbq_ovl_rclassic(struct cbq_class *cl)
- {
- struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
- struct cbq_class *this = cl;
- do {
- if (cl->level > q->toplevel) {
- cl = NULL;
- break;
- }
- } while ((cl = cl->borrow) != NULL);
- if (cl == NULL)
- cl = this;
- cbq_ovl_classic(cl);
- }
- /* TC_CBQ_OVL_DELAY: delay until it will go to underlimit */
- static void cbq_ovl_delay(struct cbq_class *cl)
- {
- struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
- psched_tdiff_t delay = PSCHED_TDIFF(cl->undertime, q->now);
- if (!cl->delayed) {
- unsigned long sched = jiffies;
- delay += cl->offtime;
- if (cl->avgidle < 0)
- delay -= (-cl->avgidle) - ((-cl->avgidle) >> cl->ewma_log);
- if (cl->avgidle < cl->minidle)
- cl->avgidle = cl->minidle;
- PSCHED_TADD2(q->now, delay, cl->undertime);
- if (delay > 0) {
- sched += PSCHED_US2JIFFIE(delay) + cl->penalty;
- cl->penalized = sched;
- cl->cpriority = TC_CBQ_MAXPRIO;
- q->pmask |= (1<<TC_CBQ_MAXPRIO);
- if (del_timer(&q->delay_timer) &&
- (long)(q->delay_timer.expires - sched) > 0)
- q->delay_timer.expires = sched;
- add_timer(&q->delay_timer);
- cl->delayed = 1;
- cl->xstats.overactions++;
- return;
- }
- delay = 1;
- }
- if (q->wd_expires == 0 || q->wd_expires > delay)
- q->wd_expires = delay;
- }
- /* TC_CBQ_OVL_LOWPRIO: penalize class by lowering its priority band */
- static void cbq_ovl_lowprio(struct cbq_class *cl)
- {
- struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
- cl->penalized = jiffies + cl->penalty;
- if (cl->cpriority != cl->priority2) {
- cl->cpriority = cl->priority2;
- q->pmask |= (1<<cl->cpriority);
- cl->xstats.overactions++;
- }
- cbq_ovl_classic(cl);
- }
- /* TC_CBQ_OVL_DROP: penalize class by dropping */
- static void cbq_ovl_drop(struct cbq_class *cl)
- {
- if (cl->q->ops->drop)
- if (cl->q->ops->drop(cl->q))
- cl->qdisc->q.qlen--;
- cl->xstats.overactions++;
- cbq_ovl_classic(cl);
- }
- static void cbq_watchdog(unsigned long arg)
- {
- struct Qdisc *sch = (struct Qdisc*)arg;
- sch->flags &= ~TCQ_F_THROTTLED;
- netif_schedule(sch->dev);
- }
- static unsigned long cbq_undelay_prio(struct cbq_sched_data *q, int prio)
- {
- struct cbq_class *cl;
- struct cbq_class *cl_prev = q->active[prio];
- unsigned long now = jiffies;
- unsigned long sched = now;
- if (cl_prev == NULL)
- return now;
- do {
- cl = cl_prev->next_alive;
- if ((long)(now - cl->penalized) > 0) {
- cl_prev->next_alive = cl->next_alive;
- cl->next_alive = NULL;
- cl->cpriority = cl->priority;
- cl->delayed = 0;
- cbq_activate_class(cl);
- if (cl == q->active[prio]) {
- q->active[prio] = cl_prev;
- if (cl == q->active[prio]) {
- q->active[prio] = NULL;
- return 0;
- }
- }
- cl = cl_prev->next_alive;
- } else if ((long)(sched - cl->penalized) > 0)
- sched = cl->penalized;
- } while ((cl_prev = cl) != q->active[prio]);
- return (long)(sched - now);
- }
- static void cbq_undelay(unsigned long arg)
- {
- struct Qdisc *sch = (struct Qdisc*)arg;
- struct cbq_sched_data *q = qdisc_priv(sch);
- long delay = 0;
- unsigned pmask;
- pmask = q->pmask;
- q->pmask = 0;
- while (pmask) {
- int prio = ffz(~pmask);
- long tmp;
- pmask &= ~(1<<prio);
- tmp = cbq_undelay_prio(q, prio);
- if (tmp > 0) {
- q->pmask |= 1<<prio;
- if (tmp < delay || delay == 0)
- delay = tmp;
- }
- }
- if (delay) {
- q->delay_timer.expires = jiffies + delay;
- add_timer(&q->delay_timer);
- }
- sch->flags &= ~TCQ_F_THROTTLED;
- netif_schedule(sch->dev);
- }
- #ifdef CONFIG_NET_CLS_POLICE
- static int cbq_reshape_fail(struct sk_buff *skb, struct Qdisc *child)
- {
- int len = skb->len;
- struct Qdisc *sch = child->__parent;
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl = q->rx_class;
- q->rx_class = NULL;
- if (cl && (cl = cbq_reclassify(skb, cl)) != NULL) {
- cbq_mark_toplevel(q, cl);
- q->rx_class = cl;
- cl->q->__parent = sch;
- if (cl->q->enqueue(skb, cl->q) == 0) {
- sch->q.qlen++;
- sch->bstats.packets++;
- sch->bstats.bytes+=len;
- if (!cl->next_alive)
- cbq_activate_class(cl);
- return 0;
- }
- sch->qstats.drops++;
- return 0;
- }
- sch->qstats.drops++;
- return -1;
- }
- #endif
- /*
- It is mission critical procedure.
- We "regenerate" toplevel cutoff, if transmitting class
- has backlog and it is not regulated. It is not part of
- original CBQ description, but looks more reasonable.
- Probably, it is wrong. This question needs further investigation.
- */
- static __inline__ void
- cbq_update_toplevel(struct cbq_sched_data *q, struct cbq_class *cl,
- struct cbq_class *borrowed)
- {
- if (cl && q->toplevel >= borrowed->level) {
- if (cl->q->q.qlen > 1) {
- do {
- if (PSCHED_IS_PASTPERFECT(borrowed->undertime)) {
- q->toplevel = borrowed->level;
- return;
- }
- } while ((borrowed=borrowed->borrow) != NULL);
- }
- #if 0
- /* It is not necessary now. Uncommenting it
- will save CPU cycles, but decrease fairness.
- */
- q->toplevel = TC_CBQ_MAXLEVEL;
- #endif
- }
- }
- static void
- cbq_update(struct cbq_sched_data *q)
- {
- struct cbq_class *this = q->tx_class;
- struct cbq_class *cl = this;
- int len = q->tx_len;
- q->tx_class = NULL;
- for ( ; cl; cl = cl->share) {
- long avgidle = cl->avgidle;
- long idle;
- cl->bstats.packets++;
- cl->bstats.bytes += len;
- /*
- (now - last) is total time between packet right edges.
- (last_pktlen/rate) is "virtual" busy time, so that
- idle = (now - last) - last_pktlen/rate
- */
- idle = PSCHED_TDIFF(q->now, cl->last);
- if ((unsigned long)idle > 128*1024*1024) {
- avgidle = cl->maxidle;
- } else {
- idle -= L2T(cl, len);
- /* true_avgidle := (1-W)*true_avgidle + W*idle,
- where W=2^{-ewma_log}. But cl->avgidle is scaled:
- cl->avgidle == true_avgidle/W,
- hence:
- */
- avgidle += idle - (avgidle>>cl->ewma_log);
- }
- if (avgidle <= 0) {
- /* Overlimit or at-limit */
- if (avgidle < cl->minidle)
- avgidle = cl->minidle;
- cl->avgidle = avgidle;
- /* Calculate expected time, when this class
- will be allowed to send.
- It will occur, when:
- (1-W)*true_avgidle + W*delay = 0, i.e.
- idle = (1/W - 1)*(-true_avgidle)
- or
- idle = (1 - W)*(-cl->avgidle);
- */
- idle = (-avgidle) - ((-avgidle) >> cl->ewma_log);
- /*
- That is not all.
- To maintain the rate allocated to the class,
- we add to undertime virtual clock,
- necessary to complete transmitted packet.
- (len/phys_bandwidth has been already passed
- to the moment of cbq_update)
- */
- idle -= L2T(&q->link, len);
- idle += L2T(cl, len);
- PSCHED_AUDIT_TDIFF(idle);
- PSCHED_TADD2(q->now, idle, cl->undertime);
- } else {
- /* Underlimit */
- PSCHED_SET_PASTPERFECT(cl->undertime);
- if (avgidle > cl->maxidle)
- cl->avgidle = cl->maxidle;
- else
- cl->avgidle = avgidle;
- }
- cl->last = q->now;
- }
- cbq_update_toplevel(q, this, q->tx_borrowed);
- }
- static __inline__ struct cbq_class *
- cbq_under_limit(struct cbq_class *cl)
- {
- struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
- struct cbq_class *this_cl = cl;
- if (cl->tparent == NULL)
- return cl;
- if (PSCHED_IS_PASTPERFECT(cl->undertime) ||
- !PSCHED_TLESS(q->now, cl->undertime)) {
- cl->delayed = 0;
- return cl;
- }
- do {
- /* It is very suspicious place. Now overlimit
- action is generated for not bounded classes
- only if link is completely congested.
- Though it is in agree with ancestor-only paradigm,
- it looks very stupid. Particularly,
- it means that this chunk of code will either
- never be called or result in strong amplification
- of burstiness. Dangerous, silly, and, however,
- no another solution exists.
- */
- if ((cl = cl->borrow) == NULL) {
- this_cl->qstats.overlimits++;
- this_cl->overlimit(this_cl);
- return NULL;
- }
- if (cl->level > q->toplevel)
- return NULL;
- } while (!PSCHED_IS_PASTPERFECT(cl->undertime) &&
- PSCHED_TLESS(q->now, cl->undertime));
- cl->delayed = 0;
- return cl;
- }
- static __inline__ struct sk_buff *
- cbq_dequeue_prio(struct Qdisc *sch, int prio)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl_tail, *cl_prev, *cl;
- struct sk_buff *skb;
- int deficit;
- cl_tail = cl_prev = q->active[prio];
- cl = cl_prev->next_alive;
- do {
- deficit = 0;
- /* Start round */
- do {
- struct cbq_class *borrow = cl;
- if (cl->q->q.qlen &&
- (borrow = cbq_under_limit(cl)) == NULL)
- goto skip_class;
- if (cl->deficit <= 0) {
- /* Class exhausted its allotment per
- this round. Switch to the next one.
- */
- deficit = 1;
- cl->deficit += cl->quantum;
- goto next_class;
- }
- skb = cl->q->dequeue(cl->q);
- /* Class did not give us any skb :-(
- It could occur even if cl->q->q.qlen != 0
- f.e. if cl->q == "tbf"
- */
- if (skb == NULL)
- goto skip_class;
- cl->deficit -= skb->len;
- q->tx_class = cl;
- q->tx_borrowed = borrow;
- if (borrow != cl) {
- #ifndef CBQ_XSTATS_BORROWS_BYTES
- borrow->xstats.borrows++;
- cl->xstats.borrows++;
- #else
- borrow->xstats.borrows += skb->len;
- cl->xstats.borrows += skb->len;
- #endif
- }
- q->tx_len = skb->len;
- if (cl->deficit <= 0) {
- q->active[prio] = cl;
- cl = cl->next_alive;
- cl->deficit += cl->quantum;
- }
- return skb;
- skip_class:
- if (cl->q->q.qlen == 0 || prio != cl->cpriority) {
- /* Class is empty or penalized.
- Unlink it from active chain.
- */
- cl_prev->next_alive = cl->next_alive;
- cl->next_alive = NULL;
- /* Did cl_tail point to it? */
- if (cl == cl_tail) {
- /* Repair it! */
- cl_tail = cl_prev;
- /* Was it the last class in this band? */
- if (cl == cl_tail) {
- /* Kill the band! */
- q->active[prio] = NULL;
- q->activemask &= ~(1<<prio);
- if (cl->q->q.qlen)
- cbq_activate_class(cl);
- return NULL;
- }
- q->active[prio] = cl_tail;
- }
- if (cl->q->q.qlen)
- cbq_activate_class(cl);
- cl = cl_prev;
- }
- next_class:
- cl_prev = cl;
- cl = cl->next_alive;
- } while (cl_prev != cl_tail);
- } while (deficit);
- q->active[prio] = cl_prev;
- return NULL;
- }
- static __inline__ struct sk_buff *
- cbq_dequeue_1(struct Qdisc *sch)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct sk_buff *skb;
- unsigned activemask;
- activemask = q->activemask&0xFF;
- while (activemask) {
- int prio = ffz(~activemask);
- activemask &= ~(1<<prio);
- skb = cbq_dequeue_prio(sch, prio);
- if (skb)
- return skb;
- }
- return NULL;
- }
- static struct sk_buff *
- cbq_dequeue(struct Qdisc *sch)
- {
- struct sk_buff *skb;
- struct cbq_sched_data *q = qdisc_priv(sch);
- psched_time_t now;
- psched_tdiff_t incr;
- PSCHED_GET_TIME(now);
- incr = PSCHED_TDIFF(now, q->now_rt);
- if (q->tx_class) {
- psched_tdiff_t incr2;
- /* Time integrator. We calculate EOS time
- by adding expected packet transmission time.
- If real time is greater, we warp artificial clock,
- so that:
- cbq_time = max(real_time, work);
- */
- incr2 = L2T(&q->link, q->tx_len);
- PSCHED_TADD(q->now, incr2);
- cbq_update(q);
- if ((incr -= incr2) < 0)
- incr = 0;
- }
- PSCHED_TADD(q->now, incr);
- q->now_rt = now;
- for (;;) {
- q->wd_expires = 0;
- skb = cbq_dequeue_1(sch);
- if (skb) {
- sch->q.qlen--;
- sch->flags &= ~TCQ_F_THROTTLED;
- return skb;
- }
- /* All the classes are overlimit.
- It is possible, if:
- 1. Scheduler is empty.
- 2. Toplevel cutoff inhibited borrowing.
- 3. Root class is overlimit.
- Reset 2d and 3d conditions and retry.
- Note, that NS and cbq-2.0 are buggy, peeking
- an arbitrary class is appropriate for ancestor-only
- sharing, but not for toplevel algorithm.
- Our version is better, but slower, because it requires
- two passes, but it is unavoidable with top-level sharing.
- */
- if (q->toplevel == TC_CBQ_MAXLEVEL &&
- PSCHED_IS_PASTPERFECT(q->link.undertime))
- break;
- q->toplevel = TC_CBQ_MAXLEVEL;
- PSCHED_SET_PASTPERFECT(q->link.undertime);
- }
- /* No packets in scheduler or nobody wants to give them to us :-(
- Sigh... start watchdog timer in the last case. */
- if (sch->q.qlen) {
- sch->qstats.overlimits++;
- if (q->wd_expires) {
- long delay = PSCHED_US2JIFFIE(q->wd_expires);
- if (delay <= 0)
- delay = 1;
- mod_timer(&q->wd_timer, jiffies + delay);
- sch->flags |= TCQ_F_THROTTLED;
- }
- }
- return NULL;
- }
- /* CBQ class maintanance routines */
- static void cbq_adjust_levels(struct cbq_class *this)
- {
- if (this == NULL)
- return;
- do {
- int level = 0;
- struct cbq_class *cl;
- if ((cl = this->children) != NULL) {
- do {
- if (cl->level > level)
- level = cl->level;
- } while ((cl = cl->sibling) != this->children);
- }
- this->level = level+1;
- } while ((this = this->tparent) != NULL);
- }
- static void cbq_normalize_quanta(struct cbq_sched_data *q, int prio)
- {
- struct cbq_class *cl;
- unsigned h;
- if (q->quanta[prio] == 0)
- return;
- for (h=0; h<16; h++) {
- for (cl = q->classes[h]; cl; cl = cl->next) {
- /* BUGGGG... Beware! This expression suffer of
- arithmetic overflows!
- */
- if (cl->priority == prio) {
- cl->quantum = (cl->weight*cl->allot*q->nclasses[prio])/
- q->quanta[prio];
- }
- if (cl->quantum <= 0 || cl->quantum>32*cl->qdisc->dev->mtu) {
- printk(KERN_WARNING "CBQ: class %08x has bad quantum==%ld, repaired.\n", cl->classid, cl->quantum);
- cl->quantum = cl->qdisc->dev->mtu/2 + 1;
- }
- }
- }
- }
- static void cbq_sync_defmap(struct cbq_class *cl)
- {
- struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
- struct cbq_class *split = cl->split;
- unsigned h;
- int i;
- if (split == NULL)
- return;
- for (i=0; i<=TC_PRIO_MAX; i++) {
- if (split->defaults[i] == cl && !(cl->defmap&(1<<i)))
- split->defaults[i] = NULL;
- }
- for (i=0; i<=TC_PRIO_MAX; i++) {
- int level = split->level;
- if (split->defaults[i])
- continue;
- for (h=0; h<16; h++) {
- struct cbq_class *c;
- for (c = q->classes[h]; c; c = c->next) {
- if (c->split == split && c->level < level &&
- c->defmap&(1<<i)) {
- split->defaults[i] = c;
- level = c->level;
- }
- }
- }
- }
- }
- static void cbq_change_defmap(struct cbq_class *cl, u32 splitid, u32 def, u32 mask)
- {
- struct cbq_class *split = NULL;
- if (splitid == 0) {
- if ((split = cl->split) == NULL)
- return;
- splitid = split->classid;
- }
- if (split == NULL || split->classid != splitid) {
- for (split = cl->tparent; split; split = split->tparent)
- if (split->classid == splitid)
- break;
- }
- if (split == NULL)
- return;
- if (cl->split != split) {
- cl->defmap = 0;
- cbq_sync_defmap(cl);
- cl->split = split;
- cl->defmap = def&mask;
- } else
- cl->defmap = (cl->defmap&~mask)|(def&mask);
- cbq_sync_defmap(cl);
- }
- static void cbq_unlink_class(struct cbq_class *this)
- {
- struct cbq_class *cl, **clp;
- struct cbq_sched_data *q = qdisc_priv(this->qdisc);
- for (clp = &q->classes[cbq_hash(this->classid)]; (cl = *clp) != NULL; clp = &cl->next) {
- if (cl == this) {
- *clp = cl->next;
- cl->next = NULL;
- break;
- }
- }
- if (this->tparent) {
- clp=&this->sibling;
- cl = *clp;
- do {
- if (cl == this) {
- *clp = cl->sibling;
- break;
- }
- clp = &cl->sibling;
- } while ((cl = *clp) != this->sibling);
- if (this->tparent->children == this) {
- this->tparent->children = this->sibling;
- if (this->sibling == this)
- this->tparent->children = NULL;
- }
- } else {
- BUG_TRAP(this->sibling == this);
- }
- }
- static void cbq_link_class(struct cbq_class *this)
- {
- struct cbq_sched_data *q = qdisc_priv(this->qdisc);
- unsigned h = cbq_hash(this->classid);
- struct cbq_class *parent = this->tparent;
- this->sibling = this;
- this->next = q->classes[h];
- q->classes[h] = this;
- if (parent == NULL)
- return;
- if (parent->children == NULL) {
- parent->children = this;
- } else {
- this->sibling = parent->children->sibling;
- parent->children->sibling = this;
- }
- }
- static unsigned int cbq_drop(struct Qdisc* sch)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl, *cl_head;
- int prio;
- unsigned int len;
- for (prio = TC_CBQ_MAXPRIO; prio >= 0; prio--) {
- if ((cl_head = q->active[prio]) == NULL)
- continue;
- cl = cl_head;
- do {
- if (cl->q->ops->drop && (len = cl->q->ops->drop(cl->q))) {
- sch->q.qlen--;
- return len;
- }
- } while ((cl = cl->next_alive) != cl_head);
- }
- return 0;
- }
- static void
- cbq_reset(struct Qdisc* sch)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl;
- int prio;
- unsigned h;
- q->activemask = 0;
- q->pmask = 0;
- q->tx_class = NULL;
- q->tx_borrowed = NULL;
- del_timer(&q->wd_timer);
- del_timer(&q->delay_timer);
- q->toplevel = TC_CBQ_MAXLEVEL;
- PSCHED_GET_TIME(q->now);
- q->now_rt = q->now;
- for (prio = 0; prio <= TC_CBQ_MAXPRIO; prio++)
- q->active[prio] = NULL;
- for (h = 0; h < 16; h++) {
- for (cl = q->classes[h]; cl; cl = cl->next) {
- qdisc_reset(cl->q);
- cl->next_alive = NULL;
- PSCHED_SET_PASTPERFECT(cl->undertime);
- cl->avgidle = cl->maxidle;
- cl->deficit = cl->quantum;
- cl->cpriority = cl->priority;
- }
- }
- sch->q.qlen = 0;
- }
- static int cbq_set_lss(struct cbq_class *cl, struct tc_cbq_lssopt *lss)
- {
- if (lss->change&TCF_CBQ_LSS_FLAGS) {
- cl->share = (lss->flags&TCF_CBQ_LSS_ISOLATED) ? NULL : cl->tparent;
- cl->borrow = (lss->flags&TCF_CBQ_LSS_BOUNDED) ? NULL : cl->tparent;
- }
- if (lss->change&TCF_CBQ_LSS_EWMA)
- cl->ewma_log = lss->ewma_log;
- if (lss->change&TCF_CBQ_LSS_AVPKT)
- cl->avpkt = lss->avpkt;
- if (lss->change&TCF_CBQ_LSS_MINIDLE)
- cl->minidle = -(long)lss->minidle;
- if (lss->change&TCF_CBQ_LSS_MAXIDLE) {
- cl->maxidle = lss->maxidle;
- cl->avgidle = lss->maxidle;
- }
- if (lss->change&TCF_CBQ_LSS_OFFTIME)
- cl->offtime = lss->offtime;
- return 0;
- }
- static void cbq_rmprio(struct cbq_sched_data *q, struct cbq_class *cl)
- {
- q->nclasses[cl->priority]--;
- q->quanta[cl->priority] -= cl->weight;
- cbq_normalize_quanta(q, cl->priority);
- }
- static void cbq_addprio(struct cbq_sched_data *q, struct cbq_class *cl)
- {
- q->nclasses[cl->priority]++;
- q->quanta[cl->priority] += cl->weight;
- cbq_normalize_quanta(q, cl->priority);
- }
- static int cbq_set_wrr(struct cbq_class *cl, struct tc_cbq_wrropt *wrr)
- {
- struct cbq_sched_data *q = qdisc_priv(cl->qdisc);
- if (wrr->allot)
- cl->allot = wrr->allot;
- if (wrr->weight)
- cl->weight = wrr->weight;
- if (wrr->priority) {
- cl->priority = wrr->priority-1;
- cl->cpriority = cl->priority;
- if (cl->priority >= cl->priority2)
- cl->priority2 = TC_CBQ_MAXPRIO-1;
- }
- cbq_addprio(q, cl);
- return 0;
- }
- static int cbq_set_overlimit(struct cbq_class *cl, struct tc_cbq_ovl *ovl)
- {
- switch (ovl->strategy) {
- case TC_CBQ_OVL_CLASSIC:
- cl->overlimit = cbq_ovl_classic;
- break;
- case TC_CBQ_OVL_DELAY:
- cl->overlimit = cbq_ovl_delay;
- break;
- case TC_CBQ_OVL_LOWPRIO:
- if (ovl->priority2-1 >= TC_CBQ_MAXPRIO ||
- ovl->priority2-1 <= cl->priority)
- return -EINVAL;
- cl->priority2 = ovl->priority2-1;
- cl->overlimit = cbq_ovl_lowprio;
- break;
- case TC_CBQ_OVL_DROP:
- cl->overlimit = cbq_ovl_drop;
- break;
- case TC_CBQ_OVL_RCLASSIC:
- cl->overlimit = cbq_ovl_rclassic;
- break;
- default:
- return -EINVAL;
- }
- cl->penalty = (ovl->penalty*HZ)/1000;
- return 0;
- }
- #ifdef CONFIG_NET_CLS_POLICE
- static int cbq_set_police(struct cbq_class *cl, struct tc_cbq_police *p)
- {
- cl->police = p->police;
- if (cl->q->handle) {
- if (p->police == TC_POLICE_RECLASSIFY)
- cl->q->reshape_fail = cbq_reshape_fail;
- else
- cl->q->reshape_fail = NULL;
- }
- return 0;
- }
- #endif
- static int cbq_set_fopt(struct cbq_class *cl, struct tc_cbq_fopt *fopt)
- {
- cbq_change_defmap(cl, fopt->split, fopt->defmap, fopt->defchange);
- return 0;
- }
- static int cbq_init(struct Qdisc *sch, struct rtattr *opt)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct rtattr *tb[TCA_CBQ_MAX];
- struct tc_ratespec *r;
- if (rtattr_parse_nested(tb, TCA_CBQ_MAX, opt) < 0 ||
- tb[TCA_CBQ_RTAB-1] == NULL || tb[TCA_CBQ_RATE-1] == NULL ||
- RTA_PAYLOAD(tb[TCA_CBQ_RATE-1]) < sizeof(struct tc_ratespec))
- return -EINVAL;
- if (tb[TCA_CBQ_LSSOPT-1] &&
- RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT-1]) < sizeof(struct tc_cbq_lssopt))
- return -EINVAL;
- r = RTA_DATA(tb[TCA_CBQ_RATE-1]);
- if ((q->link.R_tab = qdisc_get_rtab(r, tb[TCA_CBQ_RTAB-1])) == NULL)
- return -EINVAL;
- q->link.refcnt = 1;
- q->link.sibling = &q->link;
- q->link.classid = sch->handle;
- q->link.qdisc = sch;
- if (!(q->link.q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops)))
- q->link.q = &noop_qdisc;
- q->link.priority = TC_CBQ_MAXPRIO-1;
- q->link.priority2 = TC_CBQ_MAXPRIO-1;
- q->link.cpriority = TC_CBQ_MAXPRIO-1;
- q->link.ovl_strategy = TC_CBQ_OVL_CLASSIC;
- q->link.overlimit = cbq_ovl_classic;
- q->link.allot = psched_mtu(sch->dev);
- q->link.quantum = q->link.allot;
- q->link.weight = q->link.R_tab->rate.rate;
- q->link.ewma_log = TC_CBQ_DEF_EWMA;
- q->link.avpkt = q->link.allot/2;
- q->link.minidle = -0x7FFFFFFF;
- q->link.stats_lock = &sch->dev->queue_lock;
- init_timer(&q->wd_timer);
- q->wd_timer.data = (unsigned long)sch;
- q->wd_timer.function = cbq_watchdog;
- init_timer(&q->delay_timer);
- q->delay_timer.data = (unsigned long)sch;
- q->delay_timer.function = cbq_undelay;
- q->toplevel = TC_CBQ_MAXLEVEL;
- PSCHED_GET_TIME(q->now);
- q->now_rt = q->now;
- cbq_link_class(&q->link);
- if (tb[TCA_CBQ_LSSOPT-1])
- cbq_set_lss(&q->link, RTA_DATA(tb[TCA_CBQ_LSSOPT-1]));
- cbq_addprio(q, &q->link);
- return 0;
- }
- static __inline__ int cbq_dump_rate(struct sk_buff *skb, struct cbq_class *cl)
- {
- unsigned char *b = skb->tail;
- RTA_PUT(skb, TCA_CBQ_RATE, sizeof(cl->R_tab->rate), &cl->R_tab->rate);
- return skb->len;
- rtattr_failure:
- skb_trim(skb, b - skb->data);
- return -1;
- }
- static __inline__ int cbq_dump_lss(struct sk_buff *skb, struct cbq_class *cl)
- {
- unsigned char *b = skb->tail;
- struct tc_cbq_lssopt opt;
- opt.flags = 0;
- if (cl->borrow == NULL)
- opt.flags |= TCF_CBQ_LSS_BOUNDED;
- if (cl->share == NULL)
- opt.flags |= TCF_CBQ_LSS_ISOLATED;
- opt.ewma_log = cl->ewma_log;
- opt.level = cl->level;
- opt.avpkt = cl->avpkt;
- opt.maxidle = cl->maxidle;
- opt.minidle = (u32)(-cl->minidle);
- opt.offtime = cl->offtime;
- opt.change = ~0;
- RTA_PUT(skb, TCA_CBQ_LSSOPT, sizeof(opt), &opt);
- return skb->len;
- rtattr_failure:
- skb_trim(skb, b - skb->data);
- return -1;
- }
- static __inline__ int cbq_dump_wrr(struct sk_buff *skb, struct cbq_class *cl)
- {
- unsigned char *b = skb->tail;
- struct tc_cbq_wrropt opt;
- opt.flags = 0;
- opt.allot = cl->allot;
- opt.priority = cl->priority+1;
- opt.cpriority = cl->cpriority+1;
- opt.weight = cl->weight;
- RTA_PUT(skb, TCA_CBQ_WRROPT, sizeof(opt), &opt);
- return skb->len;
- rtattr_failure:
- skb_trim(skb, b - skb->data);
- return -1;
- }
- static __inline__ int cbq_dump_ovl(struct sk_buff *skb, struct cbq_class *cl)
- {
- unsigned char *b = skb->tail;
- struct tc_cbq_ovl opt;
- opt.strategy = cl->ovl_strategy;
- opt.priority2 = cl->priority2+1;
- opt.pad = 0;
- opt.penalty = (cl->penalty*1000)/HZ;
- RTA_PUT(skb, TCA_CBQ_OVL_STRATEGY, sizeof(opt), &opt);
- return skb->len;
- rtattr_failure:
- skb_trim(skb, b - skb->data);
- return -1;
- }
- static __inline__ int cbq_dump_fopt(struct sk_buff *skb, struct cbq_class *cl)
- {
- unsigned char *b = skb->tail;
- struct tc_cbq_fopt opt;
- if (cl->split || cl->defmap) {
- opt.split = cl->split ? cl->split->classid : 0;
- opt.defmap = cl->defmap;
- opt.defchange = ~0;
- RTA_PUT(skb, TCA_CBQ_FOPT, sizeof(opt), &opt);
- }
- return skb->len;
- rtattr_failure:
- skb_trim(skb, b - skb->data);
- return -1;
- }
- #ifdef CONFIG_NET_CLS_POLICE
- static __inline__ int cbq_dump_police(struct sk_buff *skb, struct cbq_class *cl)
- {
- unsigned char *b = skb->tail;
- struct tc_cbq_police opt;
- if (cl->police) {
- opt.police = cl->police;
- opt.__res1 = 0;
- opt.__res2 = 0;
- RTA_PUT(skb, TCA_CBQ_POLICE, sizeof(opt), &opt);
- }
- return skb->len;
- rtattr_failure:
- skb_trim(skb, b - skb->data);
- return -1;
- }
- #endif
- static int cbq_dump_attr(struct sk_buff *skb, struct cbq_class *cl)
- {
- if (cbq_dump_lss(skb, cl) < 0 ||
- cbq_dump_rate(skb, cl) < 0 ||
- cbq_dump_wrr(skb, cl) < 0 ||
- cbq_dump_ovl(skb, cl) < 0 ||
- #ifdef CONFIG_NET_CLS_POLICE
- cbq_dump_police(skb, cl) < 0 ||
- #endif
- cbq_dump_fopt(skb, cl) < 0)
- return -1;
- return 0;
- }
- static int cbq_dump(struct Qdisc *sch, struct sk_buff *skb)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- unsigned char *b = skb->tail;
- struct rtattr *rta;
- rta = (struct rtattr*)b;
- RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
- if (cbq_dump_attr(skb, &q->link) < 0)
- goto rtattr_failure;
- rta->rta_len = skb->tail - b;
- return skb->len;
- rtattr_failure:
- skb_trim(skb, b - skb->data);
- return -1;
- }
- static int
- cbq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- q->link.xstats.avgidle = q->link.avgidle;
- return gnet_stats_copy_app(d, &q->link.xstats, sizeof(q->link.xstats));
- }
- static int
- cbq_dump_class(struct Qdisc *sch, unsigned long arg,
- struct sk_buff *skb, struct tcmsg *tcm)
- {
- struct cbq_class *cl = (struct cbq_class*)arg;
- unsigned char *b = skb->tail;
- struct rtattr *rta;
- if (cl->tparent)
- tcm->tcm_parent = cl->tparent->classid;
- else
- tcm->tcm_parent = TC_H_ROOT;
- tcm->tcm_handle = cl->classid;
- tcm->tcm_info = cl->q->handle;
- rta = (struct rtattr*)b;
- RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
- if (cbq_dump_attr(skb, cl) < 0)
- goto rtattr_failure;
- rta->rta_len = skb->tail - b;
- return skb->len;
- rtattr_failure:
- skb_trim(skb, b - skb->data);
- return -1;
- }
- static int
- cbq_dump_class_stats(struct Qdisc *sch, unsigned long arg,
- struct gnet_dump *d)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl = (struct cbq_class*)arg;
- cl->qstats.qlen = cl->q->q.qlen;
- cl->xstats.avgidle = cl->avgidle;
- cl->xstats.undertime = 0;
- if (!PSCHED_IS_PASTPERFECT(cl->undertime))
- cl->xstats.undertime = PSCHED_TDIFF(cl->undertime, q->now);
- if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
- #ifdef CONFIG_NET_ESTIMATOR
- gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
- #endif
- gnet_stats_copy_queue(d, &cl->qstats) < 0)
- return -1;
- return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
- }
- static int cbq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
- struct Qdisc **old)
- {
- struct cbq_class *cl = (struct cbq_class*)arg;
- if (cl) {
- if (new == NULL) {
- if ((new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops)) == NULL)
- return -ENOBUFS;
- } else {
- #ifdef CONFIG_NET_CLS_POLICE
- if (cl->police == TC_POLICE_RECLASSIFY)
- new->reshape_fail = cbq_reshape_fail;
- #endif
- }
- sch_tree_lock(sch);
- *old = cl->q;
- cl->q = new;
- sch->q.qlen -= (*old)->q.qlen;
- qdisc_reset(*old);
- sch_tree_unlock(sch);
- return 0;
- }
- return -ENOENT;
- }
- static struct Qdisc *
- cbq_leaf(struct Qdisc *sch, unsigned long arg)
- {
- struct cbq_class *cl = (struct cbq_class*)arg;
- return cl ? cl->q : NULL;
- }
- static unsigned long cbq_get(struct Qdisc *sch, u32 classid)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl = cbq_class_lookup(q, classid);
- if (cl) {
- cl->refcnt++;
- return (unsigned long)cl;
- }
- return 0;
- }
- static void cbq_destroy_filters(struct cbq_class *cl)
- {
- struct tcf_proto *tp;
- while ((tp = cl->filter_list) != NULL) {
- cl->filter_list = tp->next;
- tcf_destroy(tp);
- }
- }
- static void cbq_destroy_class(struct Qdisc *sch, struct cbq_class *cl)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- BUG_TRAP(!cl->filters);
- cbq_destroy_filters(cl);
- qdisc_destroy(cl->q);
- qdisc_put_rtab(cl->R_tab);
- #ifdef CONFIG_NET_ESTIMATOR
- gen_kill_estimator(&cl->bstats, &cl->rate_est);
- #endif
- if (cl != &q->link)
- kfree(cl);
- }
- static void
- cbq_destroy(struct Qdisc* sch)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl;
- unsigned h;
- #ifdef CONFIG_NET_CLS_POLICE
- q->rx_class = NULL;
- #endif
- /*
- * Filters must be destroyed first because we don't destroy the
- * classes from root to leafs which means that filters can still
- * be bound to classes which have been destroyed already. --TGR '04
- */
- for (h = 0; h < 16; h++)
- for (cl = q->classes[h]; cl; cl = cl->next)
- cbq_destroy_filters(cl);
- for (h = 0; h < 16; h++) {
- struct cbq_class *next;
- for (cl = q->classes[h]; cl; cl = next) {
- next = cl->next;
- cbq_destroy_class(sch, cl);
- }
- }
- }
- static void cbq_put(struct Qdisc *sch, unsigned long arg)
- {
- struct cbq_class *cl = (struct cbq_class*)arg;
- if (--cl->refcnt == 0) {
- #ifdef CONFIG_NET_CLS_POLICE
- struct cbq_sched_data *q = qdisc_priv(sch);
- spin_lock_bh(&sch->dev->queue_lock);
- if (q->rx_class == cl)
- q->rx_class = NULL;
- spin_unlock_bh(&sch->dev->queue_lock);
- #endif
- cbq_destroy_class(sch, cl);
- }
- }
- static int
- cbq_change_class(struct Qdisc *sch, u32 classid, u32 parentid, struct rtattr **tca,
- unsigned long *arg)
- {
- int err;
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl = (struct cbq_class*)*arg;
- struct rtattr *opt = tca[TCA_OPTIONS-1];
- struct rtattr *tb[TCA_CBQ_MAX];
- struct cbq_class *parent;
- struct qdisc_rate_table *rtab = NULL;
- if (opt==NULL || rtattr_parse_nested(tb, TCA_CBQ_MAX, opt))
- return -EINVAL;
- if (tb[TCA_CBQ_OVL_STRATEGY-1] &&
- RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY-1]) < sizeof(struct tc_cbq_ovl))
- return -EINVAL;
- if (tb[TCA_CBQ_FOPT-1] &&
- RTA_PAYLOAD(tb[TCA_CBQ_FOPT-1]) < sizeof(struct tc_cbq_fopt))
- return -EINVAL;
- if (tb[TCA_CBQ_RATE-1] &&
- RTA_PAYLOAD(tb[TCA_CBQ_RATE-1]) < sizeof(struct tc_ratespec))
- return -EINVAL;
- if (tb[TCA_CBQ_LSSOPT-1] &&
- RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT-1]) < sizeof(struct tc_cbq_lssopt))
- return -EINVAL;
- if (tb[TCA_CBQ_WRROPT-1] &&
- RTA_PAYLOAD(tb[TCA_CBQ_WRROPT-1]) < sizeof(struct tc_cbq_wrropt))
- return -EINVAL;
- #ifdef CONFIG_NET_CLS_POLICE
- if (tb[TCA_CBQ_POLICE-1] &&
- RTA_PAYLOAD(tb[TCA_CBQ_POLICE-1]) < sizeof(struct tc_cbq_police))
- return -EINVAL;
- #endif
- if (cl) {
- /* Check parent */
- if (parentid) {
- if (cl->tparent && cl->tparent->classid != parentid)
- return -EINVAL;
- if (!cl->tparent && parentid != TC_H_ROOT)
- return -EINVAL;
- }
- if (tb[TCA_CBQ_RATE-1]) {
- rtab = qdisc_get_rtab(RTA_DATA(tb[TCA_CBQ_RATE-1]), tb[TCA_CBQ_RTAB-1]);
- if (rtab == NULL)
- return -EINVAL;
- }
- /* Change class parameters */
- sch_tree_lock(sch);
- if (cl->next_alive != NULL)
- cbq_deactivate_class(cl);
- if (rtab) {
- rtab = xchg(&cl->R_tab, rtab);
- qdisc_put_rtab(rtab);
- }
- if (tb[TCA_CBQ_LSSOPT-1])
- cbq_set_lss(cl, RTA_DATA(tb[TCA_CBQ_LSSOPT-1]));
- if (tb[TCA_CBQ_WRROPT-1]) {
- cbq_rmprio(q, cl);
- cbq_set_wrr(cl, RTA_DATA(tb[TCA_CBQ_WRROPT-1]));
- }
- if (tb[TCA_CBQ_OVL_STRATEGY-1])
- cbq_set_overlimit(cl, RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY-1]));
- #ifdef CONFIG_NET_CLS_POLICE
- if (tb[TCA_CBQ_POLICE-1])
- cbq_set_police(cl, RTA_DATA(tb[TCA_CBQ_POLICE-1]));
- #endif
- if (tb[TCA_CBQ_FOPT-1])
- cbq_set_fopt(cl, RTA_DATA(tb[TCA_CBQ_FOPT-1]));
- if (cl->q->q.qlen)
- cbq_activate_class(cl);
- sch_tree_unlock(sch);
- #ifdef CONFIG_NET_ESTIMATOR
- if (tca[TCA_RATE-1])
- gen_replace_estimator(&cl->bstats, &cl->rate_est,
- cl->stats_lock, tca[TCA_RATE-1]);
- #endif
- return 0;
- }
- if (parentid == TC_H_ROOT)
- return -EINVAL;
- if (tb[TCA_CBQ_WRROPT-1] == NULL || tb[TCA_CBQ_RATE-1] == NULL ||
- tb[TCA_CBQ_LSSOPT-1] == NULL)
- return -EINVAL;
- rtab = qdisc_get_rtab(RTA_DATA(tb[TCA_CBQ_RATE-1]), tb[TCA_CBQ_RTAB-1]);
- if (rtab == NULL)
- return -EINVAL;
- if (classid) {
- err = -EINVAL;
- if (TC_H_MAJ(classid^sch->handle) || cbq_class_lookup(q, classid))
- goto failure;
- } else {
- int i;
- classid = TC_H_MAKE(sch->handle,0x8000);
- for (i=0; i<0x8000; i++) {
- if (++q->hgenerator >= 0x8000)
- q->hgenerator = 1;
- if (cbq_class_lookup(q, classid|q->hgenerator) == NULL)
- break;
- }
- err = -ENOSR;
- if (i >= 0x8000)
- goto failure;
- classid = classid|q->hgenerator;
- }
- parent = &q->link;
- if (parentid) {
- parent = cbq_class_lookup(q, parentid);
- err = -EINVAL;
- if (parent == NULL)
- goto failure;
- }
- err = -ENOBUFS;
- cl = kmalloc(sizeof(*cl), GFP_KERNEL);
- if (cl == NULL)
- goto failure;
- memset(cl, 0, sizeof(*cl));
- cl->R_tab = rtab;
- rtab = NULL;
- cl->refcnt = 1;
- if (!(cl->q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops)))
- cl->q = &noop_qdisc;
- cl->classid = classid;
- cl->tparent = parent;
- cl->qdisc = sch;
- cl->allot = parent->allot;
- cl->quantum = cl->allot;
- cl->weight = cl->R_tab->rate.rate;
- cl->stats_lock = &sch->dev->queue_lock;
- sch_tree_lock(sch);
- cbq_link_class(cl);
- cl->borrow = cl->tparent;
- if (cl->tparent != &q->link)
- cl->share = cl->tparent;
- cbq_adjust_levels(parent);
- cl->minidle = -0x7FFFFFFF;
- cbq_set_lss(cl, RTA_DATA(tb[TCA_CBQ_LSSOPT-1]));
- cbq_set_wrr(cl, RTA_DATA(tb[TCA_CBQ_WRROPT-1]));
- if (cl->ewma_log==0)
- cl->ewma_log = q->link.ewma_log;
- if (cl->maxidle==0)
- cl->maxidle = q->link.maxidle;
- if (cl->avpkt==0)
- cl->avpkt = q->link.avpkt;
- cl->overlimit = cbq_ovl_classic;
- if (tb[TCA_CBQ_OVL_STRATEGY-1])
- cbq_set_overlimit(cl, RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY-1]));
- #ifdef CONFIG_NET_CLS_POLICE
- if (tb[TCA_CBQ_POLICE-1])
- cbq_set_police(cl, RTA_DATA(tb[TCA_CBQ_POLICE-1]));
- #endif
- if (tb[TCA_CBQ_FOPT-1])
- cbq_set_fopt(cl, RTA_DATA(tb[TCA_CBQ_FOPT-1]));
- sch_tree_unlock(sch);
- #ifdef CONFIG_NET_ESTIMATOR
- if (tca[TCA_RATE-1])
- gen_new_estimator(&cl->bstats, &cl->rate_est,
- cl->stats_lock, tca[TCA_RATE-1]);
- #endif
- *arg = (unsigned long)cl;
- return 0;
- failure:
- qdisc_put_rtab(rtab);
- return err;
- }
- static int cbq_delete(struct Qdisc *sch, unsigned long arg)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl = (struct cbq_class*)arg;
- if (cl->filters || cl->children || cl == &q->link)
- return -EBUSY;
- sch_tree_lock(sch);
- if (cl->next_alive)
- cbq_deactivate_class(cl);
- if (q->tx_borrowed == cl)
- q->tx_borrowed = q->tx_class;
- if (q->tx_class == cl) {
- q->tx_class = NULL;
- q->tx_borrowed = NULL;
- }
- #ifdef CONFIG_NET_CLS_POLICE
- if (q->rx_class == cl)
- q->rx_class = NULL;
- #endif
- cbq_unlink_class(cl);
- cbq_adjust_levels(cl->tparent);
- cl->defmap = 0;
- cbq_sync_defmap(cl);
- cbq_rmprio(q, cl);
- sch_tree_unlock(sch);
- if (--cl->refcnt == 0)
- cbq_destroy_class(sch, cl);
- return 0;
- }
- static struct tcf_proto **cbq_find_tcf(struct Qdisc *sch, unsigned long arg)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *cl = (struct cbq_class *)arg;
- if (cl == NULL)
- cl = &q->link;
- return &cl->filter_list;
- }
- static unsigned long cbq_bind_filter(struct Qdisc *sch, unsigned long parent,
- u32 classid)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- struct cbq_class *p = (struct cbq_class*)parent;
- struct cbq_class *cl = cbq_class_lookup(q, classid);
- if (cl) {
- if (p && p->level <= cl->level)
- return 0;
- cl->filters++;
- return (unsigned long)cl;
- }
- return 0;
- }
- static void cbq_unbind_filter(struct Qdisc *sch, unsigned long arg)
- {
- struct cbq_class *cl = (struct cbq_class*)arg;
- cl->filters--;
- }
- static void cbq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
- {
- struct cbq_sched_data *q = qdisc_priv(sch);
- unsigned h;
- if (arg->stop)
- return;
- for (h = 0; h < 16; h++) {
- struct cbq_class *cl;
- for (cl = q->classes[h]; cl; cl = cl->next) {
- if (arg->count < arg->skip) {
- arg->count++;
- continue;
- }
- if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
- arg->stop = 1;
- return;
- }
- arg->count++;
- }
- }
- }
- static struct Qdisc_class_ops cbq_class_ops = {
- .graft = cbq_graft,
- .leaf = cbq_leaf,
- .get = cbq_get,
- .put = cbq_put,
- .change = cbq_change_class,
- .delete = cbq_delete,
- .walk = cbq_walk,
- .tcf_chain = cbq_find_tcf,
- .bind_tcf = cbq_bind_filter,
- .unbind_tcf = cbq_unbind_filter,
- .dump = cbq_dump_class,
- .dump_stats = cbq_dump_class_stats,
- };
- static struct Qdisc_ops cbq_qdisc_ops = {
- .next = NULL,
- .cl_ops = &cbq_class_ops,
- .id = "cbq",
- .priv_size = sizeof(struct cbq_sched_data),
- .enqueue = cbq_enqueue,
- .dequeue = cbq_dequeue,
- .requeue = cbq_requeue,
- .drop = cbq_drop,
- .init = cbq_init,
- .reset = cbq_reset,
- .destroy = cbq_destroy,
- .change = NULL,
- .dump = cbq_dump,
- .dump_stats = cbq_dump_stats,
- .owner = THIS_MODULE,
- };
- static int __init cbq_module_init(void)
- {
- return register_qdisc(&cbq_qdisc_ops);
- }
- static void __exit cbq_module_exit(void)
- {
- unregister_qdisc(&cbq_qdisc_ops);
- }
- module_init(cbq_module_init)
- module_exit(cbq_module_exit)
- MODULE_LICENSE("GPL");
|