fib_trie.c 58 KB

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