sch_api.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /*
  2. * net/sched/sch_api.c Packet scheduler API.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. *
  11. * Fixes:
  12. *
  13. * Rani Assaf <rani@magic.metawire.com> :980802: JIFFIES and CPU clock sources are repaired.
  14. * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
  15. * Jamal Hadi Salim <hadi@nortelnetworks.com>: 990601: ingress support
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/string.h>
  21. #include <linux/errno.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/init.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/kmod.h>
  27. #include <linux/list.h>
  28. #include <linux/hrtimer.h>
  29. #include <linux/lockdep.h>
  30. #include <linux/slab.h>
  31. #include <linux/hashtable.h>
  32. #include <net/net_namespace.h>
  33. #include <net/sock.h>
  34. #include <net/netlink.h>
  35. #include <net/pkt_sched.h>
  36. #include <net/pkt_cls.h>
  37. /*
  38. Short review.
  39. -------------
  40. This file consists of two interrelated parts:
  41. 1. queueing disciplines manager frontend.
  42. 2. traffic classes manager frontend.
  43. Generally, queueing discipline ("qdisc") is a black box,
  44. which is able to enqueue packets and to dequeue them (when
  45. device is ready to send something) in order and at times
  46. determined by algorithm hidden in it.
  47. qdisc's are divided to two categories:
  48. - "queues", which have no internal structure visible from outside.
  49. - "schedulers", which split all the packets to "traffic classes",
  50. using "packet classifiers" (look at cls_api.c)
  51. In turn, classes may have child qdiscs (as rule, queues)
  52. attached to them etc. etc. etc.
  53. The goal of the routines in this file is to translate
  54. information supplied by user in the form of handles
  55. to more intelligible for kernel form, to make some sanity
  56. checks and part of work, which is common to all qdiscs
  57. and to provide rtnetlink notifications.
  58. All real intelligent work is done inside qdisc modules.
  59. Every discipline has two major routines: enqueue and dequeue.
  60. ---dequeue
  61. dequeue usually returns a skb to send. It is allowed to return NULL,
  62. but it does not mean that queue is empty, it just means that
  63. discipline does not want to send anything this time.
  64. Queue is really empty if q->q.qlen == 0.
  65. For complicated disciplines with multiple queues q->q is not
  66. real packet queue, but however q->q.qlen must be valid.
  67. ---enqueue
  68. enqueue returns 0, if packet was enqueued successfully.
  69. If packet (this one or another one) was dropped, it returns
  70. not zero error code.
  71. NET_XMIT_DROP - this packet dropped
  72. Expected action: do not backoff, but wait until queue will clear.
  73. NET_XMIT_CN - probably this packet enqueued, but another one dropped.
  74. Expected action: backoff or ignore
  75. Auxiliary routines:
  76. ---peek
  77. like dequeue but without removing a packet from the queue
  78. ---reset
  79. returns qdisc to initial state: purge all buffers, clear all
  80. timers, counters (except for statistics) etc.
  81. ---init
  82. initializes newly created qdisc.
  83. ---destroy
  84. destroys resources allocated by init and during lifetime of qdisc.
  85. ---change
  86. changes qdisc parameters.
  87. */
  88. /* Protects list of registered TC modules. It is pure SMP lock. */
  89. static DEFINE_RWLOCK(qdisc_mod_lock);
  90. /************************************************
  91. * Queueing disciplines manipulation. *
  92. ************************************************/
  93. /* The list of all installed queueing disciplines. */
  94. static struct Qdisc_ops *qdisc_base;
  95. /* Register/unregister queueing discipline */
  96. int register_qdisc(struct Qdisc_ops *qops)
  97. {
  98. struct Qdisc_ops *q, **qp;
  99. int rc = -EEXIST;
  100. write_lock(&qdisc_mod_lock);
  101. for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
  102. if (!strcmp(qops->id, q->id))
  103. goto out;
  104. if (qops->enqueue == NULL)
  105. qops->enqueue = noop_qdisc_ops.enqueue;
  106. if (qops->peek == NULL) {
  107. if (qops->dequeue == NULL)
  108. qops->peek = noop_qdisc_ops.peek;
  109. else
  110. goto out_einval;
  111. }
  112. if (qops->dequeue == NULL)
  113. qops->dequeue = noop_qdisc_ops.dequeue;
  114. if (qops->cl_ops) {
  115. const struct Qdisc_class_ops *cops = qops->cl_ops;
  116. if (!(cops->find && cops->walk && cops->leaf))
  117. goto out_einval;
  118. if (cops->tcf_block && !(cops->bind_tcf && cops->unbind_tcf))
  119. goto out_einval;
  120. }
  121. qops->next = NULL;
  122. *qp = qops;
  123. rc = 0;
  124. out:
  125. write_unlock(&qdisc_mod_lock);
  126. return rc;
  127. out_einval:
  128. rc = -EINVAL;
  129. goto out;
  130. }
  131. EXPORT_SYMBOL(register_qdisc);
  132. int unregister_qdisc(struct Qdisc_ops *qops)
  133. {
  134. struct Qdisc_ops *q, **qp;
  135. int err = -ENOENT;
  136. write_lock(&qdisc_mod_lock);
  137. for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
  138. if (q == qops)
  139. break;
  140. if (q) {
  141. *qp = q->next;
  142. q->next = NULL;
  143. err = 0;
  144. }
  145. write_unlock(&qdisc_mod_lock);
  146. return err;
  147. }
  148. EXPORT_SYMBOL(unregister_qdisc);
  149. /* Get default qdisc if not otherwise specified */
  150. void qdisc_get_default(char *name, size_t len)
  151. {
  152. read_lock(&qdisc_mod_lock);
  153. strlcpy(name, default_qdisc_ops->id, len);
  154. read_unlock(&qdisc_mod_lock);
  155. }
  156. static struct Qdisc_ops *qdisc_lookup_default(const char *name)
  157. {
  158. struct Qdisc_ops *q = NULL;
  159. for (q = qdisc_base; q; q = q->next) {
  160. if (!strcmp(name, q->id)) {
  161. if (!try_module_get(q->owner))
  162. q = NULL;
  163. break;
  164. }
  165. }
  166. return q;
  167. }
  168. /* Set new default qdisc to use */
  169. int qdisc_set_default(const char *name)
  170. {
  171. const struct Qdisc_ops *ops;
  172. if (!capable(CAP_NET_ADMIN))
  173. return -EPERM;
  174. write_lock(&qdisc_mod_lock);
  175. ops = qdisc_lookup_default(name);
  176. if (!ops) {
  177. /* Not found, drop lock and try to load module */
  178. write_unlock(&qdisc_mod_lock);
  179. request_module("sch_%s", name);
  180. write_lock(&qdisc_mod_lock);
  181. ops = qdisc_lookup_default(name);
  182. }
  183. if (ops) {
  184. /* Set new default */
  185. module_put(default_qdisc_ops->owner);
  186. default_qdisc_ops = ops;
  187. }
  188. write_unlock(&qdisc_mod_lock);
  189. return ops ? 0 : -ENOENT;
  190. }
  191. #ifdef CONFIG_NET_SCH_DEFAULT
  192. /* Set default value from kernel config */
  193. static int __init sch_default_qdisc(void)
  194. {
  195. return qdisc_set_default(CONFIG_DEFAULT_NET_SCH);
  196. }
  197. late_initcall(sch_default_qdisc);
  198. #endif
  199. /* We know handle. Find qdisc among all qdisc's attached to device
  200. * (root qdisc, all its children, children of children etc.)
  201. * Note: caller either uses rtnl or rcu_read_lock()
  202. */
  203. static struct Qdisc *qdisc_match_from_root(struct Qdisc *root, u32 handle)
  204. {
  205. struct Qdisc *q;
  206. if (!qdisc_dev(root))
  207. return (root->handle == handle ? root : NULL);
  208. if (!(root->flags & TCQ_F_BUILTIN) &&
  209. root->handle == handle)
  210. return root;
  211. hash_for_each_possible_rcu(qdisc_dev(root)->qdisc_hash, q, hash, handle) {
  212. if (q->handle == handle)
  213. return q;
  214. }
  215. return NULL;
  216. }
  217. void qdisc_hash_add(struct Qdisc *q, bool invisible)
  218. {
  219. if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS)) {
  220. ASSERT_RTNL();
  221. hash_add_rcu(qdisc_dev(q)->qdisc_hash, &q->hash, q->handle);
  222. if (invisible)
  223. q->flags |= TCQ_F_INVISIBLE;
  224. }
  225. }
  226. EXPORT_SYMBOL(qdisc_hash_add);
  227. void qdisc_hash_del(struct Qdisc *q)
  228. {
  229. if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS)) {
  230. ASSERT_RTNL();
  231. hash_del_rcu(&q->hash);
  232. }
  233. }
  234. EXPORT_SYMBOL(qdisc_hash_del);
  235. struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
  236. {
  237. struct Qdisc *q;
  238. if (!handle)
  239. return NULL;
  240. q = qdisc_match_from_root(dev->qdisc, handle);
  241. if (q)
  242. goto out;
  243. if (dev_ingress_queue(dev))
  244. q = qdisc_match_from_root(
  245. dev_ingress_queue(dev)->qdisc_sleeping,
  246. handle);
  247. out:
  248. return q;
  249. }
  250. static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid)
  251. {
  252. unsigned long cl;
  253. struct Qdisc *leaf;
  254. const struct Qdisc_class_ops *cops = p->ops->cl_ops;
  255. if (cops == NULL)
  256. return NULL;
  257. cl = cops->find(p, classid);
  258. if (cl == 0)
  259. return NULL;
  260. leaf = cops->leaf(p, cl);
  261. return leaf;
  262. }
  263. /* Find queueing discipline by name */
  264. static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind)
  265. {
  266. struct Qdisc_ops *q = NULL;
  267. if (kind) {
  268. read_lock(&qdisc_mod_lock);
  269. for (q = qdisc_base; q; q = q->next) {
  270. if (nla_strcmp(kind, q->id) == 0) {
  271. if (!try_module_get(q->owner))
  272. q = NULL;
  273. break;
  274. }
  275. }
  276. read_unlock(&qdisc_mod_lock);
  277. }
  278. return q;
  279. }
  280. /* The linklayer setting were not transferred from iproute2, in older
  281. * versions, and the rate tables lookup systems have been dropped in
  282. * the kernel. To keep backward compatible with older iproute2 tc
  283. * utils, we detect the linklayer setting by detecting if the rate
  284. * table were modified.
  285. *
  286. * For linklayer ATM table entries, the rate table will be aligned to
  287. * 48 bytes, thus some table entries will contain the same value. The
  288. * mpu (min packet unit) is also encoded into the old rate table, thus
  289. * starting from the mpu, we find low and high table entries for
  290. * mapping this cell. If these entries contain the same value, when
  291. * the rate tables have been modified for linklayer ATM.
  292. *
  293. * This is done by rounding mpu to the nearest 48 bytes cell/entry,
  294. * and then roundup to the next cell, calc the table entry one below,
  295. * and compare.
  296. */
  297. static __u8 __detect_linklayer(struct tc_ratespec *r, __u32 *rtab)
  298. {
  299. int low = roundup(r->mpu, 48);
  300. int high = roundup(low+1, 48);
  301. int cell_low = low >> r->cell_log;
  302. int cell_high = (high >> r->cell_log) - 1;
  303. /* rtab is too inaccurate at rates > 100Mbit/s */
  304. if ((r->rate > (100000000/8)) || (rtab[0] == 0)) {
  305. pr_debug("TC linklayer: Giving up ATM detection\n");
  306. return TC_LINKLAYER_ETHERNET;
  307. }
  308. if ((cell_high > cell_low) && (cell_high < 256)
  309. && (rtab[cell_low] == rtab[cell_high])) {
  310. pr_debug("TC linklayer: Detected ATM, low(%d)=high(%d)=%u\n",
  311. cell_low, cell_high, rtab[cell_high]);
  312. return TC_LINKLAYER_ATM;
  313. }
  314. return TC_LINKLAYER_ETHERNET;
  315. }
  316. static struct qdisc_rate_table *qdisc_rtab_list;
  317. struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r,
  318. struct nlattr *tab)
  319. {
  320. struct qdisc_rate_table *rtab;
  321. if (tab == NULL || r->rate == 0 || r->cell_log == 0 ||
  322. nla_len(tab) != TC_RTAB_SIZE)
  323. return NULL;
  324. for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
  325. if (!memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) &&
  326. !memcmp(&rtab->data, nla_data(tab), 1024)) {
  327. rtab->refcnt++;
  328. return rtab;
  329. }
  330. }
  331. rtab = kmalloc(sizeof(*rtab), GFP_KERNEL);
  332. if (rtab) {
  333. rtab->rate = *r;
  334. rtab->refcnt = 1;
  335. memcpy(rtab->data, nla_data(tab), 1024);
  336. if (r->linklayer == TC_LINKLAYER_UNAWARE)
  337. r->linklayer = __detect_linklayer(r, rtab->data);
  338. rtab->next = qdisc_rtab_list;
  339. qdisc_rtab_list = rtab;
  340. }
  341. return rtab;
  342. }
  343. EXPORT_SYMBOL(qdisc_get_rtab);
  344. void qdisc_put_rtab(struct qdisc_rate_table *tab)
  345. {
  346. struct qdisc_rate_table *rtab, **rtabp;
  347. if (!tab || --tab->refcnt)
  348. return;
  349. for (rtabp = &qdisc_rtab_list;
  350. (rtab = *rtabp) != NULL;
  351. rtabp = &rtab->next) {
  352. if (rtab == tab) {
  353. *rtabp = rtab->next;
  354. kfree(rtab);
  355. return;
  356. }
  357. }
  358. }
  359. EXPORT_SYMBOL(qdisc_put_rtab);
  360. static LIST_HEAD(qdisc_stab_list);
  361. static const struct nla_policy stab_policy[TCA_STAB_MAX + 1] = {
  362. [TCA_STAB_BASE] = { .len = sizeof(struct tc_sizespec) },
  363. [TCA_STAB_DATA] = { .type = NLA_BINARY },
  364. };
  365. static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt)
  366. {
  367. struct nlattr *tb[TCA_STAB_MAX + 1];
  368. struct qdisc_size_table *stab;
  369. struct tc_sizespec *s;
  370. unsigned int tsize = 0;
  371. u16 *tab = NULL;
  372. int err;
  373. err = nla_parse_nested(tb, TCA_STAB_MAX, opt, stab_policy, NULL);
  374. if (err < 0)
  375. return ERR_PTR(err);
  376. if (!tb[TCA_STAB_BASE])
  377. return ERR_PTR(-EINVAL);
  378. s = nla_data(tb[TCA_STAB_BASE]);
  379. if (s->tsize > 0) {
  380. if (!tb[TCA_STAB_DATA])
  381. return ERR_PTR(-EINVAL);
  382. tab = nla_data(tb[TCA_STAB_DATA]);
  383. tsize = nla_len(tb[TCA_STAB_DATA]) / sizeof(u16);
  384. }
  385. if (tsize != s->tsize || (!tab && tsize > 0))
  386. return ERR_PTR(-EINVAL);
  387. list_for_each_entry(stab, &qdisc_stab_list, list) {
  388. if (memcmp(&stab->szopts, s, sizeof(*s)))
  389. continue;
  390. if (tsize > 0 && memcmp(stab->data, tab, tsize * sizeof(u16)))
  391. continue;
  392. stab->refcnt++;
  393. return stab;
  394. }
  395. stab = kmalloc(sizeof(*stab) + tsize * sizeof(u16), GFP_KERNEL);
  396. if (!stab)
  397. return ERR_PTR(-ENOMEM);
  398. stab->refcnt = 1;
  399. stab->szopts = *s;
  400. if (tsize > 0)
  401. memcpy(stab->data, tab, tsize * sizeof(u16));
  402. list_add_tail(&stab->list, &qdisc_stab_list);
  403. return stab;
  404. }
  405. static void stab_kfree_rcu(struct rcu_head *head)
  406. {
  407. kfree(container_of(head, struct qdisc_size_table, rcu));
  408. }
  409. void qdisc_put_stab(struct qdisc_size_table *tab)
  410. {
  411. if (!tab)
  412. return;
  413. if (--tab->refcnt == 0) {
  414. list_del(&tab->list);
  415. call_rcu_bh(&tab->rcu, stab_kfree_rcu);
  416. }
  417. }
  418. EXPORT_SYMBOL(qdisc_put_stab);
  419. static int qdisc_dump_stab(struct sk_buff *skb, struct qdisc_size_table *stab)
  420. {
  421. struct nlattr *nest;
  422. nest = nla_nest_start(skb, TCA_STAB);
  423. if (nest == NULL)
  424. goto nla_put_failure;
  425. if (nla_put(skb, TCA_STAB_BASE, sizeof(stab->szopts), &stab->szopts))
  426. goto nla_put_failure;
  427. nla_nest_end(skb, nest);
  428. return skb->len;
  429. nla_put_failure:
  430. return -1;
  431. }
  432. void __qdisc_calculate_pkt_len(struct sk_buff *skb,
  433. const struct qdisc_size_table *stab)
  434. {
  435. int pkt_len, slot;
  436. pkt_len = skb->len + stab->szopts.overhead;
  437. if (unlikely(!stab->szopts.tsize))
  438. goto out;
  439. slot = pkt_len + stab->szopts.cell_align;
  440. if (unlikely(slot < 0))
  441. slot = 0;
  442. slot >>= stab->szopts.cell_log;
  443. if (likely(slot < stab->szopts.tsize))
  444. pkt_len = stab->data[slot];
  445. else
  446. pkt_len = stab->data[stab->szopts.tsize - 1] *
  447. (slot / stab->szopts.tsize) +
  448. stab->data[slot % stab->szopts.tsize];
  449. pkt_len <<= stab->szopts.size_log;
  450. out:
  451. if (unlikely(pkt_len < 1))
  452. pkt_len = 1;
  453. qdisc_skb_cb(skb)->pkt_len = pkt_len;
  454. }
  455. EXPORT_SYMBOL(__qdisc_calculate_pkt_len);
  456. void qdisc_warn_nonwc(const char *txt, struct Qdisc *qdisc)
  457. {
  458. if (!(qdisc->flags & TCQ_F_WARN_NONWC)) {
  459. pr_warn("%s: %s qdisc %X: is non-work-conserving?\n",
  460. txt, qdisc->ops->id, qdisc->handle >> 16);
  461. qdisc->flags |= TCQ_F_WARN_NONWC;
  462. }
  463. }
  464. EXPORT_SYMBOL(qdisc_warn_nonwc);
  465. static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
  466. {
  467. struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
  468. timer);
  469. rcu_read_lock();
  470. __netif_schedule(qdisc_root(wd->qdisc));
  471. rcu_read_unlock();
  472. return HRTIMER_NORESTART;
  473. }
  474. void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
  475. {
  476. hrtimer_init(&wd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  477. wd->timer.function = qdisc_watchdog;
  478. wd->qdisc = qdisc;
  479. }
  480. EXPORT_SYMBOL(qdisc_watchdog_init);
  481. void qdisc_watchdog_schedule_ns(struct qdisc_watchdog *wd, u64 expires)
  482. {
  483. if (test_bit(__QDISC_STATE_DEACTIVATED,
  484. &qdisc_root_sleeping(wd->qdisc)->state))
  485. return;
  486. if (wd->last_expires == expires)
  487. return;
  488. wd->last_expires = expires;
  489. hrtimer_start(&wd->timer,
  490. ns_to_ktime(expires),
  491. HRTIMER_MODE_ABS_PINNED);
  492. }
  493. EXPORT_SYMBOL(qdisc_watchdog_schedule_ns);
  494. void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
  495. {
  496. hrtimer_cancel(&wd->timer);
  497. }
  498. EXPORT_SYMBOL(qdisc_watchdog_cancel);
  499. static struct hlist_head *qdisc_class_hash_alloc(unsigned int n)
  500. {
  501. struct hlist_head *h;
  502. unsigned int i;
  503. h = kvmalloc_array(n, sizeof(struct hlist_head), GFP_KERNEL);
  504. if (h != NULL) {
  505. for (i = 0; i < n; i++)
  506. INIT_HLIST_HEAD(&h[i]);
  507. }
  508. return h;
  509. }
  510. void qdisc_class_hash_grow(struct Qdisc *sch, struct Qdisc_class_hash *clhash)
  511. {
  512. struct Qdisc_class_common *cl;
  513. struct hlist_node *next;
  514. struct hlist_head *nhash, *ohash;
  515. unsigned int nsize, nmask, osize;
  516. unsigned int i, h;
  517. /* Rehash when load factor exceeds 0.75 */
  518. if (clhash->hashelems * 4 <= clhash->hashsize * 3)
  519. return;
  520. nsize = clhash->hashsize * 2;
  521. nmask = nsize - 1;
  522. nhash = qdisc_class_hash_alloc(nsize);
  523. if (nhash == NULL)
  524. return;
  525. ohash = clhash->hash;
  526. osize = clhash->hashsize;
  527. sch_tree_lock(sch);
  528. for (i = 0; i < osize; i++) {
  529. hlist_for_each_entry_safe(cl, next, &ohash[i], hnode) {
  530. h = qdisc_class_hash(cl->classid, nmask);
  531. hlist_add_head(&cl->hnode, &nhash[h]);
  532. }
  533. }
  534. clhash->hash = nhash;
  535. clhash->hashsize = nsize;
  536. clhash->hashmask = nmask;
  537. sch_tree_unlock(sch);
  538. kvfree(ohash);
  539. }
  540. EXPORT_SYMBOL(qdisc_class_hash_grow);
  541. int qdisc_class_hash_init(struct Qdisc_class_hash *clhash)
  542. {
  543. unsigned int size = 4;
  544. clhash->hash = qdisc_class_hash_alloc(size);
  545. if (clhash->hash == NULL)
  546. return -ENOMEM;
  547. clhash->hashsize = size;
  548. clhash->hashmask = size - 1;
  549. clhash->hashelems = 0;
  550. return 0;
  551. }
  552. EXPORT_SYMBOL(qdisc_class_hash_init);
  553. void qdisc_class_hash_destroy(struct Qdisc_class_hash *clhash)
  554. {
  555. kvfree(clhash->hash);
  556. }
  557. EXPORT_SYMBOL(qdisc_class_hash_destroy);
  558. void qdisc_class_hash_insert(struct Qdisc_class_hash *clhash,
  559. struct Qdisc_class_common *cl)
  560. {
  561. unsigned int h;
  562. INIT_HLIST_NODE(&cl->hnode);
  563. h = qdisc_class_hash(cl->classid, clhash->hashmask);
  564. hlist_add_head(&cl->hnode, &clhash->hash[h]);
  565. clhash->hashelems++;
  566. }
  567. EXPORT_SYMBOL(qdisc_class_hash_insert);
  568. void qdisc_class_hash_remove(struct Qdisc_class_hash *clhash,
  569. struct Qdisc_class_common *cl)
  570. {
  571. hlist_del(&cl->hnode);
  572. clhash->hashelems--;
  573. }
  574. EXPORT_SYMBOL(qdisc_class_hash_remove);
  575. /* Allocate an unique handle from space managed by kernel
  576. * Possible range is [8000-FFFF]:0000 (0x8000 values)
  577. */
  578. static u32 qdisc_alloc_handle(struct net_device *dev)
  579. {
  580. int i = 0x8000;
  581. static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
  582. do {
  583. autohandle += TC_H_MAKE(0x10000U, 0);
  584. if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
  585. autohandle = TC_H_MAKE(0x80000000U, 0);
  586. if (!qdisc_lookup(dev, autohandle))
  587. return autohandle;
  588. cond_resched();
  589. } while (--i > 0);
  590. return 0;
  591. }
  592. void qdisc_tree_reduce_backlog(struct Qdisc *sch, unsigned int n,
  593. unsigned int len)
  594. {
  595. const struct Qdisc_class_ops *cops;
  596. unsigned long cl;
  597. u32 parentid;
  598. bool notify;
  599. int drops;
  600. if (n == 0 && len == 0)
  601. return;
  602. drops = max_t(int, n, 0);
  603. rcu_read_lock();
  604. while ((parentid = sch->parent)) {
  605. if (TC_H_MAJ(parentid) == TC_H_MAJ(TC_H_INGRESS))
  606. break;
  607. if (sch->flags & TCQ_F_NOPARENT)
  608. break;
  609. /* Notify parent qdisc only if child qdisc becomes empty.
  610. *
  611. * If child was empty even before update then backlog
  612. * counter is screwed and we skip notification because
  613. * parent class is already passive.
  614. */
  615. notify = !sch->q.qlen && !WARN_ON_ONCE(!n);
  616. /* TODO: perform the search on a per txq basis */
  617. sch = qdisc_lookup(qdisc_dev(sch), TC_H_MAJ(parentid));
  618. if (sch == NULL) {
  619. WARN_ON_ONCE(parentid != TC_H_ROOT);
  620. break;
  621. }
  622. cops = sch->ops->cl_ops;
  623. if (notify && cops->qlen_notify) {
  624. cl = cops->find(sch, parentid);
  625. cops->qlen_notify(sch, cl);
  626. }
  627. sch->q.qlen -= n;
  628. sch->qstats.backlog -= len;
  629. __qdisc_qstats_drop(sch, drops);
  630. }
  631. rcu_read_unlock();
  632. }
  633. EXPORT_SYMBOL(qdisc_tree_reduce_backlog);
  634. static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
  635. u32 portid, u32 seq, u16 flags, int event)
  636. {
  637. struct gnet_stats_basic_cpu __percpu *cpu_bstats = NULL;
  638. struct gnet_stats_queue __percpu *cpu_qstats = NULL;
  639. struct tcmsg *tcm;
  640. struct nlmsghdr *nlh;
  641. unsigned char *b = skb_tail_pointer(skb);
  642. struct gnet_dump d;
  643. struct qdisc_size_table *stab;
  644. __u32 qlen;
  645. cond_resched();
  646. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  647. if (!nlh)
  648. goto out_nlmsg_trim;
  649. tcm = nlmsg_data(nlh);
  650. tcm->tcm_family = AF_UNSPEC;
  651. tcm->tcm__pad1 = 0;
  652. tcm->tcm__pad2 = 0;
  653. tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
  654. tcm->tcm_parent = clid;
  655. tcm->tcm_handle = q->handle;
  656. tcm->tcm_info = refcount_read(&q->refcnt);
  657. if (nla_put_string(skb, TCA_KIND, q->ops->id))
  658. goto nla_put_failure;
  659. if (q->ops->dump && q->ops->dump(q, skb) < 0)
  660. goto nla_put_failure;
  661. qlen = q->q.qlen;
  662. stab = rtnl_dereference(q->stab);
  663. if (stab && qdisc_dump_stab(skb, stab) < 0)
  664. goto nla_put_failure;
  665. if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS,
  666. NULL, &d, TCA_PAD) < 0)
  667. goto nla_put_failure;
  668. if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
  669. goto nla_put_failure;
  670. if (qdisc_is_percpu_stats(q)) {
  671. cpu_bstats = q->cpu_bstats;
  672. cpu_qstats = q->cpu_qstats;
  673. }
  674. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(q),
  675. &d, cpu_bstats, &q->bstats) < 0 ||
  676. gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
  677. gnet_stats_copy_queue(&d, cpu_qstats, &q->qstats, qlen) < 0)
  678. goto nla_put_failure;
  679. if (gnet_stats_finish_copy(&d) < 0)
  680. goto nla_put_failure;
  681. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  682. return skb->len;
  683. out_nlmsg_trim:
  684. nla_put_failure:
  685. nlmsg_trim(skb, b);
  686. return -1;
  687. }
  688. static bool tc_qdisc_dump_ignore(struct Qdisc *q, bool dump_invisible)
  689. {
  690. if (q->flags & TCQ_F_BUILTIN)
  691. return true;
  692. if ((q->flags & TCQ_F_INVISIBLE) && !dump_invisible)
  693. return true;
  694. return false;
  695. }
  696. static int qdisc_notify(struct net *net, struct sk_buff *oskb,
  697. struct nlmsghdr *n, u32 clid,
  698. struct Qdisc *old, struct Qdisc *new)
  699. {
  700. struct sk_buff *skb;
  701. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  702. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  703. if (!skb)
  704. return -ENOBUFS;
  705. if (old && !tc_qdisc_dump_ignore(old, false)) {
  706. if (tc_fill_qdisc(skb, old, clid, portid, n->nlmsg_seq,
  707. 0, RTM_DELQDISC) < 0)
  708. goto err_out;
  709. }
  710. if (new && !tc_qdisc_dump_ignore(new, false)) {
  711. if (tc_fill_qdisc(skb, new, clid, portid, n->nlmsg_seq,
  712. old ? NLM_F_REPLACE : 0, RTM_NEWQDISC) < 0)
  713. goto err_out;
  714. }
  715. if (skb->len)
  716. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  717. n->nlmsg_flags & NLM_F_ECHO);
  718. err_out:
  719. kfree_skb(skb);
  720. return -EINVAL;
  721. }
  722. static void notify_and_destroy(struct net *net, struct sk_buff *skb,
  723. struct nlmsghdr *n, u32 clid,
  724. struct Qdisc *old, struct Qdisc *new)
  725. {
  726. if (new || old)
  727. qdisc_notify(net, skb, n, clid, old, new);
  728. if (old)
  729. qdisc_destroy(old);
  730. }
  731. /* Graft qdisc "new" to class "classid" of qdisc "parent" or
  732. * to device "dev".
  733. *
  734. * When appropriate send a netlink notification using 'skb'
  735. * and "n".
  736. *
  737. * On success, destroy old qdisc.
  738. */
  739. static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
  740. struct sk_buff *skb, struct nlmsghdr *n, u32 classid,
  741. struct Qdisc *new, struct Qdisc *old)
  742. {
  743. struct Qdisc *q = old;
  744. struct net *net = dev_net(dev);
  745. int err = 0;
  746. if (parent == NULL) {
  747. unsigned int i, num_q, ingress;
  748. ingress = 0;
  749. num_q = dev->num_tx_queues;
  750. if ((q && q->flags & TCQ_F_INGRESS) ||
  751. (new && new->flags & TCQ_F_INGRESS)) {
  752. num_q = 1;
  753. ingress = 1;
  754. if (!dev_ingress_queue(dev))
  755. return -ENOENT;
  756. }
  757. if (dev->flags & IFF_UP)
  758. dev_deactivate(dev);
  759. if (new && new->ops->attach)
  760. goto skip;
  761. for (i = 0; i < num_q; i++) {
  762. struct netdev_queue *dev_queue = dev_ingress_queue(dev);
  763. if (!ingress)
  764. dev_queue = netdev_get_tx_queue(dev, i);
  765. old = dev_graft_qdisc(dev_queue, new);
  766. if (new && i > 0)
  767. qdisc_refcount_inc(new);
  768. if (!ingress)
  769. qdisc_destroy(old);
  770. }
  771. skip:
  772. if (!ingress) {
  773. notify_and_destroy(net, skb, n, classid,
  774. dev->qdisc, new);
  775. if (new && !new->ops->attach)
  776. qdisc_refcount_inc(new);
  777. dev->qdisc = new ? : &noop_qdisc;
  778. if (new && new->ops->attach)
  779. new->ops->attach(new);
  780. } else {
  781. notify_and_destroy(net, skb, n, classid, old, new);
  782. }
  783. if (dev->flags & IFF_UP)
  784. dev_activate(dev);
  785. } else {
  786. const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
  787. err = -EOPNOTSUPP;
  788. if (cops && cops->graft) {
  789. unsigned long cl = cops->find(parent, classid);
  790. if (cl)
  791. err = cops->graft(parent, cl, new, &old);
  792. else
  793. err = -ENOENT;
  794. }
  795. if (!err)
  796. notify_and_destroy(net, skb, n, classid, old, new);
  797. }
  798. return err;
  799. }
  800. /* lockdep annotation is needed for ingress; egress gets it only for name */
  801. static struct lock_class_key qdisc_tx_lock;
  802. static struct lock_class_key qdisc_rx_lock;
  803. /*
  804. Allocate and initialize new qdisc.
  805. Parameters are passed via opt.
  806. */
  807. static struct Qdisc *qdisc_create(struct net_device *dev,
  808. struct netdev_queue *dev_queue,
  809. struct Qdisc *p, u32 parent, u32 handle,
  810. struct nlattr **tca, int *errp)
  811. {
  812. int err;
  813. struct nlattr *kind = tca[TCA_KIND];
  814. struct Qdisc *sch;
  815. struct Qdisc_ops *ops;
  816. struct qdisc_size_table *stab;
  817. ops = qdisc_lookup_ops(kind);
  818. #ifdef CONFIG_MODULES
  819. if (ops == NULL && kind != NULL) {
  820. char name[IFNAMSIZ];
  821. if (nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
  822. /* We dropped the RTNL semaphore in order to
  823. * perform the module load. So, even if we
  824. * succeeded in loading the module we have to
  825. * tell the caller to replay the request. We
  826. * indicate this using -EAGAIN.
  827. * We replay the request because the device may
  828. * go away in the mean time.
  829. */
  830. rtnl_unlock();
  831. request_module("sch_%s", name);
  832. rtnl_lock();
  833. ops = qdisc_lookup_ops(kind);
  834. if (ops != NULL) {
  835. /* We will try again qdisc_lookup_ops,
  836. * so don't keep a reference.
  837. */
  838. module_put(ops->owner);
  839. err = -EAGAIN;
  840. goto err_out;
  841. }
  842. }
  843. }
  844. #endif
  845. err = -ENOENT;
  846. if (ops == NULL)
  847. goto err_out;
  848. sch = qdisc_alloc(dev_queue, ops);
  849. if (IS_ERR(sch)) {
  850. err = PTR_ERR(sch);
  851. goto err_out2;
  852. }
  853. sch->parent = parent;
  854. if (handle == TC_H_INGRESS) {
  855. sch->flags |= TCQ_F_INGRESS;
  856. handle = TC_H_MAKE(TC_H_INGRESS, 0);
  857. lockdep_set_class(qdisc_lock(sch), &qdisc_rx_lock);
  858. } else {
  859. if (handle == 0) {
  860. handle = qdisc_alloc_handle(dev);
  861. err = -ENOMEM;
  862. if (handle == 0)
  863. goto err_out3;
  864. }
  865. lockdep_set_class(qdisc_lock(sch), &qdisc_tx_lock);
  866. if (!netif_is_multiqueue(dev))
  867. sch->flags |= TCQ_F_ONETXQUEUE;
  868. }
  869. sch->handle = handle;
  870. /* This exist to keep backward compatible with a userspace
  871. * loophole, what allowed userspace to get IFF_NO_QUEUE
  872. * facility on older kernels by setting tx_queue_len=0 (prior
  873. * to qdisc init), and then forgot to reinit tx_queue_len
  874. * before again attaching a qdisc.
  875. */
  876. if ((dev->priv_flags & IFF_NO_QUEUE) && (dev->tx_queue_len == 0)) {
  877. dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
  878. netdev_info(dev, "Caught tx_queue_len zero misconfig\n");
  879. }
  880. if (!ops->init || (err = ops->init(sch, tca[TCA_OPTIONS])) == 0) {
  881. if (qdisc_is_percpu_stats(sch)) {
  882. sch->cpu_bstats =
  883. netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
  884. if (!sch->cpu_bstats)
  885. goto err_out4;
  886. sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  887. if (!sch->cpu_qstats)
  888. goto err_out4;
  889. }
  890. if (tca[TCA_STAB]) {
  891. stab = qdisc_get_stab(tca[TCA_STAB]);
  892. if (IS_ERR(stab)) {
  893. err = PTR_ERR(stab);
  894. goto err_out4;
  895. }
  896. rcu_assign_pointer(sch->stab, stab);
  897. }
  898. if (tca[TCA_RATE]) {
  899. seqcount_t *running;
  900. err = -EOPNOTSUPP;
  901. if (sch->flags & TCQ_F_MQROOT)
  902. goto err_out4;
  903. if ((sch->parent != TC_H_ROOT) &&
  904. !(sch->flags & TCQ_F_INGRESS) &&
  905. (!p || !(p->flags & TCQ_F_MQROOT)))
  906. running = qdisc_root_sleeping_running(sch);
  907. else
  908. running = &sch->running;
  909. err = gen_new_estimator(&sch->bstats,
  910. sch->cpu_bstats,
  911. &sch->rate_est,
  912. NULL,
  913. running,
  914. tca[TCA_RATE]);
  915. if (err)
  916. goto err_out4;
  917. }
  918. qdisc_hash_add(sch, false);
  919. return sch;
  920. }
  921. /* ops->init() failed, we call ->destroy() like qdisc_create_dflt() */
  922. if (ops->destroy)
  923. ops->destroy(sch);
  924. err_out3:
  925. dev_put(dev);
  926. kfree((char *) sch - sch->padded);
  927. err_out2:
  928. module_put(ops->owner);
  929. err_out:
  930. *errp = err;
  931. return NULL;
  932. err_out4:
  933. free_percpu(sch->cpu_bstats);
  934. free_percpu(sch->cpu_qstats);
  935. /*
  936. * Any broken qdiscs that would require a ops->reset() here?
  937. * The qdisc was never in action so it shouldn't be necessary.
  938. */
  939. qdisc_put_stab(rtnl_dereference(sch->stab));
  940. if (ops->destroy)
  941. ops->destroy(sch);
  942. goto err_out3;
  943. }
  944. static int qdisc_change(struct Qdisc *sch, struct nlattr **tca)
  945. {
  946. struct qdisc_size_table *ostab, *stab = NULL;
  947. int err = 0;
  948. if (tca[TCA_OPTIONS]) {
  949. if (sch->ops->change == NULL)
  950. return -EINVAL;
  951. err = sch->ops->change(sch, tca[TCA_OPTIONS]);
  952. if (err)
  953. return err;
  954. }
  955. if (tca[TCA_STAB]) {
  956. stab = qdisc_get_stab(tca[TCA_STAB]);
  957. if (IS_ERR(stab))
  958. return PTR_ERR(stab);
  959. }
  960. ostab = rtnl_dereference(sch->stab);
  961. rcu_assign_pointer(sch->stab, stab);
  962. qdisc_put_stab(ostab);
  963. if (tca[TCA_RATE]) {
  964. /* NB: ignores errors from replace_estimator
  965. because change can't be undone. */
  966. if (sch->flags & TCQ_F_MQROOT)
  967. goto out;
  968. gen_replace_estimator(&sch->bstats,
  969. sch->cpu_bstats,
  970. &sch->rate_est,
  971. NULL,
  972. qdisc_root_sleeping_running(sch),
  973. tca[TCA_RATE]);
  974. }
  975. out:
  976. return 0;
  977. }
  978. struct check_loop_arg {
  979. struct qdisc_walker w;
  980. struct Qdisc *p;
  981. int depth;
  982. };
  983. static int check_loop_fn(struct Qdisc *q, unsigned long cl,
  984. struct qdisc_walker *w);
  985. static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
  986. {
  987. struct check_loop_arg arg;
  988. if (q->ops->cl_ops == NULL)
  989. return 0;
  990. arg.w.stop = arg.w.skip = arg.w.count = 0;
  991. arg.w.fn = check_loop_fn;
  992. arg.depth = depth;
  993. arg.p = p;
  994. q->ops->cl_ops->walk(q, &arg.w);
  995. return arg.w.stop ? -ELOOP : 0;
  996. }
  997. static int
  998. check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
  999. {
  1000. struct Qdisc *leaf;
  1001. const struct Qdisc_class_ops *cops = q->ops->cl_ops;
  1002. struct check_loop_arg *arg = (struct check_loop_arg *)w;
  1003. leaf = cops->leaf(q, cl);
  1004. if (leaf) {
  1005. if (leaf == arg->p || arg->depth > 7)
  1006. return -ELOOP;
  1007. return check_loop(leaf, arg->p, arg->depth + 1);
  1008. }
  1009. return 0;
  1010. }
  1011. /*
  1012. * Delete/get qdisc.
  1013. */
  1014. static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
  1015. struct netlink_ext_ack *extack)
  1016. {
  1017. struct net *net = sock_net(skb->sk);
  1018. struct tcmsg *tcm = nlmsg_data(n);
  1019. struct nlattr *tca[TCA_MAX + 1];
  1020. struct net_device *dev;
  1021. u32 clid;
  1022. struct Qdisc *q = NULL;
  1023. struct Qdisc *p = NULL;
  1024. int err;
  1025. if ((n->nlmsg_type != RTM_GETQDISC) &&
  1026. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  1027. return -EPERM;
  1028. err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL, extack);
  1029. if (err < 0)
  1030. return err;
  1031. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  1032. if (!dev)
  1033. return -ENODEV;
  1034. clid = tcm->tcm_parent;
  1035. if (clid) {
  1036. if (clid != TC_H_ROOT) {
  1037. if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
  1038. p = qdisc_lookup(dev, TC_H_MAJ(clid));
  1039. if (!p)
  1040. return -ENOENT;
  1041. q = qdisc_leaf(p, clid);
  1042. } else if (dev_ingress_queue(dev)) {
  1043. q = dev_ingress_queue(dev)->qdisc_sleeping;
  1044. }
  1045. } else {
  1046. q = dev->qdisc;
  1047. }
  1048. if (!q)
  1049. return -ENOENT;
  1050. if (tcm->tcm_handle && q->handle != tcm->tcm_handle)
  1051. return -EINVAL;
  1052. } else {
  1053. q = qdisc_lookup(dev, tcm->tcm_handle);
  1054. if (!q)
  1055. return -ENOENT;
  1056. }
  1057. if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
  1058. return -EINVAL;
  1059. if (n->nlmsg_type == RTM_DELQDISC) {
  1060. if (!clid)
  1061. return -EINVAL;
  1062. if (q->handle == 0)
  1063. return -ENOENT;
  1064. err = qdisc_graft(dev, p, skb, n, clid, NULL, q);
  1065. if (err != 0)
  1066. return err;
  1067. } else {
  1068. qdisc_notify(net, skb, n, clid, NULL, q);
  1069. }
  1070. return 0;
  1071. }
  1072. /*
  1073. * Create/change qdisc.
  1074. */
  1075. static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
  1076. struct netlink_ext_ack *extack)
  1077. {
  1078. struct net *net = sock_net(skb->sk);
  1079. struct tcmsg *tcm;
  1080. struct nlattr *tca[TCA_MAX + 1];
  1081. struct net_device *dev;
  1082. u32 clid;
  1083. struct Qdisc *q, *p;
  1084. int err;
  1085. if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  1086. return -EPERM;
  1087. replay:
  1088. /* Reinit, just in case something touches this. */
  1089. err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL, extack);
  1090. if (err < 0)
  1091. return err;
  1092. tcm = nlmsg_data(n);
  1093. clid = tcm->tcm_parent;
  1094. q = p = NULL;
  1095. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  1096. if (!dev)
  1097. return -ENODEV;
  1098. if (clid) {
  1099. if (clid != TC_H_ROOT) {
  1100. if (clid != TC_H_INGRESS) {
  1101. p = qdisc_lookup(dev, TC_H_MAJ(clid));
  1102. if (!p)
  1103. return -ENOENT;
  1104. q = qdisc_leaf(p, clid);
  1105. } else if (dev_ingress_queue_create(dev)) {
  1106. q = dev_ingress_queue(dev)->qdisc_sleeping;
  1107. }
  1108. } else {
  1109. q = dev->qdisc;
  1110. }
  1111. /* It may be default qdisc, ignore it */
  1112. if (q && q->handle == 0)
  1113. q = NULL;
  1114. if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
  1115. if (tcm->tcm_handle) {
  1116. if (q && !(n->nlmsg_flags & NLM_F_REPLACE))
  1117. return -EEXIST;
  1118. if (TC_H_MIN(tcm->tcm_handle))
  1119. return -EINVAL;
  1120. q = qdisc_lookup(dev, tcm->tcm_handle);
  1121. if (!q)
  1122. goto create_n_graft;
  1123. if (n->nlmsg_flags & NLM_F_EXCL)
  1124. return -EEXIST;
  1125. if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
  1126. return -EINVAL;
  1127. if (q == p ||
  1128. (p && check_loop(q, p, 0)))
  1129. return -ELOOP;
  1130. qdisc_refcount_inc(q);
  1131. goto graft;
  1132. } else {
  1133. if (!q)
  1134. goto create_n_graft;
  1135. /* This magic test requires explanation.
  1136. *
  1137. * We know, that some child q is already
  1138. * attached to this parent and have choice:
  1139. * either to change it or to create/graft new one.
  1140. *
  1141. * 1. We are allowed to create/graft only
  1142. * if CREATE and REPLACE flags are set.
  1143. *
  1144. * 2. If EXCL is set, requestor wanted to say,
  1145. * that qdisc tcm_handle is not expected
  1146. * to exist, so that we choose create/graft too.
  1147. *
  1148. * 3. The last case is when no flags are set.
  1149. * Alas, it is sort of hole in API, we
  1150. * cannot decide what to do unambiguously.
  1151. * For now we select create/graft, if
  1152. * user gave KIND, which does not match existing.
  1153. */
  1154. if ((n->nlmsg_flags & NLM_F_CREATE) &&
  1155. (n->nlmsg_flags & NLM_F_REPLACE) &&
  1156. ((n->nlmsg_flags & NLM_F_EXCL) ||
  1157. (tca[TCA_KIND] &&
  1158. nla_strcmp(tca[TCA_KIND], q->ops->id))))
  1159. goto create_n_graft;
  1160. }
  1161. }
  1162. } else {
  1163. if (!tcm->tcm_handle)
  1164. return -EINVAL;
  1165. q = qdisc_lookup(dev, tcm->tcm_handle);
  1166. }
  1167. /* Change qdisc parameters */
  1168. if (q == NULL)
  1169. return -ENOENT;
  1170. if (n->nlmsg_flags & NLM_F_EXCL)
  1171. return -EEXIST;
  1172. if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id))
  1173. return -EINVAL;
  1174. err = qdisc_change(q, tca);
  1175. if (err == 0)
  1176. qdisc_notify(net, skb, n, clid, NULL, q);
  1177. return err;
  1178. create_n_graft:
  1179. if (!(n->nlmsg_flags & NLM_F_CREATE))
  1180. return -ENOENT;
  1181. if (clid == TC_H_INGRESS) {
  1182. if (dev_ingress_queue(dev))
  1183. q = qdisc_create(dev, dev_ingress_queue(dev), p,
  1184. tcm->tcm_parent, tcm->tcm_parent,
  1185. tca, &err);
  1186. else
  1187. err = -ENOENT;
  1188. } else {
  1189. struct netdev_queue *dev_queue;
  1190. if (p && p->ops->cl_ops && p->ops->cl_ops->select_queue)
  1191. dev_queue = p->ops->cl_ops->select_queue(p, tcm);
  1192. else if (p)
  1193. dev_queue = p->dev_queue;
  1194. else
  1195. dev_queue = netdev_get_tx_queue(dev, 0);
  1196. q = qdisc_create(dev, dev_queue, p,
  1197. tcm->tcm_parent, tcm->tcm_handle,
  1198. tca, &err);
  1199. }
  1200. if (q == NULL) {
  1201. if (err == -EAGAIN)
  1202. goto replay;
  1203. return err;
  1204. }
  1205. graft:
  1206. err = qdisc_graft(dev, p, skb, n, clid, q, NULL);
  1207. if (err) {
  1208. if (q)
  1209. qdisc_destroy(q);
  1210. return err;
  1211. }
  1212. return 0;
  1213. }
  1214. static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb,
  1215. struct netlink_callback *cb,
  1216. int *q_idx_p, int s_q_idx, bool recur,
  1217. bool dump_invisible)
  1218. {
  1219. int ret = 0, q_idx = *q_idx_p;
  1220. struct Qdisc *q;
  1221. int b;
  1222. if (!root)
  1223. return 0;
  1224. q = root;
  1225. if (q_idx < s_q_idx) {
  1226. q_idx++;
  1227. } else {
  1228. if (!tc_qdisc_dump_ignore(q, dump_invisible) &&
  1229. tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).portid,
  1230. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1231. RTM_NEWQDISC) <= 0)
  1232. goto done;
  1233. q_idx++;
  1234. }
  1235. /* If dumping singletons, there is no qdisc_dev(root) and the singleton
  1236. * itself has already been dumped.
  1237. *
  1238. * If we've already dumped the top-level (ingress) qdisc above and the global
  1239. * qdisc hashtable, we don't want to hit it again
  1240. */
  1241. if (!qdisc_dev(root) || !recur)
  1242. goto out;
  1243. hash_for_each(qdisc_dev(root)->qdisc_hash, b, q, hash) {
  1244. if (q_idx < s_q_idx) {
  1245. q_idx++;
  1246. continue;
  1247. }
  1248. if (!tc_qdisc_dump_ignore(q, dump_invisible) &&
  1249. tc_fill_qdisc(skb, q, q->parent, NETLINK_CB(cb->skb).portid,
  1250. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1251. RTM_NEWQDISC) <= 0)
  1252. goto done;
  1253. q_idx++;
  1254. }
  1255. out:
  1256. *q_idx_p = q_idx;
  1257. return ret;
  1258. done:
  1259. ret = -1;
  1260. goto out;
  1261. }
  1262. static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
  1263. {
  1264. struct net *net = sock_net(skb->sk);
  1265. int idx, q_idx;
  1266. int s_idx, s_q_idx;
  1267. struct net_device *dev;
  1268. const struct nlmsghdr *nlh = cb->nlh;
  1269. struct tcmsg *tcm = nlmsg_data(nlh);
  1270. struct nlattr *tca[TCA_MAX + 1];
  1271. int err;
  1272. s_idx = cb->args[0];
  1273. s_q_idx = q_idx = cb->args[1];
  1274. idx = 0;
  1275. ASSERT_RTNL();
  1276. err = nlmsg_parse(nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
  1277. if (err < 0)
  1278. return err;
  1279. for_each_netdev(net, dev) {
  1280. struct netdev_queue *dev_queue;
  1281. if (idx < s_idx)
  1282. goto cont;
  1283. if (idx > s_idx)
  1284. s_q_idx = 0;
  1285. q_idx = 0;
  1286. if (tc_dump_qdisc_root(dev->qdisc, skb, cb, &q_idx, s_q_idx,
  1287. true, tca[TCA_DUMP_INVISIBLE]) < 0)
  1288. goto done;
  1289. dev_queue = dev_ingress_queue(dev);
  1290. if (dev_queue &&
  1291. tc_dump_qdisc_root(dev_queue->qdisc_sleeping, skb, cb,
  1292. &q_idx, s_q_idx, false,
  1293. tca[TCA_DUMP_INVISIBLE]) < 0)
  1294. goto done;
  1295. cont:
  1296. idx++;
  1297. }
  1298. done:
  1299. cb->args[0] = idx;
  1300. cb->args[1] = q_idx;
  1301. return skb->len;
  1302. }
  1303. /************************************************
  1304. * Traffic classes manipulation. *
  1305. ************************************************/
  1306. static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
  1307. unsigned long cl,
  1308. u32 portid, u32 seq, u16 flags, int event)
  1309. {
  1310. struct tcmsg *tcm;
  1311. struct nlmsghdr *nlh;
  1312. unsigned char *b = skb_tail_pointer(skb);
  1313. struct gnet_dump d;
  1314. const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
  1315. cond_resched();
  1316. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  1317. if (!nlh)
  1318. goto out_nlmsg_trim;
  1319. tcm = nlmsg_data(nlh);
  1320. tcm->tcm_family = AF_UNSPEC;
  1321. tcm->tcm__pad1 = 0;
  1322. tcm->tcm__pad2 = 0;
  1323. tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
  1324. tcm->tcm_parent = q->handle;
  1325. tcm->tcm_handle = q->handle;
  1326. tcm->tcm_info = 0;
  1327. if (nla_put_string(skb, TCA_KIND, q->ops->id))
  1328. goto nla_put_failure;
  1329. if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
  1330. goto nla_put_failure;
  1331. if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS,
  1332. NULL, &d, TCA_PAD) < 0)
  1333. goto nla_put_failure;
  1334. if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
  1335. goto nla_put_failure;
  1336. if (gnet_stats_finish_copy(&d) < 0)
  1337. goto nla_put_failure;
  1338. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1339. return skb->len;
  1340. out_nlmsg_trim:
  1341. nla_put_failure:
  1342. nlmsg_trim(skb, b);
  1343. return -1;
  1344. }
  1345. static int tclass_notify(struct net *net, struct sk_buff *oskb,
  1346. struct nlmsghdr *n, struct Qdisc *q,
  1347. unsigned long cl, int event)
  1348. {
  1349. struct sk_buff *skb;
  1350. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  1351. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1352. if (!skb)
  1353. return -ENOBUFS;
  1354. if (tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0, event) < 0) {
  1355. kfree_skb(skb);
  1356. return -EINVAL;
  1357. }
  1358. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1359. n->nlmsg_flags & NLM_F_ECHO);
  1360. }
  1361. static int tclass_del_notify(struct net *net,
  1362. const struct Qdisc_class_ops *cops,
  1363. struct sk_buff *oskb, struct nlmsghdr *n,
  1364. struct Qdisc *q, unsigned long cl)
  1365. {
  1366. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  1367. struct sk_buff *skb;
  1368. int err = 0;
  1369. if (!cops->delete)
  1370. return -EOPNOTSUPP;
  1371. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1372. if (!skb)
  1373. return -ENOBUFS;
  1374. if (tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0,
  1375. RTM_DELTCLASS) < 0) {
  1376. kfree_skb(skb);
  1377. return -EINVAL;
  1378. }
  1379. err = cops->delete(q, cl);
  1380. if (err) {
  1381. kfree_skb(skb);
  1382. return err;
  1383. }
  1384. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1385. n->nlmsg_flags & NLM_F_ECHO);
  1386. }
  1387. #ifdef CONFIG_NET_CLS
  1388. struct tcf_bind_args {
  1389. struct tcf_walker w;
  1390. u32 classid;
  1391. unsigned long cl;
  1392. };
  1393. static int tcf_node_bind(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
  1394. {
  1395. struct tcf_bind_args *a = (void *)arg;
  1396. if (tp->ops->bind_class) {
  1397. tcf_tree_lock(tp);
  1398. tp->ops->bind_class(n, a->classid, a->cl);
  1399. tcf_tree_unlock(tp);
  1400. }
  1401. return 0;
  1402. }
  1403. static void tc_bind_tclass(struct Qdisc *q, u32 portid, u32 clid,
  1404. unsigned long new_cl)
  1405. {
  1406. const struct Qdisc_class_ops *cops = q->ops->cl_ops;
  1407. struct tcf_block *block;
  1408. struct tcf_chain *chain;
  1409. unsigned long cl;
  1410. cl = cops->find(q, portid);
  1411. if (!cl)
  1412. return;
  1413. block = cops->tcf_block(q, cl);
  1414. if (!block)
  1415. return;
  1416. list_for_each_entry(chain, &block->chain_list, list) {
  1417. struct tcf_proto *tp;
  1418. for (tp = rtnl_dereference(chain->filter_chain);
  1419. tp; tp = rtnl_dereference(tp->next)) {
  1420. struct tcf_bind_args arg = {};
  1421. arg.w.fn = tcf_node_bind;
  1422. arg.classid = clid;
  1423. arg.cl = new_cl;
  1424. tp->ops->walk(tp, &arg.w);
  1425. }
  1426. }
  1427. }
  1428. #else
  1429. static void tc_bind_tclass(struct Qdisc *q, u32 portid, u32 clid,
  1430. unsigned long new_cl)
  1431. {
  1432. }
  1433. #endif
  1434. static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n,
  1435. struct netlink_ext_ack *extack)
  1436. {
  1437. struct net *net = sock_net(skb->sk);
  1438. struct tcmsg *tcm = nlmsg_data(n);
  1439. struct nlattr *tca[TCA_MAX + 1];
  1440. struct net_device *dev;
  1441. struct Qdisc *q = NULL;
  1442. const struct Qdisc_class_ops *cops;
  1443. unsigned long cl = 0;
  1444. unsigned long new_cl;
  1445. u32 portid;
  1446. u32 clid;
  1447. u32 qid;
  1448. int err;
  1449. if ((n->nlmsg_type != RTM_GETTCLASS) &&
  1450. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  1451. return -EPERM;
  1452. err = nlmsg_parse(n, sizeof(*tcm), tca, TCA_MAX, NULL, extack);
  1453. if (err < 0)
  1454. return err;
  1455. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  1456. if (!dev)
  1457. return -ENODEV;
  1458. /*
  1459. parent == TC_H_UNSPEC - unspecified parent.
  1460. parent == TC_H_ROOT - class is root, which has no parent.
  1461. parent == X:0 - parent is root class.
  1462. parent == X:Y - parent is a node in hierarchy.
  1463. parent == 0:Y - parent is X:Y, where X:0 is qdisc.
  1464. handle == 0:0 - generate handle from kernel pool.
  1465. handle == 0:Y - class is X:Y, where X:0 is qdisc.
  1466. handle == X:Y - clear.
  1467. handle == X:0 - root class.
  1468. */
  1469. /* Step 1. Determine qdisc handle X:0 */
  1470. portid = tcm->tcm_parent;
  1471. clid = tcm->tcm_handle;
  1472. qid = TC_H_MAJ(clid);
  1473. if (portid != TC_H_ROOT) {
  1474. u32 qid1 = TC_H_MAJ(portid);
  1475. if (qid && qid1) {
  1476. /* If both majors are known, they must be identical. */
  1477. if (qid != qid1)
  1478. return -EINVAL;
  1479. } else if (qid1) {
  1480. qid = qid1;
  1481. } else if (qid == 0)
  1482. qid = dev->qdisc->handle;
  1483. /* Now qid is genuine qdisc handle consistent
  1484. * both with parent and child.
  1485. *
  1486. * TC_H_MAJ(portid) still may be unspecified, complete it now.
  1487. */
  1488. if (portid)
  1489. portid = TC_H_MAKE(qid, portid);
  1490. } else {
  1491. if (qid == 0)
  1492. qid = dev->qdisc->handle;
  1493. }
  1494. /* OK. Locate qdisc */
  1495. q = qdisc_lookup(dev, qid);
  1496. if (!q)
  1497. return -ENOENT;
  1498. /* An check that it supports classes */
  1499. cops = q->ops->cl_ops;
  1500. if (cops == NULL)
  1501. return -EINVAL;
  1502. /* Now try to get class */
  1503. if (clid == 0) {
  1504. if (portid == TC_H_ROOT)
  1505. clid = qid;
  1506. } else
  1507. clid = TC_H_MAKE(qid, clid);
  1508. if (clid)
  1509. cl = cops->find(q, clid);
  1510. if (cl == 0) {
  1511. err = -ENOENT;
  1512. if (n->nlmsg_type != RTM_NEWTCLASS ||
  1513. !(n->nlmsg_flags & NLM_F_CREATE))
  1514. goto out;
  1515. } else {
  1516. switch (n->nlmsg_type) {
  1517. case RTM_NEWTCLASS:
  1518. err = -EEXIST;
  1519. if (n->nlmsg_flags & NLM_F_EXCL)
  1520. goto out;
  1521. break;
  1522. case RTM_DELTCLASS:
  1523. err = tclass_del_notify(net, cops, skb, n, q, cl);
  1524. /* Unbind the class with flilters with 0 */
  1525. tc_bind_tclass(q, portid, clid, 0);
  1526. goto out;
  1527. case RTM_GETTCLASS:
  1528. err = tclass_notify(net, skb, n, q, cl, RTM_NEWTCLASS);
  1529. goto out;
  1530. default:
  1531. err = -EINVAL;
  1532. goto out;
  1533. }
  1534. }
  1535. new_cl = cl;
  1536. err = -EOPNOTSUPP;
  1537. if (cops->change)
  1538. err = cops->change(q, clid, portid, tca, &new_cl);
  1539. if (err == 0) {
  1540. tclass_notify(net, skb, n, q, new_cl, RTM_NEWTCLASS);
  1541. /* We just create a new class, need to do reverse binding. */
  1542. if (cl != new_cl)
  1543. tc_bind_tclass(q, portid, clid, new_cl);
  1544. }
  1545. out:
  1546. return err;
  1547. }
  1548. struct qdisc_dump_args {
  1549. struct qdisc_walker w;
  1550. struct sk_buff *skb;
  1551. struct netlink_callback *cb;
  1552. };
  1553. static int qdisc_class_dump(struct Qdisc *q, unsigned long cl,
  1554. struct qdisc_walker *arg)
  1555. {
  1556. struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
  1557. return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).portid,
  1558. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1559. RTM_NEWTCLASS);
  1560. }
  1561. static int tc_dump_tclass_qdisc(struct Qdisc *q, struct sk_buff *skb,
  1562. struct tcmsg *tcm, struct netlink_callback *cb,
  1563. int *t_p, int s_t)
  1564. {
  1565. struct qdisc_dump_args arg;
  1566. if (tc_qdisc_dump_ignore(q, false) ||
  1567. *t_p < s_t || !q->ops->cl_ops ||
  1568. (tcm->tcm_parent &&
  1569. TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
  1570. (*t_p)++;
  1571. return 0;
  1572. }
  1573. if (*t_p > s_t)
  1574. memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
  1575. arg.w.fn = qdisc_class_dump;
  1576. arg.skb = skb;
  1577. arg.cb = cb;
  1578. arg.w.stop = 0;
  1579. arg.w.skip = cb->args[1];
  1580. arg.w.count = 0;
  1581. q->ops->cl_ops->walk(q, &arg.w);
  1582. cb->args[1] = arg.w.count;
  1583. if (arg.w.stop)
  1584. return -1;
  1585. (*t_p)++;
  1586. return 0;
  1587. }
  1588. static int tc_dump_tclass_root(struct Qdisc *root, struct sk_buff *skb,
  1589. struct tcmsg *tcm, struct netlink_callback *cb,
  1590. int *t_p, int s_t)
  1591. {
  1592. struct Qdisc *q;
  1593. int b;
  1594. if (!root)
  1595. return 0;
  1596. if (tc_dump_tclass_qdisc(root, skb, tcm, cb, t_p, s_t) < 0)
  1597. return -1;
  1598. if (!qdisc_dev(root))
  1599. return 0;
  1600. if (tcm->tcm_parent) {
  1601. q = qdisc_match_from_root(root, TC_H_MAJ(tcm->tcm_parent));
  1602. if (q && tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0)
  1603. return -1;
  1604. return 0;
  1605. }
  1606. hash_for_each(qdisc_dev(root)->qdisc_hash, b, q, hash) {
  1607. if (tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0)
  1608. return -1;
  1609. }
  1610. return 0;
  1611. }
  1612. static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
  1613. {
  1614. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  1615. struct net *net = sock_net(skb->sk);
  1616. struct netdev_queue *dev_queue;
  1617. struct net_device *dev;
  1618. int t, s_t;
  1619. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  1620. return 0;
  1621. dev = dev_get_by_index(net, tcm->tcm_ifindex);
  1622. if (!dev)
  1623. return 0;
  1624. s_t = cb->args[0];
  1625. t = 0;
  1626. if (tc_dump_tclass_root(dev->qdisc, skb, tcm, cb, &t, s_t) < 0)
  1627. goto done;
  1628. dev_queue = dev_ingress_queue(dev);
  1629. if (dev_queue &&
  1630. tc_dump_tclass_root(dev_queue->qdisc_sleeping, skb, tcm, cb,
  1631. &t, s_t) < 0)
  1632. goto done;
  1633. done:
  1634. cb->args[0] = t;
  1635. dev_put(dev);
  1636. return skb->len;
  1637. }
  1638. #ifdef CONFIG_PROC_FS
  1639. static int psched_show(struct seq_file *seq, void *v)
  1640. {
  1641. seq_printf(seq, "%08x %08x %08x %08x\n",
  1642. (u32)NSEC_PER_USEC, (u32)PSCHED_TICKS2NS(1),
  1643. 1000000,
  1644. (u32)NSEC_PER_SEC / hrtimer_resolution);
  1645. return 0;
  1646. }
  1647. static int psched_open(struct inode *inode, struct file *file)
  1648. {
  1649. return single_open(file, psched_show, NULL);
  1650. }
  1651. static const struct file_operations psched_fops = {
  1652. .owner = THIS_MODULE,
  1653. .open = psched_open,
  1654. .read = seq_read,
  1655. .llseek = seq_lseek,
  1656. .release = single_release,
  1657. };
  1658. static int __net_init psched_net_init(struct net *net)
  1659. {
  1660. struct proc_dir_entry *e;
  1661. e = proc_create("psched", 0, net->proc_net, &psched_fops);
  1662. if (e == NULL)
  1663. return -ENOMEM;
  1664. return 0;
  1665. }
  1666. static void __net_exit psched_net_exit(struct net *net)
  1667. {
  1668. remove_proc_entry("psched", net->proc_net);
  1669. }
  1670. #else
  1671. static int __net_init psched_net_init(struct net *net)
  1672. {
  1673. return 0;
  1674. }
  1675. static void __net_exit psched_net_exit(struct net *net)
  1676. {
  1677. }
  1678. #endif
  1679. static struct pernet_operations psched_net_ops = {
  1680. .init = psched_net_init,
  1681. .exit = psched_net_exit,
  1682. };
  1683. static int __init pktsched_init(void)
  1684. {
  1685. int err;
  1686. err = register_pernet_subsys(&psched_net_ops);
  1687. if (err) {
  1688. pr_err("pktsched_init: "
  1689. "cannot initialize per netns operations\n");
  1690. return err;
  1691. }
  1692. register_qdisc(&pfifo_fast_ops);
  1693. register_qdisc(&pfifo_qdisc_ops);
  1694. register_qdisc(&bfifo_qdisc_ops);
  1695. register_qdisc(&pfifo_head_drop_qdisc_ops);
  1696. register_qdisc(&mq_qdisc_ops);
  1697. register_qdisc(&noqueue_qdisc_ops);
  1698. rtnl_register(PF_UNSPEC, RTM_NEWQDISC, tc_modify_qdisc, NULL, 0);
  1699. rtnl_register(PF_UNSPEC, RTM_DELQDISC, tc_get_qdisc, NULL, 0);
  1700. rtnl_register(PF_UNSPEC, RTM_GETQDISC, tc_get_qdisc, tc_dump_qdisc,
  1701. 0);
  1702. rtnl_register(PF_UNSPEC, RTM_NEWTCLASS, tc_ctl_tclass, NULL, 0);
  1703. rtnl_register(PF_UNSPEC, RTM_DELTCLASS, tc_ctl_tclass, NULL, 0);
  1704. rtnl_register(PF_UNSPEC, RTM_GETTCLASS, tc_ctl_tclass, tc_dump_tclass,
  1705. 0);
  1706. return 0;
  1707. }
  1708. subsys_initcall(pktsched_init);