fib_trie.c 62 KB

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