fib_trie.c 59 KB

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