sch_htb.c 56 KB

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