fib_trie.c 61 KB

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