fib_trie.c 59 KB

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