fib_trie.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version
  5. * 2 of the License, or (at your option) any later version.
  6. *
  7. * Robert Olsson <robert.olsson@its.uu.se> Uppsala Universitet
  8. * & Swedish University of Agricultural Sciences.
  9. *
  10. * Jens Laas <jens.laas@data.slu.se> Swedish University of
  11. * Agricultural Sciences.
  12. *
  13. * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet
  14. *
  15. * This work is based on the LPC-trie which is originally described in:
  16. *
  17. * An experimental study of compression methods for dynamic tries
  18. * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.
  19. * http://www.csc.kth.se/~snilsson/software/dyntrie2/
  20. *
  21. *
  22. * IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson
  23. * IEEE Journal on Selected Areas in Communications, 17(6):1083-1092, June 1999
  24. *
  25. *
  26. * Code from fib_hash has been reused which includes the following header:
  27. *
  28. *
  29. * INET An implementation of the TCP/IP protocol suite for the LINUX
  30. * operating system. INET is implemented using the BSD Socket
  31. * interface as the means of communication with the user level.
  32. *
  33. * IPv4 FIB: lookup engine and maintenance routines.
  34. *
  35. *
  36. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  37. *
  38. * This program is free software; you can redistribute it and/or
  39. * modify it under the terms of the GNU General Public License
  40. * as published by the Free Software Foundation; either version
  41. * 2 of the License, or (at your option) any later version.
  42. *
  43. * Substantial contributions to this work comes from:
  44. *
  45. * David S. Miller, <davem@davemloft.net>
  46. * Stephen Hemminger <shemminger@osdl.org>
  47. * Paul E. McKenney <paulmck@us.ibm.com>
  48. * Patrick McHardy <kaber@trash.net>
  49. */
  50. #define VERSION "0.409"
  51. #include <asm/uaccess.h>
  52. #include <linux/bitops.h>
  53. #include <linux/types.h>
  54. #include <linux/kernel.h>
  55. #include <linux/mm.h>
  56. #include <linux/string.h>
  57. #include <linux/socket.h>
  58. #include <linux/sockios.h>
  59. #include <linux/errno.h>
  60. #include <linux/in.h>
  61. #include <linux/inet.h>
  62. #include <linux/inetdevice.h>
  63. #include <linux/netdevice.h>
  64. #include <linux/if_arp.h>
  65. #include <linux/proc_fs.h>
  66. #include <linux/rcupdate.h>
  67. #include <linux/skbuff.h>
  68. #include <linux/netlink.h>
  69. #include <linux/init.h>
  70. #include <linux/list.h>
  71. #include <linux/slab.h>
  72. #include <linux/export.h>
  73. #include <net/net_namespace.h>
  74. #include <net/ip.h>
  75. #include <net/protocol.h>
  76. #include <net/route.h>
  77. #include <net/tcp.h>
  78. #include <net/sock.h>
  79. #include <net/ip_fib.h>
  80. #include "fib_lookup.h"
  81. #define MAX_STAT_DEPTH 32
  82. #define KEYLENGTH (8*sizeof(t_key))
  83. #define KEY_MAX ((t_key)~0)
  84. typedef unsigned int t_key;
  85. #define IS_TNODE(n) ((n)->bits)
  86. #define IS_LEAF(n) (!(n)->bits)
  87. #define get_index(_key, _kv) (((_key) ^ (_kv)->key) >> (_kv)->pos)
  88. struct tnode {
  89. t_key key;
  90. unsigned char bits; /* 2log(KEYLENGTH) bits needed */
  91. unsigned char pos; /* 2log(KEYLENGTH) bits needed */
  92. unsigned char slen;
  93. struct tnode __rcu *parent;
  94. struct rcu_head rcu;
  95. union {
  96. /* The fields in this struct are valid if bits > 0 (TNODE) */
  97. struct {
  98. t_key empty_children; /* KEYLENGTH bits needed */
  99. t_key full_children; /* KEYLENGTH bits needed */
  100. struct tnode __rcu *child[0];
  101. };
  102. /* This list pointer if valid if bits == 0 (LEAF) */
  103. struct hlist_head leaf;
  104. };
  105. };
  106. #ifdef CONFIG_IP_FIB_TRIE_STATS
  107. struct trie_use_stats {
  108. unsigned int gets;
  109. unsigned int backtrack;
  110. unsigned int semantic_match_passed;
  111. unsigned int semantic_match_miss;
  112. unsigned int null_node_hit;
  113. unsigned int resize_node_skipped;
  114. };
  115. #endif
  116. struct trie_stat {
  117. unsigned int totdepth;
  118. unsigned int maxdepth;
  119. unsigned int tnodes;
  120. unsigned int leaves;
  121. unsigned int nullpointers;
  122. unsigned int prefixes;
  123. unsigned int nodesizes[MAX_STAT_DEPTH];
  124. };
  125. struct trie {
  126. struct tnode __rcu *trie;
  127. #ifdef CONFIG_IP_FIB_TRIE_STATS
  128. struct trie_use_stats __percpu *stats;
  129. #endif
  130. };
  131. static void resize(struct trie *t, struct tnode *tn);
  132. static size_t tnode_free_size;
  133. /*
  134. * synchronize_rcu after call_rcu for that many pages; it should be especially
  135. * useful before resizing the root node with PREEMPT_NONE configs; the value was
  136. * obtained experimentally, aiming to avoid visible slowdown.
  137. */
  138. static const int sync_pages = 128;
  139. static struct kmem_cache *fn_alias_kmem __read_mostly;
  140. static struct kmem_cache *trie_leaf_kmem __read_mostly;
  141. /* caller must hold RTNL */
  142. #define node_parent(n) rtnl_dereference((n)->parent)
  143. /* caller must hold RCU read lock or RTNL */
  144. #define node_parent_rcu(n) rcu_dereference_rtnl((n)->parent)
  145. /* wrapper for rcu_assign_pointer */
  146. static inline void node_set_parent(struct tnode *n, struct tnode *tp)
  147. {
  148. if (n)
  149. rcu_assign_pointer(n->parent, tp);
  150. }
  151. #define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER((n)->parent, p)
  152. /* This provides us with the number of children in this node, in the case of a
  153. * leaf this will return 0 meaning none of the children are accessible.
  154. */
  155. static inline unsigned long tnode_child_length(const struct tnode *tn)
  156. {
  157. return (1ul << tn->bits) & ~(1ul);
  158. }
  159. /* caller must hold RTNL */
  160. static inline struct tnode *tnode_get_child(const struct tnode *tn,
  161. unsigned long i)
  162. {
  163. return rtnl_dereference(tn->child[i]);
  164. }
  165. /* caller must hold RCU read lock or RTNL */
  166. static inline struct tnode *tnode_get_child_rcu(const struct tnode *tn,
  167. unsigned long i)
  168. {
  169. return rcu_dereference_rtnl(tn->child[i]);
  170. }
  171. /* To understand this stuff, an understanding of keys and all their bits is
  172. * necessary. Every node in the trie has a key associated with it, but not
  173. * all of the bits in that key are significant.
  174. *
  175. * Consider a node 'n' and its parent 'tp'.
  176. *
  177. * If n is a leaf, every bit in its key is significant. Its presence is
  178. * necessitated by path compression, since during a tree traversal (when
  179. * searching for a leaf - unless we are doing an insertion) we will completely
  180. * ignore all skipped bits we encounter. Thus we need to verify, at the end of
  181. * a potentially successful search, that we have indeed been walking the
  182. * correct key path.
  183. *
  184. * Note that we can never "miss" the correct key in the tree if present by
  185. * following the wrong path. Path compression ensures that segments of the key
  186. * that are the same for all keys with a given prefix are skipped, but the
  187. * skipped part *is* identical for each node in the subtrie below the skipped
  188. * bit! trie_insert() in this implementation takes care of that.
  189. *
  190. * if n is an internal node - a 'tnode' here, the various parts of its key
  191. * have many different meanings.
  192. *
  193. * Example:
  194. * _________________________________________________________________
  195. * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
  196. * -----------------------------------------------------------------
  197. * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
  198. *
  199. * _________________________________________________________________
  200. * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
  201. * -----------------------------------------------------------------
  202. * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  203. *
  204. * tp->pos = 22
  205. * tp->bits = 3
  206. * n->pos = 13
  207. * n->bits = 4
  208. *
  209. * First, let's just ignore the bits that come before the parent tp, that is
  210. * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
  211. * point we do not use them for anything.
  212. *
  213. * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
  214. * index into the parent's child array. That is, they will be used to find
  215. * 'n' among tp's children.
  216. *
  217. * The bits from (n->pos + n->bits) to (tn->pos - 1) - "S" - are skipped bits
  218. * for the node n.
  219. *
  220. * All the bits we have seen so far are significant to the node n. The rest
  221. * of the bits are really not needed or indeed known in n->key.
  222. *
  223. * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
  224. * n's child array, and will of course be different for each child.
  225. *
  226. * The rest of the bits, from 0 to (n->pos + n->bits), are completely unknown
  227. * at this point.
  228. */
  229. static const int halve_threshold = 25;
  230. static const int inflate_threshold = 50;
  231. static const int halve_threshold_root = 15;
  232. static const int inflate_threshold_root = 30;
  233. static void __alias_free_mem(struct rcu_head *head)
  234. {
  235. struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
  236. kmem_cache_free(fn_alias_kmem, fa);
  237. }
  238. static inline void alias_free_mem_rcu(struct fib_alias *fa)
  239. {
  240. call_rcu(&fa->rcu, __alias_free_mem);
  241. }
  242. #define TNODE_KMALLOC_MAX \
  243. ilog2((PAGE_SIZE - sizeof(struct tnode)) / sizeof(struct tnode *))
  244. static void __node_free_rcu(struct rcu_head *head)
  245. {
  246. struct tnode *n = container_of(head, struct tnode, rcu);
  247. if (IS_LEAF(n))
  248. kmem_cache_free(trie_leaf_kmem, n);
  249. else if (n->bits <= TNODE_KMALLOC_MAX)
  250. kfree(n);
  251. else
  252. vfree(n);
  253. }
  254. #define node_free(n) call_rcu(&n->rcu, __node_free_rcu)
  255. static struct tnode *tnode_alloc(size_t size)
  256. {
  257. if (size <= PAGE_SIZE)
  258. return kzalloc(size, GFP_KERNEL);
  259. else
  260. return vzalloc(size);
  261. }
  262. static inline void empty_child_inc(struct tnode *n)
  263. {
  264. ++n->empty_children ? : ++n->full_children;
  265. }
  266. static inline void empty_child_dec(struct tnode *n)
  267. {
  268. n->empty_children-- ? : n->full_children--;
  269. }
  270. static struct tnode *leaf_new(t_key key)
  271. {
  272. struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
  273. if (l) {
  274. l->parent = NULL;
  275. /* set key and pos to reflect full key value
  276. * any trailing zeros in the key should be ignored
  277. * as the nodes are searched
  278. */
  279. l->key = key;
  280. l->slen = 0;
  281. l->pos = 0;
  282. /* set bits to 0 indicating we are not a tnode */
  283. l->bits = 0;
  284. INIT_HLIST_HEAD(&l->leaf);
  285. }
  286. return l;
  287. }
  288. static struct tnode *tnode_new(t_key key, int pos, int bits)
  289. {
  290. size_t sz = offsetof(struct tnode, child[1ul << bits]);
  291. struct tnode *tn = tnode_alloc(sz);
  292. unsigned int shift = pos + bits;
  293. /* verify bits and pos their msb bits clear and values are valid */
  294. BUG_ON(!bits || (shift > KEYLENGTH));
  295. if (tn) {
  296. tn->parent = NULL;
  297. tn->slen = pos;
  298. tn->pos = pos;
  299. tn->bits = bits;
  300. tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
  301. if (bits == KEYLENGTH)
  302. tn->full_children = 1;
  303. else
  304. tn->empty_children = 1ul << bits;
  305. }
  306. pr_debug("AT %p s=%zu %zu\n", tn, sizeof(struct tnode),
  307. sizeof(struct tnode *) << bits);
  308. return tn;
  309. }
  310. /* Check whether a tnode 'n' is "full", i.e. it is an internal node
  311. * and no bits are skipped. See discussion in dyntree paper p. 6
  312. */
  313. static inline int tnode_full(const struct tnode *tn, const struct tnode *n)
  314. {
  315. return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
  316. }
  317. /* Add a child at position i overwriting the old value.
  318. * Update the value of full_children and empty_children.
  319. */
  320. static void put_child(struct tnode *tn, unsigned long i, struct tnode *n)
  321. {
  322. struct tnode *chi = tnode_get_child(tn, i);
  323. int isfull, wasfull;
  324. BUG_ON(i >= tnode_child_length(tn));
  325. /* update emptyChildren, overflow into fullChildren */
  326. if (n == NULL && chi != NULL)
  327. empty_child_inc(tn);
  328. if (n != NULL && chi == NULL)
  329. empty_child_dec(tn);
  330. /* update fullChildren */
  331. wasfull = tnode_full(tn, chi);
  332. isfull = tnode_full(tn, n);
  333. if (wasfull && !isfull)
  334. tn->full_children--;
  335. else if (!wasfull && isfull)
  336. tn->full_children++;
  337. if (n && (tn->slen < n->slen))
  338. tn->slen = n->slen;
  339. rcu_assign_pointer(tn->child[i], n);
  340. }
  341. static void update_children(struct tnode *tn)
  342. {
  343. unsigned long i;
  344. /* update all of the child parent pointers */
  345. for (i = tnode_child_length(tn); i;) {
  346. struct tnode *inode = tnode_get_child(tn, --i);
  347. if (!inode)
  348. continue;
  349. /* Either update the children of a tnode that
  350. * already belongs to us or update the child
  351. * to point to ourselves.
  352. */
  353. if (node_parent(inode) == tn)
  354. update_children(inode);
  355. else
  356. node_set_parent(inode, tn);
  357. }
  358. }
  359. static inline void put_child_root(struct tnode *tp, struct trie *t,
  360. t_key key, struct tnode *n)
  361. {
  362. if (tp)
  363. put_child(tp, get_index(key, tp), n);
  364. else
  365. rcu_assign_pointer(t->trie, n);
  366. }
  367. static inline void tnode_free_init(struct tnode *tn)
  368. {
  369. tn->rcu.next = NULL;
  370. }
  371. static inline void tnode_free_append(struct tnode *tn, struct tnode *n)
  372. {
  373. n->rcu.next = tn->rcu.next;
  374. tn->rcu.next = &n->rcu;
  375. }
  376. static void tnode_free(struct tnode *tn)
  377. {
  378. struct callback_head *head = &tn->rcu;
  379. while (head) {
  380. head = head->next;
  381. tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]);
  382. node_free(tn);
  383. tn = container_of(head, struct tnode, rcu);
  384. }
  385. if (tnode_free_size >= PAGE_SIZE * sync_pages) {
  386. tnode_free_size = 0;
  387. synchronize_rcu();
  388. }
  389. }
  390. static void replace(struct trie *t, struct tnode *oldtnode, struct tnode *tn)
  391. {
  392. struct tnode *tp = node_parent(oldtnode);
  393. unsigned long i;
  394. /* setup the parent pointer out of and back into this node */
  395. NODE_INIT_PARENT(tn, tp);
  396. put_child_root(tp, t, tn->key, tn);
  397. /* update all of the child parent pointers */
  398. update_children(tn);
  399. /* all pointers should be clean so we are done */
  400. tnode_free(oldtnode);
  401. /* resize children now that oldtnode is freed */
  402. for (i = tnode_child_length(tn); i;) {
  403. struct tnode *inode = tnode_get_child(tn, --i);
  404. /* resize child node */
  405. if (tnode_full(tn, inode))
  406. resize(t, inode);
  407. }
  408. }
  409. static int inflate(struct trie *t, struct tnode *oldtnode)
  410. {
  411. struct tnode *tn;
  412. unsigned long i;
  413. t_key m;
  414. pr_debug("In inflate\n");
  415. tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
  416. if (!tn)
  417. return -ENOMEM;
  418. /* prepare oldtnode to be freed */
  419. tnode_free_init(oldtnode);
  420. /* Assemble all of the pointers in our cluster, in this case that
  421. * represents all of the pointers out of our allocated nodes that
  422. * point to existing tnodes and the links between our allocated
  423. * nodes.
  424. */
  425. for (i = tnode_child_length(oldtnode), m = 1u << tn->pos; i;) {
  426. struct tnode *inode = tnode_get_child(oldtnode, --i);
  427. struct tnode *node0, *node1;
  428. unsigned long j, k;
  429. /* An empty child */
  430. if (inode == NULL)
  431. continue;
  432. /* A leaf or an internal node with skipped bits */
  433. if (!tnode_full(oldtnode, inode)) {
  434. put_child(tn, get_index(inode->key, tn), inode);
  435. continue;
  436. }
  437. /* drop the node in the old tnode free list */
  438. tnode_free_append(oldtnode, inode);
  439. /* An internal node with two children */
  440. if (inode->bits == 1) {
  441. put_child(tn, 2 * i + 1, tnode_get_child(inode, 1));
  442. put_child(tn, 2 * i, tnode_get_child(inode, 0));
  443. continue;
  444. }
  445. /* We will replace this node 'inode' with two new
  446. * ones, 'node0' and 'node1', each with half of the
  447. * original children. The two new nodes will have
  448. * a position one bit further down the key and this
  449. * means that the "significant" part of their keys
  450. * (see the discussion near the top of this file)
  451. * will differ by one bit, which will be "0" in
  452. * node0's key and "1" in node1's key. Since we are
  453. * moving the key position by one step, the bit that
  454. * we are moving away from - the bit at position
  455. * (tn->pos) - is the one that will differ between
  456. * node0 and node1. So... we synthesize that bit in the
  457. * two new keys.
  458. */
  459. node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
  460. if (!node1)
  461. goto nomem;
  462. node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
  463. tnode_free_append(tn, node1);
  464. if (!node0)
  465. goto nomem;
  466. tnode_free_append(tn, node0);
  467. /* populate child pointers in new nodes */
  468. for (k = tnode_child_length(inode), j = k / 2; j;) {
  469. put_child(node1, --j, tnode_get_child(inode, --k));
  470. put_child(node0, j, tnode_get_child(inode, j));
  471. put_child(node1, --j, tnode_get_child(inode, --k));
  472. put_child(node0, j, tnode_get_child(inode, j));
  473. }
  474. /* link new nodes to parent */
  475. NODE_INIT_PARENT(node1, tn);
  476. NODE_INIT_PARENT(node0, tn);
  477. /* link parent to nodes */
  478. put_child(tn, 2 * i + 1, node1);
  479. put_child(tn, 2 * i, node0);
  480. }
  481. /* setup the parent pointers into and out of this node */
  482. replace(t, oldtnode, tn);
  483. return 0;
  484. nomem:
  485. /* all pointers should be clean so we are done */
  486. tnode_free(tn);
  487. return -ENOMEM;
  488. }
  489. static int halve(struct trie *t, struct tnode *oldtnode)
  490. {
  491. struct tnode *tn;
  492. unsigned long i;
  493. pr_debug("In halve\n");
  494. tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
  495. if (!tn)
  496. return -ENOMEM;
  497. /* prepare oldtnode to be freed */
  498. tnode_free_init(oldtnode);
  499. /* Assemble all of the pointers in our cluster, in this case that
  500. * represents all of the pointers out of our allocated nodes that
  501. * point to existing tnodes and the links between our allocated
  502. * nodes.
  503. */
  504. for (i = tnode_child_length(oldtnode); i;) {
  505. struct tnode *node1 = tnode_get_child(oldtnode, --i);
  506. struct tnode *node0 = tnode_get_child(oldtnode, --i);
  507. struct tnode *inode;
  508. /* At least one of the children is empty */
  509. if (!node1 || !node0) {
  510. put_child(tn, i / 2, node1 ? : node0);
  511. continue;
  512. }
  513. /* Two nonempty children */
  514. inode = tnode_new(node0->key, oldtnode->pos, 1);
  515. if (!inode) {
  516. tnode_free(tn);
  517. return -ENOMEM;
  518. }
  519. tnode_free_append(tn, inode);
  520. /* initialize pointers out of node */
  521. put_child(inode, 1, node1);
  522. put_child(inode, 0, node0);
  523. NODE_INIT_PARENT(inode, tn);
  524. /* link parent to node */
  525. put_child(tn, i / 2, inode);
  526. }
  527. /* setup the parent pointers into and out of this node */
  528. replace(t, oldtnode, tn);
  529. return 0;
  530. }
  531. static void collapse(struct trie *t, struct tnode *oldtnode)
  532. {
  533. struct tnode *n, *tp;
  534. unsigned long i;
  535. /* scan the tnode looking for that one child that might still exist */
  536. for (n = NULL, i = tnode_child_length(oldtnode); !n && i;)
  537. n = tnode_get_child(oldtnode, --i);
  538. /* compress one level */
  539. tp = node_parent(oldtnode);
  540. put_child_root(tp, t, oldtnode->key, n);
  541. node_set_parent(n, tp);
  542. /* drop dead node */
  543. node_free(oldtnode);
  544. }
  545. static unsigned char update_suffix(struct tnode *tn)
  546. {
  547. unsigned char slen = tn->pos;
  548. unsigned long stride, i;
  549. /* search though the list of children looking for nodes that might
  550. * have a suffix greater than the one we currently have. This is
  551. * why we start with a stride of 2 since a stride of 1 would
  552. * represent the nodes with suffix length equal to tn->pos
  553. */
  554. for (i = 0, stride = 0x2ul ; i < tnode_child_length(tn); i += stride) {
  555. struct tnode *n = tnode_get_child(tn, i);
  556. if (!n || (n->slen <= slen))
  557. continue;
  558. /* update stride and slen based on new value */
  559. stride <<= (n->slen - slen);
  560. slen = n->slen;
  561. i &= ~(stride - 1);
  562. /* if slen covers all but the last bit we can stop here
  563. * there will be nothing longer than that since only node
  564. * 0 and 1 << (bits - 1) could have that as their suffix
  565. * length.
  566. */
  567. if ((slen + 1) >= (tn->pos + tn->bits))
  568. break;
  569. }
  570. tn->slen = slen;
  571. return slen;
  572. }
  573. /* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
  574. * the Helsinki University of Technology and Matti Tikkanen of Nokia
  575. * Telecommunications, page 6:
  576. * "A node is doubled if the ratio of non-empty children to all
  577. * children in the *doubled* node is at least 'high'."
  578. *
  579. * 'high' in this instance is the variable 'inflate_threshold'. It
  580. * is expressed as a percentage, so we multiply it with
  581. * tnode_child_length() and instead of multiplying by 2 (since the
  582. * child array will be doubled by inflate()) and multiplying
  583. * the left-hand side by 100 (to handle the percentage thing) we
  584. * multiply the left-hand side by 50.
  585. *
  586. * The left-hand side may look a bit weird: tnode_child_length(tn)
  587. * - tn->empty_children is of course the number of non-null children
  588. * in the current node. tn->full_children is the number of "full"
  589. * children, that is non-null tnodes with a skip value of 0.
  590. * All of those will be doubled in the resulting inflated tnode, so
  591. * we just count them one extra time here.
  592. *
  593. * A clearer way to write this would be:
  594. *
  595. * to_be_doubled = tn->full_children;
  596. * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children -
  597. * tn->full_children;
  598. *
  599. * new_child_length = tnode_child_length(tn) * 2;
  600. *
  601. * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
  602. * new_child_length;
  603. * if (new_fill_factor >= inflate_threshold)
  604. *
  605. * ...and so on, tho it would mess up the while () loop.
  606. *
  607. * anyway,
  608. * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
  609. * inflate_threshold
  610. *
  611. * avoid a division:
  612. * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
  613. * inflate_threshold * new_child_length
  614. *
  615. * expand not_to_be_doubled and to_be_doubled, and shorten:
  616. * 100 * (tnode_child_length(tn) - tn->empty_children +
  617. * tn->full_children) >= inflate_threshold * new_child_length
  618. *
  619. * expand new_child_length:
  620. * 100 * (tnode_child_length(tn) - tn->empty_children +
  621. * tn->full_children) >=
  622. * inflate_threshold * tnode_child_length(tn) * 2
  623. *
  624. * shorten again:
  625. * 50 * (tn->full_children + tnode_child_length(tn) -
  626. * tn->empty_children) >= inflate_threshold *
  627. * tnode_child_length(tn)
  628. *
  629. */
  630. static bool should_inflate(const struct tnode *tp, const struct tnode *tn)
  631. {
  632. unsigned long used = tnode_child_length(tn);
  633. unsigned long threshold = used;
  634. /* Keep root node larger */
  635. threshold *= tp ? inflate_threshold : inflate_threshold_root;
  636. used -= tn->empty_children;
  637. used += tn->full_children;
  638. /* if bits == KEYLENGTH then pos = 0, and will fail below */
  639. return (used > 1) && tn->pos && ((50 * used) >= threshold);
  640. }
  641. static bool should_halve(const struct tnode *tp, const struct tnode *tn)
  642. {
  643. unsigned long used = tnode_child_length(tn);
  644. unsigned long threshold = used;
  645. /* Keep root node larger */
  646. threshold *= tp ? halve_threshold : halve_threshold_root;
  647. used -= tn->empty_children;
  648. /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
  649. return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
  650. }
  651. static bool should_collapse(const struct tnode *tn)
  652. {
  653. unsigned long used = tnode_child_length(tn);
  654. used -= tn->empty_children;
  655. /* account for bits == KEYLENGTH case */
  656. if ((tn->bits == KEYLENGTH) && tn->full_children)
  657. used -= KEY_MAX;
  658. /* One child or none, time to drop us from the trie */
  659. return used < 2;
  660. }
  661. #define MAX_WORK 10
  662. static void resize(struct trie *t, struct tnode *tn)
  663. {
  664. struct tnode *tp = node_parent(tn);
  665. struct tnode __rcu **cptr;
  666. int max_work = MAX_WORK;
  667. pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
  668. tn, inflate_threshold, halve_threshold);
  669. /* track the tnode via the pointer from the parent instead of
  670. * doing it ourselves. This way we can let RCU fully do its
  671. * thing without us interfering
  672. */
  673. cptr = tp ? &tp->child[get_index(tn->key, tp)] : &t->trie;
  674. BUG_ON(tn != rtnl_dereference(*cptr));
  675. /* Double as long as the resulting node has a number of
  676. * nonempty nodes that are above the threshold.
  677. */
  678. while (should_inflate(tp, tn) && max_work) {
  679. if (inflate(t, tn)) {
  680. #ifdef CONFIG_IP_FIB_TRIE_STATS
  681. this_cpu_inc(t->stats->resize_node_skipped);
  682. #endif
  683. break;
  684. }
  685. max_work--;
  686. tn = rtnl_dereference(*cptr);
  687. }
  688. /* Return if at least one inflate is run */
  689. if (max_work != MAX_WORK)
  690. return;
  691. /* Halve as long as the number of empty children in this
  692. * node is above threshold.
  693. */
  694. while (should_halve(tp, tn) && max_work) {
  695. if (halve(t, tn)) {
  696. #ifdef CONFIG_IP_FIB_TRIE_STATS
  697. this_cpu_inc(t->stats->resize_node_skipped);
  698. #endif
  699. break;
  700. }
  701. max_work--;
  702. tn = rtnl_dereference(*cptr);
  703. }
  704. /* Only one child remains */
  705. if (should_collapse(tn)) {
  706. collapse(t, tn);
  707. return;
  708. }
  709. /* Return if at least one deflate was run */
  710. if (max_work != MAX_WORK)
  711. return;
  712. /* push the suffix length to the parent node */
  713. if (tn->slen > tn->pos) {
  714. unsigned char slen = update_suffix(tn);
  715. if (tp && (slen > tp->slen))
  716. tp->slen = slen;
  717. }
  718. }
  719. static void leaf_pull_suffix(struct tnode *l)
  720. {
  721. struct tnode *tp = node_parent(l);
  722. while (tp && (tp->slen > tp->pos) && (tp->slen > l->slen)) {
  723. if (update_suffix(tp) > l->slen)
  724. break;
  725. tp = node_parent(tp);
  726. }
  727. }
  728. static void leaf_push_suffix(struct tnode *l)
  729. {
  730. struct tnode *tn = node_parent(l);
  731. /* if this is a new leaf then tn will be NULL and we can sort
  732. * out parent suffix lengths as a part of trie_rebalance
  733. */
  734. while (tn && (tn->slen < l->slen)) {
  735. tn->slen = l->slen;
  736. tn = node_parent(tn);
  737. }
  738. }
  739. static void fib_remove_alias(struct tnode *l, struct fib_alias *old)
  740. {
  741. /* record the location of the previous list_info entry */
  742. struct hlist_node **pprev = old->fa_list.pprev;
  743. struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
  744. /* remove the fib_alias from the list */
  745. hlist_del_rcu(&old->fa_list);
  746. /* only access fa if it is pointing at the last valid hlist_node */
  747. if (hlist_empty(&l->leaf) || (*pprev))
  748. return;
  749. /* update the trie with the latest suffix length */
  750. l->slen = fa->fa_slen;
  751. leaf_pull_suffix(l);
  752. }
  753. static void fib_insert_alias(struct tnode *l, struct fib_alias *fa,
  754. struct fib_alias *new)
  755. {
  756. if (fa) {
  757. hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
  758. } else {
  759. struct fib_alias *last;
  760. hlist_for_each_entry(last, &l->leaf, fa_list) {
  761. if (new->fa_slen < last->fa_slen)
  762. break;
  763. fa = last;
  764. }
  765. if (fa)
  766. hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
  767. else
  768. hlist_add_head_rcu(&new->fa_list, &l->leaf);
  769. }
  770. /* if we added to the tail node then we need to update slen */
  771. if (l->slen < new->fa_slen) {
  772. l->slen = new->fa_slen;
  773. leaf_push_suffix(l);
  774. }
  775. }
  776. /* rcu_read_lock needs to be hold by caller from readside */
  777. static struct tnode *fib_find_node(struct trie *t, u32 key)
  778. {
  779. struct tnode *n = rcu_dereference_rtnl(t->trie);
  780. while (n) {
  781. unsigned long index = get_index(key, n);
  782. /* This bit of code is a bit tricky but it combines multiple
  783. * checks into a single check. The prefix consists of the
  784. * prefix plus zeros for the bits in the cindex. The index
  785. * is the difference between the key and this value. From
  786. * this we can actually derive several pieces of data.
  787. * if (index & (~0ul << bits))
  788. * we have a mismatch in skip bits and failed
  789. * else
  790. * we know the value is cindex
  791. */
  792. if (index & (~0ul << n->bits))
  793. return NULL;
  794. /* we have found a leaf. Prefixes have already been compared */
  795. if (IS_LEAF(n))
  796. break;
  797. n = tnode_get_child_rcu(n, index);
  798. }
  799. return n;
  800. }
  801. /* Return the first fib alias matching TOS with
  802. * priority less than or equal to PRIO.
  803. */
  804. static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 slen,
  805. u8 tos, u32 prio)
  806. {
  807. struct fib_alias *fa;
  808. if (!fah)
  809. return NULL;
  810. hlist_for_each_entry(fa, fah, fa_list) {
  811. if (fa->fa_slen < slen)
  812. continue;
  813. if (fa->fa_slen != slen)
  814. break;
  815. if (fa->fa_tos > tos)
  816. continue;
  817. if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
  818. return fa;
  819. }
  820. return NULL;
  821. }
  822. static void trie_rebalance(struct trie *t, struct tnode *tn)
  823. {
  824. struct tnode *tp;
  825. while ((tp = node_parent(tn)) != NULL) {
  826. resize(t, tn);
  827. tn = tp;
  828. }
  829. /* Handle last (top) tnode */
  830. if (IS_TNODE(tn))
  831. resize(t, tn);
  832. }
  833. /* only used from updater-side */
  834. static struct tnode *fib_insert_node(struct trie *t, u32 key, int plen)
  835. {
  836. struct tnode *l, *n, *tp = NULL;
  837. n = rtnl_dereference(t->trie);
  838. /* If we point to NULL, stop. Either the tree is empty and we should
  839. * just put a new leaf in if, or we have reached an empty child slot,
  840. * and we should just put our new leaf in that.
  841. *
  842. * If we hit a node with a key that does't match then we should stop
  843. * and create a new tnode to replace that node and insert ourselves
  844. * and the other node into the new tnode.
  845. */
  846. while (n) {
  847. unsigned long index = get_index(key, n);
  848. /* This bit of code is a bit tricky but it combines multiple
  849. * checks into a single check. The prefix consists of the
  850. * prefix plus zeros for the "bits" in the prefix. The index
  851. * is the difference between the key and this value. From
  852. * this we can actually derive several pieces of data.
  853. * if !(index >> bits)
  854. * we know the value is child index
  855. * else
  856. * we have a mismatch in skip bits and failed
  857. */
  858. if (index >> n->bits)
  859. break;
  860. /* we have found a leaf. Prefixes have already been compared */
  861. if (IS_LEAF(n)) {
  862. /* Case 1: n is a leaf, and prefixes match*/
  863. return n;
  864. }
  865. tp = n;
  866. n = tnode_get_child_rcu(n, index);
  867. }
  868. l = leaf_new(key);
  869. if (!l)
  870. return NULL;
  871. /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
  872. *
  873. * Add a new tnode here
  874. * first tnode need some special handling
  875. * leaves us in position for handling as case 3
  876. */
  877. if (n) {
  878. struct tnode *tn;
  879. tn = tnode_new(key, __fls(key ^ n->key), 1);
  880. if (!tn) {
  881. node_free(l);
  882. return NULL;
  883. }
  884. /* initialize routes out of node */
  885. NODE_INIT_PARENT(tn, tp);
  886. put_child(tn, get_index(key, tn) ^ 1, n);
  887. /* start adding routes into the node */
  888. put_child_root(tp, t, key, tn);
  889. node_set_parent(n, tn);
  890. /* parent now has a NULL spot where the leaf can go */
  891. tp = tn;
  892. }
  893. /* Case 3: n is NULL, and will just insert a new leaf */
  894. if (tp) {
  895. NODE_INIT_PARENT(l, tp);
  896. put_child(tp, get_index(key, tp), l);
  897. trie_rebalance(t, tp);
  898. } else {
  899. rcu_assign_pointer(t->trie, l);
  900. }
  901. return l;
  902. }
  903. /*
  904. * Caller must hold RTNL.
  905. */
  906. int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
  907. {
  908. struct trie *t = (struct trie *) tb->tb_data;
  909. struct fib_alias *fa, *new_fa;
  910. struct fib_info *fi;
  911. u8 plen = cfg->fc_dst_len;
  912. u8 slen = KEYLENGTH - plen;
  913. u8 tos = cfg->fc_tos;
  914. u32 key, mask;
  915. int err;
  916. struct tnode *l;
  917. if (plen > KEYLENGTH)
  918. return -EINVAL;
  919. key = ntohl(cfg->fc_dst);
  920. pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
  921. mask = ntohl(inet_make_mask(plen));
  922. if (key & ~mask)
  923. return -EINVAL;
  924. fi = fib_create_info(cfg);
  925. if (IS_ERR(fi)) {
  926. err = PTR_ERR(fi);
  927. goto err;
  928. }
  929. l = fib_find_node(t, key);
  930. fa = l ? fib_find_alias(&l->leaf, slen, tos, fi->fib_priority) : NULL;
  931. /* Now fa, if non-NULL, points to the first fib alias
  932. * with the same keys [prefix,tos,priority], if such key already
  933. * exists or to the node before which we will insert new one.
  934. *
  935. * If fa is NULL, we will need to allocate a new one and
  936. * insert to the tail of the section matching the suffix length
  937. * of the new alias.
  938. */
  939. if (fa && fa->fa_tos == tos &&
  940. fa->fa_info->fib_priority == fi->fib_priority) {
  941. struct fib_alias *fa_first, *fa_match;
  942. err = -EEXIST;
  943. if (cfg->fc_nlflags & NLM_F_EXCL)
  944. goto out;
  945. /* We have 2 goals:
  946. * 1. Find exact match for type, scope, fib_info to avoid
  947. * duplicate routes
  948. * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
  949. */
  950. fa_match = NULL;
  951. fa_first = fa;
  952. hlist_for_each_entry_from(fa, fa_list) {
  953. if ((fa->fa_slen != slen) || (fa->fa_tos != tos))
  954. break;
  955. if (fa->fa_info->fib_priority != fi->fib_priority)
  956. break;
  957. if (fa->fa_type == cfg->fc_type &&
  958. fa->fa_info == fi) {
  959. fa_match = fa;
  960. break;
  961. }
  962. }
  963. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  964. struct fib_info *fi_drop;
  965. u8 state;
  966. fa = fa_first;
  967. if (fa_match) {
  968. if (fa == fa_match)
  969. err = 0;
  970. goto out;
  971. }
  972. err = -ENOBUFS;
  973. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  974. if (new_fa == NULL)
  975. goto out;
  976. fi_drop = fa->fa_info;
  977. new_fa->fa_tos = fa->fa_tos;
  978. new_fa->fa_info = fi;
  979. new_fa->fa_type = cfg->fc_type;
  980. state = fa->fa_state;
  981. new_fa->fa_state = state & ~FA_S_ACCESSED;
  982. new_fa->fa_slen = fa->fa_slen;
  983. hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
  984. alias_free_mem_rcu(fa);
  985. fib_release_info(fi_drop);
  986. if (state & FA_S_ACCESSED)
  987. rt_cache_flush(cfg->fc_nlinfo.nl_net);
  988. rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
  989. tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
  990. goto succeeded;
  991. }
  992. /* Error if we find a perfect match which
  993. * uses the same scope, type, and nexthop
  994. * information.
  995. */
  996. if (fa_match)
  997. goto out;
  998. if (!(cfg->fc_nlflags & NLM_F_APPEND))
  999. fa = fa_first;
  1000. }
  1001. err = -ENOENT;
  1002. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  1003. goto out;
  1004. err = -ENOBUFS;
  1005. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  1006. if (new_fa == NULL)
  1007. goto out;
  1008. new_fa->fa_info = fi;
  1009. new_fa->fa_tos = tos;
  1010. new_fa->fa_type = cfg->fc_type;
  1011. new_fa->fa_state = 0;
  1012. new_fa->fa_slen = slen;
  1013. /* Insert new entry to the list. */
  1014. if (!l) {
  1015. l = fib_insert_node(t, key, plen);
  1016. if (unlikely(!l)) {
  1017. err = -ENOMEM;
  1018. goto out_free_new_fa;
  1019. }
  1020. }
  1021. if (!plen)
  1022. tb->tb_num_default++;
  1023. fib_insert_alias(l, fa, new_fa);
  1024. rt_cache_flush(cfg->fc_nlinfo.nl_net);
  1025. rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id,
  1026. &cfg->fc_nlinfo, 0);
  1027. succeeded:
  1028. return 0;
  1029. out_free_new_fa:
  1030. kmem_cache_free(fn_alias_kmem, new_fa);
  1031. out:
  1032. fib_release_info(fi);
  1033. err:
  1034. return err;
  1035. }
  1036. static inline t_key prefix_mismatch(t_key key, struct tnode *n)
  1037. {
  1038. t_key prefix = n->key;
  1039. return (key ^ prefix) & (prefix | -prefix);
  1040. }
  1041. /* should be called with rcu_read_lock */
  1042. int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
  1043. struct fib_result *res, int fib_flags)
  1044. {
  1045. struct trie *t = (struct trie *)tb->tb_data;
  1046. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1047. struct trie_use_stats __percpu *stats = t->stats;
  1048. #endif
  1049. const t_key key = ntohl(flp->daddr);
  1050. struct tnode *n, *pn;
  1051. struct fib_alias *fa;
  1052. t_key cindex;
  1053. n = rcu_dereference(t->trie);
  1054. if (!n)
  1055. return -EAGAIN;
  1056. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1057. this_cpu_inc(stats->gets);
  1058. #endif
  1059. pn = n;
  1060. cindex = 0;
  1061. /* Step 1: Travel to the longest prefix match in the trie */
  1062. for (;;) {
  1063. unsigned long index = get_index(key, n);
  1064. /* This bit of code is a bit tricky but it combines multiple
  1065. * checks into a single check. The prefix consists of the
  1066. * prefix plus zeros for the "bits" in the prefix. The index
  1067. * is the difference between the key and this value. From
  1068. * this we can actually derive several pieces of data.
  1069. * if (index & (~0ul << bits))
  1070. * we have a mismatch in skip bits and failed
  1071. * else
  1072. * we know the value is cindex
  1073. */
  1074. if (index & (~0ul << n->bits))
  1075. break;
  1076. /* we have found a leaf. Prefixes have already been compared */
  1077. if (IS_LEAF(n))
  1078. goto found;
  1079. /* only record pn and cindex if we are going to be chopping
  1080. * bits later. Otherwise we are just wasting cycles.
  1081. */
  1082. if (n->slen > n->pos) {
  1083. pn = n;
  1084. cindex = index;
  1085. }
  1086. n = tnode_get_child_rcu(n, index);
  1087. if (unlikely(!n))
  1088. goto backtrace;
  1089. }
  1090. /* Step 2: Sort out leaves and begin backtracing for longest prefix */
  1091. for (;;) {
  1092. /* record the pointer where our next node pointer is stored */
  1093. struct tnode __rcu **cptr = n->child;
  1094. /* This test verifies that none of the bits that differ
  1095. * between the key and the prefix exist in the region of
  1096. * the lsb and higher in the prefix.
  1097. */
  1098. if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
  1099. goto backtrace;
  1100. /* exit out and process leaf */
  1101. if (unlikely(IS_LEAF(n)))
  1102. break;
  1103. /* Don't bother recording parent info. Since we are in
  1104. * prefix match mode we will have to come back to wherever
  1105. * we started this traversal anyway
  1106. */
  1107. while ((n = rcu_dereference(*cptr)) == NULL) {
  1108. backtrace:
  1109. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1110. if (!n)
  1111. this_cpu_inc(stats->null_node_hit);
  1112. #endif
  1113. /* If we are at cindex 0 there are no more bits for
  1114. * us to strip at this level so we must ascend back
  1115. * up one level to see if there are any more bits to
  1116. * be stripped there.
  1117. */
  1118. while (!cindex) {
  1119. t_key pkey = pn->key;
  1120. pn = node_parent_rcu(pn);
  1121. if (unlikely(!pn))
  1122. return -EAGAIN;
  1123. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1124. this_cpu_inc(stats->backtrack);
  1125. #endif
  1126. /* Get Child's index */
  1127. cindex = get_index(pkey, pn);
  1128. }
  1129. /* strip the least significant bit from the cindex */
  1130. cindex &= cindex - 1;
  1131. /* grab pointer for next child node */
  1132. cptr = &pn->child[cindex];
  1133. }
  1134. }
  1135. found:
  1136. /* Step 3: Process the leaf, if that fails fall back to backtracing */
  1137. hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
  1138. struct fib_info *fi = fa->fa_info;
  1139. int nhsel, err;
  1140. if (((key ^ n->key) >= (1ul << fa->fa_slen)) &&
  1141. ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen != KEYLENGTH)))
  1142. continue;
  1143. if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
  1144. continue;
  1145. if (fi->fib_dead)
  1146. continue;
  1147. if (fa->fa_info->fib_scope < flp->flowi4_scope)
  1148. continue;
  1149. fib_alias_accessed(fa);
  1150. err = fib_props[fa->fa_type].error;
  1151. if (unlikely(err < 0)) {
  1152. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1153. this_cpu_inc(stats->semantic_match_passed);
  1154. #endif
  1155. return err;
  1156. }
  1157. if (fi->fib_flags & RTNH_F_DEAD)
  1158. continue;
  1159. for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
  1160. const struct fib_nh *nh = &fi->fib_nh[nhsel];
  1161. if (nh->nh_flags & RTNH_F_DEAD)
  1162. continue;
  1163. if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
  1164. continue;
  1165. if (!(fib_flags & FIB_LOOKUP_NOREF))
  1166. atomic_inc(&fi->fib_clntref);
  1167. res->prefixlen = KEYLENGTH - fa->fa_slen;
  1168. res->nh_sel = nhsel;
  1169. res->type = fa->fa_type;
  1170. res->scope = fi->fib_scope;
  1171. res->fi = fi;
  1172. res->table = tb;
  1173. res->fa_head = &n->leaf;
  1174. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1175. this_cpu_inc(stats->semantic_match_passed);
  1176. #endif
  1177. return err;
  1178. }
  1179. }
  1180. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1181. this_cpu_inc(stats->semantic_match_miss);
  1182. #endif
  1183. goto backtrace;
  1184. }
  1185. EXPORT_SYMBOL_GPL(fib_table_lookup);
  1186. /*
  1187. * Caller must hold RTNL.
  1188. */
  1189. int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
  1190. {
  1191. struct trie *t = (struct trie *) tb->tb_data;
  1192. struct fib_alias *fa, *fa_to_delete;
  1193. u8 plen = cfg->fc_dst_len;
  1194. u8 tos = cfg->fc_tos;
  1195. u8 slen = KEYLENGTH - plen;
  1196. struct tnode *l;
  1197. u32 key, mask;
  1198. if (plen > KEYLENGTH)
  1199. return -EINVAL;
  1200. key = ntohl(cfg->fc_dst);
  1201. mask = ntohl(inet_make_mask(plen));
  1202. if (key & ~mask)
  1203. return -EINVAL;
  1204. l = fib_find_node(t, key);
  1205. if (!l)
  1206. return -ESRCH;
  1207. fa = fib_find_alias(&l->leaf, slen, tos, 0);
  1208. if (!fa)
  1209. return -ESRCH;
  1210. pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
  1211. fa_to_delete = NULL;
  1212. hlist_for_each_entry_from(fa, fa_list) {
  1213. struct fib_info *fi = fa->fa_info;
  1214. if ((fa->fa_slen != slen) || (fa->fa_tos != tos))
  1215. break;
  1216. if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
  1217. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  1218. fa->fa_info->fib_scope == cfg->fc_scope) &&
  1219. (!cfg->fc_prefsrc ||
  1220. fi->fib_prefsrc == cfg->fc_prefsrc) &&
  1221. (!cfg->fc_protocol ||
  1222. fi->fib_protocol == cfg->fc_protocol) &&
  1223. fib_nh_match(cfg, fi) == 0) {
  1224. fa_to_delete = fa;
  1225. break;
  1226. }
  1227. }
  1228. if (!fa_to_delete)
  1229. return -ESRCH;
  1230. fa = fa_to_delete;
  1231. rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id,
  1232. &cfg->fc_nlinfo, 0);
  1233. fib_remove_alias(l, fa);
  1234. if (!plen)
  1235. tb->tb_num_default--;
  1236. if (hlist_empty(&l->leaf)) {
  1237. struct tnode *tp = node_parent(l);
  1238. if (tp) {
  1239. put_child(tp, get_index(l->key, tp), NULL);
  1240. trie_rebalance(t, tp);
  1241. } else {
  1242. RCU_INIT_POINTER(t->trie, NULL);
  1243. }
  1244. node_free(l);
  1245. }
  1246. if (fa->fa_state & FA_S_ACCESSED)
  1247. rt_cache_flush(cfg->fc_nlinfo.nl_net);
  1248. fib_release_info(fa->fa_info);
  1249. alias_free_mem_rcu(fa);
  1250. return 0;
  1251. }
  1252. /* Scan for the next right leaf starting at node p->child[idx]
  1253. * Since we have back pointer, no recursion necessary.
  1254. */
  1255. static struct tnode *leaf_walk_rcu(struct tnode *p, struct tnode *c)
  1256. {
  1257. do {
  1258. unsigned long idx = c ? idx = get_index(c->key, p) + 1 : 0;
  1259. while (idx < tnode_child_length(p)) {
  1260. c = tnode_get_child_rcu(p, idx++);
  1261. if (!c)
  1262. continue;
  1263. if (IS_LEAF(c))
  1264. return c;
  1265. /* Rescan start scanning in new node */
  1266. p = c;
  1267. idx = 0;
  1268. }
  1269. /* Node empty, walk back up to parent */
  1270. c = p;
  1271. } while ((p = node_parent_rcu(c)) != NULL);
  1272. return NULL; /* Root of trie */
  1273. }
  1274. static struct tnode *trie_firstleaf(struct trie *t)
  1275. {
  1276. struct tnode *n = rcu_dereference_rtnl(t->trie);
  1277. if (!n)
  1278. return NULL;
  1279. if (IS_LEAF(n)) /* trie is just a leaf */
  1280. return n;
  1281. return leaf_walk_rcu(n, NULL);
  1282. }
  1283. static struct tnode *trie_nextleaf(struct tnode *l)
  1284. {
  1285. struct tnode *p = node_parent_rcu(l);
  1286. if (!p)
  1287. return NULL; /* trie with just one leaf */
  1288. return leaf_walk_rcu(p, l);
  1289. }
  1290. static struct tnode *trie_leafindex(struct trie *t, int index)
  1291. {
  1292. struct tnode *l = trie_firstleaf(t);
  1293. while (l && index-- > 0)
  1294. l = trie_nextleaf(l);
  1295. return l;
  1296. }
  1297. /*
  1298. * Caller must hold RTNL.
  1299. */
  1300. int fib_table_flush(struct fib_table *tb)
  1301. {
  1302. struct trie *t = (struct trie *)tb->tb_data;
  1303. struct hlist_node *tmp;
  1304. struct fib_alias *fa;
  1305. struct tnode *n, *pn;
  1306. unsigned long cindex;
  1307. unsigned char slen;
  1308. int found = 0;
  1309. n = rcu_dereference(t->trie);
  1310. if (!n)
  1311. goto flush_complete;
  1312. pn = NULL;
  1313. cindex = 0;
  1314. while (IS_TNODE(n)) {
  1315. /* record pn and cindex for leaf walking */
  1316. pn = n;
  1317. cindex = 1ul << n->bits;
  1318. backtrace:
  1319. /* walk trie in reverse order */
  1320. do {
  1321. while (!(cindex--)) {
  1322. t_key pkey = pn->key;
  1323. n = pn;
  1324. pn = node_parent(n);
  1325. /* resize completed node */
  1326. resize(t, n);
  1327. /* if we got the root we are done */
  1328. if (!pn)
  1329. goto flush_complete;
  1330. cindex = get_index(pkey, pn);
  1331. }
  1332. /* grab the next available node */
  1333. n = tnode_get_child(pn, cindex);
  1334. } while (!n);
  1335. }
  1336. /* track slen in case any prefixes survive */
  1337. slen = 0;
  1338. hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
  1339. struct fib_info *fi = fa->fa_info;
  1340. if (fi && (fi->fib_flags & RTNH_F_DEAD)) {
  1341. hlist_del_rcu(&fa->fa_list);
  1342. fib_release_info(fa->fa_info);
  1343. alias_free_mem_rcu(fa);
  1344. found++;
  1345. continue;
  1346. }
  1347. slen = fa->fa_slen;
  1348. }
  1349. /* update leaf slen */
  1350. n->slen = slen;
  1351. if (hlist_empty(&n->leaf)) {
  1352. put_child_root(pn, t, n->key, NULL);
  1353. node_free(n);
  1354. } else {
  1355. leaf_pull_suffix(n);
  1356. }
  1357. /* if trie is leaf only loop is completed */
  1358. if (pn)
  1359. goto backtrace;
  1360. flush_complete:
  1361. pr_debug("trie_flush found=%d\n", found);
  1362. return found;
  1363. }
  1364. void fib_free_table(struct fib_table *tb)
  1365. {
  1366. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1367. struct trie *t = (struct trie *)tb->tb_data;
  1368. free_percpu(t->stats);
  1369. #endif /* CONFIG_IP_FIB_TRIE_STATS */
  1370. kfree(tb);
  1371. }
  1372. static int fn_trie_dump_leaf(struct tnode *l, struct fib_table *tb,
  1373. struct sk_buff *skb, struct netlink_callback *cb)
  1374. {
  1375. __be32 xkey = htonl(l->key);
  1376. struct fib_alias *fa;
  1377. int i, s_i;
  1378. s_i = cb->args[4];
  1379. i = 0;
  1380. /* rcu_read_lock is hold by caller */
  1381. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  1382. if (i < s_i) {
  1383. i++;
  1384. continue;
  1385. }
  1386. if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
  1387. cb->nlh->nlmsg_seq,
  1388. RTM_NEWROUTE,
  1389. tb->tb_id,
  1390. fa->fa_type,
  1391. xkey,
  1392. KEYLENGTH - fa->fa_slen,
  1393. fa->fa_tos,
  1394. fa->fa_info, NLM_F_MULTI) < 0) {
  1395. cb->args[4] = i;
  1396. return -1;
  1397. }
  1398. i++;
  1399. }
  1400. cb->args[4] = i;
  1401. return skb->len;
  1402. }
  1403. int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
  1404. struct netlink_callback *cb)
  1405. {
  1406. struct tnode *l;
  1407. struct trie *t = (struct trie *) tb->tb_data;
  1408. t_key key = cb->args[2];
  1409. int count = cb->args[3];
  1410. rcu_read_lock();
  1411. /* Dump starting at last key.
  1412. * Note: 0.0.0.0/0 (ie default) is first key.
  1413. */
  1414. if (count == 0)
  1415. l = trie_firstleaf(t);
  1416. else {
  1417. /* Normally, continue from last key, but if that is missing
  1418. * fallback to using slow rescan
  1419. */
  1420. l = fib_find_node(t, key);
  1421. if (!l)
  1422. l = trie_leafindex(t, count);
  1423. }
  1424. while (l) {
  1425. cb->args[2] = l->key;
  1426. if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
  1427. cb->args[3] = count;
  1428. rcu_read_unlock();
  1429. return -1;
  1430. }
  1431. ++count;
  1432. l = trie_nextleaf(l);
  1433. memset(&cb->args[4], 0,
  1434. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  1435. }
  1436. cb->args[3] = count;
  1437. rcu_read_unlock();
  1438. return skb->len;
  1439. }
  1440. void __init fib_trie_init(void)
  1441. {
  1442. fn_alias_kmem = kmem_cache_create("ip_fib_alias",
  1443. sizeof(struct fib_alias),
  1444. 0, SLAB_PANIC, NULL);
  1445. trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
  1446. sizeof(struct tnode),
  1447. 0, SLAB_PANIC, NULL);
  1448. }
  1449. struct fib_table *fib_trie_table(u32 id)
  1450. {
  1451. struct fib_table *tb;
  1452. struct trie *t;
  1453. tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie),
  1454. GFP_KERNEL);
  1455. if (tb == NULL)
  1456. return NULL;
  1457. tb->tb_id = id;
  1458. tb->tb_default = -1;
  1459. tb->tb_num_default = 0;
  1460. t = (struct trie *) tb->tb_data;
  1461. RCU_INIT_POINTER(t->trie, NULL);
  1462. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1463. t->stats = alloc_percpu(struct trie_use_stats);
  1464. if (!t->stats) {
  1465. kfree(tb);
  1466. tb = NULL;
  1467. }
  1468. #endif
  1469. return tb;
  1470. }
  1471. #ifdef CONFIG_PROC_FS
  1472. /* Depth first Trie walk iterator */
  1473. struct fib_trie_iter {
  1474. struct seq_net_private p;
  1475. struct fib_table *tb;
  1476. struct tnode *tnode;
  1477. unsigned int index;
  1478. unsigned int depth;
  1479. };
  1480. static struct tnode *fib_trie_get_next(struct fib_trie_iter *iter)
  1481. {
  1482. unsigned long cindex = iter->index;
  1483. struct tnode *tn = iter->tnode;
  1484. struct tnode *p;
  1485. /* A single entry routing table */
  1486. if (!tn)
  1487. return NULL;
  1488. pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
  1489. iter->tnode, iter->index, iter->depth);
  1490. rescan:
  1491. while (cindex < tnode_child_length(tn)) {
  1492. struct tnode *n = tnode_get_child_rcu(tn, cindex);
  1493. if (n) {
  1494. if (IS_LEAF(n)) {
  1495. iter->tnode = tn;
  1496. iter->index = cindex + 1;
  1497. } else {
  1498. /* push down one level */
  1499. iter->tnode = n;
  1500. iter->index = 0;
  1501. ++iter->depth;
  1502. }
  1503. return n;
  1504. }
  1505. ++cindex;
  1506. }
  1507. /* Current node exhausted, pop back up */
  1508. p = node_parent_rcu(tn);
  1509. if (p) {
  1510. cindex = get_index(tn->key, p) + 1;
  1511. tn = p;
  1512. --iter->depth;
  1513. goto rescan;
  1514. }
  1515. /* got root? */
  1516. return NULL;
  1517. }
  1518. static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter,
  1519. struct trie *t)
  1520. {
  1521. struct tnode *n;
  1522. if (!t)
  1523. return NULL;
  1524. n = rcu_dereference(t->trie);
  1525. if (!n)
  1526. return NULL;
  1527. if (IS_TNODE(n)) {
  1528. iter->tnode = n;
  1529. iter->index = 0;
  1530. iter->depth = 1;
  1531. } else {
  1532. iter->tnode = NULL;
  1533. iter->index = 0;
  1534. iter->depth = 0;
  1535. }
  1536. return n;
  1537. }
  1538. static void trie_collect_stats(struct trie *t, struct trie_stat *s)
  1539. {
  1540. struct tnode *n;
  1541. struct fib_trie_iter iter;
  1542. memset(s, 0, sizeof(*s));
  1543. rcu_read_lock();
  1544. for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
  1545. if (IS_LEAF(n)) {
  1546. struct fib_alias *fa;
  1547. s->leaves++;
  1548. s->totdepth += iter.depth;
  1549. if (iter.depth > s->maxdepth)
  1550. s->maxdepth = iter.depth;
  1551. hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
  1552. ++s->prefixes;
  1553. } else {
  1554. s->tnodes++;
  1555. if (n->bits < MAX_STAT_DEPTH)
  1556. s->nodesizes[n->bits]++;
  1557. s->nullpointers += n->empty_children;
  1558. }
  1559. }
  1560. rcu_read_unlock();
  1561. }
  1562. /*
  1563. * This outputs /proc/net/fib_triestats
  1564. */
  1565. static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
  1566. {
  1567. unsigned int i, max, pointers, bytes, avdepth;
  1568. if (stat->leaves)
  1569. avdepth = stat->totdepth*100 / stat->leaves;
  1570. else
  1571. avdepth = 0;
  1572. seq_printf(seq, "\tAver depth: %u.%02d\n",
  1573. avdepth / 100, avdepth % 100);
  1574. seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
  1575. seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
  1576. bytes = sizeof(struct tnode) * stat->leaves;
  1577. seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
  1578. bytes += sizeof(struct fib_alias) * stat->prefixes;
  1579. seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
  1580. bytes += sizeof(struct tnode) * stat->tnodes;
  1581. max = MAX_STAT_DEPTH;
  1582. while (max > 0 && stat->nodesizes[max-1] == 0)
  1583. max--;
  1584. pointers = 0;
  1585. for (i = 1; i < max; i++)
  1586. if (stat->nodesizes[i] != 0) {
  1587. seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
  1588. pointers += (1<<i) * stat->nodesizes[i];
  1589. }
  1590. seq_putc(seq, '\n');
  1591. seq_printf(seq, "\tPointers: %u\n", pointers);
  1592. bytes += sizeof(struct tnode *) * pointers;
  1593. seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
  1594. seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
  1595. }
  1596. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1597. static void trie_show_usage(struct seq_file *seq,
  1598. const struct trie_use_stats __percpu *stats)
  1599. {
  1600. struct trie_use_stats s = { 0 };
  1601. int cpu;
  1602. /* loop through all of the CPUs and gather up the stats */
  1603. for_each_possible_cpu(cpu) {
  1604. const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
  1605. s.gets += pcpu->gets;
  1606. s.backtrack += pcpu->backtrack;
  1607. s.semantic_match_passed += pcpu->semantic_match_passed;
  1608. s.semantic_match_miss += pcpu->semantic_match_miss;
  1609. s.null_node_hit += pcpu->null_node_hit;
  1610. s.resize_node_skipped += pcpu->resize_node_skipped;
  1611. }
  1612. seq_printf(seq, "\nCounters:\n---------\n");
  1613. seq_printf(seq, "gets = %u\n", s.gets);
  1614. seq_printf(seq, "backtracks = %u\n", s.backtrack);
  1615. seq_printf(seq, "semantic match passed = %u\n",
  1616. s.semantic_match_passed);
  1617. seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
  1618. seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
  1619. seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
  1620. }
  1621. #endif /* CONFIG_IP_FIB_TRIE_STATS */
  1622. static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
  1623. {
  1624. if (tb->tb_id == RT_TABLE_LOCAL)
  1625. seq_puts(seq, "Local:\n");
  1626. else if (tb->tb_id == RT_TABLE_MAIN)
  1627. seq_puts(seq, "Main:\n");
  1628. else
  1629. seq_printf(seq, "Id %d:\n", tb->tb_id);
  1630. }
  1631. static int fib_triestat_seq_show(struct seq_file *seq, void *v)
  1632. {
  1633. struct net *net = (struct net *)seq->private;
  1634. unsigned int h;
  1635. seq_printf(seq,
  1636. "Basic info: size of leaf:"
  1637. " %Zd bytes, size of tnode: %Zd bytes.\n",
  1638. sizeof(struct tnode), sizeof(struct tnode));
  1639. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1640. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  1641. struct fib_table *tb;
  1642. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  1643. struct trie *t = (struct trie *) tb->tb_data;
  1644. struct trie_stat stat;
  1645. if (!t)
  1646. continue;
  1647. fib_table_print(seq, tb);
  1648. trie_collect_stats(t, &stat);
  1649. trie_show_stats(seq, &stat);
  1650. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1651. trie_show_usage(seq, t->stats);
  1652. #endif
  1653. }
  1654. }
  1655. return 0;
  1656. }
  1657. static int fib_triestat_seq_open(struct inode *inode, struct file *file)
  1658. {
  1659. return single_open_net(inode, file, fib_triestat_seq_show);
  1660. }
  1661. static const struct file_operations fib_triestat_fops = {
  1662. .owner = THIS_MODULE,
  1663. .open = fib_triestat_seq_open,
  1664. .read = seq_read,
  1665. .llseek = seq_lseek,
  1666. .release = single_release_net,
  1667. };
  1668. static struct tnode *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
  1669. {
  1670. struct fib_trie_iter *iter = seq->private;
  1671. struct net *net = seq_file_net(seq);
  1672. loff_t idx = 0;
  1673. unsigned int h;
  1674. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1675. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  1676. struct fib_table *tb;
  1677. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  1678. struct tnode *n;
  1679. for (n = fib_trie_get_first(iter,
  1680. (struct trie *) tb->tb_data);
  1681. n; n = fib_trie_get_next(iter))
  1682. if (pos == idx++) {
  1683. iter->tb = tb;
  1684. return n;
  1685. }
  1686. }
  1687. }
  1688. return NULL;
  1689. }
  1690. static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
  1691. __acquires(RCU)
  1692. {
  1693. rcu_read_lock();
  1694. return fib_trie_get_idx(seq, *pos);
  1695. }
  1696. static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1697. {
  1698. struct fib_trie_iter *iter = seq->private;
  1699. struct net *net = seq_file_net(seq);
  1700. struct fib_table *tb = iter->tb;
  1701. struct hlist_node *tb_node;
  1702. unsigned int h;
  1703. struct tnode *n;
  1704. ++*pos;
  1705. /* next node in same table */
  1706. n = fib_trie_get_next(iter);
  1707. if (n)
  1708. return n;
  1709. /* walk rest of this hash chain */
  1710. h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
  1711. while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
  1712. tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
  1713. n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
  1714. if (n)
  1715. goto found;
  1716. }
  1717. /* new hash chain */
  1718. while (++h < FIB_TABLE_HASHSZ) {
  1719. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  1720. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  1721. n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
  1722. if (n)
  1723. goto found;
  1724. }
  1725. }
  1726. return NULL;
  1727. found:
  1728. iter->tb = tb;
  1729. return n;
  1730. }
  1731. static void fib_trie_seq_stop(struct seq_file *seq, void *v)
  1732. __releases(RCU)
  1733. {
  1734. rcu_read_unlock();
  1735. }
  1736. static void seq_indent(struct seq_file *seq, int n)
  1737. {
  1738. while (n-- > 0)
  1739. seq_puts(seq, " ");
  1740. }
  1741. static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
  1742. {
  1743. switch (s) {
  1744. case RT_SCOPE_UNIVERSE: return "universe";
  1745. case RT_SCOPE_SITE: return "site";
  1746. case RT_SCOPE_LINK: return "link";
  1747. case RT_SCOPE_HOST: return "host";
  1748. case RT_SCOPE_NOWHERE: return "nowhere";
  1749. default:
  1750. snprintf(buf, len, "scope=%d", s);
  1751. return buf;
  1752. }
  1753. }
  1754. static const char *const rtn_type_names[__RTN_MAX] = {
  1755. [RTN_UNSPEC] = "UNSPEC",
  1756. [RTN_UNICAST] = "UNICAST",
  1757. [RTN_LOCAL] = "LOCAL",
  1758. [RTN_BROADCAST] = "BROADCAST",
  1759. [RTN_ANYCAST] = "ANYCAST",
  1760. [RTN_MULTICAST] = "MULTICAST",
  1761. [RTN_BLACKHOLE] = "BLACKHOLE",
  1762. [RTN_UNREACHABLE] = "UNREACHABLE",
  1763. [RTN_PROHIBIT] = "PROHIBIT",
  1764. [RTN_THROW] = "THROW",
  1765. [RTN_NAT] = "NAT",
  1766. [RTN_XRESOLVE] = "XRESOLVE",
  1767. };
  1768. static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
  1769. {
  1770. if (t < __RTN_MAX && rtn_type_names[t])
  1771. return rtn_type_names[t];
  1772. snprintf(buf, len, "type %u", t);
  1773. return buf;
  1774. }
  1775. /* Pretty print the trie */
  1776. static int fib_trie_seq_show(struct seq_file *seq, void *v)
  1777. {
  1778. const struct fib_trie_iter *iter = seq->private;
  1779. struct tnode *n = v;
  1780. if (!node_parent_rcu(n))
  1781. fib_table_print(seq, iter->tb);
  1782. if (IS_TNODE(n)) {
  1783. __be32 prf = htonl(n->key);
  1784. seq_indent(seq, iter->depth-1);
  1785. seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
  1786. &prf, KEYLENGTH - n->pos - n->bits, n->bits,
  1787. n->full_children, n->empty_children);
  1788. } else {
  1789. __be32 val = htonl(n->key);
  1790. struct fib_alias *fa;
  1791. seq_indent(seq, iter->depth);
  1792. seq_printf(seq, " |-- %pI4\n", &val);
  1793. hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
  1794. char buf1[32], buf2[32];
  1795. seq_indent(seq, iter->depth + 1);
  1796. seq_printf(seq, " /%zu %s %s",
  1797. KEYLENGTH - fa->fa_slen,
  1798. rtn_scope(buf1, sizeof(buf1),
  1799. fa->fa_info->fib_scope),
  1800. rtn_type(buf2, sizeof(buf2),
  1801. fa->fa_type));
  1802. if (fa->fa_tos)
  1803. seq_printf(seq, " tos=%d", fa->fa_tos);
  1804. seq_putc(seq, '\n');
  1805. }
  1806. }
  1807. return 0;
  1808. }
  1809. static const struct seq_operations fib_trie_seq_ops = {
  1810. .start = fib_trie_seq_start,
  1811. .next = fib_trie_seq_next,
  1812. .stop = fib_trie_seq_stop,
  1813. .show = fib_trie_seq_show,
  1814. };
  1815. static int fib_trie_seq_open(struct inode *inode, struct file *file)
  1816. {
  1817. return seq_open_net(inode, file, &fib_trie_seq_ops,
  1818. sizeof(struct fib_trie_iter));
  1819. }
  1820. static const struct file_operations fib_trie_fops = {
  1821. .owner = THIS_MODULE,
  1822. .open = fib_trie_seq_open,
  1823. .read = seq_read,
  1824. .llseek = seq_lseek,
  1825. .release = seq_release_net,
  1826. };
  1827. struct fib_route_iter {
  1828. struct seq_net_private p;
  1829. struct trie *main_trie;
  1830. loff_t pos;
  1831. t_key key;
  1832. };
  1833. static struct tnode *fib_route_get_idx(struct fib_route_iter *iter, loff_t pos)
  1834. {
  1835. struct tnode *l = NULL;
  1836. struct trie *t = iter->main_trie;
  1837. /* use cache location of last found key */
  1838. if (iter->pos > 0 && pos >= iter->pos && (l = fib_find_node(t, iter->key)))
  1839. pos -= iter->pos;
  1840. else {
  1841. iter->pos = 0;
  1842. l = trie_firstleaf(t);
  1843. }
  1844. while (l && pos-- > 0) {
  1845. iter->pos++;
  1846. l = trie_nextleaf(l);
  1847. }
  1848. if (l)
  1849. iter->key = pos; /* remember it */
  1850. else
  1851. iter->pos = 0; /* forget it */
  1852. return l;
  1853. }
  1854. static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
  1855. __acquires(RCU)
  1856. {
  1857. struct fib_route_iter *iter = seq->private;
  1858. struct fib_table *tb;
  1859. rcu_read_lock();
  1860. tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
  1861. if (!tb)
  1862. return NULL;
  1863. iter->main_trie = (struct trie *) tb->tb_data;
  1864. if (*pos == 0)
  1865. return SEQ_START_TOKEN;
  1866. else
  1867. return fib_route_get_idx(iter, *pos - 1);
  1868. }
  1869. static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1870. {
  1871. struct fib_route_iter *iter = seq->private;
  1872. struct tnode *l = v;
  1873. ++*pos;
  1874. if (v == SEQ_START_TOKEN) {
  1875. iter->pos = 0;
  1876. l = trie_firstleaf(iter->main_trie);
  1877. } else {
  1878. iter->pos++;
  1879. l = trie_nextleaf(l);
  1880. }
  1881. if (l)
  1882. iter->key = l->key;
  1883. else
  1884. iter->pos = 0;
  1885. return l;
  1886. }
  1887. static void fib_route_seq_stop(struct seq_file *seq, void *v)
  1888. __releases(RCU)
  1889. {
  1890. rcu_read_unlock();
  1891. }
  1892. static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
  1893. {
  1894. unsigned int flags = 0;
  1895. if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
  1896. flags = RTF_REJECT;
  1897. if (fi && fi->fib_nh->nh_gw)
  1898. flags |= RTF_GATEWAY;
  1899. if (mask == htonl(0xFFFFFFFF))
  1900. flags |= RTF_HOST;
  1901. flags |= RTF_UP;
  1902. return flags;
  1903. }
  1904. /*
  1905. * This outputs /proc/net/route.
  1906. * The format of the file is not supposed to be changed
  1907. * and needs to be same as fib_hash output to avoid breaking
  1908. * legacy utilities
  1909. */
  1910. static int fib_route_seq_show(struct seq_file *seq, void *v)
  1911. {
  1912. struct fib_alias *fa;
  1913. struct tnode *l = v;
  1914. __be32 prefix;
  1915. if (v == SEQ_START_TOKEN) {
  1916. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  1917. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  1918. "\tWindow\tIRTT");
  1919. return 0;
  1920. }
  1921. prefix = htonl(l->key);
  1922. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  1923. const struct fib_info *fi = fa->fa_info;
  1924. __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
  1925. unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
  1926. if ((fa->fa_type == RTN_BROADCAST) ||
  1927. (fa->fa_type == RTN_MULTICAST))
  1928. continue;
  1929. seq_setwidth(seq, 127);
  1930. if (fi)
  1931. seq_printf(seq,
  1932. "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
  1933. "%d\t%08X\t%d\t%u\t%u",
  1934. fi->fib_dev ? fi->fib_dev->name : "*",
  1935. prefix,
  1936. fi->fib_nh->nh_gw, flags, 0, 0,
  1937. fi->fib_priority,
  1938. mask,
  1939. (fi->fib_advmss ?
  1940. fi->fib_advmss + 40 : 0),
  1941. fi->fib_window,
  1942. fi->fib_rtt >> 3);
  1943. else
  1944. seq_printf(seq,
  1945. "*\t%08X\t%08X\t%04X\t%d\t%u\t"
  1946. "%d\t%08X\t%d\t%u\t%u",
  1947. prefix, 0, flags, 0, 0, 0,
  1948. mask, 0, 0, 0);
  1949. seq_pad(seq, '\n');
  1950. }
  1951. return 0;
  1952. }
  1953. static const struct seq_operations fib_route_seq_ops = {
  1954. .start = fib_route_seq_start,
  1955. .next = fib_route_seq_next,
  1956. .stop = fib_route_seq_stop,
  1957. .show = fib_route_seq_show,
  1958. };
  1959. static int fib_route_seq_open(struct inode *inode, struct file *file)
  1960. {
  1961. return seq_open_net(inode, file, &fib_route_seq_ops,
  1962. sizeof(struct fib_route_iter));
  1963. }
  1964. static const struct file_operations fib_route_fops = {
  1965. .owner = THIS_MODULE,
  1966. .open = fib_route_seq_open,
  1967. .read = seq_read,
  1968. .llseek = seq_lseek,
  1969. .release = seq_release_net,
  1970. };
  1971. int __net_init fib_proc_init(struct net *net)
  1972. {
  1973. if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
  1974. goto out1;
  1975. if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
  1976. &fib_triestat_fops))
  1977. goto out2;
  1978. if (!proc_create("route", S_IRUGO, net->proc_net, &fib_route_fops))
  1979. goto out3;
  1980. return 0;
  1981. out3:
  1982. remove_proc_entry("fib_triestat", net->proc_net);
  1983. out2:
  1984. remove_proc_entry("fib_trie", net->proc_net);
  1985. out1:
  1986. return -ENOMEM;
  1987. }
  1988. void __net_exit fib_proc_exit(struct net *net)
  1989. {
  1990. remove_proc_entry("fib_trie", net->proc_net);
  1991. remove_proc_entry("fib_triestat", net->proc_net);
  1992. remove_proc_entry("route", net->proc_net);
  1993. }
  1994. #endif /* CONFIG_PROC_FS */