sch_htb.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/sch_htb.c Hierarchical token bucket, feed tree version
  4. *
  5. * Authors: Martin Devera, <[email protected]>
  6. *
  7. * Credits (in time order) for older HTB versions:
  8. * Stef Coene <[email protected]>
  9. * HTB support at LARTC mailing list
  10. * Ondrej Kraus, <[email protected]>
  11. * found missing INIT_QDISC(htb)
  12. * Vladimir Smelhaus, Aamer Akhter, Bert Hubert
  13. * helped a lot to locate nasty class stall bug
  14. * Andi Kleen, Jamal Hadi, Bert Hubert
  15. * code review and helpful comments on shaping
  16. * Tomasz Wrona, <[email protected]>
  17. * created test case so that I was able to fix nasty bug
  18. * Wilfried Weissmann
  19. * spotted bug in dequeue code and helped with fix
  20. * Jiri Fojtasek
  21. * fixed requeue routine
  22. * and many others. thanks.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/string.h>
  29. #include <linux/errno.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/list.h>
  32. #include <linux/compiler.h>
  33. #include <linux/rbtree.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/slab.h>
  36. #include <net/netlink.h>
  37. #include <net/sch_generic.h>
  38. #include <net/pkt_sched.h>
  39. #include <net/pkt_cls.h>
  40. /* HTB algorithm.
  41. Author: [email protected]
  42. ========================================================================
  43. HTB is like TBF with multiple classes. It is also similar to CBQ because
  44. it allows to assign priority to each class in hierarchy.
  45. In fact it is another implementation of Floyd's formal sharing.
  46. Levels:
  47. Each class is assigned level. Leaf has ALWAYS level 0 and root
  48. classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
  49. one less than their parent.
  50. */
  51. static int htb_hysteresis __read_mostly = 0; /* whether to use mode hysteresis for speedup */
  52. #define HTB_VER 0x30011 /* major must be matched with number supplied by TC as version */
  53. #if HTB_VER >> 16 != TC_HTB_PROTOVER
  54. #error "Mismatched sch_htb.c and pkt_sch.h"
  55. #endif
  56. /* Module parameter and sysfs export */
  57. module_param (htb_hysteresis, int, 0640);
  58. MODULE_PARM_DESC(htb_hysteresis, "Hysteresis mode, less CPU load, less accurate");
  59. static int htb_rate_est = 0; /* htb classes have a default rate estimator */
  60. module_param(htb_rate_est, int, 0640);
  61. MODULE_PARM_DESC(htb_rate_est, "setup a default rate estimator (4sec 16sec) for htb classes");
  62. /* used internaly to keep status of single class */
  63. enum htb_cmode {
  64. HTB_CANT_SEND, /* class can't send and can't borrow */
  65. HTB_MAY_BORROW, /* class can't send but may borrow */
  66. HTB_CAN_SEND /* class can send */
  67. };
  68. struct htb_prio {
  69. union {
  70. struct rb_root row;
  71. struct rb_root feed;
  72. };
  73. struct rb_node *ptr;
  74. /* When class changes from state 1->2 and disconnects from
  75. * parent's feed then we lost ptr value and start from the
  76. * first child again. Here we store classid of the
  77. * last valid ptr (used when ptr is NULL).
  78. */
  79. u32 last_ptr_id;
  80. };
  81. /* interior & leaf nodes; props specific to leaves are marked L:
  82. * To reduce false sharing, place mostly read fields at beginning,
  83. * and mostly written ones at the end.
  84. */
  85. struct htb_class {
  86. struct Qdisc_class_common common;
  87. struct psched_ratecfg rate;
  88. struct psched_ratecfg ceil;
  89. s64 buffer, cbuffer;/* token bucket depth/rate */
  90. s64 mbuffer; /* max wait time */
  91. u32 prio; /* these two are used only by leaves... */
  92. int quantum; /* but stored for parent-to-leaf return */
  93. struct tcf_proto __rcu *filter_list; /* class attached filters */
  94. struct tcf_block *block;
  95. int filter_cnt;
  96. int level; /* our level (see above) */
  97. unsigned int children;
  98. struct htb_class *parent; /* parent class */
  99. struct net_rate_estimator __rcu *rate_est;
  100. /*
  101. * Written often fields
  102. */
  103. struct gnet_stats_basic_sync bstats;
  104. struct gnet_stats_basic_sync bstats_bias;
  105. struct tc_htb_xstats xstats; /* our special stats */
  106. /* token bucket parameters */
  107. s64 tokens, ctokens;/* current number of tokens */
  108. s64 t_c; /* checkpoint time */
  109. union {
  110. struct htb_class_leaf {
  111. int deficit[TC_HTB_MAXDEPTH];
  112. struct Qdisc *q;
  113. struct netdev_queue *offload_queue;
  114. } leaf;
  115. struct htb_class_inner {
  116. struct htb_prio clprio[TC_HTB_NUMPRIO];
  117. } inner;
  118. };
  119. s64 pq_key;
  120. int prio_activity; /* for which prios are we active */
  121. enum htb_cmode cmode; /* current mode of the class */
  122. struct rb_node pq_node; /* node for event queue */
  123. struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
  124. unsigned int drops ____cacheline_aligned_in_smp;
  125. unsigned int overlimits;
  126. };
  127. struct htb_level {
  128. struct rb_root wait_pq;
  129. struct htb_prio hprio[TC_HTB_NUMPRIO];
  130. };
  131. struct htb_sched {
  132. struct Qdisc_class_hash clhash;
  133. int defcls; /* class where unclassified flows go to */
  134. int rate2quantum; /* quant = rate / rate2quantum */
  135. /* filters for qdisc itself */
  136. struct tcf_proto __rcu *filter_list;
  137. struct tcf_block *block;
  138. #define HTB_WARN_TOOMANYEVENTS 0x1
  139. unsigned int warned; /* only one warning */
  140. int direct_qlen;
  141. struct work_struct work;
  142. /* non shaped skbs; let them go directly thru */
  143. struct qdisc_skb_head direct_queue;
  144. u32 direct_pkts;
  145. u32 overlimits;
  146. struct qdisc_watchdog watchdog;
  147. s64 now; /* cached dequeue time */
  148. /* time of nearest event per level (row) */
  149. s64 near_ev_cache[TC_HTB_MAXDEPTH];
  150. int row_mask[TC_HTB_MAXDEPTH];
  151. struct htb_level hlevel[TC_HTB_MAXDEPTH];
  152. struct Qdisc **direct_qdiscs;
  153. unsigned int num_direct_qdiscs;
  154. bool offload;
  155. };
  156. /* find class in global hash table using given handle */
  157. static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
  158. {
  159. struct htb_sched *q = qdisc_priv(sch);
  160. struct Qdisc_class_common *clc;
  161. clc = qdisc_class_find(&q->clhash, handle);
  162. if (clc == NULL)
  163. return NULL;
  164. return container_of(clc, struct htb_class, common);
  165. }
  166. static unsigned long htb_search(struct Qdisc *sch, u32 handle)
  167. {
  168. return (unsigned long)htb_find(handle, sch);
  169. }
  170. /**
  171. * htb_classify - classify a packet into class
  172. *
  173. * It returns NULL if the packet should be dropped or -1 if the packet
  174. * should be passed directly thru. In all other cases leaf class is returned.
  175. * We allow direct class selection by classid in priority. The we examine
  176. * filters in qdisc and in inner nodes (if higher filter points to the inner
  177. * node). If we end up with classid MAJOR:0 we enqueue the skb into special
  178. * internal fifo (direct). These packets then go directly thru. If we still
  179. * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessful
  180. * then finish and return direct queue.
  181. */
  182. #define HTB_DIRECT ((struct htb_class *)-1L)
  183. static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
  184. int *qerr)
  185. {
  186. struct htb_sched *q = qdisc_priv(sch);
  187. struct htb_class *cl;
  188. struct tcf_result res;
  189. struct tcf_proto *tcf;
  190. int result;
  191. /* allow to select class by setting skb->priority to valid classid;
  192. * note that nfmark can be used too by attaching filter fw with no
  193. * rules in it
  194. */
  195. if (skb->priority == sch->handle)
  196. return HTB_DIRECT; /* X:0 (direct flow) selected */
  197. cl = htb_find(skb->priority, sch);
  198. if (cl) {
  199. if (cl->level == 0)
  200. return cl;
  201. /* Start with inner filter chain if a non-leaf class is selected */
  202. tcf = rcu_dereference_bh(cl->filter_list);
  203. } else {
  204. tcf = rcu_dereference_bh(q->filter_list);
  205. }
  206. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  207. while (tcf && (result = tcf_classify(skb, NULL, tcf, &res, false)) >= 0) {
  208. #ifdef CONFIG_NET_CLS_ACT
  209. switch (result) {
  210. case TC_ACT_QUEUED:
  211. case TC_ACT_STOLEN:
  212. case TC_ACT_TRAP:
  213. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  214. fallthrough;
  215. case TC_ACT_SHOT:
  216. return NULL;
  217. }
  218. #endif
  219. cl = (void *)res.class;
  220. if (!cl) {
  221. if (res.classid == sch->handle)
  222. return HTB_DIRECT; /* X:0 (direct flow) */
  223. cl = htb_find(res.classid, sch);
  224. if (!cl)
  225. break; /* filter selected invalid classid */
  226. }
  227. if (!cl->level)
  228. return cl; /* we hit leaf; return it */
  229. /* we have got inner class; apply inner filter chain */
  230. tcf = rcu_dereference_bh(cl->filter_list);
  231. }
  232. /* classification failed; try to use default class */
  233. cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
  234. if (!cl || cl->level)
  235. return HTB_DIRECT; /* bad default .. this is safe bet */
  236. return cl;
  237. }
  238. /**
  239. * htb_add_to_id_tree - adds class to the round robin list
  240. * @root: the root of the tree
  241. * @cl: the class to add
  242. * @prio: the give prio in class
  243. *
  244. * Routine adds class to the list (actually tree) sorted by classid.
  245. * Make sure that class is not already on such list for given prio.
  246. */
  247. static void htb_add_to_id_tree(struct rb_root *root,
  248. struct htb_class *cl, int prio)
  249. {
  250. struct rb_node **p = &root->rb_node, *parent = NULL;
  251. while (*p) {
  252. struct htb_class *c;
  253. parent = *p;
  254. c = rb_entry(parent, struct htb_class, node[prio]);
  255. if (cl->common.classid > c->common.classid)
  256. p = &parent->rb_right;
  257. else
  258. p = &parent->rb_left;
  259. }
  260. rb_link_node(&cl->node[prio], parent, p);
  261. rb_insert_color(&cl->node[prio], root);
  262. }
  263. /**
  264. * htb_add_to_wait_tree - adds class to the event queue with delay
  265. * @q: the priority event queue
  266. * @cl: the class to add
  267. * @delay: delay in microseconds
  268. *
  269. * The class is added to priority event queue to indicate that class will
  270. * change its mode in cl->pq_key microseconds. Make sure that class is not
  271. * already in the queue.
  272. */
  273. static void htb_add_to_wait_tree(struct htb_sched *q,
  274. struct htb_class *cl, s64 delay)
  275. {
  276. struct rb_node **p = &q->hlevel[cl->level].wait_pq.rb_node, *parent = NULL;
  277. cl->pq_key = q->now + delay;
  278. if (cl->pq_key == q->now)
  279. cl->pq_key++;
  280. /* update the nearest event cache */
  281. if (q->near_ev_cache[cl->level] > cl->pq_key)
  282. q->near_ev_cache[cl->level] = cl->pq_key;
  283. while (*p) {
  284. struct htb_class *c;
  285. parent = *p;
  286. c = rb_entry(parent, struct htb_class, pq_node);
  287. if (cl->pq_key >= c->pq_key)
  288. p = &parent->rb_right;
  289. else
  290. p = &parent->rb_left;
  291. }
  292. rb_link_node(&cl->pq_node, parent, p);
  293. rb_insert_color(&cl->pq_node, &q->hlevel[cl->level].wait_pq);
  294. }
  295. /**
  296. * htb_next_rb_node - finds next node in binary tree
  297. * @n: the current node in binary tree
  298. *
  299. * When we are past last key we return NULL.
  300. * Average complexity is 2 steps per call.
  301. */
  302. static inline void htb_next_rb_node(struct rb_node **n)
  303. {
  304. *n = rb_next(*n);
  305. }
  306. /**
  307. * htb_add_class_to_row - add class to its row
  308. * @q: the priority event queue
  309. * @cl: the class to add
  310. * @mask: the given priorities in class in bitmap
  311. *
  312. * The class is added to row at priorities marked in mask.
  313. * It does nothing if mask == 0.
  314. */
  315. static inline void htb_add_class_to_row(struct htb_sched *q,
  316. struct htb_class *cl, int mask)
  317. {
  318. q->row_mask[cl->level] |= mask;
  319. while (mask) {
  320. int prio = ffz(~mask);
  321. mask &= ~(1 << prio);
  322. htb_add_to_id_tree(&q->hlevel[cl->level].hprio[prio].row, cl, prio);
  323. }
  324. }
  325. /* If this triggers, it is a bug in this code, but it need not be fatal */
  326. static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root)
  327. {
  328. if (RB_EMPTY_NODE(rb)) {
  329. WARN_ON(1);
  330. } else {
  331. rb_erase(rb, root);
  332. RB_CLEAR_NODE(rb);
  333. }
  334. }
  335. /**
  336. * htb_remove_class_from_row - removes class from its row
  337. * @q: the priority event queue
  338. * @cl: the class to add
  339. * @mask: the given priorities in class in bitmap
  340. *
  341. * The class is removed from row at priorities marked in mask.
  342. * It does nothing if mask == 0.
  343. */
  344. static inline void htb_remove_class_from_row(struct htb_sched *q,
  345. struct htb_class *cl, int mask)
  346. {
  347. int m = 0;
  348. struct htb_level *hlevel = &q->hlevel[cl->level];
  349. while (mask) {
  350. int prio = ffz(~mask);
  351. struct htb_prio *hprio = &hlevel->hprio[prio];
  352. mask &= ~(1 << prio);
  353. if (hprio->ptr == cl->node + prio)
  354. htb_next_rb_node(&hprio->ptr);
  355. htb_safe_rb_erase(cl->node + prio, &hprio->row);
  356. if (!hprio->row.rb_node)
  357. m |= 1 << prio;
  358. }
  359. q->row_mask[cl->level] &= ~m;
  360. }
  361. /**
  362. * htb_activate_prios - creates active classe's feed chain
  363. * @q: the priority event queue
  364. * @cl: the class to activate
  365. *
  366. * The class is connected to ancestors and/or appropriate rows
  367. * for priorities it is participating on. cl->cmode must be new
  368. * (activated) mode. It does nothing if cl->prio_activity == 0.
  369. */
  370. static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl)
  371. {
  372. struct htb_class *p = cl->parent;
  373. long m, mask = cl->prio_activity;
  374. while (cl->cmode == HTB_MAY_BORROW && p && mask) {
  375. m = mask;
  376. while (m) {
  377. unsigned int prio = ffz(~m);
  378. if (WARN_ON_ONCE(prio >= ARRAY_SIZE(p->inner.clprio)))
  379. break;
  380. m &= ~(1 << prio);
  381. if (p->inner.clprio[prio].feed.rb_node)
  382. /* parent already has its feed in use so that
  383. * reset bit in mask as parent is already ok
  384. */
  385. mask &= ~(1 << prio);
  386. htb_add_to_id_tree(&p->inner.clprio[prio].feed, cl, prio);
  387. }
  388. p->prio_activity |= mask;
  389. cl = p;
  390. p = cl->parent;
  391. }
  392. if (cl->cmode == HTB_CAN_SEND && mask)
  393. htb_add_class_to_row(q, cl, mask);
  394. }
  395. /**
  396. * htb_deactivate_prios - remove class from feed chain
  397. * @q: the priority event queue
  398. * @cl: the class to deactivate
  399. *
  400. * cl->cmode must represent old mode (before deactivation). It does
  401. * nothing if cl->prio_activity == 0. Class is removed from all feed
  402. * chains and rows.
  403. */
  404. static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
  405. {
  406. struct htb_class *p = cl->parent;
  407. long m, mask = cl->prio_activity;
  408. while (cl->cmode == HTB_MAY_BORROW && p && mask) {
  409. m = mask;
  410. mask = 0;
  411. while (m) {
  412. int prio = ffz(~m);
  413. m &= ~(1 << prio);
  414. if (p->inner.clprio[prio].ptr == cl->node + prio) {
  415. /* we are removing child which is pointed to from
  416. * parent feed - forget the pointer but remember
  417. * classid
  418. */
  419. p->inner.clprio[prio].last_ptr_id = cl->common.classid;
  420. p->inner.clprio[prio].ptr = NULL;
  421. }
  422. htb_safe_rb_erase(cl->node + prio,
  423. &p->inner.clprio[prio].feed);
  424. if (!p->inner.clprio[prio].feed.rb_node)
  425. mask |= 1 << prio;
  426. }
  427. p->prio_activity &= ~mask;
  428. cl = p;
  429. p = cl->parent;
  430. }
  431. if (cl->cmode == HTB_CAN_SEND && mask)
  432. htb_remove_class_from_row(q, cl, mask);
  433. }
  434. static inline s64 htb_lowater(const struct htb_class *cl)
  435. {
  436. if (htb_hysteresis)
  437. return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
  438. else
  439. return 0;
  440. }
  441. static inline s64 htb_hiwater(const struct htb_class *cl)
  442. {
  443. if (htb_hysteresis)
  444. return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
  445. else
  446. return 0;
  447. }
  448. /**
  449. * htb_class_mode - computes and returns current class mode
  450. * @cl: the target class
  451. * @diff: diff time in microseconds
  452. *
  453. * It computes cl's mode at time cl->t_c+diff and returns it. If mode
  454. * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
  455. * from now to time when cl will change its state.
  456. * Also it is worth to note that class mode doesn't change simply
  457. * at cl->{c,}tokens == 0 but there can rather be hysteresis of
  458. * 0 .. -cl->{c,}buffer range. It is meant to limit number of
  459. * mode transitions per time unit. The speed gain is about 1/6.
  460. */
  461. static inline enum htb_cmode
  462. htb_class_mode(struct htb_class *cl, s64 *diff)
  463. {
  464. s64 toks;
  465. if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
  466. *diff = -toks;
  467. return HTB_CANT_SEND;
  468. }
  469. if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
  470. return HTB_CAN_SEND;
  471. *diff = -toks;
  472. return HTB_MAY_BORROW;
  473. }
  474. /**
  475. * htb_change_class_mode - changes classe's mode
  476. * @q: the priority event queue
  477. * @cl: the target class
  478. * @diff: diff time in microseconds
  479. *
  480. * This should be the only way how to change classe's mode under normal
  481. * circumstances. Routine will update feed lists linkage, change mode
  482. * and add class to the wait event queue if appropriate. New mode should
  483. * be different from old one and cl->pq_key has to be valid if changing
  484. * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
  485. */
  486. static void
  487. htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, s64 *diff)
  488. {
  489. enum htb_cmode new_mode = htb_class_mode(cl, diff);
  490. if (new_mode == cl->cmode)
  491. return;
  492. if (new_mode == HTB_CANT_SEND) {
  493. cl->overlimits++;
  494. q->overlimits++;
  495. }
  496. if (cl->prio_activity) { /* not necessary: speed optimization */
  497. if (cl->cmode != HTB_CANT_SEND)
  498. htb_deactivate_prios(q, cl);
  499. cl->cmode = new_mode;
  500. if (new_mode != HTB_CANT_SEND)
  501. htb_activate_prios(q, cl);
  502. } else
  503. cl->cmode = new_mode;
  504. }
  505. /**
  506. * htb_activate - inserts leaf cl into appropriate active feeds
  507. * @q: the priority event queue
  508. * @cl: the target class
  509. *
  510. * Routine learns (new) priority of leaf and activates feed chain
  511. * for the prio. It can be called on already active leaf safely.
  512. * It also adds leaf into droplist.
  513. */
  514. static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
  515. {
  516. WARN_ON(cl->level || !cl->leaf.q || !cl->leaf.q->q.qlen);
  517. if (!cl->prio_activity) {
  518. cl->prio_activity = 1 << cl->prio;
  519. htb_activate_prios(q, cl);
  520. }
  521. }
  522. /**
  523. * htb_deactivate - remove leaf cl from active feeds
  524. * @q: the priority event queue
  525. * @cl: the target class
  526. *
  527. * Make sure that leaf is active. In the other words it can't be called
  528. * with non-active leaf. It also removes class from the drop list.
  529. */
  530. static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
  531. {
  532. WARN_ON(!cl->prio_activity);
  533. htb_deactivate_prios(q, cl);
  534. cl->prio_activity = 0;
  535. }
  536. static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  537. struct sk_buff **to_free)
  538. {
  539. int ret;
  540. unsigned int len = qdisc_pkt_len(skb);
  541. struct htb_sched *q = qdisc_priv(sch);
  542. struct htb_class *cl = htb_classify(skb, sch, &ret);
  543. if (cl == HTB_DIRECT) {
  544. /* enqueue to helper queue */
  545. if (q->direct_queue.qlen < q->direct_qlen) {
  546. __qdisc_enqueue_tail(skb, &q->direct_queue);
  547. q->direct_pkts++;
  548. } else {
  549. return qdisc_drop(skb, sch, to_free);
  550. }
  551. #ifdef CONFIG_NET_CLS_ACT
  552. } else if (!cl) {
  553. if (ret & __NET_XMIT_BYPASS)
  554. qdisc_qstats_drop(sch);
  555. __qdisc_drop(skb, to_free);
  556. return ret;
  557. #endif
  558. } else if ((ret = qdisc_enqueue(skb, cl->leaf.q,
  559. to_free)) != NET_XMIT_SUCCESS) {
  560. if (net_xmit_drop_count(ret)) {
  561. qdisc_qstats_drop(sch);
  562. cl->drops++;
  563. }
  564. return ret;
  565. } else {
  566. htb_activate(q, cl);
  567. }
  568. sch->qstats.backlog += len;
  569. sch->q.qlen++;
  570. return NET_XMIT_SUCCESS;
  571. }
  572. static inline void htb_accnt_tokens(struct htb_class *cl, int bytes, s64 diff)
  573. {
  574. s64 toks = diff + cl->tokens;
  575. if (toks > cl->buffer)
  576. toks = cl->buffer;
  577. toks -= (s64) psched_l2t_ns(&cl->rate, bytes);
  578. if (toks <= -cl->mbuffer)
  579. toks = 1 - cl->mbuffer;
  580. cl->tokens = toks;
  581. }
  582. static inline void htb_accnt_ctokens(struct htb_class *cl, int bytes, s64 diff)
  583. {
  584. s64 toks = diff + cl->ctokens;
  585. if (toks > cl->cbuffer)
  586. toks = cl->cbuffer;
  587. toks -= (s64) psched_l2t_ns(&cl->ceil, bytes);
  588. if (toks <= -cl->mbuffer)
  589. toks = 1 - cl->mbuffer;
  590. cl->ctokens = toks;
  591. }
  592. /**
  593. * htb_charge_class - charges amount "bytes" to leaf and ancestors
  594. * @q: the priority event queue
  595. * @cl: the class to start iterate
  596. * @level: the minimum level to account
  597. * @skb: the socket buffer
  598. *
  599. * Routine assumes that packet "bytes" long was dequeued from leaf cl
  600. * borrowing from "level". It accounts bytes to ceil leaky bucket for
  601. * leaf and all ancestors and to rate bucket for ancestors at levels
  602. * "level" and higher. It also handles possible change of mode resulting
  603. * from the update. Note that mode can also increase here (MAY_BORROW to
  604. * CAN_SEND) because we can use more precise clock that event queue here.
  605. * In such case we remove class from event queue first.
  606. */
  607. static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
  608. int level, struct sk_buff *skb)
  609. {
  610. int bytes = qdisc_pkt_len(skb);
  611. enum htb_cmode old_mode;
  612. s64 diff;
  613. while (cl) {
  614. diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
  615. if (cl->level >= level) {
  616. if (cl->level == level)
  617. cl->xstats.lends++;
  618. htb_accnt_tokens(cl, bytes, diff);
  619. } else {
  620. cl->xstats.borrows++;
  621. cl->tokens += diff; /* we moved t_c; update tokens */
  622. }
  623. htb_accnt_ctokens(cl, bytes, diff);
  624. cl->t_c = q->now;
  625. old_mode = cl->cmode;
  626. diff = 0;
  627. htb_change_class_mode(q, cl, &diff);
  628. if (old_mode != cl->cmode) {
  629. if (old_mode != HTB_CAN_SEND)
  630. htb_safe_rb_erase(&cl->pq_node, &q->hlevel[cl->level].wait_pq);
  631. if (cl->cmode != HTB_CAN_SEND)
  632. htb_add_to_wait_tree(q, cl, diff);
  633. }
  634. /* update basic stats except for leaves which are already updated */
  635. if (cl->level)
  636. bstats_update(&cl->bstats, skb);
  637. cl = cl->parent;
  638. }
  639. }
  640. /**
  641. * htb_do_events - make mode changes to classes at the level
  642. * @q: the priority event queue
  643. * @level: which wait_pq in 'q->hlevel'
  644. * @start: start jiffies
  645. *
  646. * Scans event queue for pending events and applies them. Returns time of
  647. * next pending event (0 for no event in pq, q->now for too many events).
  648. * Note: Applied are events whose have cl->pq_key <= q->now.
  649. */
  650. static s64 htb_do_events(struct htb_sched *q, const int level,
  651. unsigned long start)
  652. {
  653. /* don't run for longer than 2 jiffies; 2 is used instead of
  654. * 1 to simplify things when jiffy is going to be incremented
  655. * too soon
  656. */
  657. unsigned long stop_at = start + 2;
  658. struct rb_root *wait_pq = &q->hlevel[level].wait_pq;
  659. while (time_before(jiffies, stop_at)) {
  660. struct htb_class *cl;
  661. s64 diff;
  662. struct rb_node *p = rb_first(wait_pq);
  663. if (!p)
  664. return 0;
  665. cl = rb_entry(p, struct htb_class, pq_node);
  666. if (cl->pq_key > q->now)
  667. return cl->pq_key;
  668. htb_safe_rb_erase(p, wait_pq);
  669. diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
  670. htb_change_class_mode(q, cl, &diff);
  671. if (cl->cmode != HTB_CAN_SEND)
  672. htb_add_to_wait_tree(q, cl, diff);
  673. }
  674. /* too much load - let's continue after a break for scheduling */
  675. if (!(q->warned & HTB_WARN_TOOMANYEVENTS)) {
  676. pr_warn("htb: too many events!\n");
  677. q->warned |= HTB_WARN_TOOMANYEVENTS;
  678. }
  679. return q->now;
  680. }
  681. /* Returns class->node+prio from id-tree where classe's id is >= id. NULL
  682. * is no such one exists.
  683. */
  684. static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
  685. u32 id)
  686. {
  687. struct rb_node *r = NULL;
  688. while (n) {
  689. struct htb_class *cl =
  690. rb_entry(n, struct htb_class, node[prio]);
  691. if (id > cl->common.classid) {
  692. n = n->rb_right;
  693. } else if (id < cl->common.classid) {
  694. r = n;
  695. n = n->rb_left;
  696. } else {
  697. return n;
  698. }
  699. }
  700. return r;
  701. }
  702. /**
  703. * htb_lookup_leaf - returns next leaf class in DRR order
  704. * @hprio: the current one
  705. * @prio: which prio in class
  706. *
  707. * Find leaf where current feed pointers points to.
  708. */
  709. static struct htb_class *htb_lookup_leaf(struct htb_prio *hprio, const int prio)
  710. {
  711. int i;
  712. struct {
  713. struct rb_node *root;
  714. struct rb_node **pptr;
  715. u32 *pid;
  716. } stk[TC_HTB_MAXDEPTH], *sp = stk;
  717. BUG_ON(!hprio->row.rb_node);
  718. sp->root = hprio->row.rb_node;
  719. sp->pptr = &hprio->ptr;
  720. sp->pid = &hprio->last_ptr_id;
  721. for (i = 0; i < 65535; i++) {
  722. if (!*sp->pptr && *sp->pid) {
  723. /* ptr was invalidated but id is valid - try to recover
  724. * the original or next ptr
  725. */
  726. *sp->pptr =
  727. htb_id_find_next_upper(prio, sp->root, *sp->pid);
  728. }
  729. *sp->pid = 0; /* ptr is valid now so that remove this hint as it
  730. * can become out of date quickly
  731. */
  732. if (!*sp->pptr) { /* we are at right end; rewind & go up */
  733. *sp->pptr = sp->root;
  734. while ((*sp->pptr)->rb_left)
  735. *sp->pptr = (*sp->pptr)->rb_left;
  736. if (sp > stk) {
  737. sp--;
  738. if (!*sp->pptr) {
  739. WARN_ON(1);
  740. return NULL;
  741. }
  742. htb_next_rb_node(sp->pptr);
  743. }
  744. } else {
  745. struct htb_class *cl;
  746. struct htb_prio *clp;
  747. cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
  748. if (!cl->level)
  749. return cl;
  750. clp = &cl->inner.clprio[prio];
  751. (++sp)->root = clp->feed.rb_node;
  752. sp->pptr = &clp->ptr;
  753. sp->pid = &clp->last_ptr_id;
  754. }
  755. }
  756. WARN_ON(1);
  757. return NULL;
  758. }
  759. /* dequeues packet at given priority and level; call only if
  760. * you are sure that there is active class at prio/level
  761. */
  762. static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, const int prio,
  763. const int level)
  764. {
  765. struct sk_buff *skb = NULL;
  766. struct htb_class *cl, *start;
  767. struct htb_level *hlevel = &q->hlevel[level];
  768. struct htb_prio *hprio = &hlevel->hprio[prio];
  769. /* look initial class up in the row */
  770. start = cl = htb_lookup_leaf(hprio, prio);
  771. do {
  772. next:
  773. if (unlikely(!cl))
  774. return NULL;
  775. /* class can be empty - it is unlikely but can be true if leaf
  776. * qdisc drops packets in enqueue routine or if someone used
  777. * graft operation on the leaf since last dequeue;
  778. * simply deactivate and skip such class
  779. */
  780. if (unlikely(cl->leaf.q->q.qlen == 0)) {
  781. struct htb_class *next;
  782. htb_deactivate(q, cl);
  783. /* row/level might become empty */
  784. if ((q->row_mask[level] & (1 << prio)) == 0)
  785. return NULL;
  786. next = htb_lookup_leaf(hprio, prio);
  787. if (cl == start) /* fix start if we just deleted it */
  788. start = next;
  789. cl = next;
  790. goto next;
  791. }
  792. skb = cl->leaf.q->dequeue(cl->leaf.q);
  793. if (likely(skb != NULL))
  794. break;
  795. qdisc_warn_nonwc("htb", cl->leaf.q);
  796. htb_next_rb_node(level ? &cl->parent->inner.clprio[prio].ptr:
  797. &q->hlevel[0].hprio[prio].ptr);
  798. cl = htb_lookup_leaf(hprio, prio);
  799. } while (cl != start);
  800. if (likely(skb != NULL)) {
  801. bstats_update(&cl->bstats, skb);
  802. cl->leaf.deficit[level] -= qdisc_pkt_len(skb);
  803. if (cl->leaf.deficit[level] < 0) {
  804. cl->leaf.deficit[level] += cl->quantum;
  805. htb_next_rb_node(level ? &cl->parent->inner.clprio[prio].ptr :
  806. &q->hlevel[0].hprio[prio].ptr);
  807. }
  808. /* this used to be after charge_class but this constelation
  809. * gives us slightly better performance
  810. */
  811. if (!cl->leaf.q->q.qlen)
  812. htb_deactivate(q, cl);
  813. htb_charge_class(q, cl, level, skb);
  814. }
  815. return skb;
  816. }
  817. static struct sk_buff *htb_dequeue(struct Qdisc *sch)
  818. {
  819. struct sk_buff *skb;
  820. struct htb_sched *q = qdisc_priv(sch);
  821. int level;
  822. s64 next_event;
  823. unsigned long start_at;
  824. /* try to dequeue direct packets as high prio (!) to minimize cpu work */
  825. skb = __qdisc_dequeue_head(&q->direct_queue);
  826. if (skb != NULL) {
  827. ok:
  828. qdisc_bstats_update(sch, skb);
  829. qdisc_qstats_backlog_dec(sch, skb);
  830. sch->q.qlen--;
  831. return skb;
  832. }
  833. if (!sch->q.qlen)
  834. goto fin;
  835. q->now = ktime_get_ns();
  836. start_at = jiffies;
  837. next_event = q->now + 5LLU * NSEC_PER_SEC;
  838. for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
  839. /* common case optimization - skip event handler quickly */
  840. int m;
  841. s64 event = q->near_ev_cache[level];
  842. if (q->now >= event) {
  843. event = htb_do_events(q, level, start_at);
  844. if (!event)
  845. event = q->now + NSEC_PER_SEC;
  846. q->near_ev_cache[level] = event;
  847. }
  848. if (next_event > event)
  849. next_event = event;
  850. m = ~q->row_mask[level];
  851. while (m != (int)(-1)) {
  852. int prio = ffz(m);
  853. m |= 1 << prio;
  854. skb = htb_dequeue_tree(q, prio, level);
  855. if (likely(skb != NULL))
  856. goto ok;
  857. }
  858. }
  859. if (likely(next_event > q->now))
  860. qdisc_watchdog_schedule_ns(&q->watchdog, next_event);
  861. else
  862. schedule_work(&q->work);
  863. fin:
  864. return skb;
  865. }
  866. /* reset all classes */
  867. /* always caled under BH & queue lock */
  868. static void htb_reset(struct Qdisc *sch)
  869. {
  870. struct htb_sched *q = qdisc_priv(sch);
  871. struct htb_class *cl;
  872. unsigned int i;
  873. for (i = 0; i < q->clhash.hashsize; i++) {
  874. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
  875. if (cl->level)
  876. memset(&cl->inner, 0, sizeof(cl->inner));
  877. else {
  878. if (cl->leaf.q && !q->offload)
  879. qdisc_reset(cl->leaf.q);
  880. }
  881. cl->prio_activity = 0;
  882. cl->cmode = HTB_CAN_SEND;
  883. }
  884. }
  885. qdisc_watchdog_cancel(&q->watchdog);
  886. __qdisc_reset_queue(&q->direct_queue);
  887. memset(q->hlevel, 0, sizeof(q->hlevel));
  888. memset(q->row_mask, 0, sizeof(q->row_mask));
  889. }
  890. static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
  891. [TCA_HTB_PARMS] = { .len = sizeof(struct tc_htb_opt) },
  892. [TCA_HTB_INIT] = { .len = sizeof(struct tc_htb_glob) },
  893. [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
  894. [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
  895. [TCA_HTB_DIRECT_QLEN] = { .type = NLA_U32 },
  896. [TCA_HTB_RATE64] = { .type = NLA_U64 },
  897. [TCA_HTB_CEIL64] = { .type = NLA_U64 },
  898. [TCA_HTB_OFFLOAD] = { .type = NLA_FLAG },
  899. };
  900. static void htb_work_func(struct work_struct *work)
  901. {
  902. struct htb_sched *q = container_of(work, struct htb_sched, work);
  903. struct Qdisc *sch = q->watchdog.qdisc;
  904. rcu_read_lock();
  905. __netif_schedule(qdisc_root(sch));
  906. rcu_read_unlock();
  907. }
  908. static void htb_set_lockdep_class_child(struct Qdisc *q)
  909. {
  910. static struct lock_class_key child_key;
  911. lockdep_set_class(qdisc_lock(q), &child_key);
  912. }
  913. static int htb_offload(struct net_device *dev, struct tc_htb_qopt_offload *opt)
  914. {
  915. return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_HTB, opt);
  916. }
  917. static int htb_init(struct Qdisc *sch, struct nlattr *opt,
  918. struct netlink_ext_ack *extack)
  919. {
  920. struct net_device *dev = qdisc_dev(sch);
  921. struct tc_htb_qopt_offload offload_opt;
  922. struct htb_sched *q = qdisc_priv(sch);
  923. struct nlattr *tb[TCA_HTB_MAX + 1];
  924. struct tc_htb_glob *gopt;
  925. unsigned int ntx;
  926. bool offload;
  927. int err;
  928. qdisc_watchdog_init(&q->watchdog, sch);
  929. INIT_WORK(&q->work, htb_work_func);
  930. if (!opt)
  931. return -EINVAL;
  932. err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
  933. if (err)
  934. return err;
  935. err = nla_parse_nested_deprecated(tb, TCA_HTB_MAX, opt, htb_policy,
  936. NULL);
  937. if (err < 0)
  938. return err;
  939. if (!tb[TCA_HTB_INIT])
  940. return -EINVAL;
  941. gopt = nla_data(tb[TCA_HTB_INIT]);
  942. if (gopt->version != HTB_VER >> 16)
  943. return -EINVAL;
  944. offload = nla_get_flag(tb[TCA_HTB_OFFLOAD]);
  945. if (offload) {
  946. if (sch->parent != TC_H_ROOT) {
  947. NL_SET_ERR_MSG(extack, "HTB must be the root qdisc to use offload");
  948. return -EOPNOTSUPP;
  949. }
  950. if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc) {
  951. NL_SET_ERR_MSG(extack, "hw-tc-offload ethtool feature flag must be on");
  952. return -EOPNOTSUPP;
  953. }
  954. q->num_direct_qdiscs = dev->real_num_tx_queues;
  955. q->direct_qdiscs = kcalloc(q->num_direct_qdiscs,
  956. sizeof(*q->direct_qdiscs),
  957. GFP_KERNEL);
  958. if (!q->direct_qdiscs)
  959. return -ENOMEM;
  960. }
  961. err = qdisc_class_hash_init(&q->clhash);
  962. if (err < 0)
  963. return err;
  964. if (tb[TCA_HTB_DIRECT_QLEN])
  965. q->direct_qlen = nla_get_u32(tb[TCA_HTB_DIRECT_QLEN]);
  966. else
  967. q->direct_qlen = qdisc_dev(sch)->tx_queue_len;
  968. if ((q->rate2quantum = gopt->rate2quantum) < 1)
  969. q->rate2quantum = 1;
  970. q->defcls = gopt->defcls;
  971. if (!offload)
  972. return 0;
  973. for (ntx = 0; ntx < q->num_direct_qdiscs; ntx++) {
  974. struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, ntx);
  975. struct Qdisc *qdisc;
  976. qdisc = qdisc_create_dflt(dev_queue, &pfifo_qdisc_ops,
  977. TC_H_MAKE(sch->handle, 0), extack);
  978. if (!qdisc) {
  979. return -ENOMEM;
  980. }
  981. htb_set_lockdep_class_child(qdisc);
  982. q->direct_qdiscs[ntx] = qdisc;
  983. qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  984. }
  985. sch->flags |= TCQ_F_MQROOT;
  986. offload_opt = (struct tc_htb_qopt_offload) {
  987. .command = TC_HTB_CREATE,
  988. .parent_classid = TC_H_MAJ(sch->handle) >> 16,
  989. .classid = TC_H_MIN(q->defcls),
  990. .extack = extack,
  991. };
  992. err = htb_offload(dev, &offload_opt);
  993. if (err)
  994. return err;
  995. /* Defer this assignment, so that htb_destroy skips offload-related
  996. * parts (especially calling ndo_setup_tc) on errors.
  997. */
  998. q->offload = true;
  999. return 0;
  1000. }
  1001. static void htb_attach_offload(struct Qdisc *sch)
  1002. {
  1003. struct net_device *dev = qdisc_dev(sch);
  1004. struct htb_sched *q = qdisc_priv(sch);
  1005. unsigned int ntx;
  1006. for (ntx = 0; ntx < q->num_direct_qdiscs; ntx++) {
  1007. struct Qdisc *old, *qdisc = q->direct_qdiscs[ntx];
  1008. old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
  1009. qdisc_put(old);
  1010. qdisc_hash_add(qdisc, false);
  1011. }
  1012. for (ntx = q->num_direct_qdiscs; ntx < dev->num_tx_queues; ntx++) {
  1013. struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, ntx);
  1014. struct Qdisc *old = dev_graft_qdisc(dev_queue, NULL);
  1015. qdisc_put(old);
  1016. }
  1017. kfree(q->direct_qdiscs);
  1018. q->direct_qdiscs = NULL;
  1019. }
  1020. static void htb_attach_software(struct Qdisc *sch)
  1021. {
  1022. struct net_device *dev = qdisc_dev(sch);
  1023. unsigned int ntx;
  1024. /* Resemble qdisc_graft behavior. */
  1025. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  1026. struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, ntx);
  1027. struct Qdisc *old = dev_graft_qdisc(dev_queue, sch);
  1028. qdisc_refcount_inc(sch);
  1029. qdisc_put(old);
  1030. }
  1031. }
  1032. static void htb_attach(struct Qdisc *sch)
  1033. {
  1034. struct htb_sched *q = qdisc_priv(sch);
  1035. if (q->offload)
  1036. htb_attach_offload(sch);
  1037. else
  1038. htb_attach_software(sch);
  1039. }
  1040. static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
  1041. {
  1042. struct htb_sched *q = qdisc_priv(sch);
  1043. struct nlattr *nest;
  1044. struct tc_htb_glob gopt;
  1045. if (q->offload)
  1046. sch->flags |= TCQ_F_OFFLOADED;
  1047. else
  1048. sch->flags &= ~TCQ_F_OFFLOADED;
  1049. sch->qstats.overlimits = q->overlimits;
  1050. /* Its safe to not acquire qdisc lock. As we hold RTNL,
  1051. * no change can happen on the qdisc parameters.
  1052. */
  1053. gopt.direct_pkts = q->direct_pkts;
  1054. gopt.version = HTB_VER;
  1055. gopt.rate2quantum = q->rate2quantum;
  1056. gopt.defcls = q->defcls;
  1057. gopt.debug = 0;
  1058. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  1059. if (nest == NULL)
  1060. goto nla_put_failure;
  1061. if (nla_put(skb, TCA_HTB_INIT, sizeof(gopt), &gopt) ||
  1062. nla_put_u32(skb, TCA_HTB_DIRECT_QLEN, q->direct_qlen))
  1063. goto nla_put_failure;
  1064. if (q->offload && nla_put_flag(skb, TCA_HTB_OFFLOAD))
  1065. goto nla_put_failure;
  1066. return nla_nest_end(skb, nest);
  1067. nla_put_failure:
  1068. nla_nest_cancel(skb, nest);
  1069. return -1;
  1070. }
  1071. static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
  1072. struct sk_buff *skb, struct tcmsg *tcm)
  1073. {
  1074. struct htb_class *cl = (struct htb_class *)arg;
  1075. struct htb_sched *q = qdisc_priv(sch);
  1076. struct nlattr *nest;
  1077. struct tc_htb_opt opt;
  1078. /* Its safe to not acquire qdisc lock. As we hold RTNL,
  1079. * no change can happen on the class parameters.
  1080. */
  1081. tcm->tcm_parent = cl->parent ? cl->parent->common.classid : TC_H_ROOT;
  1082. tcm->tcm_handle = cl->common.classid;
  1083. if (!cl->level && cl->leaf.q)
  1084. tcm->tcm_info = cl->leaf.q->handle;
  1085. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  1086. if (nest == NULL)
  1087. goto nla_put_failure;
  1088. memset(&opt, 0, sizeof(opt));
  1089. psched_ratecfg_getrate(&opt.rate, &cl->rate);
  1090. opt.buffer = PSCHED_NS2TICKS(cl->buffer);
  1091. psched_ratecfg_getrate(&opt.ceil, &cl->ceil);
  1092. opt.cbuffer = PSCHED_NS2TICKS(cl->cbuffer);
  1093. opt.quantum = cl->quantum;
  1094. opt.prio = cl->prio;
  1095. opt.level = cl->level;
  1096. if (nla_put(skb, TCA_HTB_PARMS, sizeof(opt), &opt))
  1097. goto nla_put_failure;
  1098. if (q->offload && nla_put_flag(skb, TCA_HTB_OFFLOAD))
  1099. goto nla_put_failure;
  1100. if ((cl->rate.rate_bytes_ps >= (1ULL << 32)) &&
  1101. nla_put_u64_64bit(skb, TCA_HTB_RATE64, cl->rate.rate_bytes_ps,
  1102. TCA_HTB_PAD))
  1103. goto nla_put_failure;
  1104. if ((cl->ceil.rate_bytes_ps >= (1ULL << 32)) &&
  1105. nla_put_u64_64bit(skb, TCA_HTB_CEIL64, cl->ceil.rate_bytes_ps,
  1106. TCA_HTB_PAD))
  1107. goto nla_put_failure;
  1108. return nla_nest_end(skb, nest);
  1109. nla_put_failure:
  1110. nla_nest_cancel(skb, nest);
  1111. return -1;
  1112. }
  1113. static void htb_offload_aggregate_stats(struct htb_sched *q,
  1114. struct htb_class *cl)
  1115. {
  1116. u64 bytes = 0, packets = 0;
  1117. struct htb_class *c;
  1118. unsigned int i;
  1119. gnet_stats_basic_sync_init(&cl->bstats);
  1120. for (i = 0; i < q->clhash.hashsize; i++) {
  1121. hlist_for_each_entry(c, &q->clhash.hash[i], common.hnode) {
  1122. struct htb_class *p = c;
  1123. while (p && p->level < cl->level)
  1124. p = p->parent;
  1125. if (p != cl)
  1126. continue;
  1127. bytes += u64_stats_read(&c->bstats_bias.bytes);
  1128. packets += u64_stats_read(&c->bstats_bias.packets);
  1129. if (c->level == 0) {
  1130. bytes += u64_stats_read(&c->leaf.q->bstats.bytes);
  1131. packets += u64_stats_read(&c->leaf.q->bstats.packets);
  1132. }
  1133. }
  1134. }
  1135. _bstats_update(&cl->bstats, bytes, packets);
  1136. }
  1137. static int
  1138. htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
  1139. {
  1140. struct htb_class *cl = (struct htb_class *)arg;
  1141. struct htb_sched *q = qdisc_priv(sch);
  1142. struct gnet_stats_queue qs = {
  1143. .drops = cl->drops,
  1144. .overlimits = cl->overlimits,
  1145. };
  1146. __u32 qlen = 0;
  1147. if (!cl->level && cl->leaf.q)
  1148. qdisc_qstats_qlen_backlog(cl->leaf.q, &qlen, &qs.backlog);
  1149. cl->xstats.tokens = clamp_t(s64, PSCHED_NS2TICKS(cl->tokens),
  1150. INT_MIN, INT_MAX);
  1151. cl->xstats.ctokens = clamp_t(s64, PSCHED_NS2TICKS(cl->ctokens),
  1152. INT_MIN, INT_MAX);
  1153. if (q->offload) {
  1154. if (!cl->level) {
  1155. if (cl->leaf.q)
  1156. cl->bstats = cl->leaf.q->bstats;
  1157. else
  1158. gnet_stats_basic_sync_init(&cl->bstats);
  1159. _bstats_update(&cl->bstats,
  1160. u64_stats_read(&cl->bstats_bias.bytes),
  1161. u64_stats_read(&cl->bstats_bias.packets));
  1162. } else {
  1163. htb_offload_aggregate_stats(q, cl);
  1164. }
  1165. }
  1166. if (gnet_stats_copy_basic(d, NULL, &cl->bstats, true) < 0 ||
  1167. gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
  1168. gnet_stats_copy_queue(d, NULL, &qs, qlen) < 0)
  1169. return -1;
  1170. return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
  1171. }
  1172. static struct netdev_queue *
  1173. htb_select_queue(struct Qdisc *sch, struct tcmsg *tcm)
  1174. {
  1175. struct net_device *dev = qdisc_dev(sch);
  1176. struct tc_htb_qopt_offload offload_opt;
  1177. struct htb_sched *q = qdisc_priv(sch);
  1178. int err;
  1179. if (!q->offload)
  1180. return sch->dev_queue;
  1181. offload_opt = (struct tc_htb_qopt_offload) {
  1182. .command = TC_HTB_LEAF_QUERY_QUEUE,
  1183. .classid = TC_H_MIN(tcm->tcm_parent),
  1184. };
  1185. err = htb_offload(dev, &offload_opt);
  1186. if (err || offload_opt.qid >= dev->num_tx_queues)
  1187. return NULL;
  1188. return netdev_get_tx_queue(dev, offload_opt.qid);
  1189. }
  1190. static struct Qdisc *
  1191. htb_graft_helper(struct netdev_queue *dev_queue, struct Qdisc *new_q)
  1192. {
  1193. struct net_device *dev = dev_queue->dev;
  1194. struct Qdisc *old_q;
  1195. if (dev->flags & IFF_UP)
  1196. dev_deactivate(dev);
  1197. old_q = dev_graft_qdisc(dev_queue, new_q);
  1198. if (new_q)
  1199. new_q->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  1200. if (dev->flags & IFF_UP)
  1201. dev_activate(dev);
  1202. return old_q;
  1203. }
  1204. static struct netdev_queue *htb_offload_get_queue(struct htb_class *cl)
  1205. {
  1206. struct netdev_queue *queue;
  1207. queue = cl->leaf.offload_queue;
  1208. if (!(cl->leaf.q->flags & TCQ_F_BUILTIN))
  1209. WARN_ON(cl->leaf.q->dev_queue != queue);
  1210. return queue;
  1211. }
  1212. static void htb_offload_move_qdisc(struct Qdisc *sch, struct htb_class *cl_old,
  1213. struct htb_class *cl_new, bool destroying)
  1214. {
  1215. struct netdev_queue *queue_old, *queue_new;
  1216. struct net_device *dev = qdisc_dev(sch);
  1217. queue_old = htb_offload_get_queue(cl_old);
  1218. queue_new = htb_offload_get_queue(cl_new);
  1219. if (!destroying) {
  1220. struct Qdisc *qdisc;
  1221. if (dev->flags & IFF_UP)
  1222. dev_deactivate(dev);
  1223. qdisc = dev_graft_qdisc(queue_old, NULL);
  1224. WARN_ON(qdisc != cl_old->leaf.q);
  1225. }
  1226. if (!(cl_old->leaf.q->flags & TCQ_F_BUILTIN))
  1227. cl_old->leaf.q->dev_queue = queue_new;
  1228. cl_old->leaf.offload_queue = queue_new;
  1229. if (!destroying) {
  1230. struct Qdisc *qdisc;
  1231. qdisc = dev_graft_qdisc(queue_new, cl_old->leaf.q);
  1232. if (dev->flags & IFF_UP)
  1233. dev_activate(dev);
  1234. WARN_ON(!(qdisc->flags & TCQ_F_BUILTIN));
  1235. }
  1236. }
  1237. static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  1238. struct Qdisc **old, struct netlink_ext_ack *extack)
  1239. {
  1240. struct netdev_queue *dev_queue = sch->dev_queue;
  1241. struct htb_class *cl = (struct htb_class *)arg;
  1242. struct htb_sched *q = qdisc_priv(sch);
  1243. struct Qdisc *old_q;
  1244. if (cl->level)
  1245. return -EINVAL;
  1246. if (q->offload)
  1247. dev_queue = htb_offload_get_queue(cl);
  1248. if (!new) {
  1249. new = qdisc_create_dflt(dev_queue, &pfifo_qdisc_ops,
  1250. cl->common.classid, extack);
  1251. if (!new)
  1252. return -ENOBUFS;
  1253. }
  1254. if (q->offload) {
  1255. htb_set_lockdep_class_child(new);
  1256. /* One ref for cl->leaf.q, the other for dev_queue->qdisc. */
  1257. qdisc_refcount_inc(new);
  1258. old_q = htb_graft_helper(dev_queue, new);
  1259. }
  1260. *old = qdisc_replace(sch, new, &cl->leaf.q);
  1261. if (q->offload) {
  1262. WARN_ON(old_q != *old);
  1263. qdisc_put(old_q);
  1264. }
  1265. return 0;
  1266. }
  1267. static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
  1268. {
  1269. struct htb_class *cl = (struct htb_class *)arg;
  1270. return !cl->level ? cl->leaf.q : NULL;
  1271. }
  1272. static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
  1273. {
  1274. struct htb_class *cl = (struct htb_class *)arg;
  1275. htb_deactivate(qdisc_priv(sch), cl);
  1276. }
  1277. static inline int htb_parent_last_child(struct htb_class *cl)
  1278. {
  1279. if (!cl->parent)
  1280. /* the root class */
  1281. return 0;
  1282. if (cl->parent->children > 1)
  1283. /* not the last child */
  1284. return 0;
  1285. return 1;
  1286. }
  1287. static void htb_parent_to_leaf(struct Qdisc *sch, struct htb_class *cl,
  1288. struct Qdisc *new_q)
  1289. {
  1290. struct htb_sched *q = qdisc_priv(sch);
  1291. struct htb_class *parent = cl->parent;
  1292. WARN_ON(cl->level || !cl->leaf.q || cl->prio_activity);
  1293. if (parent->cmode != HTB_CAN_SEND)
  1294. htb_safe_rb_erase(&parent->pq_node,
  1295. &q->hlevel[parent->level].wait_pq);
  1296. parent->level = 0;
  1297. memset(&parent->inner, 0, sizeof(parent->inner));
  1298. parent->leaf.q = new_q ? new_q : &noop_qdisc;
  1299. parent->tokens = parent->buffer;
  1300. parent->ctokens = parent->cbuffer;
  1301. parent->t_c = ktime_get_ns();
  1302. parent->cmode = HTB_CAN_SEND;
  1303. if (q->offload)
  1304. parent->leaf.offload_queue = cl->leaf.offload_queue;
  1305. }
  1306. static void htb_parent_to_leaf_offload(struct Qdisc *sch,
  1307. struct netdev_queue *dev_queue,
  1308. struct Qdisc *new_q)
  1309. {
  1310. struct Qdisc *old_q;
  1311. /* One ref for cl->leaf.q, the other for dev_queue->qdisc. */
  1312. if (new_q)
  1313. qdisc_refcount_inc(new_q);
  1314. old_q = htb_graft_helper(dev_queue, new_q);
  1315. WARN_ON(!(old_q->flags & TCQ_F_BUILTIN));
  1316. }
  1317. static int htb_destroy_class_offload(struct Qdisc *sch, struct htb_class *cl,
  1318. bool last_child, bool destroying,
  1319. struct netlink_ext_ack *extack)
  1320. {
  1321. struct tc_htb_qopt_offload offload_opt;
  1322. struct netdev_queue *dev_queue;
  1323. struct Qdisc *q = cl->leaf.q;
  1324. struct Qdisc *old;
  1325. int err;
  1326. if (cl->level)
  1327. return -EINVAL;
  1328. WARN_ON(!q);
  1329. dev_queue = htb_offload_get_queue(cl);
  1330. /* When destroying, caller qdisc_graft grafts the new qdisc and invokes
  1331. * qdisc_put for the qdisc being destroyed. htb_destroy_class_offload
  1332. * does not need to graft or qdisc_put the qdisc being destroyed.
  1333. */
  1334. if (!destroying) {
  1335. old = htb_graft_helper(dev_queue, NULL);
  1336. /* Last qdisc grafted should be the same as cl->leaf.q when
  1337. * calling htb_delete.
  1338. */
  1339. WARN_ON(old != q);
  1340. }
  1341. if (cl->parent) {
  1342. _bstats_update(&cl->parent->bstats_bias,
  1343. u64_stats_read(&q->bstats.bytes),
  1344. u64_stats_read(&q->bstats.packets));
  1345. }
  1346. offload_opt = (struct tc_htb_qopt_offload) {
  1347. .command = !last_child ? TC_HTB_LEAF_DEL :
  1348. destroying ? TC_HTB_LEAF_DEL_LAST_FORCE :
  1349. TC_HTB_LEAF_DEL_LAST,
  1350. .classid = cl->common.classid,
  1351. .extack = extack,
  1352. };
  1353. err = htb_offload(qdisc_dev(sch), &offload_opt);
  1354. if (!destroying) {
  1355. if (!err)
  1356. qdisc_put(old);
  1357. else
  1358. htb_graft_helper(dev_queue, old);
  1359. }
  1360. if (last_child)
  1361. return err;
  1362. if (!err && offload_opt.classid != TC_H_MIN(cl->common.classid)) {
  1363. u32 classid = TC_H_MAJ(sch->handle) |
  1364. TC_H_MIN(offload_opt.classid);
  1365. struct htb_class *moved_cl = htb_find(classid, sch);
  1366. htb_offload_move_qdisc(sch, moved_cl, cl, destroying);
  1367. }
  1368. return err;
  1369. }
  1370. static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
  1371. {
  1372. if (!cl->level) {
  1373. WARN_ON(!cl->leaf.q);
  1374. qdisc_put(cl->leaf.q);
  1375. }
  1376. gen_kill_estimator(&cl->rate_est);
  1377. tcf_block_put(cl->block);
  1378. kfree(cl);
  1379. }
  1380. static void htb_destroy(struct Qdisc *sch)
  1381. {
  1382. struct net_device *dev = qdisc_dev(sch);
  1383. struct tc_htb_qopt_offload offload_opt;
  1384. struct htb_sched *q = qdisc_priv(sch);
  1385. struct hlist_node *next;
  1386. bool nonempty, changed;
  1387. struct htb_class *cl;
  1388. unsigned int i;
  1389. cancel_work_sync(&q->work);
  1390. qdisc_watchdog_cancel(&q->watchdog);
  1391. /* This line used to be after htb_destroy_class call below
  1392. * and surprisingly it worked in 2.4. But it must precede it
  1393. * because filter need its target class alive to be able to call
  1394. * unbind_filter on it (without Oops).
  1395. */
  1396. tcf_block_put(q->block);
  1397. for (i = 0; i < q->clhash.hashsize; i++) {
  1398. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
  1399. tcf_block_put(cl->block);
  1400. cl->block = NULL;
  1401. }
  1402. }
  1403. do {
  1404. nonempty = false;
  1405. changed = false;
  1406. for (i = 0; i < q->clhash.hashsize; i++) {
  1407. hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
  1408. common.hnode) {
  1409. bool last_child;
  1410. if (!q->offload) {
  1411. htb_destroy_class(sch, cl);
  1412. continue;
  1413. }
  1414. nonempty = true;
  1415. if (cl->level)
  1416. continue;
  1417. changed = true;
  1418. last_child = htb_parent_last_child(cl);
  1419. htb_destroy_class_offload(sch, cl, last_child,
  1420. true, NULL);
  1421. qdisc_class_hash_remove(&q->clhash,
  1422. &cl->common);
  1423. if (cl->parent)
  1424. cl->parent->children--;
  1425. if (last_child)
  1426. htb_parent_to_leaf(sch, cl, NULL);
  1427. htb_destroy_class(sch, cl);
  1428. }
  1429. }
  1430. } while (changed);
  1431. WARN_ON(nonempty);
  1432. qdisc_class_hash_destroy(&q->clhash);
  1433. __qdisc_reset_queue(&q->direct_queue);
  1434. if (q->offload) {
  1435. offload_opt = (struct tc_htb_qopt_offload) {
  1436. .command = TC_HTB_DESTROY,
  1437. };
  1438. htb_offload(dev, &offload_opt);
  1439. }
  1440. if (!q->direct_qdiscs)
  1441. return;
  1442. for (i = 0; i < q->num_direct_qdiscs && q->direct_qdiscs[i]; i++)
  1443. qdisc_put(q->direct_qdiscs[i]);
  1444. kfree(q->direct_qdiscs);
  1445. }
  1446. static int htb_delete(struct Qdisc *sch, unsigned long arg,
  1447. struct netlink_ext_ack *extack)
  1448. {
  1449. struct htb_sched *q = qdisc_priv(sch);
  1450. struct htb_class *cl = (struct htb_class *)arg;
  1451. struct Qdisc *new_q = NULL;
  1452. int last_child = 0;
  1453. int err;
  1454. /* TODO: why don't allow to delete subtree ? references ? does
  1455. * tc subsys guarantee us that in htb_destroy it holds no class
  1456. * refs so that we can remove children safely there ?
  1457. */
  1458. if (cl->children || cl->filter_cnt)
  1459. return -EBUSY;
  1460. if (!cl->level && htb_parent_last_child(cl))
  1461. last_child = 1;
  1462. if (q->offload) {
  1463. err = htb_destroy_class_offload(sch, cl, last_child, false,
  1464. extack);
  1465. if (err)
  1466. return err;
  1467. }
  1468. if (last_child) {
  1469. struct netdev_queue *dev_queue = sch->dev_queue;
  1470. if (q->offload)
  1471. dev_queue = htb_offload_get_queue(cl);
  1472. new_q = qdisc_create_dflt(dev_queue, &pfifo_qdisc_ops,
  1473. cl->parent->common.classid,
  1474. NULL);
  1475. if (q->offload) {
  1476. if (new_q)
  1477. htb_set_lockdep_class_child(new_q);
  1478. htb_parent_to_leaf_offload(sch, dev_queue, new_q);
  1479. }
  1480. }
  1481. sch_tree_lock(sch);
  1482. if (!cl->level)
  1483. qdisc_purge_queue(cl->leaf.q);
  1484. /* delete from hash and active; remainder in destroy_class */
  1485. qdisc_class_hash_remove(&q->clhash, &cl->common);
  1486. if (cl->parent)
  1487. cl->parent->children--;
  1488. if (cl->prio_activity)
  1489. htb_deactivate(q, cl);
  1490. if (cl->cmode != HTB_CAN_SEND)
  1491. htb_safe_rb_erase(&cl->pq_node,
  1492. &q->hlevel[cl->level].wait_pq);
  1493. if (last_child)
  1494. htb_parent_to_leaf(sch, cl, new_q);
  1495. sch_tree_unlock(sch);
  1496. htb_destroy_class(sch, cl);
  1497. return 0;
  1498. }
  1499. static int htb_change_class(struct Qdisc *sch, u32 classid,
  1500. u32 parentid, struct nlattr **tca,
  1501. unsigned long *arg, struct netlink_ext_ack *extack)
  1502. {
  1503. int err = -EINVAL;
  1504. struct htb_sched *q = qdisc_priv(sch);
  1505. struct htb_class *cl = (struct htb_class *)*arg, *parent;
  1506. struct tc_htb_qopt_offload offload_opt;
  1507. struct nlattr *opt = tca[TCA_OPTIONS];
  1508. struct nlattr *tb[TCA_HTB_MAX + 1];
  1509. struct Qdisc *parent_qdisc = NULL;
  1510. struct netdev_queue *dev_queue;
  1511. struct tc_htb_opt *hopt;
  1512. u64 rate64, ceil64;
  1513. int warn = 0;
  1514. /* extract all subattrs from opt attr */
  1515. if (!opt)
  1516. goto failure;
  1517. err = nla_parse_nested_deprecated(tb, TCA_HTB_MAX, opt, htb_policy,
  1518. NULL);
  1519. if (err < 0)
  1520. goto failure;
  1521. err = -EINVAL;
  1522. if (tb[TCA_HTB_PARMS] == NULL)
  1523. goto failure;
  1524. parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
  1525. hopt = nla_data(tb[TCA_HTB_PARMS]);
  1526. if (!hopt->rate.rate || !hopt->ceil.rate)
  1527. goto failure;
  1528. if (q->offload) {
  1529. /* Options not supported by the offload. */
  1530. if (hopt->rate.overhead || hopt->ceil.overhead) {
  1531. NL_SET_ERR_MSG(extack, "HTB offload doesn't support the overhead parameter");
  1532. goto failure;
  1533. }
  1534. if (hopt->rate.mpu || hopt->ceil.mpu) {
  1535. NL_SET_ERR_MSG(extack, "HTB offload doesn't support the mpu parameter");
  1536. goto failure;
  1537. }
  1538. if (hopt->quantum) {
  1539. NL_SET_ERR_MSG(extack, "HTB offload doesn't support the quantum parameter");
  1540. goto failure;
  1541. }
  1542. if (hopt->prio) {
  1543. NL_SET_ERR_MSG(extack, "HTB offload doesn't support the prio parameter");
  1544. goto failure;
  1545. }
  1546. }
  1547. /* Keeping backward compatible with rate_table based iproute2 tc */
  1548. if (hopt->rate.linklayer == TC_LINKLAYER_UNAWARE)
  1549. qdisc_put_rtab(qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB],
  1550. NULL));
  1551. if (hopt->ceil.linklayer == TC_LINKLAYER_UNAWARE)
  1552. qdisc_put_rtab(qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB],
  1553. NULL));
  1554. rate64 = tb[TCA_HTB_RATE64] ? nla_get_u64(tb[TCA_HTB_RATE64]) : 0;
  1555. ceil64 = tb[TCA_HTB_CEIL64] ? nla_get_u64(tb[TCA_HTB_CEIL64]) : 0;
  1556. if (!cl) { /* new class */
  1557. struct net_device *dev = qdisc_dev(sch);
  1558. struct Qdisc *new_q, *old_q;
  1559. int prio;
  1560. struct {
  1561. struct nlattr nla;
  1562. struct gnet_estimator opt;
  1563. } est = {
  1564. .nla = {
  1565. .nla_len = nla_attr_size(sizeof(est.opt)),
  1566. .nla_type = TCA_RATE,
  1567. },
  1568. .opt = {
  1569. /* 4s interval, 16s averaging constant */
  1570. .interval = 2,
  1571. .ewma_log = 2,
  1572. },
  1573. };
  1574. /* check for valid classid */
  1575. if (!classid || TC_H_MAJ(classid ^ sch->handle) ||
  1576. htb_find(classid, sch))
  1577. goto failure;
  1578. /* check maximal depth */
  1579. if (parent && parent->parent && parent->parent->level < 2) {
  1580. pr_err("htb: tree is too deep\n");
  1581. goto failure;
  1582. }
  1583. err = -ENOBUFS;
  1584. cl = kzalloc(sizeof(*cl), GFP_KERNEL);
  1585. if (!cl)
  1586. goto failure;
  1587. gnet_stats_basic_sync_init(&cl->bstats);
  1588. gnet_stats_basic_sync_init(&cl->bstats_bias);
  1589. err = tcf_block_get(&cl->block, &cl->filter_list, sch, extack);
  1590. if (err) {
  1591. kfree(cl);
  1592. goto failure;
  1593. }
  1594. if (htb_rate_est || tca[TCA_RATE]) {
  1595. err = gen_new_estimator(&cl->bstats, NULL,
  1596. &cl->rate_est,
  1597. NULL,
  1598. true,
  1599. tca[TCA_RATE] ? : &est.nla);
  1600. if (err)
  1601. goto err_block_put;
  1602. }
  1603. cl->children = 0;
  1604. RB_CLEAR_NODE(&cl->pq_node);
  1605. for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
  1606. RB_CLEAR_NODE(&cl->node[prio]);
  1607. cl->common.classid = classid;
  1608. /* Make sure nothing interrupts us in between of two
  1609. * ndo_setup_tc calls.
  1610. */
  1611. ASSERT_RTNL();
  1612. /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
  1613. * so that can't be used inside of sch_tree_lock
  1614. * -- thanks to Karlis Peisenieks
  1615. */
  1616. if (!q->offload) {
  1617. dev_queue = sch->dev_queue;
  1618. } else if (!(parent && !parent->level)) {
  1619. /* Assign a dev_queue to this classid. */
  1620. offload_opt = (struct tc_htb_qopt_offload) {
  1621. .command = TC_HTB_LEAF_ALLOC_QUEUE,
  1622. .classid = cl->common.classid,
  1623. .parent_classid = parent ?
  1624. TC_H_MIN(parent->common.classid) :
  1625. TC_HTB_CLASSID_ROOT,
  1626. .rate = max_t(u64, hopt->rate.rate, rate64),
  1627. .ceil = max_t(u64, hopt->ceil.rate, ceil64),
  1628. .extack = extack,
  1629. };
  1630. err = htb_offload(dev, &offload_opt);
  1631. if (err) {
  1632. pr_err("htb: TC_HTB_LEAF_ALLOC_QUEUE failed with err = %d\n",
  1633. err);
  1634. goto err_kill_estimator;
  1635. }
  1636. dev_queue = netdev_get_tx_queue(dev, offload_opt.qid);
  1637. } else { /* First child. */
  1638. dev_queue = htb_offload_get_queue(parent);
  1639. old_q = htb_graft_helper(dev_queue, NULL);
  1640. WARN_ON(old_q != parent->leaf.q);
  1641. offload_opt = (struct tc_htb_qopt_offload) {
  1642. .command = TC_HTB_LEAF_TO_INNER,
  1643. .classid = cl->common.classid,
  1644. .parent_classid =
  1645. TC_H_MIN(parent->common.classid),
  1646. .rate = max_t(u64, hopt->rate.rate, rate64),
  1647. .ceil = max_t(u64, hopt->ceil.rate, ceil64),
  1648. .extack = extack,
  1649. };
  1650. err = htb_offload(dev, &offload_opt);
  1651. if (err) {
  1652. pr_err("htb: TC_HTB_LEAF_TO_INNER failed with err = %d\n",
  1653. err);
  1654. htb_graft_helper(dev_queue, old_q);
  1655. goto err_kill_estimator;
  1656. }
  1657. _bstats_update(&parent->bstats_bias,
  1658. u64_stats_read(&old_q->bstats.bytes),
  1659. u64_stats_read(&old_q->bstats.packets));
  1660. qdisc_put(old_q);
  1661. }
  1662. new_q = qdisc_create_dflt(dev_queue, &pfifo_qdisc_ops,
  1663. classid, NULL);
  1664. if (q->offload) {
  1665. if (new_q) {
  1666. htb_set_lockdep_class_child(new_q);
  1667. /* One ref for cl->leaf.q, the other for
  1668. * dev_queue->qdisc.
  1669. */
  1670. qdisc_refcount_inc(new_q);
  1671. }
  1672. old_q = htb_graft_helper(dev_queue, new_q);
  1673. /* No qdisc_put needed. */
  1674. WARN_ON(!(old_q->flags & TCQ_F_BUILTIN));
  1675. }
  1676. sch_tree_lock(sch);
  1677. if (parent && !parent->level) {
  1678. /* turn parent into inner node */
  1679. qdisc_purge_queue(parent->leaf.q);
  1680. parent_qdisc = parent->leaf.q;
  1681. if (parent->prio_activity)
  1682. htb_deactivate(q, parent);
  1683. /* remove from evt list because of level change */
  1684. if (parent->cmode != HTB_CAN_SEND) {
  1685. htb_safe_rb_erase(&parent->pq_node, &q->hlevel[0].wait_pq);
  1686. parent->cmode = HTB_CAN_SEND;
  1687. }
  1688. parent->level = (parent->parent ? parent->parent->level
  1689. : TC_HTB_MAXDEPTH) - 1;
  1690. memset(&parent->inner, 0, sizeof(parent->inner));
  1691. }
  1692. /* leaf (we) needs elementary qdisc */
  1693. cl->leaf.q = new_q ? new_q : &noop_qdisc;
  1694. if (q->offload)
  1695. cl->leaf.offload_queue = dev_queue;
  1696. cl->parent = parent;
  1697. /* set class to be in HTB_CAN_SEND state */
  1698. cl->tokens = PSCHED_TICKS2NS(hopt->buffer);
  1699. cl->ctokens = PSCHED_TICKS2NS(hopt->cbuffer);
  1700. cl->mbuffer = 60ULL * NSEC_PER_SEC; /* 1min */
  1701. cl->t_c = ktime_get_ns();
  1702. cl->cmode = HTB_CAN_SEND;
  1703. /* attach to the hash list and parent's family */
  1704. qdisc_class_hash_insert(&q->clhash, &cl->common);
  1705. if (parent)
  1706. parent->children++;
  1707. if (cl->leaf.q != &noop_qdisc)
  1708. qdisc_hash_add(cl->leaf.q, true);
  1709. } else {
  1710. if (tca[TCA_RATE]) {
  1711. err = gen_replace_estimator(&cl->bstats, NULL,
  1712. &cl->rate_est,
  1713. NULL,
  1714. true,
  1715. tca[TCA_RATE]);
  1716. if (err)
  1717. return err;
  1718. }
  1719. if (q->offload) {
  1720. struct net_device *dev = qdisc_dev(sch);
  1721. offload_opt = (struct tc_htb_qopt_offload) {
  1722. .command = TC_HTB_NODE_MODIFY,
  1723. .classid = cl->common.classid,
  1724. .rate = max_t(u64, hopt->rate.rate, rate64),
  1725. .ceil = max_t(u64, hopt->ceil.rate, ceil64),
  1726. .extack = extack,
  1727. };
  1728. err = htb_offload(dev, &offload_opt);
  1729. if (err)
  1730. /* Estimator was replaced, and rollback may fail
  1731. * as well, so we don't try to recover it, and
  1732. * the estimator won't work property with the
  1733. * offload anyway, because bstats are updated
  1734. * only when the stats are queried.
  1735. */
  1736. return err;
  1737. }
  1738. sch_tree_lock(sch);
  1739. }
  1740. psched_ratecfg_precompute(&cl->rate, &hopt->rate, rate64);
  1741. psched_ratecfg_precompute(&cl->ceil, &hopt->ceil, ceil64);
  1742. /* it used to be a nasty bug here, we have to check that node
  1743. * is really leaf before changing cl->leaf !
  1744. */
  1745. if (!cl->level) {
  1746. u64 quantum = cl->rate.rate_bytes_ps;
  1747. do_div(quantum, q->rate2quantum);
  1748. cl->quantum = min_t(u64, quantum, INT_MAX);
  1749. if (!hopt->quantum && cl->quantum < 1000) {
  1750. warn = -1;
  1751. cl->quantum = 1000;
  1752. }
  1753. if (!hopt->quantum && cl->quantum > 200000) {
  1754. warn = 1;
  1755. cl->quantum = 200000;
  1756. }
  1757. if (hopt->quantum)
  1758. cl->quantum = hopt->quantum;
  1759. if ((cl->prio = hopt->prio) >= TC_HTB_NUMPRIO)
  1760. cl->prio = TC_HTB_NUMPRIO - 1;
  1761. }
  1762. cl->buffer = PSCHED_TICKS2NS(hopt->buffer);
  1763. cl->cbuffer = PSCHED_TICKS2NS(hopt->cbuffer);
  1764. sch_tree_unlock(sch);
  1765. qdisc_put(parent_qdisc);
  1766. if (warn)
  1767. pr_warn("HTB: quantum of class %X is %s. Consider r2q change.\n",
  1768. cl->common.classid, (warn == -1 ? "small" : "big"));
  1769. qdisc_class_hash_grow(sch, &q->clhash);
  1770. *arg = (unsigned long)cl;
  1771. return 0;
  1772. err_kill_estimator:
  1773. gen_kill_estimator(&cl->rate_est);
  1774. err_block_put:
  1775. tcf_block_put(cl->block);
  1776. kfree(cl);
  1777. failure:
  1778. return err;
  1779. }
  1780. static struct tcf_block *htb_tcf_block(struct Qdisc *sch, unsigned long arg,
  1781. struct netlink_ext_ack *extack)
  1782. {
  1783. struct htb_sched *q = qdisc_priv(sch);
  1784. struct htb_class *cl = (struct htb_class *)arg;
  1785. return cl ? cl->block : q->block;
  1786. }
  1787. static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
  1788. u32 classid)
  1789. {
  1790. struct htb_class *cl = htb_find(classid, sch);
  1791. /*if (cl && !cl->level) return 0;
  1792. * The line above used to be there to prevent attaching filters to
  1793. * leaves. But at least tc_index filter uses this just to get class
  1794. * for other reasons so that we have to allow for it.
  1795. * ----
  1796. * 19.6.2002 As Werner explained it is ok - bind filter is just
  1797. * another way to "lock" the class - unlike "get" this lock can
  1798. * be broken by class during destroy IIUC.
  1799. */
  1800. if (cl)
  1801. cl->filter_cnt++;
  1802. return (unsigned long)cl;
  1803. }
  1804. static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
  1805. {
  1806. struct htb_class *cl = (struct htb_class *)arg;
  1807. if (cl)
  1808. cl->filter_cnt--;
  1809. }
  1810. static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  1811. {
  1812. struct htb_sched *q = qdisc_priv(sch);
  1813. struct htb_class *cl;
  1814. unsigned int i;
  1815. if (arg->stop)
  1816. return;
  1817. for (i = 0; i < q->clhash.hashsize; i++) {
  1818. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
  1819. if (!tc_qdisc_stats_dump(sch, (unsigned long)cl, arg))
  1820. return;
  1821. }
  1822. }
  1823. }
  1824. static const struct Qdisc_class_ops htb_class_ops = {
  1825. .select_queue = htb_select_queue,
  1826. .graft = htb_graft,
  1827. .leaf = htb_leaf,
  1828. .qlen_notify = htb_qlen_notify,
  1829. .find = htb_search,
  1830. .change = htb_change_class,
  1831. .delete = htb_delete,
  1832. .walk = htb_walk,
  1833. .tcf_block = htb_tcf_block,
  1834. .bind_tcf = htb_bind_filter,
  1835. .unbind_tcf = htb_unbind_filter,
  1836. .dump = htb_dump_class,
  1837. .dump_stats = htb_dump_class_stats,
  1838. };
  1839. static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
  1840. .cl_ops = &htb_class_ops,
  1841. .id = "htb",
  1842. .priv_size = sizeof(struct htb_sched),
  1843. .enqueue = htb_enqueue,
  1844. .dequeue = htb_dequeue,
  1845. .peek = qdisc_peek_dequeued,
  1846. .init = htb_init,
  1847. .attach = htb_attach,
  1848. .reset = htb_reset,
  1849. .destroy = htb_destroy,
  1850. .dump = htb_dump,
  1851. .owner = THIS_MODULE,
  1852. };
  1853. static int __init htb_module_init(void)
  1854. {
  1855. return register_qdisc(&htb_qdisc_ops);
  1856. }
  1857. static void __exit htb_module_exit(void)
  1858. {
  1859. unregister_qdisc(&htb_qdisc_ops);
  1860. }
  1861. module_init(htb_module_init)
  1862. module_exit(htb_module_exit)
  1863. MODULE_LICENSE("GPL");