fib_trie.c 59 KB

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