fib_trie.c 58 KB

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