fib_trie.c 60 KB

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